builtin-trace.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. #include <traceevent/event-parse.h>
  2. #include "builtin.h"
  3. #include "util/color.h"
  4. #include "util/debug.h"
  5. #include "util/evlist.h"
  6. #include "util/machine.h"
  7. #include "util/thread.h"
  8. #include "util/parse-options.h"
  9. #include "util/strlist.h"
  10. #include "util/thread_map.h"
  11. #include <libaudit.h>
  12. #include <stdlib.h>
  13. static size_t syscall_arg__scnprintf_hex(char *bf, size_t size, unsigned long arg)
  14. {
  15. return scnprintf(bf, size, "%#lx", arg);
  16. }
  17. static struct syscall_fmt {
  18. const char *name;
  19. const char *alias;
  20. bool errmsg;
  21. bool timeout;
  22. } syscall_fmts[] = {
  23. { .name = "access", .errmsg = true, },
  24. { .name = "arch_prctl", .errmsg = true, .alias = "prctl", },
  25. { .name = "connect", .errmsg = true, },
  26. { .name = "fstat", .errmsg = true, .alias = "newfstat", },
  27. { .name = "fstatat", .errmsg = true, .alias = "newfstatat", },
  28. { .name = "futex", .errmsg = true, },
  29. { .name = "open", .errmsg = true, },
  30. { .name = "poll", .errmsg = true, .timeout = true, },
  31. { .name = "ppoll", .errmsg = true, .timeout = true, },
  32. { .name = "read", .errmsg = true, },
  33. { .name = "recvfrom", .errmsg = true, },
  34. { .name = "select", .errmsg = true, .timeout = true, },
  35. { .name = "socket", .errmsg = true, },
  36. { .name = "stat", .errmsg = true, .alias = "newstat", },
  37. };
  38. static int syscall_fmt__cmp(const void *name, const void *fmtp)
  39. {
  40. const struct syscall_fmt *fmt = fmtp;
  41. return strcmp(name, fmt->name);
  42. }
  43. static struct syscall_fmt *syscall_fmt__find(const char *name)
  44. {
  45. const int nmemb = ARRAY_SIZE(syscall_fmts);
  46. return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp);
  47. }
  48. struct syscall {
  49. struct event_format *tp_format;
  50. const char *name;
  51. bool filtered;
  52. struct syscall_fmt *fmt;
  53. size_t (**arg_scnprintf)(char *bf, size_t size, unsigned long arg);
  54. };
  55. static size_t fprintf_duration(unsigned long t, FILE *fp)
  56. {
  57. double duration = (double)t / NSEC_PER_MSEC;
  58. size_t printed = fprintf(fp, "(");
  59. if (duration >= 1.0)
  60. printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration);
  61. else if (duration >= 0.01)
  62. printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration);
  63. else
  64. printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration);
  65. return printed + fprintf(fp, "): ");
  66. }
  67. struct thread_trace {
  68. u64 entry_time;
  69. u64 exit_time;
  70. bool entry_pending;
  71. unsigned long nr_events;
  72. char *entry_str;
  73. double runtime_ms;
  74. };
  75. static struct thread_trace *thread_trace__new(void)
  76. {
  77. return zalloc(sizeof(struct thread_trace));
  78. }
  79. static struct thread_trace *thread__trace(struct thread *thread, FILE *fp)
  80. {
  81. struct thread_trace *ttrace;
  82. if (thread == NULL)
  83. goto fail;
  84. if (thread->priv == NULL)
  85. thread->priv = thread_trace__new();
  86. if (thread->priv == NULL)
  87. goto fail;
  88. ttrace = thread->priv;
  89. ++ttrace->nr_events;
  90. return ttrace;
  91. fail:
  92. color_fprintf(fp, PERF_COLOR_RED,
  93. "WARNING: not enough memory, dropping samples!\n");
  94. return NULL;
  95. }
  96. struct trace {
  97. struct perf_tool tool;
  98. int audit_machine;
  99. struct {
  100. int max;
  101. struct syscall *table;
  102. } syscalls;
  103. struct perf_record_opts opts;
  104. struct machine host;
  105. u64 base_time;
  106. FILE *output;
  107. unsigned long nr_events;
  108. struct strlist *ev_qualifier;
  109. bool not_ev_qualifier;
  110. bool sched;
  111. bool multiple_threads;
  112. double duration_filter;
  113. double runtime_ms;
  114. };
  115. static bool trace__filter_duration(struct trace *trace, double t)
  116. {
  117. return t < (trace->duration_filter * NSEC_PER_MSEC);
  118. }
  119. static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
  120. {
  121. double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC;
  122. return fprintf(fp, "%10.3f ", ts);
  123. }
  124. static bool done = false;
  125. static void sig_handler(int sig __maybe_unused)
  126. {
  127. done = true;
  128. }
  129. static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread,
  130. u64 duration, u64 tstamp, FILE *fp)
  131. {
  132. size_t printed = trace__fprintf_tstamp(trace, tstamp, fp);
  133. printed += fprintf_duration(duration, fp);
  134. if (trace->multiple_threads)
  135. printed += fprintf(fp, "%d ", thread->tid);
  136. return printed;
  137. }
  138. static int trace__process_event(struct trace *trace, struct machine *machine,
  139. union perf_event *event)
  140. {
  141. int ret = 0;
  142. switch (event->header.type) {
  143. case PERF_RECORD_LOST:
  144. color_fprintf(trace->output, PERF_COLOR_RED,
  145. "LOST %" PRIu64 " events!\n", event->lost.lost);
  146. ret = machine__process_lost_event(machine, event);
  147. default:
  148. ret = machine__process_event(machine, event);
  149. break;
  150. }
  151. return ret;
  152. }
  153. static int trace__tool_process(struct perf_tool *tool,
  154. union perf_event *event,
  155. struct perf_sample *sample __maybe_unused,
  156. struct machine *machine)
  157. {
  158. struct trace *trace = container_of(tool, struct trace, tool);
  159. return trace__process_event(trace, machine, event);
  160. }
  161. static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist)
  162. {
  163. int err = symbol__init();
  164. if (err)
  165. return err;
  166. machine__init(&trace->host, "", HOST_KERNEL_ID);
  167. machine__create_kernel_maps(&trace->host);
  168. if (perf_target__has_task(&trace->opts.target)) {
  169. err = perf_event__synthesize_thread_map(&trace->tool, evlist->threads,
  170. trace__tool_process,
  171. &trace->host);
  172. } else {
  173. err = perf_event__synthesize_threads(&trace->tool, trace__tool_process,
  174. &trace->host);
  175. }
  176. if (err)
  177. symbol__exit();
  178. return err;
  179. }
  180. static int syscall__set_arg_fmts(struct syscall *sc)
  181. {
  182. struct format_field *field;
  183. int idx = 0;
  184. sc->arg_scnprintf = calloc(sc->tp_format->format.nr_fields - 1, sizeof(void *));
  185. if (sc->arg_scnprintf == NULL)
  186. return -1;
  187. for (field = sc->tp_format->format.fields->next; field; field = field->next) {
  188. if (field->flags & FIELD_IS_POINTER)
  189. sc->arg_scnprintf[idx] = syscall_arg__scnprintf_hex;
  190. ++idx;
  191. }
  192. return 0;
  193. }
  194. static int trace__read_syscall_info(struct trace *trace, int id)
  195. {
  196. char tp_name[128];
  197. struct syscall *sc;
  198. const char *name = audit_syscall_to_name(id, trace->audit_machine);
  199. if (name == NULL)
  200. return -1;
  201. if (id > trace->syscalls.max) {
  202. struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc));
  203. if (nsyscalls == NULL)
  204. return -1;
  205. if (trace->syscalls.max != -1) {
  206. memset(nsyscalls + trace->syscalls.max + 1, 0,
  207. (id - trace->syscalls.max) * sizeof(*sc));
  208. } else {
  209. memset(nsyscalls, 0, (id + 1) * sizeof(*sc));
  210. }
  211. trace->syscalls.table = nsyscalls;
  212. trace->syscalls.max = id;
  213. }
  214. sc = trace->syscalls.table + id;
  215. sc->name = name;
  216. if (trace->ev_qualifier) {
  217. bool in = strlist__find(trace->ev_qualifier, name) != NULL;
  218. if (!(in ^ trace->not_ev_qualifier)) {
  219. sc->filtered = true;
  220. /*
  221. * No need to do read tracepoint information since this will be
  222. * filtered out.
  223. */
  224. return 0;
  225. }
  226. }
  227. sc->fmt = syscall_fmt__find(sc->name);
  228. snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
  229. sc->tp_format = event_format__new("syscalls", tp_name);
  230. if (sc->tp_format == NULL && sc->fmt && sc->fmt->alias) {
  231. snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias);
  232. sc->tp_format = event_format__new("syscalls", tp_name);
  233. }
  234. if (sc->tp_format == NULL)
  235. return -1;
  236. return syscall__set_arg_fmts(sc);
  237. }
  238. static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
  239. unsigned long *args)
  240. {
  241. int i = 0;
  242. size_t printed = 0;
  243. if (sc->tp_format != NULL) {
  244. struct format_field *field;
  245. for (field = sc->tp_format->format.fields->next; field; field = field->next) {
  246. printed += scnprintf(bf + printed, size - printed,
  247. "%s%s: ", printed ? ", " : "", field->name);
  248. if (sc->arg_scnprintf && sc->arg_scnprintf[i])
  249. printed += sc->arg_scnprintf[i](bf + printed, size - printed, args[i]);
  250. else
  251. printed += scnprintf(bf + printed, size - printed,
  252. "%ld", args[i]);
  253. ++i;
  254. }
  255. } else {
  256. while (i < 6) {
  257. printed += scnprintf(bf + printed, size - printed,
  258. "%sarg%d: %ld",
  259. printed ? ", " : "", i, args[i]);
  260. ++i;
  261. }
  262. }
  263. return printed;
  264. }
  265. typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel,
  266. struct perf_sample *sample);
  267. static struct syscall *trace__syscall_info(struct trace *trace,
  268. struct perf_evsel *evsel,
  269. struct perf_sample *sample)
  270. {
  271. int id = perf_evsel__intval(evsel, sample, "id");
  272. if (id < 0) {
  273. /*
  274. * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried
  275. * before that, leaving at a higher verbosity level till that is
  276. * explained. Reproduced with plain ftrace with:
  277. *
  278. * echo 1 > /t/events/raw_syscalls/sys_exit/enable
  279. * grep "NR -1 " /t/trace_pipe
  280. *
  281. * After generating some load on the machine.
  282. */
  283. if (verbose > 1) {
  284. static u64 n;
  285. fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n",
  286. id, perf_evsel__name(evsel), ++n);
  287. }
  288. return NULL;
  289. }
  290. if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) &&
  291. trace__read_syscall_info(trace, id))
  292. goto out_cant_read;
  293. if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL))
  294. goto out_cant_read;
  295. return &trace->syscalls.table[id];
  296. out_cant_read:
  297. if (verbose) {
  298. fprintf(trace->output, "Problems reading syscall %d", id);
  299. if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL)
  300. fprintf(trace->output, "(%s)", trace->syscalls.table[id].name);
  301. fputs(" information\n", trace->output);
  302. }
  303. return NULL;
  304. }
  305. static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
  306. struct perf_sample *sample)
  307. {
  308. char *msg;
  309. void *args;
  310. size_t printed = 0;
  311. struct thread *thread;
  312. struct syscall *sc = trace__syscall_info(trace, evsel, sample);
  313. struct thread_trace *ttrace;
  314. if (sc == NULL)
  315. return -1;
  316. if (sc->filtered)
  317. return 0;
  318. thread = machine__findnew_thread(&trace->host, sample->tid);
  319. ttrace = thread__trace(thread, trace->output);
  320. if (ttrace == NULL)
  321. return -1;
  322. args = perf_evsel__rawptr(evsel, sample, "args");
  323. if (args == NULL) {
  324. fprintf(trace->output, "Problems reading syscall arguments\n");
  325. return -1;
  326. }
  327. ttrace = thread->priv;
  328. if (ttrace->entry_str == NULL) {
  329. ttrace->entry_str = malloc(1024);
  330. if (!ttrace->entry_str)
  331. return -1;
  332. }
  333. ttrace->entry_time = sample->time;
  334. msg = ttrace->entry_str;
  335. printed += scnprintf(msg + printed, 1024 - printed, "%s(", sc->name);
  336. printed += syscall__scnprintf_args(sc, msg + printed, 1024 - printed, args);
  337. if (!strcmp(sc->name, "exit_group") || !strcmp(sc->name, "exit")) {
  338. if (!trace->duration_filter) {
  339. trace__fprintf_entry_head(trace, thread, 1, sample->time, trace->output);
  340. fprintf(trace->output, "%-70s\n", ttrace->entry_str);
  341. }
  342. } else
  343. ttrace->entry_pending = true;
  344. return 0;
  345. }
  346. static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
  347. struct perf_sample *sample)
  348. {
  349. int ret;
  350. u64 duration = 0;
  351. struct thread *thread;
  352. struct syscall *sc = trace__syscall_info(trace, evsel, sample);
  353. struct thread_trace *ttrace;
  354. if (sc == NULL)
  355. return -1;
  356. if (sc->filtered)
  357. return 0;
  358. thread = machine__findnew_thread(&trace->host, sample->tid);
  359. ttrace = thread__trace(thread, trace->output);
  360. if (ttrace == NULL)
  361. return -1;
  362. ret = perf_evsel__intval(evsel, sample, "ret");
  363. ttrace = thread->priv;
  364. ttrace->exit_time = sample->time;
  365. if (ttrace->entry_time) {
  366. duration = sample->time - ttrace->entry_time;
  367. if (trace__filter_duration(trace, duration))
  368. goto out;
  369. } else if (trace->duration_filter)
  370. goto out;
  371. trace__fprintf_entry_head(trace, thread, duration, sample->time, trace->output);
  372. if (ttrace->entry_pending) {
  373. fprintf(trace->output, "%-70s", ttrace->entry_str);
  374. } else {
  375. fprintf(trace->output, " ... [");
  376. color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued");
  377. fprintf(trace->output, "]: %s()", sc->name);
  378. }
  379. if (ret < 0 && sc->fmt && sc->fmt->errmsg) {
  380. char bf[256];
  381. const char *emsg = strerror_r(-ret, bf, sizeof(bf)),
  382. *e = audit_errno_to_name(-ret);
  383. fprintf(trace->output, ") = -1 %s %s", e, emsg);
  384. } else if (ret == 0 && sc->fmt && sc->fmt->timeout)
  385. fprintf(trace->output, ") = 0 Timeout");
  386. else
  387. fprintf(trace->output, ") = %d", ret);
  388. fputc('\n', trace->output);
  389. out:
  390. ttrace->entry_pending = false;
  391. return 0;
  392. }
  393. static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel,
  394. struct perf_sample *sample)
  395. {
  396. u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
  397. double runtime_ms = (double)runtime / NSEC_PER_MSEC;
  398. struct thread *thread = machine__findnew_thread(&trace->host, sample->tid);
  399. struct thread_trace *ttrace = thread__trace(thread, trace->output);
  400. if (ttrace == NULL)
  401. goto out_dump;
  402. ttrace->runtime_ms += runtime_ms;
  403. trace->runtime_ms += runtime_ms;
  404. return 0;
  405. out_dump:
  406. fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n",
  407. evsel->name,
  408. perf_evsel__strval(evsel, sample, "comm"),
  409. (pid_t)perf_evsel__intval(evsel, sample, "pid"),
  410. runtime,
  411. perf_evsel__intval(evsel, sample, "vruntime"));
  412. return 0;
  413. }
  414. static int trace__run(struct trace *trace, int argc, const char **argv)
  415. {
  416. struct perf_evlist *evlist = perf_evlist__new();
  417. struct perf_evsel *evsel;
  418. int err = -1, i;
  419. unsigned long before;
  420. const bool forks = argc > 0;
  421. if (evlist == NULL) {
  422. fprintf(trace->output, "Not enough memory to run!\n");
  423. goto out;
  424. }
  425. if (perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_enter", trace__sys_enter) ||
  426. perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_exit", trace__sys_exit)) {
  427. fprintf(trace->output, "Couldn't read the raw_syscalls tracepoints information!\n");
  428. goto out_delete_evlist;
  429. }
  430. if (trace->sched &&
  431. perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime",
  432. trace__sched_stat_runtime)) {
  433. fprintf(trace->output, "Couldn't read the sched_stat_runtime tracepoint information!\n");
  434. goto out_delete_evlist;
  435. }
  436. err = perf_evlist__create_maps(evlist, &trace->opts.target);
  437. if (err < 0) {
  438. fprintf(trace->output, "Problems parsing the target to trace, check your options!\n");
  439. goto out_delete_evlist;
  440. }
  441. err = trace__symbols_init(trace, evlist);
  442. if (err < 0) {
  443. fprintf(trace->output, "Problems initializing symbol libraries!\n");
  444. goto out_delete_maps;
  445. }
  446. perf_evlist__config(evlist, &trace->opts);
  447. signal(SIGCHLD, sig_handler);
  448. signal(SIGINT, sig_handler);
  449. if (forks) {
  450. err = perf_evlist__prepare_workload(evlist, &trace->opts.target,
  451. argv, false, false);
  452. if (err < 0) {
  453. fprintf(trace->output, "Couldn't run the workload!\n");
  454. goto out_delete_maps;
  455. }
  456. }
  457. err = perf_evlist__open(evlist);
  458. if (err < 0) {
  459. fprintf(trace->output, "Couldn't create the events: %s\n", strerror(errno));
  460. goto out_delete_maps;
  461. }
  462. err = perf_evlist__mmap(evlist, UINT_MAX, false);
  463. if (err < 0) {
  464. fprintf(trace->output, "Couldn't mmap the events: %s\n", strerror(errno));
  465. goto out_close_evlist;
  466. }
  467. perf_evlist__enable(evlist);
  468. if (forks)
  469. perf_evlist__start_workload(evlist);
  470. trace->multiple_threads = evlist->threads->map[0] == -1 || evlist->threads->nr > 1;
  471. again:
  472. before = trace->nr_events;
  473. for (i = 0; i < evlist->nr_mmaps; i++) {
  474. union perf_event *event;
  475. while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
  476. const u32 type = event->header.type;
  477. tracepoint_handler handler;
  478. struct perf_sample sample;
  479. ++trace->nr_events;
  480. err = perf_evlist__parse_sample(evlist, event, &sample);
  481. if (err) {
  482. fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err);
  483. continue;
  484. }
  485. if (trace->base_time == 0)
  486. trace->base_time = sample.time;
  487. if (type != PERF_RECORD_SAMPLE) {
  488. trace__process_event(trace, &trace->host, event);
  489. continue;
  490. }
  491. evsel = perf_evlist__id2evsel(evlist, sample.id);
  492. if (evsel == NULL) {
  493. fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample.id);
  494. continue;
  495. }
  496. if (sample.raw_data == NULL) {
  497. fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
  498. perf_evsel__name(evsel), sample.tid,
  499. sample.cpu, sample.raw_size);
  500. continue;
  501. }
  502. handler = evsel->handler.func;
  503. handler(trace, evsel, &sample);
  504. }
  505. }
  506. if (trace->nr_events == before) {
  507. if (done)
  508. goto out_unmap_evlist;
  509. poll(evlist->pollfd, evlist->nr_fds, -1);
  510. }
  511. if (done)
  512. perf_evlist__disable(evlist);
  513. goto again;
  514. out_unmap_evlist:
  515. perf_evlist__munmap(evlist);
  516. out_close_evlist:
  517. perf_evlist__close(evlist);
  518. out_delete_maps:
  519. perf_evlist__delete_maps(evlist);
  520. out_delete_evlist:
  521. perf_evlist__delete(evlist);
  522. out:
  523. return err;
  524. }
  525. static size_t trace__fprintf_threads_header(FILE *fp)
  526. {
  527. size_t printed;
  528. printed = fprintf(fp, "\n _____________________________________________________________________\n");
  529. printed += fprintf(fp," __) Summary of events (__\n\n");
  530. printed += fprintf(fp," [ task - pid ] [ events ] [ ratio ] [ runtime ]\n");
  531. printed += fprintf(fp," _____________________________________________________________________\n\n");
  532. return printed;
  533. }
  534. static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp)
  535. {
  536. size_t printed = trace__fprintf_threads_header(fp);
  537. struct rb_node *nd;
  538. for (nd = rb_first(&trace->host.threads); nd; nd = rb_next(nd)) {
  539. struct thread *thread = rb_entry(nd, struct thread, rb_node);
  540. struct thread_trace *ttrace = thread->priv;
  541. const char *color;
  542. double ratio;
  543. if (ttrace == NULL)
  544. continue;
  545. ratio = (double)ttrace->nr_events / trace->nr_events * 100.0;
  546. color = PERF_COLOR_NORMAL;
  547. if (ratio > 50.0)
  548. color = PERF_COLOR_RED;
  549. else if (ratio > 25.0)
  550. color = PERF_COLOR_GREEN;
  551. else if (ratio > 5.0)
  552. color = PERF_COLOR_YELLOW;
  553. printed += color_fprintf(fp, color, "%20s", thread->comm);
  554. printed += fprintf(fp, " - %-5d :%11lu [", thread->tid, ttrace->nr_events);
  555. printed += color_fprintf(fp, color, "%5.1f%%", ratio);
  556. printed += fprintf(fp, " ] %10.3f ms\n", ttrace->runtime_ms);
  557. }
  558. return printed;
  559. }
  560. static int trace__set_duration(const struct option *opt, const char *str,
  561. int unset __maybe_unused)
  562. {
  563. struct trace *trace = opt->value;
  564. trace->duration_filter = atof(str);
  565. return 0;
  566. }
  567. static int trace__open_output(struct trace *trace, const char *filename)
  568. {
  569. struct stat st;
  570. if (!stat(filename, &st) && st.st_size) {
  571. char oldname[PATH_MAX];
  572. scnprintf(oldname, sizeof(oldname), "%s.old", filename);
  573. unlink(oldname);
  574. rename(filename, oldname);
  575. }
  576. trace->output = fopen(filename, "w");
  577. return trace->output == NULL ? -errno : 0;
  578. }
  579. int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
  580. {
  581. const char * const trace_usage[] = {
  582. "perf trace [<options>] [<command>]",
  583. "perf trace [<options>] -- <command> [<options>]",
  584. NULL
  585. };
  586. struct trace trace = {
  587. .audit_machine = audit_detect_machine(),
  588. .syscalls = {
  589. . max = -1,
  590. },
  591. .opts = {
  592. .target = {
  593. .uid = UINT_MAX,
  594. .uses_mmap = true,
  595. },
  596. .user_freq = UINT_MAX,
  597. .user_interval = ULLONG_MAX,
  598. .no_delay = true,
  599. .mmap_pages = 1024,
  600. },
  601. .output = stdout,
  602. };
  603. const char *output_name = NULL;
  604. const char *ev_qualifier_str = NULL;
  605. const struct option trace_options[] = {
  606. OPT_STRING('e', "expr", &ev_qualifier_str, "expr",
  607. "list of events to trace"),
  608. OPT_STRING('o', "output", &output_name, "file", "output file name"),
  609. OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
  610. "trace events on existing process id"),
  611. OPT_STRING('t', "tid", &trace.opts.target.tid, "tid",
  612. "trace events on existing thread id"),
  613. OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide,
  614. "system-wide collection from all CPUs"),
  615. OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu",
  616. "list of cpus to monitor"),
  617. OPT_BOOLEAN('i', "no-inherit", &trace.opts.no_inherit,
  618. "child tasks do not inherit counters"),
  619. OPT_UINTEGER('m', "mmap-pages", &trace.opts.mmap_pages,
  620. "number of mmap data pages"),
  621. OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user",
  622. "user to profile"),
  623. OPT_CALLBACK(0, "duration", &trace, "float",
  624. "show only events with duration > N.M ms",
  625. trace__set_duration),
  626. OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"),
  627. OPT_INCR('v', "verbose", &verbose, "be more verbose"),
  628. OPT_END()
  629. };
  630. int err;
  631. char bf[BUFSIZ];
  632. argc = parse_options(argc, argv, trace_options, trace_usage, 0);
  633. if (output_name != NULL) {
  634. err = trace__open_output(&trace, output_name);
  635. if (err < 0) {
  636. perror("failed to create output file");
  637. goto out;
  638. }
  639. }
  640. if (ev_qualifier_str != NULL) {
  641. const char *s = ev_qualifier_str;
  642. trace.not_ev_qualifier = *s == '!';
  643. if (trace.not_ev_qualifier)
  644. ++s;
  645. trace.ev_qualifier = strlist__new(true, s);
  646. if (trace.ev_qualifier == NULL) {
  647. fputs("Not enough memory to parse event qualifier",
  648. trace.output);
  649. err = -ENOMEM;
  650. goto out_close;
  651. }
  652. }
  653. err = perf_target__validate(&trace.opts.target);
  654. if (err) {
  655. perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf));
  656. fprintf(trace.output, "%s", bf);
  657. goto out_close;
  658. }
  659. err = perf_target__parse_uid(&trace.opts.target);
  660. if (err) {
  661. perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf));
  662. fprintf(trace.output, "%s", bf);
  663. goto out_close;
  664. }
  665. if (!argc && perf_target__none(&trace.opts.target))
  666. trace.opts.target.system_wide = true;
  667. err = trace__run(&trace, argc, argv);
  668. if (trace.sched && !err)
  669. trace__fprintf_thread_summary(&trace, trace.output);
  670. out_close:
  671. if (output_name != NULL)
  672. fclose(trace.output);
  673. out:
  674. return err;
  675. }