Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package io.perfmark.tracewriter;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonIOException;
import com.google.gson.annotations.SerializedName;
import io.perfmark.impl.Mark;
import io.perfmark.impl.MarkList;
import io.perfmark.impl.Storage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.lang.management.ManagementFactory;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.GZIPOutputStream;
import javax.annotation.Nullable;
/**
* Writes the PerfMark results to a "Trace Event" JSON file usable by the Chromium Profiler
* "Catapult". The format is defined at
* https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview
*/
public final class TraceEventWriter {
private static final Logger logger = Logger.getLogger(TraceEventWriter.class.getName());
/**
* Writes trace events the home directory. By default, it prefers the location in {@code
* $XDG_DATA_HOME/perfmark} environment variable. If unset, it attempts to use {@code
* $HOME/.local/share/perfmark}.
*
*
Authors note: if you are on Windows, or the above defaults aren't right, I'm not really sure
* where else is a good place to put this data. Please file an issue at https://perfmark.io/ if
* you have a preference.
*
* @throws IOException if there is an error writing to the file.
*/
public static void writeTraceEvents() throws IOException {
Path p = pickNextDest(guessDirectory());
try (OutputStream os = Files.newOutputStream(p)) {
logger.info("Writing trace to " + p);
try (OutputStream gzos = new GZIPOutputStream(os)) {
try (Writer osw = new OutputStreamWriter(gzos, UTF_8)) {
writeTraceEvents(osw);
}
}
}
}
public static void writeTraceEvents(Writer destination) throws IOException {
writeTraceEvents(
destination, Storage.read(), Storage.getInitNanoTime(), System.nanoTime(), getPid());
}
/**
* Writes the trace events gathered from {@link Storage#read()}. This method is not API stable. It
* will be eventually.
*/
public static void writeTraceEvents(
Writer destination,
List extends MarkList> markLists,
long initNanoTime,
long nowNanoTime,
long pid)
throws IOException {
List traceEvents = new ArrayList<>();
new TraceEventWalker(traceEvents, pid, initNanoTime).walk(markLists, nowNanoTime);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
try {
gson.toJson(new TraceEventObject(traceEvents), destination);
} catch (JsonIOException e) {
throw new IOException(e);
}
}
private static Path pickNextDest(Path dir) throws IOException {
String fmt = "perfmark-trace-%03d.json.gz";
int lo;
int hi = 0;
while (true) {
Path candidate = dir.resolve(String.format(fmt, hi));
if (!Files.exists(candidate)) {
lo = hi >>> 1;
break;
}
if (hi == 0) {
hi++;
} else if (hi >>> 1 >= Integer.MAX_VALUE) {
throw new IOException("too many files in dir");
} else {
hi <<= 1;
}
}
// After this point, hi must always point to a non-existent file.
while (hi > lo) {
int mid = (hi + lo) >>> 1; // take THAT, overflow!
Path candidate = dir.resolve(String.format(fmt, mid));
if (Files.exists(candidate)) {
lo = mid + 1;
} else {
hi = mid;
}
}
return dir.resolve(String.format(fmt, hi));
}
private static Path guessDirectory() throws IOException {
final String PERFMARK_TRACE_DIR = "perfmark";
final String sep = File.separator;
List dataHomeChoices = new ArrayList<>();
String dataHome = System.getenv("XDG_DATA_HOME");
if (dataHome != null) {
dataHomeChoices.add(new File(dataHome + sep + PERFMARK_TRACE_DIR).toPath());
}
String home = System.getProperty("user.home");
if (home != null) {
dataHomeChoices.add(
new File(home + sep + ".local" + sep + "share" + sep + PERFMARK_TRACE_DIR).toPath());
}
for (Path path : dataHomeChoices) {
if (!Files.exists(path)) {
Files.createDirectories(path);
} else {
if (!Files.isDirectory(path)) {
continue;
}
}
return path;
}
return new File(".").toPath();
}
static final class TraceEventObject {
@SerializedName("traceEvents")
@SuppressWarnings("unused")
final List traceEvents;
@SerializedName("displayTimeUnit")
@SuppressWarnings("unused")
final String displayTimeUnit = "ns";
@SerializedName("systemTraceEvents")
@SuppressWarnings("unused")
final String systemTraceData = "";
@SerializedName("samples")
@SuppressWarnings("unused")
final List