builtin-trace.c 20 KB

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