builtin-top.c 36 KB

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