db-export.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*
  2. * db-export.c: Support for exporting data suitable for import to a database
  3. * Copyright (c) 2014, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. */
  15. #include <errno.h>
  16. #include "evsel.h"
  17. #include "machine.h"
  18. #include "thread.h"
  19. #include "comm.h"
  20. #include "symbol.h"
  21. #include "event.h"
  22. #include "util.h"
  23. #include "thread-stack.h"
  24. #include "callchain.h"
  25. #include "call-path.h"
  26. #include "db-export.h"
  27. struct deferred_export {
  28. struct list_head node;
  29. struct comm *comm;
  30. };
  31. static int db_export__deferred(struct db_export *dbe)
  32. {
  33. struct deferred_export *de;
  34. int err;
  35. while (!list_empty(&dbe->deferred)) {
  36. de = list_entry(dbe->deferred.next, struct deferred_export,
  37. node);
  38. err = dbe->export_comm(dbe, de->comm);
  39. list_del(&de->node);
  40. free(de);
  41. if (err)
  42. return err;
  43. }
  44. return 0;
  45. }
  46. static void db_export__free_deferred(struct db_export *dbe)
  47. {
  48. struct deferred_export *de;
  49. while (!list_empty(&dbe->deferred)) {
  50. de = list_entry(dbe->deferred.next, struct deferred_export,
  51. node);
  52. list_del(&de->node);
  53. free(de);
  54. }
  55. }
  56. static int db_export__defer_comm(struct db_export *dbe, struct comm *comm)
  57. {
  58. struct deferred_export *de;
  59. de = zalloc(sizeof(struct deferred_export));
  60. if (!de)
  61. return -ENOMEM;
  62. de->comm = comm;
  63. list_add_tail(&de->node, &dbe->deferred);
  64. return 0;
  65. }
  66. int db_export__init(struct db_export *dbe)
  67. {
  68. memset(dbe, 0, sizeof(struct db_export));
  69. INIT_LIST_HEAD(&dbe->deferred);
  70. return 0;
  71. }
  72. int db_export__flush(struct db_export *dbe)
  73. {
  74. return db_export__deferred(dbe);
  75. }
  76. void db_export__exit(struct db_export *dbe)
  77. {
  78. db_export__free_deferred(dbe);
  79. call_return_processor__free(dbe->crp);
  80. dbe->crp = NULL;
  81. }
  82. int db_export__evsel(struct db_export *dbe, struct perf_evsel *evsel)
  83. {
  84. if (evsel->db_id)
  85. return 0;
  86. evsel->db_id = ++dbe->evsel_last_db_id;
  87. if (dbe->export_evsel)
  88. return dbe->export_evsel(dbe, evsel);
  89. return 0;
  90. }
  91. int db_export__machine(struct db_export *dbe, struct machine *machine)
  92. {
  93. if (machine->db_id)
  94. return 0;
  95. machine->db_id = ++dbe->machine_last_db_id;
  96. if (dbe->export_machine)
  97. return dbe->export_machine(dbe, machine);
  98. return 0;
  99. }
  100. int db_export__thread(struct db_export *dbe, struct thread *thread,
  101. struct machine *machine, struct comm *comm)
  102. {
  103. struct thread *main_thread;
  104. u64 main_thread_db_id = 0;
  105. int err;
  106. if (thread->db_id)
  107. return 0;
  108. thread->db_id = ++dbe->thread_last_db_id;
  109. if (thread->pid_ != -1) {
  110. if (thread->pid_ == thread->tid) {
  111. main_thread = thread;
  112. } else {
  113. main_thread = machine__findnew_thread(machine,
  114. thread->pid_,
  115. thread->pid_);
  116. if (!main_thread)
  117. return -ENOMEM;
  118. err = db_export__thread(dbe, main_thread, machine,
  119. comm);
  120. if (err)
  121. goto out_put;
  122. if (comm) {
  123. err = db_export__comm_thread(dbe, comm, thread);
  124. if (err)
  125. goto out_put;
  126. }
  127. }
  128. main_thread_db_id = main_thread->db_id;
  129. if (main_thread != thread)
  130. thread__put(main_thread);
  131. }
  132. if (dbe->export_thread)
  133. return dbe->export_thread(dbe, thread, main_thread_db_id,
  134. machine);
  135. return 0;
  136. out_put:
  137. thread__put(main_thread);
  138. return err;
  139. }
  140. int db_export__comm(struct db_export *dbe, struct comm *comm,
  141. struct thread *main_thread)
  142. {
  143. int err;
  144. if (comm->db_id)
  145. return 0;
  146. comm->db_id = ++dbe->comm_last_db_id;
  147. if (dbe->export_comm) {
  148. if (main_thread->comm_set)
  149. err = dbe->export_comm(dbe, comm);
  150. else
  151. err = db_export__defer_comm(dbe, comm);
  152. if (err)
  153. return err;
  154. }
  155. return db_export__comm_thread(dbe, comm, main_thread);
  156. }
  157. int db_export__comm_thread(struct db_export *dbe, struct comm *comm,
  158. struct thread *thread)
  159. {
  160. u64 db_id;
  161. db_id = ++dbe->comm_thread_last_db_id;
  162. if (dbe->export_comm_thread)
  163. return dbe->export_comm_thread(dbe, db_id, comm, thread);
  164. return 0;
  165. }
  166. int db_export__dso(struct db_export *dbe, struct dso *dso,
  167. struct machine *machine)
  168. {
  169. if (dso->db_id)
  170. return 0;
  171. dso->db_id = ++dbe->dso_last_db_id;
  172. if (dbe->export_dso)
  173. return dbe->export_dso(dbe, dso, machine);
  174. return 0;
  175. }
  176. int db_export__symbol(struct db_export *dbe, struct symbol *sym,
  177. struct dso *dso)
  178. {
  179. u64 *sym_db_id = symbol__priv(sym);
  180. if (*sym_db_id)
  181. return 0;
  182. *sym_db_id = ++dbe->symbol_last_db_id;
  183. if (dbe->export_symbol)
  184. return dbe->export_symbol(dbe, sym, dso);
  185. return 0;
  186. }
  187. static int db_ids_from_al(struct db_export *dbe, struct addr_location *al,
  188. u64 *dso_db_id, u64 *sym_db_id, u64 *offset)
  189. {
  190. int err;
  191. if (al->map) {
  192. struct dso *dso = al->map->dso;
  193. err = db_export__dso(dbe, dso, al->machine);
  194. if (err)
  195. return err;
  196. *dso_db_id = dso->db_id;
  197. if (!al->sym) {
  198. al->sym = symbol__new(al->addr, 0, 0, 0, "unknown");
  199. if (al->sym)
  200. dso__insert_symbol(dso, al->sym);
  201. }
  202. if (al->sym) {
  203. u64 *db_id = symbol__priv(al->sym);
  204. err = db_export__symbol(dbe, al->sym, dso);
  205. if (err)
  206. return err;
  207. *sym_db_id = *db_id;
  208. *offset = al->addr - al->sym->start;
  209. }
  210. }
  211. return 0;
  212. }
  213. static struct call_path *call_path_from_sample(struct db_export *dbe,
  214. struct machine *machine,
  215. struct thread *thread,
  216. struct perf_sample *sample,
  217. struct perf_evsel *evsel)
  218. {
  219. u64 kernel_start = machine__kernel_start(machine);
  220. struct call_path *current = &dbe->cpr->call_path;
  221. enum chain_order saved_order = callchain_param.order;
  222. int err;
  223. if (!symbol_conf.use_callchain || !sample->callchain)
  224. return NULL;
  225. /*
  226. * Since the call path tree must be built starting with the root, we
  227. * must use ORDER_CALL for call chain resolution, in order to process
  228. * the callchain starting with the root node and ending with the leaf.
  229. */
  230. callchain_param.order = ORDER_CALLER;
  231. err = thread__resolve_callchain(thread, &callchain_cursor, evsel,
  232. sample, NULL, NULL, PERF_MAX_STACK_DEPTH);
  233. if (err) {
  234. callchain_param.order = saved_order;
  235. return NULL;
  236. }
  237. callchain_cursor_commit(&callchain_cursor);
  238. while (1) {
  239. struct callchain_cursor_node *node;
  240. struct addr_location al;
  241. u64 dso_db_id = 0, sym_db_id = 0, offset = 0;
  242. memset(&al, 0, sizeof(al));
  243. node = callchain_cursor_current(&callchain_cursor);
  244. if (!node)
  245. break;
  246. /*
  247. * Handle export of symbol and dso for this node by
  248. * constructing an addr_location struct and then passing it to
  249. * db_ids_from_al() to perform the export.
  250. */
  251. al.sym = node->sym;
  252. al.map = node->map;
  253. al.machine = machine;
  254. al.addr = node->ip;
  255. if (al.map && !al.sym)
  256. al.sym = dso__find_symbol(al.map->dso, al.addr);
  257. db_ids_from_al(dbe, &al, &dso_db_id, &sym_db_id, &offset);
  258. /* add node to the call path tree if it doesn't exist */
  259. current = call_path__findnew(dbe->cpr, current,
  260. al.sym, node->ip,
  261. kernel_start);
  262. callchain_cursor_advance(&callchain_cursor);
  263. }
  264. /* Reset the callchain order to its prior value. */
  265. callchain_param.order = saved_order;
  266. if (current == &dbe->cpr->call_path) {
  267. /* Bail because the callchain was empty. */
  268. return NULL;
  269. }
  270. return current;
  271. }
  272. int db_export__branch_type(struct db_export *dbe, u32 branch_type,
  273. const char *name)
  274. {
  275. if (dbe->export_branch_type)
  276. return dbe->export_branch_type(dbe, branch_type, name);
  277. return 0;
  278. }
  279. int db_export__sample(struct db_export *dbe, union perf_event *event,
  280. struct perf_sample *sample, struct perf_evsel *evsel,
  281. struct addr_location *al)
  282. {
  283. struct thread* thread = al->thread;
  284. struct export_sample es = {
  285. .event = event,
  286. .sample = sample,
  287. .evsel = evsel,
  288. .al = al,
  289. };
  290. struct thread *main_thread;
  291. struct comm *comm = NULL;
  292. int err;
  293. err = db_export__evsel(dbe, evsel);
  294. if (err)
  295. return err;
  296. err = db_export__machine(dbe, al->machine);
  297. if (err)
  298. return err;
  299. main_thread = thread__main_thread(al->machine, thread);
  300. if (main_thread)
  301. comm = machine__thread_exec_comm(al->machine, main_thread);
  302. err = db_export__thread(dbe, thread, al->machine, comm);
  303. if (err)
  304. goto out_put;
  305. if (comm) {
  306. err = db_export__comm(dbe, comm, main_thread);
  307. if (err)
  308. goto out_put;
  309. es.comm_db_id = comm->db_id;
  310. }
  311. es.db_id = ++dbe->sample_last_db_id;
  312. err = db_ids_from_al(dbe, al, &es.dso_db_id, &es.sym_db_id, &es.offset);
  313. if (err)
  314. goto out_put;
  315. if (dbe->cpr) {
  316. struct call_path *cp = call_path_from_sample(dbe, al->machine,
  317. thread, sample,
  318. evsel);
  319. if (cp) {
  320. db_export__call_path(dbe, cp);
  321. es.call_path_id = cp->db_id;
  322. }
  323. }
  324. if ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
  325. sample_addr_correlates_sym(&evsel->attr)) {
  326. struct addr_location addr_al;
  327. thread__resolve(thread, &addr_al, sample);
  328. err = db_ids_from_al(dbe, &addr_al, &es.addr_dso_db_id,
  329. &es.addr_sym_db_id, &es.addr_offset);
  330. if (err)
  331. goto out_put;
  332. if (dbe->crp) {
  333. err = thread_stack__process(thread, comm, sample, al,
  334. &addr_al, es.db_id,
  335. dbe->crp);
  336. if (err)
  337. goto out_put;
  338. }
  339. }
  340. if (dbe->export_sample)
  341. err = dbe->export_sample(dbe, &es);
  342. out_put:
  343. thread__put(main_thread);
  344. return err;
  345. }
  346. static struct {
  347. u32 branch_type;
  348. const char *name;
  349. } branch_types[] = {
  350. {0, "no branch"},
  351. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
  352. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
  353. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "conditional jump"},
  354. {PERF_IP_FLAG_BRANCH, "unconditional jump"},
  355. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT,
  356. "software interrupt"},
  357. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT,
  358. "return from interrupt"},
  359. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET,
  360. "system call"},
  361. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET,
  362. "return from system call"},
  363. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "asynchronous branch"},
  364. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
  365. PERF_IP_FLAG_INTERRUPT, "hardware interrupt"},
  366. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "transaction abort"},
  367. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "trace begin"},
  368. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "trace end"},
  369. {0, NULL}
  370. };
  371. int db_export__branch_types(struct db_export *dbe)
  372. {
  373. int i, err = 0;
  374. for (i = 0; branch_types[i].name ; i++) {
  375. err = db_export__branch_type(dbe, branch_types[i].branch_type,
  376. branch_types[i].name);
  377. if (err)
  378. break;
  379. }
  380. return err;
  381. }
  382. int db_export__call_path(struct db_export *dbe, struct call_path *cp)
  383. {
  384. int err;
  385. if (cp->db_id)
  386. return 0;
  387. if (cp->parent) {
  388. err = db_export__call_path(dbe, cp->parent);
  389. if (err)
  390. return err;
  391. }
  392. cp->db_id = ++dbe->call_path_last_db_id;
  393. if (dbe->export_call_path)
  394. return dbe->export_call_path(dbe, cp);
  395. return 0;
  396. }
  397. int db_export__call_return(struct db_export *dbe, struct call_return *cr)
  398. {
  399. int err;
  400. if (cr->db_id)
  401. return 0;
  402. err = db_export__call_path(dbe, cr->cp);
  403. if (err)
  404. return err;
  405. cr->db_id = ++dbe->call_return_last_db_id;
  406. if (dbe->export_call_return)
  407. return dbe->export_call_return(dbe, cr);
  408. return 0;
  409. }