builtin-top.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * builtin-top.c
  4. *
  5. * Builtin top command: Display a continuously updated profile of
  6. * any workload, CPU or specific PID.
  7. *
  8. * Copyright (C) 2008, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
  9. * 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
  10. *
  11. * Improvements and fixes by:
  12. *
  13. * Arjan van de Ven <arjan@linux.intel.com>
  14. * Yanmin Zhang <yanmin.zhang@intel.com>
  15. * Wu Fengguang <fengguang.wu@intel.com>
  16. * Mike Galbraith <efault@gmx.de>
  17. * Paul Mackerras <paulus@samba.org>
  18. */
  19. #include "builtin.h"
  20. #include "perf.h"
  21. #include "util/annotate.h"
  22. #include "util/bpf-event.h"
  23. #include "util/config.h"
  24. #include "util/color.h"
  25. #include "util/dso.h"
  26. #include "util/evlist.h"
  27. #include "util/evsel.h"
  28. #include "util/evsel_config.h"
  29. #include "util/event.h"
  30. #include "util/machine.h"
  31. #include "util/map.h"
  32. #include "util/mmap.h"
  33. #include "util/session.h"
  34. #include "util/thread.h"
  35. #include "util/symbol.h"
  36. #include "util/synthetic-events.h"
  37. #include "util/top.h"
  38. #include "util/util.h"
  39. #include <linux/rbtree.h>
  40. #include <subcmd/parse-options.h>
  41. #include "util/parse-events.h"
  42. #include "util/callchain.h"
  43. #include "util/cpumap.h"
  44. #include "util/sort.h"
  45. #include "util/string2.h"
  46. #include "util/term.h"
  47. #include "util/intlist.h"
  48. #include "util/parse-branch-options.h"
  49. #include "arch/common.h"
  50. #include "ui/ui.h"
  51. #include "util/debug.h"
  52. #include "util/ordered-events.h"
  53. #include "util/pfm.h"
  54. #include <assert.h>
  55. #include <elf.h>
  56. #include <fcntl.h>
  57. #include <stdio.h>
  58. #include <termios.h>
  59. #include <unistd.h>
  60. #include <inttypes.h>
  61. #include <errno.h>
  62. #include <time.h>
  63. #include <sched.h>
  64. #include <signal.h>
  65. #include <sys/syscall.h>
  66. #include <sys/ioctl.h>
  67. #include <poll.h>
  68. #include <sys/prctl.h>
  69. #include <sys/wait.h>
  70. #include <sys/uio.h>
  71. #include <sys/utsname.h>
  72. #include <sys/mman.h>
  73. #include <linux/stringify.h>
  74. #include <linux/time64.h>
  75. #include <linux/types.h>
  76. #include <linux/err.h>
  77. #include <linux/ctype.h>
  78. #include <perf/mmap.h>
  79. static volatile int done;
  80. static volatile int resize;
  81. #define HEADER_LINE_NR 5
  82. static void perf_top__update_print_entries(struct perf_top *top)
  83. {
  84. top->print_entries = top->winsize.ws_row - HEADER_LINE_NR;
  85. }
  86. static void winch_sig(int sig __maybe_unused)
  87. {
  88. resize = 1;
  89. }
  90. static void perf_top__resize(struct perf_top *top)
  91. {
  92. get_term_dimensions(&top->winsize);
  93. perf_top__update_print_entries(top);
  94. }
  95. static int perf_top__parse_source(struct perf_top *top, struct hist_entry *he)
  96. {
  97. struct evsel *evsel;
  98. struct symbol *sym;
  99. struct annotation *notes;
  100. struct map *map;
  101. int err = -1;
  102. if (!he || !he->ms.sym)
  103. return -1;
  104. evsel = hists_to_evsel(he->hists);
  105. sym = he->ms.sym;
  106. map = he->ms.map;
  107. /*
  108. * We can't annotate with just /proc/kallsyms
  109. */
  110. if (map->dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
  111. !dso__is_kcore(map->dso)) {
  112. pr_err("Can't annotate %s: No vmlinux file was found in the "
  113. "path\n", sym->name);
  114. sleep(1);
  115. return -1;
  116. }
  117. notes = symbol__annotation(sym);
  118. pthread_mutex_lock(&notes->lock);
  119. if (!symbol__hists(sym, top->evlist->core.nr_entries)) {
  120. pthread_mutex_unlock(&notes->lock);
  121. pr_err("Not enough memory for annotating '%s' symbol!\n",
  122. sym->name);
  123. sleep(1);
  124. return err;
  125. }
  126. err = symbol__annotate(&he->ms, evsel, &top->annotation_opts, NULL);
  127. if (err == 0) {
  128. top->sym_filter_entry = he;
  129. } else {
  130. char msg[BUFSIZ];
  131. symbol__strerror_disassemble(&he->ms, err, msg, sizeof(msg));
  132. pr_err("Couldn't annotate %s: %s\n", sym->name, msg);
  133. }
  134. pthread_mutex_unlock(&notes->lock);
  135. return err;
  136. }
  137. static void __zero_source_counters(struct hist_entry *he)
  138. {
  139. struct symbol *sym = he->ms.sym;
  140. symbol__annotate_zero_histograms(sym);
  141. }
  142. static void ui__warn_map_erange(struct map *map, struct symbol *sym, u64 ip)
  143. {
  144. struct utsname uts;
  145. int err = uname(&uts);
  146. ui__warning("Out of bounds address found:\n\n"
  147. "Addr: %" PRIx64 "\n"
  148. "DSO: %s %c\n"
  149. "Map: %" PRIx64 "-%" PRIx64 "\n"
  150. "Symbol: %" PRIx64 "-%" PRIx64 " %c %s\n"
  151. "Arch: %s\n"
  152. "Kernel: %s\n"
  153. "Tools: %s\n\n"
  154. "Not all samples will be on the annotation output.\n\n"
  155. "Please report to linux-kernel@vger.kernel.org\n",
  156. ip, map->dso->long_name, dso__symtab_origin(map->dso),
  157. map->start, map->end, sym->start, sym->end,
  158. sym->binding == STB_GLOBAL ? 'g' :
  159. sym->binding == STB_LOCAL ? 'l' : 'w', sym->name,
  160. err ? "[unknown]" : uts.machine,
  161. err ? "[unknown]" : uts.release, perf_version_string);
  162. if (use_browser <= 0)
  163. sleep(5);
  164. map->erange_warned = true;
  165. }
  166. static void perf_top__record_precise_ip(struct perf_top *top,
  167. struct hist_entry *he,
  168. struct perf_sample *sample,
  169. struct evsel *evsel, u64 ip)
  170. {
  171. struct annotation *notes;
  172. struct symbol *sym = he->ms.sym;
  173. int err = 0;
  174. if (sym == NULL || (use_browser == 0 &&
  175. (top->sym_filter_entry == NULL ||
  176. top->sym_filter_entry->ms.sym != sym)))
  177. return;
  178. notes = symbol__annotation(sym);
  179. if (pthread_mutex_trylock(&notes->lock))
  180. return;
  181. err = hist_entry__inc_addr_samples(he, sample, evsel, ip);
  182. pthread_mutex_unlock(&notes->lock);
  183. if (unlikely(err)) {
  184. /*
  185. * This function is now called with he->hists->lock held.
  186. * Release it before going to sleep.
  187. */
  188. pthread_mutex_unlock(&he->hists->lock);
  189. if (err == -ERANGE && !he->ms.map->erange_warned)
  190. ui__warn_map_erange(he->ms.map, sym, ip);
  191. else if (err == -ENOMEM) {
  192. pr_err("Not enough memory for annotating '%s' symbol!\n",
  193. sym->name);
  194. sleep(1);
  195. }
  196. pthread_mutex_lock(&he->hists->lock);
  197. }
  198. }
  199. static void perf_top__show_details(struct perf_top *top)
  200. {
  201. struct hist_entry *he = top->sym_filter_entry;
  202. struct evsel *evsel;
  203. struct annotation *notes;
  204. struct symbol *symbol;
  205. int more;
  206. if (!he)
  207. return;
  208. evsel = hists_to_evsel(he->hists);
  209. symbol = he->ms.sym;
  210. notes = symbol__annotation(symbol);
  211. pthread_mutex_lock(&notes->lock);
  212. symbol__calc_percent(symbol, evsel);
  213. if (notes->src == NULL)
  214. goto out_unlock;
  215. printf("Showing %s for %s\n", evsel__name(top->sym_evsel), symbol->name);
  216. printf(" Events Pcnt (>=%d%%)\n", top->annotation_opts.min_pcnt);
  217. more = symbol__annotate_printf(&he->ms, top->sym_evsel, &top->annotation_opts);
  218. if (top->evlist->enabled) {
  219. if (top->zero)
  220. symbol__annotate_zero_histogram(symbol, top->sym_evsel->idx);
  221. else
  222. symbol__annotate_decay_histogram(symbol, top->sym_evsel->idx);
  223. }
  224. if (more != 0)
  225. printf("%d lines not displayed, maybe increase display entries [e]\n", more);
  226. out_unlock:
  227. pthread_mutex_unlock(&notes->lock);
  228. }
  229. static void perf_top__resort_hists(struct perf_top *t)
  230. {
  231. struct evlist *evlist = t->evlist;
  232. struct evsel *pos;
  233. evlist__for_each_entry(evlist, pos) {
  234. struct hists *hists = evsel__hists(pos);
  235. /*
  236. * unlink existing entries so that they can be linked
  237. * in a correct order in hists__match() below.
  238. */
  239. hists__unlink(hists);
  240. if (evlist->enabled) {
  241. if (t->zero) {
  242. hists__delete_entries(hists);
  243. } else {
  244. hists__decay_entries(hists, t->hide_user_symbols,
  245. t->hide_kernel_symbols);
  246. }
  247. }
  248. hists__collapse_resort(hists, NULL);
  249. /* Non-group events are considered as leader */
  250. if (symbol_conf.event_group && !evsel__is_group_leader(pos)) {
  251. struct hists *leader_hists = evsel__hists(pos->leader);
  252. hists__match(leader_hists, hists);
  253. hists__link(leader_hists, hists);
  254. }
  255. }
  256. evlist__for_each_entry(evlist, pos) {
  257. evsel__output_resort(pos, NULL);
  258. }
  259. }
  260. static void perf_top__print_sym_table(struct perf_top *top)
  261. {
  262. char bf[160];
  263. int printed = 0;
  264. const int win_width = top->winsize.ws_col - 1;
  265. struct evsel *evsel = top->sym_evsel;
  266. struct hists *hists = evsel__hists(evsel);
  267. puts(CONSOLE_CLEAR);
  268. perf_top__header_snprintf(top, bf, sizeof(bf));
  269. printf("%s\n", bf);
  270. printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
  271. if (!top->record_opts.overwrite &&
  272. (hists->stats.nr_lost_warned !=
  273. hists->stats.nr_events[PERF_RECORD_LOST])) {
  274. hists->stats.nr_lost_warned =
  275. hists->stats.nr_events[PERF_RECORD_LOST];
  276. color_fprintf(stdout, PERF_COLOR_RED,
  277. "WARNING: LOST %d chunks, Check IO/CPU overload",
  278. hists->stats.nr_lost_warned);
  279. ++printed;
  280. }
  281. if (top->sym_filter_entry) {
  282. perf_top__show_details(top);
  283. return;
  284. }
  285. perf_top__resort_hists(top);
  286. hists__output_recalc_col_len(hists, top->print_entries - printed);
  287. putchar('\n');
  288. hists__fprintf(hists, false, top->print_entries - printed, win_width,
  289. top->min_percent, stdout, !symbol_conf.use_callchain);
  290. }
  291. static void prompt_integer(int *target, const char *msg)
  292. {
  293. char *buf = malloc(0), *p;
  294. size_t dummy = 0;
  295. int tmp;
  296. fprintf(stdout, "\n%s: ", msg);
  297. if (getline(&buf, &dummy, stdin) < 0)
  298. return;
  299. p = strchr(buf, '\n');
  300. if (p)
  301. *p = 0;
  302. p = buf;
  303. while(*p) {
  304. if (!isdigit(*p))
  305. goto out_free;
  306. p++;
  307. }
  308. tmp = strtoul(buf, NULL, 10);
  309. *target = tmp;
  310. out_free:
  311. free(buf);
  312. }
  313. static void prompt_percent(int *target, const char *msg)
  314. {
  315. int tmp = 0;
  316. prompt_integer(&tmp, msg);
  317. if (tmp >= 0 && tmp <= 100)
  318. *target = tmp;
  319. }
  320. static void perf_top__prompt_symbol(struct perf_top *top, const char *msg)
  321. {
  322. char *buf = malloc(0), *p;
  323. struct hist_entry *syme = top->sym_filter_entry, *n, *found = NULL;
  324. struct hists *hists = evsel__hists(top->sym_evsel);
  325. struct rb_node *next;
  326. size_t dummy = 0;
  327. /* zero counters of active symbol */
  328. if (syme) {
  329. __zero_source_counters(syme);
  330. top->sym_filter_entry = NULL;
  331. }
  332. fprintf(stdout, "\n%s: ", msg);
  333. if (getline(&buf, &dummy, stdin) < 0)
  334. goto out_free;
  335. p = strchr(buf, '\n');
  336. if (p)
  337. *p = 0;
  338. next = rb_first_cached(&hists->entries);
  339. while (next) {
  340. n = rb_entry(next, struct hist_entry, rb_node);
  341. if (n->ms.sym && !strcmp(buf, n->ms.sym->name)) {
  342. found = n;
  343. break;
  344. }
  345. next = rb_next(&n->rb_node);
  346. }
  347. if (!found) {
  348. fprintf(stderr, "Sorry, %s is not active.\n", buf);
  349. sleep(1);
  350. } else
  351. perf_top__parse_source(top, found);
  352. out_free:
  353. free(buf);
  354. }
  355. static void perf_top__print_mapped_keys(struct perf_top *top)
  356. {
  357. char *name = NULL;
  358. if (top->sym_filter_entry) {
  359. struct symbol *sym = top->sym_filter_entry->ms.sym;
  360. name = sym->name;
  361. }
  362. fprintf(stdout, "\nMapped keys:\n");
  363. fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", top->delay_secs);
  364. fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", top->print_entries);
  365. if (top->evlist->core.nr_entries > 1)
  366. fprintf(stdout, "\t[E] active event counter. \t(%s)\n", evsel__name(top->sym_evsel));
  367. fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", top->count_filter);
  368. fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", top->annotation_opts.min_pcnt);
  369. fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL");
  370. fprintf(stdout, "\t[S] stop annotation.\n");
  371. fprintf(stdout,
  372. "\t[K] hide kernel symbols. \t(%s)\n",
  373. top->hide_kernel_symbols ? "yes" : "no");
  374. fprintf(stdout,
  375. "\t[U] hide user symbols. \t(%s)\n",
  376. top->hide_user_symbols ? "yes" : "no");
  377. fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", top->zero ? 1 : 0);
  378. fprintf(stdout, "\t[qQ] quit.\n");
  379. }
  380. static int perf_top__key_mapped(struct perf_top *top, int c)
  381. {
  382. switch (c) {
  383. case 'd':
  384. case 'e':
  385. case 'f':
  386. case 'z':
  387. case 'q':
  388. case 'Q':
  389. case 'K':
  390. case 'U':
  391. case 'F':
  392. case 's':
  393. case 'S':
  394. return 1;
  395. case 'E':
  396. return top->evlist->core.nr_entries > 1 ? 1 : 0;
  397. default:
  398. break;
  399. }
  400. return 0;
  401. }
  402. static bool perf_top__handle_keypress(struct perf_top *top, int c)
  403. {
  404. bool ret = true;
  405. if (!perf_top__key_mapped(top, c)) {
  406. struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
  407. struct termios save;
  408. perf_top__print_mapped_keys(top);
  409. fprintf(stdout, "\nEnter selection, or unmapped key to continue: ");
  410. fflush(stdout);
  411. set_term_quiet_input(&save);
  412. poll(&stdin_poll, 1, -1);
  413. c = getc(stdin);
  414. tcsetattr(0, TCSAFLUSH, &save);
  415. if (!perf_top__key_mapped(top, c))
  416. return ret;
  417. }
  418. switch (c) {
  419. case 'd':
  420. prompt_integer(&top->delay_secs, "Enter display delay");
  421. if (top->delay_secs < 1)
  422. top->delay_secs = 1;
  423. break;
  424. case 'e':
  425. prompt_integer(&top->print_entries, "Enter display entries (lines)");
  426. if (top->print_entries == 0) {
  427. perf_top__resize(top);
  428. signal(SIGWINCH, winch_sig);
  429. } else {
  430. signal(SIGWINCH, SIG_DFL);
  431. }
  432. break;
  433. case 'E':
  434. if (top->evlist->core.nr_entries > 1) {
  435. /* Select 0 as the default event: */
  436. int counter = 0;
  437. fprintf(stderr, "\nAvailable events:");
  438. evlist__for_each_entry(top->evlist, top->sym_evsel)
  439. fprintf(stderr, "\n\t%d %s", top->sym_evsel->idx, evsel__name(top->sym_evsel));
  440. prompt_integer(&counter, "Enter details event counter");
  441. if (counter >= top->evlist->core.nr_entries) {
  442. top->sym_evsel = evlist__first(top->evlist);
  443. fprintf(stderr, "Sorry, no such event, using %s.\n", evsel__name(top->sym_evsel));
  444. sleep(1);
  445. break;
  446. }
  447. evlist__for_each_entry(top->evlist, top->sym_evsel)
  448. if (top->sym_evsel->idx == counter)
  449. break;
  450. } else
  451. top->sym_evsel = evlist__first(top->evlist);
  452. break;
  453. case 'f':
  454. prompt_integer(&top->count_filter, "Enter display event count filter");
  455. break;
  456. case 'F':
  457. prompt_percent(&top->annotation_opts.min_pcnt,
  458. "Enter details display event filter (percent)");
  459. break;
  460. case 'K':
  461. top->hide_kernel_symbols = !top->hide_kernel_symbols;
  462. break;
  463. case 'q':
  464. case 'Q':
  465. printf("exiting.\n");
  466. if (top->dump_symtab)
  467. perf_session__fprintf_dsos(top->session, stderr);
  468. ret = false;
  469. break;
  470. case 's':
  471. perf_top__prompt_symbol(top, "Enter details symbol");
  472. break;
  473. case 'S':
  474. if (!top->sym_filter_entry)
  475. break;
  476. else {
  477. struct hist_entry *syme = top->sym_filter_entry;
  478. top->sym_filter_entry = NULL;
  479. __zero_source_counters(syme);
  480. }
  481. break;
  482. case 'U':
  483. top->hide_user_symbols = !top->hide_user_symbols;
  484. break;
  485. case 'z':
  486. top->zero = !top->zero;
  487. break;
  488. default:
  489. break;
  490. }
  491. return ret;
  492. }
  493. static void perf_top__sort_new_samples(void *arg)
  494. {
  495. struct perf_top *t = arg;
  496. if (t->evlist->selected != NULL)
  497. t->sym_evsel = t->evlist->selected;
  498. perf_top__resort_hists(t);
  499. if (t->lost || t->drop)
  500. pr_warning("Too slow to read ring buffer (change period (-c/-F) or limit CPUs (-C)\n");
  501. }
  502. static void stop_top(void)
  503. {
  504. session_done = 1;
  505. done = 1;
  506. }
  507. static void *display_thread_tui(void *arg)
  508. {
  509. struct evsel *pos;
  510. struct perf_top *top = arg;
  511. const char *help = "For a higher level overview, try: perf top --sort comm,dso";
  512. struct hist_browser_timer hbt = {
  513. .timer = perf_top__sort_new_samples,
  514. .arg = top,
  515. .refresh = top->delay_secs,
  516. };
  517. int ret;
  518. /* In order to read symbols from other namespaces perf to needs to call
  519. * setns(2). This isn't permitted if the struct_fs has multiple users.
  520. * unshare(2) the fs so that we may continue to setns into namespaces
  521. * that we're observing.
  522. */
  523. unshare(CLONE_FS);
  524. prctl(PR_SET_NAME, "perf-top-UI", 0, 0, 0);
  525. repeat:
  526. perf_top__sort_new_samples(top);
  527. /*
  528. * Initialize the uid_filter_str, in the future the TUI will allow
  529. * Zooming in/out UIDs. For now just use whatever the user passed
  530. * via --uid.
  531. */
  532. evlist__for_each_entry(top->evlist, pos) {
  533. struct hists *hists = evsel__hists(pos);
  534. hists->uid_filter_str = top->record_opts.target.uid_str;
  535. }
  536. ret = perf_evlist__tui_browse_hists(top->evlist, help, &hbt,
  537. top->min_percent,
  538. &top->session->header.env,
  539. !top->record_opts.overwrite,
  540. &top->annotation_opts);
  541. if (ret == K_RELOAD) {
  542. top->zero = true;
  543. goto repeat;
  544. } else
  545. stop_top();
  546. return NULL;
  547. }
  548. static void display_sig(int sig __maybe_unused)
  549. {
  550. stop_top();
  551. }
  552. static void display_setup_sig(void)
  553. {
  554. signal(SIGSEGV, sighandler_dump_stack);
  555. signal(SIGFPE, sighandler_dump_stack);
  556. signal(SIGINT, display_sig);
  557. signal(SIGQUIT, display_sig);
  558. signal(SIGTERM, display_sig);
  559. }
  560. static void *display_thread(void *arg)
  561. {
  562. struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
  563. struct termios save;
  564. struct perf_top *top = arg;
  565. int delay_msecs, c;
  566. /* In order to read symbols from other namespaces perf to needs to call
  567. * setns(2). This isn't permitted if the struct_fs has multiple users.
  568. * unshare(2) the fs so that we may continue to setns into namespaces
  569. * that we're observing.
  570. */
  571. unshare(CLONE_FS);
  572. prctl(PR_SET_NAME, "perf-top-UI", 0, 0, 0);
  573. display_setup_sig();
  574. pthread__unblock_sigwinch();
  575. repeat:
  576. delay_msecs = top->delay_secs * MSEC_PER_SEC;
  577. set_term_quiet_input(&save);
  578. /* trash return*/
  579. clearerr(stdin);
  580. if (poll(&stdin_poll, 1, 0) > 0)
  581. getc(stdin);
  582. while (!done) {
  583. perf_top__print_sym_table(top);
  584. /*
  585. * Either timeout expired or we got an EINTR due to SIGWINCH,
  586. * refresh screen in both cases.
  587. */
  588. switch (poll(&stdin_poll, 1, delay_msecs)) {
  589. case 0:
  590. continue;
  591. case -1:
  592. if (errno == EINTR)
  593. continue;
  594. __fallthrough;
  595. default:
  596. c = getc(stdin);
  597. tcsetattr(0, TCSAFLUSH, &save);
  598. if (perf_top__handle_keypress(top, c))
  599. goto repeat;
  600. stop_top();
  601. }
  602. }
  603. tcsetattr(0, TCSAFLUSH, &save);
  604. return NULL;
  605. }
  606. static int hist_iter__top_callback(struct hist_entry_iter *iter,
  607. struct addr_location *al, bool single,
  608. void *arg)
  609. {
  610. struct perf_top *top = arg;
  611. struct hist_entry *he = iter->he;
  612. struct evsel *evsel = iter->evsel;
  613. if (perf_hpp_list.sym && single)
  614. perf_top__record_precise_ip(top, he, iter->sample, evsel, al->addr);
  615. hist__account_cycles(iter->sample->branch_stack, al, iter->sample,
  616. !(top->record_opts.branch_stack & PERF_SAMPLE_BRANCH_ANY),
  617. NULL);
  618. return 0;
  619. }
  620. static void perf_event__process_sample(struct perf_tool *tool,
  621. const union perf_event *event,
  622. struct evsel *evsel,
  623. struct perf_sample *sample,
  624. struct machine *machine)
  625. {
  626. struct perf_top *top = container_of(tool, struct perf_top, tool);
  627. struct addr_location al;
  628. int err;
  629. if (!machine && perf_guest) {
  630. static struct intlist *seen;
  631. if (!seen)
  632. seen = intlist__new(NULL);
  633. if (!intlist__has_entry(seen, sample->pid)) {
  634. pr_err("Can't find guest [%d]'s kernel information\n",
  635. sample->pid);
  636. intlist__add(seen, sample->pid);
  637. }
  638. return;
  639. }
  640. if (!machine) {
  641. pr_err("%u unprocessable samples recorded.\r",
  642. top->session->evlist->stats.nr_unprocessable_samples++);
  643. return;
  644. }
  645. if (event->header.misc & PERF_RECORD_MISC_EXACT_IP)
  646. top->exact_samples++;
  647. if (machine__resolve(machine, &al, sample) < 0)
  648. return;
  649. if (top->stitch_lbr)
  650. al.thread->lbr_stitch_enable = true;
  651. if (!machine->kptr_restrict_warned &&
  652. symbol_conf.kptr_restrict &&
  653. al.cpumode == PERF_RECORD_MISC_KERNEL) {
  654. if (!perf_evlist__exclude_kernel(top->session->evlist)) {
  655. ui__warning(
  656. "Kernel address maps (/proc/{kallsyms,modules}) are restricted.\n\n"
  657. "Check /proc/sys/kernel/kptr_restrict and /proc/sys/kernel/perf_event_paranoid.\n\n"
  658. "Kernel%s samples will not be resolved.\n",
  659. al.map && map__has_symbols(al.map) ?
  660. " modules" : "");
  661. if (use_browser <= 0)
  662. sleep(5);
  663. }
  664. machine->kptr_restrict_warned = true;
  665. }
  666. if (al.sym == NULL && al.map != NULL) {
  667. const char *msg = "Kernel samples will not be resolved.\n";
  668. /*
  669. * As we do lazy loading of symtabs we only will know if the
  670. * specified vmlinux file is invalid when we actually have a
  671. * hit in kernel space and then try to load it. So if we get
  672. * here and there are _no_ symbols in the DSO backing the
  673. * kernel map, bail out.
  674. *
  675. * We may never get here, for instance, if we use -K/
  676. * --hide-kernel-symbols, even if the user specifies an
  677. * invalid --vmlinux ;-)
  678. */
  679. if (!machine->kptr_restrict_warned && !top->vmlinux_warned &&
  680. __map__is_kernel(al.map) && map__has_symbols(al.map)) {
  681. if (symbol_conf.vmlinux_name) {
  682. char serr[256];
  683. dso__strerror_load(al.map->dso, serr, sizeof(serr));
  684. ui__warning("The %s file can't be used: %s\n%s",
  685. symbol_conf.vmlinux_name, serr, msg);
  686. } else {
  687. ui__warning("A vmlinux file was not found.\n%s",
  688. msg);
  689. }
  690. if (use_browser <= 0)
  691. sleep(5);
  692. top->vmlinux_warned = true;
  693. }
  694. }
  695. if (al.sym == NULL || !al.sym->idle) {
  696. struct hists *hists = evsel__hists(evsel);
  697. struct hist_entry_iter iter = {
  698. .evsel = evsel,
  699. .sample = sample,
  700. .add_entry_cb = hist_iter__top_callback,
  701. };
  702. if (symbol_conf.cumulate_callchain)
  703. iter.ops = &hist_iter_cumulative;
  704. else
  705. iter.ops = &hist_iter_normal;
  706. pthread_mutex_lock(&hists->lock);
  707. err = hist_entry_iter__add(&iter, &al, top->max_stack, top);
  708. if (err < 0)
  709. pr_err("Problem incrementing symbol period, skipping event\n");
  710. pthread_mutex_unlock(&hists->lock);
  711. }
  712. addr_location__put(&al);
  713. }
  714. static void
  715. perf_top__process_lost(struct perf_top *top, union perf_event *event,
  716. struct evsel *evsel)
  717. {
  718. struct hists *hists = evsel__hists(evsel);
  719. top->lost += event->lost.lost;
  720. top->lost_total += event->lost.lost;
  721. hists->stats.total_lost += event->lost.lost;
  722. }
  723. static void
  724. perf_top__process_lost_samples(struct perf_top *top,
  725. union perf_event *event,
  726. struct evsel *evsel)
  727. {
  728. struct hists *hists = evsel__hists(evsel);
  729. top->lost += event->lost_samples.lost;
  730. top->lost_total += event->lost_samples.lost;
  731. hists->stats.total_lost_samples += event->lost_samples.lost;
  732. }
  733. static u64 last_timestamp;
  734. static void perf_top__mmap_read_idx(struct perf_top *top, int idx)
  735. {
  736. struct record_opts *opts = &top->record_opts;
  737. struct evlist *evlist = top->evlist;
  738. struct mmap *md;
  739. union perf_event *event;
  740. md = opts->overwrite ? &evlist->overwrite_mmap[idx] : &evlist->mmap[idx];
  741. if (perf_mmap__read_init(&md->core) < 0)
  742. return;
  743. while ((event = perf_mmap__read_event(&md->core)) != NULL) {
  744. int ret;
  745. ret = perf_evlist__parse_sample_timestamp(evlist, event, &last_timestamp);
  746. if (ret && ret != -1)
  747. break;
  748. ret = ordered_events__queue(top->qe.in, event, last_timestamp, 0);
  749. if (ret)
  750. break;
  751. perf_mmap__consume(&md->core);
  752. if (top->qe.rotate) {
  753. pthread_mutex_lock(&top->qe.mutex);
  754. top->qe.rotate = false;
  755. pthread_cond_signal(&top->qe.cond);
  756. pthread_mutex_unlock(&top->qe.mutex);
  757. }
  758. }
  759. perf_mmap__read_done(&md->core);
  760. }
  761. static void perf_top__mmap_read(struct perf_top *top)
  762. {
  763. bool overwrite = top->record_opts.overwrite;
  764. struct evlist *evlist = top->evlist;
  765. int i;
  766. if (overwrite)
  767. perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_DATA_PENDING);
  768. for (i = 0; i < top->evlist->core.nr_mmaps; i++)
  769. perf_top__mmap_read_idx(top, i);
  770. if (overwrite) {
  771. perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_EMPTY);
  772. perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
  773. }
  774. }
  775. /*
  776. * Check per-event overwrite term.
  777. * perf top should support consistent term for all events.
  778. * - All events don't have per-event term
  779. * E.g. "cpu/cpu-cycles/,cpu/instructions/"
  780. * Nothing change, return 0.
  781. * - All events have same per-event term
  782. * E.g. "cpu/cpu-cycles,no-overwrite/,cpu/instructions,no-overwrite/
  783. * Using the per-event setting to replace the opts->overwrite if
  784. * they are different, then return 0.
  785. * - Events have different per-event term
  786. * E.g. "cpu/cpu-cycles,overwrite/,cpu/instructions,no-overwrite/"
  787. * Return -1
  788. * - Some of the event set per-event term, but some not.
  789. * E.g. "cpu/cpu-cycles/,cpu/instructions,no-overwrite/"
  790. * Return -1
  791. */
  792. static int perf_top__overwrite_check(struct perf_top *top)
  793. {
  794. struct record_opts *opts = &top->record_opts;
  795. struct evlist *evlist = top->evlist;
  796. struct evsel_config_term *term;
  797. struct list_head *config_terms;
  798. struct evsel *evsel;
  799. int set, overwrite = -1;
  800. evlist__for_each_entry(evlist, evsel) {
  801. set = -1;
  802. config_terms = &evsel->config_terms;
  803. list_for_each_entry(term, config_terms, list) {
  804. if (term->type == EVSEL__CONFIG_TERM_OVERWRITE)
  805. set = term->val.overwrite ? 1 : 0;
  806. }
  807. /* no term for current and previous event (likely) */
  808. if ((overwrite < 0) && (set < 0))
  809. continue;
  810. /* has term for both current and previous event, compare */
  811. if ((overwrite >= 0) && (set >= 0) && (overwrite != set))
  812. return -1;
  813. /* no term for current event but has term for previous one */
  814. if ((overwrite >= 0) && (set < 0))
  815. return -1;
  816. /* has term for current event */
  817. if ((overwrite < 0) && (set >= 0)) {
  818. /* if it's first event, set overwrite */
  819. if (evsel == evlist__first(evlist))
  820. overwrite = set;
  821. else
  822. return -1;
  823. }
  824. }
  825. if ((overwrite >= 0) && (opts->overwrite != overwrite))
  826. opts->overwrite = overwrite;
  827. return 0;
  828. }
  829. static int perf_top_overwrite_fallback(struct perf_top *top,
  830. struct evsel *evsel)
  831. {
  832. struct record_opts *opts = &top->record_opts;
  833. struct evlist *evlist = top->evlist;
  834. struct evsel *counter;
  835. if (!opts->overwrite)
  836. return 0;
  837. /* only fall back when first event fails */
  838. if (evsel != evlist__first(evlist))
  839. return 0;
  840. evlist__for_each_entry(evlist, counter)
  841. counter->core.attr.write_backward = false;
  842. opts->overwrite = false;
  843. pr_debug2("fall back to non-overwrite mode\n");
  844. return 1;
  845. }
  846. static int perf_top__start_counters(struct perf_top *top)
  847. {
  848. char msg[BUFSIZ];
  849. struct evsel *counter;
  850. struct evlist *evlist = top->evlist;
  851. struct record_opts *opts = &top->record_opts;
  852. if (perf_top__overwrite_check(top)) {
  853. ui__error("perf top only support consistent per-event "
  854. "overwrite setting for all events\n");
  855. goto out_err;
  856. }
  857. perf_evlist__config(evlist, opts, &callchain_param);
  858. evlist__for_each_entry(evlist, counter) {
  859. try_again:
  860. if (evsel__open(counter, top->evlist->core.cpus,
  861. top->evlist->core.threads) < 0) {
  862. /*
  863. * Specially handle overwrite fall back.
  864. * Because perf top is the only tool which has
  865. * overwrite mode by default, support
  866. * both overwrite and non-overwrite mode, and
  867. * require consistent mode for all events.
  868. *
  869. * May move it to generic code with more tools
  870. * have similar attribute.
  871. */
  872. if (perf_missing_features.write_backward &&
  873. perf_top_overwrite_fallback(top, counter))
  874. goto try_again;
  875. if (evsel__fallback(counter, errno, msg, sizeof(msg))) {
  876. if (verbose > 0)
  877. ui__warning("%s\n", msg);
  878. goto try_again;
  879. }
  880. evsel__open_strerror(counter, &opts->target, errno, msg, sizeof(msg));
  881. ui__error("%s\n", msg);
  882. goto out_err;
  883. }
  884. }
  885. if (evlist__mmap(evlist, opts->mmap_pages) < 0) {
  886. ui__error("Failed to mmap with %d (%s)\n",
  887. errno, str_error_r(errno, msg, sizeof(msg)));
  888. goto out_err;
  889. }
  890. return 0;
  891. out_err:
  892. return -1;
  893. }
  894. static int callchain_param__setup_sample_type(struct callchain_param *callchain)
  895. {
  896. if (callchain->mode != CHAIN_NONE) {
  897. if (callchain_register_param(callchain) < 0) {
  898. ui__error("Can't register callchain params.\n");
  899. return -EINVAL;
  900. }
  901. }
  902. return 0;
  903. }
  904. static struct ordered_events *rotate_queues(struct perf_top *top)
  905. {
  906. struct ordered_events *in = top->qe.in;
  907. if (top->qe.in == &top->qe.data[1])
  908. top->qe.in = &top->qe.data[0];
  909. else
  910. top->qe.in = &top->qe.data[1];
  911. return in;
  912. }
  913. static void *process_thread(void *arg)
  914. {
  915. struct perf_top *top = arg;
  916. while (!done) {
  917. struct ordered_events *out, *in = top->qe.in;
  918. if (!in->nr_events) {
  919. usleep(100);
  920. continue;
  921. }
  922. out = rotate_queues(top);
  923. pthread_mutex_lock(&top->qe.mutex);
  924. top->qe.rotate = true;
  925. pthread_cond_wait(&top->qe.cond, &top->qe.mutex);
  926. pthread_mutex_unlock(&top->qe.mutex);
  927. if (ordered_events__flush(out, OE_FLUSH__TOP))
  928. pr_err("failed to process events\n");
  929. }
  930. return NULL;
  931. }
  932. /*
  933. * Allow only 'top->delay_secs' seconds behind samples.
  934. */
  935. static int should_drop(struct ordered_event *qevent, struct perf_top *top)
  936. {
  937. union perf_event *event = qevent->event;
  938. u64 delay_timestamp;
  939. if (event->header.type != PERF_RECORD_SAMPLE)
  940. return false;
  941. delay_timestamp = qevent->timestamp + top->delay_secs * NSEC_PER_SEC;
  942. return delay_timestamp < last_timestamp;
  943. }
  944. static int deliver_event(struct ordered_events *qe,
  945. struct ordered_event *qevent)
  946. {
  947. struct perf_top *top = qe->data;
  948. struct evlist *evlist = top->evlist;
  949. struct perf_session *session = top->session;
  950. union perf_event *event = qevent->event;
  951. struct perf_sample sample;
  952. struct evsel *evsel;
  953. struct machine *machine;
  954. int ret = -1;
  955. if (should_drop(qevent, top)) {
  956. top->drop++;
  957. top->drop_total++;
  958. return 0;
  959. }
  960. ret = perf_evlist__parse_sample(evlist, event, &sample);
  961. if (ret) {
  962. pr_err("Can't parse sample, err = %d\n", ret);
  963. goto next_event;
  964. }
  965. evsel = perf_evlist__id2evsel(session->evlist, sample.id);
  966. assert(evsel != NULL);
  967. if (event->header.type == PERF_RECORD_SAMPLE) {
  968. if (evswitch__discard(&top->evswitch, evsel))
  969. return 0;
  970. ++top->samples;
  971. }
  972. switch (sample.cpumode) {
  973. case PERF_RECORD_MISC_USER:
  974. ++top->us_samples;
  975. if (top->hide_user_symbols)
  976. goto next_event;
  977. machine = &session->machines.host;
  978. break;
  979. case PERF_RECORD_MISC_KERNEL:
  980. ++top->kernel_samples;
  981. if (top->hide_kernel_symbols)
  982. goto next_event;
  983. machine = &session->machines.host;
  984. break;
  985. case PERF_RECORD_MISC_GUEST_KERNEL:
  986. ++top->guest_kernel_samples;
  987. machine = perf_session__find_machine(session,
  988. sample.pid);
  989. break;
  990. case PERF_RECORD_MISC_GUEST_USER:
  991. ++top->guest_us_samples;
  992. /*
  993. * TODO: we don't process guest user from host side
  994. * except simple counting.
  995. */
  996. goto next_event;
  997. default:
  998. if (event->header.type == PERF_RECORD_SAMPLE)
  999. goto next_event;
  1000. machine = &session->machines.host;
  1001. break;
  1002. }
  1003. if (event->header.type == PERF_RECORD_SAMPLE) {
  1004. perf_event__process_sample(&top->tool, event, evsel,
  1005. &sample, machine);
  1006. } else if (event->header.type == PERF_RECORD_LOST) {
  1007. perf_top__process_lost(top, event, evsel);
  1008. } else if (event->header.type == PERF_RECORD_LOST_SAMPLES) {
  1009. perf_top__process_lost_samples(top, event, evsel);
  1010. } else if (event->header.type < PERF_RECORD_MAX) {
  1011. hists__inc_nr_events(evsel__hists(evsel), event->header.type);
  1012. machine__process_event(machine, event, &sample);
  1013. } else
  1014. ++session->evlist->stats.nr_unknown_events;
  1015. ret = 0;
  1016. next_event:
  1017. return ret;
  1018. }
  1019. static void init_process_thread(struct perf_top *top)
  1020. {
  1021. ordered_events__init(&top->qe.data[0], deliver_event, top);
  1022. ordered_events__init(&top->qe.data[1], deliver_event, top);
  1023. ordered_events__set_copy_on_queue(&top->qe.data[0], true);
  1024. ordered_events__set_copy_on_queue(&top->qe.data[1], true);
  1025. top->qe.in = &top->qe.data[0];
  1026. pthread_mutex_init(&top->qe.mutex, NULL);
  1027. pthread_cond_init(&top->qe.cond, NULL);
  1028. }
  1029. static int __cmd_top(struct perf_top *top)
  1030. {
  1031. struct record_opts *opts = &top->record_opts;
  1032. pthread_t thread, thread_process;
  1033. int ret;
  1034. if (!top->annotation_opts.objdump_path) {
  1035. ret = perf_env__lookup_objdump(&top->session->header.env,
  1036. &top->annotation_opts.objdump_path);
  1037. if (ret)
  1038. return ret;
  1039. }
  1040. ret = callchain_param__setup_sample_type(&callchain_param);
  1041. if (ret)
  1042. return ret;
  1043. if (perf_session__register_idle_thread(top->session) < 0)
  1044. return ret;
  1045. if (top->nr_threads_synthesize > 1)
  1046. perf_set_multithreaded();
  1047. init_process_thread(top);
  1048. if (opts->record_namespaces)
  1049. top->tool.namespace_events = true;
  1050. if (opts->record_cgroup) {
  1051. #ifdef HAVE_FILE_HANDLE
  1052. top->tool.cgroup_events = true;
  1053. #else
  1054. pr_err("cgroup tracking is not supported.\n");
  1055. return -1;
  1056. #endif
  1057. }
  1058. ret = perf_event__synthesize_bpf_events(top->session, perf_event__process,
  1059. &top->session->machines.host,
  1060. &top->record_opts);
  1061. if (ret < 0)
  1062. pr_debug("Couldn't synthesize BPF events: Pre-existing BPF programs won't have symbols resolved.\n");
  1063. ret = perf_event__synthesize_cgroups(&top->tool, perf_event__process,
  1064. &top->session->machines.host);
  1065. if (ret < 0)
  1066. pr_debug("Couldn't synthesize cgroup events.\n");
  1067. machine__synthesize_threads(&top->session->machines.host, &opts->target,
  1068. top->evlist->core.threads, false,
  1069. top->nr_threads_synthesize);
  1070. if (top->nr_threads_synthesize > 1)
  1071. perf_set_singlethreaded();
  1072. if (perf_hpp_list.socket) {
  1073. ret = perf_env__read_cpu_topology_map(&perf_env);
  1074. if (ret < 0) {
  1075. char errbuf[BUFSIZ];
  1076. const char *err = str_error_r(-ret, errbuf, sizeof(errbuf));
  1077. ui__error("Could not read the CPU topology map: %s\n", err);
  1078. return ret;
  1079. }
  1080. }
  1081. ret = perf_top__start_counters(top);
  1082. if (ret)
  1083. return ret;
  1084. top->session->evlist = top->evlist;
  1085. perf_session__set_id_hdr_size(top->session);
  1086. /*
  1087. * When perf is starting the traced process, all the events (apart from
  1088. * group members) have enable_on_exec=1 set, so don't spoil it by
  1089. * prematurely enabling them.
  1090. *
  1091. * XXX 'top' still doesn't start workloads like record, trace, but should,
  1092. * so leave the check here.
  1093. */
  1094. if (!target__none(&opts->target))
  1095. evlist__enable(top->evlist);
  1096. ret = -1;
  1097. if (pthread_create(&thread_process, NULL, process_thread, top)) {
  1098. ui__error("Could not create process thread.\n");
  1099. return ret;
  1100. }
  1101. if (pthread_create(&thread, NULL, (use_browser > 0 ? display_thread_tui :
  1102. display_thread), top)) {
  1103. ui__error("Could not create display thread.\n");
  1104. goto out_join_thread;
  1105. }
  1106. if (top->realtime_prio) {
  1107. struct sched_param param;
  1108. param.sched_priority = top->realtime_prio;
  1109. if (sched_setscheduler(0, SCHED_FIFO, &param)) {
  1110. ui__error("Could not set realtime priority.\n");
  1111. goto out_join;
  1112. }
  1113. }
  1114. /* Wait for a minimal set of events before starting the snapshot */
  1115. evlist__poll(top->evlist, 100);
  1116. perf_top__mmap_read(top);
  1117. while (!done) {
  1118. u64 hits = top->samples;
  1119. perf_top__mmap_read(top);
  1120. if (opts->overwrite || (hits == top->samples))
  1121. ret = evlist__poll(top->evlist, 100);
  1122. if (resize) {
  1123. perf_top__resize(top);
  1124. resize = 0;
  1125. }
  1126. }
  1127. ret = 0;
  1128. out_join:
  1129. pthread_join(thread, NULL);
  1130. out_join_thread:
  1131. pthread_cond_signal(&top->qe.cond);
  1132. pthread_join(thread_process, NULL);
  1133. return ret;
  1134. }
  1135. static int
  1136. callchain_opt(const struct option *opt, const char *arg, int unset)
  1137. {
  1138. symbol_conf.use_callchain = true;
  1139. return record_callchain_opt(opt, arg, unset);
  1140. }
  1141. static int
  1142. parse_callchain_opt(const struct option *opt, const char *arg, int unset)
  1143. {
  1144. struct callchain_param *callchain = opt->value;
  1145. callchain->enabled = !unset;
  1146. callchain->record_mode = CALLCHAIN_FP;
  1147. /*
  1148. * --no-call-graph
  1149. */
  1150. if (unset) {
  1151. symbol_conf.use_callchain = false;
  1152. callchain->record_mode = CALLCHAIN_NONE;
  1153. return 0;
  1154. }
  1155. return parse_callchain_top_opt(arg);
  1156. }
  1157. static int perf_top_config(const char *var, const char *value, void *cb __maybe_unused)
  1158. {
  1159. if (!strcmp(var, "top.call-graph")) {
  1160. var = "call-graph.record-mode";
  1161. return perf_default_config(var, value, cb);
  1162. }
  1163. if (!strcmp(var, "top.children")) {
  1164. symbol_conf.cumulate_callchain = perf_config_bool(var, value);
  1165. return 0;
  1166. }
  1167. return 0;
  1168. }
  1169. static int
  1170. parse_percent_limit(const struct option *opt, const char *arg,
  1171. int unset __maybe_unused)
  1172. {
  1173. struct perf_top *top = opt->value;
  1174. top->min_percent = strtof(arg, NULL);
  1175. return 0;
  1176. }
  1177. const char top_callchain_help[] = CALLCHAIN_RECORD_HELP CALLCHAIN_REPORT_HELP
  1178. "\n\t\t\t\tDefault: fp,graph,0.5,caller,function";
  1179. int cmd_top(int argc, const char **argv)
  1180. {
  1181. char errbuf[BUFSIZ];
  1182. struct perf_top top = {
  1183. .count_filter = 5,
  1184. .delay_secs = 2,
  1185. .record_opts = {
  1186. .mmap_pages = UINT_MAX,
  1187. .user_freq = UINT_MAX,
  1188. .user_interval = ULLONG_MAX,
  1189. .freq = 4000, /* 4 KHz */
  1190. .target = {
  1191. .uses_mmap = true,
  1192. },
  1193. /*
  1194. * FIXME: This will lose PERF_RECORD_MMAP and other metadata
  1195. * when we pause, fix that and reenable. Probably using a
  1196. * separate evlist with a dummy event, i.e. a non-overwrite
  1197. * ring buffer just for metadata events, while PERF_RECORD_SAMPLE
  1198. * stays in overwrite mode. -acme
  1199. * */
  1200. .overwrite = 0,
  1201. .sample_time = true,
  1202. .sample_time_set = true,
  1203. },
  1204. .max_stack = sysctl__max_stack(),
  1205. .annotation_opts = annotation__default_options,
  1206. .nr_threads_synthesize = UINT_MAX,
  1207. };
  1208. struct record_opts *opts = &top.record_opts;
  1209. struct target *target = &opts->target;
  1210. const struct option options[] = {
  1211. OPT_CALLBACK('e', "event", &top.evlist, "event",
  1212. "event selector. use 'perf list' to list available events",
  1213. parse_events_option),
  1214. OPT_U64('c', "count", &opts->user_interval, "event period to sample"),
  1215. OPT_STRING('p', "pid", &target->pid, "pid",
  1216. "profile events on existing process id"),
  1217. OPT_STRING('t', "tid", &target->tid, "tid",
  1218. "profile events on existing thread id"),
  1219. OPT_BOOLEAN('a', "all-cpus", &target->system_wide,
  1220. "system-wide collection from all CPUs"),
  1221. OPT_STRING('C', "cpu", &target->cpu_list, "cpu",
  1222. "list of cpus to monitor"),
  1223. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  1224. "file", "vmlinux pathname"),
  1225. OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux,
  1226. "don't load vmlinux even if found"),
  1227. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
  1228. "file", "kallsyms pathname"),
  1229. OPT_BOOLEAN('K', "hide_kernel_symbols", &top.hide_kernel_symbols,
  1230. "hide kernel symbols"),
  1231. OPT_CALLBACK('m', "mmap-pages", &opts->mmap_pages, "pages",
  1232. "number of mmap data pages",
  1233. perf_evlist__parse_mmap_pages),
  1234. OPT_INTEGER('r', "realtime", &top.realtime_prio,
  1235. "collect data with this RT SCHED_FIFO priority"),
  1236. OPT_INTEGER('d', "delay", &top.delay_secs,
  1237. "number of seconds to delay between refreshes"),
  1238. OPT_BOOLEAN('D', "dump-symtab", &top.dump_symtab,
  1239. "dump the symbol table used for profiling"),
  1240. OPT_INTEGER('f', "count-filter", &top.count_filter,
  1241. "only display functions with more events than this"),
  1242. OPT_BOOLEAN(0, "group", &opts->group,
  1243. "put the counters into a counter group"),
  1244. OPT_BOOLEAN('i', "no-inherit", &opts->no_inherit,
  1245. "child tasks do not inherit counters"),
  1246. OPT_STRING(0, "sym-annotate", &top.sym_filter, "symbol name",
  1247. "symbol to annotate"),
  1248. OPT_BOOLEAN('z', "zero", &top.zero, "zero history across updates"),
  1249. OPT_CALLBACK('F', "freq", &top.record_opts, "freq or 'max'",
  1250. "profile at this frequency",
  1251. record__parse_freq),
  1252. OPT_INTEGER('E', "entries", &top.print_entries,
  1253. "display this many functions"),
  1254. OPT_BOOLEAN('U', "hide_user_symbols", &top.hide_user_symbols,
  1255. "hide user symbols"),
  1256. OPT_BOOLEAN(0, "tui", &top.use_tui, "Use the TUI interface"),
  1257. OPT_BOOLEAN(0, "stdio", &top.use_stdio, "Use the stdio interface"),
  1258. OPT_INCR('v', "verbose", &verbose,
  1259. "be more verbose (show counter open errors, etc)"),
  1260. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  1261. "sort by key(s): pid, comm, dso, symbol, parent, cpu, srcline, ..."
  1262. " Please refer the man page for the complete list."),
  1263. OPT_STRING(0, "fields", &field_order, "key[,keys...]",
  1264. "output field(s): overhead, period, sample plus all of sort keys"),
  1265. OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
  1266. "Show a column with the number of samples"),
  1267. OPT_CALLBACK_NOOPT('g', NULL, &callchain_param,
  1268. NULL, "enables call-graph recording and display",
  1269. &callchain_opt),
  1270. OPT_CALLBACK(0, "call-graph", &callchain_param,
  1271. "record_mode[,record_size],print_type,threshold[,print_limit],order,sort_key[,branch]",
  1272. top_callchain_help, &parse_callchain_opt),
  1273. OPT_BOOLEAN(0, "children", &symbol_conf.cumulate_callchain,
  1274. "Accumulate callchains of children and show total overhead as well"),
  1275. OPT_INTEGER(0, "max-stack", &top.max_stack,
  1276. "Set the maximum stack depth when parsing the callchain. "
  1277. "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
  1278. OPT_CALLBACK(0, "ignore-callees", NULL, "regex",
  1279. "ignore callees of these functions in call graphs",
  1280. report_parse_ignore_callees_opt),
  1281. OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
  1282. "Show a column with the sum of periods"),
  1283. OPT_STRING(0, "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  1284. "only consider symbols in these dsos"),
  1285. OPT_STRING(0, "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  1286. "only consider symbols in these comms"),
  1287. OPT_STRING(0, "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  1288. "only consider these symbols"),
  1289. OPT_BOOLEAN(0, "source", &top.annotation_opts.annotate_src,
  1290. "Interleave source code with assembly code (default)"),
  1291. OPT_BOOLEAN(0, "asm-raw", &top.annotation_opts.show_asm_raw,
  1292. "Display raw encoding of assembly instructions (default)"),
  1293. OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
  1294. "Enable kernel symbol demangling"),
  1295. OPT_BOOLEAN(0, "no-bpf-event", &top.record_opts.no_bpf_event, "do not record bpf events"),
  1296. OPT_STRING(0, "objdump", &top.annotation_opts.objdump_path, "path",
  1297. "objdump binary to use for disassembly and annotations"),
  1298. OPT_STRING('M', "disassembler-style", &top.annotation_opts.disassembler_style, "disassembler style",
  1299. "Specify disassembler style (e.g. -M intel for intel syntax)"),
  1300. OPT_STRING(0, "prefix", &top.annotation_opts.prefix, "prefix",
  1301. "Add prefix to source file path names in programs (with --prefix-strip)"),
  1302. OPT_STRING(0, "prefix-strip", &top.annotation_opts.prefix_strip, "N",
  1303. "Strip first N entries of source file path name in programs (with --prefix)"),
  1304. OPT_STRING('u', "uid", &target->uid_str, "user", "user to profile"),
  1305. OPT_CALLBACK(0, "percent-limit", &top, "percent",
  1306. "Don't show entries under that percent", parse_percent_limit),
  1307. OPT_CALLBACK(0, "percentage", NULL, "relative|absolute",
  1308. "How to display percentage of filtered entries", parse_filter_percentage),
  1309. OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
  1310. "width[,width...]",
  1311. "don't try to adjust column width, use these fixed values"),
  1312. OPT_UINTEGER(0, "proc-map-timeout", &proc_map_timeout,
  1313. "per thread proc mmap processing timeout in ms"),
  1314. OPT_CALLBACK_NOOPT('b', "branch-any", &opts->branch_stack,
  1315. "branch any", "sample any taken branches",
  1316. parse_branch_stack),
  1317. OPT_CALLBACK('j', "branch-filter", &opts->branch_stack,
  1318. "branch filter mask", "branch stack filter modes",
  1319. parse_branch_stack),
  1320. OPT_BOOLEAN(0, "raw-trace", &symbol_conf.raw_trace,
  1321. "Show raw trace event output (do not use print fmt or plugins)"),
  1322. OPT_BOOLEAN(0, "hierarchy", &symbol_conf.report_hierarchy,
  1323. "Show entries in a hierarchy"),
  1324. OPT_BOOLEAN(0, "overwrite", &top.record_opts.overwrite,
  1325. "Use a backward ring buffer, default: no"),
  1326. OPT_BOOLEAN(0, "force", &symbol_conf.force, "don't complain, do it"),
  1327. OPT_UINTEGER(0, "num-thread-synthesize", &top.nr_threads_synthesize,
  1328. "number of thread to run event synthesize"),
  1329. OPT_BOOLEAN(0, "namespaces", &opts->record_namespaces,
  1330. "Record namespaces events"),
  1331. OPT_BOOLEAN(0, "all-cgroups", &opts->record_cgroup,
  1332. "Record cgroup events"),
  1333. OPT_INTEGER(0, "group-sort-idx", &symbol_conf.group_sort_idx,
  1334. "Sort the output by the event at the index n in group. "
  1335. "If n is invalid, sort by the first event. "
  1336. "WARNING: should be used on grouped events."),
  1337. OPT_BOOLEAN(0, "stitch-lbr", &top.stitch_lbr,
  1338. "Enable LBR callgraph stitching approach"),
  1339. #ifdef HAVE_LIBPFM
  1340. OPT_CALLBACK(0, "pfm-events", &top.evlist, "event",
  1341. "libpfm4 event selector. use 'perf list' to list available events",
  1342. parse_libpfm_events_option),
  1343. #endif
  1344. OPTS_EVSWITCH(&top.evswitch),
  1345. OPT_END()
  1346. };
  1347. const char * const top_usage[] = {
  1348. "perf top [<options>]",
  1349. NULL
  1350. };
  1351. int status = hists__init();
  1352. if (status < 0)
  1353. return status;
  1354. top.annotation_opts.min_pcnt = 5;
  1355. top.annotation_opts.context = 4;
  1356. top.evlist = evlist__new();
  1357. if (top.evlist == NULL)
  1358. return -ENOMEM;
  1359. status = perf_config(perf_top_config, &top);
  1360. if (status)
  1361. return status;
  1362. /*
  1363. * Since the per arch annotation init routine may need the cpuid, read
  1364. * it here, since we are not getting this from the perf.data header.
  1365. */
  1366. status = perf_env__read_cpuid(&perf_env);
  1367. if (status) {
  1368. /*
  1369. * Some arches do not provide a get_cpuid(), so just use pr_debug, otherwise
  1370. * warn the user explicitely.
  1371. */
  1372. eprintf(status == ENOSYS ? 1 : 0, verbose,
  1373. "Couldn't read the cpuid for this machine: %s\n",
  1374. str_error_r(errno, errbuf, sizeof(errbuf)));
  1375. }
  1376. top.evlist->env = &perf_env;
  1377. argc = parse_options(argc, argv, options, top_usage, 0);
  1378. if (argc)
  1379. usage_with_options(top_usage, options);
  1380. if (annotate_check_args(&top.annotation_opts) < 0)
  1381. goto out_delete_evlist;
  1382. if (!top.evlist->core.nr_entries &&
  1383. evlist__add_default(top.evlist) < 0) {
  1384. pr_err("Not enough memory for event selector list\n");
  1385. goto out_delete_evlist;
  1386. }
  1387. status = evswitch__init(&top.evswitch, top.evlist, stderr);
  1388. if (status)
  1389. goto out_delete_evlist;
  1390. if (symbol_conf.report_hierarchy) {
  1391. /* disable incompatible options */
  1392. symbol_conf.event_group = false;
  1393. symbol_conf.cumulate_callchain = false;
  1394. if (field_order) {
  1395. pr_err("Error: --hierarchy and --fields options cannot be used together\n");
  1396. parse_options_usage(top_usage, options, "fields", 0);
  1397. parse_options_usage(NULL, options, "hierarchy", 0);
  1398. goto out_delete_evlist;
  1399. }
  1400. }
  1401. if (top.stitch_lbr && !(callchain_param.record_mode == CALLCHAIN_LBR)) {
  1402. pr_err("Error: --stitch-lbr must be used with --call-graph lbr\n");
  1403. goto out_delete_evlist;
  1404. }
  1405. if (opts->branch_stack && callchain_param.enabled)
  1406. symbol_conf.show_branchflag_count = true;
  1407. sort__mode = SORT_MODE__TOP;
  1408. /* display thread wants entries to be collapsed in a different tree */
  1409. perf_hpp_list.need_collapse = 1;
  1410. if (top.use_stdio)
  1411. use_browser = 0;
  1412. else if (top.use_tui)
  1413. use_browser = 1;
  1414. setup_browser(false);
  1415. if (setup_sorting(top.evlist) < 0) {
  1416. if (sort_order)
  1417. parse_options_usage(top_usage, options, "s", 1);
  1418. if (field_order)
  1419. parse_options_usage(sort_order ? NULL : top_usage,
  1420. options, "fields", 0);
  1421. goto out_delete_evlist;
  1422. }
  1423. status = target__validate(target);
  1424. if (status) {
  1425. target__strerror(target, status, errbuf, BUFSIZ);
  1426. ui__warning("%s\n", errbuf);
  1427. }
  1428. status = target__parse_uid(target);
  1429. if (status) {
  1430. int saved_errno = errno;
  1431. target__strerror(target, status, errbuf, BUFSIZ);
  1432. ui__error("%s\n", errbuf);
  1433. status = -saved_errno;
  1434. goto out_delete_evlist;
  1435. }
  1436. if (target__none(target))
  1437. target->system_wide = true;
  1438. if (perf_evlist__create_maps(top.evlist, target) < 0) {
  1439. ui__error("Couldn't create thread/CPU maps: %s\n",
  1440. errno == ENOENT ? "No such process" : str_error_r(errno, errbuf, sizeof(errbuf)));
  1441. goto out_delete_evlist;
  1442. }
  1443. if (top.delay_secs < 1)
  1444. top.delay_secs = 1;
  1445. if (record_opts__config(opts)) {
  1446. status = -EINVAL;
  1447. goto out_delete_evlist;
  1448. }
  1449. top.sym_evsel = evlist__first(top.evlist);
  1450. if (!callchain_param.enabled) {
  1451. symbol_conf.cumulate_callchain = false;
  1452. perf_hpp__cancel_cumulate();
  1453. }
  1454. if (symbol_conf.cumulate_callchain && !callchain_param.order_set)
  1455. callchain_param.order = ORDER_CALLER;
  1456. status = symbol__annotation_init();
  1457. if (status < 0)
  1458. goto out_delete_evlist;
  1459. annotation_config__init(&top.annotation_opts);
  1460. symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
  1461. status = symbol__init(NULL);
  1462. if (status < 0)
  1463. goto out_delete_evlist;
  1464. sort__setup_elide(stdout);
  1465. get_term_dimensions(&top.winsize);
  1466. if (top.print_entries == 0) {
  1467. perf_top__update_print_entries(&top);
  1468. signal(SIGWINCH, winch_sig);
  1469. }
  1470. top.session = perf_session__new(NULL, false, NULL);
  1471. if (IS_ERR(top.session)) {
  1472. status = PTR_ERR(top.session);
  1473. goto out_delete_evlist;
  1474. }
  1475. #ifdef HAVE_LIBBPF_SUPPORT
  1476. if (!top.record_opts.no_bpf_event) {
  1477. top.sb_evlist = evlist__new();
  1478. if (top.sb_evlist == NULL) {
  1479. pr_err("Couldn't create side band evlist.\n.");
  1480. goto out_delete_evlist;
  1481. }
  1482. if (evlist__add_bpf_sb_event(top.sb_evlist, &perf_env)) {
  1483. pr_err("Couldn't ask for PERF_RECORD_BPF_EVENT side band events.\n.");
  1484. goto out_delete_evlist;
  1485. }
  1486. }
  1487. #endif
  1488. if (perf_evlist__start_sb_thread(top.sb_evlist, target)) {
  1489. pr_debug("Couldn't start the BPF side band thread:\nBPF programs starting from now on won't be annotatable\n");
  1490. opts->no_bpf_event = true;
  1491. }
  1492. status = __cmd_top(&top);
  1493. if (!opts->no_bpf_event)
  1494. perf_evlist__stop_sb_thread(top.sb_evlist);
  1495. out_delete_evlist:
  1496. evlist__delete(top.evlist);
  1497. perf_session__delete(top.session);
  1498. return status;
  1499. }