builtin-top.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517
  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. getc(stdin);
  547. while (!done) {
  548. perf_top__print_sym_table(top);
  549. /*
  550. * Either timeout expired or we got an EINTR due to SIGWINCH,
  551. * refresh screen in both cases.
  552. */
  553. switch (poll(&stdin_poll, 1, delay_msecs)) {
  554. case 0:
  555. continue;
  556. case -1:
  557. if (errno == EINTR)
  558. continue;
  559. __fallthrough;
  560. default:
  561. c = getc(stdin);
  562. tcsetattr(0, TCSAFLUSH, &save);
  563. if (perf_top__handle_keypress(top, c))
  564. goto repeat;
  565. done = 1;
  566. }
  567. }
  568. tcsetattr(0, TCSAFLUSH, &save);
  569. return NULL;
  570. }
  571. static int hist_iter__top_callback(struct hist_entry_iter *iter,
  572. struct addr_location *al, bool single,
  573. void *arg)
  574. {
  575. struct perf_top *top = arg;
  576. struct hist_entry *he = iter->he;
  577. struct perf_evsel *evsel = iter->evsel;
  578. if (perf_hpp_list.sym && single)
  579. perf_top__record_precise_ip(top, he, iter->sample, evsel, al->addr);
  580. hist__account_cycles(iter->sample->branch_stack, al, iter->sample,
  581. !(top->record_opts.branch_stack & PERF_SAMPLE_BRANCH_ANY));
  582. return 0;
  583. }
  584. static void perf_event__process_sample(struct perf_tool *tool,
  585. const union perf_event *event,
  586. struct perf_evsel *evsel,
  587. struct perf_sample *sample,
  588. struct machine *machine)
  589. {
  590. struct perf_top *top = container_of(tool, struct perf_top, tool);
  591. struct addr_location al;
  592. int err;
  593. if (!machine && perf_guest) {
  594. static struct intlist *seen;
  595. if (!seen)
  596. seen = intlist__new(NULL);
  597. if (!intlist__has_entry(seen, sample->pid)) {
  598. pr_err("Can't find guest [%d]'s kernel information\n",
  599. sample->pid);
  600. intlist__add(seen, sample->pid);
  601. }
  602. return;
  603. }
  604. if (!machine) {
  605. pr_err("%u unprocessable samples recorded.\r",
  606. top->session->evlist->stats.nr_unprocessable_samples++);
  607. return;
  608. }
  609. if (event->header.misc & PERF_RECORD_MISC_EXACT_IP)
  610. top->exact_samples++;
  611. if (machine__resolve(machine, &al, sample) < 0)
  612. return;
  613. if (!machine->kptr_restrict_warned &&
  614. symbol_conf.kptr_restrict &&
  615. al.cpumode == PERF_RECORD_MISC_KERNEL) {
  616. if (!perf_evlist__exclude_kernel(top->session->evlist)) {
  617. ui__warning(
  618. "Kernel address maps (/proc/{kallsyms,modules}) are restricted.\n\n"
  619. "Check /proc/sys/kernel/kptr_restrict.\n\n"
  620. "Kernel%s samples will not be resolved.\n",
  621. al.map && map__has_symbols(al.map) ?
  622. " modules" : "");
  623. if (use_browser <= 0)
  624. sleep(5);
  625. }
  626. machine->kptr_restrict_warned = true;
  627. }
  628. if (al.sym == NULL && al.map != NULL) {
  629. const char *msg = "Kernel samples will not be resolved.\n";
  630. /*
  631. * As we do lazy loading of symtabs we only will know if the
  632. * specified vmlinux file is invalid when we actually have a
  633. * hit in kernel space and then try to load it. So if we get
  634. * here and there are _no_ symbols in the DSO backing the
  635. * kernel map, bail out.
  636. *
  637. * We may never get here, for instance, if we use -K/
  638. * --hide-kernel-symbols, even if the user specifies an
  639. * invalid --vmlinux ;-)
  640. */
  641. if (!machine->kptr_restrict_warned && !top->vmlinux_warned &&
  642. __map__is_kernel(al.map) && map__has_symbols(al.map)) {
  643. if (symbol_conf.vmlinux_name) {
  644. char serr[256];
  645. dso__strerror_load(al.map->dso, serr, sizeof(serr));
  646. ui__warning("The %s file can't be used: %s\n%s",
  647. symbol_conf.vmlinux_name, serr, msg);
  648. } else {
  649. ui__warning("A vmlinux file was not found.\n%s",
  650. msg);
  651. }
  652. if (use_browser <= 0)
  653. sleep(5);
  654. top->vmlinux_warned = true;
  655. }
  656. }
  657. if (al.sym == NULL || !al.sym->idle) {
  658. struct hists *hists = evsel__hists(evsel);
  659. struct hist_entry_iter iter = {
  660. .evsel = evsel,
  661. .sample = sample,
  662. .add_entry_cb = hist_iter__top_callback,
  663. };
  664. if (symbol_conf.cumulate_callchain)
  665. iter.ops = &hist_iter_cumulative;
  666. else
  667. iter.ops = &hist_iter_normal;
  668. pthread_mutex_lock(&hists->lock);
  669. err = hist_entry_iter__add(&iter, &al, top->max_stack, top);
  670. if (err < 0)
  671. pr_err("Problem incrementing symbol period, skipping event\n");
  672. pthread_mutex_unlock(&hists->lock);
  673. }
  674. addr_location__put(&al);
  675. }
  676. static void perf_top__mmap_read_idx(struct perf_top *top, int idx)
  677. {
  678. struct record_opts *opts = &top->record_opts;
  679. struct perf_evlist *evlist = top->evlist;
  680. struct perf_sample sample;
  681. struct perf_evsel *evsel;
  682. struct perf_mmap *md;
  683. struct perf_session *session = top->session;
  684. union perf_event *event;
  685. struct machine *machine;
  686. int ret;
  687. md = opts->overwrite ? &evlist->overwrite_mmap[idx] : &evlist->mmap[idx];
  688. if (perf_mmap__read_init(md) < 0)
  689. return;
  690. while ((event = perf_mmap__read_event(md)) != NULL) {
  691. ret = perf_evlist__parse_sample(evlist, event, &sample);
  692. if (ret) {
  693. pr_err("Can't parse sample, err = %d\n", ret);
  694. goto next_event;
  695. }
  696. evsel = perf_evlist__id2evsel(session->evlist, sample.id);
  697. assert(evsel != NULL);
  698. if (event->header.type == PERF_RECORD_SAMPLE)
  699. ++top->samples;
  700. switch (sample.cpumode) {
  701. case PERF_RECORD_MISC_USER:
  702. ++top->us_samples;
  703. if (top->hide_user_symbols)
  704. goto next_event;
  705. machine = &session->machines.host;
  706. break;
  707. case PERF_RECORD_MISC_KERNEL:
  708. ++top->kernel_samples;
  709. if (top->hide_kernel_symbols)
  710. goto next_event;
  711. machine = &session->machines.host;
  712. break;
  713. case PERF_RECORD_MISC_GUEST_KERNEL:
  714. ++top->guest_kernel_samples;
  715. machine = perf_session__find_machine(session,
  716. sample.pid);
  717. break;
  718. case PERF_RECORD_MISC_GUEST_USER:
  719. ++top->guest_us_samples;
  720. /*
  721. * TODO: we don't process guest user from host side
  722. * except simple counting.
  723. */
  724. goto next_event;
  725. default:
  726. if (event->header.type == PERF_RECORD_SAMPLE)
  727. goto next_event;
  728. machine = &session->machines.host;
  729. break;
  730. }
  731. if (event->header.type == PERF_RECORD_SAMPLE) {
  732. perf_event__process_sample(&top->tool, event, evsel,
  733. &sample, machine);
  734. } else if (event->header.type < PERF_RECORD_MAX) {
  735. hists__inc_nr_events(evsel__hists(evsel), event->header.type);
  736. machine__process_event(machine, event, &sample);
  737. } else
  738. ++session->evlist->stats.nr_unknown_events;
  739. next_event:
  740. perf_mmap__consume(md);
  741. }
  742. perf_mmap__read_done(md);
  743. }
  744. static void perf_top__mmap_read(struct perf_top *top)
  745. {
  746. bool overwrite = top->record_opts.overwrite;
  747. struct perf_evlist *evlist = top->evlist;
  748. unsigned long long start, end;
  749. int i;
  750. start = rdclock();
  751. if (overwrite)
  752. perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_DATA_PENDING);
  753. for (i = 0; i < top->evlist->nr_mmaps; i++)
  754. perf_top__mmap_read_idx(top, i);
  755. if (overwrite) {
  756. perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_EMPTY);
  757. perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
  758. }
  759. end = rdclock();
  760. if ((end - start) > (unsigned long long)top->delay_secs * NSEC_PER_SEC)
  761. ui__warning("Too slow to read ring buffer.\n"
  762. "Please try increasing the period (-c) or\n"
  763. "decreasing the freq (-F) or\n"
  764. "limiting the number of CPUs (-C)\n");
  765. }
  766. /*
  767. * Check per-event overwrite term.
  768. * perf top should support consistent term for all events.
  769. * - All events don't have per-event term
  770. * E.g. "cpu/cpu-cycles/,cpu/instructions/"
  771. * Nothing change, return 0.
  772. * - All events have same per-event term
  773. * E.g. "cpu/cpu-cycles,no-overwrite/,cpu/instructions,no-overwrite/
  774. * Using the per-event setting to replace the opts->overwrite if
  775. * they are different, then return 0.
  776. * - Events have different per-event term
  777. * E.g. "cpu/cpu-cycles,overwrite/,cpu/instructions,no-overwrite/"
  778. * Return -1
  779. * - Some of the event set per-event term, but some not.
  780. * E.g. "cpu/cpu-cycles/,cpu/instructions,no-overwrite/"
  781. * Return -1
  782. */
  783. static int perf_top__overwrite_check(struct perf_top *top)
  784. {
  785. struct record_opts *opts = &top->record_opts;
  786. struct perf_evlist *evlist = top->evlist;
  787. struct perf_evsel_config_term *term;
  788. struct list_head *config_terms;
  789. struct perf_evsel *evsel;
  790. int set, overwrite = -1;
  791. evlist__for_each_entry(evlist, evsel) {
  792. set = -1;
  793. config_terms = &evsel->config_terms;
  794. list_for_each_entry(term, config_terms, list) {
  795. if (term->type == PERF_EVSEL__CONFIG_TERM_OVERWRITE)
  796. set = term->val.overwrite ? 1 : 0;
  797. }
  798. /* no term for current and previous event (likely) */
  799. if ((overwrite < 0) && (set < 0))
  800. continue;
  801. /* has term for both current and previous event, compare */
  802. if ((overwrite >= 0) && (set >= 0) && (overwrite != set))
  803. return -1;
  804. /* no term for current event but has term for previous one */
  805. if ((overwrite >= 0) && (set < 0))
  806. return -1;
  807. /* has term for current event */
  808. if ((overwrite < 0) && (set >= 0)) {
  809. /* if it's first event, set overwrite */
  810. if (evsel == perf_evlist__first(evlist))
  811. overwrite = set;
  812. else
  813. return -1;
  814. }
  815. }
  816. if ((overwrite >= 0) && (opts->overwrite != overwrite))
  817. opts->overwrite = overwrite;
  818. return 0;
  819. }
  820. static int perf_top_overwrite_fallback(struct perf_top *top,
  821. struct perf_evsel *evsel)
  822. {
  823. struct record_opts *opts = &top->record_opts;
  824. struct perf_evlist *evlist = top->evlist;
  825. struct perf_evsel *counter;
  826. if (!opts->overwrite)
  827. return 0;
  828. /* only fall back when first event fails */
  829. if (evsel != perf_evlist__first(evlist))
  830. return 0;
  831. evlist__for_each_entry(evlist, counter)
  832. counter->attr.write_backward = false;
  833. opts->overwrite = false;
  834. pr_debug2("fall back to non-overwrite mode\n");
  835. return 1;
  836. }
  837. static int perf_top__start_counters(struct perf_top *top)
  838. {
  839. char msg[BUFSIZ];
  840. struct perf_evsel *counter;
  841. struct perf_evlist *evlist = top->evlist;
  842. struct record_opts *opts = &top->record_opts;
  843. if (perf_top__overwrite_check(top)) {
  844. ui__error("perf top only support consistent per-event "
  845. "overwrite setting for all events\n");
  846. goto out_err;
  847. }
  848. perf_evlist__config(evlist, opts, &callchain_param);
  849. evlist__for_each_entry(evlist, counter) {
  850. try_again:
  851. if (perf_evsel__open(counter, top->evlist->cpus,
  852. top->evlist->threads) < 0) {
  853. /*
  854. * Specially handle overwrite fall back.
  855. * Because perf top is the only tool which has
  856. * overwrite mode by default, support
  857. * both overwrite and non-overwrite mode, and
  858. * require consistent mode for all events.
  859. *
  860. * May move it to generic code with more tools
  861. * have similar attribute.
  862. */
  863. if (perf_missing_features.write_backward &&
  864. perf_top_overwrite_fallback(top, counter))
  865. goto try_again;
  866. if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) {
  867. if (verbose > 0)
  868. ui__warning("%s\n", msg);
  869. goto try_again;
  870. }
  871. perf_evsel__open_strerror(counter, &opts->target,
  872. errno, msg, sizeof(msg));
  873. ui__error("%s\n", msg);
  874. goto out_err;
  875. }
  876. }
  877. if (perf_evlist__mmap(evlist, opts->mmap_pages) < 0) {
  878. ui__error("Failed to mmap with %d (%s)\n",
  879. errno, str_error_r(errno, msg, sizeof(msg)));
  880. goto out_err;
  881. }
  882. return 0;
  883. out_err:
  884. return -1;
  885. }
  886. static int callchain_param__setup_sample_type(struct callchain_param *callchain)
  887. {
  888. if (!perf_hpp_list.sym) {
  889. if (callchain->enabled) {
  890. ui__error("Selected -g but \"sym\" not present in --sort/-s.");
  891. return -EINVAL;
  892. }
  893. } else if (callchain->mode != CHAIN_NONE) {
  894. if (callchain_register_param(callchain) < 0) {
  895. ui__error("Can't register callchain params.\n");
  896. return -EINVAL;
  897. }
  898. }
  899. return 0;
  900. }
  901. static int __cmd_top(struct perf_top *top)
  902. {
  903. char msg[512];
  904. struct perf_evsel *pos;
  905. struct perf_evsel_config_term *err_term;
  906. struct perf_evlist *evlist = top->evlist;
  907. struct record_opts *opts = &top->record_opts;
  908. pthread_t thread;
  909. int ret;
  910. top->session = perf_session__new(NULL, false, NULL);
  911. if (top->session == NULL)
  912. return -1;
  913. if (!top->annotation_opts.objdump_path) {
  914. ret = perf_env__lookup_objdump(&top->session->header.env,
  915. &top->annotation_opts.objdump_path);
  916. if (ret)
  917. goto out_delete;
  918. }
  919. ret = callchain_param__setup_sample_type(&callchain_param);
  920. if (ret)
  921. goto out_delete;
  922. if (perf_session__register_idle_thread(top->session) < 0)
  923. goto out_delete;
  924. if (top->nr_threads_synthesize > 1)
  925. perf_set_multithreaded();
  926. machine__synthesize_threads(&top->session->machines.host, &opts->target,
  927. top->evlist->threads, false,
  928. opts->proc_map_timeout,
  929. top->nr_threads_synthesize);
  930. if (top->nr_threads_synthesize > 1)
  931. perf_set_singlethreaded();
  932. if (perf_hpp_list.socket) {
  933. ret = perf_env__read_cpu_topology_map(&perf_env);
  934. if (ret < 0)
  935. goto out_err_cpu_topo;
  936. }
  937. ret = perf_top__start_counters(top);
  938. if (ret)
  939. goto out_delete;
  940. ret = perf_evlist__apply_drv_configs(evlist, &pos, &err_term);
  941. if (ret) {
  942. pr_err("failed to set config \"%s\" on event %s with %d (%s)\n",
  943. err_term->val.drv_cfg, perf_evsel__name(pos), errno,
  944. str_error_r(errno, msg, sizeof(msg)));
  945. goto out_delete;
  946. }
  947. top->session->evlist = top->evlist;
  948. perf_session__set_id_hdr_size(top->session);
  949. /*
  950. * When perf is starting the traced process, all the events (apart from
  951. * group members) have enable_on_exec=1 set, so don't spoil it by
  952. * prematurely enabling them.
  953. *
  954. * XXX 'top' still doesn't start workloads like record, trace, but should,
  955. * so leave the check here.
  956. */
  957. if (!target__none(&opts->target))
  958. perf_evlist__enable(top->evlist);
  959. /* Wait for a minimal set of events before starting the snapshot */
  960. perf_evlist__poll(top->evlist, 100);
  961. perf_top__mmap_read(top);
  962. ret = -1;
  963. if (pthread_create(&thread, NULL, (use_browser > 0 ? display_thread_tui :
  964. display_thread), top)) {
  965. ui__error("Could not create display thread.\n");
  966. goto out_delete;
  967. }
  968. if (top->realtime_prio) {
  969. struct sched_param param;
  970. param.sched_priority = top->realtime_prio;
  971. if (sched_setscheduler(0, SCHED_FIFO, &param)) {
  972. ui__error("Could not set realtime priority.\n");
  973. goto out_join;
  974. }
  975. }
  976. while (!done) {
  977. u64 hits = top->samples;
  978. perf_top__mmap_read(top);
  979. if (opts->overwrite || (hits == top->samples))
  980. ret = perf_evlist__poll(top->evlist, 100);
  981. if (resize) {
  982. perf_top__resize(top);
  983. resize = 0;
  984. }
  985. }
  986. ret = 0;
  987. out_join:
  988. pthread_join(thread, NULL);
  989. out_delete:
  990. perf_session__delete(top->session);
  991. top->session = NULL;
  992. return ret;
  993. out_err_cpu_topo: {
  994. char errbuf[BUFSIZ];
  995. const char *err = str_error_r(-ret, errbuf, sizeof(errbuf));
  996. ui__error("Could not read the CPU topology map: %s\n", err);
  997. goto out_delete;
  998. }
  999. }
  1000. static int
  1001. callchain_opt(const struct option *opt, const char *arg, int unset)
  1002. {
  1003. symbol_conf.use_callchain = true;
  1004. return record_callchain_opt(opt, arg, unset);
  1005. }
  1006. static int
  1007. parse_callchain_opt(const struct option *opt, const char *arg, int unset)
  1008. {
  1009. struct callchain_param *callchain = opt->value;
  1010. callchain->enabled = !unset;
  1011. callchain->record_mode = CALLCHAIN_FP;
  1012. /*
  1013. * --no-call-graph
  1014. */
  1015. if (unset) {
  1016. symbol_conf.use_callchain = false;
  1017. callchain->record_mode = CALLCHAIN_NONE;
  1018. return 0;
  1019. }
  1020. return parse_callchain_top_opt(arg);
  1021. }
  1022. static int perf_top_config(const char *var, const char *value, void *cb __maybe_unused)
  1023. {
  1024. if (!strcmp(var, "top.call-graph")) {
  1025. var = "call-graph.record-mode";
  1026. return perf_default_config(var, value, cb);
  1027. }
  1028. if (!strcmp(var, "top.children")) {
  1029. symbol_conf.cumulate_callchain = perf_config_bool(var, value);
  1030. return 0;
  1031. }
  1032. return 0;
  1033. }
  1034. static int
  1035. parse_percent_limit(const struct option *opt, const char *arg,
  1036. int unset __maybe_unused)
  1037. {
  1038. struct perf_top *top = opt->value;
  1039. top->min_percent = strtof(arg, NULL);
  1040. return 0;
  1041. }
  1042. const char top_callchain_help[] = CALLCHAIN_RECORD_HELP CALLCHAIN_REPORT_HELP
  1043. "\n\t\t\t\tDefault: fp,graph,0.5,caller,function";
  1044. int cmd_top(int argc, const char **argv)
  1045. {
  1046. char errbuf[BUFSIZ];
  1047. struct perf_top top = {
  1048. .count_filter = 5,
  1049. .delay_secs = 2,
  1050. .record_opts = {
  1051. .mmap_pages = UINT_MAX,
  1052. .user_freq = UINT_MAX,
  1053. .user_interval = ULLONG_MAX,
  1054. .freq = 4000, /* 4 KHz */
  1055. .target = {
  1056. .uses_mmap = true,
  1057. },
  1058. .proc_map_timeout = 500,
  1059. .overwrite = 1,
  1060. },
  1061. .max_stack = sysctl__max_stack(),
  1062. .annotation_opts = annotation__default_options,
  1063. .nr_threads_synthesize = UINT_MAX,
  1064. };
  1065. struct record_opts *opts = &top.record_opts;
  1066. struct target *target = &opts->target;
  1067. const struct option options[] = {
  1068. OPT_CALLBACK('e', "event", &top.evlist, "event",
  1069. "event selector. use 'perf list' to list available events",
  1070. parse_events_option),
  1071. OPT_U64('c', "count", &opts->user_interval, "event period to sample"),
  1072. OPT_STRING('p', "pid", &target->pid, "pid",
  1073. "profile events on existing process id"),
  1074. OPT_STRING('t', "tid", &target->tid, "tid",
  1075. "profile events on existing thread id"),
  1076. OPT_BOOLEAN('a', "all-cpus", &target->system_wide,
  1077. "system-wide collection from all CPUs"),
  1078. OPT_STRING('C', "cpu", &target->cpu_list, "cpu",
  1079. "list of cpus to monitor"),
  1080. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  1081. "file", "vmlinux pathname"),
  1082. OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux,
  1083. "don't load vmlinux even if found"),
  1084. OPT_BOOLEAN('K', "hide_kernel_symbols", &top.hide_kernel_symbols,
  1085. "hide kernel symbols"),
  1086. OPT_CALLBACK('m', "mmap-pages", &opts->mmap_pages, "pages",
  1087. "number of mmap data pages",
  1088. perf_evlist__parse_mmap_pages),
  1089. OPT_INTEGER('r', "realtime", &top.realtime_prio,
  1090. "collect data with this RT SCHED_FIFO priority"),
  1091. OPT_INTEGER('d', "delay", &top.delay_secs,
  1092. "number of seconds to delay between refreshes"),
  1093. OPT_BOOLEAN('D', "dump-symtab", &top.dump_symtab,
  1094. "dump the symbol table used for profiling"),
  1095. OPT_INTEGER('f', "count-filter", &top.count_filter,
  1096. "only display functions with more events than this"),
  1097. OPT_BOOLEAN(0, "group", &opts->group,
  1098. "put the counters into a counter group"),
  1099. OPT_BOOLEAN('i', "no-inherit", &opts->no_inherit,
  1100. "child tasks do not inherit counters"),
  1101. OPT_STRING(0, "sym-annotate", &top.sym_filter, "symbol name",
  1102. "symbol to annotate"),
  1103. OPT_BOOLEAN('z', "zero", &top.zero, "zero history across updates"),
  1104. OPT_CALLBACK('F', "freq", &top.record_opts, "freq or 'max'",
  1105. "profile at this frequency",
  1106. record__parse_freq),
  1107. OPT_INTEGER('E', "entries", &top.print_entries,
  1108. "display this many functions"),
  1109. OPT_BOOLEAN('U', "hide_user_symbols", &top.hide_user_symbols,
  1110. "hide user symbols"),
  1111. OPT_BOOLEAN(0, "tui", &top.use_tui, "Use the TUI interface"),
  1112. OPT_BOOLEAN(0, "stdio", &top.use_stdio, "Use the stdio interface"),
  1113. OPT_INCR('v', "verbose", &verbose,
  1114. "be more verbose (show counter open errors, etc)"),
  1115. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  1116. "sort by key(s): pid, comm, dso, symbol, parent, cpu, srcline, ..."
  1117. " Please refer the man page for the complete list."),
  1118. OPT_STRING(0, "fields", &field_order, "key[,keys...]",
  1119. "output field(s): overhead, period, sample plus all of sort keys"),
  1120. OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
  1121. "Show a column with the number of samples"),
  1122. OPT_CALLBACK_NOOPT('g', NULL, &callchain_param,
  1123. NULL, "enables call-graph recording and display",
  1124. &callchain_opt),
  1125. OPT_CALLBACK(0, "call-graph", &callchain_param,
  1126. "record_mode[,record_size],print_type,threshold[,print_limit],order,sort_key[,branch]",
  1127. top_callchain_help, &parse_callchain_opt),
  1128. OPT_BOOLEAN(0, "children", &symbol_conf.cumulate_callchain,
  1129. "Accumulate callchains of children and show total overhead as well"),
  1130. OPT_INTEGER(0, "max-stack", &top.max_stack,
  1131. "Set the maximum stack depth when parsing the callchain. "
  1132. "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
  1133. OPT_CALLBACK(0, "ignore-callees", NULL, "regex",
  1134. "ignore callees of these functions in call graphs",
  1135. report_parse_ignore_callees_opt),
  1136. OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
  1137. "Show a column with the sum of periods"),
  1138. OPT_STRING(0, "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  1139. "only consider symbols in these dsos"),
  1140. OPT_STRING(0, "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  1141. "only consider symbols in these comms"),
  1142. OPT_STRING(0, "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  1143. "only consider these symbols"),
  1144. OPT_BOOLEAN(0, "source", &top.annotation_opts.annotate_src,
  1145. "Interleave source code with assembly code (default)"),
  1146. OPT_BOOLEAN(0, "asm-raw", &top.annotation_opts.show_asm_raw,
  1147. "Display raw encoding of assembly instructions (default)"),
  1148. OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
  1149. "Enable kernel symbol demangling"),
  1150. OPT_STRING(0, "objdump", &top.annotation_opts.objdump_path, "path",
  1151. "objdump binary to use for disassembly and annotations"),
  1152. OPT_STRING('M', "disassembler-style", &top.annotation_opts.disassembler_style, "disassembler style",
  1153. "Specify disassembler style (e.g. -M intel for intel syntax)"),
  1154. OPT_STRING('u', "uid", &target->uid_str, "user", "user to profile"),
  1155. OPT_CALLBACK(0, "percent-limit", &top, "percent",
  1156. "Don't show entries under that percent", parse_percent_limit),
  1157. OPT_CALLBACK(0, "percentage", NULL, "relative|absolute",
  1158. "How to display percentage of filtered entries", parse_filter_percentage),
  1159. OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
  1160. "width[,width...]",
  1161. "don't try to adjust column width, use these fixed values"),
  1162. OPT_UINTEGER(0, "proc-map-timeout", &opts->proc_map_timeout,
  1163. "per thread proc mmap processing timeout in ms"),
  1164. OPT_CALLBACK_NOOPT('b', "branch-any", &opts->branch_stack,
  1165. "branch any", "sample any taken branches",
  1166. parse_branch_stack),
  1167. OPT_CALLBACK('j', "branch-filter", &opts->branch_stack,
  1168. "branch filter mask", "branch stack filter modes",
  1169. parse_branch_stack),
  1170. OPT_BOOLEAN(0, "raw-trace", &symbol_conf.raw_trace,
  1171. "Show raw trace event output (do not use print fmt or plugins)"),
  1172. OPT_BOOLEAN(0, "hierarchy", &symbol_conf.report_hierarchy,
  1173. "Show entries in a hierarchy"),
  1174. OPT_BOOLEAN(0, "force", &symbol_conf.force, "don't complain, do it"),
  1175. OPT_UINTEGER(0, "num-thread-synthesize", &top.nr_threads_synthesize,
  1176. "number of thread to run event synthesize"),
  1177. OPT_END()
  1178. };
  1179. const char * const top_usage[] = {
  1180. "perf top [<options>]",
  1181. NULL
  1182. };
  1183. int status = hists__init();
  1184. if (status < 0)
  1185. return status;
  1186. top.annotation_opts.min_pcnt = 5;
  1187. top.annotation_opts.context = 4;
  1188. top.evlist = perf_evlist__new();
  1189. if (top.evlist == NULL)
  1190. return -ENOMEM;
  1191. status = perf_config(perf_top_config, &top);
  1192. if (status)
  1193. return status;
  1194. argc = parse_options(argc, argv, options, top_usage, 0);
  1195. if (argc)
  1196. usage_with_options(top_usage, options);
  1197. if (!top.evlist->nr_entries &&
  1198. perf_evlist__add_default(top.evlist) < 0) {
  1199. pr_err("Not enough memory for event selector list\n");
  1200. goto out_delete_evlist;
  1201. }
  1202. if (symbol_conf.report_hierarchy) {
  1203. /* disable incompatible options */
  1204. symbol_conf.event_group = false;
  1205. symbol_conf.cumulate_callchain = false;
  1206. if (field_order) {
  1207. pr_err("Error: --hierarchy and --fields options cannot be used together\n");
  1208. parse_options_usage(top_usage, options, "fields", 0);
  1209. parse_options_usage(NULL, options, "hierarchy", 0);
  1210. goto out_delete_evlist;
  1211. }
  1212. }
  1213. sort__mode = SORT_MODE__TOP;
  1214. /* display thread wants entries to be collapsed in a different tree */
  1215. perf_hpp_list.need_collapse = 1;
  1216. if (top.use_stdio)
  1217. use_browser = 0;
  1218. else if (top.use_tui)
  1219. use_browser = 1;
  1220. setup_browser(false);
  1221. if (setup_sorting(top.evlist) < 0) {
  1222. if (sort_order)
  1223. parse_options_usage(top_usage, options, "s", 1);
  1224. if (field_order)
  1225. parse_options_usage(sort_order ? NULL : top_usage,
  1226. options, "fields", 0);
  1227. goto out_delete_evlist;
  1228. }
  1229. status = target__validate(target);
  1230. if (status) {
  1231. target__strerror(target, status, errbuf, BUFSIZ);
  1232. ui__warning("%s\n", errbuf);
  1233. }
  1234. status = target__parse_uid(target);
  1235. if (status) {
  1236. int saved_errno = errno;
  1237. target__strerror(target, status, errbuf, BUFSIZ);
  1238. ui__error("%s\n", errbuf);
  1239. status = -saved_errno;
  1240. goto out_delete_evlist;
  1241. }
  1242. if (target__none(target))
  1243. target->system_wide = true;
  1244. if (perf_evlist__create_maps(top.evlist, target) < 0) {
  1245. ui__error("Couldn't create thread/CPU maps: %s\n",
  1246. errno == ENOENT ? "No such process" : str_error_r(errno, errbuf, sizeof(errbuf)));
  1247. goto out_delete_evlist;
  1248. }
  1249. if (top.delay_secs < 1)
  1250. top.delay_secs = 1;
  1251. if (record_opts__config(opts)) {
  1252. status = -EINVAL;
  1253. goto out_delete_evlist;
  1254. }
  1255. top.sym_evsel = perf_evlist__first(top.evlist);
  1256. if (!callchain_param.enabled) {
  1257. symbol_conf.cumulate_callchain = false;
  1258. perf_hpp__cancel_cumulate();
  1259. }
  1260. if (symbol_conf.cumulate_callchain && !callchain_param.order_set)
  1261. callchain_param.order = ORDER_CALLER;
  1262. status = symbol__annotation_init();
  1263. if (status < 0)
  1264. goto out_delete_evlist;
  1265. annotation_config__init();
  1266. symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
  1267. status = symbol__init(NULL);
  1268. if (status < 0)
  1269. goto out_delete_evlist;
  1270. sort__setup_elide(stdout);
  1271. get_term_dimensions(&top.winsize);
  1272. if (top.print_entries == 0) {
  1273. perf_top__update_print_entries(&top);
  1274. signal(SIGWINCH, winch_sig);
  1275. }
  1276. status = __cmd_top(&top);
  1277. out_delete_evlist:
  1278. perf_evlist__delete(top.evlist);
  1279. return status;
  1280. }