db-export.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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, "unknown");
  199. if (al->sym)
  200. dso__insert_symbol(dso, al->map->type, 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, MAP__FUNCTION,
  257. al.addr);
  258. db_ids_from_al(dbe, &al, &dso_db_id, &sym_db_id, &offset);
  259. /* add node to the call path tree if it doesn't exist */
  260. current = call_path__findnew(dbe->cpr, current,
  261. al.sym, node->ip,
  262. kernel_start);
  263. callchain_cursor_advance(&callchain_cursor);
  264. }
  265. /* Reset the callchain order to its prior value. */
  266. callchain_param.order = saved_order;
  267. if (current == &dbe->cpr->call_path) {
  268. /* Bail because the callchain was empty. */
  269. return NULL;
  270. }
  271. return current;
  272. }
  273. int db_export__branch_type(struct db_export *dbe, u32 branch_type,
  274. const char *name)
  275. {
  276. if (dbe->export_branch_type)
  277. return dbe->export_branch_type(dbe, branch_type, name);
  278. return 0;
  279. }
  280. int db_export__sample(struct db_export *dbe, union perf_event *event,
  281. struct perf_sample *sample, struct perf_evsel *evsel,
  282. struct addr_location *al)
  283. {
  284. struct thread* thread = al->thread;
  285. struct export_sample es = {
  286. .event = event,
  287. .sample = sample,
  288. .evsel = evsel,
  289. .al = al,
  290. };
  291. struct thread *main_thread;
  292. struct comm *comm = NULL;
  293. int err;
  294. err = db_export__evsel(dbe, evsel);
  295. if (err)
  296. return err;
  297. err = db_export__machine(dbe, al->machine);
  298. if (err)
  299. return err;
  300. main_thread = thread__main_thread(al->machine, thread);
  301. if (main_thread)
  302. comm = machine__thread_exec_comm(al->machine, main_thread);
  303. err = db_export__thread(dbe, thread, al->machine, comm);
  304. if (err)
  305. goto out_put;
  306. if (comm) {
  307. err = db_export__comm(dbe, comm, main_thread);
  308. if (err)
  309. goto out_put;
  310. es.comm_db_id = comm->db_id;
  311. }
  312. es.db_id = ++dbe->sample_last_db_id;
  313. err = db_ids_from_al(dbe, al, &es.dso_db_id, &es.sym_db_id, &es.offset);
  314. if (err)
  315. goto out_put;
  316. if (dbe->cpr) {
  317. struct call_path *cp = call_path_from_sample(dbe, al->machine,
  318. thread, sample,
  319. evsel);
  320. if (cp) {
  321. db_export__call_path(dbe, cp);
  322. es.call_path_id = cp->db_id;
  323. }
  324. }
  325. if ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
  326. sample_addr_correlates_sym(&evsel->attr)) {
  327. struct addr_location addr_al;
  328. thread__resolve(thread, &addr_al, sample);
  329. err = db_ids_from_al(dbe, &addr_al, &es.addr_dso_db_id,
  330. &es.addr_sym_db_id, &es.addr_offset);
  331. if (err)
  332. goto out_put;
  333. if (dbe->crp) {
  334. err = thread_stack__process(thread, comm, sample, al,
  335. &addr_al, es.db_id,
  336. dbe->crp);
  337. if (err)
  338. goto out_put;
  339. }
  340. }
  341. if (dbe->export_sample)
  342. err = dbe->export_sample(dbe, &es);
  343. out_put:
  344. thread__put(main_thread);
  345. return err;
  346. }
  347. static struct {
  348. u32 branch_type;
  349. const char *name;
  350. } branch_types[] = {
  351. {0, "no branch"},
  352. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
  353. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
  354. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "conditional jump"},
  355. {PERF_IP_FLAG_BRANCH, "unconditional jump"},
  356. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT,
  357. "software interrupt"},
  358. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT,
  359. "return from interrupt"},
  360. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET,
  361. "system call"},
  362. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET,
  363. "return from system call"},
  364. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "asynchronous branch"},
  365. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
  366. PERF_IP_FLAG_INTERRUPT, "hardware interrupt"},
  367. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "transaction abort"},
  368. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "trace begin"},
  369. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "trace end"},
  370. {0, NULL}
  371. };
  372. int db_export__branch_types(struct db_export *dbe)
  373. {
  374. int i, err = 0;
  375. for (i = 0; branch_types[i].name ; i++) {
  376. err = db_export__branch_type(dbe, branch_types[i].branch_type,
  377. branch_types[i].name);
  378. if (err)
  379. break;
  380. }
  381. return err;
  382. }
  383. int db_export__call_path(struct db_export *dbe, struct call_path *cp)
  384. {
  385. int err;
  386. if (cp->db_id)
  387. return 0;
  388. if (cp->parent) {
  389. err = db_export__call_path(dbe, cp->parent);
  390. if (err)
  391. return err;
  392. }
  393. cp->db_id = ++dbe->call_path_last_db_id;
  394. if (dbe->export_call_path)
  395. return dbe->export_call_path(dbe, cp);
  396. return 0;
  397. }
  398. int db_export__call_return(struct db_export *dbe, struct call_return *cr)
  399. {
  400. int err;
  401. if (cr->db_id)
  402. return 0;
  403. err = db_export__call_path(dbe, cr->cp);
  404. if (err)
  405. return err;
  406. cr->db_id = ++dbe->call_return_last_db_id;
  407. if (dbe->export_call_return)
  408. return dbe->export_call_return(dbe, cr);
  409. return 0;
  410. }