-
Notifications
You must be signed in to change notification settings - Fork 4
/
sched-time.py
397 lines (365 loc) · 11.5 KB
/
sched-time.py
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#!/usr/bin/python3
from bcc import BPF
import argparse
from time import sleep
import json
import copy
from collections import OrderedDict
def print_tasks(tasks):
last_tgid = 0
for k,v in tasks:
out_str = "Pid {} tgid {} runtime {} sleeptime {} iotime {} preemttime {}".format(
v.pid, v.tgid, v.run_time, v.sleep_time, v.io_time, v.preempt_time)
if last_tgid != v.tgid:
print(out_str)
last_tgid = v.tgid
else:
print("\t{}".format(out_str))
bpf_text = """
#include <uapi/linux/ptrace.h>
#include <linux/sched.h>
typedef struct val_s {
u32 pid;
u32 tgid;
u64 run_time;
u64 preempt_time;
u64 sleep_time;
u64 io_time;
u64 run_events;
u64 sleep_events;
u32 short_lived;
u32 priority;
} val_t;
typedef struct sleep_val_s {
u64 ts;
u64 state;
} sleep_val_t;
typedef struct wake_dep_s {
u32 waker_pid;
u32 sleeper_pid;
u32 tgid;
} wake_dep_t;
BPF_HASH(tasks, u64, val_t);
BPF_HASH(wake_deps, wake_dep_t);
BPF_HASH(start, u64);
BPF_HASH(end, u64, sleep_val_t);
BPF_HASH(futexes, u64);
int waker(struct pt_regs *ctx, struct task_struct *p)
{
u32 pid = p->pid;
u32 tgid = p->tgid;
if (!(PID_FILTER))
return 0;
u64 pid_key = bpf_get_current_pid_tgid();
pid = pid_key;
tgid = pid_key >> 32;
if (tgid != p->tgid)
return 0;
if (!(PID_FILTER))
return 0;
u64 *val = futexes.lookup(&pid_key);
if (!val)
return 0;
wake_dep_t info = {
.waker_pid = pid,
.sleeper_pid = p->pid,
.tgid = tgid,
};
u64 zero = 0;
val = wake_deps.lookup_or_init(&info, &zero);
(*val)++;
return 0;
}
int enter_futex(struct pt_regs *ctx)
{
u64 pid_key = bpf_get_current_pid_tgid();
u64 zero = 0;
futexes.lookup_or_init(&pid_key, &zero);
return 0;
}
int exit_futex(struct pt_regs *ctx)
{
u64 pid_key = bpf_get_current_pid_tgid();
futexes.delete(&pid_key);
return 0;
}
int oncpu(struct pt_regs *ctx, struct task_struct *prev)
{
u64 pid_key = bpf_get_current_pid_tgid();
u32 pid = pid_key;
u32 tgid = pid_key >> 32;
u64 ts, *tsp;
if (PID_FILTER) {
sleep_val_t *sval;
ts = bpf_ktime_get_ns();
start.update(&pid_key, &ts);
val_t zero = {
.pid = pid,
.tgid = tgid,
};
val_t *info = tasks.lookup_or_init(&pid_key, &zero);
sval = end.lookup(&pid_key);
if (sval) {
u64 sleep_delta = ts - sval->ts;
if (sval->state == TASK_RUNNING)
info->preempt_time += sleep_delta;
else if (sval->state & TASK_INTERRUPTIBLE) {
info->run_events++;
info->sleep_time += sleep_delta;
} else if (sval->state & TASK_UNINTERRUPTIBLE) {
info->run_events++;
info->io_time += sleep_delta;
}
}
end.delete(&pid_key);
}
pid = prev->pid;
tgid = prev->tgid;
pid_key = (u64)tgid << 32 | pid;
if (!(PID_FILTER))
return 0;
tsp = start.lookup(&pid_key);
if (tsp) {
u64 run_delta = bpf_ktime_get_ns() - *tsp;
start.delete(&pid_key);
val_t zero = {
.pid = pid,
.tgid = tgid,
};
val_t *info = tasks.lookup_or_init(&pid_key, &zero);
info->run_time += run_delta;
info->priority = prev->prio;
info->sleep_events++;
}
sleep_val_t sleep_val = {
.ts = bpf_ktime_get_ns(),
.state = prev->state,
};
end.update(&pid_key, &sleep_val);
return 0;
}
int trace_do_exit(struct pt_regs *ctx)
{
u64 pid = bpf_get_current_pid_tgid();
val_t *info = tasks.lookup(&pid);
if (!info)
return 0;
u64 ts = bpf_ktime_get_ns(), *tsp;
tsp = start.lookup(&pid);
if (tsp) {
u64 delta = ts - *tsp;
info->run_time += delta;
start.delete(&pid);
}
info->short_lived = 1;
return 0;
}
"""
parser = argparse.ArgumentParser(description="Summarize cpu usage of a task")
parser.add_argument("--pids", metavar='P', type=int, nargs='+',
help="List of pids to trace")
parser.add_argument("--tgids", metavar='T', type=int, nargs='+',
help="List of pids to trace")
parser.add_argument("--duration", default=99999999,
type=int, help="duration of trace, in seconds")
parser.add_argument("--rtapp", type=bool, default=False,
help="Output an rt-app config for the run")
args = parser.parse_args()
if not args.pids and not args.tgids:
print("Must specify tgid's or pids")
exit(1)
if args.pids and args.tgids:
print("Cannot specify tgid's and pidss")
exit(1)
duration = int(args.duration)
filter_str = ""
pids = []
tgids = []
if args.pids:
pids = args.pids
if args.tgids:
tgids = args.tgids
for p in pids:
this_str = "pid == {}".format(p)
if len(filter_str):
filter_str += "|| {}".format(this_str)
else:
filter_str = this_str
for p in tgids:
this_str = "tgid == {}".format(p)
if len(filter_str):
filter_str += "|| {}".format(this_str)
else:
filter_str = this_str
bpf_text = bpf_text.replace('PID_FILTER', filter_str)
b = BPF(text=bpf_text)
b.attach_kprobe(event="finish_task_switch", fn_name="oncpu")
if args.rtapp:
b.attach_kprobe(event="try_to_wake_up", fn_name="waker")
b.attach_kprobe(event="do_futex", fn_name="enter_futex")
b.attach_kretprobe(event="do_futex", fn_name="exit_futex")
try:
sleep(duration)
except KeyboardInterrupt:
pass
tasks = b.get_table("tasks")
sorted_tasks = sorted(tasks.items(), key=lambda run: run[1].tgid, reverse=True)
if not args.rtapp:
print_tasks(sorted_tasks)
exit(0)
waker_deps = b.get_table("wake_deps")
waker_sets = {}
for k,v in waker_deps.items():
waker = k.waker_pid
sleeper = k.sleeper_pid
# we add our waker to our list because consumers may wake producers to
# indicate they have completed their task
if waker not in waker_sets:
waker_sets[waker] = set([sleeper])
elif sleeper not in waker_sets[waker]:
waker_sets[waker].update([sleeper])
def reduce(waker_sets):
need_loop = True
groups = {}
counter = 0
while need_loop:
need_loop = False
producer = None
for pid,wakeset in waker_sets.items():
found = False
need_break = False
for name,base in groups.items():
if wakeset.issubset(base):
found = True
break
elif wakeset.issuperset(base):
found = True
groups[pid] = wakeset.copy()
groups.pop(name, None)
need_break = True
break
elif len(wakeset.intersection(base)):
need_break = True
waker_sets[pid] -= base
break
if need_break:
need_loop = True
break
if not found:
groups[pid] = wakeset.copy()
need_loop = True
return groups
groups = {}
loops = 0
while True or loops > 10:
loops += 1
blah = reduce(waker_sets)
if len(groups) != len(blah):
groups = blah
waker_sets = blah
else:
break
for k,v in groups.items():
if len(v) == 1:
groups.pop(k, None)
last_tgid = 0
threads_dict = {}
global_dict = {"duration": args.duration}
threads_list = []
for k,v in sorted_tasks:
if last_tgid != v.tgid:
if last_tgid != 0:
for name,actions in threads_dict['tasks'].items():
if actions['instance'] > 1:
actions['run'] /= actions['instance']
threads_list.append(copy.copy(threads_dict))
threads_dict = {}
threads_dict["global"] = global_dict
threads_dict["tasks"] = {}
last_tgid = v.tgid
total_time = 1000000
runtime = v.run_time + v.preempt_time
runevents = v.run_events
sleeptime = v.sleep_time + v.io_time
tdict = {}
if v.pid in groups:
tdict['loop'] = -1
tdict['instance'] = 1
if v.priority != 120:
tdict['priority'] = v.priority - 120
tdict['lock'] = 'mutex{}'.format(v.pid)
tdict['broad'] = 'shared{}'.format(v.pid)
tdict['unlock'] = 'mutex{}'.format(v.pid)
tdict['sleep'] = 0
threads_dict["tasks"][v.pid] = tdict
found = False
for pid,pidset in groups.items():
if v.pid in pidset:
found = True
name = "threads{}".format(pid)
priority = 0
if v.priority != 120:
priority = v.priority - 120
name = "threads{}priority{}".format(pid, priority)
if name not in threads_dict["tasks"]:
threads_dict["tasks"][name] = tdict
tdict['instance'] = 0
tdict['loop'] = -1
if v.priority != 120:
tdict['priority'] = v.priority - 120
tdict['lock'] = 'mutex{}'.format(pid)
tdict['wait'] = { 'ref': 'shared{}'.format(pid),
'mutex': 'mutex{}'.format(pid) }
tdict['unlock'] = 'mutex{}'.format(pid)
tdict['run'] = 0
else:
tdict = threads_dict["tasks"][name]
tdict['run'] += (runtime / 1000) / runevents
tdict['instance'] += 1
break
if found:
continue
tdict['instance'] = 1
tdict['loop'] = -1
tdict['run'] = (runtime * total_time) / (runtime + sleeptime)
if sleeptime > 0:
tdict['sleep'] = (sleeptime * total_time) / (runtime + sleeptime)
threads_dict["tasks"][v.pid] = tdict
# we need to load the wake deps into our dicts. This isn't super awesome
# because rt-app only does pthreads, so we'll lose any process->process wakeups,
# but those shouldn't matter too much. We also have to search all the task
# lists, because I'm shit at python and don't know a better way to do this
for name,actions in threads_dict['tasks'].items():
if actions['instance'] > 1:
actions['run'] /= actions['instance']
threads_list.append(threads_dict)
# for task in threads_list:
# if waker in task["tasks"] and sleeper in task["tasks"]:
# task["tasks"][waker].append(('resume', sleeper))
# if sleeper not in suspends:
# task["tasks"][sleeper].append(('suspend', sleeper))
# suspends.append(sleeper)
# break
# Now we have to sort our output. rt-app expects the thread instructions to be
# in the order that they are executed. We don't have to worry about sorting the
# threads themselves, just the actions. I shamelessly stole this from SO.
#
# The ordering for the waker should be
#
# loop->run->resume->sleep
#
# This is to simulate the producer getting a request, waking up the worker, and
# going to sleep until the next thing shows up. The ordering for the worker
# should be
#
# loop->suspend->run->sleep
#
# This simulates the thread waiting to be given work, waking up and then going
# back to sleep to wait for the next work set.
for task in threads_list:
sort_order = ['instance', 'loop', 'priority', 'lock', 'wait', 'broad', 'unlock', 'run',
'sleep']
for name,actions in task['tasks'].items():
task['tasks'][name] = OrderedDict(sorted(actions.iteritems(),
key=lambda (k, v): sort_order.index(k)))
print(json.dumps(task, indent=4))