Skip to content

Commit

Permalink
StatisticsHelper: Remove attempts to "setDaemon" the stats polling th…
Browse files Browse the repository at this point in the history
…read

And update memory status one final time in stopStatistics()

Closes mikemccand#24
  • Loading branch information
dsmiley committed Sep 20, 2018
1 parent 4b2d0cd commit 968de85
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions src/main/perf/StatisticsHelper.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** Poached from cometd 2.1.0, Apache 2 license */
/* Poached from cometd 2.1.0, Apache 2 license */

package perf;

Expand All @@ -18,7 +18,8 @@
import java.util.concurrent.atomic.AtomicInteger;

/**
* @version $Revision$ $Date$
* See <a href="https://github.com/cometd/cometd/blob/2.4.x/cometd-java/cometd-java-examples/src/main/java/org/cometd/benchmark/BenchmarkHelper.java"
* >BenchmarkHelper in CometD</a>.
*/
public class StatisticsHelper implements Runnable {

Expand All @@ -30,6 +31,8 @@ public class StatisticsHelper implements Runnable {

private final AtomicInteger starts = new AtomicInteger();

// TODO reconsider "volatile" everywhere here; we don't need synchronization based on how we use this

private volatile MemoryPoolMXBean youngMemoryPool;

private volatile MemoryPoolMXBean survivorMemoryPool;
Expand Down Expand Up @@ -112,23 +115,11 @@ public StatisticsHelper() {

@Override
public void run() {

// So we don't prevent process exit if this
// is the only thread left:
Thread.currentThread().setDaemon(true);

if (!hasMemoryPools)
return;

// So we don't prevent process exit if this
// is the only thread left:
Thread.currentThread().setDaemon(true);

long young = youngMemoryPool.getUsage().getUsed();
long survivor = survivorMemoryPool.getUsage().getUsed();
long old = oldMemoryPool.getUsage().getUsed();

if (!polling) {
if (!polling) { // in initial state; don't save deltas
polling = true;
} else {
if (lastYoungUsed <= young) {
Expand Down Expand Up @@ -190,9 +181,11 @@ public boolean startStatistics() {
}
System.err.println("- - - - - - - - - - - - - - - - - - - - ");

scheduler = Executors.newSingleThreadScheduledExecutor();
polling = false;
memoryPoller = scheduler.scheduleWithFixedDelay(this, 0, 250, TimeUnit.MILLISECONDS);
if (hasMemoryPools) {
scheduler = Executors.newSingleThreadScheduledExecutor();
polling = false;
memoryPoller = scheduler.scheduleWithFixedDelay(this, 0, 250, TimeUnit.MILLISECONDS);
}

lastYoungUsed = 0;
if (hasCollectors) {
Expand Down Expand Up @@ -225,8 +218,13 @@ public boolean stopStatistics() {
if (starts.decrementAndGet() > 0)
return false;

memoryPoller.cancel(false);
scheduler.shutdown();
if (scheduler != null) {
memoryPoller.cancel(false);
scheduler.shutdown();
}
if (hasMemoryPools) {
run();// update latest stats
}

System.err.println("- - - - - - - - - - - - - - - - - - - - ");
System.err.println("Statistics Ended at " + new Date());
Expand Down

0 comments on commit 968de85

Please sign in to comment.