builtin-top.c 40 KB

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