builtin-top.c 35 KB

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