Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use CAS for safer counter collection #394

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions kamon-core/src/main/java/kamon/jsr166/LongAdder.java
Expand Up @@ -151,6 +151,21 @@ public long sumThenReset() {
return sum;
}

public long sumAndReset() {
long sum = getAndSetBase(0L);
Cell[] as = cells;
if (as != null) {
int n = as.length;
for (int i = 0; i < n; ++i) {
Cell a = as[i];
if (a != null) {
sum += a.getAndSet(0L);
}
}
}
return sum;
}

/**
* Returns the String representation of the {@link #sum}.
* @return the String representation of the {@link #sum}
Expand Down
11 changes: 11 additions & 0 deletions kamon-core/src/main/java/kamon/jsr166/Striped64.java
Expand Up @@ -105,6 +105,10 @@ final boolean cas(long cmp, long val) {
return UNSAFE.compareAndSwapLong(this, valueOffset, cmp, val);
}

final long getAndSet(long val) {
return UNSAFE.getAndSetLong(this, valueOffset, val);
}

// Unsafe mechanics
private static final sun.misc.Unsafe UNSAFE;
private static final long valueOffset;
Expand Down Expand Up @@ -181,6 +185,13 @@ final boolean casBase(long cmp, long val) {
return UNSAFE.compareAndSwapLong(this, baseOffset, cmp, val);
}

/**
* CASes the base field.
Copy link
Contributor

@dpsoft dpsoft Sep 28, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@isaacl This code are using Java 8 Unsafe intrinsics (in this case lock xchg) and isn't supported in prior java versions.
Maybe we could check if support getAndSetObject in this way Unsafe.class.getMethod("getAndSetObject", Object.class, Long.TYPE,Object.class) and use this method, otherwise fallback to something like:

 final long getAndSetBase(long val) {
  long v;
     do {
          v = UNSAFE.getLongVolatile(this, baseOffset);
      } while (!UNSAFE.compareAndSwapLong(this, baseOffset, v, val));
  return v;
 }

or simply use the code above (CAS version, I do not know if the code compiles ;)) and the same for the final long getAndSet(long val)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If getAndSetLong was added too recently I'd suggest adding the function as a static helper, as it's needed for both the base get and the cell get.

The code you wrote is identical to the actual impl.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, is a good idea!

*/
final long getAndSetBase(long val) {
return UNSAFE.getAndSetLong(this, baseOffset, val);
}

/**
* CASes the busy field from 0 to 1 to acquire lock.
*/
Expand Down
Expand Up @@ -48,7 +48,7 @@ class LongAdderCounter extends Counter {
counter.add(times)
}

def collect(context: CollectionContext): Counter.Snapshot = CounterSnapshot(counter.sumThenReset())
def collect(context: CollectionContext): Counter.Snapshot = CounterSnapshot(counter.sumAndReset())

def cleanup: Unit = {}
}
Expand Down