-
Notifications
You must be signed in to change notification settings - Fork 37
/
cgroup_rstat_tracepoint.bt
executable file
·277 lines (239 loc) · 7.76 KB
/
cgroup_rstat_tracepoint.bt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env bpftrace
/* SPDX-License-Identifier: GPL-2.0+
*
* Script for evaluation the latency effect cgroup rstat (recursive stats).
*
* It depends on tracepoints expected added in kernel 6.10
*
* Production experience with softirq_net_latency.bt indicate that reading
* cgroup rstat (files under subdirs in /sys/fs/cgroup/) is *one* of the
* factors that can disrupt softirq processing.
*
* This script zoom into cgroup rstat locking tracepoints to evaluate kernel
* improvements in this area.
*
* 19-Apr-2024 Jesper Dangaard Brouer Created this
*
*/
BEGIN
{
/* Cmdline arg#1: runtime threshold input in usec */
@threshold1_usecs = $1 ? $1: 10000;
@threshold1_ns = @threshold1_usecs * 1000;
/* Cmdline arg#2: wait time threshold input in usec */
@threshold2_usecs = $2 ? $2: 1000;
@threshold2_ns = @threshold2_usecs * 1000;
/* Cmdline arg#3: wait time threshold input in usec */
@threshold3_usecs = $3 ? $3: 100;
@threshold3_ns = @threshold3_usecs * 1000;
printf("Tracing latency caused by cgroup rstat\n");
printf(" - Will report on locked time above %d usecs (= %d ms)\n",
@threshold1_usecs, @threshold1_usecs / 1000);
printf(" - Will report on WAIT time above %d usecs (= %d ms)\n",
@threshold2_usecs, @threshold2_usecs / 1000);
printf(" - Will report on per-CPU (wait/locked) time above %d usecs (= %d ms)\n",
@threshold3_usecs, @threshold3_usecs / 1000);
printf("... Hit Ctrl-C to end.\n");
}
tracepoint:cgroup:cgroup_rstat_lock_contended
{
$now = nsecs;
/* Concurrency issue here? */
if (@wait_start[tid]) {
printf("Concurrency issue: tid[%d] comm[%s] %s\n",
tid, comm, probe);
}
@contended_cnt++;
@contended_cnt2++;
@wait_start[tid]=$now;
}
tracepoint:cgroup:cgroup_rstat_locked
{
$now = nsecs;
@locked_start[tid]=$now;
if (args->contended) {
$wait_time = $now - @wait_start[tid];
@wait_ns=hist($wait_time);
delete(@wait_start[tid]);
} else {
$wait = 0;
}
/* WARNING: Using @xxx++ is an expensive atomic operation.
* Recommend using @xxx = count()
* Not doing it here as "count" type cannot be used in printf
*/
@lock_cnt++;
@lock_cnt2++;
/* Detect if lock was yielded inside cgroup_rstat_flush_locked */
if (args->cpu >= 0) {
@yield_cnt++;
@yield_cnt2++;
}
/* Report/record on events over threshold.
* for lock contention on cgroup_rstat_lock.
*/
if ($wait_time >= @threshold2_ns ) {
$cgrp = args->id;
@stack_wait[tid, comm, $cgrp] = kstack;
time("%H:%M:%S ");
printf("High Lock-contention: wait: %d usec (%d ms) on CPU:%d cgrp:%d comm:%s\n",
$wait_time / 1000, $wait_time / 1000000,
cpu, $cgrp, comm);
}
}
tracepoint:cgroup:cgroup_rstat_unlock
{
$now = nsecs;
$locked_time = $now - @locked_start[tid];
@locked_ns = hist($locked_time);
/* Report/record on events over threshold.
* for lock contention on cgroup_rstat_lock.
*/
if ($locked_time >= @threshold1_ns ) {
$cgrp = args->id;
@stack_locked[tid, comm, $cgrp] = kstack;
time("%H:%M:%S ");
printf("Long lock-hold time: %d usec (%d ms) on CPU:%d cgrp:%d comm:%s\n",
$locked_time / 1000, $locked_time / 1000000,
cpu, $cgrp, comm);
}
delete(@locked_start[tid]);
}
/* Get a count on how many times flush is called.
* - Remember that lock can be yielded inside this function
* Thus, a single call can result in many unlock/lock calls
*/
kfunc:cgroup_rstat_flush_locked
{
@flush_calls++;
@flush_calls2++;
// @flush_calls = count();
}
/* Track high freq update events
* - WARNING comment-in for DEBUGGING only
* - overhead is mitigated via count() but still enable with caution
*
kfunc:__mod_memcg_state,
kfunc:__mod_memcg_lruvec_state,
kfunc:__count_memcg_events
{
// These functions calls: memcg_rstat_updated() inlined
@memcg_rstat_updated_cnt[probe] = count();
}
kfunc:cgroup_rstat_updated
{
// Notice: using count() which does per-cpu counter until read
// Don't use @cgroup_rstat_updated_cnt++ in a place like this!
@cgroup_rstat_updated_cnt = count();
}
* WARNING comment-in above for DEBUGGING only */
/* The per-CPU locks are different from global lock.
* - While holding global lock
* - Kernel walks for_each CPU talking these locks
* - Global lock can by yielded
* - Thus, per-CPU lock latency shows lower bound on latency possible
* - Reminder: This is slow-path "flush" code, not fast-path "update" code
*/
tracepoint:cgroup:cgroup_rstat_cpu_lock_contended
{
$now = nsecs;
/* Concurrency issue here? */
if (@wait_cpu_start[tid]) {
printf("Concurrency issue: tid[%d] comm[%s] %s\n",
tid, comm, probe);
}
@contended_cpu_cnt++;
@contended_cpu_cnt2++;
@wait_cpu_start[tid]=$now;
}
tracepoint:cgroup:cgroup_rstat_cpu_locked
{
$now = nsecs;
@locked_cpu_start[tid]=$now;
if (args->contended) {
$wait_time = $now - @wait_cpu_start[tid];
@wait_per_cpu_ns=hist($wait_time);
delete(@wait_cpu_start[tid]);
} else {
$wait = 0;
}
@lock_cpu_cnt = count();
@lock_cpu_cnt2 = count();
if ($wait_time >= @threshold3_ns ) {
$cgrp = args->id;
@stack_per_cpu_wait[tid, comm, $cgrp] = kstack;
time("%H:%M:%S ");
printf("PER_CPU - High CPU-Lock-contention: wait: %d usec (%d ms) on CPU:%d cgrp:%d comm:%s\n",
$wait_time / 1000, $wait_time / 1000000,
cpu, $cgrp, comm);
}
}
tracepoint:cgroup:cgroup_rstat_cpu_unlock
{
$now = nsecs;
$locked_time = $now - @locked_cpu_start[tid];
@locked_per_cpu_ns = hist($locked_time);
if ($locked_time >= @threshold3_ns ) {
$cgrp = args->id;
@stack_per_cpu_locked[tid, comm, $cgrp] = kstack;
time("%H:%M:%S ");
printf("PER_CPU - Long CPU-lock-hold time: %d usec (%d ms) on CPU:%d cgrp:%d comm:%s\n",
$locked_time / 1000, $locked_time / 1000000,
cpu, $cgrp, comm);
}
delete(@locked_cpu_start[tid]);
}
interval:s:1
{
$sec = (uint64) elapsed/1000000000;
time("\n%H:%M:%S ");
printf(" time elapsed: %d sec (interval = 1 sec)\n", $sec);
/* Rate per interval is calculate via resetting cnt2 vars */
$flush_interval = @flush_calls2; @flush_calls2 = 0;
$lock_interval = @lock_cnt2; @lock_cnt2 = 0;
$yield_interval = @yield_cnt2; @yield_cnt2 = 0;
$contended_interval = @contended_cnt2; @contended_cnt2 = 0;
$contended_cpu_interval = @contended_cpu_cnt2; @contended_cpu_cnt2 = 0;
printf(" Flushes(%d) %d/interval (avg %d/sec)\n",
@flush_calls, $flush_interval, @flush_calls / $sec);
printf(" Locks(%d) %d/interval (avg %d/sec)\n",
@lock_cnt, $lock_interval, @lock_cnt / $sec);
printf(" Yields(%d) %d/interval (avg %d/sec)\n",
@yield_cnt, $yield_interval, @yield_cnt / $sec);
printf(" Contended(%d) %d/interval (avg %d/sec)\n",
@contended_cnt, $contended_interval, @contended_cnt / $sec);
printf(" PER-CPU-contended(%d) %d/interval (avg %d/sec)\n",
@contended_cpu_cnt, $contended_cpu_interval, @contended_cpu_cnt / $sec);
//print(@wait_ns);
//print(@locked_ns);
/* For high-freq update events comment below in
*
printf("DEBUG: memcg_rstat_updated per/interval:\n");
print(@memcg_rstat_updated_cnt);
clear(@memcg_rstat_updated_cnt);
printf("DEBUG: cgroup_rstat_updated per/interval:\n");
print(@cgroup_rstat_updated_cnt);
clear(@cgroup_rstat_updated_cnt);
*/
/* How can we record rate spikes?*/
@flush_rates = hist($flush_interval);
@locks_rates = hist($lock_interval);
@yield_rates = hist($yield_interval);
@contended_rates = hist($contended_interval);
@PER_CPU_contended_rates = hist($contended_cpu_interval);
}
END
{ /* Default bpftrace will print all remaining maps at END */
clear(@threshold1_usecs);
clear(@threshold1_ns);
clear(@threshold2_usecs);
clear(@threshold2_ns);
/* Interval counter are not relevant to print at END */
clear(@contended_cnt2);
clear(@lock_cnt2);
clear(@yield_cnt2);
clear(@lock_cnt2);
clear(@flush_calls2);
time("%H:%M:%S ");
printf("END time elapsed: %d sec\n", elapsed / 1000000000);
}