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 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("Permission error - are you root?\n"
  1018. "\t Consider tweaking"
  1019. " /proc/sys/kernel/perf_event_paranoid.\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("sys_perf_event_open() syscall returned with %d (%s). /bin/dmesg may provide additional information.\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. }