Commit | Line | Data |
---|---|---|
0f70d8e9 YJ |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * builtin-kwork.c | |
4 | * | |
5 | * Copyright (c) 2022 Huawei Inc, Yang Jihong <yangjihong1@huawei.com> | |
6 | */ | |
7 | ||
8 | #include "builtin.h" | |
9 | ||
10 | #include "util/data.h" | |
628d6999 ACM |
11 | #include "util/evlist.h" |
12 | #include "util/evsel.h" | |
13 | #include "util/header.h" | |
0f70d8e9 YJ |
14 | #include "util/kwork.h" |
15 | #include "util/debug.h" | |
628d6999 | 16 | #include "util/session.h" |
0f70d8e9 YJ |
17 | #include "util/symbol.h" |
18 | #include "util/thread.h" | |
19 | #include "util/string2.h" | |
20 | #include "util/callchain.h" | |
21 | #include "util/evsel_fprintf.h" | |
f12ad272 | 22 | #include "util/util.h" |
0f70d8e9 YJ |
23 | |
24 | #include <subcmd/pager.h> | |
25 | #include <subcmd/parse-options.h> | |
378ef0f5 | 26 | #include <traceevent/event-parse.h> |
0f70d8e9 YJ |
27 | |
28 | #include <errno.h> | |
29 | #include <inttypes.h> | |
628d6999 | 30 | #include <signal.h> |
0f70d8e9 YJ |
31 | #include <linux/err.h> |
32 | #include <linux/time64.h> | |
33 | #include <linux/zalloc.h> | |
34 | ||
f98919ec YJ |
35 | /* |
36 | * report header elements width | |
37 | */ | |
38 | #define PRINT_CPU_WIDTH 4 | |
39 | #define PRINT_COUNT_WIDTH 9 | |
40 | #define PRINT_RUNTIME_WIDTH 10 | |
ad3d9f7a | 41 | #define PRINT_LATENCY_WIDTH 10 |
f98919ec YJ |
42 | #define PRINT_TIMESTAMP_WIDTH 17 |
43 | #define PRINT_KWORK_NAME_WIDTH 30 | |
44 | #define RPINT_DECIMAL_WIDTH 3 | |
bcc8b3e8 | 45 | #define PRINT_BRACKETPAIR_WIDTH 2 |
f98919ec YJ |
46 | #define PRINT_TIME_UNIT_SEC_WIDTH 2 |
47 | #define PRINT_TIME_UNIT_MESC_WIDTH 3 | |
55c40e50 YJ |
48 | #define PRINT_PID_WIDTH 7 |
49 | #define PRINT_TASK_NAME_WIDTH 16 | |
50 | #define PRINT_CPU_USAGE_WIDTH 6 | |
51 | #define PRINT_CPU_USAGE_DECIMAL_WIDTH 2 | |
52 | #define PRINT_CPU_USAGE_HIST_WIDTH 30 | |
f98919ec | 53 | #define PRINT_RUNTIME_HEADER_WIDTH (PRINT_RUNTIME_WIDTH + PRINT_TIME_UNIT_MESC_WIDTH) |
ad3d9f7a | 54 | #define PRINT_LATENCY_HEADER_WIDTH (PRINT_LATENCY_WIDTH + PRINT_TIME_UNIT_MESC_WIDTH) |
bcc8b3e8 | 55 | #define PRINT_TIMEHIST_CPU_WIDTH (PRINT_CPU_WIDTH + PRINT_BRACKETPAIR_WIDTH) |
f98919ec YJ |
56 | #define PRINT_TIMESTAMP_HEADER_WIDTH (PRINT_TIMESTAMP_WIDTH + PRINT_TIME_UNIT_SEC_WIDTH) |
57 | ||
58 | struct sort_dimension { | |
59 | const char *name; | |
60 | int (*cmp)(struct kwork_work *l, struct kwork_work *r); | |
61 | struct list_head list; | |
62 | }; | |
63 | ||
64 | static int id_cmp(struct kwork_work *l, struct kwork_work *r) | |
65 | { | |
66 | if (l->cpu > r->cpu) | |
67 | return 1; | |
68 | if (l->cpu < r->cpu) | |
69 | return -1; | |
70 | ||
71 | if (l->id > r->id) | |
72 | return 1; | |
73 | if (l->id < r->id) | |
74 | return -1; | |
75 | ||
76 | return 0; | |
77 | } | |
78 | ||
79 | static int count_cmp(struct kwork_work *l, struct kwork_work *r) | |
80 | { | |
81 | if (l->nr_atoms > r->nr_atoms) | |
82 | return 1; | |
83 | if (l->nr_atoms < r->nr_atoms) | |
84 | return -1; | |
85 | ||
86 | return 0; | |
87 | } | |
88 | ||
89 | static int runtime_cmp(struct kwork_work *l, struct kwork_work *r) | |
90 | { | |
91 | if (l->total_runtime > r->total_runtime) | |
92 | return 1; | |
93 | if (l->total_runtime < r->total_runtime) | |
94 | return -1; | |
95 | ||
96 | return 0; | |
97 | } | |
98 | ||
99 | static int max_runtime_cmp(struct kwork_work *l, struct kwork_work *r) | |
100 | { | |
101 | if (l->max_runtime > r->max_runtime) | |
102 | return 1; | |
103 | if (l->max_runtime < r->max_runtime) | |
104 | return -1; | |
105 | ||
106 | return 0; | |
107 | } | |
108 | ||
ad3d9f7a YJ |
109 | static int avg_latency_cmp(struct kwork_work *l, struct kwork_work *r) |
110 | { | |
111 | u64 avgl, avgr; | |
112 | ||
113 | if (!r->nr_atoms) | |
114 | return 1; | |
115 | if (!l->nr_atoms) | |
116 | return -1; | |
117 | ||
118 | avgl = l->total_latency / l->nr_atoms; | |
119 | avgr = r->total_latency / r->nr_atoms; | |
120 | ||
121 | if (avgl > avgr) | |
122 | return 1; | |
123 | if (avgl < avgr) | |
124 | return -1; | |
125 | ||
126 | return 0; | |
127 | } | |
128 | ||
129 | static int max_latency_cmp(struct kwork_work *l, struct kwork_work *r) | |
130 | { | |
131 | if (l->max_latency > r->max_latency) | |
132 | return 1; | |
133 | if (l->max_latency < r->max_latency) | |
134 | return -1; | |
135 | ||
136 | return 0; | |
137 | } | |
138 | ||
55c40e50 YJ |
139 | static int cpu_usage_cmp(struct kwork_work *l, struct kwork_work *r) |
140 | { | |
141 | if (l->cpu_usage > r->cpu_usage) | |
142 | return 1; | |
143 | if (l->cpu_usage < r->cpu_usage) | |
144 | return -1; | |
145 | ||
146 | return 0; | |
147 | } | |
148 | ||
f98919ec YJ |
149 | static int sort_dimension__add(struct perf_kwork *kwork __maybe_unused, |
150 | const char *tok, struct list_head *list) | |
151 | { | |
152 | size_t i; | |
153 | static struct sort_dimension max_sort_dimension = { | |
154 | .name = "max", | |
155 | .cmp = max_runtime_cmp, | |
156 | }; | |
157 | static struct sort_dimension id_sort_dimension = { | |
158 | .name = "id", | |
159 | .cmp = id_cmp, | |
160 | }; | |
161 | static struct sort_dimension runtime_sort_dimension = { | |
162 | .name = "runtime", | |
163 | .cmp = runtime_cmp, | |
164 | }; | |
165 | static struct sort_dimension count_sort_dimension = { | |
166 | .name = "count", | |
167 | .cmp = count_cmp, | |
168 | }; | |
ad3d9f7a YJ |
169 | static struct sort_dimension avg_sort_dimension = { |
170 | .name = "avg", | |
171 | .cmp = avg_latency_cmp, | |
172 | }; | |
55c40e50 YJ |
173 | static struct sort_dimension rate_sort_dimension = { |
174 | .name = "rate", | |
175 | .cmp = cpu_usage_cmp, | |
176 | }; | |
f98919ec YJ |
177 | struct sort_dimension *available_sorts[] = { |
178 | &id_sort_dimension, | |
179 | &max_sort_dimension, | |
180 | &count_sort_dimension, | |
181 | &runtime_sort_dimension, | |
ad3d9f7a | 182 | &avg_sort_dimension, |
55c40e50 | 183 | &rate_sort_dimension, |
f98919ec YJ |
184 | }; |
185 | ||
ad3d9f7a YJ |
186 | if (kwork->report == KWORK_REPORT_LATENCY) |
187 | max_sort_dimension.cmp = max_latency_cmp; | |
188 | ||
f98919ec YJ |
189 | for (i = 0; i < ARRAY_SIZE(available_sorts); i++) { |
190 | if (!strcmp(available_sorts[i]->name, tok)) { | |
191 | list_add_tail(&available_sorts[i]->list, list); | |
192 | return 0; | |
193 | } | |
194 | } | |
195 | ||
196 | return -1; | |
197 | } | |
198 | ||
199 | static void setup_sorting(struct perf_kwork *kwork, | |
200 | const struct option *options, | |
201 | const char * const usage_msg[]) | |
202 | { | |
203 | char *tmp, *tok, *str = strdup(kwork->sort_order); | |
204 | ||
205 | for (tok = strtok_r(str, ", ", &tmp); | |
206 | tok; tok = strtok_r(NULL, ", ", &tmp)) { | |
207 | if (sort_dimension__add(kwork, tok, &kwork->sort_list) < 0) | |
208 | usage_with_options_msg(usage_msg, options, | |
209 | "Unknown --sort key: `%s'", tok); | |
210 | } | |
211 | ||
212 | pr_debug("Sort order: %s\n", kwork->sort_order); | |
213 | free(str); | |
214 | } | |
215 | ||
216 | static struct kwork_atom *atom_new(struct perf_kwork *kwork, | |
217 | struct perf_sample *sample) | |
218 | { | |
219 | unsigned long i; | |
220 | struct kwork_atom_page *page; | |
221 | struct kwork_atom *atom = NULL; | |
222 | ||
223 | list_for_each_entry(page, &kwork->atom_page_list, list) { | |
224 | if (!bitmap_full(page->bitmap, NR_ATOM_PER_PAGE)) { | |
225 | i = find_first_zero_bit(page->bitmap, NR_ATOM_PER_PAGE); | |
226 | BUG_ON(i >= NR_ATOM_PER_PAGE); | |
227 | atom = &page->atoms[i]; | |
228 | goto found_atom; | |
229 | } | |
230 | } | |
231 | ||
232 | /* | |
233 | * new page | |
234 | */ | |
235 | page = zalloc(sizeof(*page)); | |
236 | if (page == NULL) { | |
237 | pr_err("Failed to zalloc kwork atom page\n"); | |
238 | return NULL; | |
239 | } | |
240 | ||
241 | i = 0; | |
242 | atom = &page->atoms[0]; | |
243 | list_add_tail(&page->list, &kwork->atom_page_list); | |
244 | ||
245 | found_atom: | |
49bd97c2 | 246 | __set_bit(i, page->bitmap); |
f98919ec YJ |
247 | atom->time = sample->time; |
248 | atom->prev = NULL; | |
249 | atom->page_addr = page; | |
250 | atom->bit_inpage = i; | |
251 | return atom; | |
252 | } | |
253 | ||
254 | static void atom_free(struct kwork_atom *atom) | |
255 | { | |
256 | if (atom->prev != NULL) | |
257 | atom_free(atom->prev); | |
258 | ||
49bd97c2 SC |
259 | __clear_bit(atom->bit_inpage, |
260 | ((struct kwork_atom_page *)atom->page_addr)->bitmap); | |
f98919ec YJ |
261 | } |
262 | ||
263 | static void atom_del(struct kwork_atom *atom) | |
264 | { | |
265 | list_del(&atom->list); | |
266 | atom_free(atom); | |
267 | } | |
268 | ||
269 | static int work_cmp(struct list_head *list, | |
270 | struct kwork_work *l, struct kwork_work *r) | |
271 | { | |
272 | int ret = 0; | |
273 | struct sort_dimension *sort; | |
274 | ||
275 | BUG_ON(list_empty(list)); | |
276 | ||
277 | list_for_each_entry(sort, list, list) { | |
278 | ret = sort->cmp(l, r); | |
279 | if (ret) | |
280 | return ret; | |
281 | } | |
282 | ||
283 | return ret; | |
284 | } | |
285 | ||
286 | static struct kwork_work *work_search(struct rb_root_cached *root, | |
287 | struct kwork_work *key, | |
288 | struct list_head *sort_list) | |
289 | { | |
290 | int cmp; | |
291 | struct kwork_work *work; | |
292 | struct rb_node *node = root->rb_root.rb_node; | |
293 | ||
294 | while (node) { | |
295 | work = container_of(node, struct kwork_work, node); | |
296 | cmp = work_cmp(sort_list, key, work); | |
297 | if (cmp > 0) | |
298 | node = node->rb_left; | |
299 | else if (cmp < 0) | |
300 | node = node->rb_right; | |
301 | else { | |
302 | if (work->name == NULL) | |
303 | work->name = key->name; | |
304 | return work; | |
305 | } | |
306 | } | |
307 | return NULL; | |
308 | } | |
309 | ||
310 | static void work_insert(struct rb_root_cached *root, | |
311 | struct kwork_work *key, struct list_head *sort_list) | |
312 | { | |
313 | int cmp; | |
314 | bool leftmost = true; | |
315 | struct kwork_work *cur; | |
316 | struct rb_node **new = &(root->rb_root.rb_node), *parent = NULL; | |
317 | ||
318 | while (*new) { | |
319 | cur = container_of(*new, struct kwork_work, node); | |
320 | parent = *new; | |
321 | cmp = work_cmp(sort_list, key, cur); | |
322 | ||
323 | if (cmp > 0) | |
324 | new = &((*new)->rb_left); | |
325 | else { | |
326 | new = &((*new)->rb_right); | |
327 | leftmost = false; | |
328 | } | |
329 | } | |
330 | ||
331 | rb_link_node(&key->node, parent, new); | |
332 | rb_insert_color_cached(&key->node, root, leftmost); | |
333 | } | |
334 | ||
335 | static struct kwork_work *work_new(struct kwork_work *key) | |
336 | { | |
337 | int i; | |
338 | struct kwork_work *work = zalloc(sizeof(*work)); | |
339 | ||
340 | if (work == NULL) { | |
341 | pr_err("Failed to zalloc kwork work\n"); | |
342 | return NULL; | |
343 | } | |
344 | ||
345 | for (i = 0; i < KWORK_TRACE_MAX; i++) | |
346 | INIT_LIST_HEAD(&work->atom_list[i]); | |
347 | ||
348 | work->id = key->id; | |
349 | work->cpu = key->cpu; | |
350 | work->name = key->name; | |
351 | work->class = key->class; | |
352 | return work; | |
353 | } | |
354 | ||
355 | static struct kwork_work *work_findnew(struct rb_root_cached *root, | |
356 | struct kwork_work *key, | |
357 | struct list_head *sort_list) | |
358 | { | |
daf07d22 | 359 | struct kwork_work *work = work_search(root, key, sort_list); |
f98919ec | 360 | |
f98919ec YJ |
361 | if (work != NULL) |
362 | return work; | |
363 | ||
364 | work = work_new(key); | |
daf07d22 YJ |
365 | if (work) |
366 | work_insert(root, work, sort_list); | |
f98919ec | 367 | |
f98919ec YJ |
368 | return work; |
369 | } | |
370 | ||
371 | static void profile_update_timespan(struct perf_kwork *kwork, | |
372 | struct perf_sample *sample) | |
373 | { | |
374 | if (!kwork->summary) | |
375 | return; | |
376 | ||
377 | if ((kwork->timestart == 0) || (kwork->timestart > sample->time)) | |
378 | kwork->timestart = sample->time; | |
379 | ||
380 | if (kwork->timeend < sample->time) | |
381 | kwork->timeend = sample->time; | |
382 | } | |
383 | ||
384 | static bool profile_event_match(struct perf_kwork *kwork, | |
385 | struct kwork_work *work, | |
386 | struct perf_sample *sample) | |
387 | { | |
388 | int cpu = work->cpu; | |
389 | u64 time = sample->time; | |
390 | struct perf_time_interval *ptime = &kwork->ptime; | |
391 | ||
392 | if ((kwork->cpu_list != NULL) && !test_bit(cpu, kwork->cpu_bitmap)) | |
393 | return false; | |
394 | ||
395 | if (((ptime->start != 0) && (ptime->start > time)) || | |
396 | ((ptime->end != 0) && (ptime->end < time))) | |
397 | return false; | |
398 | ||
399 | if ((kwork->profile_name != NULL) && | |
400 | (work->name != NULL) && | |
401 | (strcmp(work->name, kwork->profile_name) != 0)) | |
402 | return false; | |
403 | ||
404 | profile_update_timespan(kwork, sample); | |
405 | return true; | |
406 | } | |
407 | ||
408 | static int work_push_atom(struct perf_kwork *kwork, | |
409 | struct kwork_class *class, | |
410 | enum kwork_trace_type src_type, | |
411 | enum kwork_trace_type dst_type, | |
412 | struct evsel *evsel, | |
413 | struct perf_sample *sample, | |
414 | struct machine *machine, | |
86c67c8a YJ |
415 | struct kwork_work **ret_work, |
416 | bool overwrite) | |
f98919ec | 417 | { |
86c67c8a | 418 | struct kwork_atom *atom, *dst_atom, *last_atom; |
f98919ec YJ |
419 | struct kwork_work *work, key; |
420 | ||
421 | BUG_ON(class->work_init == NULL); | |
95064b33 | 422 | class->work_init(kwork, class, &key, src_type, evsel, sample, machine); |
f98919ec YJ |
423 | |
424 | atom = atom_new(kwork, sample); | |
425 | if (atom == NULL) | |
426 | return -1; | |
427 | ||
428 | work = work_findnew(&class->work_root, &key, &kwork->cmp_id); | |
429 | if (work == NULL) { | |
d3971008 | 430 | atom_free(atom); |
f98919ec YJ |
431 | return -1; |
432 | } | |
433 | ||
d3971008 YJ |
434 | if (!profile_event_match(kwork, work, sample)) { |
435 | atom_free(atom); | |
f98919ec | 436 | return 0; |
d3971008 | 437 | } |
f98919ec YJ |
438 | |
439 | if (dst_type < KWORK_TRACE_MAX) { | |
440 | dst_atom = list_last_entry_or_null(&work->atom_list[dst_type], | |
441 | struct kwork_atom, list); | |
442 | if (dst_atom != NULL) { | |
443 | atom->prev = dst_atom; | |
444 | list_del(&dst_atom->list); | |
445 | } | |
446 | } | |
447 | ||
448 | if (ret_work != NULL) | |
449 | *ret_work = work; | |
450 | ||
86c67c8a YJ |
451 | if (overwrite) { |
452 | last_atom = list_last_entry_or_null(&work->atom_list[src_type], | |
453 | struct kwork_atom, list); | |
454 | if (last_atom) { | |
455 | atom_del(last_atom); | |
456 | ||
457 | kwork->nr_skipped_events[src_type]++; | |
458 | kwork->nr_skipped_events[KWORK_TRACE_MAX]++; | |
459 | } | |
460 | } | |
461 | ||
f98919ec YJ |
462 | list_add_tail(&atom->list, &work->atom_list[src_type]); |
463 | ||
464 | return 0; | |
465 | } | |
466 | ||
467 | static struct kwork_atom *work_pop_atom(struct perf_kwork *kwork, | |
468 | struct kwork_class *class, | |
469 | enum kwork_trace_type src_type, | |
470 | enum kwork_trace_type dst_type, | |
471 | struct evsel *evsel, | |
472 | struct perf_sample *sample, | |
473 | struct machine *machine, | |
474 | struct kwork_work **ret_work) | |
475 | { | |
476 | struct kwork_atom *atom, *src_atom; | |
477 | struct kwork_work *work, key; | |
478 | ||
479 | BUG_ON(class->work_init == NULL); | |
95064b33 | 480 | class->work_init(kwork, class, &key, src_type, evsel, sample, machine); |
f98919ec YJ |
481 | |
482 | work = work_findnew(&class->work_root, &key, &kwork->cmp_id); | |
483 | if (ret_work != NULL) | |
484 | *ret_work = work; | |
485 | ||
486 | if (work == NULL) | |
487 | return NULL; | |
488 | ||
489 | if (!profile_event_match(kwork, work, sample)) | |
490 | return NULL; | |
491 | ||
492 | atom = list_last_entry_or_null(&work->atom_list[dst_type], | |
493 | struct kwork_atom, list); | |
494 | if (atom != NULL) | |
495 | return atom; | |
496 | ||
497 | src_atom = atom_new(kwork, sample); | |
498 | if (src_atom != NULL) | |
499 | list_add_tail(&src_atom->list, &work->atom_list[src_type]); | |
500 | else { | |
501 | if (ret_work != NULL) | |
502 | *ret_work = NULL; | |
503 | } | |
504 | ||
505 | return NULL; | |
506 | } | |
507 | ||
55c40e50 YJ |
508 | static struct kwork_work *find_work_by_id(struct rb_root_cached *root, |
509 | u64 id, int cpu) | |
510 | { | |
511 | struct rb_node *next; | |
512 | struct kwork_work *work; | |
513 | ||
514 | next = rb_first_cached(root); | |
515 | while (next) { | |
516 | work = rb_entry(next, struct kwork_work, node); | |
517 | if ((cpu != -1 && work->id == id && work->cpu == cpu) || | |
518 | (cpu == -1 && work->id == id)) | |
519 | return work; | |
520 | ||
521 | next = rb_next(next); | |
522 | } | |
523 | ||
524 | return NULL; | |
525 | } | |
526 | ||
527 | static struct kwork_class *get_kwork_class(struct perf_kwork *kwork, | |
528 | enum kwork_class_type type) | |
529 | { | |
530 | struct kwork_class *class; | |
531 | ||
532 | list_for_each_entry(class, &kwork->class_list, list) { | |
533 | if (class->type == type) | |
534 | return class; | |
535 | } | |
536 | ||
537 | return NULL; | |
538 | } | |
539 | ||
f98919ec YJ |
540 | static void report_update_exit_event(struct kwork_work *work, |
541 | struct kwork_atom *atom, | |
542 | struct perf_sample *sample) | |
543 | { | |
544 | u64 delta; | |
545 | u64 exit_time = sample->time; | |
546 | u64 entry_time = atom->time; | |
547 | ||
548 | if ((entry_time != 0) && (exit_time >= entry_time)) { | |
549 | delta = exit_time - entry_time; | |
550 | if ((delta > work->max_runtime) || | |
551 | (work->max_runtime == 0)) { | |
552 | work->max_runtime = delta; | |
553 | work->max_runtime_start = entry_time; | |
554 | work->max_runtime_end = exit_time; | |
555 | } | |
556 | work->total_runtime += delta; | |
557 | work->nr_atoms++; | |
558 | } | |
559 | } | |
560 | ||
561 | static int report_entry_event(struct perf_kwork *kwork, | |
562 | struct kwork_class *class, | |
563 | struct evsel *evsel, | |
564 | struct perf_sample *sample, | |
565 | struct machine *machine) | |
566 | { | |
567 | return work_push_atom(kwork, class, KWORK_TRACE_ENTRY, | |
568 | KWORK_TRACE_MAX, evsel, sample, | |
86c67c8a | 569 | machine, NULL, true); |
f98919ec YJ |
570 | } |
571 | ||
572 | static int report_exit_event(struct perf_kwork *kwork, | |
573 | struct kwork_class *class, | |
574 | struct evsel *evsel, | |
575 | struct perf_sample *sample, | |
576 | struct machine *machine) | |
577 | { | |
578 | struct kwork_atom *atom = NULL; | |
579 | struct kwork_work *work = NULL; | |
580 | ||
581 | atom = work_pop_atom(kwork, class, KWORK_TRACE_EXIT, | |
582 | KWORK_TRACE_ENTRY, evsel, sample, | |
583 | machine, &work); | |
584 | if (work == NULL) | |
585 | return -1; | |
586 | ||
587 | if (atom != NULL) { | |
588 | report_update_exit_event(work, atom, sample); | |
589 | atom_del(atom); | |
590 | } | |
591 | ||
592 | return 0; | |
593 | } | |
594 | ||
ad3d9f7a YJ |
595 | static void latency_update_entry_event(struct kwork_work *work, |
596 | struct kwork_atom *atom, | |
597 | struct perf_sample *sample) | |
598 | { | |
599 | u64 delta; | |
600 | u64 entry_time = sample->time; | |
601 | u64 raise_time = atom->time; | |
602 | ||
603 | if ((raise_time != 0) && (entry_time >= raise_time)) { | |
604 | delta = entry_time - raise_time; | |
605 | if ((delta > work->max_latency) || | |
606 | (work->max_latency == 0)) { | |
607 | work->max_latency = delta; | |
608 | work->max_latency_start = raise_time; | |
609 | work->max_latency_end = entry_time; | |
610 | } | |
611 | work->total_latency += delta; | |
612 | work->nr_atoms++; | |
613 | } | |
614 | } | |
615 | ||
616 | static int latency_raise_event(struct perf_kwork *kwork, | |
617 | struct kwork_class *class, | |
618 | struct evsel *evsel, | |
619 | struct perf_sample *sample, | |
620 | struct machine *machine) | |
621 | { | |
622 | return work_push_atom(kwork, class, KWORK_TRACE_RAISE, | |
623 | KWORK_TRACE_MAX, evsel, sample, | |
86c67c8a | 624 | machine, NULL, true); |
ad3d9f7a YJ |
625 | } |
626 | ||
627 | static int latency_entry_event(struct perf_kwork *kwork, | |
628 | struct kwork_class *class, | |
629 | struct evsel *evsel, | |
630 | struct perf_sample *sample, | |
631 | struct machine *machine) | |
632 | { | |
633 | struct kwork_atom *atom = NULL; | |
634 | struct kwork_work *work = NULL; | |
635 | ||
636 | atom = work_pop_atom(kwork, class, KWORK_TRACE_ENTRY, | |
637 | KWORK_TRACE_RAISE, evsel, sample, | |
638 | machine, &work); | |
639 | if (work == NULL) | |
640 | return -1; | |
641 | ||
642 | if (atom != NULL) { | |
643 | latency_update_entry_event(work, atom, sample); | |
644 | atom_del(atom); | |
645 | } | |
646 | ||
647 | return 0; | |
648 | } | |
649 | ||
bcc8b3e8 YJ |
650 | static void timehist_save_callchain(struct perf_kwork *kwork, |
651 | struct perf_sample *sample, | |
652 | struct evsel *evsel, | |
653 | struct machine *machine) | |
654 | { | |
655 | struct symbol *sym; | |
656 | struct thread *thread; | |
657 | struct callchain_cursor_node *node; | |
8ab12a20 | 658 | struct callchain_cursor *cursor; |
bcc8b3e8 YJ |
659 | |
660 | if (!kwork->show_callchain || sample->callchain == NULL) | |
661 | return; | |
662 | ||
663 | /* want main thread for process - has maps */ | |
664 | thread = machine__findnew_thread(machine, sample->pid, sample->pid); | |
665 | if (thread == NULL) { | |
666 | pr_debug("Failed to get thread for pid %d\n", sample->pid); | |
667 | return; | |
668 | } | |
669 | ||
8ab12a20 IR |
670 | cursor = get_tls_callchain_cursor(); |
671 | ||
bcc8b3e8 YJ |
672 | if (thread__resolve_callchain(thread, cursor, evsel, sample, |
673 | NULL, NULL, kwork->max_stack + 2) != 0) { | |
674 | pr_debug("Failed to resolve callchain, skipping\n"); | |
675 | goto out_put; | |
676 | } | |
677 | ||
678 | callchain_cursor_commit(cursor); | |
679 | ||
680 | while (true) { | |
681 | node = callchain_cursor_current(cursor); | |
682 | if (node == NULL) | |
683 | break; | |
684 | ||
685 | sym = node->ms.sym; | |
686 | if (sym) { | |
687 | if (!strcmp(sym->name, "__softirqentry_text_start") || | |
688 | !strcmp(sym->name, "__do_softirq")) | |
689 | sym->ignore = 1; | |
690 | } | |
691 | ||
692 | callchain_cursor_advance(cursor); | |
693 | } | |
694 | ||
695 | out_put: | |
696 | thread__put(thread); | |
697 | } | |
698 | ||
699 | static void timehist_print_event(struct perf_kwork *kwork, | |
700 | struct kwork_work *work, | |
701 | struct kwork_atom *atom, | |
702 | struct perf_sample *sample, | |
703 | struct addr_location *al) | |
704 | { | |
705 | char entrytime[32], exittime[32]; | |
706 | char kwork_name[PRINT_KWORK_NAME_WIDTH]; | |
707 | ||
708 | /* | |
709 | * runtime start | |
710 | */ | |
711 | timestamp__scnprintf_usec(atom->time, | |
712 | entrytime, sizeof(entrytime)); | |
713 | printf(" %*s ", PRINT_TIMESTAMP_WIDTH, entrytime); | |
714 | ||
715 | /* | |
716 | * runtime end | |
717 | */ | |
718 | timestamp__scnprintf_usec(sample->time, | |
719 | exittime, sizeof(exittime)); | |
720 | printf(" %*s ", PRINT_TIMESTAMP_WIDTH, exittime); | |
721 | ||
722 | /* | |
723 | * cpu | |
724 | */ | |
725 | printf(" [%0*d] ", PRINT_CPU_WIDTH, work->cpu); | |
726 | ||
727 | /* | |
728 | * kwork name | |
729 | */ | |
730 | if (work->class && work->class->work_name) { | |
731 | work->class->work_name(work, kwork_name, | |
732 | PRINT_KWORK_NAME_WIDTH); | |
733 | printf(" %-*s ", PRINT_KWORK_NAME_WIDTH, kwork_name); | |
734 | } else | |
735 | printf(" %-*s ", PRINT_KWORK_NAME_WIDTH, ""); | |
736 | ||
737 | /* | |
738 | *runtime | |
739 | */ | |
740 | printf(" %*.*f ", | |
741 | PRINT_RUNTIME_WIDTH, RPINT_DECIMAL_WIDTH, | |
742 | (double)(sample->time - atom->time) / NSEC_PER_MSEC); | |
743 | ||
744 | /* | |
745 | * delaytime | |
746 | */ | |
747 | if (atom->prev != NULL) | |
748 | printf(" %*.*f ", PRINT_LATENCY_WIDTH, RPINT_DECIMAL_WIDTH, | |
749 | (double)(atom->time - atom->prev->time) / NSEC_PER_MSEC); | |
750 | else | |
751 | printf(" %*s ", PRINT_LATENCY_WIDTH, " "); | |
752 | ||
753 | /* | |
754 | * callchain | |
755 | */ | |
756 | if (kwork->show_callchain) { | |
8ab12a20 IR |
757 | struct callchain_cursor *cursor = get_tls_callchain_cursor(); |
758 | ||
759 | if (cursor == NULL) | |
760 | return; | |
761 | ||
bcc8b3e8 | 762 | printf(" "); |
8ab12a20 | 763 | |
bcc8b3e8 YJ |
764 | sample__fprintf_sym(sample, al, 0, |
765 | EVSEL__PRINT_SYM | EVSEL__PRINT_ONELINE | | |
766 | EVSEL__PRINT_CALLCHAIN_ARROW | | |
767 | EVSEL__PRINT_SKIP_IGNORED, | |
8ab12a20 | 768 | cursor, symbol_conf.bt_stop_list, |
bcc8b3e8 YJ |
769 | stdout); |
770 | } | |
771 | ||
772 | printf("\n"); | |
773 | } | |
774 | ||
775 | static int timehist_raise_event(struct perf_kwork *kwork, | |
776 | struct kwork_class *class, | |
777 | struct evsel *evsel, | |
778 | struct perf_sample *sample, | |
779 | struct machine *machine) | |
780 | { | |
781 | return work_push_atom(kwork, class, KWORK_TRACE_RAISE, | |
782 | KWORK_TRACE_MAX, evsel, sample, | |
86c67c8a | 783 | machine, NULL, true); |
bcc8b3e8 YJ |
784 | } |
785 | ||
786 | static int timehist_entry_event(struct perf_kwork *kwork, | |
787 | struct kwork_class *class, | |
788 | struct evsel *evsel, | |
789 | struct perf_sample *sample, | |
790 | struct machine *machine) | |
791 | { | |
792 | int ret; | |
793 | struct kwork_work *work = NULL; | |
794 | ||
795 | ret = work_push_atom(kwork, class, KWORK_TRACE_ENTRY, | |
796 | KWORK_TRACE_RAISE, evsel, sample, | |
86c67c8a | 797 | machine, &work, true); |
bcc8b3e8 YJ |
798 | if (ret) |
799 | return ret; | |
800 | ||
801 | if (work != NULL) | |
802 | timehist_save_callchain(kwork, sample, evsel, machine); | |
803 | ||
804 | return 0; | |
805 | } | |
806 | ||
807 | static int timehist_exit_event(struct perf_kwork *kwork, | |
808 | struct kwork_class *class, | |
809 | struct evsel *evsel, | |
810 | struct perf_sample *sample, | |
811 | struct machine *machine) | |
812 | { | |
813 | struct kwork_atom *atom = NULL; | |
814 | struct kwork_work *work = NULL; | |
815 | struct addr_location al; | |
0dd5041c | 816 | int ret = 0; |
bcc8b3e8 | 817 | |
0dd5041c | 818 | addr_location__init(&al); |
bcc8b3e8 YJ |
819 | if (machine__resolve(machine, &al, sample) < 0) { |
820 | pr_debug("Problem processing event, skipping it\n"); | |
0dd5041c IR |
821 | ret = -1; |
822 | goto out; | |
bcc8b3e8 YJ |
823 | } |
824 | ||
825 | atom = work_pop_atom(kwork, class, KWORK_TRACE_EXIT, | |
826 | KWORK_TRACE_ENTRY, evsel, sample, | |
827 | machine, &work); | |
0dd5041c IR |
828 | if (work == NULL) { |
829 | ret = -1; | |
830 | goto out; | |
831 | } | |
bcc8b3e8 YJ |
832 | |
833 | if (atom != NULL) { | |
834 | work->nr_atoms++; | |
835 | timehist_print_event(kwork, work, atom, sample, &al); | |
836 | atom_del(atom); | |
837 | } | |
838 | ||
0dd5041c IR |
839 | out: |
840 | addr_location__exit(&al); | |
841 | return ret; | |
bcc8b3e8 YJ |
842 | } |
843 | ||
55c40e50 YJ |
844 | static void top_update_runtime(struct kwork_work *work, |
845 | struct kwork_atom *atom, | |
846 | struct perf_sample *sample) | |
847 | { | |
848 | u64 delta; | |
849 | u64 exit_time = sample->time; | |
850 | u64 entry_time = atom->time; | |
851 | ||
852 | if ((entry_time != 0) && (exit_time >= entry_time)) { | |
853 | delta = exit_time - entry_time; | |
854 | work->total_runtime += delta; | |
855 | } | |
856 | } | |
857 | ||
858 | static int top_entry_event(struct perf_kwork *kwork, | |
859 | struct kwork_class *class, | |
860 | struct evsel *evsel, | |
861 | struct perf_sample *sample, | |
862 | struct machine *machine) | |
863 | { | |
864 | return work_push_atom(kwork, class, KWORK_TRACE_ENTRY, | |
865 | KWORK_TRACE_MAX, evsel, sample, | |
866 | machine, NULL, true); | |
867 | } | |
868 | ||
2f21f5e4 YJ |
869 | static int top_exit_event(struct perf_kwork *kwork, |
870 | struct kwork_class *class, | |
871 | struct evsel *evsel, | |
872 | struct perf_sample *sample, | |
873 | struct machine *machine) | |
874 | { | |
875 | struct kwork_work *work, *sched_work; | |
876 | struct kwork_class *sched_class; | |
877 | struct kwork_atom *atom; | |
878 | ||
879 | atom = work_pop_atom(kwork, class, KWORK_TRACE_EXIT, | |
880 | KWORK_TRACE_ENTRY, evsel, sample, | |
881 | machine, &work); | |
882 | if (!work) | |
883 | return -1; | |
884 | ||
885 | if (atom) { | |
886 | sched_class = get_kwork_class(kwork, KWORK_CLASS_SCHED); | |
887 | if (sched_class) { | |
888 | sched_work = find_work_by_id(&sched_class->work_root, | |
889 | work->id, work->cpu); | |
890 | if (sched_work) | |
891 | top_update_runtime(work, atom, sample); | |
892 | } | |
893 | atom_del(atom); | |
894 | } | |
895 | ||
896 | return 0; | |
897 | } | |
898 | ||
55c40e50 YJ |
899 | static int top_sched_switch_event(struct perf_kwork *kwork, |
900 | struct kwork_class *class, | |
901 | struct evsel *evsel, | |
902 | struct perf_sample *sample, | |
903 | struct machine *machine) | |
904 | { | |
905 | struct kwork_atom *atom; | |
906 | struct kwork_work *work; | |
907 | ||
908 | atom = work_pop_atom(kwork, class, KWORK_TRACE_EXIT, | |
909 | KWORK_TRACE_ENTRY, evsel, sample, | |
910 | machine, &work); | |
911 | if (!work) | |
912 | return -1; | |
913 | ||
914 | if (atom) { | |
915 | top_update_runtime(work, atom, sample); | |
916 | atom_del(atom); | |
917 | } | |
918 | ||
919 | return top_entry_event(kwork, class, evsel, sample, machine); | |
920 | } | |
921 | ||
94348520 YJ |
922 | static struct kwork_class kwork_irq; |
923 | static int process_irq_handler_entry_event(struct perf_tool *tool, | |
924 | struct evsel *evsel, | |
925 | struct perf_sample *sample, | |
926 | struct machine *machine) | |
927 | { | |
928 | struct perf_kwork *kwork = container_of(tool, struct perf_kwork, tool); | |
929 | ||
930 | if (kwork->tp_handler->entry_event) | |
931 | return kwork->tp_handler->entry_event(kwork, &kwork_irq, | |
932 | evsel, sample, machine); | |
933 | return 0; | |
934 | } | |
935 | ||
936 | static int process_irq_handler_exit_event(struct perf_tool *tool, | |
937 | struct evsel *evsel, | |
938 | struct perf_sample *sample, | |
939 | struct machine *machine) | |
940 | { | |
941 | struct perf_kwork *kwork = container_of(tool, struct perf_kwork, tool); | |
942 | ||
943 | if (kwork->tp_handler->exit_event) | |
944 | return kwork->tp_handler->exit_event(kwork, &kwork_irq, | |
945 | evsel, sample, machine); | |
946 | return 0; | |
947 | } | |
948 | ||
4f8ae962 | 949 | const struct evsel_str_handler irq_tp_handlers[] = { |
94348520 YJ |
950 | { "irq:irq_handler_entry", process_irq_handler_entry_event, }, |
951 | { "irq:irq_handler_exit", process_irq_handler_exit_event, }, | |
4f8ae962 YJ |
952 | }; |
953 | ||
94348520 YJ |
954 | static int irq_class_init(struct kwork_class *class, |
955 | struct perf_session *session) | |
956 | { | |
957 | if (perf_session__set_tracepoints_handlers(session, irq_tp_handlers)) { | |
958 | pr_err("Failed to set irq tracepoints handlers\n"); | |
959 | return -1; | |
960 | } | |
961 | ||
962 | class->work_root = RB_ROOT_CACHED; | |
963 | return 0; | |
964 | } | |
965 | ||
2f21f5e4 | 966 | static void irq_work_init(struct perf_kwork *kwork, |
95064b33 | 967 | struct kwork_class *class, |
94348520 | 968 | struct kwork_work *work, |
95064b33 | 969 | enum kwork_trace_type src_type __maybe_unused, |
94348520 YJ |
970 | struct evsel *evsel, |
971 | struct perf_sample *sample, | |
972 | struct machine *machine __maybe_unused) | |
973 | { | |
974 | work->class = class; | |
975 | work->cpu = sample->cpu; | |
2f21f5e4 YJ |
976 | |
977 | if (kwork->report == KWORK_REPORT_TOP) { | |
978 | work->id = evsel__intval_common(evsel, sample, "common_pid"); | |
979 | work->name = NULL; | |
980 | } else { | |
981 | work->id = evsel__intval(evsel, sample, "irq"); | |
982 | work->name = evsel__strval(evsel, sample, "name"); | |
983 | } | |
94348520 YJ |
984 | } |
985 | ||
986 | static void irq_work_name(struct kwork_work *work, char *buf, int len) | |
987 | { | |
988 | snprintf(buf, len, "%s:%" PRIu64 "", work->name, work->id); | |
989 | } | |
990 | ||
4f8ae962 YJ |
991 | static struct kwork_class kwork_irq = { |
992 | .name = "irq", | |
993 | .type = KWORK_CLASS_IRQ, | |
994 | .nr_tracepoints = 2, | |
995 | .tp_handlers = irq_tp_handlers, | |
94348520 YJ |
996 | .class_init = irq_class_init, |
997 | .work_init = irq_work_init, | |
998 | .work_name = irq_work_name, | |
4f8ae962 YJ |
999 | }; |
1000 | ||
4c148191 | 1001 | static struct kwork_class kwork_softirq; |
19807bba YJ |
1002 | static int process_softirq_raise_event(struct perf_tool *tool, |
1003 | struct evsel *evsel, | |
1004 | struct perf_sample *sample, | |
1005 | struct machine *machine) | |
1006 | { | |
1007 | struct perf_kwork *kwork = container_of(tool, struct perf_kwork, tool); | |
1008 | ||
1009 | if (kwork->tp_handler->raise_event) | |
1010 | return kwork->tp_handler->raise_event(kwork, &kwork_softirq, | |
1011 | evsel, sample, machine); | |
1012 | ||
1013 | return 0; | |
1014 | } | |
1015 | ||
4c148191 YJ |
1016 | static int process_softirq_entry_event(struct perf_tool *tool, |
1017 | struct evsel *evsel, | |
1018 | struct perf_sample *sample, | |
1019 | struct machine *machine) | |
1020 | { | |
1021 | struct perf_kwork *kwork = container_of(tool, struct perf_kwork, tool); | |
1022 | ||
1023 | if (kwork->tp_handler->entry_event) | |
1024 | return kwork->tp_handler->entry_event(kwork, &kwork_softirq, | |
1025 | evsel, sample, machine); | |
1026 | ||
1027 | return 0; | |
1028 | } | |
1029 | ||
1030 | static int process_softirq_exit_event(struct perf_tool *tool, | |
1031 | struct evsel *evsel, | |
1032 | struct perf_sample *sample, | |
1033 | struct machine *machine) | |
1034 | { | |
1035 | struct perf_kwork *kwork = container_of(tool, struct perf_kwork, tool); | |
1036 | ||
1037 | if (kwork->tp_handler->exit_event) | |
1038 | return kwork->tp_handler->exit_event(kwork, &kwork_softirq, | |
1039 | evsel, sample, machine); | |
1040 | ||
1041 | return 0; | |
1042 | } | |
1043 | ||
e6439321 | 1044 | const struct evsel_str_handler softirq_tp_handlers[] = { |
19807bba | 1045 | { "irq:softirq_raise", process_softirq_raise_event, }, |
4c148191 YJ |
1046 | { "irq:softirq_entry", process_softirq_entry_event, }, |
1047 | { "irq:softirq_exit", process_softirq_exit_event, }, | |
e6439321 YJ |
1048 | }; |
1049 | ||
4c148191 YJ |
1050 | static int softirq_class_init(struct kwork_class *class, |
1051 | struct perf_session *session) | |
1052 | { | |
1053 | if (perf_session__set_tracepoints_handlers(session, | |
1054 | softirq_tp_handlers)) { | |
1055 | pr_err("Failed to set softirq tracepoints handlers\n"); | |
1056 | return -1; | |
1057 | } | |
1058 | ||
1059 | class->work_root = RB_ROOT_CACHED; | |
1060 | return 0; | |
1061 | } | |
1062 | ||
1063 | static char *evsel__softirq_name(struct evsel *evsel, u64 num) | |
1064 | { | |
1065 | char *name = NULL; | |
1066 | bool found = false; | |
1067 | struct tep_print_flag_sym *sym = NULL; | |
1068 | struct tep_print_arg *args = evsel->tp_format->print_fmt.args; | |
1069 | ||
1070 | if ((args == NULL) || (args->next == NULL)) | |
1071 | return NULL; | |
1072 | ||
1073 | /* skip softirq field: "REC->vec" */ | |
1074 | for (sym = args->next->symbol.symbols; sym != NULL; sym = sym->next) { | |
1075 | if ((eval_flag(sym->value) == (unsigned long long)num) && | |
1076 | (strlen(sym->str) != 0)) { | |
1077 | found = true; | |
1078 | break; | |
1079 | } | |
1080 | } | |
1081 | ||
1082 | if (!found) | |
1083 | return NULL; | |
1084 | ||
1085 | name = strdup(sym->str); | |
1086 | if (name == NULL) { | |
1087 | pr_err("Failed to copy symbol name\n"); | |
1088 | return NULL; | |
1089 | } | |
1090 | return name; | |
1091 | } | |
1092 | ||
95064b33 YJ |
1093 | static void softirq_work_init(struct perf_kwork *kwork __maybe_unused, |
1094 | struct kwork_class *class, | |
4c148191 | 1095 | struct kwork_work *work, |
95064b33 | 1096 | enum kwork_trace_type src_type __maybe_unused, |
4c148191 YJ |
1097 | struct evsel *evsel, |
1098 | struct perf_sample *sample, | |
1099 | struct machine *machine __maybe_unused) | |
1100 | { | |
1101 | u64 num = evsel__intval(evsel, sample, "vec"); | |
1102 | ||
1103 | work->id = num; | |
1104 | work->class = class; | |
1105 | work->cpu = sample->cpu; | |
1106 | work->name = evsel__softirq_name(evsel, num); | |
1107 | } | |
1108 | ||
1109 | static void softirq_work_name(struct kwork_work *work, char *buf, int len) | |
1110 | { | |
1111 | snprintf(buf, len, "(s)%s:%" PRIu64 "", work->name, work->id); | |
1112 | } | |
1113 | ||
e6439321 YJ |
1114 | static struct kwork_class kwork_softirq = { |
1115 | .name = "softirq", | |
1116 | .type = KWORK_CLASS_SOFTIRQ, | |
1117 | .nr_tracepoints = 3, | |
1118 | .tp_handlers = softirq_tp_handlers, | |
4c148191 YJ |
1119 | .class_init = softirq_class_init, |
1120 | .work_init = softirq_work_init, | |
1121 | .work_name = softirq_work_name, | |
e6439321 YJ |
1122 | }; |
1123 | ||
8dbc3c86 | 1124 | static struct kwork_class kwork_workqueue; |
53e49e32 YJ |
1125 | static int process_workqueue_activate_work_event(struct perf_tool *tool, |
1126 | struct evsel *evsel, | |
1127 | struct perf_sample *sample, | |
1128 | struct machine *machine) | |
1129 | { | |
1130 | struct perf_kwork *kwork = container_of(tool, struct perf_kwork, tool); | |
1131 | ||
1132 | if (kwork->tp_handler->raise_event) | |
1133 | return kwork->tp_handler->raise_event(kwork, &kwork_workqueue, | |
1134 | evsel, sample, machine); | |
1135 | ||
1136 | return 0; | |
1137 | } | |
1138 | ||
8dbc3c86 YJ |
1139 | static int process_workqueue_execute_start_event(struct perf_tool *tool, |
1140 | struct evsel *evsel, | |
1141 | struct perf_sample *sample, | |
1142 | struct machine *machine) | |
1143 | { | |
1144 | struct perf_kwork *kwork = container_of(tool, struct perf_kwork, tool); | |
1145 | ||
1146 | if (kwork->tp_handler->entry_event) | |
1147 | return kwork->tp_handler->entry_event(kwork, &kwork_workqueue, | |
1148 | evsel, sample, machine); | |
1149 | ||
1150 | return 0; | |
1151 | } | |
1152 | ||
1153 | static int process_workqueue_execute_end_event(struct perf_tool *tool, | |
1154 | struct evsel *evsel, | |
1155 | struct perf_sample *sample, | |
1156 | struct machine *machine) | |
1157 | { | |
1158 | struct perf_kwork *kwork = container_of(tool, struct perf_kwork, tool); | |
1159 | ||
1160 | if (kwork->tp_handler->exit_event) | |
1161 | return kwork->tp_handler->exit_event(kwork, &kwork_workqueue, | |
1162 | evsel, sample, machine); | |
1163 | ||
1164 | return 0; | |
1165 | } | |
1166 | ||
97179d9d | 1167 | const struct evsel_str_handler workqueue_tp_handlers[] = { |
53e49e32 | 1168 | { "workqueue:workqueue_activate_work", process_workqueue_activate_work_event, }, |
8dbc3c86 YJ |
1169 | { "workqueue:workqueue_execute_start", process_workqueue_execute_start_event, }, |
1170 | { "workqueue:workqueue_execute_end", process_workqueue_execute_end_event, }, | |
97179d9d YJ |
1171 | }; |
1172 | ||
8dbc3c86 YJ |
1173 | static int workqueue_class_init(struct kwork_class *class, |
1174 | struct perf_session *session) | |
1175 | { | |
1176 | if (perf_session__set_tracepoints_handlers(session, | |
1177 | workqueue_tp_handlers)) { | |
1178 | pr_err("Failed to set workqueue tracepoints handlers\n"); | |
1179 | return -1; | |
1180 | } | |
1181 | ||
1182 | class->work_root = RB_ROOT_CACHED; | |
1183 | return 0; | |
1184 | } | |
1185 | ||
95064b33 YJ |
1186 | static void workqueue_work_init(struct perf_kwork *kwork __maybe_unused, |
1187 | struct kwork_class *class, | |
8dbc3c86 | 1188 | struct kwork_work *work, |
95064b33 | 1189 | enum kwork_trace_type src_type __maybe_unused, |
8dbc3c86 YJ |
1190 | struct evsel *evsel, |
1191 | struct perf_sample *sample, | |
1192 | struct machine *machine) | |
1193 | { | |
1194 | char *modp = NULL; | |
1195 | unsigned long long function_addr = evsel__intval(evsel, | |
1196 | sample, "function"); | |
1197 | ||
1198 | work->class = class; | |
1199 | work->cpu = sample->cpu; | |
1200 | work->id = evsel__intval(evsel, sample, "work"); | |
1201 | work->name = function_addr == 0 ? NULL : | |
1202 | machine__resolve_kernel_addr(machine, &function_addr, &modp); | |
1203 | } | |
1204 | ||
1205 | static void workqueue_work_name(struct kwork_work *work, char *buf, int len) | |
1206 | { | |
1207 | if (work->name != NULL) | |
1208 | snprintf(buf, len, "(w)%s", work->name); | |
1209 | else | |
1210 | snprintf(buf, len, "(w)0x%" PRIx64, work->id); | |
1211 | } | |
1212 | ||
97179d9d YJ |
1213 | static struct kwork_class kwork_workqueue = { |
1214 | .name = "workqueue", | |
1215 | .type = KWORK_CLASS_WORKQUEUE, | |
1216 | .nr_tracepoints = 3, | |
1217 | .tp_handlers = workqueue_tp_handlers, | |
8dbc3c86 YJ |
1218 | .class_init = workqueue_class_init, |
1219 | .work_init = workqueue_work_init, | |
1220 | .work_name = workqueue_work_name, | |
97179d9d YJ |
1221 | }; |
1222 | ||
38d8d013 YJ |
1223 | static struct kwork_class kwork_sched; |
1224 | static int process_sched_switch_event(struct perf_tool *tool, | |
1225 | struct evsel *evsel, | |
1226 | struct perf_sample *sample, | |
1227 | struct machine *machine) | |
1228 | { | |
1229 | struct perf_kwork *kwork = container_of(tool, struct perf_kwork, tool); | |
1230 | ||
1231 | if (kwork->tp_handler->sched_switch_event) | |
1232 | return kwork->tp_handler->sched_switch_event(kwork, &kwork_sched, | |
1233 | evsel, sample, machine); | |
1234 | return 0; | |
1235 | } | |
1236 | ||
1237 | const struct evsel_str_handler sched_tp_handlers[] = { | |
1238 | { "sched:sched_switch", process_sched_switch_event, }, | |
1239 | }; | |
1240 | ||
1241 | static int sched_class_init(struct kwork_class *class, | |
1242 | struct perf_session *session) | |
1243 | { | |
1244 | if (perf_session__set_tracepoints_handlers(session, | |
1245 | sched_tp_handlers)) { | |
1246 | pr_err("Failed to set sched tracepoints handlers\n"); | |
1247 | return -1; | |
1248 | } | |
1249 | ||
1250 | class->work_root = RB_ROOT_CACHED; | |
1251 | return 0; | |
1252 | } | |
1253 | ||
1254 | static void sched_work_init(struct perf_kwork *kwork __maybe_unused, | |
1255 | struct kwork_class *class, | |
1256 | struct kwork_work *work, | |
1257 | enum kwork_trace_type src_type, | |
1258 | struct evsel *evsel, | |
1259 | struct perf_sample *sample, | |
1260 | struct machine *machine __maybe_unused) | |
1261 | { | |
1262 | work->class = class; | |
1263 | work->cpu = sample->cpu; | |
1264 | ||
1265 | if (src_type == KWORK_TRACE_EXIT) { | |
1266 | work->id = evsel__intval(evsel, sample, "prev_pid"); | |
1267 | work->name = strdup(evsel__strval(evsel, sample, "prev_comm")); | |
1268 | } else if (src_type == KWORK_TRACE_ENTRY) { | |
1269 | work->id = evsel__intval(evsel, sample, "next_pid"); | |
1270 | work->name = strdup(evsel__strval(evsel, sample, "next_comm")); | |
1271 | } | |
1272 | } | |
1273 | ||
1274 | static void sched_work_name(struct kwork_work *work, char *buf, int len) | |
1275 | { | |
1276 | snprintf(buf, len, "%s", work->name); | |
1277 | } | |
1278 | ||
1279 | static struct kwork_class kwork_sched = { | |
1280 | .name = "sched", | |
1281 | .type = KWORK_CLASS_SCHED, | |
1282 | .nr_tracepoints = ARRAY_SIZE(sched_tp_handlers), | |
1283 | .tp_handlers = sched_tp_handlers, | |
1284 | .class_init = sched_class_init, | |
1285 | .work_init = sched_work_init, | |
1286 | .work_name = sched_work_name, | |
1287 | }; | |
1288 | ||
0f70d8e9 | 1289 | static struct kwork_class *kwork_class_supported_list[KWORK_CLASS_MAX] = { |
4f8ae962 | 1290 | [KWORK_CLASS_IRQ] = &kwork_irq, |
e6439321 | 1291 | [KWORK_CLASS_SOFTIRQ] = &kwork_softirq, |
97179d9d | 1292 | [KWORK_CLASS_WORKQUEUE] = &kwork_workqueue, |
38d8d013 | 1293 | [KWORK_CLASS_SCHED] = &kwork_sched, |
0f70d8e9 YJ |
1294 | }; |
1295 | ||
f98919ec YJ |
1296 | static void print_separator(int len) |
1297 | { | |
1298 | printf(" %.*s\n", len, graph_dotted_line); | |
1299 | } | |
1300 | ||
1301 | static int report_print_work(struct perf_kwork *kwork, struct kwork_work *work) | |
1302 | { | |
1303 | int ret = 0; | |
1304 | char kwork_name[PRINT_KWORK_NAME_WIDTH]; | |
1305 | char max_runtime_start[32], max_runtime_end[32]; | |
ad3d9f7a | 1306 | char max_latency_start[32], max_latency_end[32]; |
f98919ec YJ |
1307 | |
1308 | printf(" "); | |
1309 | ||
1310 | /* | |
1311 | * kwork name | |
1312 | */ | |
1313 | if (work->class && work->class->work_name) { | |
1314 | work->class->work_name(work, kwork_name, | |
1315 | PRINT_KWORK_NAME_WIDTH); | |
1316 | ret += printf(" %-*s |", PRINT_KWORK_NAME_WIDTH, kwork_name); | |
1317 | } else { | |
1318 | ret += printf(" %-*s |", PRINT_KWORK_NAME_WIDTH, ""); | |
1319 | } | |
1320 | ||
1321 | /* | |
1322 | * cpu | |
1323 | */ | |
1324 | ret += printf(" %0*d |", PRINT_CPU_WIDTH, work->cpu); | |
1325 | ||
1326 | /* | |
1327 | * total runtime | |
1328 | */ | |
1329 | if (kwork->report == KWORK_REPORT_RUNTIME) { | |
1330 | ret += printf(" %*.*f ms |", | |
1331 | PRINT_RUNTIME_WIDTH, RPINT_DECIMAL_WIDTH, | |
1332 | (double)work->total_runtime / NSEC_PER_MSEC); | |
ad3d9f7a YJ |
1333 | } else if (kwork->report == KWORK_REPORT_LATENCY) { // avg delay |
1334 | ret += printf(" %*.*f ms |", | |
1335 | PRINT_LATENCY_WIDTH, RPINT_DECIMAL_WIDTH, | |
1336 | (double)work->total_latency / | |
1337 | work->nr_atoms / NSEC_PER_MSEC); | |
f98919ec YJ |
1338 | } |
1339 | ||
1340 | /* | |
1341 | * count | |
1342 | */ | |
1343 | ret += printf(" %*" PRIu64 " |", PRINT_COUNT_WIDTH, work->nr_atoms); | |
1344 | ||
1345 | /* | |
1346 | * max runtime, max runtime start, max runtime end | |
1347 | */ | |
1348 | if (kwork->report == KWORK_REPORT_RUNTIME) { | |
1349 | timestamp__scnprintf_usec(work->max_runtime_start, | |
1350 | max_runtime_start, | |
1351 | sizeof(max_runtime_start)); | |
1352 | timestamp__scnprintf_usec(work->max_runtime_end, | |
1353 | max_runtime_end, | |
1354 | sizeof(max_runtime_end)); | |
1355 | ret += printf(" %*.*f ms | %*s s | %*s s |", | |
1356 | PRINT_RUNTIME_WIDTH, RPINT_DECIMAL_WIDTH, | |
1357 | (double)work->max_runtime / NSEC_PER_MSEC, | |
1358 | PRINT_TIMESTAMP_WIDTH, max_runtime_start, | |
1359 | PRINT_TIMESTAMP_WIDTH, max_runtime_end); | |
1360 | } | |
ad3d9f7a YJ |
1361 | /* |
1362 | * max delay, max delay start, max delay end | |
1363 | */ | |
1364 | else if (kwork->report == KWORK_REPORT_LATENCY) { | |
1365 | timestamp__scnprintf_usec(work->max_latency_start, | |
1366 | max_latency_start, | |
1367 | sizeof(max_latency_start)); | |
1368 | timestamp__scnprintf_usec(work->max_latency_end, | |
1369 | max_latency_end, | |
1370 | sizeof(max_latency_end)); | |
1371 | ret += printf(" %*.*f ms | %*s s | %*s s |", | |
1372 | PRINT_LATENCY_WIDTH, RPINT_DECIMAL_WIDTH, | |
1373 | (double)work->max_latency / NSEC_PER_MSEC, | |
1374 | PRINT_TIMESTAMP_WIDTH, max_latency_start, | |
1375 | PRINT_TIMESTAMP_WIDTH, max_latency_end); | |
1376 | } | |
f98919ec YJ |
1377 | |
1378 | printf("\n"); | |
1379 | return ret; | |
1380 | } | |
1381 | ||
1382 | static int report_print_header(struct perf_kwork *kwork) | |
1383 | { | |
1384 | int ret; | |
1385 | ||
1386 | printf("\n "); | |
1387 | ret = printf(" %-*s | %-*s |", | |
1388 | PRINT_KWORK_NAME_WIDTH, "Kwork Name", | |
1389 | PRINT_CPU_WIDTH, "Cpu"); | |
1390 | ||
1391 | if (kwork->report == KWORK_REPORT_RUNTIME) { | |
1392 | ret += printf(" %-*s |", | |
1393 | PRINT_RUNTIME_HEADER_WIDTH, "Total Runtime"); | |
ad3d9f7a YJ |
1394 | } else if (kwork->report == KWORK_REPORT_LATENCY) { |
1395 | ret += printf(" %-*s |", | |
1396 | PRINT_LATENCY_HEADER_WIDTH, "Avg delay"); | |
f98919ec YJ |
1397 | } |
1398 | ||
1399 | ret += printf(" %-*s |", PRINT_COUNT_WIDTH, "Count"); | |
1400 | ||
1401 | if (kwork->report == KWORK_REPORT_RUNTIME) { | |
1402 | ret += printf(" %-*s | %-*s | %-*s |", | |
1403 | PRINT_RUNTIME_HEADER_WIDTH, "Max runtime", | |
1404 | PRINT_TIMESTAMP_HEADER_WIDTH, "Max runtime start", | |
1405 | PRINT_TIMESTAMP_HEADER_WIDTH, "Max runtime end"); | |
ad3d9f7a YJ |
1406 | } else if (kwork->report == KWORK_REPORT_LATENCY) { |
1407 | ret += printf(" %-*s | %-*s | %-*s |", | |
1408 | PRINT_LATENCY_HEADER_WIDTH, "Max delay", | |
1409 | PRINT_TIMESTAMP_HEADER_WIDTH, "Max delay start", | |
1410 | PRINT_TIMESTAMP_HEADER_WIDTH, "Max delay end"); | |
f98919ec YJ |
1411 | } |
1412 | ||
1413 | printf("\n"); | |
1414 | print_separator(ret); | |
1415 | return ret; | |
1416 | } | |
1417 | ||
bcc8b3e8 YJ |
1418 | static void timehist_print_header(void) |
1419 | { | |
1420 | /* | |
1421 | * header row | |
1422 | */ | |
1423 | printf(" %-*s %-*s %-*s %-*s %-*s %-*s\n", | |
1424 | PRINT_TIMESTAMP_WIDTH, "Runtime start", | |
1425 | PRINT_TIMESTAMP_WIDTH, "Runtime end", | |
1426 | PRINT_TIMEHIST_CPU_WIDTH, "Cpu", | |
1427 | PRINT_KWORK_NAME_WIDTH, "Kwork name", | |
1428 | PRINT_RUNTIME_WIDTH, "Runtime", | |
1429 | PRINT_RUNTIME_WIDTH, "Delaytime"); | |
1430 | ||
1431 | /* | |
1432 | * units row | |
1433 | */ | |
1434 | printf(" %-*s %-*s %-*s %-*s %-*s %-*s\n", | |
1435 | PRINT_TIMESTAMP_WIDTH, "", | |
1436 | PRINT_TIMESTAMP_WIDTH, "", | |
1437 | PRINT_TIMEHIST_CPU_WIDTH, "", | |
1438 | PRINT_KWORK_NAME_WIDTH, "(TYPE)NAME:NUM", | |
1439 | PRINT_RUNTIME_WIDTH, "(msec)", | |
1440 | PRINT_RUNTIME_WIDTH, "(msec)"); | |
1441 | ||
1442 | /* | |
1443 | * separator | |
1444 | */ | |
1445 | printf(" %.*s %.*s %.*s %.*s %.*s %.*s\n", | |
1446 | PRINT_TIMESTAMP_WIDTH, graph_dotted_line, | |
1447 | PRINT_TIMESTAMP_WIDTH, graph_dotted_line, | |
1448 | PRINT_TIMEHIST_CPU_WIDTH, graph_dotted_line, | |
1449 | PRINT_KWORK_NAME_WIDTH, graph_dotted_line, | |
1450 | PRINT_RUNTIME_WIDTH, graph_dotted_line, | |
1451 | PRINT_RUNTIME_WIDTH, graph_dotted_line); | |
1452 | } | |
1453 | ||
f98919ec YJ |
1454 | static void print_summary(struct perf_kwork *kwork) |
1455 | { | |
1456 | u64 time = kwork->timeend - kwork->timestart; | |
1457 | ||
1458 | printf(" Total count : %9" PRIu64 "\n", kwork->all_count); | |
1459 | printf(" Total runtime (msec) : %9.3f (%.3f%% load average)\n", | |
1460 | (double)kwork->all_runtime / NSEC_PER_MSEC, | |
1461 | time == 0 ? 0 : (double)kwork->all_runtime / time); | |
1462 | printf(" Total time span (msec) : %9.3f\n", | |
1463 | (double)time / NSEC_PER_MSEC); | |
1464 | } | |
1465 | ||
1466 | static unsigned long long nr_list_entry(struct list_head *head) | |
1467 | { | |
1468 | struct list_head *pos; | |
1469 | unsigned long long n = 0; | |
1470 | ||
1471 | list_for_each(pos, head) | |
1472 | n++; | |
1473 | ||
1474 | return n; | |
1475 | } | |
1476 | ||
1477 | static void print_skipped_events(struct perf_kwork *kwork) | |
1478 | { | |
1479 | int i; | |
1480 | const char *const kwork_event_str[] = { | |
ad3d9f7a | 1481 | [KWORK_TRACE_RAISE] = "raise", |
f98919ec YJ |
1482 | [KWORK_TRACE_ENTRY] = "entry", |
1483 | [KWORK_TRACE_EXIT] = "exit", | |
1484 | }; | |
1485 | ||
1486 | if ((kwork->nr_skipped_events[KWORK_TRACE_MAX] != 0) && | |
1487 | (kwork->nr_events != 0)) { | |
1488 | printf(" INFO: %.3f%% skipped events (%" PRIu64 " including ", | |
1489 | (double)kwork->nr_skipped_events[KWORK_TRACE_MAX] / | |
1490 | (double)kwork->nr_events * 100.0, | |
1491 | kwork->nr_skipped_events[KWORK_TRACE_MAX]); | |
1492 | ||
1493 | for (i = 0; i < KWORK_TRACE_MAX; i++) { | |
1494 | printf("%" PRIu64 " %s%s", | |
1495 | kwork->nr_skipped_events[i], | |
1496 | kwork_event_str[i], | |
1497 | (i == KWORK_TRACE_MAX - 1) ? ")\n" : ", "); | |
1498 | } | |
1499 | } | |
1500 | ||
1501 | if (verbose > 0) | |
1502 | printf(" INFO: use %lld atom pages\n", | |
1503 | nr_list_entry(&kwork->atom_page_list)); | |
1504 | } | |
1505 | ||
1506 | static void print_bad_events(struct perf_kwork *kwork) | |
1507 | { | |
1508 | if ((kwork->nr_lost_events != 0) && (kwork->nr_events != 0)) { | |
1509 | printf(" INFO: %.3f%% lost events (%ld out of %ld, in %ld chunks)\n", | |
1510 | (double)kwork->nr_lost_events / | |
1511 | (double)kwork->nr_events * 100.0, | |
1512 | kwork->nr_lost_events, kwork->nr_events, | |
1513 | kwork->nr_lost_chunks); | |
1514 | } | |
1515 | } | |
1516 | ||
55c40e50 YJ |
1517 | const char *graph_load = "||||||||||||||||||||||||||||||||||||||||||||||||"; |
1518 | const char *graph_idle = " "; | |
1519 | static void top_print_per_cpu_load(struct perf_kwork *kwork) | |
1520 | { | |
1521 | int i, load_width; | |
1522 | u64 total, load, load_ratio; | |
1523 | struct kwork_top_stat *stat = &kwork->top_stat; | |
1524 | ||
1525 | for (i = 0; i < MAX_NR_CPUS; i++) { | |
1526 | total = stat->cpus_runtime[i].total; | |
1527 | load = stat->cpus_runtime[i].load; | |
1528 | if (test_bit(i, stat->all_cpus_bitmap) && total) { | |
1529 | load_ratio = load * 10000 / total; | |
1530 | load_width = PRINT_CPU_USAGE_HIST_WIDTH * | |
1531 | load_ratio / 10000; | |
1532 | ||
1533 | printf("%%Cpu%-*d[%.*s%.*s %*.*f%%]\n", | |
1534 | PRINT_CPU_WIDTH, i, | |
1535 | load_width, graph_load, | |
1536 | PRINT_CPU_USAGE_HIST_WIDTH - load_width, | |
1537 | graph_idle, | |
1538 | PRINT_CPU_USAGE_WIDTH, | |
1539 | PRINT_CPU_USAGE_DECIMAL_WIDTH, | |
1540 | (double)load_ratio / 100); | |
1541 | } | |
1542 | } | |
1543 | } | |
1544 | ||
1545 | static void top_print_cpu_usage(struct perf_kwork *kwork) | |
1546 | { | |
1547 | struct kwork_top_stat *stat = &kwork->top_stat; | |
1548 | u64 idle_time = stat->cpus_runtime[MAX_NR_CPUS].idle; | |
2f21f5e4 | 1549 | u64 hardirq_time = stat->cpus_runtime[MAX_NR_CPUS].irq; |
55c40e50 YJ |
1550 | int cpus_nr = bitmap_weight(stat->all_cpus_bitmap, MAX_NR_CPUS); |
1551 | u64 cpus_total_time = stat->cpus_runtime[MAX_NR_CPUS].total; | |
1552 | ||
1553 | printf("Total : %*.*f ms, %d cpus\n", | |
1554 | PRINT_RUNTIME_WIDTH, RPINT_DECIMAL_WIDTH, | |
1555 | (double)cpus_total_time / NSEC_PER_MSEC, | |
1556 | cpus_nr); | |
1557 | ||
2f21f5e4 YJ |
1558 | printf("%%Cpu(s): %*.*f%% id, %*.*f%% hi\n", |
1559 | PRINT_CPU_USAGE_WIDTH, PRINT_CPU_USAGE_DECIMAL_WIDTH, | |
1560 | cpus_total_time ? (double)idle_time * 100 / cpus_total_time : 0, | |
1561 | ||
55c40e50 | 1562 | PRINT_CPU_USAGE_WIDTH, PRINT_CPU_USAGE_DECIMAL_WIDTH, |
2f21f5e4 | 1563 | cpus_total_time ? (double)hardirq_time * 100 / cpus_total_time : 0); |
55c40e50 YJ |
1564 | |
1565 | top_print_per_cpu_load(kwork); | |
1566 | } | |
1567 | ||
1568 | static void top_print_header(struct perf_kwork *kwork __maybe_unused) | |
1569 | { | |
1570 | int ret; | |
1571 | ||
1572 | printf("\n "); | |
1573 | ret = printf(" %*s %*s %*s %-*s", | |
1574 | PRINT_PID_WIDTH, "PID", | |
1575 | PRINT_CPU_USAGE_WIDTH, "%CPU", | |
1576 | PRINT_RUNTIME_HEADER_WIDTH + RPINT_DECIMAL_WIDTH, "RUNTIME", | |
1577 | PRINT_TASK_NAME_WIDTH, "COMMMAND"); | |
1578 | printf("\n "); | |
1579 | print_separator(ret); | |
1580 | } | |
1581 | ||
1582 | static int top_print_work(struct perf_kwork *kwork __maybe_unused, struct kwork_work *work) | |
1583 | { | |
1584 | int ret = 0; | |
1585 | ||
1586 | printf(" "); | |
1587 | ||
1588 | /* | |
1589 | * pid | |
1590 | */ | |
1591 | ret += printf(" %*ld ", PRINT_PID_WIDTH, work->id); | |
1592 | ||
1593 | /* | |
1594 | * cpu usage | |
1595 | */ | |
1596 | ret += printf(" %*.*f ", | |
1597 | PRINT_CPU_USAGE_WIDTH, PRINT_CPU_USAGE_DECIMAL_WIDTH, | |
1598 | (double)work->cpu_usage / 100); | |
1599 | ||
1600 | /* | |
1601 | * total runtime | |
1602 | */ | |
1603 | ret += printf(" %*.*f ms ", | |
1604 | PRINT_RUNTIME_WIDTH + RPINT_DECIMAL_WIDTH, RPINT_DECIMAL_WIDTH, | |
1605 | (double)work->total_runtime / NSEC_PER_MSEC); | |
1606 | ||
1607 | /* | |
1608 | * command | |
1609 | */ | |
1610 | ret += printf(" %-*s", PRINT_TASK_NAME_WIDTH, work->name); | |
1611 | ||
1612 | printf("\n"); | |
1613 | return ret; | |
1614 | } | |
1615 | ||
b83b5071 YJ |
1616 | static void work_sort(struct perf_kwork *kwork, |
1617 | struct kwork_class *class, struct rb_root_cached *root) | |
f98919ec YJ |
1618 | { |
1619 | struct rb_node *node; | |
1620 | struct kwork_work *data; | |
f98919ec YJ |
1621 | |
1622 | pr_debug("Sorting %s ...\n", class->name); | |
1623 | for (;;) { | |
1624 | node = rb_first_cached(root); | |
1625 | if (!node) | |
1626 | break; | |
1627 | ||
1628 | rb_erase_cached(node, root); | |
1629 | data = rb_entry(node, struct kwork_work, node); | |
1630 | work_insert(&kwork->sorted_work_root, | |
1631 | data, &kwork->sort_list); | |
1632 | } | |
1633 | } | |
1634 | ||
1635 | static void perf_kwork__sort(struct perf_kwork *kwork) | |
1636 | { | |
1637 | struct kwork_class *class; | |
1638 | ||
1639 | list_for_each_entry(class, &kwork->class_list, list) | |
b83b5071 | 1640 | work_sort(kwork, class, &class->work_root); |
f98919ec YJ |
1641 | } |
1642 | ||
1643 | static int perf_kwork__check_config(struct perf_kwork *kwork, | |
1644 | struct perf_session *session) | |
1645 | { | |
1646 | int ret; | |
bcc8b3e8 | 1647 | struct evsel *evsel; |
f98919ec YJ |
1648 | struct kwork_class *class; |
1649 | ||
1650 | static struct trace_kwork_handler report_ops = { | |
1651 | .entry_event = report_entry_event, | |
1652 | .exit_event = report_exit_event, | |
1653 | }; | |
ad3d9f7a YJ |
1654 | static struct trace_kwork_handler latency_ops = { |
1655 | .raise_event = latency_raise_event, | |
1656 | .entry_event = latency_entry_event, | |
1657 | }; | |
bcc8b3e8 YJ |
1658 | static struct trace_kwork_handler timehist_ops = { |
1659 | .raise_event = timehist_raise_event, | |
1660 | .entry_event = timehist_entry_event, | |
1661 | .exit_event = timehist_exit_event, | |
1662 | }; | |
55c40e50 | 1663 | static struct trace_kwork_handler top_ops = { |
2f21f5e4 YJ |
1664 | .entry_event = timehist_entry_event, |
1665 | .exit_event = top_exit_event, | |
55c40e50 YJ |
1666 | .sched_switch_event = top_sched_switch_event, |
1667 | }; | |
f98919ec YJ |
1668 | |
1669 | switch (kwork->report) { | |
1670 | case KWORK_REPORT_RUNTIME: | |
1671 | kwork->tp_handler = &report_ops; | |
1672 | break; | |
ad3d9f7a YJ |
1673 | case KWORK_REPORT_LATENCY: |
1674 | kwork->tp_handler = &latency_ops; | |
1675 | break; | |
bcc8b3e8 YJ |
1676 | case KWORK_REPORT_TIMEHIST: |
1677 | kwork->tp_handler = &timehist_ops; | |
1678 | break; | |
55c40e50 YJ |
1679 | case KWORK_REPORT_TOP: |
1680 | kwork->tp_handler = &top_ops; | |
1681 | break; | |
f98919ec YJ |
1682 | default: |
1683 | pr_debug("Invalid report type %d\n", kwork->report); | |
1684 | return -1; | |
1685 | } | |
1686 | ||
1687 | list_for_each_entry(class, &kwork->class_list, list) | |
1688 | if ((class->class_init != NULL) && | |
1689 | (class->class_init(class, session) != 0)) | |
1690 | return -1; | |
1691 | ||
1692 | if (kwork->cpu_list != NULL) { | |
1693 | ret = perf_session__cpu_bitmap(session, | |
1694 | kwork->cpu_list, | |
1695 | kwork->cpu_bitmap); | |
1696 | if (ret < 0) { | |
1697 | pr_err("Invalid cpu bitmap\n"); | |
1698 | return -1; | |
1699 | } | |
1700 | } | |
1701 | ||
1702 | if (kwork->time_str != NULL) { | |
1703 | ret = perf_time__parse_str(&kwork->ptime, kwork->time_str); | |
1704 | if (ret != 0) { | |
1705 | pr_err("Invalid time span\n"); | |
1706 | return -1; | |
1707 | } | |
1708 | } | |
1709 | ||
bcc8b3e8 YJ |
1710 | list_for_each_entry(evsel, &session->evlist->core.entries, core.node) { |
1711 | if (kwork->show_callchain && !evsel__has_callchain(evsel)) { | |
1712 | pr_debug("Samples do not have callchains\n"); | |
1713 | kwork->show_callchain = 0; | |
1714 | symbol_conf.use_callchain = 0; | |
1715 | } | |
1716 | } | |
1717 | ||
f98919ec YJ |
1718 | return 0; |
1719 | } | |
1720 | ||
1721 | static int perf_kwork__read_events(struct perf_kwork *kwork) | |
1722 | { | |
1723 | int ret = -1; | |
1724 | struct perf_session *session = NULL; | |
1725 | ||
1726 | struct perf_data data = { | |
1727 | .path = input_name, | |
1728 | .mode = PERF_DATA_MODE_READ, | |
1729 | .force = kwork->force, | |
1730 | }; | |
1731 | ||
1732 | session = perf_session__new(&data, &kwork->tool); | |
1733 | if (IS_ERR(session)) { | |
1734 | pr_debug("Error creating perf session\n"); | |
1735 | return PTR_ERR(session); | |
1736 | } | |
1737 | ||
1738 | symbol__init(&session->header.env); | |
1739 | ||
1740 | if (perf_kwork__check_config(kwork, session) != 0) | |
1741 | goto out_delete; | |
1742 | ||
1743 | if (session->tevent.pevent && | |
1744 | tep_set_function_resolver(session->tevent.pevent, | |
1745 | machine__resolve_kernel_addr, | |
1746 | &session->machines.host) < 0) { | |
1747 | pr_err("Failed to set libtraceevent function resolver\n"); | |
1748 | goto out_delete; | |
1749 | } | |
1750 | ||
bcc8b3e8 YJ |
1751 | if (kwork->report == KWORK_REPORT_TIMEHIST) |
1752 | timehist_print_header(); | |
1753 | ||
f98919ec YJ |
1754 | ret = perf_session__process_events(session); |
1755 | if (ret) { | |
1756 | pr_debug("Failed to process events, error %d\n", ret); | |
1757 | goto out_delete; | |
1758 | } | |
1759 | ||
1760 | kwork->nr_events = session->evlist->stats.nr_events[0]; | |
1761 | kwork->nr_lost_events = session->evlist->stats.total_lost; | |
1762 | kwork->nr_lost_chunks = session->evlist->stats.nr_events[PERF_RECORD_LOST]; | |
1763 | ||
1764 | out_delete: | |
1765 | perf_session__delete(session); | |
1766 | return ret; | |
1767 | } | |
1768 | ||
1769 | static void process_skipped_events(struct perf_kwork *kwork, | |
1770 | struct kwork_work *work) | |
1771 | { | |
1772 | int i; | |
1773 | unsigned long long count; | |
1774 | ||
1775 | for (i = 0; i < KWORK_TRACE_MAX; i++) { | |
1776 | count = nr_list_entry(&work->atom_list[i]); | |
1777 | kwork->nr_skipped_events[i] += count; | |
1778 | kwork->nr_skipped_events[KWORK_TRACE_MAX] += count; | |
1779 | } | |
1780 | } | |
1781 | ||
daf07d22 YJ |
1782 | struct kwork_work *perf_kwork_add_work(struct perf_kwork *kwork, |
1783 | struct kwork_class *class, | |
1784 | struct kwork_work *key) | |
1785 | { | |
1786 | struct kwork_work *work = NULL; | |
1787 | ||
1788 | work = work_new(key); | |
1789 | if (work == NULL) | |
1790 | return NULL; | |
1791 | ||
1792 | work_insert(&class->work_root, work, &kwork->cmp_id); | |
1793 | return work; | |
1794 | } | |
1795 | ||
1796 | static void sig_handler(int sig) | |
1797 | { | |
1798 | /* | |
1799 | * Simply capture termination signal so that | |
1800 | * the program can continue after pause returns | |
1801 | */ | |
1802 | pr_debug("Captuer signal %d\n", sig); | |
1803 | } | |
1804 | ||
1805 | static int perf_kwork__report_bpf(struct perf_kwork *kwork) | |
1806 | { | |
1807 | int ret; | |
1808 | ||
1809 | signal(SIGINT, sig_handler); | |
1810 | signal(SIGTERM, sig_handler); | |
1811 | ||
1812 | ret = perf_kwork__trace_prepare_bpf(kwork); | |
1813 | if (ret) | |
1814 | return -1; | |
1815 | ||
1816 | printf("Starting trace, Hit <Ctrl+C> to stop and report\n"); | |
1817 | ||
1818 | perf_kwork__trace_start(); | |
1819 | ||
1820 | /* | |
1821 | * a simple pause, wait here for stop signal | |
1822 | */ | |
1823 | pause(); | |
1824 | ||
1825 | perf_kwork__trace_finish(); | |
1826 | ||
1827 | perf_kwork__report_read_bpf(kwork); | |
1828 | ||
1829 | perf_kwork__report_cleanup_bpf(); | |
1830 | ||
1831 | return 0; | |
1832 | } | |
1833 | ||
f98919ec YJ |
1834 | static int perf_kwork__report(struct perf_kwork *kwork) |
1835 | { | |
1836 | int ret; | |
1837 | struct rb_node *next; | |
1838 | struct kwork_work *work; | |
1839 | ||
daf07d22 YJ |
1840 | if (kwork->use_bpf) |
1841 | ret = perf_kwork__report_bpf(kwork); | |
1842 | else | |
1843 | ret = perf_kwork__read_events(kwork); | |
1844 | ||
f98919ec YJ |
1845 | if (ret != 0) |
1846 | return -1; | |
1847 | ||
1848 | perf_kwork__sort(kwork); | |
1849 | ||
1850 | setup_pager(); | |
1851 | ||
1852 | ret = report_print_header(kwork); | |
1853 | next = rb_first_cached(&kwork->sorted_work_root); | |
1854 | while (next) { | |
1855 | work = rb_entry(next, struct kwork_work, node); | |
1856 | process_skipped_events(kwork, work); | |
1857 | ||
1858 | if (work->nr_atoms != 0) { | |
1859 | report_print_work(kwork, work); | |
1860 | if (kwork->summary) { | |
1861 | kwork->all_runtime += work->total_runtime; | |
1862 | kwork->all_count += work->nr_atoms; | |
1863 | } | |
1864 | } | |
1865 | next = rb_next(next); | |
1866 | } | |
1867 | print_separator(ret); | |
1868 | ||
1869 | if (kwork->summary) { | |
1870 | print_summary(kwork); | |
1871 | print_separator(ret); | |
1872 | } | |
1873 | ||
1874 | print_bad_events(kwork); | |
1875 | print_skipped_events(kwork); | |
1876 | printf("\n"); | |
1877 | ||
1878 | return 0; | |
1879 | } | |
1880 | ||
1881 | typedef int (*tracepoint_handler)(struct perf_tool *tool, | |
1882 | struct evsel *evsel, | |
1883 | struct perf_sample *sample, | |
1884 | struct machine *machine); | |
1885 | ||
1886 | static int perf_kwork__process_tracepoint_sample(struct perf_tool *tool, | |
1887 | union perf_event *event __maybe_unused, | |
1888 | struct perf_sample *sample, | |
1889 | struct evsel *evsel, | |
1890 | struct machine *machine) | |
1891 | { | |
1892 | int err = 0; | |
1893 | ||
1894 | if (evsel->handler != NULL) { | |
1895 | tracepoint_handler f = evsel->handler; | |
1896 | ||
1897 | err = f(tool, evsel, sample, machine); | |
1898 | } | |
1899 | ||
1900 | return err; | |
1901 | } | |
1902 | ||
bcc8b3e8 YJ |
1903 | static int perf_kwork__timehist(struct perf_kwork *kwork) |
1904 | { | |
1905 | /* | |
1906 | * event handlers for timehist option | |
1907 | */ | |
1908 | kwork->tool.comm = perf_event__process_comm; | |
1909 | kwork->tool.exit = perf_event__process_exit; | |
1910 | kwork->tool.fork = perf_event__process_fork; | |
1911 | kwork->tool.attr = perf_event__process_attr; | |
1912 | kwork->tool.tracing_data = perf_event__process_tracing_data; | |
1913 | kwork->tool.build_id = perf_event__process_build_id; | |
1914 | kwork->tool.ordered_events = true; | |
1915 | kwork->tool.ordering_requires_timestamps = true; | |
1916 | symbol_conf.use_callchain = kwork->show_callchain; | |
1917 | ||
1918 | if (symbol__validate_sym_arguments()) { | |
1919 | pr_err("Failed to validate sym arguments\n"); | |
1920 | return -1; | |
1921 | } | |
1922 | ||
1923 | setup_pager(); | |
1924 | ||
1925 | return perf_kwork__read_events(kwork); | |
1926 | } | |
1927 | ||
55c40e50 YJ |
1928 | static void top_calc_total_runtime(struct perf_kwork *kwork) |
1929 | { | |
1930 | struct kwork_class *class; | |
1931 | struct kwork_work *work; | |
1932 | struct rb_node *next; | |
1933 | struct kwork_top_stat *stat = &kwork->top_stat; | |
1934 | ||
1935 | class = get_kwork_class(kwork, KWORK_CLASS_SCHED); | |
1936 | if (!class) | |
1937 | return; | |
1938 | ||
1939 | next = rb_first_cached(&class->work_root); | |
1940 | while (next) { | |
1941 | work = rb_entry(next, struct kwork_work, node); | |
1942 | BUG_ON(work->cpu >= MAX_NR_CPUS); | |
1943 | stat->cpus_runtime[work->cpu].total += work->total_runtime; | |
1944 | stat->cpus_runtime[MAX_NR_CPUS].total += work->total_runtime; | |
1945 | next = rb_next(next); | |
1946 | } | |
1947 | } | |
1948 | ||
1949 | static void top_calc_idle_time(struct perf_kwork *kwork, | |
1950 | struct kwork_work *work) | |
1951 | { | |
1952 | struct kwork_top_stat *stat = &kwork->top_stat; | |
1953 | ||
1954 | if (work->id == 0) { | |
1955 | stat->cpus_runtime[work->cpu].idle += work->total_runtime; | |
1956 | stat->cpus_runtime[MAX_NR_CPUS].idle += work->total_runtime; | |
1957 | } | |
1958 | } | |
1959 | ||
2f21f5e4 YJ |
1960 | static void top_calc_irq_runtime(struct perf_kwork *kwork, |
1961 | enum kwork_class_type type, | |
1962 | struct kwork_work *work) | |
1963 | { | |
1964 | struct kwork_top_stat *stat = &kwork->top_stat; | |
1965 | ||
1966 | if (type == KWORK_CLASS_IRQ) { | |
1967 | stat->cpus_runtime[work->cpu].irq += work->total_runtime; | |
1968 | stat->cpus_runtime[MAX_NR_CPUS].irq += work->total_runtime; | |
1969 | } | |
1970 | } | |
1971 | ||
1972 | static void top_subtract_irq_runtime(struct perf_kwork *kwork, | |
1973 | struct kwork_work *work) | |
1974 | { | |
1975 | struct kwork_class *class; | |
1976 | struct kwork_work *data; | |
1977 | unsigned int i; | |
1978 | int irq_class_list[] = {KWORK_CLASS_IRQ}; | |
1979 | ||
1980 | for (i = 0; i < ARRAY_SIZE(irq_class_list); i++) { | |
1981 | class = get_kwork_class(kwork, irq_class_list[i]); | |
1982 | if (!class) | |
1983 | continue; | |
1984 | ||
1985 | data = find_work_by_id(&class->work_root, | |
1986 | work->id, work->cpu); | |
1987 | if (!data) | |
1988 | continue; | |
1989 | ||
1990 | if (work->total_runtime > data->total_runtime) { | |
1991 | work->total_runtime -= data->total_runtime; | |
1992 | top_calc_irq_runtime(kwork, irq_class_list[i], data); | |
1993 | } | |
1994 | } | |
1995 | } | |
1996 | ||
55c40e50 YJ |
1997 | static void top_calc_cpu_usage(struct perf_kwork *kwork) |
1998 | { | |
1999 | struct kwork_class *class; | |
2000 | struct kwork_work *work; | |
2001 | struct rb_node *next; | |
2002 | struct kwork_top_stat *stat = &kwork->top_stat; | |
2003 | ||
2004 | class = get_kwork_class(kwork, KWORK_CLASS_SCHED); | |
2005 | if (!class) | |
2006 | return; | |
2007 | ||
2008 | next = rb_first_cached(&class->work_root); | |
2009 | while (next) { | |
2010 | work = rb_entry(next, struct kwork_work, node); | |
2011 | ||
2012 | if (work->total_runtime == 0) | |
2013 | goto next; | |
2014 | ||
2015 | __set_bit(work->cpu, stat->all_cpus_bitmap); | |
2016 | ||
2f21f5e4 YJ |
2017 | top_subtract_irq_runtime(kwork, work); |
2018 | ||
55c40e50 YJ |
2019 | work->cpu_usage = work->total_runtime * 10000 / |
2020 | stat->cpus_runtime[work->cpu].total; | |
2021 | ||
2022 | top_calc_idle_time(kwork, work); | |
2023 | next: | |
2024 | next = rb_next(next); | |
2025 | } | |
2026 | } | |
2027 | ||
2028 | static void top_calc_load_runtime(struct perf_kwork *kwork, | |
2029 | struct kwork_work *work) | |
2030 | { | |
2031 | struct kwork_top_stat *stat = &kwork->top_stat; | |
2032 | ||
2033 | if (work->id != 0) { | |
2034 | stat->cpus_runtime[work->cpu].load += work->total_runtime; | |
2035 | stat->cpus_runtime[MAX_NR_CPUS].load += work->total_runtime; | |
2036 | } | |
2037 | } | |
2038 | ||
2039 | static void top_merge_tasks(struct perf_kwork *kwork) | |
2040 | { | |
2041 | struct kwork_work *merged_work, *data; | |
2042 | struct kwork_class *class; | |
2043 | struct rb_node *node; | |
2044 | int cpu; | |
2045 | struct rb_root_cached merged_root = RB_ROOT_CACHED; | |
2046 | ||
2047 | class = get_kwork_class(kwork, KWORK_CLASS_SCHED); | |
2048 | if (!class) | |
2049 | return; | |
2050 | ||
2051 | for (;;) { | |
2052 | node = rb_first_cached(&class->work_root); | |
2053 | if (!node) | |
2054 | break; | |
2055 | ||
2056 | rb_erase_cached(node, &class->work_root); | |
2057 | data = rb_entry(node, struct kwork_work, node); | |
2058 | ||
2059 | cpu = data->cpu; | |
2060 | merged_work = find_work_by_id(&merged_root, data->id, | |
2061 | data->id == 0 ? cpu : -1); | |
2062 | if (!merged_work) { | |
2063 | work_insert(&merged_root, data, &kwork->cmp_id); | |
2064 | } else { | |
2065 | merged_work->total_runtime += data->total_runtime; | |
2066 | merged_work->cpu_usage += data->cpu_usage; | |
2067 | } | |
2068 | ||
2069 | top_calc_load_runtime(kwork, data); | |
2070 | } | |
2071 | ||
2072 | work_sort(kwork, class, &merged_root); | |
2073 | } | |
2074 | ||
2075 | static void perf_kwork__top_report(struct perf_kwork *kwork) | |
2076 | { | |
2077 | struct kwork_work *work; | |
2078 | struct rb_node *next; | |
2079 | ||
2080 | printf("\n"); | |
2081 | ||
2082 | top_print_cpu_usage(kwork); | |
2083 | top_print_header(kwork); | |
2084 | next = rb_first_cached(&kwork->sorted_work_root); | |
2085 | while (next) { | |
2086 | work = rb_entry(next, struct kwork_work, node); | |
2087 | process_skipped_events(kwork, work); | |
2088 | ||
2089 | if (work->total_runtime == 0) | |
2090 | goto next; | |
2091 | ||
2092 | top_print_work(kwork, work); | |
2093 | ||
2094 | next: | |
2095 | next = rb_next(next); | |
2096 | } | |
2097 | ||
2098 | printf("\n"); | |
2099 | } | |
2100 | ||
2101 | static int perf_kwork__top(struct perf_kwork *kwork) | |
2102 | { | |
2103 | struct __top_cpus_runtime *cpus_runtime; | |
2104 | int ret = 0; | |
2105 | ||
2106 | cpus_runtime = zalloc(sizeof(struct __top_cpus_runtime) * (MAX_NR_CPUS + 1)); | |
2107 | if (!cpus_runtime) | |
2108 | return -1; | |
2109 | ||
2110 | kwork->top_stat.cpus_runtime = cpus_runtime; | |
2111 | bitmap_zero(kwork->top_stat.all_cpus_bitmap, MAX_NR_CPUS); | |
2112 | ||
2113 | ret = perf_kwork__read_events(kwork); | |
2114 | if (ret) | |
2115 | goto out; | |
2116 | ||
2117 | top_calc_total_runtime(kwork); | |
2118 | top_calc_cpu_usage(kwork); | |
2119 | top_merge_tasks(kwork); | |
2120 | ||
2121 | setup_pager(); | |
2122 | ||
2123 | perf_kwork__top_report(kwork); | |
2124 | ||
2125 | out: | |
2126 | free(kwork->top_stat.cpus_runtime); | |
2127 | return ret; | |
2128 | } | |
2129 | ||
0f70d8e9 YJ |
2130 | static void setup_event_list(struct perf_kwork *kwork, |
2131 | const struct option *options, | |
2132 | const char * const usage_msg[]) | |
2133 | { | |
2134 | int i; | |
2135 | struct kwork_class *class; | |
2136 | char *tmp, *tok, *str; | |
2137 | ||
26b7254f YJ |
2138 | /* |
2139 | * set default events list if not specified | |
2140 | */ | |
0f70d8e9 | 2141 | if (kwork->event_list_str == NULL) |
26b7254f | 2142 | kwork->event_list_str = "irq, softirq, workqueue"; |
0f70d8e9 YJ |
2143 | |
2144 | str = strdup(kwork->event_list_str); | |
2145 | for (tok = strtok_r(str, ", ", &tmp); | |
2146 | tok; tok = strtok_r(NULL, ", ", &tmp)) { | |
2147 | for (i = 0; i < KWORK_CLASS_MAX; i++) { | |
2148 | class = kwork_class_supported_list[i]; | |
2149 | if (strcmp(tok, class->name) == 0) { | |
2150 | list_add_tail(&class->list, &kwork->class_list); | |
2151 | break; | |
2152 | } | |
2153 | } | |
2154 | if (i == KWORK_CLASS_MAX) { | |
2155 | usage_with_options_msg(usage_msg, options, | |
2156 | "Unknown --event key: `%s'", tok); | |
2157 | } | |
2158 | } | |
2159 | free(str); | |
2160 | ||
0f70d8e9 YJ |
2161 | pr_debug("Config event list:"); |
2162 | list_for_each_entry(class, &kwork->class_list, list) | |
2163 | pr_debug(" %s", class->name); | |
2164 | pr_debug("\n"); | |
2165 | } | |
2166 | ||
2167 | static int perf_kwork__record(struct perf_kwork *kwork, | |
2168 | int argc, const char **argv) | |
2169 | { | |
2170 | const char **rec_argv; | |
2171 | unsigned int rec_argc, i, j; | |
2172 | struct kwork_class *class; | |
2173 | ||
2174 | const char *const record_args[] = { | |
2175 | "record", | |
2176 | "-a", | |
2177 | "-R", | |
2178 | "-m", "1024", | |
2179 | "-c", "1", | |
2180 | }; | |
2181 | ||
2182 | rec_argc = ARRAY_SIZE(record_args) + argc - 1; | |
2183 | ||
2184 | list_for_each_entry(class, &kwork->class_list, list) | |
2185 | rec_argc += 2 * class->nr_tracepoints; | |
2186 | ||
2187 | rec_argv = calloc(rec_argc + 1, sizeof(char *)); | |
2188 | if (rec_argv == NULL) | |
2189 | return -ENOMEM; | |
2190 | ||
2191 | for (i = 0; i < ARRAY_SIZE(record_args); i++) | |
2192 | rec_argv[i] = strdup(record_args[i]); | |
2193 | ||
2194 | list_for_each_entry(class, &kwork->class_list, list) { | |
2195 | for (j = 0; j < class->nr_tracepoints; j++) { | |
2196 | rec_argv[i++] = strdup("-e"); | |
2197 | rec_argv[i++] = strdup(class->tp_handlers[j].name); | |
2198 | } | |
2199 | } | |
2200 | ||
2201 | for (j = 1; j < (unsigned int)argc; j++, i++) | |
2202 | rec_argv[i] = argv[j]; | |
2203 | ||
2204 | BUG_ON(i != rec_argc); | |
2205 | ||
2206 | pr_debug("record comm: "); | |
2207 | for (j = 0; j < rec_argc; j++) | |
2208 | pr_debug("%s ", rec_argv[j]); | |
2209 | pr_debug("\n"); | |
2210 | ||
2211 | return cmd_record(i, rec_argv); | |
2212 | } | |
2213 | ||
2214 | int cmd_kwork(int argc, const char **argv) | |
2215 | { | |
2216 | static struct perf_kwork kwork = { | |
2217 | .class_list = LIST_HEAD_INIT(kwork.class_list), | |
f98919ec | 2218 | .tool = { |
0c526579 YJ |
2219 | .mmap = perf_event__process_mmap, |
2220 | .mmap2 = perf_event__process_mmap2, | |
2221 | .sample = perf_kwork__process_tracepoint_sample, | |
2222 | .ordered_events = true, | |
f98919ec YJ |
2223 | }, |
2224 | .atom_page_list = LIST_HEAD_INIT(kwork.atom_page_list), | |
2225 | .sort_list = LIST_HEAD_INIT(kwork.sort_list), | |
2226 | .cmp_id = LIST_HEAD_INIT(kwork.cmp_id), | |
2227 | .sorted_work_root = RB_ROOT_CACHED, | |
2228 | .tp_handler = NULL, | |
2229 | .profile_name = NULL, | |
2230 | .cpu_list = NULL, | |
2231 | .time_str = NULL, | |
0f70d8e9 YJ |
2232 | .force = false, |
2233 | .event_list_str = NULL, | |
f98919ec YJ |
2234 | .summary = false, |
2235 | .sort_order = NULL, | |
bcc8b3e8 YJ |
2236 | .show_callchain = false, |
2237 | .max_stack = 5, | |
f98919ec YJ |
2238 | .timestart = 0, |
2239 | .timeend = 0, | |
2240 | .nr_events = 0, | |
2241 | .nr_lost_chunks = 0, | |
2242 | .nr_lost_events = 0, | |
2243 | .all_runtime = 0, | |
2244 | .all_count = 0, | |
2245 | .nr_skipped_events = { 0 }, | |
0f70d8e9 | 2246 | }; |
f98919ec | 2247 | static const char default_report_sort_order[] = "runtime, max, count"; |
ad3d9f7a | 2248 | static const char default_latency_sort_order[] = "avg, max, count"; |
55c40e50 | 2249 | static const char default_top_sort_order[] = "rate, runtime"; |
0f70d8e9 YJ |
2250 | const struct option kwork_options[] = { |
2251 | OPT_INCR('v', "verbose", &verbose, | |
2252 | "be more verbose (show symbol address, etc)"), | |
2253 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, | |
2254 | "dump raw trace in ASCII"), | |
2255 | OPT_STRING('k', "kwork", &kwork.event_list_str, "kwork", | |
38d8d013 | 2256 | "list of kwork to profile (irq, softirq, workqueue, sched, etc)"), |
0f70d8e9 YJ |
2257 | OPT_BOOLEAN('f', "force", &kwork.force, "don't complain, do it"), |
2258 | OPT_END() | |
2259 | }; | |
f98919ec YJ |
2260 | const struct option report_options[] = { |
2261 | OPT_STRING('s', "sort", &kwork.sort_order, "key[,key2...]", | |
2262 | "sort by key(s): runtime, max, count"), | |
2263 | OPT_STRING('C', "cpu", &kwork.cpu_list, "cpu", | |
2264 | "list of cpus to profile"), | |
2265 | OPT_STRING('n', "name", &kwork.profile_name, "name", | |
2266 | "event name to profile"), | |
2267 | OPT_STRING(0, "time", &kwork.time_str, "str", | |
2268 | "Time span for analysis (start,stop)"), | |
2269 | OPT_STRING('i', "input", &input_name, "file", | |
2270 | "input file name"), | |
2271 | OPT_BOOLEAN('S', "with-summary", &kwork.summary, | |
2272 | "Show summary with statistics"), | |
daf07d22 YJ |
2273 | #ifdef HAVE_BPF_SKEL |
2274 | OPT_BOOLEAN('b', "use-bpf", &kwork.use_bpf, | |
2275 | "Use BPF to measure kwork runtime"), | |
2276 | #endif | |
f98919ec YJ |
2277 | OPT_PARENT(kwork_options) |
2278 | }; | |
ad3d9f7a YJ |
2279 | const struct option latency_options[] = { |
2280 | OPT_STRING('s', "sort", &kwork.sort_order, "key[,key2...]", | |
2281 | "sort by key(s): avg, max, count"), | |
2282 | OPT_STRING('C', "cpu", &kwork.cpu_list, "cpu", | |
2283 | "list of cpus to profile"), | |
2284 | OPT_STRING('n', "name", &kwork.profile_name, "name", | |
2285 | "event name to profile"), | |
2286 | OPT_STRING(0, "time", &kwork.time_str, "str", | |
2287 | "Time span for analysis (start,stop)"), | |
2288 | OPT_STRING('i', "input", &input_name, "file", | |
2289 | "input file name"), | |
daf07d22 YJ |
2290 | #ifdef HAVE_BPF_SKEL |
2291 | OPT_BOOLEAN('b', "use-bpf", &kwork.use_bpf, | |
2292 | "Use BPF to measure kwork latency"), | |
2293 | #endif | |
ad3d9f7a YJ |
2294 | OPT_PARENT(kwork_options) |
2295 | }; | |
bcc8b3e8 YJ |
2296 | const struct option timehist_options[] = { |
2297 | OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, | |
2298 | "file", "vmlinux pathname"), | |
2299 | OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name, | |
2300 | "file", "kallsyms pathname"), | |
2301 | OPT_BOOLEAN('g', "call-graph", &kwork.show_callchain, | |
2302 | "Display call chains if present"), | |
2303 | OPT_UINTEGER(0, "max-stack", &kwork.max_stack, | |
2304 | "Maximum number of functions to display backtrace."), | |
2305 | OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory", | |
2306 | "Look for files with symbols relative to this directory"), | |
2307 | OPT_STRING(0, "time", &kwork.time_str, "str", | |
2308 | "Time span for analysis (start,stop)"), | |
2309 | OPT_STRING('C', "cpu", &kwork.cpu_list, "cpu", | |
2310 | "list of cpus to profile"), | |
2311 | OPT_STRING('n', "name", &kwork.profile_name, "name", | |
2312 | "event name to profile"), | |
2313 | OPT_STRING('i', "input", &input_name, "file", | |
2314 | "input file name"), | |
2315 | OPT_PARENT(kwork_options) | |
2316 | }; | |
55c40e50 YJ |
2317 | const struct option top_options[] = { |
2318 | OPT_PARENT(kwork_options) | |
2319 | }; | |
0f70d8e9 YJ |
2320 | const char *kwork_usage[] = { |
2321 | NULL, | |
2322 | NULL | |
2323 | }; | |
f98919ec YJ |
2324 | const char * const report_usage[] = { |
2325 | "perf kwork report [<options>]", | |
2326 | NULL | |
2327 | }; | |
ad3d9f7a YJ |
2328 | const char * const latency_usage[] = { |
2329 | "perf kwork latency [<options>]", | |
2330 | NULL | |
2331 | }; | |
bcc8b3e8 YJ |
2332 | const char * const timehist_usage[] = { |
2333 | "perf kwork timehist [<options>]", | |
2334 | NULL | |
2335 | }; | |
55c40e50 YJ |
2336 | const char * const top_usage[] = { |
2337 | "perf kwork top [<options>]", | |
2338 | NULL | |
2339 | }; | |
0f70d8e9 | 2340 | const char *const kwork_subcommands[] = { |
55c40e50 | 2341 | "record", "report", "latency", "timehist", "top", NULL |
0f70d8e9 YJ |
2342 | }; |
2343 | ||
2344 | argc = parse_options_subcommand(argc, argv, kwork_options, | |
2345 | kwork_subcommands, kwork_usage, | |
2346 | PARSE_OPT_STOP_AT_NON_OPTION); | |
2347 | if (!argc) | |
2348 | usage_with_options(kwork_usage, kwork_options); | |
2349 | ||
f98919ec | 2350 | sort_dimension__add(&kwork, "id", &kwork.cmp_id); |
0f70d8e9 | 2351 | |
26b7254f YJ |
2352 | if (strlen(argv[0]) > 2 && strstarts("record", argv[0])) { |
2353 | setup_event_list(&kwork, kwork_options, kwork_usage); | |
0f70d8e9 | 2354 | return perf_kwork__record(&kwork, argc, argv); |
26b7254f | 2355 | } else if (strlen(argv[0]) > 2 && strstarts("report", argv[0])) { |
f98919ec YJ |
2356 | kwork.sort_order = default_report_sort_order; |
2357 | if (argc > 1) { | |
2358 | argc = parse_options(argc, argv, report_options, report_usage, 0); | |
2359 | if (argc) | |
2360 | usage_with_options(report_usage, report_options); | |
2361 | } | |
2362 | kwork.report = KWORK_REPORT_RUNTIME; | |
2363 | setup_sorting(&kwork, report_options, report_usage); | |
26b7254f | 2364 | setup_event_list(&kwork, kwork_options, kwork_usage); |
f98919ec | 2365 | return perf_kwork__report(&kwork); |
ad3d9f7a YJ |
2366 | } else if (strlen(argv[0]) > 2 && strstarts("latency", argv[0])) { |
2367 | kwork.sort_order = default_latency_sort_order; | |
2368 | if (argc > 1) { | |
2369 | argc = parse_options(argc, argv, latency_options, latency_usage, 0); | |
2370 | if (argc) | |
2371 | usage_with_options(latency_usage, latency_options); | |
2372 | } | |
2373 | kwork.report = KWORK_REPORT_LATENCY; | |
2374 | setup_sorting(&kwork, latency_options, latency_usage); | |
26b7254f | 2375 | setup_event_list(&kwork, kwork_options, kwork_usage); |
ad3d9f7a | 2376 | return perf_kwork__report(&kwork); |
bcc8b3e8 YJ |
2377 | } else if (strlen(argv[0]) > 2 && strstarts("timehist", argv[0])) { |
2378 | if (argc > 1) { | |
2379 | argc = parse_options(argc, argv, timehist_options, timehist_usage, 0); | |
2380 | if (argc) | |
2381 | usage_with_options(timehist_usage, timehist_options); | |
2382 | } | |
2383 | kwork.report = KWORK_REPORT_TIMEHIST; | |
26b7254f | 2384 | setup_event_list(&kwork, kwork_options, kwork_usage); |
bcc8b3e8 | 2385 | return perf_kwork__timehist(&kwork); |
55c40e50 YJ |
2386 | } else if (strlen(argv[0]) > 2 && strstarts("top", argv[0])) { |
2387 | kwork.sort_order = default_top_sort_order; | |
2388 | if (argc > 1) { | |
2389 | argc = parse_options(argc, argv, top_options, top_usage, 0); | |
2390 | if (argc) | |
2391 | usage_with_options(top_usage, top_options); | |
2392 | } | |
2393 | kwork.report = KWORK_REPORT_TOP; | |
2394 | if (!kwork.event_list_str) | |
2f21f5e4 | 2395 | kwork.event_list_str = "sched, irq"; |
55c40e50 YJ |
2396 | setup_event_list(&kwork, kwork_options, kwork_usage); |
2397 | setup_sorting(&kwork, top_options, top_usage); | |
2398 | return perf_kwork__top(&kwork); | |
f98919ec | 2399 | } else |
0f70d8e9 YJ |
2400 | usage_with_options(kwork_usage, kwork_options); |
2401 | ||
2402 | return 0; | |
2403 | } |