Skip to content

Commit

Permalink
change code format and delete StopWatch.java
Browse files Browse the repository at this point in the history
  • Loading branch information
ChangerYoung committed Oct 16, 2017
1 parent 78d7fa0 commit 1dc7ee9
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 236 deletions.
26 changes: 0 additions & 26 deletions core/common/src/main/java/alluxio/util/Daemon.java
Expand Up @@ -11,8 +11,6 @@


package alluxio.util; package alluxio.util;


import java.util.concurrent.ThreadFactory;

/** /**
* A thread that has called {@link Thread#setDaemon(boolean) } with true. * A thread that has called {@link Thread#setDaemon(boolean) } with true.
*/ */
Expand All @@ -22,18 +20,6 @@ public class Daemon extends Thread {
setDaemon(true); setDaemon(true);
} }


/**
* Provide a factory for named daemon threads,
* for use in ExecutorServices constructors.
*/
public static class DaemonFactory extends Daemon implements ThreadFactory {

@Override
public Thread newThread(Runnable runnable) {
return new Daemon(runnable);
}
}

Runnable mRunnable = null; Runnable mRunnable = null;


/** /**
Expand All @@ -54,18 +40,6 @@ public Daemon(Runnable runnable) {
setName(((Object) mRunnable).toString()); setName(((Object) mRunnable).toString());
} }


/**
* Constructs a daemon thread to be part of a specified thread group.
*
* @param group thread group
* @param runnable an Runnable object
*/
public Daemon(ThreadGroup group, Runnable runnable) {
super(group, runnable);
mRunnable = runnable;
setName(((Object) runnable).toString());
}

/** /**
* @return mRunnable * @return mRunnable
*/ */
Expand Down
186 changes: 83 additions & 103 deletions core/common/src/main/java/alluxio/util/JvmPauseMonitor.java
Expand Up @@ -17,45 +17,43 @@
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.base.Stopwatch;
import com.google.common.collect.Sets;


import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;


import java.lang.management.GarbageCollectorMXBean; import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory; import java.lang.management.ManagementFactory;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;


/** /**
* Class which sets up a simple thread which runs in a loop sleeping * Class to monitor JVM with a daemon thread, the thread sleep period of time
* for a short interval of time. If the sleep takes significantly longer * and get the true time the sleep takes. If it is longer than it should be, the
* than its target time, it implies that the JVM or host machine has * JVM has paused processing. Then log it into different level.
* paused processing, which may cause other problems. If such a pause is
* detected, the thread logs a message.
*/ */
public final class JvmPauseMonitor { public final class JvmPauseMonitor {
private static final Log LOG = LogFactory.getLog( private static final Log LOG = LogFactory.getLog(
JvmPauseMonitor.class); JvmPauseMonitor.class);


/** The target sleep time. */ /** The time to sleep. */
private final long mGcSleepIntervalMs; private final long mGcSleepIntervalMs;


/** log WARN if we detect a pause longer than this threshold.*/ /** Extra sleep time longer than this threshold, log WARN. */
private final long mWarnThresholdMs; private final long mWarnThresholdMs;


/** log INFO if we detect a pause longer than this threshold. */ /** Extra sleep time longer than this threshold, log INFO. */
private final long mInfoThresholdMs; private final long mInfoThresholdMs;


private long mNumGcWarnThresholdExceeded = 0; /** Times extra sleep time exceed WARN. */
private long mNumGcInfoThresholdExceeded = 0; private long mExceedWarnTimes = 0;
private long mTotalGcExtraSleepTime = 0; /** Times extra sleep time exceed INFO. */
private long mExceedInfoTimes = 0;
/** Total extra sleep time. */
private long mTotalExtraTime = 0;


private Thread mMonitorThread; private Thread mJvmMonitorThread;
private volatile boolean mShouldRun = true; private volatile boolean mThreadStarted = true;


/** /**
* Constructs JvmPauseMonitor. * Constructs JvmPauseMonitor.
Expand All @@ -67,143 +65,125 @@ public JvmPauseMonitor() {
} }


/** /**
* Starts jvm monitor. * Starts jvm monitor thread.
*/ */
public void start() { public void start() {
Preconditions.checkState(mMonitorThread == null, Preconditions.checkState(mJvmMonitorThread == null,
"Already started"); "JVM monitor thread already started");
mMonitorThread = new Daemon(new Monitor()); mJvmMonitorThread = new Daemon(new Monitor());
mMonitorThread.start(); mJvmMonitorThread.start();
} }


/** /**
* Stops jvm monitor. * Stops jvm monitor.
*/ */
public void stop() { public void stop() {
mShouldRun = false; mThreadStarted = false;
mMonitorThread.interrupt(); mJvmMonitorThread.interrupt();
try { try {
mMonitorThread.join(); mJvmMonitorThread.join();
} catch (InterruptedException e) { } catch (InterruptedException e) {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
} }
reset();
} }


/** /**
* @return boolean if started,false otherwise * Resets value of mJvmMonitorThread and mThreadStarted.
* @return reseted mThreadStarted
*/
public JvmPauseMonitor reset() {
mJvmMonitorThread = null;
mThreadStarted = true;
return this;
}

/**
* @return true if thread started,false otherwise
*/ */
public boolean isStarted() { public boolean isStarted() {
return mMonitorThread != null; return mJvmMonitorThread != null;
} }


/** /**
* @return mNumGcWarnThresholdExceeded * @return Times exceed WARN threshold
*/ */
public long getNumGcWarnThreadholdExceeded() { public long getExceedWarnTimes() {
return mNumGcWarnThresholdExceeded; return mExceedWarnTimes;
} }


/** /**
* @return mNumGcInfoThresholdExceeded * @return Times exceed INFO threshold
*/ */
public long getNumGcInfoThresholdExceeded() { public long getExceedInfoTimes() {
return mNumGcInfoThresholdExceeded; return mExceedInfoTimes;
} }


/** /**
* @return mTotalGcExtraSleepTime * @return Total extra time
*/ */
public long getTotalGcExtraSleepTime() { public long getTotalExtraTime() {
return mTotalGcExtraSleepTime; return mTotalExtraTime;
} }


private String formatMessage(long extraSleepTime, Map<String, GcTimes> gcTimesAfterSleep, private String formatLogString(long extraSleepTime,
Map<String, GcTimes> gcTimesBeforeSleep) { List<GarbageCollectorMXBean> gcMXBeanListBeforeSleep,

List<GarbageCollectorMXBean> gcMXBeanListAfterSleep) {
Set<String> gcBeanNames = Sets.intersection( List<String> diffBean = Lists.newArrayList();
gcTimesAfterSleep.keySet(), GarbageCollectorMXBean oldBean;
gcTimesBeforeSleep.keySet()); GarbageCollectorMXBean newBean;
List<String> gcDiffs = Lists.newArrayList(); for (int i = 0; i < gcMXBeanListBeforeSleep.size(); i++) {
for (String name : gcBeanNames) { oldBean = gcMXBeanListBeforeSleep.get(i);
GcTimes diff = gcTimesAfterSleep.get(name).subtract( newBean = gcMXBeanListAfterSleep.get(i);
gcTimesBeforeSleep.get(name)); if (oldBean.getCollectionTime() != newBean.getCollectionTime()
if (diff.mGcCount != 0) { || oldBean.getCollectionCount() != newBean.getCollectionCount()) {
gcDiffs.add("GC pool '" + name + "' had collection(s): " diffBean.add("GC name= '" + newBean.getName() + " count="
+ diff.toString()); + newBean.getCollectionCount() + " time=" + newBean.getCollectionTime() + "ms");
} }
} }

String ret = "JVM pause " + extraSleepTime + "ms\n";
String ret = "Detected pause in JVM or host machine (eg GC): " if (diffBean.isEmpty()) {
+ "pause of approximately " + extraSleepTime + "ms\n";
if (gcDiffs.isEmpty()) {
ret += "No GCs detected"; ret += "No GCs detected";
} else { } else {
ret += Joiner.on("\n").join(gcDiffs); ret += "GC list:\n" + Joiner.on("\n").join(diffBean);
} }
return ret; return ret;
} }


private Map<String, GcTimes> getGcTimes() { private List<GarbageCollectorMXBean> getGarbageCollectorMXBeanList() {
Map<String, GcTimes> map = Maps.newHashMap(); return ManagementFactory.getGarbageCollectorMXBeans();
List<GarbageCollectorMXBean> gcBeans =
ManagementFactory.getGarbageCollectorMXBeans();
for (GarbageCollectorMXBean gcBean : gcBeans) {
map.put(gcBean.getName(), new GcTimes(gcBean));
}
return map;
}

private static final class GcTimes {
private GcTimes(GarbageCollectorMXBean gcBean) {
mGcCount = gcBean.getCollectionCount();
mGcTimeMillis = gcBean.getCollectionTime();
}

private GcTimes(long count, long time) {
mGcCount = count;
mGcTimeMillis = time;
}

private GcTimes subtract(GcTimes other) {
return new GcTimes(this.mGcCount - other.mGcCount,
mGcTimeMillis - other.mGcTimeMillis);
}

@Override
public String toString() {
return "count=" + mGcCount + " time=" + mGcTimeMillis + "ms";
}

private long mGcCount;
private long mGcTimeMillis;
} }


private class Monitor implements Runnable { private class Monitor implements Runnable {
@Override @Override
public void run() { public void run() {
StopWatch sw = new StopWatch(); Stopwatch sw = new Stopwatch();
Map<String, GcTimes> gcTimesBeforeSleep = getGcTimes(); List<GarbageCollectorMXBean> gcBeanListBeforeSleep = getGarbageCollectorMXBeanList();
while (mShouldRun) { while (mThreadStarted) {
sw.reset().start(); sw.reset().start();
try { try {
Thread.sleep(mGcSleepIntervalMs); Thread.sleep(mGcSleepIntervalMs);
} catch (InterruptedException ie) { } catch (InterruptedException ie) {
LOG.warn(ie.getStackTrace());
return; return;
} }
long extraSleepTime = sw.now(TimeUnit.MILLISECONDS) - mGcSleepIntervalMs; long extraTime = sw.elapsed(TimeUnit.MILLISECONDS) - mGcSleepIntervalMs;
Map<String, GcTimes> gcTimesAfterSleep = getGcTimes(); mTotalExtraTime += extraTime;

List<GarbageCollectorMXBean> gcBeanListAfterSleep = getGarbageCollectorMXBeanList();
if (extraSleepTime > mWarnThresholdMs) {
++mNumGcWarnThresholdExceeded; if (extraTime > mWarnThresholdMs) {
LOG.warn(formatMessage( ++mExceedWarnTimes;
extraSleepTime, gcTimesAfterSleep, gcTimesBeforeSleep)); LOG.warn(formatLogString(
} else if (extraSleepTime > mInfoThresholdMs) { extraTime, gcBeanListBeforeSleep, gcBeanListAfterSleep));
++mNumGcInfoThresholdExceeded; } else if (extraTime > mInfoThresholdMs) {
LOG.info(formatMessage( ++mExceedInfoTimes;
extraSleepTime, gcTimesAfterSleep, gcTimesBeforeSleep)); LOG.info(formatLogString(
extraTime, gcBeanListBeforeSleep, gcBeanListAfterSleep));
} else {
LOG.info(formatLogString(
extraTime, gcBeanListBeforeSleep, gcBeanListAfterSleep));
} }
mTotalGcExtraSleepTime += extraSleepTime; gcBeanListBeforeSleep = gcBeanListAfterSleep;
gcTimesBeforeSleep = gcTimesAfterSleep;
} }
} }
} }
Expand Down

0 comments on commit 1dc7ee9

Please sign in to comment.