Last active
January 15, 2021 19:34
-
-
Save apangin/f5cc1c38af00f9544edb75d8a9eef28a to your computer and use it in GitHub Desktop.
Java safepoint event listener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import one.log.event.ILoggingEvent; | |
import one.log.event.OperationLogEvent; | |
import one.log.stat.StatCollector; | |
import one.log.stat.Statistics; | |
import one.nio.mgt.DiagnosticCommand; | |
import one.nio.os.BatchThread; | |
import java.io.BufferedReader; | |
import java.io.FileReader; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public class LogSafepoints { | |
public static void main(String[] args) throws Exception { | |
String pipe = "/tmp/safepoint_pipe.log"; | |
new ProcessBuilder("mkfifo", pipe).start().waitFor(); | |
Thread safepointListener = new BatchThread("Safepoint Listener") { | |
@Override | |
public void run() { | |
Pattern pattern = Pattern.compile("\\[(\\d+)ms] Safepoint \"([0-z ]+)\", Time since last: \\d+ ns, Reaching safepoint: (\\d+) ns, At safepoint: \\d+ ns, Total: (\\d+) ns"); | |
try (BufferedReader br = new BufferedReader(new FileReader(pipe))) { | |
for (String s; (s = br.readLine()) != null; ) { | |
Matcher m = pattern.matcher(s); | |
if (m.matches()) { | |
logSafepoint("safepoint.stat", Long.parseLong(m.group(1)), m.group(2), Long.parseLong(m.group(3)), Long.parseLong(m.group(4))); | |
} | |
} | |
} catch (Exception e) { | |
// return | |
} | |
} | |
private void logSafepoint(String statId, long eventTime, String vmop, long syncTime, long totalTime) { | |
StatCollector<ILoggingEvent> collector = Statistics.find(statId); | |
if (collector != null) { | |
Object[] args = {vmop}; | |
collector.collect(new OperationLogEvent(statId, true, syncTime, 1, eventTime, "safepoint.sync", args)); | |
collector.collect(new OperationLogEvent(statId, true, totalTime, 1, eventTime, "safepoint.total", args)); | |
} | |
} | |
}; | |
safepointListener.setDaemon(true); | |
safepointListener.start(); | |
DiagnosticCommand.execute("vmLog", | |
"output=file=" + pipe, | |
"output_options=filecount=0,filesize=0", | |
"what=safepoint=info", | |
"decorators=timemillis"); | |
// Test | |
while (true) { | |
System.gc(); | |
Thread.sleep(1000); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment