builtin-top.c 34 KB

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