builtin-top.c 35 KB

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