-
Notifications
You must be signed in to change notification settings - Fork 1
/
rt-plot.c
518 lines (437 loc) · 12.4 KB
/
rt-plot.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#include <gtk/gtk.h>
#include <string.h>
#include "trace-graph.h"
#include "list.h"
struct record_list {
struct record_list *next;
struct record *record;
};
/*
* Insert @record into @list, storing the record in @node.
*/
void insert_record(struct graph_info *ginfo, struct record_list *list,
struct record *record, struct record_list *node,
int reverse)
{
if (node->next)
die("Node is in use!");
if (!record) {
return;
}
node->record = record;
struct record_list *pos = list;
while (pos->next) {
unsigned long long next_rts = get_rts(ginfo, pos->next->record);
if (( reverse && next_rts < get_rts(ginfo, record)) ||
(!reverse && next_rts > get_rts(ginfo, record))){
break;
}
pos = pos->next;
}
node->next = pos->next;
pos->next = node;
}
/*
* Remove the first record in @list and put into @node.
*/
int pop_record(struct graph_info *ginfo, struct record_list *list,
struct record_list **node)
{
if (!list->next)
return 0;
*node = list->next;
list->next = list->next->next;
(*node)->next = 0;
return 1;
}
/* For communication between get_previous_release and its iterator */
struct prev_release_args {
unsigned long long min_ts;
struct rt_plot_common *common;
unsigned long long time;
int match_tid;
unsigned int *out_job;
unsigned long long *out_release;
unsigned long long *out_deadline;
};
static int
prev_release_iterator(struct graph_info *ginfo, struct record *rec, void *data)
{
int tid, is_release, job;
unsigned long long release, deadline;
struct prev_release_args *args = data;
if (get_rts(ginfo, rec) < args->min_ts) {
args->common->last_job.release = get_rts(ginfo, rec);
args->common->last_job.deadline = args->time;
return 0;
}
#define ARG ginfo, rec, &tid, &job, &release, &deadline
is_release = rt_graph_check_task_release(ARG) ||
rt_graph_check_server_release(ARG);
#undef ARG
if (is_release && args->match_tid == tid && release <= args->time) {
*(args->out_job) = job;
*(args->out_release) = release;
*(args->out_deadline) = deadline;
/* Cache to minimize work later */
args->common->last_job.no = job;
args->common->last_job.release = release;
args->common->last_job.deadline = deadline;
args->common->last_job.start = get_rts(ginfo, rec);
args->common->last_job.end = args->time;
return 0;
} else {
return 1;
}
}
/* For communication between find_prev_display_record and its iterator */
struct prev_display_args {
struct rt_plot_common *common;
struct record *result;
unsigned long long min_ts;
};
static int
prev_display_iterator(struct graph_info *ginfo, struct record *record, void *data)
{
int eid, ignored;
struct prev_display_args *args = data;
if (get_rts(ginfo, record) < args->min_ts) {
return 0;
}
eid = pevent_data_type(ginfo->pevent, record);
ignored = (eid == ginfo->event_sched_switch_id);
if (!ignored) {
ignored = args->common->is_drawn(ginfo, eid);
}
if (!ignored && args->common->record_matches(args->common, ginfo, record)) {
args->result = record;
++record->ref_count;
return 0;
} else {
return 1;
}
}
/*
* Return first displayed record before @time, abandoning search after @range.
*/
static struct record*
find_prev_display_record(struct graph_info *ginfo, struct rt_plot_common *rt_info,
unsigned long long time, unsigned long long range)
{
struct prev_display_args args = {rt_info, NULL, 0};
if (range) {
args.min_ts = time - range;
} else {
args.min_ts = time - max_rt_search(ginfo);
}
set_cpus_to_rts(ginfo, time);
iterate(ginfo, 1, prev_display_iterator, &args);
return args.result;
}
/**
* Return first relevant record after @time.
* @display: If set, only considers records which aren't plotted
*/
struct record*
__find_rt_record(struct graph_info *ginfo, struct rt_plot_common *rt_info,
guint64 time, int display, unsigned long long range)
{
int next_cpu, match, eid, ignored;
struct record *record = NULL;
set_cpus_to_rts(ginfo, time);
while ((record = tracecmd_read_next_data(ginfo->handle, &next_cpu))) {
if (range && get_rts(ginfo, record) >= time + range) {
free_record(record);
record = NULL;
break;
}
eid = pevent_data_type(ginfo->pevent, record);
ignored = (eid == ginfo->event_sched_switch_id);
if (!ignored && display) {
ignored = rt_info->is_drawn(ginfo, eid);
}
match = !ignored &&
rt_info->record_matches(rt_info, ginfo, record);
if (get_rts(ginfo, record) >= time && match)
break;
free_record(record);
};
return record;
}
/**
* rt_plot_display_info - write information about @time into @s
*/
int
rt_plot_display_info(struct graph_info *ginfo, struct graph_plot *plot,
struct trace_seq *s, unsigned long long time)
{
struct rt_plot_common *rt_info = plot->private;
struct event_format *event;
struct record *record = NULL, *prev_record = NULL, *data_record = NULL;
unsigned long long msec, nsec, rts, ptime, rtime, range;
long long pdiff, rdiff;
int eid;
/* Write plot-specific data */
data_record = rt_info->write_header(rt_info, ginfo, s, time);
/* Select closest relevant record */
range = 2 / ginfo->resolution;
record = __find_rt_record(ginfo, rt_info, time, 1, range);
prev_record = find_prev_display_record(ginfo, rt_info, time, range);
if (!record) {
record = prev_record;
} else if (prev_record) {
ptime = get_rts(ginfo, prev_record);
rtime = get_rts(ginfo, record);
pdiff = (ptime < time) ? time - ptime : ptime - time;
rdiff = (rtime < time) ? time - rtime : rtime - time;
if (pdiff < rdiff) {
free_record(record);
record = prev_record;
} else {
free_record(prev_record);
}
}
/* Write event info */
if (record) {
rts = get_rts(ginfo, record);
eid = pevent_data_type(ginfo->pevent, record);
if (in_res(ginfo, rts, time)) {
event = pevent_data_event_from_type(ginfo->pevent, eid);
if (event) {
trace_seq_putc(s, '\n');
trace_seq_puts(s, event->name);
trace_seq_putc(s, '\n');
pevent_event_info(s, event, record);
} else
trace_seq_printf(s, "\nUNKNOWN EVENT %d\n", eid);
}
free_record(record);
}
/* Metadata */
trace_seq_putc(s, '\n');
nano_to_milli(time, &msec, &nsec);
trace_seq_printf(s, "%llu.%06llu ms", msec, nsec);
if (data_record) {
trace_seq_printf(s, " CPU: %03d", data_record->cpu);
free_record(data_record);
}
return 1;
}
/**
* rt_plot_find_rt_record - return matching record around @time.
*/
struct record*
rt_plot_find_record(struct graph_info *ginfo, struct graph_plot *plot,
unsigned long long time)
{
return find_rt_record(ginfo, plot->private, time);
}
/**
* rt_plot_match_time - return 1 if there is an exact match at @time.
*/
int
rt_plot_match_time(struct graph_info *ginfo, struct graph_plot *plot,
unsigned long long time)
{
struct record *record = NULL;
struct rt_plot_common *rt_info = plot->private;
int next_cpu, match, ret;
set_cpus_to_rts(ginfo, time);
do {
free_record(record);
record = tracecmd_read_next_data(ginfo->handle, &next_cpu);
if (!record)
return 0;
match = rt_info->record_matches(rt_info, ginfo, record);
} while ((!match && get_rts(ginfo, record) < time + 1) ||
(match && get_rts(ginfo, record) < time));
if (record && get_rts(ginfo, record) == time)
ret = 1;
free_record(record);
return ret;
}
/**
* set_cpu_to_rts - seek CPU to a time closely preceding a real-time timestamp.
* @cpu: The CPU to seek
* @rt_target: RT time to seek towards
*
* This seeks to a real-time timestamp, not the default ftrace timestamps.
* The @cpu seek location will be placed on the last record whose timestamp
* is less than @rt_target.
*/
long long correction = 0;
unsigned long long set_cpu_to_rts(struct graph_info *ginfo,
unsigned long long rt_target, int cpu)
{
struct record *record, *last_record;
unsigned long long rts, seek_time;
long long next_diff;
seek_time = rt_target + correction;
tracecmd_set_cpu_to_timestamp(ginfo->handle, cpu, seek_time);
last_record = tracecmd_read_data(ginfo->handle, cpu);
rts = get_rts(ginfo, last_record);
if (rts < rt_target) {
while ((record = tracecmd_read_data(ginfo->handle, cpu))) {
if (get_rts(ginfo, record) >= rt_target) {
free_record(record);
break;
}
free_record(last_record);
last_record = record;
}
} else if (rts > rt_target) {
while ((record = tracecmd_read_prev(ginfo->handle, last_record))) {
if (get_rts(ginfo, record) <= rt_target) {
free_record(last_record);
last_record = record;
break;
}
free_record(last_record);
last_record = record;
}
}
if (last_record) {
next_diff = (last_record->ts - rt_target);
if (correction)
correction = correction *3/ 4 + (next_diff) / 4;
else
correction = next_diff;
tracecmd_set_cursor(ginfo->handle, cpu, last_record->offset);
free_record(last_record);
}
return rts;
}
/**
* set_cpus_to_time - seek all cpus to real-time @rt_target.
*/
unsigned long long set_cpus_to_rts(struct graph_info *ginfo, unsigned long long rt_target)
{
int cpu;
unsigned long long min_rts = ULLONG_MAX;
for (cpu = 0; cpu < ginfo->cpus; cpu++) {
unsigned long long rts = set_cpu_to_rts(ginfo, rt_target, cpu);
min_rts = MIN(min_rts, rts);
}
return min_rts;
}
/**
* is_task_running - return 1 if @match_pid is running at @time.
*/
int is_task_running(struct graph_info *ginfo,
unsigned long long time,
int match_pid)
{
int pid, job, cpu, running = 0;
unsigned long long ts, min_ts;
struct record *rec;
set_cpus_to_rts(ginfo, time);
min_ts = time - max_rt_search(ginfo);
for (cpu = 0; cpu < ginfo->cpus; cpu++) {
rec = tracecmd_peek_data(ginfo->handle, cpu);
if (!rec)
continue;
while ((rec = tracecmd_read_prev(ginfo->handle, rec))) {
if (get_rts(ginfo, rec) < min_ts)
goto out;
#define ARG ginfo, rec, &pid, &job, &ts
if (rt_graph_check_switch_away(ARG)) {
if (pid == match_pid)
goto out;
} else if (rt_graph_check_switch_to(ARG)) {
if (pid == match_pid) {
running = 1;
goto out;
}
}
#undef ARG
}
free_record(rec);
}
out:
free_record(rec);
return running;
}
/**
* iterate - pass records in real-time timestamp order to @cb.
*/
void iterate(struct graph_info *ginfo, int reverse, iterate_cb cb, void *data)
{
int proceed, cpu;
struct record *next, *prev;
struct record_list list;
struct record_list *nodes, *node;
nodes = malloc_or_die(sizeof(*nodes) * ginfo->cpus);
memset(nodes, 0, sizeof(*nodes) * ginfo->cpus);
memset(&list, 0, sizeof(list));
/* Start with the first record on each CPU */
for (cpu = 0; cpu < ginfo->cpus; cpu++) {
next = tracecmd_peek_data(ginfo->handle, cpu);
if (next) {
if (reverse) {
/* Reading backwards is clumsy...*/
prev = next;
next = tracecmd_read_prev(ginfo->handle, prev);
if (prev != next && prev->data)
free_record(prev);
} else {
next = tracecmd_read_data(ginfo->handle, cpu);
}
insert_record(ginfo, &list, next, &nodes[cpu], reverse);
}
}
/* Read record with the next timestamp until the callback is finished
* consuming data
*/
while (pop_record(ginfo, &list, &node)) {
next = node->record;
/* Pass records into callback */
proceed = cb(ginfo, next, data);
if (!proceed) {
free_record(next);
break;
}
/* Replace this record with the next record from the same CPU */
prev = next;
if (!reverse)
next = tracecmd_read_data(ginfo->handle, next->cpu);
else
next = tracecmd_read_prev(ginfo->handle, next);
free_record(prev);
if (prev != next)
insert_record(ginfo, &list, next, node, reverse);
}
/* Free unused records */
while (pop_record(ginfo, &list, &node)) {
free_record(node->record);
}
}
/**
* get_previous_release - return stats for the latest release of @match_tid
*
* This method will NOT re-seek the CPUs near time. The caller must have placed
* the CPUs near the the CPUs themselves.
*
* Returns release record and @out_job, @out_release, and @out_deadline if a
* release was found for @tid before @time.
*/
void get_previous_release(struct graph_info *ginfo, struct rt_plot_common *common,
int match_tid,
unsigned long long time,
int *out_job,
unsigned long long *out_release,
unsigned long long *out_deadline)
{
struct prev_release_args args = {
(time - max_rt_search(ginfo)), common, time, match_tid,
out_job, out_release, out_deadline};
/* Use cached job info, if possible */
if (time >= common->last_job.start &&
time <= common->last_job.end) {
*out_job = common->last_job.no;
*out_release = common->last_job.release;
*out_deadline = common->last_job.deadline;
return;
}
set_cpus_to_rts(ginfo, time);
iterate(ginfo, 1, prev_release_iterator, &args);
}