Skip to content

Commit 8d3cfbe

Browse files
committed
Cleanups
1 parent b9f13f3 commit 8d3cfbe

File tree

7 files changed

+59
-152
lines changed

7 files changed

+59
-152
lines changed

x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/action/GetStackTracesResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params
102102
stackTraceEvents,
103103
(n, v) -> ChunkedToXContentHelper.wrapWithObject(
104104
n,
105-
Iterators.map(v.values().iterator(), e -> (b, p) -> b.field(e.stacktraceID, e.count))
105+
Iterators.map(v.entrySet().iterator(), e -> (b, p) -> b.field(e.getKey().stacktraceID(), e.getValue().count))
106106
)
107107
),
108108
Iterators.single((b, p) -> b.field("total_frames", totalFrames).field("sampling_rate", samplingRate).endObject())

x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/action/GetStackTracesResponseBuilder.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -137,23 +137,6 @@ public void setTotalSamples(long totalSamples) {
137137
}
138138

139139
public GetStackTracesResponse build() {
140-
// Merge the TraceEvent data into the StackTraces.
141-
if (stackTraces != null && stackTraceEvents != null) {
142-
stackTraceEvents.forEach((id, event) -> {
143-
StackTrace stackTrace = stackTraces.get(id.stacktraceID());
144-
if (stackTrace == null) {
145-
return;
146-
}
147-
stackTrace.count += event.count;
148-
stackTrace.executableName = event.executableName;
149-
stackTrace.threadName = event.threadName;
150-
if (event.subGroups != null) {
151-
stackTrace.subGroups = event.subGroups;
152-
}
153-
stackTrace.annualCO2Tons += event.annualCO2Tons;
154-
stackTrace.annualCostsUSD += event.annualCostsUSD;
155-
});
156-
}
157140
return new GetStackTracesResponse(stackTraces, stackFrames, executables, stackTraceEvents, totalFrames, samplingRate, totalSamples);
158141
}
159142
}

x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/action/StackTrace.java

Lines changed: 5 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,12 @@ final class StackTrace implements ToXContentObject {
2727
String[] frameIds;
2828
int[] typeIds;
2929
SubGroup subGroups;
30-
double annualCO2Tons;
31-
double annualCostsUSD;
32-
long count;
33-
String executableName;
34-
String threadName;
35-
36-
StackTrace(
37-
int[] addressOrLines,
38-
String[] fileIds,
39-
String[] frameIds,
40-
int[] typeIds,
41-
double annualCO2Tons,
42-
double annualCostsUSD,
43-
long count
44-
) {
30+
31+
StackTrace(int[] addressOrLines, String[] fileIds, String[] frameIds, int[] typeIds) {
4532
this.addressOrLines = addressOrLines;
4633
this.fileIds = fileIds;
4734
this.frameIds = frameIds;
4835
this.typeIds = typeIds;
49-
this.annualCO2Tons = annualCO2Tons;
50-
this.annualCostsUSD = annualCostsUSD;
51-
this.count = count;
52-
this.executableName = "";
53-
this.threadName = "";
5436
}
5537

5638
private static final int BASE64_FRAME_ID_LENGTH = 32;
@@ -214,7 +196,7 @@ public static StackTrace fromSource(Map<String, Object> source) {
214196
// Step 2: Convert the run-length byte encoding into a list of uint8s.
215197
int[] typeIDs = runLengthDecodeBase64Url(inputFrameTypes, inputFrameTypes.length(), countsFrameIDs);
216198

217-
return new StackTrace(addressOrLines, fileIDs, frameIDs, typeIDs, 0, 0, 0);
199+
return new StackTrace(addressOrLines, fileIDs, frameIDs, typeIDs);
218200
}
219201

220202
public void forNativeAndKernelFrames(Consumer<String> consumer) {
@@ -226,34 +208,13 @@ public void forNativeAndKernelFrames(Consumer<String> consumer) {
226208
}
227209
}
228210

229-
public String getExecutableName() {
230-
if (executableName.isEmpty() && this.typeIds.length > 0 && this.typeIds[0] == KERNEL_FRAME_TYPE) {
231-
// kernel threads are not associated with an executable
232-
return "kernel";
233-
}
234-
return executableName;
235-
}
236-
237-
public String getThreadName() {
238-
if (threadName.isEmpty()) {
239-
// kernel threads are not associated with an executable
240-
return executableName;
241-
}
242-
return threadName;
243-
}
244-
245211
@Override
246212
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
247213
builder.startObject();
248214
builder.field("address_or_lines", this.addressOrLines);
249215
builder.field("file_ids", this.fileIds);
250216
builder.field("frame_ids", this.frameIds);
251217
builder.field("type_ids", this.typeIds);
252-
builder.field("annual_co2_tons", this.annualCO2Tons);
253-
builder.field("annual_costs_usd", this.annualCostsUSD);
254-
builder.field("count", this.count);
255-
builder.field("executable_name", this.executableName);
256-
builder.field("thread_name", this.threadName);
257218
builder.endObject();
258219
return builder;
259220
}
@@ -268,10 +229,8 @@ public boolean equals(Object o) {
268229
return Arrays.equals(addressOrLines, that.addressOrLines)
269230
&& Arrays.equals(fileIds, that.fileIds)
270231
&& Arrays.equals(frameIds, that.frameIds)
271-
&& Arrays.equals(typeIds, that.typeIds)
272-
&& executableName.equals(that.executableName)
273-
&& threadName.equals(that.threadName);
274-
// Don't compare metadata like annualized co2, annualized costs, subGroups and count.
232+
&& Arrays.equals(typeIds, that.typeIds);
233+
// Don't compare metadata like subGroups.
275234
}
276235

277236
// Don't hash metadata like annualized co2, annualized costs, subGroups and count.
@@ -281,8 +240,6 @@ public int hashCode() {
281240
result = 31 * result + Arrays.hashCode(fileIds);
282241
result = 31 * result + Arrays.hashCode(frameIds);
283242
result = 31 * result + Arrays.hashCode(typeIds);
284-
result = 31 * result + executableName.hashCode();
285-
result = 31 * result + threadName.hashCode();
286243
return result;
287244
}
288245
}

x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/action/TraceEvent.java

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,71 +7,23 @@
77

88
package org.elasticsearch.xpack.profiling.action;
99

10-
import java.util.Objects;
11-
1210
final class TraceEvent {
13-
final String stacktraceID;
14-
final String executableName;
15-
final String threadName;
16-
final String hostID;
1711
long count;
1812
double annualCO2Tons;
1913
double annualCostsUSD;
2014
SubGroup subGroups;
2115

22-
TraceEvent(String stacktraceID) {
23-
this(stacktraceID, "", "", "", 0);
24-
}
25-
26-
TraceEvent(String stacktraceID, long count) {
27-
this(stacktraceID, "", "", "", count);
16+
TraceEvent() {
17+
this(0);
2818
}
2919

30-
TraceEvent(String stacktraceID, String executableName, String threadName, String hostID) {
31-
this(stacktraceID, executableName, threadName, hostID, 0);
32-
}
33-
34-
TraceEvent(String stacktraceID, String executableName, String threadName, String hostID, long count) {
35-
this.stacktraceID = stacktraceID;
36-
this.executableName = executableName;
37-
this.threadName = threadName;
38-
this.hostID = hostID;
20+
TraceEvent(long count) {
3921
this.count = count;
4022
}
4123

42-
@Override
43-
public boolean equals(Object o) {
44-
if (this == o) {
45-
return true;
46-
}
47-
if (o == null || getClass() != o.getClass()) {
48-
return false;
49-
}
50-
TraceEvent event = (TraceEvent) o;
51-
return count == event.count
52-
&& Objects.equals(stacktraceID, event.stacktraceID)
53-
&& Objects.equals(executableName, event.executableName)
54-
&& Objects.equals(threadName, event.threadName)
55-
&& Objects.equals(hostID, event.hostID);
56-
}
57-
58-
@Override
59-
public int hashCode() {
60-
return Objects.hash(stacktraceID, executableName, threadName, hostID, count);
61-
}
62-
6324
@Override
6425
public String toString() {
6526
return "TraceEvent{"
66-
+ "stacktraceID='"
67-
+ stacktraceID
68-
+ '\''
69-
+ ", executableName='"
70-
+ executableName
71-
+ '\''
72-
+ ", threadName='"
73-
+ threadName
74-
+ '\''
7527
+ ", count="
7628
+ count
7729
+ ", annualCO2Tons="

x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/action/TransportGetFlamegraphAction.java

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
import java.util.List;
2626
import java.util.Map;
2727
import java.util.Objects;
28-
import java.util.SortedMap;
29-
import java.util.TreeMap;
28+
import java.util.concurrent.atomic.AtomicInteger;
3029

3130
public class TransportGetFlamegraphAction extends TransportAction<GetStackTracesRequest, GetFlamegraphResponse> {
3231
private static final int FRAMETYPE_ROOT = 0x100;
@@ -77,24 +76,30 @@ static GetFlamegraphResponse buildFlamegraph(GetStackTracesResponse response) {
7776
return builder.build();
7877
}
7978

80-
SortedMap<String, StackTrace> sortedStacktraces = new TreeMap<>(response.getStackTraces());
81-
for (Map.Entry<String, StackTrace> st : sortedStacktraces.entrySet()) {
82-
StackTrace stackTrace = st.getValue();
83-
builder.setCurrentNode(0);
79+
Map<String, StackTrace> stackTraces = response.getStackTraces();
80+
AtomicInteger lostStackTraces = new AtomicInteger();
81+
82+
response.getStackTraceEvents().forEach((eventId, event) -> {
83+
StackTrace stackTrace = stackTraces.get(eventId.stacktraceID());
84+
if (stackTrace == null) {
85+
lostStackTraces.getAndIncrement();
86+
return;
87+
}
88+
89+
long samples = event.count;
90+
double annualCO2Tons = event.annualCO2Tons;
91+
double annualCostsUSD = event.annualCostsUSD;
8492

85-
long samples = stackTrace.count;
86-
double annualCO2Tons = stackTrace.annualCO2Tons;
87-
double annualCostsUSD = stackTrace.annualCostsUSD;
93+
String executableName = eventId.executableName();
94+
if (executableName.isEmpty() && stackTrace.typeIds.length > 0 && stackTrace.typeIds[0] == StackTrace.KERNEL_FRAME_TYPE) {
95+
// kernel threads are not associated with an executable
96+
executableName = "kernel";
97+
}
8898

99+
builder.setCurrentNode(0);
89100
builder.addToRootNode(samples, annualCO2Tons, annualCostsUSD);
90-
builder.addOrUpdateAggregationNode(
91-
stackTrace.getExecutableName(),
92-
samples,
93-
annualCO2Tons,
94-
annualCostsUSD,
95-
FRAMETYPE_EXECUTABLE
96-
);
97-
builder.addOrUpdateAggregationNode(stackTrace.getThreadName(), samples, annualCO2Tons, annualCostsUSD, FRAMETYPE_THREAD);
101+
builder.addOrUpdateAggregationNode(executableName, samples, annualCO2Tons, annualCostsUSD, FRAMETYPE_EXECUTABLE);
102+
builder.addOrUpdateAggregationNode(eventId.threadName(), samples, annualCO2Tons, annualCostsUSD, FRAMETYPE_THREAD);
98103

99104
int frameCount = stackTrace.frameIds.length;
100105
for (int i = 0; i < frameCount; i++) {
@@ -140,6 +145,9 @@ static GetFlamegraphResponse buildFlamegraph(GetStackTracesResponse response) {
140145
builder.setCurrentNode(nodeId);
141146
});
142147
}
148+
});
149+
if (lostStackTraces.get() != 0) {
150+
log.warn("Lost {} stacktraces.", lostStackTraces);
143151
}
144152
return builder.build();
145153
}

x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/action/TransportGetStackTracesAction.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
import java.util.ArrayList;
5757
import java.util.Collections;
5858
import java.util.HashMap;
59-
import java.util.HashSet;
6059
import java.util.List;
6160
import java.util.Map;
6261
import java.util.Set;
@@ -292,7 +291,7 @@ private void searchGenericEventGroupedByStackTrace(
292291
String stackTraceID = stacktraceBucket.getKeyAsString();
293292

294293
TraceEventID eventID = new TraceEventID("", "", "", stackTraceID);
295-
TraceEvent event = stackTraceEvents.computeIfAbsent(eventID, k -> new TraceEvent(stackTraceID));
294+
TraceEvent event = stackTraceEvents.computeIfAbsent(eventID, k -> new TraceEvent());
296295
event.count += count;
297296
subGroups.collectResults(stacktraceBucket, event);
298297
}
@@ -398,10 +397,7 @@ private void searchEventGroupedByStackTrace(
398397
String stackTraceID = stacktraceBucket.getKeyAsString();
399398

400399
TraceEventID eventID = new TraceEventID(executableName, threadName, hostID, stackTraceID);
401-
TraceEvent event = stackTraceEvents.computeIfAbsent(
402-
eventID,
403-
k -> new TraceEvent(stackTraceID, executableName, threadName, hostID)
404-
);
400+
TraceEvent event = stackTraceEvents.computeIfAbsent(eventID, k -> new TraceEvent());
405401
event.count += finalCount;
406402
subGroups.collectResults(stacktraceBucket, event);
407403
}
@@ -605,7 +601,7 @@ public void onStackTraceResponse(MultiGetResponse multiGetItemResponses) {
605601
if (stackTracePerId.putIfAbsent(id, stacktrace) == null) {
606602
totalFrames.addAndGet(stacktrace.frameIds.length);
607603
stackFrameIds.addAll(List.of(stacktrace.frameIds));
608-
stacktrace.forNativeAndKernelFrames(e -> executableIds.add(e));
604+
stacktrace.forNativeAndKernelFrames(executableIds::add);
609605
}
610606
}
611607
}
@@ -644,10 +640,10 @@ public void calculateCO2AndCosts() {
644640
responseBuilder.getCustomCostPerCoreHour()
645641
);
646642

647-
for (TraceEvent event : responseBuilder.getStackTraceEvents().values()) {
648-
event.annualCO2Tons += co2Calculator.getAnnualCO2Tons(event.hostID, event.count);
649-
event.annualCostsUSD += costCalculator.annualCostsUSD(event.hostID, event.count);
650-
}
643+
responseBuilder.getStackTraceEvents().forEach((eventId, event) -> {
644+
event.annualCO2Tons += co2Calculator.getAnnualCO2Tons(eventId.hostID(), event.count);
645+
event.annualCostsUSD += costCalculator.annualCostsUSD(eventId.hostID(), event.count);
646+
});
651647

652648
log.debug(watch::report);
653649
}
@@ -843,6 +839,4 @@ private void mget(Client client, List<Index> indices, List<String> slice, Action
843839
.execute(new RefCountAwareThreadedActionListener<>(responseExecutor, listener));
844840
}
845841
}
846-
847-
record HostEventCount(String hostID, String stacktraceID, int count) {}
848842
}

x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/action/TransportGetTopNFunctionsAction.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
import java.util.HashMap;
2626
import java.util.HashSet;
2727
import java.util.List;
28+
import java.util.Map;
2829
import java.util.Set;
30+
import java.util.concurrent.atomic.AtomicInteger;
2931

3032
public class TransportGetTopNFunctionsAction extends TransportAction<GetStackTracesRequest, GetTopNFunctionsResponse> {
3133
private static final Logger log = LogManager.getLogger(TransportGetTopNFunctionsAction.class);
@@ -57,11 +59,20 @@ static GetTopNFunctionsResponse buildTopNFunctions(GetStackTracesResponse respon
5759
return builder.build();
5860
}
5961

60-
for (StackTrace stackTrace : response.getStackTraces().values()) {
62+
Map<String, StackTrace> stackTraces = response.getStackTraces();
63+
AtomicInteger lostStackTraces = new AtomicInteger();
64+
65+
response.getStackTraceEvents().forEach((eventId, event) -> {
6166
Set<String> frameGroupsPerStackTrace = new HashSet<>();
62-
long samples = stackTrace.count;
63-
double annualCO2Tons = stackTrace.annualCO2Tons;
64-
double annualCostsUSD = stackTrace.annualCostsUSD;
67+
long samples = event.count;
68+
double annualCO2Tons = event.annualCO2Tons;
69+
double annualCostsUSD = event.annualCostsUSD;
70+
71+
StackTrace stackTrace = stackTraces.get(eventId.stacktraceID());
72+
if (stackTrace == null) {
73+
lostStackTraces.getAndIncrement();
74+
return;
75+
}
6576

6677
int frameCount = stackTrace.frameIds.length;
6778
for (int i = 0; i < frameCount; i++) {
@@ -116,8 +127,10 @@ static GetTopNFunctionsResponse buildTopNFunctions(GetStackTracesResponse respon
116127
}
117128
});
118129
}
130+
});
131+
if (lostStackTraces.get() != 0) {
132+
log.warn("Lost {} stacktraces.", lostStackTraces);
119133
}
120-
121134
return builder.build();
122135
}
123136

0 commit comments

Comments
 (0)