builtin-top.c 36 KB

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