db-export.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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(thread))
  54. return 0;
  55. thread__set_db_id(thread, ++dbe->thread_last_db_id);
  56. if (main_thread)
  57. main_thread_db_id = thread__db_id(main_thread);
  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(dso))
  118. return 0;
  119. dso__set_db_id(dso, ++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 = map__dso(al->map);
  141. err = db_export__dso(dbe, dso, maps__machine(thread__maps(al->thread)));
  142. if (err)
  143. return err;
  144. *dso_db_id = dso__db_id(dso);
  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. struct callchain_cursor *cursor;
  171. int err;
  172. if (!symbol_conf.use_callchain || !sample->callchain)
  173. return NULL;
  174. /*
  175. * Since the call path tree must be built starting with the root, we
  176. * must use ORDER_CALL for call chain resolution, in order to process
  177. * the callchain starting with the root node and ending with the leaf.
  178. */
  179. callchain_param.order = ORDER_CALLER;
  180. cursor = get_tls_callchain_cursor();
  181. err = thread__resolve_callchain(thread, cursor, evsel,
  182. sample, NULL, NULL, PERF_MAX_STACK_DEPTH);
  183. if (err) {
  184. callchain_param.order = saved_order;
  185. return NULL;
  186. }
  187. callchain_cursor_commit(cursor);
  188. while (1) {
  189. struct callchain_cursor_node *node;
  190. struct addr_location al;
  191. u64 dso_db_id = 0, sym_db_id = 0, offset = 0;
  192. node = callchain_cursor_current(cursor);
  193. if (!node)
  194. break;
  195. /*
  196. * Handle export of symbol and dso for this node by
  197. * constructing an addr_location struct and then passing it to
  198. * db_ids_from_al() to perform the export.
  199. */
  200. addr_location__init(&al);
  201. al.sym = node->ms.sym;
  202. al.map = map__get(node->ms.map);
  203. al.addr = node->ip;
  204. al.thread = thread__get(thread);
  205. if (al.map && !al.sym)
  206. al.sym = dso__find_symbol(map__dso(al.map), al.addr);
  207. db_ids_from_al(dbe, &al, &dso_db_id, &sym_db_id, &offset);
  208. /* add node to the call path tree if it doesn't exist */
  209. current = call_path__findnew(dbe->cpr, current,
  210. al.sym, node->ip,
  211. kernel_start);
  212. callchain_cursor_advance(cursor);
  213. addr_location__exit(&al);
  214. }
  215. /* Reset the callchain order to its prior value. */
  216. callchain_param.order = saved_order;
  217. if (current == &dbe->cpr->call_path) {
  218. /* Bail because the callchain was empty. */
  219. return NULL;
  220. }
  221. return current;
  222. }
  223. int db_export__branch_type(struct db_export *dbe, u32 branch_type,
  224. const char *name)
  225. {
  226. if (dbe->export_branch_type)
  227. return dbe->export_branch_type(dbe, branch_type, name);
  228. return 0;
  229. }
  230. static int db_export__threads(struct db_export *dbe, struct thread *thread,
  231. struct thread *main_thread,
  232. struct machine *machine, struct comm **comm_ptr)
  233. {
  234. struct comm *comm = NULL;
  235. struct comm *curr_comm;
  236. int err;
  237. if (main_thread) {
  238. /*
  239. * A thread has a reference to the main thread, so export the
  240. * main thread first.
  241. */
  242. err = db_export__thread(dbe, main_thread, machine, main_thread);
  243. if (err)
  244. return err;
  245. /*
  246. * Export comm before exporting the non-main thread because
  247. * db_export__comm_thread() can be called further below.
  248. */
  249. comm = machine__thread_exec_comm(machine, main_thread);
  250. if (comm) {
  251. err = db_export__exec_comm(dbe, comm, main_thread);
  252. if (err)
  253. return err;
  254. *comm_ptr = comm;
  255. }
  256. }
  257. if (thread != main_thread) {
  258. /*
  259. * For a non-main thread, db_export__comm_thread() must be
  260. * called only if thread has not previously been exported.
  261. */
  262. bool export_comm_thread = comm && !thread__db_id(thread);
  263. err = db_export__thread(dbe, thread, machine, main_thread);
  264. if (err)
  265. return err;
  266. if (export_comm_thread) {
  267. err = db_export__comm_thread(dbe, comm, thread);
  268. if (err)
  269. return err;
  270. }
  271. }
  272. curr_comm = thread__comm(thread);
  273. if (curr_comm)
  274. return db_export__comm(dbe, curr_comm, thread);
  275. return 0;
  276. }
  277. int db_export__sample(struct db_export *dbe, union perf_event *event,
  278. struct perf_sample *sample, struct evsel *evsel,
  279. struct addr_location *al, struct addr_location *addr_al)
  280. {
  281. struct thread *thread = al->thread;
  282. struct export_sample es = {
  283. .event = event,
  284. .sample = sample,
  285. .evsel = evsel,
  286. .al = al,
  287. };
  288. struct thread *main_thread;
  289. struct comm *comm = NULL;
  290. struct machine *machine = NULL;
  291. int err;
  292. if (thread__maps(thread))
  293. machine = maps__machine(thread__maps(thread));
  294. if (!machine)
  295. return -1;
  296. err = db_export__evsel(dbe, evsel);
  297. if (err)
  298. return err;
  299. err = db_export__machine(dbe, machine);
  300. if (err)
  301. return err;
  302. main_thread = thread__main_thread(machine, thread);
  303. err = db_export__threads(dbe, thread, main_thread, machine, &comm);
  304. if (err)
  305. goto out_put;
  306. if (comm)
  307. es.comm_db_id = comm->db_id;
  308. es.db_id = ++dbe->sample_last_db_id;
  309. err = db_ids_from_al(dbe, al, &es.dso_db_id, &es.sym_db_id, &es.offset);
  310. if (err)
  311. goto out_put;
  312. if (dbe->cpr) {
  313. struct call_path *cp = call_path_from_sample(dbe, machine,
  314. thread, sample,
  315. evsel);
  316. if (cp) {
  317. db_export__call_path(dbe, cp);
  318. es.call_path_id = cp->db_id;
  319. }
  320. }
  321. if (addr_al) {
  322. err = db_ids_from_al(dbe, addr_al, &es.addr_dso_db_id,
  323. &es.addr_sym_db_id, &es.addr_offset);
  324. if (err)
  325. goto out_put;
  326. if (dbe->crp) {
  327. err = thread_stack__process(thread, comm, sample, al,
  328. addr_al, es.db_id,
  329. dbe->crp);
  330. if (err)
  331. goto out_put;
  332. }
  333. }
  334. if (dbe->export_sample)
  335. err = dbe->export_sample(dbe, &es);
  336. out_put:
  337. thread__put(main_thread);
  338. return err;
  339. }
  340. static struct {
  341. u32 branch_type;
  342. const char *name;
  343. } branch_types[] = {
  344. {0, "no branch"},
  345. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
  346. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
  347. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "conditional jump"},
  348. {PERF_IP_FLAG_BRANCH, "unconditional jump"},
  349. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT,
  350. "software interrupt"},
  351. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT,
  352. "return from interrupt"},
  353. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET,
  354. "system call"},
  355. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET,
  356. "return from system call"},
  357. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "asynchronous branch"},
  358. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
  359. PERF_IP_FLAG_INTERRUPT, "hardware interrupt"},
  360. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "transaction abort"},
  361. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "trace begin"},
  362. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "trace end"},
  363. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_VMENTRY, "vm entry"},
  364. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_VMEXIT, "vm exit"},
  365. {0, NULL}
  366. };
  367. int db_export__branch_types(struct db_export *dbe)
  368. {
  369. int i, err = 0;
  370. for (i = 0; branch_types[i].name ; i++) {
  371. err = db_export__branch_type(dbe, branch_types[i].branch_type,
  372. branch_types[i].name);
  373. if (err)
  374. break;
  375. }
  376. /* Add trace begin / end variants */
  377. for (i = 0; branch_types[i].name ; i++) {
  378. const char *name = branch_types[i].name;
  379. u32 type = branch_types[i].branch_type;
  380. char buf[64];
  381. if (type == PERF_IP_FLAG_BRANCH ||
  382. (type & (PERF_IP_FLAG_TRACE_BEGIN | PERF_IP_FLAG_TRACE_END)))
  383. continue;
  384. snprintf(buf, sizeof(buf), "trace begin / %s", name);
  385. err = db_export__branch_type(dbe, type | PERF_IP_FLAG_TRACE_BEGIN, buf);
  386. if (err)
  387. break;
  388. snprintf(buf, sizeof(buf), "%s / trace end", name);
  389. err = db_export__branch_type(dbe, type | PERF_IP_FLAG_TRACE_END, buf);
  390. if (err)
  391. break;
  392. }
  393. return err;
  394. }
  395. int db_export__call_path(struct db_export *dbe, struct call_path *cp)
  396. {
  397. int err;
  398. if (cp->db_id)
  399. return 0;
  400. if (cp->parent) {
  401. err = db_export__call_path(dbe, cp->parent);
  402. if (err)
  403. return err;
  404. }
  405. cp->db_id = ++dbe->call_path_last_db_id;
  406. if (dbe->export_call_path)
  407. return dbe->export_call_path(dbe, cp);
  408. return 0;
  409. }
  410. int db_export__call_return(struct db_export *dbe, struct call_return *cr,
  411. u64 *parent_db_id)
  412. {
  413. int err;
  414. err = db_export__call_path(dbe, cr->cp);
  415. if (err)
  416. return err;
  417. if (!cr->db_id)
  418. cr->db_id = ++dbe->call_return_last_db_id;
  419. if (parent_db_id) {
  420. if (!*parent_db_id)
  421. *parent_db_id = ++dbe->call_return_last_db_id;
  422. cr->parent_db_id = *parent_db_id;
  423. }
  424. if (dbe->export_call_return)
  425. return dbe->export_call_return(dbe, cr);
  426. return 0;
  427. }
  428. static int db_export__pid_tid(struct db_export *dbe, struct machine *machine,
  429. pid_t pid, pid_t tid, u64 *db_id,
  430. struct comm **comm_ptr, bool *is_idle)
  431. {
  432. struct thread *thread = machine__find_thread(machine, pid, tid);
  433. struct thread *main_thread;
  434. int err = 0;
  435. if (!thread || !thread__comm_set(thread))
  436. goto out_put;
  437. *is_idle = !thread__pid(thread) && !thread__tid(thread);
  438. main_thread = thread__main_thread(machine, thread);
  439. err = db_export__threads(dbe, thread, main_thread, machine, comm_ptr);
  440. *db_id = thread__db_id(thread);
  441. thread__put(main_thread);
  442. out_put:
  443. thread__put(thread);
  444. return err;
  445. }
  446. int db_export__switch(struct db_export *dbe, union perf_event *event,
  447. struct perf_sample *sample, struct machine *machine)
  448. {
  449. bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
  450. bool out_preempt = out &&
  451. (event->header.misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT);
  452. int flags = out | (out_preempt << 1);
  453. bool is_idle_a = false, is_idle_b = false;
  454. u64 th_a_id = 0, th_b_id = 0;
  455. u64 comm_out_id, comm_in_id;
  456. struct comm *comm_a = NULL;
  457. struct comm *comm_b = NULL;
  458. u64 th_out_id, th_in_id;
  459. u64 db_id;
  460. int err;
  461. err = db_export__machine(dbe, machine);
  462. if (err)
  463. return err;
  464. err = db_export__pid_tid(dbe, machine, sample->pid, sample->tid,
  465. &th_a_id, &comm_a, &is_idle_a);
  466. if (err)
  467. return err;
  468. if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE) {
  469. pid_t pid = event->context_switch.next_prev_pid;
  470. pid_t tid = event->context_switch.next_prev_tid;
  471. err = db_export__pid_tid(dbe, machine, pid, tid, &th_b_id,
  472. &comm_b, &is_idle_b);
  473. if (err)
  474. return err;
  475. }
  476. /*
  477. * Do not export if both threads are unknown (i.e. not being traced),
  478. * or one is unknown and the other is the idle task.
  479. */
  480. if ((!th_a_id || is_idle_a) && (!th_b_id || is_idle_b))
  481. return 0;
  482. db_id = ++dbe->context_switch_last_db_id;
  483. if (out) {
  484. th_out_id = th_a_id;
  485. th_in_id = th_b_id;
  486. comm_out_id = comm_a ? comm_a->db_id : 0;
  487. comm_in_id = comm_b ? comm_b->db_id : 0;
  488. } else {
  489. th_out_id = th_b_id;
  490. th_in_id = th_a_id;
  491. comm_out_id = comm_b ? comm_b->db_id : 0;
  492. comm_in_id = comm_a ? comm_a->db_id : 0;
  493. }
  494. if (dbe->export_context_switch)
  495. return dbe->export_context_switch(dbe, db_id, machine, sample,
  496. th_out_id, comm_out_id,
  497. th_in_id, comm_in_id, flags);
  498. return 0;
  499. }