diff options
author | ulfjack <ulfjack@google.com> | 2018-07-25 06:42:16 -0700 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-07-25 06:43:42 -0700 |
commit | d7177ab65910cbf8428da86353cf431d232d7cae (patch) | |
tree | 780a76369e676952b8127db225d0a5615591ff5d /src/main/java/com/google/devtools/build/lib/profiler/Profiler.java | |
parent | 466121931913107f9ecbd1c7b96854d1d1d6d405 (diff) |
Add the thread name as metadata to the Json profile
PiperOrigin-RevId: 205985818
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/profiler/Profiler.java')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/profiler/Profiler.java | 38 |
1 files changed, 33 insertions, 5 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/profiler/Profiler.java b/src/main/java/com/google/devtools/build/lib/profiler/Profiler.java index 47008de2be..4f1ccb80ae 100644 --- a/src/main/java/com/google/devtools/build/lib/profiler/Profiler.java +++ b/src/main/java/com/google/devtools/build/lib/profiler/Profiler.java @@ -990,12 +990,30 @@ public final class Profiler { private static class JsonTraceFileWriter extends FileWriter { private final OutputStream outStream; private final long profileStartTimeNanos; + private final ThreadLocal<Boolean> metadataPosted = + ThreadLocal.withInitial(() -> Boolean.FALSE); JsonTraceFileWriter(OutputStream outStream, long profileStartTimeNanos) { this.outStream = outStream; this.profileStartTimeNanos = profileStartTimeNanos; } + @Override + public void enqueue(TaskData data) { + if (!metadataPosted.get().booleanValue()) { + metadataPosted.set(Boolean.TRUE); + // Create a TaskData object that is special-cased below. + queue.add( + new TaskData( + /* id= */ 0, + /* startTimeNanos= */ -1, + /* parent= */ null, + ProfilerTask.THREAD_NAME, + Thread.currentThread().getName())); + } + queue.add(data); + } + /** * Saves all gathered information from taskQueue queue to the file. * Method is invoked internally by the Timer-based thread and at the end of @@ -1010,17 +1028,27 @@ public final class Profiler { // in the future. new OutputStreamWriter( new BufferedOutputStream(outStream, 262144), StandardCharsets.UTF_8)) { - out.append("["); + out.append("[\n"); boolean first = true; TaskData data; while ((data = queue.take()) != POISON_PILL) { - if (data.duration == 0) { + if (data.duration == 0 && data.type != ProfilerTask.THREAD_NAME) { continue; } if (first) { first = false; } else { - out.append(","); + out.append(",\n"); + } + if (data.type == ProfilerTask.THREAD_NAME) { + out.append("{"); + out.append("\"name\":\"thread_name\","); + out.append("\"ph\":\"M\","); + out.append("\"pid\":1,"); + out.append("\"tid\":").append(Long.toString(data.threadId)).append(","); + out.append("\"args\": {\"name\":\"").append(data.description).append("\"}"); + out.append("}"); + continue; } char eventType = data.duration == 0 ? 'i' : 'X'; out.append("{"); @@ -1039,10 +1067,10 @@ public final class Profiler { } out.append("\"pid\":1,"); out.append("\"tid\":").append(Long.toString(data.threadId)); - out.append("}\n"); + out.append("}"); } receivedPoisonPill = true; - out.append("]"); + out.append("\n]"); } catch (IOException e) { this.savedException = e; if (!receivedPoisonPill) { |