From 7d1b25a048265017651235fdb8b430f3d3da3a16 Mon Sep 17 00:00:00 2001 From: Daniel Mitterdorfer Date: Mon, 11 Sep 2023 17:05:23 +0200 Subject: [PATCH] Fix counts for root frame --- .../xpack/profiling/GetFlamegraphResponse.java | 8 ++++++++ .../xpack/profiling/TransportGetFlamegraphAction.java | 6 +++++- .../profiling/TransportGetFlamegraphActionTests.java | 3 +++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/GetFlamegraphResponse.java b/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/GetFlamegraphResponse.java index 6044db723dbf3..509c9317ac686 100644 --- a/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/GetFlamegraphResponse.java +++ b/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/GetFlamegraphResponse.java @@ -120,6 +120,14 @@ public double getTotalSeconds() { return totalSeconds; } + public List getCountInclusive() { + return countInclusive; + } + + public List getCountExclusive() { + return countExclusive; + } + @Override public Iterator toXContentChunked(ToXContent.Params params) { return Iterators.concat( diff --git a/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/TransportGetFlamegraphAction.java b/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/TransportGetFlamegraphAction.java index af4498485dacf..0a28f5aa2e89e 100644 --- a/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/TransportGetFlamegraphAction.java +++ b/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/TransportGetFlamegraphAction.java @@ -75,7 +75,9 @@ public void onFailure(Exception e) { static GetFlamegraphResponse buildFlamegraph(GetStackTracesResponse response) { // TODO: Are full seconds good enough? (they probably are) - long totalSeconds = Duration.between(response.getStartTime(), response.getEndTime()).getSeconds(); + Duration observedDuration = Duration.between(response.getStartTime(), response.getEndTime()); + long totalSeconds = (observedDuration.getNano() > 0) ? observedDuration.getSeconds() + 1 : observedDuration.getSeconds(); + FlamegraphBuilder builder = new FlamegraphBuilder(response.getTotalFrames(), response.getSamplingRate(), totalSeconds); if (response.getTotalFrames() == 0) { return builder.build(); @@ -92,6 +94,8 @@ static GetFlamegraphResponse buildFlamegraph(GetStackTracesResponse response) { StackTrace stackTrace = st.getValue(); int samples = response.getStackTraceEvents().getOrDefault(stackTraceId, 0); builder.setCurrentNode(0); + builder.addSamplesInclusive(0, samples); + builder.addSamplesExclusive(0, 0); int frameCount = stackTrace.frameIds.size(); for (int i = 0; i < frameCount; i++) { diff --git a/x-pack/plugin/profiling/src/test/java/org/elasticsearch/xpack/profiling/TransportGetFlamegraphActionTests.java b/x-pack/plugin/profiling/src/test/java/org/elasticsearch/xpack/profiling/TransportGetFlamegraphActionTests.java index ee7c090b22fa1..c3a208ee0bad2 100644 --- a/x-pack/plugin/profiling/src/test/java/org/elasticsearch/xpack/profiling/TransportGetFlamegraphActionTests.java +++ b/x-pack/plugin/profiling/src/test/java/org/elasticsearch/xpack/profiling/TransportGetFlamegraphActionTests.java @@ -58,5 +58,8 @@ public void testCreateFlamegraph() { assertEquals(10, response.getSize()); assertEquals(1.0d, response.getTotalSeconds(), 0.001d); assertEquals(1.0d, response.getSamplingRate(), 0.001d); + assertEquals(List.of(1, 1, 1, 1, 1, 1, 1, 1, 1, 1), response.getCountInclusive()); + assertEquals(List.of(0, 0, 0, 0, 0, 0, 0, 0, 0, 1), response.getCountExclusive()); + } }