builtin-top.c 35 KB

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