builtin-top.c 35 KB

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