builtin-top.c 48 KB

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