Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipException;
Expand Down Expand Up @@ -82,12 +84,17 @@ public static BazelProfile createFromInputStream(InputStream inputStream)
new JsonReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)));
}

public static BazelProfile of(Reader reader) {
return new BazelProfile(new JsonReader(reader));
}

private final BazelVersion bazelVersion;
private final Map<String, String> otherData = new HashMap<>();
private final Map<ThreadId, ProfileThread> threads = new HashMap<>();
private final Map<ThreadId, ProfileThread> threads;

private BazelProfile(JsonReader profileReader) {
try {
Map<ThreadId, ProfileThread.Builder> threadBuilders = new HashMap<>();
boolean hasOtherData = false;
boolean hasTraceEvents = false;
profileReader.beginObject();
Expand Down Expand Up @@ -116,17 +123,17 @@ private BazelProfile(JsonReader profileReader) {
continue;
}
ThreadId threadId = new ThreadId(pid, tid);
ProfileThread profileThread =
threads.compute(
ProfileThread.Builder profileThreadBuilder =
threadBuilders.compute(
threadId,
(key, t) -> {
if (t == null) {
t = new ProfileThread(threadId);
t = new ProfileThread.Builder().setThreadId(key);
}
return t;
});
// TODO: Use success response to take action on errant events.
profileThread.addEvent(traceEvent);
profileThreadBuilder.addEvent(traceEvent);
}
profileReader.endArray();
break;
Expand All @@ -143,6 +150,9 @@ private BazelProfile(JsonReader profileReader) {
TraceEventFormatConstants.SECTION_OTHER_DATA,
TraceEventFormatConstants.SECTION_TRACE_EVENTS));
}
threads =
threadBuilders.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().build()));
} catch (IllegalStateException | IOException e) {
throw new IllegalArgumentException("Could not parse Bazel profile.", e);
}
Expand Down
Loading