builtin-top.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490
  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. static int event__process(event_t *event, struct perf_session *session)
  913. {
  914. switch (event->header.type) {
  915. case PERF_RECORD_COMM:
  916. event__process_comm(event, session);
  917. break;
  918. case PERF_RECORD_MMAP:
  919. event__process_mmap(event, session);
  920. break;
  921. case PERF_RECORD_FORK:
  922. case PERF_RECORD_EXIT:
  923. event__process_task(event, session);
  924. break;
  925. default:
  926. break;
  927. }
  928. return 0;
  929. }
  930. struct mmap_data {
  931. int counter;
  932. void *base;
  933. int mask;
  934. unsigned int prev;
  935. };
  936. static unsigned int mmap_read_head(struct mmap_data *md)
  937. {
  938. struct perf_event_mmap_page *pc = md->base;
  939. int head;
  940. head = pc->data_head;
  941. rmb();
  942. return head;
  943. }
  944. static void perf_session__mmap_read_counter(struct perf_session *self,
  945. struct mmap_data *md)
  946. {
  947. unsigned int head = mmap_read_head(md);
  948. unsigned int old = md->prev;
  949. unsigned char *data = md->base + page_size;
  950. int diff;
  951. /*
  952. * If we're further behind than half the buffer, there's a chance
  953. * the writer will bite our tail and mess up the samples under us.
  954. *
  955. * If we somehow ended up ahead of the head, we got messed up.
  956. *
  957. * In either case, truncate and restart at head.
  958. */
  959. diff = head - old;
  960. if (diff > md->mask / 2 || diff < 0) {
  961. fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
  962. /*
  963. * head points to a known good entry, start there.
  964. */
  965. old = head;
  966. }
  967. for (; old != head;) {
  968. event_t *event = (event_t *)&data[old & md->mask];
  969. event_t event_copy;
  970. size_t size = event->header.size;
  971. /*
  972. * Event straddles the mmap boundary -- header should always
  973. * be inside due to u64 alignment of output.
  974. */
  975. if ((old & md->mask) + size != ((old + size) & md->mask)) {
  976. unsigned int offset = old;
  977. unsigned int len = min(sizeof(*event), size), cpy;
  978. void *dst = &event_copy;
  979. do {
  980. cpy = min(md->mask + 1 - (offset & md->mask), len);
  981. memcpy(dst, &data[offset & md->mask], cpy);
  982. offset += cpy;
  983. dst += cpy;
  984. len -= cpy;
  985. } while (len);
  986. event = &event_copy;
  987. }
  988. if (event->header.type == PERF_RECORD_SAMPLE)
  989. event__process_sample(event, self, md->counter);
  990. else
  991. event__process(event, self);
  992. old += size;
  993. }
  994. md->prev = old;
  995. }
  996. static struct pollfd *event_array;
  997. static struct mmap_data *mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
  998. static void perf_session__mmap_read(struct perf_session *self)
  999. {
  1000. int i, counter, thread_index;
  1001. for (i = 0; i < nr_cpus; i++) {
  1002. for (counter = 0; counter < nr_counters; counter++)
  1003. for (thread_index = 0;
  1004. thread_index < thread_num;
  1005. thread_index++) {
  1006. perf_session__mmap_read_counter(self,
  1007. &mmap_array[i][counter][thread_index]);
  1008. }
  1009. }
  1010. }
  1011. int nr_poll;
  1012. int group_fd;
  1013. static void start_counter(int i, int counter)
  1014. {
  1015. struct perf_event_attr *attr;
  1016. int cpu;
  1017. int thread_index;
  1018. cpu = profile_cpu;
  1019. if (target_tid == -1 && profile_cpu == -1)
  1020. cpu = cpumap[i];
  1021. attr = attrs + counter;
  1022. attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
  1023. if (freq) {
  1024. attr->sample_type |= PERF_SAMPLE_PERIOD;
  1025. attr->freq = 1;
  1026. attr->sample_freq = freq;
  1027. }
  1028. attr->inherit = (cpu < 0) && inherit;
  1029. attr->mmap = 1;
  1030. for (thread_index = 0; thread_index < thread_num; thread_index++) {
  1031. try_again:
  1032. fd[i][counter][thread_index] = sys_perf_event_open(attr,
  1033. all_tids[thread_index], cpu, group_fd, 0);
  1034. if (fd[i][counter][thread_index] < 0) {
  1035. int err = errno;
  1036. if (err == EPERM || err == EACCES)
  1037. die("No permission - are you root?\n");
  1038. /*
  1039. * If it's cycles then fall back to hrtimer
  1040. * based cpu-clock-tick sw counter, which
  1041. * is always available even if no PMU support:
  1042. */
  1043. if (attr->type == PERF_TYPE_HARDWARE
  1044. && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
  1045. if (verbose)
  1046. warning(" ... trying to fall back to cpu-clock-ticks\n");
  1047. attr->type = PERF_TYPE_SOFTWARE;
  1048. attr->config = PERF_COUNT_SW_CPU_CLOCK;
  1049. goto try_again;
  1050. }
  1051. printf("\n");
  1052. error("perfcounter syscall returned with %d (%s)\n",
  1053. fd[i][counter][thread_index], strerror(err));
  1054. die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
  1055. exit(-1);
  1056. }
  1057. assert(fd[i][counter][thread_index] >= 0);
  1058. fcntl(fd[i][counter][thread_index], F_SETFL, O_NONBLOCK);
  1059. /*
  1060. * First counter acts as the group leader:
  1061. */
  1062. if (group && group_fd == -1)
  1063. group_fd = fd[i][counter][thread_index];
  1064. event_array[nr_poll].fd = fd[i][counter][thread_index];
  1065. event_array[nr_poll].events = POLLIN;
  1066. nr_poll++;
  1067. mmap_array[i][counter][thread_index].counter = counter;
  1068. mmap_array[i][counter][thread_index].prev = 0;
  1069. mmap_array[i][counter][thread_index].mask = mmap_pages*page_size - 1;
  1070. mmap_array[i][counter][thread_index].base = mmap(NULL, (mmap_pages+1)*page_size,
  1071. PROT_READ, MAP_SHARED, fd[i][counter][thread_index], 0);
  1072. if (mmap_array[i][counter][thread_index].base == MAP_FAILED)
  1073. die("failed to mmap with %d (%s)\n", errno, strerror(errno));
  1074. }
  1075. }
  1076. static int __cmd_top(void)
  1077. {
  1078. pthread_t thread;
  1079. int i, counter;
  1080. int ret;
  1081. /*
  1082. * FIXME: perf_session__new should allow passing a O_MMAP, so that all this
  1083. * mmap reading, etc is encapsulated in it. Use O_WRONLY for now.
  1084. */
  1085. struct perf_session *session = perf_session__new(NULL, O_WRONLY, false, false);
  1086. if (session == NULL)
  1087. return -ENOMEM;
  1088. if (target_tid != -1)
  1089. event__synthesize_thread(target_tid, event__process, session);
  1090. else
  1091. event__synthesize_threads(event__process, session);
  1092. for (i = 0; i < nr_cpus; i++) {
  1093. group_fd = -1;
  1094. for (counter = 0; counter < nr_counters; counter++)
  1095. start_counter(i, counter);
  1096. }
  1097. /* Wait for a minimal set of events before starting the snapshot */
  1098. poll(&event_array[0], nr_poll, 100);
  1099. perf_session__mmap_read(session);
  1100. if (pthread_create(&thread, NULL, display_thread, session)) {
  1101. printf("Could not create display thread.\n");
  1102. exit(-1);
  1103. }
  1104. if (realtime_prio) {
  1105. struct sched_param param;
  1106. param.sched_priority = realtime_prio;
  1107. if (sched_setscheduler(0, SCHED_FIFO, &param)) {
  1108. printf("Could not set realtime priority.\n");
  1109. exit(-1);
  1110. }
  1111. }
  1112. while (1) {
  1113. int hits = samples;
  1114. perf_session__mmap_read(session);
  1115. if (hits == samples)
  1116. ret = poll(event_array, nr_poll, 100);
  1117. }
  1118. return 0;
  1119. }
  1120. static const char * const top_usage[] = {
  1121. "perf top [<options>]",
  1122. NULL
  1123. };
  1124. static const struct option options[] = {
  1125. OPT_CALLBACK('e', "event", NULL, "event",
  1126. "event selector. use 'perf list' to list available events",
  1127. parse_events),
  1128. OPT_INTEGER('c', "count", &default_interval,
  1129. "event period to sample"),
  1130. OPT_INTEGER('p', "pid", &target_pid,
  1131. "profile events on existing process id"),
  1132. OPT_INTEGER('t', "tid", &target_tid,
  1133. "profile events on existing thread id"),
  1134. OPT_BOOLEAN('a', "all-cpus", &system_wide,
  1135. "system-wide collection from all CPUs"),
  1136. OPT_STRING('C', "cpu", &cpu_list, "cpu",
  1137. "list of cpus to monitor"),
  1138. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  1139. "file", "vmlinux pathname"),
  1140. OPT_BOOLEAN('K', "hide_kernel_symbols", &hide_kernel_symbols,
  1141. "hide kernel symbols"),
  1142. OPT_UINTEGER('m', "mmap-pages", &mmap_pages, "number of mmap data pages"),
  1143. OPT_INTEGER('r', "realtime", &realtime_prio,
  1144. "collect data with this RT SCHED_FIFO priority"),
  1145. OPT_INTEGER('d', "delay", &delay_secs,
  1146. "number of seconds to delay between refreshes"),
  1147. OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
  1148. "dump the symbol table used for profiling"),
  1149. OPT_INTEGER('f', "count-filter", &count_filter,
  1150. "only display functions with more events than this"),
  1151. OPT_BOOLEAN('g', "group", &group,
  1152. "put the counters into a counter group"),
  1153. OPT_BOOLEAN('i', "inherit", &inherit,
  1154. "child tasks inherit counters"),
  1155. OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name",
  1156. "symbol to annotate"),
  1157. OPT_BOOLEAN('z', "zero", &zero,
  1158. "zero history across updates"),
  1159. OPT_INTEGER('F', "freq", &freq,
  1160. "profile at this frequency"),
  1161. OPT_INTEGER('E', "entries", &print_entries,
  1162. "display this many functions"),
  1163. OPT_BOOLEAN('U', "hide_user_symbols", &hide_user_symbols,
  1164. "hide user symbols"),
  1165. OPT_INCR('v', "verbose", &verbose,
  1166. "be more verbose (show counter open errors, etc)"),
  1167. OPT_END()
  1168. };
  1169. int cmd_top(int argc, const char **argv, const char *prefix __used)
  1170. {
  1171. int counter;
  1172. int i,j;
  1173. page_size = sysconf(_SC_PAGE_SIZE);
  1174. argc = parse_options(argc, argv, options, top_usage, 0);
  1175. if (argc)
  1176. usage_with_options(top_usage, options);
  1177. if (target_pid != -1) {
  1178. target_tid = target_pid;
  1179. thread_num = find_all_tid(target_pid, &all_tids);
  1180. if (thread_num <= 0) {
  1181. fprintf(stderr, "Can't find all threads of pid %d\n",
  1182. target_pid);
  1183. usage_with_options(top_usage, options);
  1184. }
  1185. } else {
  1186. all_tids=malloc(sizeof(pid_t));
  1187. if (!all_tids)
  1188. return -ENOMEM;
  1189. all_tids[0] = target_tid;
  1190. thread_num = 1;
  1191. }
  1192. for (i = 0; i < MAX_NR_CPUS; i++) {
  1193. for (j = 0; j < MAX_COUNTERS; j++) {
  1194. fd[i][j] = malloc(sizeof(int)*thread_num);
  1195. mmap_array[i][j] = zalloc(
  1196. sizeof(struct mmap_data)*thread_num);
  1197. if (!fd[i][j] || !mmap_array[i][j])
  1198. return -ENOMEM;
  1199. }
  1200. }
  1201. event_array = malloc(
  1202. sizeof(struct pollfd)*MAX_NR_CPUS*MAX_COUNTERS*thread_num);
  1203. if (!event_array)
  1204. return -ENOMEM;
  1205. /* CPU and PID are mutually exclusive */
  1206. if (target_tid > 0 && cpu_list) {
  1207. printf("WARNING: PID switch overriding CPU\n");
  1208. sleep(1);
  1209. cpu_list = NULL;
  1210. }
  1211. if (!nr_counters)
  1212. nr_counters = 1;
  1213. symbol_conf.priv_size = (sizeof(struct sym_entry) +
  1214. (nr_counters + 1) * sizeof(unsigned long));
  1215. symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
  1216. if (symbol__init() < 0)
  1217. return -1;
  1218. if (delay_secs < 1)
  1219. delay_secs = 1;
  1220. /*
  1221. * User specified count overrides default frequency.
  1222. */
  1223. if (default_interval)
  1224. freq = 0;
  1225. else if (freq) {
  1226. default_interval = freq;
  1227. } else {
  1228. fprintf(stderr, "frequency and count are zero, aborting\n");
  1229. exit(EXIT_FAILURE);
  1230. }
  1231. /*
  1232. * Fill in the ones not specifically initialized via -c:
  1233. */
  1234. for (counter = 0; counter < nr_counters; counter++) {
  1235. if (attrs[counter].sample_period)
  1236. continue;
  1237. attrs[counter].sample_period = default_interval;
  1238. }
  1239. if (target_tid != -1)
  1240. nr_cpus = 1;
  1241. else
  1242. nr_cpus = read_cpu_map(cpu_list);
  1243. if (nr_cpus < 1)
  1244. usage_with_options(top_usage, options);
  1245. get_term_dimensions(&winsize);
  1246. if (print_entries == 0) {
  1247. update_print_entries(&winsize);
  1248. signal(SIGWINCH, sig_winch_handler);
  1249. }
  1250. return __cmd_top();
  1251. }