db-export.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * db-export.c: Support for exporting data suitable for import to a database
  4. * Copyright (c) 2014, Intel Corporation.
  5. */
  6. #include <errno.h>
  7. #include <stdlib.h>
  8. #include "dso.h"
  9. #include "evsel.h"
  10. #include "machine.h"
  11. #include "thread.h"
  12. #include "comm.h"
  13. #include "symbol.h"
  14. #include "map.h"
  15. #include "event.h"
  16. #include "thread-stack.h"
  17. #include "callchain.h"
  18. #include "call-path.h"
  19. #include "db-export.h"
  20. #include <linux/zalloc.h>
  21. int db_export__init(struct db_export *dbe)
  22. {
  23. memset(dbe, 0, sizeof(struct db_export));
  24. return 0;
  25. }
  26. void db_export__exit(struct db_export *dbe)
  27. {
  28. call_return_processor__free(dbe->crp);
  29. dbe->crp = NULL;
  30. }
  31. int db_export__evsel(struct db_export *dbe, struct evsel *evsel)
  32. {
  33. if (evsel->db_id)
  34. return 0;
  35. evsel->db_id = ++dbe->evsel_last_db_id;
  36. if (dbe->export_evsel)
  37. return dbe->export_evsel(dbe, evsel);
  38. return 0;
  39. }
  40. int db_export__machine(struct db_export *dbe, struct machine *machine)
  41. {
  42. if (machine->db_id)
  43. return 0;
  44. machine->db_id = ++dbe->machine_last_db_id;
  45. if (dbe->export_machine)
  46. return dbe->export_machine(dbe, machine);
  47. return 0;
  48. }
  49. int db_export__thread(struct db_export *dbe, struct thread *thread,
  50. struct machine *machine, struct thread *main_thread)
  51. {
  52. u64 main_thread_db_id = 0;
  53. if (thread->db_id)
  54. return 0;
  55. thread->db_id = ++dbe->thread_last_db_id;
  56. if (main_thread)
  57. main_thread_db_id = main_thread->db_id;
  58. if (dbe->export_thread)
  59. return dbe->export_thread(dbe, thread, main_thread_db_id,
  60. machine);
  61. return 0;
  62. }
  63. static int __db_export__comm(struct db_export *dbe, struct comm *comm,
  64. struct thread *thread)
  65. {
  66. comm->db_id = ++dbe->comm_last_db_id;
  67. if (dbe->export_comm)
  68. return dbe->export_comm(dbe, comm, thread);
  69. return 0;
  70. }
  71. int db_export__comm(struct db_export *dbe, struct comm *comm,
  72. struct thread *thread)
  73. {
  74. if (comm->db_id)
  75. return 0;
  76. return __db_export__comm(dbe, comm, thread);
  77. }
  78. /*
  79. * Export the "exec" comm. The "exec" comm is the program / application command
  80. * name at the time it first executes. It is used to group threads for the same
  81. * program. Note that the main thread pid (or thread group id tgid) cannot be
  82. * used because it does not change when a new program is exec'ed.
  83. */
  84. int db_export__exec_comm(struct db_export *dbe, struct comm *comm,
  85. struct thread *main_thread)
  86. {
  87. int err;
  88. if (comm->db_id)
  89. return 0;
  90. err = __db_export__comm(dbe, comm, main_thread);
  91. if (err)
  92. return err;
  93. /*
  94. * Record the main thread for this comm. Note that the main thread can
  95. * have many "exec" comms because there will be a new one every time it
  96. * exec's. An "exec" comm however will only ever have 1 main thread.
  97. * That is different to any other threads for that same program because
  98. * exec() will effectively kill them, so the relationship between the
  99. * "exec" comm and non-main threads is 1-to-1. That is why
  100. * db_export__comm_thread() is called here for the main thread, but it
  101. * is called for non-main threads when they are exported.
  102. */
  103. return db_export__comm_thread(dbe, comm, main_thread);
  104. }
  105. int db_export__comm_thread(struct db_export *dbe, struct comm *comm,
  106. struct thread *thread)
  107. {
  108. u64 db_id;
  109. db_id = ++dbe->comm_thread_last_db_id;
  110. if (dbe->export_comm_thread)
  111. return dbe->export_comm_thread(dbe, db_id, comm, thread);
  112. return 0;
  113. }
  114. int db_export__dso(struct db_export *dbe, struct dso *dso,
  115. struct machine *machine)
  116. {
  117. if (dso->db_id)
  118. return 0;
  119. dso->db_id = ++dbe->dso_last_db_id;
  120. if (dbe->export_dso)
  121. return dbe->export_dso(dbe, dso, machine);
  122. return 0;
  123. }
  124. int db_export__symbol(struct db_export *dbe, struct symbol *sym,
  125. struct dso *dso)
  126. {
  127. u64 *sym_db_id = symbol__priv(sym);
  128. if (*sym_db_id)
  129. return 0;
  130. *sym_db_id = ++dbe->symbol_last_db_id;
  131. if (dbe->export_symbol)
  132. return dbe->export_symbol(dbe, sym, dso);
  133. return 0;
  134. }
  135. static int db_ids_from_al(struct db_export *dbe, struct addr_location *al,
  136. u64 *dso_db_id, u64 *sym_db_id, u64 *offset)
  137. {
  138. int err;
  139. if (al->map) {
  140. struct dso *dso = al->map->dso;
  141. err = db_export__dso(dbe, dso, al->maps->machine);
  142. if (err)
  143. return err;
  144. *dso_db_id = dso->db_id;
  145. if (!al->sym) {
  146. al->sym = symbol__new(al->addr, 0, 0, 0, "unknown");
  147. if (al->sym)
  148. dso__insert_symbol(dso, al->sym);
  149. }
  150. if (al->sym) {
  151. u64 *db_id = symbol__priv(al->sym);
  152. err = db_export__symbol(dbe, al->sym, dso);
  153. if (err)
  154. return err;
  155. *sym_db_id = *db_id;
  156. *offset = al->addr - al->sym->start;
  157. }
  158. }
  159. return 0;
  160. }
  161. static struct call_path *call_path_from_sample(struct db_export *dbe,
  162. struct machine *machine,
  163. struct thread *thread,
  164. struct perf_sample *sample,
  165. struct evsel *evsel)
  166. {
  167. u64 kernel_start = machine__kernel_start(machine);
  168. struct call_path *current = &dbe->cpr->call_path;
  169. enum chain_order saved_order = callchain_param.order;
  170. int err;
  171. if (!symbol_conf.use_callchain || !sample->callchain)
  172. return NULL;
  173. /*
  174. * Since the call path tree must be built starting with the root, we
  175. * must use ORDER_CALL for call chain resolution, in order to process
  176. * the callchain starting with the root node and ending with the leaf.
  177. */
  178. callchain_param.order = ORDER_CALLER;
  179. err = thread__resolve_callchain(thread, &callchain_cursor, evsel,
  180. sample, NULL, NULL, PERF_MAX_STACK_DEPTH);
  181. if (err) {
  182. callchain_param.order = saved_order;
  183. return NULL;
  184. }
  185. callchain_cursor_commit(&callchain_cursor);
  186. while (1) {
  187. struct callchain_cursor_node *node;
  188. struct addr_location al;
  189. u64 dso_db_id = 0, sym_db_id = 0, offset = 0;
  190. memset(&al, 0, sizeof(al));
  191. node = callchain_cursor_current(&callchain_cursor);
  192. if (!node)
  193. break;
  194. /*
  195. * Handle export of symbol and dso for this node by
  196. * constructing an addr_location struct and then passing it to
  197. * db_ids_from_al() to perform the export.
  198. */
  199. al.sym = node->ms.sym;
  200. al.map = node->ms.map;
  201. al.maps = thread->maps;
  202. al.addr = node->ip;
  203. if (al.map && !al.sym)
  204. al.sym = dso__find_symbol(al.map->dso, al.addr);
  205. db_ids_from_al(dbe, &al, &dso_db_id, &sym_db_id, &offset);
  206. /* add node to the call path tree if it doesn't exist */
  207. current = call_path__findnew(dbe->cpr, current,
  208. al.sym, node->ip,
  209. kernel_start);
  210. callchain_cursor_advance(&callchain_cursor);
  211. }
  212. /* Reset the callchain order to its prior value. */
  213. callchain_param.order = saved_order;
  214. if (current == &dbe->cpr->call_path) {
  215. /* Bail because the callchain was empty. */
  216. return NULL;
  217. }
  218. return current;
  219. }
  220. int db_export__branch_type(struct db_export *dbe, u32 branch_type,
  221. const char *name)
  222. {
  223. if (dbe->export_branch_type)
  224. return dbe->export_branch_type(dbe, branch_type, name);
  225. return 0;
  226. }
  227. static int db_export__threads(struct db_export *dbe, struct thread *thread,
  228. struct thread *main_thread,
  229. struct machine *machine, struct comm **comm_ptr)
  230. {
  231. struct comm *comm = NULL;
  232. struct comm *curr_comm;
  233. int err;
  234. if (main_thread) {
  235. /*
  236. * A thread has a reference to the main thread, so export the
  237. * main thread first.
  238. */
  239. err = db_export__thread(dbe, main_thread, machine, main_thread);
  240. if (err)
  241. return err;
  242. /*
  243. * Export comm before exporting the non-main thread because
  244. * db_export__comm_thread() can be called further below.
  245. */
  246. comm = machine__thread_exec_comm(machine, main_thread);
  247. if (comm) {
  248. err = db_export__exec_comm(dbe, comm, main_thread);
  249. if (err)
  250. return err;
  251. *comm_ptr = comm;
  252. }
  253. }
  254. if (thread != main_thread) {
  255. /*
  256. * For a non-main thread, db_export__comm_thread() must be
  257. * called only if thread has not previously been exported.
  258. */
  259. bool export_comm_thread = comm && !thread->db_id;
  260. err = db_export__thread(dbe, thread, machine, main_thread);
  261. if (err)
  262. return err;
  263. if (export_comm_thread) {
  264. err = db_export__comm_thread(dbe, comm, thread);
  265. if (err)
  266. return err;
  267. }
  268. }
  269. curr_comm = thread__comm(thread);
  270. if (curr_comm)
  271. return db_export__comm(dbe, curr_comm, thread);
  272. return 0;
  273. }
  274. int db_export__sample(struct db_export *dbe, union perf_event *event,
  275. struct perf_sample *sample, struct evsel *evsel,
  276. struct addr_location *al)
  277. {
  278. struct thread *thread = al->thread;
  279. struct export_sample es = {
  280. .event = event,
  281. .sample = sample,
  282. .evsel = evsel,
  283. .al = al,
  284. };
  285. struct thread *main_thread;
  286. struct comm *comm = NULL;
  287. int err;
  288. err = db_export__evsel(dbe, evsel);
  289. if (err)
  290. return err;
  291. err = db_export__machine(dbe, al->maps->machine);
  292. if (err)
  293. return err;
  294. main_thread = thread__main_thread(al->maps->machine, thread);
  295. err = db_export__threads(dbe, thread, main_thread, al->maps->machine, &comm);
  296. if (err)
  297. goto out_put;
  298. if (comm)
  299. es.comm_db_id = comm->db_id;
  300. es.db_id = ++dbe->sample_last_db_id;
  301. err = db_ids_from_al(dbe, al, &es.dso_db_id, &es.sym_db_id, &es.offset);
  302. if (err)
  303. goto out_put;
  304. if (dbe->cpr) {
  305. struct call_path *cp = call_path_from_sample(dbe, al->maps->machine,
  306. thread, sample,
  307. evsel);
  308. if (cp) {
  309. db_export__call_path(dbe, cp);
  310. es.call_path_id = cp->db_id;
  311. }
  312. }
  313. if ((evsel->core.attr.sample_type & PERF_SAMPLE_ADDR) &&
  314. sample_addr_correlates_sym(&evsel->core.attr)) {
  315. struct addr_location addr_al;
  316. thread__resolve(thread, &addr_al, sample);
  317. err = db_ids_from_al(dbe, &addr_al, &es.addr_dso_db_id,
  318. &es.addr_sym_db_id, &es.addr_offset);
  319. if (err)
  320. goto out_put;
  321. if (dbe->crp) {
  322. err = thread_stack__process(thread, comm, sample, al,
  323. &addr_al, es.db_id,
  324. dbe->crp);
  325. if (err)
  326. goto out_put;
  327. }
  328. }
  329. if (dbe->export_sample)
  330. err = dbe->export_sample(dbe, &es);
  331. out_put:
  332. thread__put(main_thread);
  333. return err;
  334. }
  335. static struct {
  336. u32 branch_type;
  337. const char *name;
  338. } branch_types[] = {
  339. {0, "no branch"},
  340. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
  341. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
  342. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "conditional jump"},
  343. {PERF_IP_FLAG_BRANCH, "unconditional jump"},
  344. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT,
  345. "software interrupt"},
  346. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT,
  347. "return from interrupt"},
  348. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET,
  349. "system call"},
  350. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET,
  351. "return from system call"},
  352. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "asynchronous branch"},
  353. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
  354. PERF_IP_FLAG_INTERRUPT, "hardware interrupt"},
  355. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "transaction abort"},
  356. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "trace begin"},
  357. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "trace end"},
  358. {0, NULL}
  359. };
  360. int db_export__branch_types(struct db_export *dbe)
  361. {
  362. int i, err = 0;
  363. for (i = 0; branch_types[i].name ; i++) {
  364. err = db_export__branch_type(dbe, branch_types[i].branch_type,
  365. branch_types[i].name);
  366. if (err)
  367. break;
  368. }
  369. /* Add trace begin / end variants */
  370. for (i = 0; branch_types[i].name ; i++) {
  371. const char *name = branch_types[i].name;
  372. u32 type = branch_types[i].branch_type;
  373. char buf[64];
  374. if (type == PERF_IP_FLAG_BRANCH ||
  375. (type & (PERF_IP_FLAG_TRACE_BEGIN | PERF_IP_FLAG_TRACE_END)))
  376. continue;
  377. snprintf(buf, sizeof(buf), "trace begin / %s", name);
  378. err = db_export__branch_type(dbe, type | PERF_IP_FLAG_TRACE_BEGIN, buf);
  379. if (err)
  380. break;
  381. snprintf(buf, sizeof(buf), "%s / trace end", name);
  382. err = db_export__branch_type(dbe, type | PERF_IP_FLAG_TRACE_END, buf);
  383. if (err)
  384. break;
  385. }
  386. return err;
  387. }
  388. int db_export__call_path(struct db_export *dbe, struct call_path *cp)
  389. {
  390. int err;
  391. if (cp->db_id)
  392. return 0;
  393. if (cp->parent) {
  394. err = db_export__call_path(dbe, cp->parent);
  395. if (err)
  396. return err;
  397. }
  398. cp->db_id = ++dbe->call_path_last_db_id;
  399. if (dbe->export_call_path)
  400. return dbe->export_call_path(dbe, cp);
  401. return 0;
  402. }
  403. int db_export__call_return(struct db_export *dbe, struct call_return *cr,
  404. u64 *parent_db_id)
  405. {
  406. int err;
  407. err = db_export__call_path(dbe, cr->cp);
  408. if (err)
  409. return err;
  410. if (!cr->db_id)
  411. cr->db_id = ++dbe->call_return_last_db_id;
  412. if (parent_db_id) {
  413. if (!*parent_db_id)
  414. *parent_db_id = ++dbe->call_return_last_db_id;
  415. cr->parent_db_id = *parent_db_id;
  416. }
  417. if (dbe->export_call_return)
  418. return dbe->export_call_return(dbe, cr);
  419. return 0;
  420. }
  421. static int db_export__pid_tid(struct db_export *dbe, struct machine *machine,
  422. pid_t pid, pid_t tid, u64 *db_id,
  423. struct comm **comm_ptr, bool *is_idle)
  424. {
  425. struct thread *thread = machine__find_thread(machine, pid, tid);
  426. struct thread *main_thread;
  427. int err = 0;
  428. if (!thread || !thread->comm_set)
  429. goto out_put;
  430. *is_idle = !thread->pid_ && !thread->tid;
  431. main_thread = thread__main_thread(machine, thread);
  432. err = db_export__threads(dbe, thread, main_thread, machine, comm_ptr);
  433. *db_id = thread->db_id;
  434. thread__put(main_thread);
  435. out_put:
  436. thread__put(thread);
  437. return err;
  438. }
  439. int db_export__switch(struct db_export *dbe, union perf_event *event,
  440. struct perf_sample *sample, struct machine *machine)
  441. {
  442. bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
  443. bool out_preempt = out &&
  444. (event->header.misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT);
  445. int flags = out | (out_preempt << 1);
  446. bool is_idle_a = false, is_idle_b = false;
  447. u64 th_a_id = 0, th_b_id = 0;
  448. u64 comm_out_id, comm_in_id;
  449. struct comm *comm_a = NULL;
  450. struct comm *comm_b = NULL;
  451. u64 th_out_id, th_in_id;
  452. u64 db_id;
  453. int err;
  454. err = db_export__machine(dbe, machine);
  455. if (err)
  456. return err;
  457. err = db_export__pid_tid(dbe, machine, sample->pid, sample->tid,
  458. &th_a_id, &comm_a, &is_idle_a);
  459. if (err)
  460. return err;
  461. if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE) {
  462. pid_t pid = event->context_switch.next_prev_pid;
  463. pid_t tid = event->context_switch.next_prev_tid;
  464. err = db_export__pid_tid(dbe, machine, pid, tid, &th_b_id,
  465. &comm_b, &is_idle_b);
  466. if (err)
  467. return err;
  468. }
  469. /*
  470. * Do not export if both threads are unknown (i.e. not being traced),
  471. * or one is unknown and the other is the idle task.
  472. */
  473. if ((!th_a_id || is_idle_a) && (!th_b_id || is_idle_b))
  474. return 0;
  475. db_id = ++dbe->context_switch_last_db_id;
  476. if (out) {
  477. th_out_id = th_a_id;
  478. th_in_id = th_b_id;
  479. comm_out_id = comm_a ? comm_a->db_id : 0;
  480. comm_in_id = comm_b ? comm_b->db_id : 0;
  481. } else {
  482. th_out_id = th_b_id;
  483. th_in_id = th_a_id;
  484. comm_out_id = comm_b ? comm_b->db_id : 0;
  485. comm_in_id = comm_a ? comm_a->db_id : 0;
  486. }
  487. if (dbe->export_context_switch)
  488. return dbe->export_context_switch(dbe, db_id, machine, sample,
  489. th_out_id, comm_out_id,
  490. th_in_id, comm_in_id, flags);
  491. return 0;
  492. }