Skip to content

Commit bf7e68f

Browse files
committed
Update differential_flamegraph script to make supporting negation easy once it's possible
Signed-off-by: Dom Del Nano <ddelnano@gmail.com>
1 parent 965b00a commit bf7e68f

File tree

2 files changed

+50
-22
lines changed

2 files changed

+50
-22
lines changed

src/pxl_scripts/px/differential_flamegraph/differential.pxl

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,62 @@
1616

1717
import px
1818

19-
def export_flame_graph(start_time: str, namespace: str, pod: str, baseline_pod: str):
19+
negate = False
20+
# TODO(ddelnano): negation requires switching the type of join from right to left
21+
# or returning a different DataFrame. This might not be possible with pxl's current
22+
# functionality, but this should be implemented once it's possible.
23+
24+
def merge_and_compute_delta(pod1, pod2, negate: bool):
25+
26+
diff = pod1.merge(
27+
pod2,
28+
how='right',
29+
left_on='stack_trace',
30+
right_on='stack_trace'
31+
suffixes=['_1', '_2'],
32+
)
33+
# TODO(ddelnano): This needs to be switched with pod1 if the flamegraph should
34+
# be negated.
35+
percentage_agg = pod2.groupby(['pod']).agg(
36+
count=('count', px.sum),
37+
)
38+
diff.pod = px.select(negate, diff.pod_1, diff.pod_2)
39+
diff.stack_trace = px.select(negate, diff.stack_trace_1, diff.stack_trace_2)
40+
diff.stack_trace = px.replace(' ', diff.stack_trace, '')
41+
diff.count = px.select(negate, diff.count_1, diff.count_2)
42+
diff.delta = diff.count_2 - diff.count_1
43+
diff.delta = px.select(negate, px.negate(diff.delta), diff.delta)
44+
45+
merged = diff.merge(
46+
percentage_agg,
47+
how='inner',
48+
left_on='pod',
49+
right_on='pod',
50+
suffixes=['', '_x']
51+
)
52+
merged.percent = 100 * merged.count / merged.count_x
53+
return merged
54+
55+
def differential_flamegraph(start_time: str, namespace: str, pod: str, baseline_pod: str):
2056
stack_traces = px.DataFrame(table='stack_traces.beta', start_time=start_time)
2157
stack_traces.namespace = stack_traces.ctx['namespace']
22-
stack_traces = stack_traces[px.contains(stack_traces.namespace, namespace)]
58+
stack_traces = stack_traces[stack_traces.namespace == namespace]
2359
stack_traces.node = px.Node(px._exec_hostname())
2460
stack_traces.pod = stack_traces.ctx['pod']
25-
stack_traces.keep_row = px.select(px.contains(stack_traces.pod, baseline_pod), True, False)
26-
stack_traces.keep_row = px.select(stack_traces.keep_row or px.contains(stack_traces.pod, pod), True, False)
61+
stack_traces.keep_row = stack_traces.pod == baseline_pod
62+
stack_traces.keep_row = px.select(stack_traces.keep_row or stack_traces.pod == pod, True, False)
2763
stack_traces = stack_traces[stack_traces.keep_row]
2864

2965
stack_traces = stack_traces.groupby(['node', 'namespace', 'pod', 'stack_trace_id']).agg(
3066
stack_trace=('stack_trace', px.any),
3167
count=('count', px.sum)
3268
)
3369

34-
pod1 = stack_traces[px.contains(stack_traces.pod, baseline_pod)]
35-
pod1 = pod1.drop(['node', 'namespace', 'pod', 'stack_trace_id'])
70+
pod1 = stack_traces[stack_traces.pod == baseline_pod]
71+
pod1 = pod1.drop(['node', 'namespace', 'stack_trace_id'])
3672

37-
pod2 = stack_traces[px.contains(stack_traces.pod, pod)]
38-
pod2 = pod2.drop(['node', 'namespace', 'pod', 'stack_trace_id'])
73+
pod2 = stack_traces[stack_traces.pod == pod]
74+
pod2 = pod2.drop(['node', 'namespace', 'stack_trace_id'])
3975

40-
merged = pod1.merge(
41-
pod2,
42-
how='right',
43-
left_on='stack_trace',
44-
right_on='stack_trace'
45-
suffixes=['_1', '_2'],
46-
)
47-
# Pixie's stack trace format permits spaces, but Brendan Gregg's scripts do not
48-
merged.stack_trace_2 = px.replace(' ', merged.stack_trace_2, '')
49-
merged.delta = merged.count_2 - merged.count_1
50-
return merged[['stack_trace_2', 'count_1', 'count_2', 'delta']]
76+
merged = merge_and_compute_delta(pod1, pod2, negate)
77+
return merged[['stack_trace', 'count', 'delta', 'percent']]

src/pxl_scripts/px/differential_flamegraph/vis.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"h": 6
3434
},
3535
"func": {
36-
"name": "export_flame_graph",
36+
"name": "differential_flamegraph",
3737
"args": [
3838
{
3939
"name": "start_time",
@@ -55,8 +55,9 @@
5555
},
5656
"displaySpec": {
5757
"@type": "types.px.dev/px.vispb.StackTraceFlameGraph",
58-
"stacktraceColumn": "stack_trace_2",
59-
"countColumn": "count_2",
58+
"stacktraceColumn": "stack_trace",
59+
"countColumn": "count",
60+
"percentageColumn": "percent",
6061
"differenceColumn": "delta"
6162
}
6263
}

0 commit comments

Comments
 (0)