builtin-ftrace.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * builtin-ftrace.c
  4. *
  5. * Copyright (c) 2013 LG Electronics, Namhyung Kim <namhyung@kernel.org>
  6. * Copyright (c) 2020 Changbin Du <changbin.du@gmail.com>, significant enhancement.
  7. */
  8. #include "builtin.h"
  9. #include <errno.h>
  10. #include <unistd.h>
  11. #include <signal.h>
  12. #include <stdlib.h>
  13. #include <fcntl.h>
  14. #include <inttypes.h>
  15. #include <math.h>
  16. #include <poll.h>
  17. #include <ctype.h>
  18. #include <linux/capability.h>
  19. #include <linux/string.h>
  20. #include "debug.h"
  21. #include <subcmd/pager.h>
  22. #include <subcmd/parse-options.h>
  23. #include <api/io.h>
  24. #include <api/fs/tracing_path.h>
  25. #include "evlist.h"
  26. #include "target.h"
  27. #include "cpumap.h"
  28. #include "hashmap.h"
  29. #include "thread_map.h"
  30. #include "strfilter.h"
  31. #include "util/cap.h"
  32. #include "util/config.h"
  33. #include "util/ftrace.h"
  34. #include "util/stat.h"
  35. #include "util/units.h"
  36. #include "util/parse-sublevel-options.h"
  37. #define DEFAULT_TRACER "function_graph"
  38. static volatile sig_atomic_t workload_exec_errno;
  39. static volatile sig_atomic_t done;
  40. static void sig_handler(int sig __maybe_unused)
  41. {
  42. done = true;
  43. }
  44. /*
  45. * evlist__prepare_workload will send a SIGUSR1 if the fork fails, since
  46. * we asked by setting its exec_error to the function below,
  47. * ftrace__workload_exec_failed_signal.
  48. *
  49. * XXX We need to handle this more appropriately, emitting an error, etc.
  50. */
  51. static void ftrace__workload_exec_failed_signal(int signo __maybe_unused,
  52. siginfo_t *info __maybe_unused,
  53. void *ucontext __maybe_unused)
  54. {
  55. workload_exec_errno = info->si_value.sival_int;
  56. done = true;
  57. }
  58. static bool check_ftrace_capable(void)
  59. {
  60. bool used_root;
  61. if (perf_cap__capable(CAP_PERFMON, &used_root))
  62. return true;
  63. if (!used_root && perf_cap__capable(CAP_SYS_ADMIN, &used_root))
  64. return true;
  65. pr_err("ftrace only works for %s!\n",
  66. used_root ? "root"
  67. : "users with the CAP_PERFMON or CAP_SYS_ADMIN capability"
  68. );
  69. return false;
  70. }
  71. static bool is_ftrace_supported(void)
  72. {
  73. char *file;
  74. bool supported = false;
  75. file = get_tracing_file("set_ftrace_pid");
  76. if (!file) {
  77. pr_debug("cannot get tracing file set_ftrace_pid\n");
  78. return false;
  79. }
  80. if (!access(file, F_OK))
  81. supported = true;
  82. put_tracing_file(file);
  83. return supported;
  84. }
  85. static int __write_tracing_file(const char *name, const char *val, bool append)
  86. {
  87. char *file;
  88. int fd, ret = -1;
  89. ssize_t size = strlen(val);
  90. int flags = O_WRONLY;
  91. char errbuf[512];
  92. char *val_copy;
  93. file = get_tracing_file(name);
  94. if (!file) {
  95. pr_debug("cannot get tracing file: %s\n", name);
  96. return -1;
  97. }
  98. if (append)
  99. flags |= O_APPEND;
  100. else
  101. flags |= O_TRUNC;
  102. fd = open(file, flags);
  103. if (fd < 0) {
  104. pr_debug("cannot open tracing file: %s: %s\n",
  105. name, str_error_r(errno, errbuf, sizeof(errbuf)));
  106. goto out;
  107. }
  108. /*
  109. * Copy the original value and append a '\n'. Without this,
  110. * the kernel can hide possible errors.
  111. */
  112. val_copy = strdup(val);
  113. if (!val_copy)
  114. goto out_close;
  115. val_copy[size] = '\n';
  116. if (write(fd, val_copy, size + 1) == size + 1)
  117. ret = 0;
  118. else
  119. pr_debug("write '%s' to tracing/%s failed: %s\n",
  120. val, name, str_error_r(errno, errbuf, sizeof(errbuf)));
  121. free(val_copy);
  122. out_close:
  123. close(fd);
  124. out:
  125. put_tracing_file(file);
  126. return ret;
  127. }
  128. static int write_tracing_file(const char *name, const char *val)
  129. {
  130. return __write_tracing_file(name, val, false);
  131. }
  132. static int append_tracing_file(const char *name, const char *val)
  133. {
  134. return __write_tracing_file(name, val, true);
  135. }
  136. static int read_tracing_file_to_stdout(const char *name)
  137. {
  138. char buf[4096];
  139. char *file;
  140. int fd;
  141. int ret = -1;
  142. file = get_tracing_file(name);
  143. if (!file) {
  144. pr_debug("cannot get tracing file: %s\n", name);
  145. return -1;
  146. }
  147. fd = open(file, O_RDONLY);
  148. if (fd < 0) {
  149. pr_debug("cannot open tracing file: %s: %s\n",
  150. name, str_error_r(errno, buf, sizeof(buf)));
  151. goto out;
  152. }
  153. /* read contents to stdout */
  154. while (true) {
  155. int n = read(fd, buf, sizeof(buf));
  156. if (n == 0)
  157. break;
  158. else if (n < 0)
  159. goto out_close;
  160. if (fwrite(buf, n, 1, stdout) != 1)
  161. goto out_close;
  162. }
  163. ret = 0;
  164. out_close:
  165. close(fd);
  166. out:
  167. put_tracing_file(file);
  168. return ret;
  169. }
  170. static int read_tracing_file_by_line(const char *name,
  171. void (*cb)(char *str, void *arg),
  172. void *cb_arg)
  173. {
  174. char *line = NULL;
  175. size_t len = 0;
  176. char *file;
  177. FILE *fp;
  178. file = get_tracing_file(name);
  179. if (!file) {
  180. pr_debug("cannot get tracing file: %s\n", name);
  181. return -1;
  182. }
  183. fp = fopen(file, "r");
  184. if (fp == NULL) {
  185. pr_debug("cannot open tracing file: %s\n", name);
  186. put_tracing_file(file);
  187. return -1;
  188. }
  189. while (getline(&line, &len, fp) != -1) {
  190. cb(line, cb_arg);
  191. }
  192. if (line)
  193. free(line);
  194. fclose(fp);
  195. put_tracing_file(file);
  196. return 0;
  197. }
  198. static int write_tracing_file_int(const char *name, int value)
  199. {
  200. char buf[16];
  201. snprintf(buf, sizeof(buf), "%d", value);
  202. if (write_tracing_file(name, buf) < 0)
  203. return -1;
  204. return 0;
  205. }
  206. static int write_tracing_option_file(const char *name, const char *val)
  207. {
  208. char *file;
  209. int ret;
  210. if (asprintf(&file, "options/%s", name) < 0)
  211. return -1;
  212. ret = __write_tracing_file(file, val, false);
  213. free(file);
  214. return ret;
  215. }
  216. static int reset_tracing_cpu(void);
  217. static void reset_tracing_filters(void);
  218. static void reset_tracing_options(struct perf_ftrace *ftrace __maybe_unused)
  219. {
  220. write_tracing_option_file("function-fork", "0");
  221. write_tracing_option_file("func_stack_trace", "0");
  222. write_tracing_option_file("sleep-time", "1");
  223. write_tracing_option_file("funcgraph-irqs", "1");
  224. write_tracing_option_file("funcgraph-proc", "0");
  225. write_tracing_option_file("funcgraph-abstime", "0");
  226. write_tracing_option_file("funcgraph-tail", "0");
  227. write_tracing_option_file("latency-format", "0");
  228. write_tracing_option_file("irq-info", "0");
  229. }
  230. static int reset_tracing_files(struct perf_ftrace *ftrace __maybe_unused)
  231. {
  232. if (write_tracing_file("tracing_on", "0") < 0)
  233. return -1;
  234. if (write_tracing_file("current_tracer", "nop") < 0)
  235. return -1;
  236. if (write_tracing_file("set_ftrace_pid", " ") < 0)
  237. return -1;
  238. if (reset_tracing_cpu() < 0)
  239. return -1;
  240. if (write_tracing_file("max_graph_depth", "0") < 0)
  241. return -1;
  242. if (write_tracing_file("tracing_thresh", "0") < 0)
  243. return -1;
  244. reset_tracing_filters();
  245. reset_tracing_options(ftrace);
  246. return 0;
  247. }
  248. static int set_tracing_pid(struct perf_ftrace *ftrace)
  249. {
  250. int i;
  251. char buf[16];
  252. if (target__has_cpu(&ftrace->target))
  253. return 0;
  254. for (i = 0; i < perf_thread_map__nr(ftrace->evlist->core.threads); i++) {
  255. scnprintf(buf, sizeof(buf), "%d",
  256. perf_thread_map__pid(ftrace->evlist->core.threads, i));
  257. if (append_tracing_file("set_ftrace_pid", buf) < 0)
  258. return -1;
  259. }
  260. return 0;
  261. }
  262. static int set_tracing_cpumask(struct perf_cpu_map *cpumap)
  263. {
  264. char *cpumask;
  265. size_t mask_size;
  266. int ret;
  267. int last_cpu;
  268. last_cpu = perf_cpu_map__cpu(cpumap, perf_cpu_map__nr(cpumap) - 1).cpu;
  269. mask_size = last_cpu / 4 + 2; /* one more byte for EOS */
  270. mask_size += last_cpu / 32; /* ',' is needed for every 32th cpus */
  271. cpumask = malloc(mask_size);
  272. if (cpumask == NULL) {
  273. pr_debug("failed to allocate cpu mask\n");
  274. return -1;
  275. }
  276. cpu_map__snprint_mask(cpumap, cpumask, mask_size);
  277. ret = write_tracing_file("tracing_cpumask", cpumask);
  278. free(cpumask);
  279. return ret;
  280. }
  281. static int set_tracing_cpu(struct perf_ftrace *ftrace)
  282. {
  283. struct perf_cpu_map *cpumap = ftrace->evlist->core.user_requested_cpus;
  284. if (!target__has_cpu(&ftrace->target))
  285. return 0;
  286. return set_tracing_cpumask(cpumap);
  287. }
  288. static int set_tracing_func_stack_trace(struct perf_ftrace *ftrace)
  289. {
  290. if (!ftrace->func_stack_trace)
  291. return 0;
  292. if (write_tracing_option_file("func_stack_trace", "1") < 0)
  293. return -1;
  294. return 0;
  295. }
  296. static int set_tracing_func_irqinfo(struct perf_ftrace *ftrace)
  297. {
  298. if (!ftrace->func_irq_info)
  299. return 0;
  300. if (write_tracing_option_file("irq-info", "1") < 0)
  301. return -1;
  302. return 0;
  303. }
  304. static int reset_tracing_cpu(void)
  305. {
  306. struct perf_cpu_map *cpumap = perf_cpu_map__new_online_cpus();
  307. int ret;
  308. ret = set_tracing_cpumask(cpumap);
  309. perf_cpu_map__put(cpumap);
  310. return ret;
  311. }
  312. static int __set_tracing_filter(const char *filter_file, struct list_head *funcs)
  313. {
  314. struct filter_entry *pos;
  315. list_for_each_entry(pos, funcs, list) {
  316. if (append_tracing_file(filter_file, pos->name) < 0)
  317. return -1;
  318. }
  319. return 0;
  320. }
  321. static int set_tracing_filters(struct perf_ftrace *ftrace)
  322. {
  323. int ret;
  324. ret = __set_tracing_filter("set_ftrace_filter", &ftrace->filters);
  325. if (ret < 0)
  326. return ret;
  327. ret = __set_tracing_filter("set_ftrace_notrace", &ftrace->notrace);
  328. if (ret < 0)
  329. return ret;
  330. ret = __set_tracing_filter("set_graph_function", &ftrace->graph_funcs);
  331. if (ret < 0)
  332. return ret;
  333. /* old kernels do not have this filter */
  334. __set_tracing_filter("set_graph_notrace", &ftrace->nograph_funcs);
  335. return ret;
  336. }
  337. static void reset_tracing_filters(void)
  338. {
  339. write_tracing_file("set_ftrace_filter", " ");
  340. write_tracing_file("set_ftrace_notrace", " ");
  341. write_tracing_file("set_graph_function", " ");
  342. write_tracing_file("set_graph_notrace", " ");
  343. }
  344. static int set_tracing_depth(struct perf_ftrace *ftrace)
  345. {
  346. if (ftrace->graph_depth == 0)
  347. return 0;
  348. if (ftrace->graph_depth < 0) {
  349. pr_err("invalid graph depth: %d\n", ftrace->graph_depth);
  350. return -1;
  351. }
  352. if (write_tracing_file_int("max_graph_depth", ftrace->graph_depth) < 0)
  353. return -1;
  354. return 0;
  355. }
  356. static int set_tracing_percpu_buffer_size(struct perf_ftrace *ftrace)
  357. {
  358. int ret;
  359. if (ftrace->percpu_buffer_size == 0)
  360. return 0;
  361. ret = write_tracing_file_int("buffer_size_kb",
  362. ftrace->percpu_buffer_size / 1024);
  363. if (ret < 0)
  364. return ret;
  365. return 0;
  366. }
  367. static int set_tracing_trace_inherit(struct perf_ftrace *ftrace)
  368. {
  369. if (!ftrace->inherit)
  370. return 0;
  371. if (write_tracing_option_file("function-fork", "1") < 0)
  372. return -1;
  373. return 0;
  374. }
  375. static int set_tracing_sleep_time(struct perf_ftrace *ftrace)
  376. {
  377. if (!ftrace->graph_nosleep_time)
  378. return 0;
  379. if (write_tracing_option_file("sleep-time", "0") < 0)
  380. return -1;
  381. return 0;
  382. }
  383. static int set_tracing_funcgraph_irqs(struct perf_ftrace *ftrace)
  384. {
  385. if (!ftrace->graph_noirqs)
  386. return 0;
  387. if (write_tracing_option_file("funcgraph-irqs", "0") < 0)
  388. return -1;
  389. return 0;
  390. }
  391. static int set_tracing_funcgraph_verbose(struct perf_ftrace *ftrace)
  392. {
  393. if (!ftrace->graph_verbose)
  394. return 0;
  395. if (write_tracing_option_file("funcgraph-proc", "1") < 0)
  396. return -1;
  397. if (write_tracing_option_file("funcgraph-abstime", "1") < 0)
  398. return -1;
  399. if (write_tracing_option_file("latency-format", "1") < 0)
  400. return -1;
  401. return 0;
  402. }
  403. static int set_tracing_funcgraph_tail(struct perf_ftrace *ftrace)
  404. {
  405. if (!ftrace->graph_tail)
  406. return 0;
  407. if (write_tracing_option_file("funcgraph-tail", "1") < 0)
  408. return -1;
  409. return 0;
  410. }
  411. static int set_tracing_thresh(struct perf_ftrace *ftrace)
  412. {
  413. int ret;
  414. if (ftrace->graph_thresh == 0)
  415. return 0;
  416. ret = write_tracing_file_int("tracing_thresh", ftrace->graph_thresh);
  417. if (ret < 0)
  418. return ret;
  419. return 0;
  420. }
  421. static int set_tracing_options(struct perf_ftrace *ftrace)
  422. {
  423. if (set_tracing_pid(ftrace) < 0) {
  424. pr_err("failed to set ftrace pid\n");
  425. return -1;
  426. }
  427. if (set_tracing_cpu(ftrace) < 0) {
  428. pr_err("failed to set tracing cpumask\n");
  429. return -1;
  430. }
  431. if (set_tracing_func_stack_trace(ftrace) < 0) {
  432. pr_err("failed to set tracing option func_stack_trace\n");
  433. return -1;
  434. }
  435. if (set_tracing_func_irqinfo(ftrace) < 0) {
  436. pr_err("failed to set tracing option irq-info\n");
  437. return -1;
  438. }
  439. if (set_tracing_filters(ftrace) < 0) {
  440. pr_err("failed to set tracing filters\n");
  441. return -1;
  442. }
  443. if (set_tracing_depth(ftrace) < 0) {
  444. pr_err("failed to set graph depth\n");
  445. return -1;
  446. }
  447. if (set_tracing_percpu_buffer_size(ftrace) < 0) {
  448. pr_err("failed to set tracing per-cpu buffer size\n");
  449. return -1;
  450. }
  451. if (set_tracing_trace_inherit(ftrace) < 0) {
  452. pr_err("failed to set tracing option function-fork\n");
  453. return -1;
  454. }
  455. if (set_tracing_sleep_time(ftrace) < 0) {
  456. pr_err("failed to set tracing option sleep-time\n");
  457. return -1;
  458. }
  459. if (set_tracing_funcgraph_irqs(ftrace) < 0) {
  460. pr_err("failed to set tracing option funcgraph-irqs\n");
  461. return -1;
  462. }
  463. if (set_tracing_funcgraph_verbose(ftrace) < 0) {
  464. pr_err("failed to set tracing option funcgraph-proc/funcgraph-abstime\n");
  465. return -1;
  466. }
  467. if (set_tracing_thresh(ftrace) < 0) {
  468. pr_err("failed to set tracing thresh\n");
  469. return -1;
  470. }
  471. if (set_tracing_funcgraph_tail(ftrace) < 0) {
  472. pr_err("failed to set tracing option funcgraph-tail\n");
  473. return -1;
  474. }
  475. return 0;
  476. }
  477. static void select_tracer(struct perf_ftrace *ftrace)
  478. {
  479. bool graph = !list_empty(&ftrace->graph_funcs) ||
  480. !list_empty(&ftrace->nograph_funcs);
  481. bool func = !list_empty(&ftrace->filters) ||
  482. !list_empty(&ftrace->notrace);
  483. /* The function_graph has priority over function tracer. */
  484. if (graph)
  485. ftrace->tracer = "function_graph";
  486. else if (func)
  487. ftrace->tracer = "function";
  488. /* Otherwise, the default tracer is used. */
  489. pr_debug("%s tracer is used\n", ftrace->tracer);
  490. }
  491. static int __cmd_ftrace(struct perf_ftrace *ftrace)
  492. {
  493. char *trace_file;
  494. int trace_fd;
  495. char buf[4096];
  496. struct pollfd pollfd = {
  497. .events = POLLIN,
  498. };
  499. select_tracer(ftrace);
  500. if (reset_tracing_files(ftrace) < 0) {
  501. pr_err("failed to reset ftrace\n");
  502. goto out;
  503. }
  504. /* reset ftrace buffer */
  505. if (write_tracing_file("trace", "0") < 0)
  506. goto out;
  507. if (set_tracing_options(ftrace) < 0)
  508. goto out_reset;
  509. if (write_tracing_file("current_tracer", ftrace->tracer) < 0) {
  510. pr_err("failed to set current_tracer to %s\n", ftrace->tracer);
  511. goto out_reset;
  512. }
  513. setup_pager();
  514. trace_file = get_tracing_file("trace_pipe");
  515. if (!trace_file) {
  516. pr_err("failed to open trace_pipe\n");
  517. goto out_reset;
  518. }
  519. trace_fd = open(trace_file, O_RDONLY);
  520. put_tracing_file(trace_file);
  521. if (trace_fd < 0) {
  522. pr_err("failed to open trace_pipe\n");
  523. goto out_reset;
  524. }
  525. fcntl(trace_fd, F_SETFL, O_NONBLOCK);
  526. pollfd.fd = trace_fd;
  527. /* display column headers */
  528. read_tracing_file_to_stdout("trace");
  529. if (!ftrace->target.initial_delay) {
  530. if (write_tracing_file("tracing_on", "1") < 0) {
  531. pr_err("can't enable tracing\n");
  532. goto out_close_fd;
  533. }
  534. }
  535. evlist__start_workload(ftrace->evlist);
  536. if (ftrace->target.initial_delay > 0) {
  537. usleep(ftrace->target.initial_delay * 1000);
  538. if (write_tracing_file("tracing_on", "1") < 0) {
  539. pr_err("can't enable tracing\n");
  540. goto out_close_fd;
  541. }
  542. }
  543. while (!done) {
  544. if (poll(&pollfd, 1, -1) < 0)
  545. break;
  546. if (pollfd.revents & POLLIN) {
  547. int n = read(trace_fd, buf, sizeof(buf));
  548. if (n < 0)
  549. break;
  550. if (fwrite(buf, n, 1, stdout) != 1)
  551. break;
  552. /* flush output since stdout is in full buffering mode due to pager */
  553. fflush(stdout);
  554. }
  555. }
  556. write_tracing_file("tracing_on", "0");
  557. if (workload_exec_errno) {
  558. const char *emsg = str_error_r(workload_exec_errno, buf, sizeof(buf));
  559. /* flush stdout first so below error msg appears at the end. */
  560. fflush(stdout);
  561. pr_err("workload failed: %s\n", emsg);
  562. goto out_close_fd;
  563. }
  564. /* read remaining buffer contents */
  565. while (true) {
  566. int n = read(trace_fd, buf, sizeof(buf));
  567. if (n <= 0)
  568. break;
  569. if (fwrite(buf, n, 1, stdout) != 1)
  570. break;
  571. }
  572. out_close_fd:
  573. close(trace_fd);
  574. out_reset:
  575. reset_tracing_files(ftrace);
  576. out:
  577. return (done && !workload_exec_errno) ? 0 : -1;
  578. }
  579. static void make_histogram(int buckets[], char *buf, size_t len, char *linebuf,
  580. bool use_nsec)
  581. {
  582. char *p, *q;
  583. char *unit;
  584. double num;
  585. int i;
  586. /* ensure NUL termination */
  587. buf[len] = '\0';
  588. /* handle data line by line */
  589. for (p = buf; (q = strchr(p, '\n')) != NULL; p = q + 1) {
  590. *q = '\0';
  591. /* move it to the line buffer */
  592. strcat(linebuf, p);
  593. /*
  594. * parse trace output to get function duration like in
  595. *
  596. * # tracer: function_graph
  597. * #
  598. * # CPU DURATION FUNCTION CALLS
  599. * # | | | | | | |
  600. * 1) + 10.291 us | do_filp_open();
  601. * 1) 4.889 us | do_filp_open();
  602. * 1) 6.086 us | do_filp_open();
  603. *
  604. */
  605. if (linebuf[0] == '#')
  606. goto next;
  607. /* ignore CPU */
  608. p = strchr(linebuf, ')');
  609. if (p == NULL)
  610. p = linebuf;
  611. while (*p && !isdigit(*p) && (*p != '|'))
  612. p++;
  613. /* no duration */
  614. if (*p == '\0' || *p == '|')
  615. goto next;
  616. num = strtod(p, &unit);
  617. if (!unit || strncmp(unit, " us", 3))
  618. goto next;
  619. if (use_nsec)
  620. num *= 1000;
  621. i = log2(num);
  622. if (i < 0)
  623. i = 0;
  624. if (i >= NUM_BUCKET)
  625. i = NUM_BUCKET - 1;
  626. buckets[i]++;
  627. next:
  628. /* empty the line buffer for the next output */
  629. linebuf[0] = '\0';
  630. }
  631. /* preserve any remaining output (before newline) */
  632. strcat(linebuf, p);
  633. }
  634. static void display_histogram(int buckets[], bool use_nsec)
  635. {
  636. int i;
  637. int total = 0;
  638. int bar_total = 46; /* to fit in 80 column */
  639. char bar[] = "###############################################";
  640. int bar_len;
  641. for (i = 0; i < NUM_BUCKET; i++)
  642. total += buckets[i];
  643. if (total == 0) {
  644. printf("No data found\n");
  645. return;
  646. }
  647. printf("# %14s | %10s | %-*s |\n",
  648. " DURATION ", "COUNT", bar_total, "GRAPH");
  649. bar_len = buckets[0] * bar_total / total;
  650. printf(" %4d - %-4d %s | %10d | %.*s%*s |\n",
  651. 0, 1, use_nsec ? "ns" : "us", buckets[0], bar_len, bar, bar_total - bar_len, "");
  652. for (i = 1; i < NUM_BUCKET - 1; i++) {
  653. int start = (1 << (i - 1));
  654. int stop = 1 << i;
  655. const char *unit = use_nsec ? "ns" : "us";
  656. if (start >= 1024) {
  657. start >>= 10;
  658. stop >>= 10;
  659. unit = use_nsec ? "us" : "ms";
  660. }
  661. bar_len = buckets[i] * bar_total / total;
  662. printf(" %4d - %-4d %s | %10d | %.*s%*s |\n",
  663. start, stop, unit, buckets[i], bar_len, bar,
  664. bar_total - bar_len, "");
  665. }
  666. bar_len = buckets[NUM_BUCKET - 1] * bar_total / total;
  667. printf(" %4d - %-4s %s | %10d | %.*s%*s |\n",
  668. 1, "...", use_nsec ? "ms" : " s", buckets[NUM_BUCKET - 1],
  669. bar_len, bar, bar_total - bar_len, "");
  670. }
  671. static int prepare_func_latency(struct perf_ftrace *ftrace)
  672. {
  673. char *trace_file;
  674. int fd;
  675. if (ftrace->target.use_bpf)
  676. return perf_ftrace__latency_prepare_bpf(ftrace);
  677. if (reset_tracing_files(ftrace) < 0) {
  678. pr_err("failed to reset ftrace\n");
  679. return -1;
  680. }
  681. /* reset ftrace buffer */
  682. if (write_tracing_file("trace", "0") < 0)
  683. return -1;
  684. if (set_tracing_options(ftrace) < 0)
  685. return -1;
  686. /* force to use the function_graph tracer to track duration */
  687. if (write_tracing_file("current_tracer", "function_graph") < 0) {
  688. pr_err("failed to set current_tracer to function_graph\n");
  689. return -1;
  690. }
  691. trace_file = get_tracing_file("trace_pipe");
  692. if (!trace_file) {
  693. pr_err("failed to open trace_pipe\n");
  694. return -1;
  695. }
  696. fd = open(trace_file, O_RDONLY);
  697. if (fd < 0)
  698. pr_err("failed to open trace_pipe\n");
  699. put_tracing_file(trace_file);
  700. return fd;
  701. }
  702. static int start_func_latency(struct perf_ftrace *ftrace)
  703. {
  704. if (ftrace->target.use_bpf)
  705. return perf_ftrace__latency_start_bpf(ftrace);
  706. if (write_tracing_file("tracing_on", "1") < 0) {
  707. pr_err("can't enable tracing\n");
  708. return -1;
  709. }
  710. return 0;
  711. }
  712. static int stop_func_latency(struct perf_ftrace *ftrace)
  713. {
  714. if (ftrace->target.use_bpf)
  715. return perf_ftrace__latency_stop_bpf(ftrace);
  716. write_tracing_file("tracing_on", "0");
  717. return 0;
  718. }
  719. static int read_func_latency(struct perf_ftrace *ftrace, int buckets[])
  720. {
  721. if (ftrace->target.use_bpf)
  722. return perf_ftrace__latency_read_bpf(ftrace, buckets);
  723. return 0;
  724. }
  725. static int cleanup_func_latency(struct perf_ftrace *ftrace)
  726. {
  727. if (ftrace->target.use_bpf)
  728. return perf_ftrace__latency_cleanup_bpf(ftrace);
  729. reset_tracing_files(ftrace);
  730. return 0;
  731. }
  732. static int __cmd_latency(struct perf_ftrace *ftrace)
  733. {
  734. int trace_fd;
  735. char buf[4096];
  736. char line[256];
  737. struct pollfd pollfd = {
  738. .events = POLLIN,
  739. };
  740. int buckets[NUM_BUCKET] = { };
  741. trace_fd = prepare_func_latency(ftrace);
  742. if (trace_fd < 0)
  743. goto out;
  744. fcntl(trace_fd, F_SETFL, O_NONBLOCK);
  745. pollfd.fd = trace_fd;
  746. if (start_func_latency(ftrace) < 0)
  747. goto out;
  748. evlist__start_workload(ftrace->evlist);
  749. line[0] = '\0';
  750. while (!done) {
  751. if (poll(&pollfd, 1, -1) < 0)
  752. break;
  753. if (pollfd.revents & POLLIN) {
  754. int n = read(trace_fd, buf, sizeof(buf) - 1);
  755. if (n < 0)
  756. break;
  757. make_histogram(buckets, buf, n, line, ftrace->use_nsec);
  758. }
  759. }
  760. stop_func_latency(ftrace);
  761. if (workload_exec_errno) {
  762. const char *emsg = str_error_r(workload_exec_errno, buf, sizeof(buf));
  763. pr_err("workload failed: %s\n", emsg);
  764. goto out;
  765. }
  766. /* read remaining buffer contents */
  767. while (!ftrace->target.use_bpf) {
  768. int n = read(trace_fd, buf, sizeof(buf) - 1);
  769. if (n <= 0)
  770. break;
  771. make_histogram(buckets, buf, n, line, ftrace->use_nsec);
  772. }
  773. read_func_latency(ftrace, buckets);
  774. display_histogram(buckets, ftrace->use_nsec);
  775. out:
  776. close(trace_fd);
  777. cleanup_func_latency(ftrace);
  778. return (done && !workload_exec_errno) ? 0 : -1;
  779. }
  780. static size_t profile_hash(long func, void *ctx __maybe_unused)
  781. {
  782. return str_hash((char *)func);
  783. }
  784. static bool profile_equal(long func1, long func2, void *ctx __maybe_unused)
  785. {
  786. return !strcmp((char *)func1, (char *)func2);
  787. }
  788. static int prepare_func_profile(struct perf_ftrace *ftrace)
  789. {
  790. ftrace->tracer = "function_graph";
  791. ftrace->graph_tail = 1;
  792. ftrace->profile_hash = hashmap__new(profile_hash, profile_equal, NULL);
  793. if (ftrace->profile_hash == NULL)
  794. return -ENOMEM;
  795. return 0;
  796. }
  797. /* This is saved in a hashmap keyed by the function name */
  798. struct ftrace_profile_data {
  799. struct stats st;
  800. };
  801. static int add_func_duration(struct perf_ftrace *ftrace, char *func, double time_ns)
  802. {
  803. struct ftrace_profile_data *prof = NULL;
  804. if (!hashmap__find(ftrace->profile_hash, func, &prof)) {
  805. char *key = strdup(func);
  806. if (key == NULL)
  807. return -ENOMEM;
  808. prof = zalloc(sizeof(*prof));
  809. if (prof == NULL) {
  810. free(key);
  811. return -ENOMEM;
  812. }
  813. init_stats(&prof->st);
  814. hashmap__add(ftrace->profile_hash, key, prof);
  815. }
  816. update_stats(&prof->st, time_ns);
  817. return 0;
  818. }
  819. /*
  820. * The ftrace function_graph text output normally looks like below:
  821. *
  822. * CPU DURATION FUNCTION
  823. *
  824. * 0) | syscall_trace_enter.isra.0() {
  825. * 0) | __audit_syscall_entry() {
  826. * 0) | auditd_test_task() {
  827. * 0) 0.271 us | __rcu_read_lock();
  828. * 0) 0.275 us | __rcu_read_unlock();
  829. * 0) 1.254 us | } /\* auditd_test_task *\/
  830. * 0) 0.279 us | ktime_get_coarse_real_ts64();
  831. * 0) 2.227 us | } /\* __audit_syscall_entry *\/
  832. * 0) 2.713 us | } /\* syscall_trace_enter.isra.0 *\/
  833. *
  834. * Parse the line and get the duration and function name.
  835. */
  836. static int parse_func_duration(struct perf_ftrace *ftrace, char *line, size_t len)
  837. {
  838. char *p;
  839. char *func;
  840. double duration;
  841. /* skip CPU */
  842. p = strchr(line, ')');
  843. if (p == NULL)
  844. return 0;
  845. /* get duration */
  846. p = skip_spaces(p + 1);
  847. /* no duration? */
  848. if (p == NULL || *p == '|')
  849. return 0;
  850. /* skip markers like '*' or '!' for longer than ms */
  851. if (!isdigit(*p))
  852. p++;
  853. duration = strtod(p, &p);
  854. if (strncmp(p, " us", 3)) {
  855. pr_debug("non-usec time found.. ignoring\n");
  856. return 0;
  857. }
  858. /*
  859. * profile stat keeps the max and min values as integer,
  860. * convert to nsec time so that we can have accurate max.
  861. */
  862. duration *= 1000;
  863. /* skip to the pipe */
  864. while (p < line + len && *p != '|')
  865. p++;
  866. if (*p++ != '|')
  867. return -EINVAL;
  868. /* get function name */
  869. func = skip_spaces(p);
  870. /* skip the closing bracket and the start of comment */
  871. if (*func == '}')
  872. func += 5;
  873. /* remove semi-colon or end of comment at the end */
  874. p = line + len - 1;
  875. while (!isalnum(*p) && *p != ']') {
  876. *p = '\0';
  877. --p;
  878. }
  879. return add_func_duration(ftrace, func, duration);
  880. }
  881. enum perf_ftrace_profile_sort_key {
  882. PFP_SORT_TOTAL = 0,
  883. PFP_SORT_AVG,
  884. PFP_SORT_MAX,
  885. PFP_SORT_COUNT,
  886. PFP_SORT_NAME,
  887. };
  888. static enum perf_ftrace_profile_sort_key profile_sort = PFP_SORT_TOTAL;
  889. static int cmp_profile_data(const void *a, const void *b)
  890. {
  891. const struct hashmap_entry *e1 = *(const struct hashmap_entry **)a;
  892. const struct hashmap_entry *e2 = *(const struct hashmap_entry **)b;
  893. struct ftrace_profile_data *p1 = e1->pvalue;
  894. struct ftrace_profile_data *p2 = e2->pvalue;
  895. double v1, v2;
  896. switch (profile_sort) {
  897. case PFP_SORT_NAME:
  898. return strcmp(e1->pkey, e2->pkey);
  899. case PFP_SORT_AVG:
  900. v1 = p1->st.mean;
  901. v2 = p2->st.mean;
  902. break;
  903. case PFP_SORT_MAX:
  904. v1 = p1->st.max;
  905. v2 = p2->st.max;
  906. break;
  907. case PFP_SORT_COUNT:
  908. v1 = p1->st.n;
  909. v2 = p2->st.n;
  910. break;
  911. case PFP_SORT_TOTAL:
  912. default:
  913. v1 = p1->st.n * p1->st.mean;
  914. v2 = p2->st.n * p2->st.mean;
  915. break;
  916. }
  917. if (v1 > v2)
  918. return -1;
  919. if (v1 < v2)
  920. return 1;
  921. return 0;
  922. }
  923. static void print_profile_result(struct perf_ftrace *ftrace)
  924. {
  925. struct hashmap_entry *entry, **profile;
  926. size_t i, nr, bkt;
  927. nr = hashmap__size(ftrace->profile_hash);
  928. if (nr == 0)
  929. return;
  930. profile = calloc(nr, sizeof(*profile));
  931. if (profile == NULL) {
  932. pr_err("failed to allocate memory for the result\n");
  933. return;
  934. }
  935. i = 0;
  936. hashmap__for_each_entry(ftrace->profile_hash, entry, bkt)
  937. profile[i++] = entry;
  938. assert(i == nr);
  939. //cmp_profile_data(profile[0], profile[1]);
  940. qsort(profile, nr, sizeof(*profile), cmp_profile_data);
  941. printf("# %10s %10s %10s %10s %s\n",
  942. "Total (us)", "Avg (us)", "Max (us)", "Count", "Function");
  943. for (i = 0; i < nr; i++) {
  944. const char *name = profile[i]->pkey;
  945. struct ftrace_profile_data *p = profile[i]->pvalue;
  946. printf("%12.3f %10.3f %6"PRIu64".%03"PRIu64" %10.0f %s\n",
  947. p->st.n * p->st.mean / 1000, p->st.mean / 1000,
  948. p->st.max / 1000, p->st.max % 1000, p->st.n, name);
  949. }
  950. free(profile);
  951. hashmap__for_each_entry(ftrace->profile_hash, entry, bkt) {
  952. free((char *)entry->pkey);
  953. free(entry->pvalue);
  954. }
  955. hashmap__free(ftrace->profile_hash);
  956. ftrace->profile_hash = NULL;
  957. }
  958. static int __cmd_profile(struct perf_ftrace *ftrace)
  959. {
  960. char *trace_file;
  961. int trace_fd;
  962. char buf[4096];
  963. struct io io;
  964. char *line = NULL;
  965. size_t line_len = 0;
  966. if (prepare_func_profile(ftrace) < 0) {
  967. pr_err("failed to prepare func profiler\n");
  968. goto out;
  969. }
  970. if (reset_tracing_files(ftrace) < 0) {
  971. pr_err("failed to reset ftrace\n");
  972. goto out;
  973. }
  974. /* reset ftrace buffer */
  975. if (write_tracing_file("trace", "0") < 0)
  976. goto out;
  977. if (set_tracing_options(ftrace) < 0)
  978. return -1;
  979. if (write_tracing_file("current_tracer", ftrace->tracer) < 0) {
  980. pr_err("failed to set current_tracer to %s\n", ftrace->tracer);
  981. goto out_reset;
  982. }
  983. setup_pager();
  984. trace_file = get_tracing_file("trace_pipe");
  985. if (!trace_file) {
  986. pr_err("failed to open trace_pipe\n");
  987. goto out_reset;
  988. }
  989. trace_fd = open(trace_file, O_RDONLY);
  990. put_tracing_file(trace_file);
  991. if (trace_fd < 0) {
  992. pr_err("failed to open trace_pipe\n");
  993. goto out_reset;
  994. }
  995. fcntl(trace_fd, F_SETFL, O_NONBLOCK);
  996. if (write_tracing_file("tracing_on", "1") < 0) {
  997. pr_err("can't enable tracing\n");
  998. goto out_close_fd;
  999. }
  1000. evlist__start_workload(ftrace->evlist);
  1001. io__init(&io, trace_fd, buf, sizeof(buf));
  1002. io.timeout_ms = -1;
  1003. while (!done && !io.eof) {
  1004. if (io__getline(&io, &line, &line_len) < 0)
  1005. break;
  1006. if (parse_func_duration(ftrace, line, line_len) < 0)
  1007. break;
  1008. }
  1009. write_tracing_file("tracing_on", "0");
  1010. if (workload_exec_errno) {
  1011. const char *emsg = str_error_r(workload_exec_errno, buf, sizeof(buf));
  1012. /* flush stdout first so below error msg appears at the end. */
  1013. fflush(stdout);
  1014. pr_err("workload failed: %s\n", emsg);
  1015. goto out_free_line;
  1016. }
  1017. /* read remaining buffer contents */
  1018. io.timeout_ms = 0;
  1019. while (!io.eof) {
  1020. if (io__getline(&io, &line, &line_len) < 0)
  1021. break;
  1022. if (parse_func_duration(ftrace, line, line_len) < 0)
  1023. break;
  1024. }
  1025. print_profile_result(ftrace);
  1026. out_free_line:
  1027. free(line);
  1028. out_close_fd:
  1029. close(trace_fd);
  1030. out_reset:
  1031. reset_tracing_files(ftrace);
  1032. out:
  1033. return (done && !workload_exec_errno) ? 0 : -1;
  1034. }
  1035. static int perf_ftrace_config(const char *var, const char *value, void *cb)
  1036. {
  1037. struct perf_ftrace *ftrace = cb;
  1038. if (!strstarts(var, "ftrace."))
  1039. return 0;
  1040. if (strcmp(var, "ftrace.tracer"))
  1041. return -1;
  1042. if (!strcmp(value, "function_graph") ||
  1043. !strcmp(value, "function")) {
  1044. ftrace->tracer = value;
  1045. return 0;
  1046. }
  1047. pr_err("Please select \"function_graph\" (default) or \"function\"\n");
  1048. return -1;
  1049. }
  1050. static void list_function_cb(char *str, void *arg)
  1051. {
  1052. struct strfilter *filter = (struct strfilter *)arg;
  1053. if (strfilter__compare(filter, str))
  1054. printf("%s", str);
  1055. }
  1056. static int opt_list_avail_functions(const struct option *opt __maybe_unused,
  1057. const char *str, int unset)
  1058. {
  1059. struct strfilter *filter;
  1060. const char *err = NULL;
  1061. int ret;
  1062. if (unset || !str)
  1063. return -1;
  1064. filter = strfilter__new(str, &err);
  1065. if (!filter)
  1066. return err ? -EINVAL : -ENOMEM;
  1067. ret = strfilter__or(filter, str, &err);
  1068. if (ret == -EINVAL) {
  1069. pr_err("Filter parse error at %td.\n", err - str + 1);
  1070. pr_err("Source: \"%s\"\n", str);
  1071. pr_err(" %*c\n", (int)(err - str + 1), '^');
  1072. strfilter__delete(filter);
  1073. return ret;
  1074. }
  1075. ret = read_tracing_file_by_line("available_filter_functions",
  1076. list_function_cb, filter);
  1077. strfilter__delete(filter);
  1078. if (ret < 0)
  1079. return ret;
  1080. exit(0);
  1081. }
  1082. static int parse_filter_func(const struct option *opt, const char *str,
  1083. int unset __maybe_unused)
  1084. {
  1085. struct list_head *head = opt->value;
  1086. struct filter_entry *entry;
  1087. entry = malloc(sizeof(*entry) + strlen(str) + 1);
  1088. if (entry == NULL)
  1089. return -ENOMEM;
  1090. strcpy(entry->name, str);
  1091. list_add_tail(&entry->list, head);
  1092. return 0;
  1093. }
  1094. static void delete_filter_func(struct list_head *head)
  1095. {
  1096. struct filter_entry *pos, *tmp;
  1097. list_for_each_entry_safe(pos, tmp, head, list) {
  1098. list_del_init(&pos->list);
  1099. free(pos);
  1100. }
  1101. }
  1102. static int parse_buffer_size(const struct option *opt,
  1103. const char *str, int unset)
  1104. {
  1105. unsigned long *s = (unsigned long *)opt->value;
  1106. static struct parse_tag tags_size[] = {
  1107. { .tag = 'B', .mult = 1 },
  1108. { .tag = 'K', .mult = 1 << 10 },
  1109. { .tag = 'M', .mult = 1 << 20 },
  1110. { .tag = 'G', .mult = 1 << 30 },
  1111. { .tag = 0 },
  1112. };
  1113. unsigned long val;
  1114. if (unset) {
  1115. *s = 0;
  1116. return 0;
  1117. }
  1118. val = parse_tag_value(str, tags_size);
  1119. if (val != (unsigned long) -1) {
  1120. if (val < 1024) {
  1121. pr_err("buffer size too small, must larger than 1KB.");
  1122. return -1;
  1123. }
  1124. *s = val;
  1125. return 0;
  1126. }
  1127. return -1;
  1128. }
  1129. static int parse_func_tracer_opts(const struct option *opt,
  1130. const char *str, int unset)
  1131. {
  1132. int ret;
  1133. struct perf_ftrace *ftrace = (struct perf_ftrace *) opt->value;
  1134. struct sublevel_option func_tracer_opts[] = {
  1135. { .name = "call-graph", .value_ptr = &ftrace->func_stack_trace },
  1136. { .name = "irq-info", .value_ptr = &ftrace->func_irq_info },
  1137. { .name = NULL, }
  1138. };
  1139. if (unset)
  1140. return 0;
  1141. ret = perf_parse_sublevel_options(str, func_tracer_opts);
  1142. if (ret)
  1143. return ret;
  1144. return 0;
  1145. }
  1146. static int parse_graph_tracer_opts(const struct option *opt,
  1147. const char *str, int unset)
  1148. {
  1149. int ret;
  1150. struct perf_ftrace *ftrace = (struct perf_ftrace *) opt->value;
  1151. struct sublevel_option graph_tracer_opts[] = {
  1152. { .name = "nosleep-time", .value_ptr = &ftrace->graph_nosleep_time },
  1153. { .name = "noirqs", .value_ptr = &ftrace->graph_noirqs },
  1154. { .name = "verbose", .value_ptr = &ftrace->graph_verbose },
  1155. { .name = "thresh", .value_ptr = &ftrace->graph_thresh },
  1156. { .name = "depth", .value_ptr = &ftrace->graph_depth },
  1157. { .name = "tail", .value_ptr = &ftrace->graph_tail },
  1158. { .name = NULL, }
  1159. };
  1160. if (unset)
  1161. return 0;
  1162. ret = perf_parse_sublevel_options(str, graph_tracer_opts);
  1163. if (ret)
  1164. return ret;
  1165. return 0;
  1166. }
  1167. static int parse_sort_key(const struct option *opt, const char *str, int unset)
  1168. {
  1169. enum perf_ftrace_profile_sort_key *key = (void *)opt->value;
  1170. if (unset)
  1171. return 0;
  1172. if (!strcmp(str, "total"))
  1173. *key = PFP_SORT_TOTAL;
  1174. else if (!strcmp(str, "avg"))
  1175. *key = PFP_SORT_AVG;
  1176. else if (!strcmp(str, "max"))
  1177. *key = PFP_SORT_MAX;
  1178. else if (!strcmp(str, "count"))
  1179. *key = PFP_SORT_COUNT;
  1180. else if (!strcmp(str, "name"))
  1181. *key = PFP_SORT_NAME;
  1182. else {
  1183. pr_err("Unknown sort key: %s\n", str);
  1184. return -1;
  1185. }
  1186. return 0;
  1187. }
  1188. enum perf_ftrace_subcommand {
  1189. PERF_FTRACE_NONE,
  1190. PERF_FTRACE_TRACE,
  1191. PERF_FTRACE_LATENCY,
  1192. PERF_FTRACE_PROFILE,
  1193. };
  1194. int cmd_ftrace(int argc, const char **argv)
  1195. {
  1196. int ret;
  1197. int (*cmd_func)(struct perf_ftrace *) = NULL;
  1198. struct perf_ftrace ftrace = {
  1199. .tracer = DEFAULT_TRACER,
  1200. .target = { .uid = UINT_MAX, },
  1201. };
  1202. const struct option common_options[] = {
  1203. OPT_STRING('p', "pid", &ftrace.target.pid, "pid",
  1204. "Trace on existing process id"),
  1205. /* TODO: Add short option -t after -t/--tracer can be removed. */
  1206. OPT_STRING(0, "tid", &ftrace.target.tid, "tid",
  1207. "Trace on existing thread id (exclusive to --pid)"),
  1208. OPT_INCR('v', "verbose", &verbose,
  1209. "Be more verbose"),
  1210. OPT_BOOLEAN('a', "all-cpus", &ftrace.target.system_wide,
  1211. "System-wide collection from all CPUs"),
  1212. OPT_STRING('C', "cpu", &ftrace.target.cpu_list, "cpu",
  1213. "List of cpus to monitor"),
  1214. OPT_END()
  1215. };
  1216. const struct option ftrace_options[] = {
  1217. OPT_STRING('t', "tracer", &ftrace.tracer, "tracer",
  1218. "Tracer to use: function_graph(default) or function"),
  1219. OPT_CALLBACK_DEFAULT('F', "funcs", NULL, "[FILTER]",
  1220. "Show available functions to filter",
  1221. opt_list_avail_functions, "*"),
  1222. OPT_CALLBACK('T', "trace-funcs", &ftrace.filters, "func",
  1223. "Trace given functions using function tracer",
  1224. parse_filter_func),
  1225. OPT_CALLBACK('N', "notrace-funcs", &ftrace.notrace, "func",
  1226. "Do not trace given functions", parse_filter_func),
  1227. OPT_CALLBACK(0, "func-opts", &ftrace, "options",
  1228. "Function tracer options, available options: call-graph,irq-info",
  1229. parse_func_tracer_opts),
  1230. OPT_CALLBACK('G', "graph-funcs", &ftrace.graph_funcs, "func",
  1231. "Trace given functions using function_graph tracer",
  1232. parse_filter_func),
  1233. OPT_CALLBACK('g', "nograph-funcs", &ftrace.nograph_funcs, "func",
  1234. "Set nograph filter on given functions", parse_filter_func),
  1235. OPT_CALLBACK(0, "graph-opts", &ftrace, "options",
  1236. "Graph tracer options, available options: nosleep-time,noirqs,verbose,thresh=<n>,depth=<n>",
  1237. parse_graph_tracer_opts),
  1238. OPT_CALLBACK('m', "buffer-size", &ftrace.percpu_buffer_size, "size",
  1239. "Size of per cpu buffer, needs to use a B, K, M or G suffix.", parse_buffer_size),
  1240. OPT_BOOLEAN(0, "inherit", &ftrace.inherit,
  1241. "Trace children processes"),
  1242. OPT_INTEGER('D', "delay", &ftrace.target.initial_delay,
  1243. "Number of milliseconds to wait before starting tracing after program start"),
  1244. OPT_PARENT(common_options),
  1245. };
  1246. const struct option latency_options[] = {
  1247. OPT_CALLBACK('T', "trace-funcs", &ftrace.filters, "func",
  1248. "Show latency of given function", parse_filter_func),
  1249. #ifdef HAVE_BPF_SKEL
  1250. OPT_BOOLEAN('b', "use-bpf", &ftrace.target.use_bpf,
  1251. "Use BPF to measure function latency"),
  1252. #endif
  1253. OPT_BOOLEAN('n', "use-nsec", &ftrace.use_nsec,
  1254. "Use nano-second histogram"),
  1255. OPT_PARENT(common_options),
  1256. };
  1257. const struct option profile_options[] = {
  1258. OPT_CALLBACK('T', "trace-funcs", &ftrace.filters, "func",
  1259. "Trace given functions using function tracer",
  1260. parse_filter_func),
  1261. OPT_CALLBACK('N', "notrace-funcs", &ftrace.notrace, "func",
  1262. "Do not trace given functions", parse_filter_func),
  1263. OPT_CALLBACK('G', "graph-funcs", &ftrace.graph_funcs, "func",
  1264. "Trace given functions using function_graph tracer",
  1265. parse_filter_func),
  1266. OPT_CALLBACK('g', "nograph-funcs", &ftrace.nograph_funcs, "func",
  1267. "Set nograph filter on given functions", parse_filter_func),
  1268. OPT_CALLBACK('m', "buffer-size", &ftrace.percpu_buffer_size, "size",
  1269. "Size of per cpu buffer, needs to use a B, K, M or G suffix.", parse_buffer_size),
  1270. OPT_CALLBACK('s', "sort", &profile_sort, "key",
  1271. "Sort result by key: total (default), avg, max, count, name.",
  1272. parse_sort_key),
  1273. OPT_PARENT(common_options),
  1274. };
  1275. const struct option *options = ftrace_options;
  1276. const char * const ftrace_usage[] = {
  1277. "perf ftrace [<options>] [<command>]",
  1278. "perf ftrace [<options>] -- [<command>] [<options>]",
  1279. "perf ftrace {trace|latency|profile} [<options>] [<command>]",
  1280. "perf ftrace {trace|latency|profile} [<options>] -- [<command>] [<options>]",
  1281. NULL
  1282. };
  1283. enum perf_ftrace_subcommand subcmd = PERF_FTRACE_NONE;
  1284. INIT_LIST_HEAD(&ftrace.filters);
  1285. INIT_LIST_HEAD(&ftrace.notrace);
  1286. INIT_LIST_HEAD(&ftrace.graph_funcs);
  1287. INIT_LIST_HEAD(&ftrace.nograph_funcs);
  1288. signal(SIGINT, sig_handler);
  1289. signal(SIGUSR1, sig_handler);
  1290. signal(SIGCHLD, sig_handler);
  1291. signal(SIGPIPE, sig_handler);
  1292. if (!check_ftrace_capable())
  1293. return -1;
  1294. if (!is_ftrace_supported()) {
  1295. pr_err("ftrace is not supported on this system\n");
  1296. return -ENOTSUP;
  1297. }
  1298. ret = perf_config(perf_ftrace_config, &ftrace);
  1299. if (ret < 0)
  1300. return -1;
  1301. if (argc > 1) {
  1302. if (!strcmp(argv[1], "trace")) {
  1303. subcmd = PERF_FTRACE_TRACE;
  1304. } else if (!strcmp(argv[1], "latency")) {
  1305. subcmd = PERF_FTRACE_LATENCY;
  1306. options = latency_options;
  1307. } else if (!strcmp(argv[1], "profile")) {
  1308. subcmd = PERF_FTRACE_PROFILE;
  1309. options = profile_options;
  1310. }
  1311. if (subcmd != PERF_FTRACE_NONE) {
  1312. argc--;
  1313. argv++;
  1314. }
  1315. }
  1316. /* for backward compatibility */
  1317. if (subcmd == PERF_FTRACE_NONE)
  1318. subcmd = PERF_FTRACE_TRACE;
  1319. argc = parse_options(argc, argv, options, ftrace_usage,
  1320. PARSE_OPT_STOP_AT_NON_OPTION);
  1321. if (argc < 0) {
  1322. ret = -EINVAL;
  1323. goto out_delete_filters;
  1324. }
  1325. /* Make system wide (-a) the default target. */
  1326. if (!argc && target__none(&ftrace.target))
  1327. ftrace.target.system_wide = true;
  1328. switch (subcmd) {
  1329. case PERF_FTRACE_TRACE:
  1330. cmd_func = __cmd_ftrace;
  1331. break;
  1332. case PERF_FTRACE_LATENCY:
  1333. if (list_empty(&ftrace.filters)) {
  1334. pr_err("Should provide a function to measure\n");
  1335. parse_options_usage(ftrace_usage, options, "T", 1);
  1336. ret = -EINVAL;
  1337. goto out_delete_filters;
  1338. }
  1339. cmd_func = __cmd_latency;
  1340. break;
  1341. case PERF_FTRACE_PROFILE:
  1342. cmd_func = __cmd_profile;
  1343. break;
  1344. case PERF_FTRACE_NONE:
  1345. default:
  1346. pr_err("Invalid subcommand\n");
  1347. ret = -EINVAL;
  1348. goto out_delete_filters;
  1349. }
  1350. ret = target__validate(&ftrace.target);
  1351. if (ret) {
  1352. char errbuf[512];
  1353. target__strerror(&ftrace.target, ret, errbuf, 512);
  1354. pr_err("%s\n", errbuf);
  1355. goto out_delete_filters;
  1356. }
  1357. ftrace.evlist = evlist__new();
  1358. if (ftrace.evlist == NULL) {
  1359. ret = -ENOMEM;
  1360. goto out_delete_filters;
  1361. }
  1362. ret = evlist__create_maps(ftrace.evlist, &ftrace.target);
  1363. if (ret < 0)
  1364. goto out_delete_evlist;
  1365. if (argc) {
  1366. ret = evlist__prepare_workload(ftrace.evlist, &ftrace.target,
  1367. argv, false,
  1368. ftrace__workload_exec_failed_signal);
  1369. if (ret < 0)
  1370. goto out_delete_evlist;
  1371. }
  1372. ret = cmd_func(&ftrace);
  1373. out_delete_evlist:
  1374. evlist__delete(ftrace.evlist);
  1375. out_delete_filters:
  1376. delete_filter_func(&ftrace.filters);
  1377. delete_filter_func(&ftrace.notrace);
  1378. delete_filter_func(&ftrace.graph_funcs);
  1379. delete_filter_func(&ftrace.nograph_funcs);
  1380. return ret;
  1381. }