builtin-top.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497
  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. *
  9. * Improvements and fixes by:
  10. *
  11. * Arjan van de Ven <arjan@linux.intel.com>
  12. * Yanmin Zhang <yanmin.zhang@intel.com>
  13. * Wu Fengguang <fengguang.wu@intel.com>
  14. * Mike Galbraith <efault@gmx.de>
  15. * Paul Mackerras <paulus@samba.org>
  16. *
  17. * Released under the GPL v2. (and only v2, not any later version)
  18. */
  19. #include "builtin.h"
  20. #include "perf.h"
  21. #include "util/color.h"
  22. #include "util/evsel.h"
  23. #include "util/session.h"
  24. #include "util/symbol.h"
  25. #include "util/thread.h"
  26. #include "util/util.h"
  27. #include <linux/rbtree.h>
  28. #include "util/parse-options.h"
  29. #include "util/parse-events.h"
  30. #include "util/cpumap.h"
  31. #include "util/xyarray.h"
  32. #include "util/debug.h"
  33. #include <assert.h>
  34. #include <fcntl.h>
  35. #include <stdio.h>
  36. #include <termios.h>
  37. #include <unistd.h>
  38. #include <inttypes.h>
  39. #include <errno.h>
  40. #include <time.h>
  41. #include <sched.h>
  42. #include <pthread.h>
  43. #include <sys/syscall.h>
  44. #include <sys/ioctl.h>
  45. #include <sys/poll.h>
  46. #include <sys/prctl.h>
  47. #include <sys/wait.h>
  48. #include <sys/uio.h>
  49. #include <sys/mman.h>
  50. #include <linux/unistd.h>
  51. #include <linux/types.h>
  52. #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
  53. static bool system_wide = false;
  54. static int default_interval = 0;
  55. static int count_filter = 5;
  56. static int print_entries;
  57. static int target_pid = -1;
  58. static int target_tid = -1;
  59. static struct thread_map *threads;
  60. static bool inherit = false;
  61. static struct cpu_map *cpus;
  62. static int realtime_prio = 0;
  63. static bool group = false;
  64. static unsigned int page_size;
  65. static unsigned int mmap_pages = 16;
  66. static int freq = 1000; /* 1 KHz */
  67. static int delay_secs = 2;
  68. static bool zero = false;
  69. static bool dump_symtab = false;
  70. static bool hide_kernel_symbols = false;
  71. static bool hide_user_symbols = false;
  72. static struct winsize winsize;
  73. /*
  74. * Source
  75. */
  76. struct source_line {
  77. u64 eip;
  78. unsigned long count[MAX_COUNTERS];
  79. char *line;
  80. struct source_line *next;
  81. };
  82. static const char *sym_filter = NULL;
  83. struct sym_entry *sym_filter_entry = NULL;
  84. struct sym_entry *sym_filter_entry_sched = NULL;
  85. static int sym_pcnt_filter = 5;
  86. static int sym_counter = 0;
  87. static struct perf_evsel *sym_evsel = NULL;
  88. static int display_weighted = -1;
  89. static const char *cpu_list;
  90. /*
  91. * Symbols
  92. */
  93. struct sym_entry_source {
  94. struct source_line *source;
  95. struct source_line *lines;
  96. struct source_line **lines_tail;
  97. pthread_mutex_t lock;
  98. };
  99. struct sym_entry {
  100. struct rb_node rb_node;
  101. struct list_head node;
  102. unsigned long snap_count;
  103. double weight;
  104. int skip;
  105. u16 name_len;
  106. u8 origin;
  107. struct map *map;
  108. struct sym_entry_source *src;
  109. unsigned long count[0];
  110. };
  111. /*
  112. * Source functions
  113. */
  114. static inline struct symbol *sym_entry__symbol(struct sym_entry *self)
  115. {
  116. return ((void *)self) + symbol_conf.priv_size;
  117. }
  118. void get_term_dimensions(struct winsize *ws)
  119. {
  120. char *s = getenv("LINES");
  121. if (s != NULL) {
  122. ws->ws_row = atoi(s);
  123. s = getenv("COLUMNS");
  124. if (s != NULL) {
  125. ws->ws_col = atoi(s);
  126. if (ws->ws_row && ws->ws_col)
  127. return;
  128. }
  129. }
  130. #ifdef TIOCGWINSZ
  131. if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
  132. ws->ws_row && ws->ws_col)
  133. return;
  134. #endif
  135. ws->ws_row = 25;
  136. ws->ws_col = 80;
  137. }
  138. static void update_print_entries(struct winsize *ws)
  139. {
  140. print_entries = ws->ws_row;
  141. if (print_entries > 9)
  142. print_entries -= 9;
  143. }
  144. static void sig_winch_handler(int sig __used)
  145. {
  146. get_term_dimensions(&winsize);
  147. update_print_entries(&winsize);
  148. }
  149. static int parse_source(struct sym_entry *syme)
  150. {
  151. struct symbol *sym;
  152. struct sym_entry_source *source;
  153. struct map *map;
  154. FILE *file;
  155. char command[PATH_MAX*2];
  156. const char *path;
  157. u64 len;
  158. if (!syme)
  159. return -1;
  160. sym = sym_entry__symbol(syme);
  161. map = syme->map;
  162. /*
  163. * We can't annotate with just /proc/kallsyms
  164. */
  165. if (map->dso->origin == DSO__ORIG_KERNEL)
  166. return -1;
  167. if (syme->src == NULL) {
  168. syme->src = zalloc(sizeof(*source));
  169. if (syme->src == NULL)
  170. return -1;
  171. pthread_mutex_init(&syme->src->lock, NULL);
  172. }
  173. source = syme->src;
  174. if (source->lines) {
  175. pthread_mutex_lock(&source->lock);
  176. goto out_assign;
  177. }
  178. path = map->dso->long_name;
  179. len = sym->end - sym->start;
  180. sprintf(command,
  181. "objdump --start-address=%#0*" PRIx64 " --stop-address=%#0*" PRIx64 " -dS %s",
  182. BITS_PER_LONG / 4, map__rip_2objdump(map, sym->start),
  183. BITS_PER_LONG / 4, map__rip_2objdump(map, sym->end), path);
  184. file = popen(command, "r");
  185. if (!file)
  186. return -1;
  187. pthread_mutex_lock(&source->lock);
  188. source->lines_tail = &source->lines;
  189. while (!feof(file)) {
  190. struct source_line *src;
  191. size_t dummy = 0;
  192. char *c, *sep;
  193. src = malloc(sizeof(struct source_line));
  194. assert(src != NULL);
  195. memset(src, 0, sizeof(struct source_line));
  196. if (getline(&src->line, &dummy, file) < 0)
  197. break;
  198. if (!src->line)
  199. break;
  200. c = strchr(src->line, '\n');
  201. if (c)
  202. *c = 0;
  203. src->next = NULL;
  204. *source->lines_tail = src;
  205. source->lines_tail = &src->next;
  206. src->eip = strtoull(src->line, &sep, 16);
  207. if (*sep == ':')
  208. src->eip = map__objdump_2ip(map, src->eip);
  209. else /* this line has no ip info (e.g. source line) */
  210. src->eip = 0;
  211. }
  212. pclose(file);
  213. out_assign:
  214. sym_filter_entry = syme;
  215. pthread_mutex_unlock(&source->lock);
  216. return 0;
  217. }
  218. static void __zero_source_counters(struct sym_entry *syme)
  219. {
  220. int i;
  221. struct source_line *line;
  222. line = syme->src->lines;
  223. while (line) {
  224. for (i = 0; i < nr_counters; i++)
  225. line->count[i] = 0;
  226. line = line->next;
  227. }
  228. }
  229. static void record_precise_ip(struct sym_entry *syme, int counter, u64 ip)
  230. {
  231. struct source_line *line;
  232. if (syme != sym_filter_entry)
  233. return;
  234. if (pthread_mutex_trylock(&syme->src->lock))
  235. return;
  236. if (syme->src == NULL || syme->src->source == NULL)
  237. goto out_unlock;
  238. for (line = syme->src->lines; line; line = line->next) {
  239. /* skip lines without IP info */
  240. if (line->eip == 0)
  241. continue;
  242. if (line->eip == ip) {
  243. line->count[counter]++;
  244. break;
  245. }
  246. if (line->eip > ip)
  247. break;
  248. }
  249. out_unlock:
  250. pthread_mutex_unlock(&syme->src->lock);
  251. }
  252. #define PATTERN_LEN (BITS_PER_LONG / 4 + 2)
  253. static void lookup_sym_source(struct sym_entry *syme)
  254. {
  255. struct symbol *symbol = sym_entry__symbol(syme);
  256. struct source_line *line;
  257. char pattern[PATTERN_LEN + 1];
  258. sprintf(pattern, "%0*" PRIx64 " <", BITS_PER_LONG / 4,
  259. map__rip_2objdump(syme->map, symbol->start));
  260. pthread_mutex_lock(&syme->src->lock);
  261. for (line = syme->src->lines; line; line = line->next) {
  262. if (memcmp(line->line, pattern, PATTERN_LEN) == 0) {
  263. syme->src->source = line;
  264. break;
  265. }
  266. }
  267. pthread_mutex_unlock(&syme->src->lock);
  268. }
  269. static void show_lines(struct source_line *queue, int count, int total)
  270. {
  271. int i;
  272. struct source_line *line;
  273. line = queue;
  274. for (i = 0; i < count; i++) {
  275. float pcnt = 100.0*(float)line->count[sym_counter]/(float)total;
  276. printf("%8li %4.1f%%\t%s\n", line->count[sym_counter], pcnt, line->line);
  277. line = line->next;
  278. }
  279. }
  280. #define TRACE_COUNT 3
  281. static void show_details(struct sym_entry *syme)
  282. {
  283. struct symbol *symbol;
  284. struct source_line *line;
  285. struct source_line *line_queue = NULL;
  286. int displayed = 0;
  287. int line_queue_count = 0, total = 0, more = 0;
  288. if (!syme)
  289. return;
  290. if (!syme->src->source)
  291. lookup_sym_source(syme);
  292. if (!syme->src->source)
  293. return;
  294. symbol = sym_entry__symbol(syme);
  295. printf("Showing %s for %s\n", event_name(sym_evsel), symbol->name);
  296. printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter);
  297. pthread_mutex_lock(&syme->src->lock);
  298. line = syme->src->source;
  299. while (line) {
  300. total += line->count[sym_counter];
  301. line = line->next;
  302. }
  303. line = syme->src->source;
  304. while (line) {
  305. float pcnt = 0.0;
  306. if (!line_queue_count)
  307. line_queue = line;
  308. line_queue_count++;
  309. if (line->count[sym_counter])
  310. pcnt = 100.0 * line->count[sym_counter] / (float)total;
  311. if (pcnt >= (float)sym_pcnt_filter) {
  312. if (displayed <= print_entries)
  313. show_lines(line_queue, line_queue_count, total);
  314. else more++;
  315. displayed += line_queue_count;
  316. line_queue_count = 0;
  317. line_queue = NULL;
  318. } else if (line_queue_count > TRACE_COUNT) {
  319. line_queue = line_queue->next;
  320. line_queue_count--;
  321. }
  322. line->count[sym_counter] = zero ? 0 : line->count[sym_counter] * 7 / 8;
  323. line = line->next;
  324. }
  325. pthread_mutex_unlock(&syme->src->lock);
  326. if (more)
  327. printf("%d lines not displayed, maybe increase display entries [e]\n", more);
  328. }
  329. /*
  330. * Symbols will be added here in event__process_sample and will get out
  331. * after decayed.
  332. */
  333. static LIST_HEAD(active_symbols);
  334. static pthread_mutex_t active_symbols_lock = PTHREAD_MUTEX_INITIALIZER;
  335. /*
  336. * Ordering weight: count-1 * count-2 * ... / count-n
  337. */
  338. static double sym_weight(const struct sym_entry *sym)
  339. {
  340. double weight = sym->snap_count;
  341. int counter;
  342. if (!display_weighted)
  343. return weight;
  344. for (counter = 1; counter < nr_counters-1; counter++)
  345. weight *= sym->count[counter];
  346. weight /= (sym->count[counter] + 1);
  347. return weight;
  348. }
  349. static long samples;
  350. static long kernel_samples, us_samples;
  351. static long exact_samples;
  352. static long guest_us_samples, guest_kernel_samples;
  353. static const char CONSOLE_CLEAR[] = "";
  354. static void __list_insert_active_sym(struct sym_entry *syme)
  355. {
  356. list_add(&syme->node, &active_symbols);
  357. }
  358. static void list_remove_active_sym(struct sym_entry *syme)
  359. {
  360. pthread_mutex_lock(&active_symbols_lock);
  361. list_del_init(&syme->node);
  362. pthread_mutex_unlock(&active_symbols_lock);
  363. }
  364. static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se)
  365. {
  366. struct rb_node **p = &tree->rb_node;
  367. struct rb_node *parent = NULL;
  368. struct sym_entry *iter;
  369. while (*p != NULL) {
  370. parent = *p;
  371. iter = rb_entry(parent, struct sym_entry, rb_node);
  372. if (se->weight > iter->weight)
  373. p = &(*p)->rb_left;
  374. else
  375. p = &(*p)->rb_right;
  376. }
  377. rb_link_node(&se->rb_node, parent, p);
  378. rb_insert_color(&se->rb_node, tree);
  379. }
  380. static void print_sym_table(void)
  381. {
  382. int printed = 0, j;
  383. struct perf_evsel *counter;
  384. int snap = !display_weighted ? sym_counter : 0;
  385. float samples_per_sec = samples/delay_secs;
  386. float ksamples_per_sec = kernel_samples/delay_secs;
  387. float us_samples_per_sec = (us_samples)/delay_secs;
  388. float guest_kernel_samples_per_sec = (guest_kernel_samples)/delay_secs;
  389. float guest_us_samples_per_sec = (guest_us_samples)/delay_secs;
  390. float esamples_percent = (100.0*exact_samples)/samples;
  391. float sum_ksamples = 0.0;
  392. struct sym_entry *syme, *n;
  393. struct rb_root tmp = RB_ROOT;
  394. struct rb_node *nd;
  395. int sym_width = 0, dso_width = 0, dso_short_width = 0;
  396. const int win_width = winsize.ws_col - 1;
  397. samples = us_samples = kernel_samples = exact_samples = 0;
  398. guest_kernel_samples = guest_us_samples = 0;
  399. /* Sort the active symbols */
  400. pthread_mutex_lock(&active_symbols_lock);
  401. syme = list_entry(active_symbols.next, struct sym_entry, node);
  402. pthread_mutex_unlock(&active_symbols_lock);
  403. list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
  404. syme->snap_count = syme->count[snap];
  405. if (syme->snap_count != 0) {
  406. if ((hide_user_symbols &&
  407. syme->origin == PERF_RECORD_MISC_USER) ||
  408. (hide_kernel_symbols &&
  409. syme->origin == PERF_RECORD_MISC_KERNEL)) {
  410. list_remove_active_sym(syme);
  411. continue;
  412. }
  413. syme->weight = sym_weight(syme);
  414. rb_insert_active_sym(&tmp, syme);
  415. sum_ksamples += syme->snap_count;
  416. for (j = 0; j < nr_counters; j++)
  417. syme->count[j] = zero ? 0 : syme->count[j] * 7 / 8;
  418. } else
  419. list_remove_active_sym(syme);
  420. }
  421. puts(CONSOLE_CLEAR);
  422. printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
  423. if (!perf_guest) {
  424. printf(" PerfTop:%8.0f irqs/sec kernel:%4.1f%%"
  425. " exact: %4.1f%% [",
  426. samples_per_sec,
  427. 100.0 - (100.0 * ((samples_per_sec - ksamples_per_sec) /
  428. samples_per_sec)),
  429. esamples_percent);
  430. } else {
  431. printf(" PerfTop:%8.0f irqs/sec kernel:%4.1f%% us:%4.1f%%"
  432. " guest kernel:%4.1f%% guest us:%4.1f%%"
  433. " exact: %4.1f%% [",
  434. samples_per_sec,
  435. 100.0 - (100.0 * ((samples_per_sec-ksamples_per_sec) /
  436. samples_per_sec)),
  437. 100.0 - (100.0 * ((samples_per_sec-us_samples_per_sec) /
  438. samples_per_sec)),
  439. 100.0 - (100.0 * ((samples_per_sec -
  440. guest_kernel_samples_per_sec) /
  441. samples_per_sec)),
  442. 100.0 - (100.0 * ((samples_per_sec -
  443. guest_us_samples_per_sec) /
  444. samples_per_sec)),
  445. esamples_percent);
  446. }
  447. if (nr_counters == 1 || !display_weighted) {
  448. struct perf_evsel *first;
  449. first = list_entry(evsel_list.next, struct perf_evsel, node);
  450. printf("%" PRIu64, (uint64_t)first->attr.sample_period);
  451. if (freq)
  452. printf("Hz ");
  453. else
  454. printf(" ");
  455. }
  456. if (!display_weighted)
  457. printf("%s", event_name(sym_evsel));
  458. else list_for_each_entry(counter, &evsel_list, node) {
  459. if (counter->idx)
  460. printf("/");
  461. printf("%s", event_name(counter));
  462. }
  463. printf( "], ");
  464. if (target_pid != -1)
  465. printf(" (target_pid: %d", target_pid);
  466. else if (target_tid != -1)
  467. printf(" (target_tid: %d", target_tid);
  468. else
  469. printf(" (all");
  470. if (cpu_list)
  471. printf(", CPU%s: %s)\n", cpus->nr > 1 ? "s" : "", cpu_list);
  472. else {
  473. if (target_tid != -1)
  474. printf(")\n");
  475. else
  476. printf(", %d CPU%s)\n", cpus->nr, cpus->nr > 1 ? "s" : "");
  477. }
  478. printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
  479. if (sym_filter_entry) {
  480. show_details(sym_filter_entry);
  481. return;
  482. }
  483. /*
  484. * Find the longest symbol name that will be displayed
  485. */
  486. for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
  487. syme = rb_entry(nd, struct sym_entry, rb_node);
  488. if (++printed > print_entries ||
  489. (int)syme->snap_count < count_filter)
  490. continue;
  491. if (syme->map->dso->long_name_len > dso_width)
  492. dso_width = syme->map->dso->long_name_len;
  493. if (syme->map->dso->short_name_len > dso_short_width)
  494. dso_short_width = syme->map->dso->short_name_len;
  495. if (syme->name_len > sym_width)
  496. sym_width = syme->name_len;
  497. }
  498. printed = 0;
  499. if (sym_width + dso_width > winsize.ws_col - 29) {
  500. dso_width = dso_short_width;
  501. if (sym_width + dso_width > winsize.ws_col - 29)
  502. sym_width = winsize.ws_col - dso_width - 29;
  503. }
  504. putchar('\n');
  505. if (nr_counters == 1)
  506. printf(" samples pcnt");
  507. else
  508. printf(" weight samples pcnt");
  509. if (verbose)
  510. printf(" RIP ");
  511. printf(" %-*.*s DSO\n", sym_width, sym_width, "function");
  512. printf(" %s _______ _____",
  513. nr_counters == 1 ? " " : "______");
  514. if (verbose)
  515. printf(" ________________");
  516. printf(" %-*.*s", sym_width, sym_width, graph_line);
  517. printf(" %-*.*s", dso_width, dso_width, graph_line);
  518. puts("\n");
  519. for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
  520. struct symbol *sym;
  521. double pcnt;
  522. syme = rb_entry(nd, struct sym_entry, rb_node);
  523. sym = sym_entry__symbol(syme);
  524. if (++printed > print_entries || (int)syme->snap_count < count_filter)
  525. continue;
  526. pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) /
  527. sum_ksamples));
  528. if (nr_counters == 1 || !display_weighted)
  529. printf("%20.2f ", syme->weight);
  530. else
  531. printf("%9.1f %10ld ", syme->weight, syme->snap_count);
  532. percent_color_fprintf(stdout, "%4.1f%%", pcnt);
  533. if (verbose)
  534. printf(" %016" PRIx64, sym->start);
  535. printf(" %-*.*s", sym_width, sym_width, sym->name);
  536. printf(" %-*.*s\n", dso_width, dso_width,
  537. dso_width >= syme->map->dso->long_name_len ?
  538. syme->map->dso->long_name :
  539. syme->map->dso->short_name);
  540. }
  541. }
  542. static void prompt_integer(int *target, const char *msg)
  543. {
  544. char *buf = malloc(0), *p;
  545. size_t dummy = 0;
  546. int tmp;
  547. fprintf(stdout, "\n%s: ", msg);
  548. if (getline(&buf, &dummy, stdin) < 0)
  549. return;
  550. p = strchr(buf, '\n');
  551. if (p)
  552. *p = 0;
  553. p = buf;
  554. while(*p) {
  555. if (!isdigit(*p))
  556. goto out_free;
  557. p++;
  558. }
  559. tmp = strtoul(buf, NULL, 10);
  560. *target = tmp;
  561. out_free:
  562. free(buf);
  563. }
  564. static void prompt_percent(int *target, const char *msg)
  565. {
  566. int tmp = 0;
  567. prompt_integer(&tmp, msg);
  568. if (tmp >= 0 && tmp <= 100)
  569. *target = tmp;
  570. }
  571. static void prompt_symbol(struct sym_entry **target, const char *msg)
  572. {
  573. char *buf = malloc(0), *p;
  574. struct sym_entry *syme = *target, *n, *found = NULL;
  575. size_t dummy = 0;
  576. /* zero counters of active symbol */
  577. if (syme) {
  578. pthread_mutex_lock(&syme->src->lock);
  579. __zero_source_counters(syme);
  580. *target = NULL;
  581. pthread_mutex_unlock(&syme->src->lock);
  582. }
  583. fprintf(stdout, "\n%s: ", msg);
  584. if (getline(&buf, &dummy, stdin) < 0)
  585. goto out_free;
  586. p = strchr(buf, '\n');
  587. if (p)
  588. *p = 0;
  589. pthread_mutex_lock(&active_symbols_lock);
  590. syme = list_entry(active_symbols.next, struct sym_entry, node);
  591. pthread_mutex_unlock(&active_symbols_lock);
  592. list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
  593. struct symbol *sym = sym_entry__symbol(syme);
  594. if (!strcmp(buf, sym->name)) {
  595. found = syme;
  596. break;
  597. }
  598. }
  599. if (!found) {
  600. fprintf(stderr, "Sorry, %s is not active.\n", buf);
  601. sleep(1);
  602. return;
  603. } else
  604. parse_source(found);
  605. out_free:
  606. free(buf);
  607. }
  608. static void print_mapped_keys(void)
  609. {
  610. char *name = NULL;
  611. if (sym_filter_entry) {
  612. struct symbol *sym = sym_entry__symbol(sym_filter_entry);
  613. name = sym->name;
  614. }
  615. fprintf(stdout, "\nMapped keys:\n");
  616. fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", delay_secs);
  617. fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", print_entries);
  618. if (nr_counters > 1)
  619. fprintf(stdout, "\t[E] active event counter. \t(%s)\n", event_name(sym_evsel));
  620. fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", count_filter);
  621. fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", sym_pcnt_filter);
  622. fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL");
  623. fprintf(stdout, "\t[S] stop annotation.\n");
  624. if (nr_counters > 1)
  625. fprintf(stdout, "\t[w] toggle display weighted/count[E]r. \t(%d)\n", display_weighted ? 1 : 0);
  626. fprintf(stdout,
  627. "\t[K] hide kernel_symbols symbols. \t(%s)\n",
  628. hide_kernel_symbols ? "yes" : "no");
  629. fprintf(stdout,
  630. "\t[U] hide user symbols. \t(%s)\n",
  631. hide_user_symbols ? "yes" : "no");
  632. fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", zero ? 1 : 0);
  633. fprintf(stdout, "\t[qQ] quit.\n");
  634. }
  635. static int key_mapped(int c)
  636. {
  637. switch (c) {
  638. case 'd':
  639. case 'e':
  640. case 'f':
  641. case 'z':
  642. case 'q':
  643. case 'Q':
  644. case 'K':
  645. case 'U':
  646. case 'F':
  647. case 's':
  648. case 'S':
  649. return 1;
  650. case 'E':
  651. case 'w':
  652. return nr_counters > 1 ? 1 : 0;
  653. default:
  654. break;
  655. }
  656. return 0;
  657. }
  658. static void handle_keypress(struct perf_session *session, int c)
  659. {
  660. if (!key_mapped(c)) {
  661. struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
  662. struct termios tc, save;
  663. print_mapped_keys();
  664. fprintf(stdout, "\nEnter selection, or unmapped key to continue: ");
  665. fflush(stdout);
  666. tcgetattr(0, &save);
  667. tc = save;
  668. tc.c_lflag &= ~(ICANON | ECHO);
  669. tc.c_cc[VMIN] = 0;
  670. tc.c_cc[VTIME] = 0;
  671. tcsetattr(0, TCSANOW, &tc);
  672. poll(&stdin_poll, 1, -1);
  673. c = getc(stdin);
  674. tcsetattr(0, TCSAFLUSH, &save);
  675. if (!key_mapped(c))
  676. return;
  677. }
  678. switch (c) {
  679. case 'd':
  680. prompt_integer(&delay_secs, "Enter display delay");
  681. if (delay_secs < 1)
  682. delay_secs = 1;
  683. break;
  684. case 'e':
  685. prompt_integer(&print_entries, "Enter display entries (lines)");
  686. if (print_entries == 0) {
  687. sig_winch_handler(SIGWINCH);
  688. signal(SIGWINCH, sig_winch_handler);
  689. } else
  690. signal(SIGWINCH, SIG_DFL);
  691. break;
  692. case 'E':
  693. if (nr_counters > 1) {
  694. fprintf(stderr, "\nAvailable events:");
  695. list_for_each_entry(sym_evsel, &evsel_list, node)
  696. fprintf(stderr, "\n\t%d %s", sym_evsel->idx, event_name(sym_evsel));
  697. prompt_integer(&sym_counter, "Enter details event counter");
  698. if (sym_counter >= nr_counters) {
  699. sym_evsel = list_entry(evsel_list.next, struct perf_evsel, node);
  700. sym_counter = 0;
  701. fprintf(stderr, "Sorry, no such event, using %s.\n", event_name(sym_evsel));
  702. sleep(1);
  703. break;
  704. }
  705. list_for_each_entry(sym_evsel, &evsel_list, node)
  706. if (sym_evsel->idx == sym_counter)
  707. break;
  708. } else sym_counter = 0;
  709. break;
  710. case 'f':
  711. prompt_integer(&count_filter, "Enter display event count filter");
  712. break;
  713. case 'F':
  714. prompt_percent(&sym_pcnt_filter, "Enter details display event filter (percent)");
  715. break;
  716. case 'K':
  717. hide_kernel_symbols = !hide_kernel_symbols;
  718. break;
  719. case 'q':
  720. case 'Q':
  721. printf("exiting.\n");
  722. if (dump_symtab)
  723. perf_session__fprintf_dsos(session, stderr);
  724. exit(0);
  725. case 's':
  726. prompt_symbol(&sym_filter_entry, "Enter details symbol");
  727. break;
  728. case 'S':
  729. if (!sym_filter_entry)
  730. break;
  731. else {
  732. struct sym_entry *syme = sym_filter_entry;
  733. pthread_mutex_lock(&syme->src->lock);
  734. sym_filter_entry = NULL;
  735. __zero_source_counters(syme);
  736. pthread_mutex_unlock(&syme->src->lock);
  737. }
  738. break;
  739. case 'U':
  740. hide_user_symbols = !hide_user_symbols;
  741. break;
  742. case 'w':
  743. display_weighted = ~display_weighted;
  744. break;
  745. case 'z':
  746. zero = !zero;
  747. break;
  748. default:
  749. break;
  750. }
  751. }
  752. static void *display_thread(void *arg __used)
  753. {
  754. struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
  755. struct termios tc, save;
  756. int delay_msecs, c;
  757. struct perf_session *session = (struct perf_session *) arg;
  758. tcgetattr(0, &save);
  759. tc = save;
  760. tc.c_lflag &= ~(ICANON | ECHO);
  761. tc.c_cc[VMIN] = 0;
  762. tc.c_cc[VTIME] = 0;
  763. repeat:
  764. delay_msecs = delay_secs * 1000;
  765. tcsetattr(0, TCSANOW, &tc);
  766. /* trash return*/
  767. getc(stdin);
  768. do {
  769. print_sym_table();
  770. } while (!poll(&stdin_poll, 1, delay_msecs) == 1);
  771. c = getc(stdin);
  772. tcsetattr(0, TCSAFLUSH, &save);
  773. handle_keypress(session, c);
  774. goto repeat;
  775. return NULL;
  776. }
  777. /* Tag samples to be skipped. */
  778. static const char *skip_symbols[] = {
  779. "default_idle",
  780. "cpu_idle",
  781. "enter_idle",
  782. "exit_idle",
  783. "mwait_idle",
  784. "mwait_idle_with_hints",
  785. "poll_idle",
  786. "ppc64_runlatch_off",
  787. "pseries_dedicated_idle_sleep",
  788. NULL
  789. };
  790. static int symbol_filter(struct map *map, struct symbol *sym)
  791. {
  792. struct sym_entry *syme;
  793. const char *name = sym->name;
  794. int i;
  795. /*
  796. * ppc64 uses function descriptors and appends a '.' to the
  797. * start of every instruction address. Remove it.
  798. */
  799. if (name[0] == '.')
  800. name++;
  801. if (!strcmp(name, "_text") ||
  802. !strcmp(name, "_etext") ||
  803. !strcmp(name, "_sinittext") ||
  804. !strncmp("init_module", name, 11) ||
  805. !strncmp("cleanup_module", name, 14) ||
  806. strstr(name, "_text_start") ||
  807. strstr(name, "_text_end"))
  808. return 1;
  809. syme = symbol__priv(sym);
  810. syme->map = map;
  811. syme->src = NULL;
  812. if (!sym_filter_entry && sym_filter && !strcmp(name, sym_filter)) {
  813. /* schedule initial sym_filter_entry setup */
  814. sym_filter_entry_sched = syme;
  815. sym_filter = NULL;
  816. }
  817. for (i = 0; skip_symbols[i]; i++) {
  818. if (!strcmp(skip_symbols[i], name)) {
  819. syme->skip = 1;
  820. break;
  821. }
  822. }
  823. if (!syme->skip)
  824. syme->name_len = strlen(sym->name);
  825. return 0;
  826. }
  827. static void event__process_sample(const event_t *self,
  828. struct sample_data *sample,
  829. struct perf_session *session,
  830. struct perf_evsel *evsel)
  831. {
  832. u64 ip = self->ip.ip;
  833. struct sym_entry *syme;
  834. struct addr_location al;
  835. struct machine *machine;
  836. u8 origin = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  837. ++samples;
  838. switch (origin) {
  839. case PERF_RECORD_MISC_USER:
  840. ++us_samples;
  841. if (hide_user_symbols)
  842. return;
  843. machine = perf_session__find_host_machine(session);
  844. break;
  845. case PERF_RECORD_MISC_KERNEL:
  846. ++kernel_samples;
  847. if (hide_kernel_symbols)
  848. return;
  849. machine = perf_session__find_host_machine(session);
  850. break;
  851. case PERF_RECORD_MISC_GUEST_KERNEL:
  852. ++guest_kernel_samples;
  853. machine = perf_session__find_machine(session, self->ip.pid);
  854. break;
  855. case PERF_RECORD_MISC_GUEST_USER:
  856. ++guest_us_samples;
  857. /*
  858. * TODO: we don't process guest user from host side
  859. * except simple counting.
  860. */
  861. return;
  862. default:
  863. return;
  864. }
  865. if (!machine && perf_guest) {
  866. pr_err("Can't find guest [%d]'s kernel information\n",
  867. self->ip.pid);
  868. return;
  869. }
  870. if (self->header.misc & PERF_RECORD_MISC_EXACT_IP)
  871. exact_samples++;
  872. if (event__preprocess_sample(self, session, &al, sample,
  873. symbol_filter) < 0 ||
  874. al.filtered)
  875. return;
  876. if (al.sym == NULL) {
  877. /*
  878. * As we do lazy loading of symtabs we only will know if the
  879. * specified vmlinux file is invalid when we actually have a
  880. * hit in kernel space and then try to load it. So if we get
  881. * here and there are _no_ symbols in the DSO backing the
  882. * kernel map, bail out.
  883. *
  884. * We may never get here, for instance, if we use -K/
  885. * --hide-kernel-symbols, even if the user specifies an
  886. * invalid --vmlinux ;-)
  887. */
  888. if (al.map == machine->vmlinux_maps[MAP__FUNCTION] &&
  889. RB_EMPTY_ROOT(&al.map->dso->symbols[MAP__FUNCTION])) {
  890. pr_err("The %s file can't be used\n",
  891. symbol_conf.vmlinux_name);
  892. exit(1);
  893. }
  894. return;
  895. }
  896. /* let's see, whether we need to install initial sym_filter_entry */
  897. if (sym_filter_entry_sched) {
  898. sym_filter_entry = sym_filter_entry_sched;
  899. sym_filter_entry_sched = NULL;
  900. if (parse_source(sym_filter_entry) < 0) {
  901. struct symbol *sym = sym_entry__symbol(sym_filter_entry);
  902. pr_err("Can't annotate %s", sym->name);
  903. if (sym_filter_entry->map->dso->origin == DSO__ORIG_KERNEL) {
  904. pr_err(": No vmlinux file was found in the path:\n");
  905. machine__fprintf_vmlinux_path(machine, stderr);
  906. } else
  907. pr_err(".\n");
  908. exit(1);
  909. }
  910. }
  911. syme = symbol__priv(al.sym);
  912. if (!syme->skip) {
  913. syme->count[evsel->idx]++;
  914. syme->origin = origin;
  915. record_precise_ip(syme, evsel->idx, ip);
  916. pthread_mutex_lock(&active_symbols_lock);
  917. if (list_empty(&syme->node) || !syme->node.next)
  918. __list_insert_active_sym(syme);
  919. pthread_mutex_unlock(&active_symbols_lock);
  920. }
  921. }
  922. struct mmap_data {
  923. void *base;
  924. int mask;
  925. unsigned int prev;
  926. };
  927. static int perf_evsel__alloc_mmap_per_thread(struct perf_evsel *evsel,
  928. int ncpus, int nthreads)
  929. {
  930. evsel->priv = xyarray__new(ncpus, nthreads, sizeof(struct mmap_data));
  931. return evsel->priv != NULL ? 0 : -ENOMEM;
  932. }
  933. static void perf_evsel__free_mmap(struct perf_evsel *evsel)
  934. {
  935. xyarray__delete(evsel->priv);
  936. evsel->priv = NULL;
  937. }
  938. static unsigned int mmap_read_head(struct mmap_data *md)
  939. {
  940. struct perf_event_mmap_page *pc = md->base;
  941. int head;
  942. head = pc->data_head;
  943. rmb();
  944. return head;
  945. }
  946. static void perf_session__mmap_read_counter(struct perf_session *self,
  947. struct perf_evsel *evsel,
  948. int cpu, int thread_idx)
  949. {
  950. struct xyarray *mmap_array = evsel->priv;
  951. struct mmap_data *md = xyarray__entry(mmap_array, cpu, thread_idx);
  952. unsigned int head = mmap_read_head(md);
  953. unsigned int old = md->prev;
  954. unsigned char *data = md->base + page_size;
  955. struct sample_data sample;
  956. int diff;
  957. /*
  958. * If we're further behind than half the buffer, there's a chance
  959. * the writer will bite our tail and mess up the samples under us.
  960. *
  961. * If we somehow ended up ahead of the head, we got messed up.
  962. *
  963. * In either case, truncate and restart at head.
  964. */
  965. diff = head - old;
  966. if (diff > md->mask / 2 || diff < 0) {
  967. fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
  968. /*
  969. * head points to a known good entry, start there.
  970. */
  971. old = head;
  972. }
  973. for (; old != head;) {
  974. event_t *event = (event_t *)&data[old & md->mask];
  975. event_t event_copy;
  976. size_t size = event->header.size;
  977. /*
  978. * Event straddles the mmap boundary -- header should always
  979. * be inside due to u64 alignment of output.
  980. */
  981. if ((old & md->mask) + size != ((old + size) & md->mask)) {
  982. unsigned int offset = old;
  983. unsigned int len = min(sizeof(*event), size), cpy;
  984. void *dst = &event_copy;
  985. do {
  986. cpy = min(md->mask + 1 - (offset & md->mask), len);
  987. memcpy(dst, &data[offset & md->mask], cpy);
  988. offset += cpy;
  989. dst += cpy;
  990. len -= cpy;
  991. } while (len);
  992. event = &event_copy;
  993. }
  994. event__parse_sample(event, self, &sample);
  995. if (event->header.type == PERF_RECORD_SAMPLE)
  996. event__process_sample(event, &sample, self, evsel);
  997. else
  998. event__process(event, &sample, self);
  999. old += size;
  1000. }
  1001. md->prev = old;
  1002. }
  1003. static struct pollfd *event_array;
  1004. static void perf_session__mmap_read(struct perf_session *self)
  1005. {
  1006. struct perf_evsel *counter;
  1007. int i, thread_index;
  1008. for (i = 0; i < cpus->nr; i++) {
  1009. list_for_each_entry(counter, &evsel_list, node) {
  1010. for (thread_index = 0;
  1011. thread_index < threads->nr;
  1012. thread_index++) {
  1013. perf_session__mmap_read_counter(self,
  1014. counter, i, thread_index);
  1015. }
  1016. }
  1017. }
  1018. }
  1019. int nr_poll;
  1020. int group_fd;
  1021. static void start_counter(int i, struct perf_evsel *evsel)
  1022. {
  1023. struct xyarray *mmap_array = evsel->priv;
  1024. struct mmap_data *mm;
  1025. struct perf_event_attr *attr;
  1026. int cpu = -1;
  1027. int thread_index;
  1028. if (target_tid == -1)
  1029. cpu = cpus->map[i];
  1030. attr = &evsel->attr;
  1031. attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
  1032. if (freq) {
  1033. attr->sample_type |= PERF_SAMPLE_PERIOD;
  1034. attr->freq = 1;
  1035. attr->sample_freq = freq;
  1036. }
  1037. attr->inherit = (cpu < 0) && inherit;
  1038. attr->mmap = 1;
  1039. for (thread_index = 0; thread_index < threads->nr; thread_index++) {
  1040. try_again:
  1041. FD(evsel, i, thread_index) = sys_perf_event_open(attr,
  1042. threads->map[thread_index], cpu, group_fd, 0);
  1043. if (FD(evsel, i, thread_index) < 0) {
  1044. int err = errno;
  1045. if (err == EPERM || err == EACCES)
  1046. die("Permission error - are you root?\n"
  1047. "\t Consider tweaking"
  1048. " /proc/sys/kernel/perf_event_paranoid.\n");
  1049. /*
  1050. * If it's cycles then fall back to hrtimer
  1051. * based cpu-clock-tick sw counter, which
  1052. * is always available even if no PMU support:
  1053. */
  1054. if (attr->type == PERF_TYPE_HARDWARE
  1055. && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
  1056. if (verbose)
  1057. warning(" ... trying to fall back to cpu-clock-ticks\n");
  1058. attr->type = PERF_TYPE_SOFTWARE;
  1059. attr->config = PERF_COUNT_SW_CPU_CLOCK;
  1060. goto try_again;
  1061. }
  1062. printf("\n");
  1063. error("sys_perf_event_open() syscall returned with %d (%s). /bin/dmesg may provide additional information.\n",
  1064. FD(evsel, i, thread_index), strerror(err));
  1065. die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
  1066. exit(-1);
  1067. }
  1068. assert(FD(evsel, i, thread_index) >= 0);
  1069. fcntl(FD(evsel, i, thread_index), F_SETFL, O_NONBLOCK);
  1070. /*
  1071. * First counter acts as the group leader:
  1072. */
  1073. if (group && group_fd == -1)
  1074. group_fd = FD(evsel, i, thread_index);
  1075. event_array[nr_poll].fd = FD(evsel, i, thread_index);
  1076. event_array[nr_poll].events = POLLIN;
  1077. nr_poll++;
  1078. mm = xyarray__entry(mmap_array, i, thread_index);
  1079. mm->prev = 0;
  1080. mm->mask = mmap_pages*page_size - 1;
  1081. mm->base = mmap(NULL, (mmap_pages+1)*page_size,
  1082. PROT_READ, MAP_SHARED, FD(evsel, i, thread_index), 0);
  1083. if (mm->base == MAP_FAILED)
  1084. die("failed to mmap with %d (%s)\n", errno, strerror(errno));
  1085. }
  1086. }
  1087. static int __cmd_top(void)
  1088. {
  1089. pthread_t thread;
  1090. struct perf_evsel *counter;
  1091. int i, ret;
  1092. /*
  1093. * FIXME: perf_session__new should allow passing a O_MMAP, so that all this
  1094. * mmap reading, etc is encapsulated in it. Use O_WRONLY for now.
  1095. */
  1096. struct perf_session *session = perf_session__new(NULL, O_WRONLY, false, false, NULL);
  1097. if (session == NULL)
  1098. return -ENOMEM;
  1099. if (target_tid != -1)
  1100. event__synthesize_thread_map(threads, event__process, session);
  1101. else
  1102. event__synthesize_threads(event__process, session);
  1103. for (i = 0; i < cpus->nr; i++) {
  1104. group_fd = -1;
  1105. list_for_each_entry(counter, &evsel_list, node)
  1106. start_counter(i, counter);
  1107. }
  1108. /* Wait for a minimal set of events before starting the snapshot */
  1109. poll(&event_array[0], nr_poll, 100);
  1110. perf_session__mmap_read(session);
  1111. if (pthread_create(&thread, NULL, display_thread, session)) {
  1112. printf("Could not create display thread.\n");
  1113. exit(-1);
  1114. }
  1115. if (realtime_prio) {
  1116. struct sched_param param;
  1117. param.sched_priority = realtime_prio;
  1118. if (sched_setscheduler(0, SCHED_FIFO, &param)) {
  1119. printf("Could not set realtime priority.\n");
  1120. exit(-1);
  1121. }
  1122. }
  1123. while (1) {
  1124. int hits = samples;
  1125. perf_session__mmap_read(session);
  1126. if (hits == samples)
  1127. ret = poll(event_array, nr_poll, 100);
  1128. }
  1129. return 0;
  1130. }
  1131. static const char * const top_usage[] = {
  1132. "perf top [<options>]",
  1133. NULL
  1134. };
  1135. static const struct option options[] = {
  1136. OPT_CALLBACK('e', "event", NULL, "event",
  1137. "event selector. use 'perf list' to list available events",
  1138. parse_events),
  1139. OPT_INTEGER('c', "count", &default_interval,
  1140. "event period to sample"),
  1141. OPT_INTEGER('p', "pid", &target_pid,
  1142. "profile events on existing process id"),
  1143. OPT_INTEGER('t', "tid", &target_tid,
  1144. "profile events on existing thread id"),
  1145. OPT_BOOLEAN('a', "all-cpus", &system_wide,
  1146. "system-wide collection from all CPUs"),
  1147. OPT_STRING('C', "cpu", &cpu_list, "cpu",
  1148. "list of cpus to monitor"),
  1149. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  1150. "file", "vmlinux pathname"),
  1151. OPT_BOOLEAN('K', "hide_kernel_symbols", &hide_kernel_symbols,
  1152. "hide kernel symbols"),
  1153. OPT_UINTEGER('m', "mmap-pages", &mmap_pages, "number of mmap data pages"),
  1154. OPT_INTEGER('r', "realtime", &realtime_prio,
  1155. "collect data with this RT SCHED_FIFO priority"),
  1156. OPT_INTEGER('d', "delay", &delay_secs,
  1157. "number of seconds to delay between refreshes"),
  1158. OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
  1159. "dump the symbol table used for profiling"),
  1160. OPT_INTEGER('f', "count-filter", &count_filter,
  1161. "only display functions with more events than this"),
  1162. OPT_BOOLEAN('g', "group", &group,
  1163. "put the counters into a counter group"),
  1164. OPT_BOOLEAN('i', "inherit", &inherit,
  1165. "child tasks inherit counters"),
  1166. OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name",
  1167. "symbol to annotate"),
  1168. OPT_BOOLEAN('z', "zero", &zero,
  1169. "zero history across updates"),
  1170. OPT_INTEGER('F', "freq", &freq,
  1171. "profile at this frequency"),
  1172. OPT_INTEGER('E', "entries", &print_entries,
  1173. "display this many functions"),
  1174. OPT_BOOLEAN('U', "hide_user_symbols", &hide_user_symbols,
  1175. "hide user symbols"),
  1176. OPT_INCR('v', "verbose", &verbose,
  1177. "be more verbose (show counter open errors, etc)"),
  1178. OPT_END()
  1179. };
  1180. int cmd_top(int argc, const char **argv, const char *prefix __used)
  1181. {
  1182. struct perf_evsel *pos;
  1183. int status = -ENOMEM;
  1184. page_size = sysconf(_SC_PAGE_SIZE);
  1185. argc = parse_options(argc, argv, options, top_usage, 0);
  1186. if (argc)
  1187. usage_with_options(top_usage, options);
  1188. if (target_pid != -1)
  1189. target_tid = target_pid;
  1190. threads = thread_map__new(target_pid, target_tid);
  1191. if (threads == NULL) {
  1192. pr_err("Problems finding threads of monitor\n");
  1193. usage_with_options(top_usage, options);
  1194. }
  1195. event_array = malloc((sizeof(struct pollfd) *
  1196. MAX_NR_CPUS * MAX_COUNTERS * threads->nr));
  1197. if (!event_array)
  1198. return -ENOMEM;
  1199. /* CPU and PID are mutually exclusive */
  1200. if (target_tid > 0 && cpu_list) {
  1201. printf("WARNING: PID switch overriding CPU\n");
  1202. sleep(1);
  1203. cpu_list = NULL;
  1204. }
  1205. if (!nr_counters && perf_evsel_list__create_default() < 0) {
  1206. pr_err("Not enough memory for event selector list\n");
  1207. return -ENOMEM;
  1208. }
  1209. if (delay_secs < 1)
  1210. delay_secs = 1;
  1211. /*
  1212. * User specified count overrides default frequency.
  1213. */
  1214. if (default_interval)
  1215. freq = 0;
  1216. else if (freq) {
  1217. default_interval = freq;
  1218. } else {
  1219. fprintf(stderr, "frequency and count are zero, aborting\n");
  1220. exit(EXIT_FAILURE);
  1221. }
  1222. if (target_tid != -1)
  1223. cpus = cpu_map__dummy_new();
  1224. else
  1225. cpus = cpu_map__new(cpu_list);
  1226. if (cpus == NULL)
  1227. usage_with_options(top_usage, options);
  1228. list_for_each_entry(pos, &evsel_list, node) {
  1229. if (perf_evsel__alloc_mmap_per_thread(pos, cpus->nr, threads->nr) < 0 ||
  1230. perf_evsel__alloc_fd(pos, cpus->nr, threads->nr) < 0)
  1231. goto out_free_fd;
  1232. /*
  1233. * Fill in the ones not specifically initialized via -c:
  1234. */
  1235. if (pos->attr.sample_period)
  1236. continue;
  1237. pos->attr.sample_period = default_interval;
  1238. }
  1239. sym_evsel = list_entry(evsel_list.next, struct perf_evsel, node);
  1240. symbol_conf.priv_size = (sizeof(struct sym_entry) +
  1241. (nr_counters + 1) * sizeof(unsigned long));
  1242. symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
  1243. if (symbol__init() < 0)
  1244. return -1;
  1245. get_term_dimensions(&winsize);
  1246. if (print_entries == 0) {
  1247. update_print_entries(&winsize);
  1248. signal(SIGWINCH, sig_winch_handler);
  1249. }
  1250. status = __cmd_top();
  1251. out_free_fd:
  1252. list_for_each_entry(pos, &evsel_list, node)
  1253. perf_evsel__free_mmap(pos);
  1254. perf_evsel_list__delete();
  1255. return status;
  1256. }