builtin-top.c 41 KB

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