-
Notifications
You must be signed in to change notification settings - Fork 5
/
ev_task.c
419 lines (351 loc) · 9.51 KB
/
ev_task.c
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/**
* LTTng to GTKwave trace conversion
*
* Authors:
* Ivan Djelic <[email protected]>
* Matthieu Castet <[email protected]>
*
* Copyright (C) 2013 Parrot S.A.
*/
#include "lttng2lxt.h"
#include "lttng.h"
#define PROCESS_STATE "proc.state.[%d-%d] %s"
#define PROCESS_INFO "proc.info.[%d-%d] %s (info)"
static void *root;
static struct task *current_task[MAX_CPU];
struct task *get_current_task(int cpu)
{
return current_task[cpu];
}
static int compare(const void *pa, const void *pb)
{
int a = ((struct task *)pa)->pid;
int b = ((struct task *)pb)->pid;
if (a < b)
return -1;
if (a > b)
return 1;
return 0;
}
static void update_task(struct task *task, const char *name, int pid, int tgid)
{
int need_refresh = 0;
if (name && strcmp(name, task->name)) {
free(task->name);
task->name = strdup(name);
need_refresh = 1;
}
if (pid && (pid != task->pid)) {
task->pid = pid;
need_refresh = 1;
}
if (tgid && (tgid != task->tgid)) {
task->tgid = tgid;
need_refresh = 1;
}
/* refresh trace name if necessary */
if (need_refresh) {
refresh_name(task->state_trace, PROCESS_STATE, task->tgid,
task->pid, task->name);
refresh_name(task->info_trace, PROCESS_INFO, task->tgid,
task->pid, task->name);
}
}
void emit_state_trace(struct task *task, union ltt_value value, int cpu)
{
char buf[16];
emit_trace(task->state_trace, value);
if (show_cpu_switch && task->current_cpu != cpu) {
task->current_cpu = cpu;
snprintf(buf, sizeof(buf), "%d", cpu);
emit_trace(task->info_trace, (union ltt_value)buf);
}
}
static struct task *new_task(const char *name, int pid)
{
struct task *task;
struct ltt_trace *data;
/* this can happen if we on the first cs from this process */
if (!name)
name = "????";
data = calloc(2, sizeof(struct ltt_trace));
assert(data);
task = malloc(sizeof(struct task));
assert(task);
task->name = strdup(name);
task->pid = pid;
/* tgid will be updated later */
task->tgid = 0;
task->state_trace = &data[0];
task->info_trace = &data[1];
task->mode = PROCESS_KERNEL;
task->current_cpu = -1;
init_trace(task->state_trace, TG_PROCESS,
1.0 + (task->tgid << 16) + task->pid,
TRACE_SYM_F_BITS, PROCESS_STATE, task->tgid, task->pid,
task->name);
init_trace(task->info_trace, /*TG_PROCESS*/0,
1.1 + (task->tgid << 16) + task->pid,
TRACE_SYM_F_STRING, PROCESS_INFO, task->tgid,
task->pid, task->name);
/* add new task to tree */
task = tsearch(task, &root, compare);
assert(task);
task = *((void **)task);
return task;
}
struct task *find_or_add_task(const char *name, int pid)
{
int cpu;
struct task task, *ret;
char buf[32];
task.pid = pid;
ret = tfind(&task, &root, compare);
if (name) {
/* show 'idle thread' instead of 'swapper' */
if (strcmp(name, "swapper") == 0) {
name = "idle thread";
} else if (sscanf(name, "swapper/%d", &cpu) == 1) {
snprintf(buf, sizeof(buf), "idle/%d thread", cpu);
name = buf;
} else {
snprintf(buf, sizeof(buf), "%s", name);
symbol_clean_name(buf);
name = buf;
}
}
if (!ret) {
ret = new_task(name, pid);
} else {
ret = *((void **)ret);
if (name)
/* just refresh task name */
update_task(ret, name, 0, 0);
}
return ret;
}
static
void lttng_statedump_process_state_process(const char *modname, int pass,
double clock, int cpu, void *args)
{
int tid, pid, /*type,*/ mode, status;
const char *name;
union ltt_value value;
struct task *task;
tid = (int)get_arg_u64(args, "tid");
if (pass == 1) {
name = get_arg_str(args, "name");
pid = (int)get_arg_u64(args, "pid");
task = find_or_add_task(name, tid);
/* set tgid */
update_task(task, NULL, 0, pid);
}
if (pass == 2) {
/*type = (int)get_arg_u64(args, "type");*/
mode = (int)get_arg_u64(args, "mode");
status = (int)get_arg_u64(args, "status");
task = find_or_add_task(NULL, tid);
switch (status) {
case LTTNG_WAIT_FORK:
case LTTNG_WAIT_CPU:
case LTTNG_WAIT:
/* skip processes which have not been running yet */
if (!task->state_trace->emitted)
return;
value = (union ltt_value)PROCESS_IDLE;
break;
case LTTNG_RUN:
task->mode = ((mode == LTTNG_USER_MODE) ?
PROCESS_USER : PROCESS_KERNEL);
value = (union ltt_value)task->mode;
break;
default:
case LTTNG_UNNAMED:
case LTTNG_DEAD:
case LTTNG_EXIT:
case LTTNG_ZOMBIE:
/* skip processes which have not been running yet */
if (!task->state_trace->emitted)
return;
value = (union ltt_value)PROCESS_DEAD;
break;
}
emit_state_trace(task, value, cpu);
}
}
MODULE(lttng_statedump_process_state);
static void sched_switch_process(const char *modname, int pass, double clock,
int cpu, void *args)
{
int prev_tid, next_tid, prev_state;
const char *prev_comm, *next_comm;
struct task *task;
/*
* prev_state:
{ 1, "S"} , { 2, "D" }, { 4, "T" }, { 8, "t" }, { 16, "Z" }, { 32, "X" },
{ 64, "x" }, { 128, "W" }) : "R",
*/
prev_comm = get_arg_str(args, "prev_comm");
prev_tid = (int)get_arg_u64(args, "prev_tid");
prev_state = (int)get_arg_u64(args, "prev_state");
next_comm = get_arg_str(args, "next_comm");
next_tid = (int)get_arg_u64(args, "next_tid");
/* hack to have different line for per cpu idle */
if (prev_tid == 0)
prev_tid = -cpu;
if (next_tid == 0)
next_tid = -cpu;
if (pass == 1) {
find_or_add_task(prev_comm, prev_tid);
find_or_add_task(next_comm, next_tid);
}
if (pass == 2) {
task = find_or_add_task(NULL, prev_tid);
/* restore idle state only if prev process is not dead */
if (!(prev_state & 0x70 /*XXX*/))
emit_state_trace(task, (union ltt_value)PROCESS_IDLE,
cpu);
else
emit_state_trace(task, (union ltt_value)PROCESS_DEAD,
cpu);
/* emit state of newly scheduled task */
task = find_or_add_task(NULL, next_tid);
emit_state_trace(task, (union ltt_value)task->mode, cpu);
current_task[cpu] = task;
if (next_tid <= 0)
set_cpu_idle(clock, cpu);
else
set_cpu_running(clock, cpu);
}
}
MODULE(sched_switch);
static void sched_wakeup_process(const char *modname, int pass, double clock,
int cpu, void *args)
{
int tid;
const char *comm;
struct task *task;
tid = (int)get_arg_u64(args, "tid");
if (pass == 1) {
comm = get_arg_str(args, "comm");
find_or_add_task(comm, tid);
}
if (pass == 2) {
task = find_or_add_task(NULL, tid);
emit_state_trace(task, (union ltt_value)PROCESS_WAKEUP, cpu);
}
}
MODULE(sched_wakeup);
static void sched_wakeup_new_process(const char *modname, int pass,
double clock, int cpu, void *args)
{
sched_wakeup_process(modname, pass, clock, cpu, args);
}
MODULE(sched_wakeup_new);
static void sched_process_wait_process(const char *modname, int pass,
double clock, int cpu, void *args)
{
int tid;
const char *comm;
struct task *task;
tid = (int)get_arg_u64(args, "tid");
/* why is tid == 0 on some sched_process_wait events ? */
if (!tid)
return;
if (pass == 1) {
comm = get_arg_str(args, "comm");
find_or_add_task(comm, tid);
}
if (pass == 2) {
task = find_or_add_task(NULL, tid);
emit_state_trace(task, (union ltt_value)PROCESS_IDLE, cpu);
}
}
MODULE(sched_process_wait);
/*
* 'sched_process_exit' happens way before the process really exits, track
* state using 'sched_process_free' instead.
*/
static void sched_process_free_process(const char *modname, int pass,
double clock, int cpu, void *args)
{
int tid;
const char *comm;
struct task *task;
tid = (int)get_arg_u64(args, "tid");
if (pass == 1) {
comm = get_arg_str(args, "comm");
find_or_add_task(comm, tid);
}
if (pass == 2) {
task = find_or_add_task(NULL, tid);
emit_state_trace(task, (union ltt_value)PROCESS_DEAD, cpu);
}
}
MODULE(sched_process_free);
static struct ltt_trace sched_fork[MAX_CPU];
static void sched_process_fork_process(const char *modname, int pass,
double clock, int cpu, void *args)
{
const char *parent_comm;
int parent_tid, child_tid;
if (pass == 1)
init_trace(&sched_fork[cpu], TG_GLOBAL, 1.0+0.1*cpu,
TRACE_SYM_F_STRING, "fork/%d", cpu);
if (pass == 2) {
parent_comm = get_arg_str(args, "parent_comm");
parent_tid = (int)get_arg_u64(args, "parent_tid");
child_tid = (int)get_arg_u64(args, "child_tid");
emit_trace(&sched_fork[cpu], (union ltt_value)"[%d] %s -> [%d]",
parent_tid, parent_comm, child_tid);
}
}
MODULE(sched_process_fork);
static void sched_process_exec_process(const char *modname, int pass,
double clock, int cpu, void *args)
{
int pid;
struct task *task;
if (pass == 1) {
pid = (int)get_arg_u64(args, "pid");
task = find_or_add_task(NULL, pid);
/* after exec we know this task's tgid */
update_task(task, NULL, 0, pid);
}
}
MODULE(sched_process_exec);
static void sched_migrate_task_process(const char *modname, int pass,
double clock, int cpu, void *args)
{
const char *comm;
struct task *task;
int orig_cpu, dest_cpu, tid;
tid = (int)get_arg_u64(args, "tid");
if (pass == 1) {
comm = get_arg_str(args, "comm");
(void)find_or_add_task(comm, tid);
}
if (pass == 2) {
orig_cpu = (int)get_arg_u64(args, "orig_cpu");
dest_cpu = (int)get_arg_u64(args, "dest_cpu");
task = find_or_add_task(NULL, tid);
if (task) {
emit_trace(task->info_trace,
(union ltt_value)"cpu%d->cpu%d",
orig_cpu, dest_cpu);
}
}
}
MODULE(sched_migrate_task);
static void sched_stat_runtime_process(const char *modname, int pass,
double clock, int cpu, void *args)
{
const char *comm;
int tid;
if (pass == 1) {
comm = get_arg_str(args, "comm");
tid = (int)get_arg_u64(args, "tid");
(void)find_or_add_task(comm, tid);
}
}
MODULE(sched_stat_runtime);