builtin-top.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439
  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 int system_wide = 0;
  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 int inherit = 0;
  59. static int profile_cpu = -1;
  60. static int nr_cpus = 0;
  61. static unsigned int realtime_prio = 0;
  62. static int group = 0;
  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 int zero = 0;
  68. static int dump_symtab = 0;
  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 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 userspace_samples;
  348. static long exact_samples;
  349. static const char CONSOLE_CLEAR[] = "";
  350. static void __list_insert_active_sym(struct sym_entry *syme)
  351. {
  352. list_add(&syme->node, &active_symbols);
  353. }
  354. static void list_remove_active_sym(struct sym_entry *syme)
  355. {
  356. pthread_mutex_lock(&active_symbols_lock);
  357. list_del_init(&syme->node);
  358. pthread_mutex_unlock(&active_symbols_lock);
  359. }
  360. static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se)
  361. {
  362. struct rb_node **p = &tree->rb_node;
  363. struct rb_node *parent = NULL;
  364. struct sym_entry *iter;
  365. while (*p != NULL) {
  366. parent = *p;
  367. iter = rb_entry(parent, struct sym_entry, rb_node);
  368. if (se->weight > iter->weight)
  369. p = &(*p)->rb_left;
  370. else
  371. p = &(*p)->rb_right;
  372. }
  373. rb_link_node(&se->rb_node, parent, p);
  374. rb_insert_color(&se->rb_node, tree);
  375. }
  376. static void print_sym_table(void)
  377. {
  378. int printed = 0, j;
  379. int counter, snap = !display_weighted ? sym_counter : 0;
  380. float samples_per_sec = samples/delay_secs;
  381. float ksamples_per_sec = (samples-userspace_samples)/delay_secs;
  382. float esamples_percent = (100.0*exact_samples)/samples;
  383. float sum_ksamples = 0.0;
  384. struct sym_entry *syme, *n;
  385. struct rb_root tmp = RB_ROOT;
  386. struct rb_node *nd;
  387. int sym_width = 0, dso_width = 0, dso_short_width = 0;
  388. const int win_width = winsize.ws_col - 1;
  389. samples = userspace_samples = exact_samples = 0;
  390. /* Sort the active symbols */
  391. pthread_mutex_lock(&active_symbols_lock);
  392. syme = list_entry(active_symbols.next, struct sym_entry, node);
  393. pthread_mutex_unlock(&active_symbols_lock);
  394. list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
  395. syme->snap_count = syme->count[snap];
  396. if (syme->snap_count != 0) {
  397. if ((hide_user_symbols &&
  398. syme->origin == PERF_RECORD_MISC_USER) ||
  399. (hide_kernel_symbols &&
  400. syme->origin == PERF_RECORD_MISC_KERNEL)) {
  401. list_remove_active_sym(syme);
  402. continue;
  403. }
  404. syme->weight = sym_weight(syme);
  405. rb_insert_active_sym(&tmp, syme);
  406. sum_ksamples += syme->snap_count;
  407. for (j = 0; j < nr_counters; j++)
  408. syme->count[j] = zero ? 0 : syme->count[j] * 7 / 8;
  409. } else
  410. list_remove_active_sym(syme);
  411. }
  412. puts(CONSOLE_CLEAR);
  413. printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
  414. printf( " PerfTop:%8.0f irqs/sec kernel:%4.1f%% exact: %4.1f%% [",
  415. samples_per_sec,
  416. 100.0 - (100.0*((samples_per_sec-ksamples_per_sec)/samples_per_sec)),
  417. esamples_percent);
  418. if (nr_counters == 1 || !display_weighted) {
  419. printf("%Ld", (u64)attrs[0].sample_period);
  420. if (freq)
  421. printf("Hz ");
  422. else
  423. printf(" ");
  424. }
  425. if (!display_weighted)
  426. printf("%s", event_name(sym_counter));
  427. else for (counter = 0; counter < nr_counters; counter++) {
  428. if (counter)
  429. printf("/");
  430. printf("%s", event_name(counter));
  431. }
  432. printf( "], ");
  433. if (target_pid != -1)
  434. printf(" (target_pid: %d", target_pid);
  435. else if (target_tid != -1)
  436. printf(" (target_tid: %d", target_tid);
  437. else
  438. printf(" (all");
  439. if (profile_cpu != -1)
  440. printf(", cpu: %d)\n", profile_cpu);
  441. else {
  442. if (target_tid != -1)
  443. printf(")\n");
  444. else
  445. printf(", %d CPUs)\n", nr_cpus);
  446. }
  447. printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
  448. if (sym_filter_entry) {
  449. show_details(sym_filter_entry);
  450. return;
  451. }
  452. /*
  453. * Find the longest symbol name that will be displayed
  454. */
  455. for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
  456. syme = rb_entry(nd, struct sym_entry, rb_node);
  457. if (++printed > print_entries ||
  458. (int)syme->snap_count < count_filter)
  459. continue;
  460. if (syme->map->dso->long_name_len > dso_width)
  461. dso_width = syme->map->dso->long_name_len;
  462. if (syme->map->dso->short_name_len > dso_short_width)
  463. dso_short_width = syme->map->dso->short_name_len;
  464. if (syme->name_len > sym_width)
  465. sym_width = syme->name_len;
  466. }
  467. printed = 0;
  468. if (sym_width + dso_width > winsize.ws_col - 29) {
  469. dso_width = dso_short_width;
  470. if (sym_width + dso_width > winsize.ws_col - 29)
  471. sym_width = winsize.ws_col - dso_width - 29;
  472. }
  473. putchar('\n');
  474. if (nr_counters == 1)
  475. printf(" samples pcnt");
  476. else
  477. printf(" weight samples pcnt");
  478. if (verbose)
  479. printf(" RIP ");
  480. printf(" %-*.*s DSO\n", sym_width, sym_width, "function");
  481. printf(" %s _______ _____",
  482. nr_counters == 1 ? " " : "______");
  483. if (verbose)
  484. printf(" ________________");
  485. printf(" %-*.*s", sym_width, sym_width, graph_line);
  486. printf(" %-*.*s", dso_width, dso_width, graph_line);
  487. puts("\n");
  488. for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
  489. struct symbol *sym;
  490. double pcnt;
  491. syme = rb_entry(nd, struct sym_entry, rb_node);
  492. sym = sym_entry__symbol(syme);
  493. if (++printed > print_entries || (int)syme->snap_count < count_filter)
  494. continue;
  495. pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) /
  496. sum_ksamples));
  497. if (nr_counters == 1 || !display_weighted)
  498. printf("%20.2f ", syme->weight);
  499. else
  500. printf("%9.1f %10ld ", syme->weight, syme->snap_count);
  501. percent_color_fprintf(stdout, "%4.1f%%", pcnt);
  502. if (verbose)
  503. printf(" %016llx", sym->start);
  504. printf(" %-*.*s", sym_width, sym_width, sym->name);
  505. printf(" %-*.*s\n", dso_width, dso_width,
  506. dso_width >= syme->map->dso->long_name_len ?
  507. syme->map->dso->long_name :
  508. syme->map->dso->short_name);
  509. }
  510. }
  511. static void prompt_integer(int *target, const char *msg)
  512. {
  513. char *buf = malloc(0), *p;
  514. size_t dummy = 0;
  515. int tmp;
  516. fprintf(stdout, "\n%s: ", msg);
  517. if (getline(&buf, &dummy, stdin) < 0)
  518. return;
  519. p = strchr(buf, '\n');
  520. if (p)
  521. *p = 0;
  522. p = buf;
  523. while(*p) {
  524. if (!isdigit(*p))
  525. goto out_free;
  526. p++;
  527. }
  528. tmp = strtoul(buf, NULL, 10);
  529. *target = tmp;
  530. out_free:
  531. free(buf);
  532. }
  533. static void prompt_percent(int *target, const char *msg)
  534. {
  535. int tmp = 0;
  536. prompt_integer(&tmp, msg);
  537. if (tmp >= 0 && tmp <= 100)
  538. *target = tmp;
  539. }
  540. static void prompt_symbol(struct sym_entry **target, const char *msg)
  541. {
  542. char *buf = malloc(0), *p;
  543. struct sym_entry *syme = *target, *n, *found = NULL;
  544. size_t dummy = 0;
  545. /* zero counters of active symbol */
  546. if (syme) {
  547. pthread_mutex_lock(&syme->src->lock);
  548. __zero_source_counters(syme);
  549. *target = NULL;
  550. pthread_mutex_unlock(&syme->src->lock);
  551. }
  552. fprintf(stdout, "\n%s: ", msg);
  553. if (getline(&buf, &dummy, stdin) < 0)
  554. goto out_free;
  555. p = strchr(buf, '\n');
  556. if (p)
  557. *p = 0;
  558. pthread_mutex_lock(&active_symbols_lock);
  559. syme = list_entry(active_symbols.next, struct sym_entry, node);
  560. pthread_mutex_unlock(&active_symbols_lock);
  561. list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
  562. struct symbol *sym = sym_entry__symbol(syme);
  563. if (!strcmp(buf, sym->name)) {
  564. found = syme;
  565. break;
  566. }
  567. }
  568. if (!found) {
  569. fprintf(stderr, "Sorry, %s is not active.\n", buf);
  570. sleep(1);
  571. return;
  572. } else
  573. parse_source(found);
  574. out_free:
  575. free(buf);
  576. }
  577. static void print_mapped_keys(void)
  578. {
  579. char *name = NULL;
  580. if (sym_filter_entry) {
  581. struct symbol *sym = sym_entry__symbol(sym_filter_entry);
  582. name = sym->name;
  583. }
  584. fprintf(stdout, "\nMapped keys:\n");
  585. fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", delay_secs);
  586. fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", print_entries);
  587. if (nr_counters > 1)
  588. fprintf(stdout, "\t[E] active event counter. \t(%s)\n", event_name(sym_counter));
  589. fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", count_filter);
  590. fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", sym_pcnt_filter);
  591. fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL");
  592. fprintf(stdout, "\t[S] stop annotation.\n");
  593. if (nr_counters > 1)
  594. fprintf(stdout, "\t[w] toggle display weighted/count[E]r. \t(%d)\n", display_weighted ? 1 : 0);
  595. fprintf(stdout,
  596. "\t[K] hide kernel_symbols symbols. \t(%s)\n",
  597. hide_kernel_symbols ? "yes" : "no");
  598. fprintf(stdout,
  599. "\t[U] hide user symbols. \t(%s)\n",
  600. hide_user_symbols ? "yes" : "no");
  601. fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", zero ? 1 : 0);
  602. fprintf(stdout, "\t[qQ] quit.\n");
  603. }
  604. static int key_mapped(int c)
  605. {
  606. switch (c) {
  607. case 'd':
  608. case 'e':
  609. case 'f':
  610. case 'z':
  611. case 'q':
  612. case 'Q':
  613. case 'K':
  614. case 'U':
  615. case 'F':
  616. case 's':
  617. case 'S':
  618. return 1;
  619. case 'E':
  620. case 'w':
  621. return nr_counters > 1 ? 1 : 0;
  622. default:
  623. break;
  624. }
  625. return 0;
  626. }
  627. static void handle_keypress(int c)
  628. {
  629. if (!key_mapped(c)) {
  630. struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
  631. struct termios tc, save;
  632. print_mapped_keys();
  633. fprintf(stdout, "\nEnter selection, or unmapped key to continue: ");
  634. fflush(stdout);
  635. tcgetattr(0, &save);
  636. tc = save;
  637. tc.c_lflag &= ~(ICANON | ECHO);
  638. tc.c_cc[VMIN] = 0;
  639. tc.c_cc[VTIME] = 0;
  640. tcsetattr(0, TCSANOW, &tc);
  641. poll(&stdin_poll, 1, -1);
  642. c = getc(stdin);
  643. tcsetattr(0, TCSAFLUSH, &save);
  644. if (!key_mapped(c))
  645. return;
  646. }
  647. switch (c) {
  648. case 'd':
  649. prompt_integer(&delay_secs, "Enter display delay");
  650. if (delay_secs < 1)
  651. delay_secs = 1;
  652. break;
  653. case 'e':
  654. prompt_integer(&print_entries, "Enter display entries (lines)");
  655. if (print_entries == 0) {
  656. sig_winch_handler(SIGWINCH);
  657. signal(SIGWINCH, sig_winch_handler);
  658. } else
  659. signal(SIGWINCH, SIG_DFL);
  660. break;
  661. case 'E':
  662. if (nr_counters > 1) {
  663. int i;
  664. fprintf(stderr, "\nAvailable events:");
  665. for (i = 0; i < nr_counters; i++)
  666. fprintf(stderr, "\n\t%d %s", i, event_name(i));
  667. prompt_integer(&sym_counter, "Enter details event counter");
  668. if (sym_counter >= nr_counters) {
  669. fprintf(stderr, "Sorry, no such event, using %s.\n", event_name(0));
  670. sym_counter = 0;
  671. sleep(1);
  672. }
  673. } else sym_counter = 0;
  674. break;
  675. case 'f':
  676. prompt_integer(&count_filter, "Enter display event count filter");
  677. break;
  678. case 'F':
  679. prompt_percent(&sym_pcnt_filter, "Enter details display event filter (percent)");
  680. break;
  681. case 'K':
  682. hide_kernel_symbols = !hide_kernel_symbols;
  683. break;
  684. case 'q':
  685. case 'Q':
  686. printf("exiting.\n");
  687. if (dump_symtab)
  688. dsos__fprintf(stderr);
  689. exit(0);
  690. case 's':
  691. prompt_symbol(&sym_filter_entry, "Enter details symbol");
  692. break;
  693. case 'S':
  694. if (!sym_filter_entry)
  695. break;
  696. else {
  697. struct sym_entry *syme = sym_filter_entry;
  698. pthread_mutex_lock(&syme->src->lock);
  699. sym_filter_entry = NULL;
  700. __zero_source_counters(syme);
  701. pthread_mutex_unlock(&syme->src->lock);
  702. }
  703. break;
  704. case 'U':
  705. hide_user_symbols = !hide_user_symbols;
  706. break;
  707. case 'w':
  708. display_weighted = ~display_weighted;
  709. break;
  710. case 'z':
  711. zero = ~zero;
  712. break;
  713. default:
  714. break;
  715. }
  716. }
  717. static void *display_thread(void *arg __used)
  718. {
  719. struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
  720. struct termios tc, save;
  721. int delay_msecs, c;
  722. tcgetattr(0, &save);
  723. tc = save;
  724. tc.c_lflag &= ~(ICANON | ECHO);
  725. tc.c_cc[VMIN] = 0;
  726. tc.c_cc[VTIME] = 0;
  727. repeat:
  728. delay_msecs = delay_secs * 1000;
  729. tcsetattr(0, TCSANOW, &tc);
  730. /* trash return*/
  731. getc(stdin);
  732. do {
  733. print_sym_table();
  734. } while (!poll(&stdin_poll, 1, delay_msecs) == 1);
  735. c = getc(stdin);
  736. tcsetattr(0, TCSAFLUSH, &save);
  737. handle_keypress(c);
  738. goto repeat;
  739. return NULL;
  740. }
  741. /* Tag samples to be skipped. */
  742. static const char *skip_symbols[] = {
  743. "default_idle",
  744. "cpu_idle",
  745. "enter_idle",
  746. "exit_idle",
  747. "mwait_idle",
  748. "mwait_idle_with_hints",
  749. "poll_idle",
  750. "ppc64_runlatch_off",
  751. "pseries_dedicated_idle_sleep",
  752. NULL
  753. };
  754. static int symbol_filter(struct map *map, struct symbol *sym)
  755. {
  756. struct sym_entry *syme;
  757. const char *name = sym->name;
  758. int i;
  759. /*
  760. * ppc64 uses function descriptors and appends a '.' to the
  761. * start of every instruction address. Remove it.
  762. */
  763. if (name[0] == '.')
  764. name++;
  765. if (!strcmp(name, "_text") ||
  766. !strcmp(name, "_etext") ||
  767. !strcmp(name, "_sinittext") ||
  768. !strncmp("init_module", name, 11) ||
  769. !strncmp("cleanup_module", name, 14) ||
  770. strstr(name, "_text_start") ||
  771. strstr(name, "_text_end"))
  772. return 1;
  773. syme = symbol__priv(sym);
  774. syme->map = map;
  775. syme->src = NULL;
  776. if (!sym_filter_entry && sym_filter && !strcmp(name, sym_filter)) {
  777. /* schedule initial sym_filter_entry setup */
  778. sym_filter_entry_sched = syme;
  779. sym_filter = NULL;
  780. }
  781. for (i = 0; skip_symbols[i]; i++) {
  782. if (!strcmp(skip_symbols[i], name)) {
  783. syme->skip = 1;
  784. break;
  785. }
  786. }
  787. if (!syme->skip)
  788. syme->name_len = strlen(sym->name);
  789. return 0;
  790. }
  791. static void event__process_sample(const event_t *self,
  792. struct perf_session *session, int counter)
  793. {
  794. u64 ip = self->ip.ip;
  795. struct sym_entry *syme;
  796. struct addr_location al;
  797. u8 origin = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  798. ++samples;
  799. switch (origin) {
  800. case PERF_RECORD_MISC_USER:
  801. ++userspace_samples;
  802. if (hide_user_symbols)
  803. return;
  804. break;
  805. case PERF_RECORD_MISC_KERNEL:
  806. if (hide_kernel_symbols)
  807. return;
  808. break;
  809. default:
  810. return;
  811. }
  812. if (self->header.misc & PERF_RECORD_MISC_EXACT)
  813. exact_samples++;
  814. if (event__preprocess_sample(self, session, &al, symbol_filter) < 0 ||
  815. al.filtered)
  816. return;
  817. if (al.sym == NULL) {
  818. /*
  819. * As we do lazy loading of symtabs we only will know if the
  820. * specified vmlinux file is invalid when we actually have a
  821. * hit in kernel space and then try to load it. So if we get
  822. * here and there are _no_ symbols in the DSO backing the
  823. * kernel map, bail out.
  824. *
  825. * We may never get here, for instance, if we use -K/
  826. * --hide-kernel-symbols, even if the user specifies an
  827. * invalid --vmlinux ;-)
  828. */
  829. if (al.map == session->vmlinux_maps[MAP__FUNCTION] &&
  830. RB_EMPTY_ROOT(&al.map->dso->symbols[MAP__FUNCTION])) {
  831. pr_err("The %s file can't be used\n",
  832. symbol_conf.vmlinux_name);
  833. exit(1);
  834. }
  835. return;
  836. }
  837. /* let's see, whether we need to install initial sym_filter_entry */
  838. if (sym_filter_entry_sched) {
  839. sym_filter_entry = sym_filter_entry_sched;
  840. sym_filter_entry_sched = NULL;
  841. if (parse_source(sym_filter_entry) < 0) {
  842. struct symbol *sym = sym_entry__symbol(sym_filter_entry);
  843. pr_err("Can't annotate %s", sym->name);
  844. if (sym_filter_entry->map->dso->origin == DSO__ORIG_KERNEL) {
  845. pr_err(": No vmlinux file was found in the path:\n");
  846. vmlinux_path__fprintf(stderr);
  847. } else
  848. pr_err(".\n");
  849. exit(1);
  850. }
  851. }
  852. syme = symbol__priv(al.sym);
  853. if (!syme->skip) {
  854. syme->count[counter]++;
  855. syme->origin = origin;
  856. record_precise_ip(syme, counter, ip);
  857. pthread_mutex_lock(&active_symbols_lock);
  858. if (list_empty(&syme->node) || !syme->node.next)
  859. __list_insert_active_sym(syme);
  860. pthread_mutex_unlock(&active_symbols_lock);
  861. }
  862. }
  863. static int event__process(event_t *event, struct perf_session *session)
  864. {
  865. switch (event->header.type) {
  866. case PERF_RECORD_COMM:
  867. event__process_comm(event, session);
  868. break;
  869. case PERF_RECORD_MMAP:
  870. event__process_mmap(event, session);
  871. break;
  872. case PERF_RECORD_FORK:
  873. case PERF_RECORD_EXIT:
  874. event__process_task(event, session);
  875. break;
  876. default:
  877. break;
  878. }
  879. return 0;
  880. }
  881. struct mmap_data {
  882. int counter;
  883. void *base;
  884. int mask;
  885. unsigned int prev;
  886. };
  887. static unsigned int mmap_read_head(struct mmap_data *md)
  888. {
  889. struct perf_event_mmap_page *pc = md->base;
  890. int head;
  891. head = pc->data_head;
  892. rmb();
  893. return head;
  894. }
  895. static void perf_session__mmap_read_counter(struct perf_session *self,
  896. struct mmap_data *md)
  897. {
  898. unsigned int head = mmap_read_head(md);
  899. unsigned int old = md->prev;
  900. unsigned char *data = md->base + page_size;
  901. int diff;
  902. /*
  903. * If we're further behind than half the buffer, there's a chance
  904. * the writer will bite our tail and mess up the samples under us.
  905. *
  906. * If we somehow ended up ahead of the head, we got messed up.
  907. *
  908. * In either case, truncate and restart at head.
  909. */
  910. diff = head - old;
  911. if (diff > md->mask / 2 || diff < 0) {
  912. fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
  913. /*
  914. * head points to a known good entry, start there.
  915. */
  916. old = head;
  917. }
  918. for (; old != head;) {
  919. event_t *event = (event_t *)&data[old & md->mask];
  920. event_t event_copy;
  921. size_t size = event->header.size;
  922. /*
  923. * Event straddles the mmap boundary -- header should always
  924. * be inside due to u64 alignment of output.
  925. */
  926. if ((old & md->mask) + size != ((old + size) & md->mask)) {
  927. unsigned int offset = old;
  928. unsigned int len = min(sizeof(*event), size), cpy;
  929. void *dst = &event_copy;
  930. do {
  931. cpy = min(md->mask + 1 - (offset & md->mask), len);
  932. memcpy(dst, &data[offset & md->mask], cpy);
  933. offset += cpy;
  934. dst += cpy;
  935. len -= cpy;
  936. } while (len);
  937. event = &event_copy;
  938. }
  939. if (event->header.type == PERF_RECORD_SAMPLE)
  940. event__process_sample(event, self, md->counter);
  941. else
  942. event__process(event, self);
  943. old += size;
  944. }
  945. md->prev = old;
  946. }
  947. static struct pollfd *event_array;
  948. static struct mmap_data *mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
  949. static void perf_session__mmap_read(struct perf_session *self)
  950. {
  951. int i, counter, thread_index;
  952. for (i = 0; i < nr_cpus; i++) {
  953. for (counter = 0; counter < nr_counters; counter++)
  954. for (thread_index = 0;
  955. thread_index < thread_num;
  956. thread_index++) {
  957. perf_session__mmap_read_counter(self,
  958. &mmap_array[i][counter][thread_index]);
  959. }
  960. }
  961. }
  962. int nr_poll;
  963. int group_fd;
  964. static void start_counter(int i, int counter)
  965. {
  966. struct perf_event_attr *attr;
  967. int cpu;
  968. int thread_index;
  969. cpu = profile_cpu;
  970. if (target_tid == -1 && profile_cpu == -1)
  971. cpu = cpumap[i];
  972. attr = attrs + counter;
  973. attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
  974. if (freq) {
  975. attr->sample_type |= PERF_SAMPLE_PERIOD;
  976. attr->freq = 1;
  977. attr->sample_freq = freq;
  978. }
  979. attr->inherit = (cpu < 0) && inherit;
  980. attr->mmap = 1;
  981. for (thread_index = 0; thread_index < thread_num; thread_index++) {
  982. try_again:
  983. fd[i][counter][thread_index] = sys_perf_event_open(attr,
  984. all_tids[thread_index], cpu, group_fd, 0);
  985. if (fd[i][counter][thread_index] < 0) {
  986. int err = errno;
  987. if (err == EPERM || err == EACCES)
  988. die("No permission - are you root?\n");
  989. /*
  990. * If it's cycles then fall back to hrtimer
  991. * based cpu-clock-tick sw counter, which
  992. * is always available even if no PMU support:
  993. */
  994. if (attr->type == PERF_TYPE_HARDWARE
  995. && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
  996. if (verbose)
  997. warning(" ... trying to fall back to cpu-clock-ticks\n");
  998. attr->type = PERF_TYPE_SOFTWARE;
  999. attr->config = PERF_COUNT_SW_CPU_CLOCK;
  1000. goto try_again;
  1001. }
  1002. printf("\n");
  1003. error("perfcounter syscall returned with %d (%s)\n",
  1004. fd[i][counter][thread_index], strerror(err));
  1005. die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
  1006. exit(-1);
  1007. }
  1008. assert(fd[i][counter][thread_index] >= 0);
  1009. fcntl(fd[i][counter][thread_index], F_SETFL, O_NONBLOCK);
  1010. /*
  1011. * First counter acts as the group leader:
  1012. */
  1013. if (group && group_fd == -1)
  1014. group_fd = fd[i][counter][thread_index];
  1015. event_array[nr_poll].fd = fd[i][counter][thread_index];
  1016. event_array[nr_poll].events = POLLIN;
  1017. nr_poll++;
  1018. mmap_array[i][counter][thread_index].counter = counter;
  1019. mmap_array[i][counter][thread_index].prev = 0;
  1020. mmap_array[i][counter][thread_index].mask = mmap_pages*page_size - 1;
  1021. mmap_array[i][counter][thread_index].base = mmap(NULL, (mmap_pages+1)*page_size,
  1022. PROT_READ, MAP_SHARED, fd[i][counter][thread_index], 0);
  1023. if (mmap_array[i][counter][thread_index].base == MAP_FAILED)
  1024. die("failed to mmap with %d (%s)\n", errno, strerror(errno));
  1025. }
  1026. }
  1027. static int __cmd_top(void)
  1028. {
  1029. pthread_t thread;
  1030. int i, counter;
  1031. int ret;
  1032. /*
  1033. * FIXME: perf_session__new should allow passing a O_MMAP, so that all this
  1034. * mmap reading, etc is encapsulated in it. Use O_WRONLY for now.
  1035. */
  1036. struct perf_session *session = perf_session__new(NULL, O_WRONLY, false);
  1037. if (session == NULL)
  1038. return -ENOMEM;
  1039. if (target_tid != -1)
  1040. event__synthesize_thread(target_tid, event__process, session);
  1041. else
  1042. event__synthesize_threads(event__process, session);
  1043. for (i = 0; i < nr_cpus; i++) {
  1044. group_fd = -1;
  1045. for (counter = 0; counter < nr_counters; counter++)
  1046. start_counter(i, counter);
  1047. }
  1048. /* Wait for a minimal set of events before starting the snapshot */
  1049. poll(&event_array[0], nr_poll, 100);
  1050. perf_session__mmap_read(session);
  1051. if (pthread_create(&thread, NULL, display_thread, NULL)) {
  1052. printf("Could not create display thread.\n");
  1053. exit(-1);
  1054. }
  1055. if (realtime_prio) {
  1056. struct sched_param param;
  1057. param.sched_priority = realtime_prio;
  1058. if (sched_setscheduler(0, SCHED_FIFO, &param)) {
  1059. printf("Could not set realtime priority.\n");
  1060. exit(-1);
  1061. }
  1062. }
  1063. while (1) {
  1064. int hits = samples;
  1065. perf_session__mmap_read(session);
  1066. if (hits == samples)
  1067. ret = poll(event_array, nr_poll, 100);
  1068. }
  1069. return 0;
  1070. }
  1071. static const char * const top_usage[] = {
  1072. "perf top [<options>]",
  1073. NULL
  1074. };
  1075. static const struct option options[] = {
  1076. OPT_CALLBACK('e', "event", NULL, "event",
  1077. "event selector. use 'perf list' to list available events",
  1078. parse_events),
  1079. OPT_INTEGER('c', "count", &default_interval,
  1080. "event period to sample"),
  1081. OPT_INTEGER('p', "pid", &target_pid,
  1082. "profile events on existing process id"),
  1083. OPT_INTEGER('t', "tid", &target_tid,
  1084. "profile events on existing thread id"),
  1085. OPT_BOOLEAN('a', "all-cpus", &system_wide,
  1086. "system-wide collection from all CPUs"),
  1087. OPT_INTEGER('C', "CPU", &profile_cpu,
  1088. "CPU to profile on"),
  1089. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  1090. "file", "vmlinux pathname"),
  1091. OPT_BOOLEAN('K', "hide_kernel_symbols", &hide_kernel_symbols,
  1092. "hide kernel symbols"),
  1093. OPT_INTEGER('m', "mmap-pages", &mmap_pages,
  1094. "number of mmap data pages"),
  1095. OPT_INTEGER('r', "realtime", &realtime_prio,
  1096. "collect data with this RT SCHED_FIFO priority"),
  1097. OPT_INTEGER('d', "delay", &delay_secs,
  1098. "number of seconds to delay between refreshes"),
  1099. OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
  1100. "dump the symbol table used for profiling"),
  1101. OPT_INTEGER('f', "count-filter", &count_filter,
  1102. "only display functions with more events than this"),
  1103. OPT_BOOLEAN('g', "group", &group,
  1104. "put the counters into a counter group"),
  1105. OPT_BOOLEAN('i', "inherit", &inherit,
  1106. "child tasks inherit counters"),
  1107. OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name",
  1108. "symbol to annotate"),
  1109. OPT_BOOLEAN('z', "zero", &zero,
  1110. "zero history across updates"),
  1111. OPT_INTEGER('F', "freq", &freq,
  1112. "profile at this frequency"),
  1113. OPT_INTEGER('E', "entries", &print_entries,
  1114. "display this many functions"),
  1115. OPT_BOOLEAN('U', "hide_user_symbols", &hide_user_symbols,
  1116. "hide user symbols"),
  1117. OPT_BOOLEAN('v', "verbose", &verbose,
  1118. "be more verbose (show counter open errors, etc)"),
  1119. OPT_END()
  1120. };
  1121. int cmd_top(int argc, const char **argv, const char *prefix __used)
  1122. {
  1123. int counter;
  1124. int i,j;
  1125. page_size = sysconf(_SC_PAGE_SIZE);
  1126. argc = parse_options(argc, argv, options, top_usage, 0);
  1127. if (argc)
  1128. usage_with_options(top_usage, options);
  1129. if (target_pid != -1) {
  1130. target_tid = target_pid;
  1131. thread_num = find_all_tid(target_pid, &all_tids);
  1132. if (thread_num <= 0) {
  1133. fprintf(stderr, "Can't find all threads of pid %d\n",
  1134. target_pid);
  1135. usage_with_options(top_usage, options);
  1136. }
  1137. } else {
  1138. all_tids=malloc(sizeof(pid_t));
  1139. if (!all_tids)
  1140. return -ENOMEM;
  1141. all_tids[0] = target_tid;
  1142. thread_num = 1;
  1143. }
  1144. for (i = 0; i < MAX_NR_CPUS; i++) {
  1145. for (j = 0; j < MAX_COUNTERS; j++) {
  1146. fd[i][j] = malloc(sizeof(int)*thread_num);
  1147. mmap_array[i][j] = malloc(
  1148. sizeof(struct mmap_data)*thread_num);
  1149. if (!fd[i][j] || !mmap_array[i][j])
  1150. return -ENOMEM;
  1151. }
  1152. }
  1153. event_array = malloc(
  1154. sizeof(struct pollfd)*MAX_NR_CPUS*MAX_COUNTERS*thread_num);
  1155. if (!event_array)
  1156. return -ENOMEM;
  1157. /* CPU and PID are mutually exclusive */
  1158. if (target_tid > 0 && profile_cpu != -1) {
  1159. printf("WARNING: PID switch overriding CPU\n");
  1160. sleep(1);
  1161. profile_cpu = -1;
  1162. }
  1163. if (!nr_counters)
  1164. nr_counters = 1;
  1165. symbol_conf.priv_size = (sizeof(struct sym_entry) +
  1166. (nr_counters + 1) * sizeof(unsigned long));
  1167. symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
  1168. if (symbol__init() < 0)
  1169. return -1;
  1170. if (delay_secs < 1)
  1171. delay_secs = 1;
  1172. /*
  1173. * User specified count overrides default frequency.
  1174. */
  1175. if (default_interval)
  1176. freq = 0;
  1177. else if (freq) {
  1178. default_interval = freq;
  1179. } else {
  1180. fprintf(stderr, "frequency and count are zero, aborting\n");
  1181. exit(EXIT_FAILURE);
  1182. }
  1183. /*
  1184. * Fill in the ones not specifically initialized via -c:
  1185. */
  1186. for (counter = 0; counter < nr_counters; counter++) {
  1187. if (attrs[counter].sample_period)
  1188. continue;
  1189. attrs[counter].sample_period = default_interval;
  1190. }
  1191. if (target_tid != -1 || profile_cpu != -1)
  1192. nr_cpus = 1;
  1193. else
  1194. nr_cpus = read_cpu_map();
  1195. get_term_dimensions(&winsize);
  1196. if (print_entries == 0) {
  1197. update_print_entries(&winsize);
  1198. signal(SIGWINCH, sig_winch_handler);
  1199. }
  1200. return __cmd_top();
  1201. }