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
|
#include <gtk/gtk.h>
#include "trace-graph.h"
/**
* 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)
{
int next_cpu, match, eid, ignored;
struct record *record;
set_cpus_to_rts(ginfo, time);
while ((record = tracecmd_read_next_data(ginfo->handle, &next_cpu))) {
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_last_event - write event name at @time onto plot.
*/
int
rt_plot_display_last_event(struct graph_info *ginfo, struct graph_plot *plot,
struct trace_seq *s, unsigned long long time)
{
int eid;
struct event_format *event;
struct record *record;
struct offset_cache *offsets;
struct rt_plot_common *rt_info = plot->private;
offsets = save_offsets(ginfo);
record = find_rt_display_record(ginfo, rt_info, time);
restore_offsets(ginfo, offsets);
if (!record)
return 0;
eid = pevent_data_type(ginfo->pevent, record);
event = pevent_data_event_from_type(ginfo->pevent, eid);
if (event)
trace_seq_puts(s, event->name);
else
trace_seq_printf(s, "UNKNOWN EVENT %d\n", eid);
trace_seq_putc(s, '\n');
trace_seq_printf(s, "CPU %d\n", record->cpu);
free_record(record);
return 1;
}
static struct record*
find_prev_record(struct graph_info *ginfo, struct rt_plot_common *rt_info,
unsigned long long time)
{
int eid, ignored, match, cpu;
struct record *prev, *res = NULL;
unsigned long long min_ts;
min_ts = time - max_rt_search(ginfo);
set_cpus_to_rts(ginfo, time);
for (cpu = 0; cpu < ginfo->cpus; cpu++) {
prev = tracecmd_peek_data(ginfo->handle, cpu);
while ((prev = tracecmd_read_prev(ginfo->handle, prev)) &&
get_rts(ginfo, prev) > min_ts) {
eid = pevent_data_type(ginfo->pevent, prev);
ignored = (eid == ginfo->event_sched_switch_id);
if (!ignored) {
ignored = rt_info->is_drawn(ginfo, eid);
}
match = !ignored &&
rt_info->record_matches(rt_info, ginfo, prev);
if (match) {
if (!res ||
get_rts(ginfo, prev) > get_rts(ginfo, res)) {
free_record(res);
res = prev;
}
break;
}
free_record(prev);
}
}
return res;
}
/**
* 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, *prev_record;
unsigned long long msec, nsec, rts;
int eid;
record = rt_info->write_header(rt_info, ginfo, s, time);
prev_record = find_prev_record(ginfo, rt_info, time);
if (!record || (prev_record && prev_record != record &&
(time - get_rts(ginfo, prev_record)) <
(get_rts(ginfo, record) - time))) {
free_record(record);
record = prev_record;
}
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);
}
trace_seq_putc(s, '\n');
nano_to_milli(time, &msec, &nsec);
trace_seq_printf(s, "%llu.%06llu ms CPU: %03d",
msec, nsec, record->cpu);
free_record(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;
}
/**
* next_rts - find a real-time timestamp AROUND an FTRACE time
* @ginfo: Current state of the graph
* @cpu: CPU to search
* @ft_target: FTRACE time to seek towards
*
* Returns the RT time of a record CLOSELY BEFORE @ft_time.
*/
unsigned long long
next_rts(struct graph_info *ginfo, int cpu, unsigned long long ft_target)
{
struct record *record;
unsigned long long ts = 0ULL;
tracecmd_set_cpu_to_timestamp(ginfo->handle, cpu, ft_target);
record = tracecmd_read_data(ginfo->handle, cpu);
if (record) {
ts = get_rts(ginfo, record);
free_record(record);
return ts;
} else
return 0;
}
/**
* set_cpu_to_rts - seek CPU to a time closely preceding a real-time timestamp
* @ginfo: Current state o the graph
* @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 before the given time, but will
* not necessarily be placed _right_ before the time.
*/
void
set_cpu_to_rts(struct graph_info *ginfo, unsigned long long rt_target, int cpu)
{
struct record *record;
unsigned long long last_rts, rts, seek_time, last_seek;
long long diff;
rts = next_rts(ginfo, cpu, rt_target);
diff = rt_target - rts;
/* "Guess" a new target based on difference */
seek_time = rt_target + diff;
rts = next_rts(ginfo, cpu, seek_time);
diff = rt_target - rts;
/* Zero in in 1.5x the difference increments */
if (rts && diff > 0) {
/* rts rt_target | real-time time
* seek ? | trace-cmd time
* ---|---->>----|--------
*/
do {
last_seek = seek_time;
last_rts = rts;
seek_time = seek_time + 1.5 * (rt_target - rts);
rts = next_rts(ginfo, cpu, seek_time);
} while (rts < rt_target && last_rts != rts);
tracecmd_set_cpu_to_timestamp(ginfo->handle, cpu, last_seek);
seek_time = last_seek;
} else if (rts && diff < 0) {
/* rt_target rts | real-time time
* ? seek | trace-cmd time
* ---|----<<----|--------
*/
do {
seek_time = seek_time - 1.5 * (rts - rt_target);
rts = next_rts(ginfo, cpu, seek_time);
} while (rts > rt_target);
}
/* Get to first record at or after time */
while ((record = tracecmd_read_data(ginfo->handle, cpu))) {
if (get_rts(ginfo, record) >= rt_target)
break;
free_record(record);
}
if (record) {
tracecmd_set_cursor(ginfo->handle, cpu, record->offset);
free_record(record);
} else
tracecmd_set_cpu_to_timestamp(ginfo->handle, cpu, seek_time);
}
/**
* set_cpus_to_time - seek all cpus to real-time @rt_target
*/
void set_cpus_to_rts(struct graph_info *ginfo, unsigned long long rt_target)
{
int cpu;
for (cpu = 0; cpu < ginfo->cpus; cpu++)
set_cpu_to_rts(ginfo, rt_target, cpu);
}
/**
* 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;
}
/**
* Find the information for the last release of @match_tid on @cpu before @time.
*
* 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.
*/
struct record* get_previous_release(struct graph_info *ginfo, int match_tid,
unsigned long long time,
int *out_job,
unsigned long long *out_release,
unsigned long long *out_deadline)
{
int tid, cpu, match, job;
unsigned long long release, deadline, min_ts;
struct record *last_rec = NULL, *rec, *ret = NULL;
min_ts = time - max_rt_search(ginfo);
/* The release record could have occurred on any CPU. Search all */
for (cpu = 0; cpu < ginfo->cpus; cpu++) {
last_rec = tracecmd_peek_data(ginfo->handle, cpu);
/* Require a record to start with */
if (!last_rec)
goto loop_end;
last_rec->ref_count++;
while ((rec = tracecmd_read_prev(ginfo->handle, last_rec))) {
if (rec->ts < min_ts) {
free_record(rec);
goto loop_end;
}
#define ARG ginfo, rec, &tid, &job, &release, &deadline
match = rt_graph_check_task_release(ARG) ||
rt_graph_check_server_release(ARG);
#undef ARG
free_record(last_rec);
last_rec = rec;
/* Only consider releases before the current time */
if (match && tid == match_tid && release <= time) {
/* Return the lastest release */
if (!ret || *out_job < job) {
free_record(ret);
ret = rec;
*out_job = job;
*out_release = release;
*out_deadline = deadline;
}
last_rec = NULL;
goto loop_end;
}
}
loop_end:
free_record(last_rec);
}
return ret;
}
|