builtin-top.c 36 KB

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