db-export.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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 "db-export.h"
  25. struct deferred_export {
  26. struct list_head node;
  27. struct comm *comm;
  28. };
  29. static int db_export__deferred(struct db_export *dbe)
  30. {
  31. struct deferred_export *de;
  32. int err;
  33. while (!list_empty(&dbe->deferred)) {
  34. de = list_entry(dbe->deferred.next, struct deferred_export,
  35. node);
  36. err = dbe->export_comm(dbe, de->comm);
  37. list_del(&de->node);
  38. free(de);
  39. if (err)
  40. return err;
  41. }
  42. return 0;
  43. }
  44. static void db_export__free_deferred(struct db_export *dbe)
  45. {
  46. struct deferred_export *de;
  47. while (!list_empty(&dbe->deferred)) {
  48. de = list_entry(dbe->deferred.next, struct deferred_export,
  49. node);
  50. list_del(&de->node);
  51. free(de);
  52. }
  53. }
  54. static int db_export__defer_comm(struct db_export *dbe, struct comm *comm)
  55. {
  56. struct deferred_export *de;
  57. de = zalloc(sizeof(struct deferred_export));
  58. if (!de)
  59. return -ENOMEM;
  60. de->comm = comm;
  61. list_add_tail(&de->node, &dbe->deferred);
  62. return 0;
  63. }
  64. int db_export__init(struct db_export *dbe)
  65. {
  66. memset(dbe, 0, sizeof(struct db_export));
  67. INIT_LIST_HEAD(&dbe->deferred);
  68. return 0;
  69. }
  70. int db_export__flush(struct db_export *dbe)
  71. {
  72. return db_export__deferred(dbe);
  73. }
  74. void db_export__exit(struct db_export *dbe)
  75. {
  76. db_export__free_deferred(dbe);
  77. call_return_processor__free(dbe->crp);
  78. dbe->crp = NULL;
  79. }
  80. int db_export__evsel(struct db_export *dbe, struct perf_evsel *evsel)
  81. {
  82. if (evsel->db_id)
  83. return 0;
  84. evsel->db_id = ++dbe->evsel_last_db_id;
  85. if (dbe->export_evsel)
  86. return dbe->export_evsel(dbe, evsel);
  87. return 0;
  88. }
  89. int db_export__machine(struct db_export *dbe, struct machine *machine)
  90. {
  91. if (machine->db_id)
  92. return 0;
  93. machine->db_id = ++dbe->machine_last_db_id;
  94. if (dbe->export_machine)
  95. return dbe->export_machine(dbe, machine);
  96. return 0;
  97. }
  98. int db_export__thread(struct db_export *dbe, struct thread *thread,
  99. struct machine *machine, struct comm *comm)
  100. {
  101. struct thread *main_thread;
  102. u64 main_thread_db_id = 0;
  103. int err;
  104. if (thread->db_id)
  105. return 0;
  106. thread->db_id = ++dbe->thread_last_db_id;
  107. if (thread->pid_ != -1) {
  108. if (thread->pid_ == thread->tid) {
  109. main_thread = thread;
  110. } else {
  111. main_thread = machine__findnew_thread(machine,
  112. thread->pid_,
  113. thread->pid_);
  114. if (!main_thread)
  115. return -ENOMEM;
  116. err = db_export__thread(dbe, main_thread, machine,
  117. comm);
  118. if (err)
  119. goto out_put;
  120. if (comm) {
  121. err = db_export__comm_thread(dbe, comm, thread);
  122. if (err)
  123. goto out_put;
  124. }
  125. }
  126. main_thread_db_id = main_thread->db_id;
  127. if (main_thread != thread)
  128. thread__put(main_thread);
  129. }
  130. if (dbe->export_thread)
  131. return dbe->export_thread(dbe, thread, main_thread_db_id,
  132. machine);
  133. return 0;
  134. out_put:
  135. thread__put(main_thread);
  136. return err;
  137. }
  138. int db_export__comm(struct db_export *dbe, struct comm *comm,
  139. struct thread *main_thread)
  140. {
  141. int err;
  142. if (comm->db_id)
  143. return 0;
  144. comm->db_id = ++dbe->comm_last_db_id;
  145. if (dbe->export_comm) {
  146. if (main_thread->comm_set)
  147. err = dbe->export_comm(dbe, comm);
  148. else
  149. err = db_export__defer_comm(dbe, comm);
  150. if (err)
  151. return err;
  152. }
  153. return db_export__comm_thread(dbe, comm, main_thread);
  154. }
  155. int db_export__comm_thread(struct db_export *dbe, struct comm *comm,
  156. struct thread *thread)
  157. {
  158. u64 db_id;
  159. db_id = ++dbe->comm_thread_last_db_id;
  160. if (dbe->export_comm_thread)
  161. return dbe->export_comm_thread(dbe, db_id, comm, thread);
  162. return 0;
  163. }
  164. int db_export__dso(struct db_export *dbe, struct dso *dso,
  165. struct machine *machine)
  166. {
  167. if (dso->db_id)
  168. return 0;
  169. dso->db_id = ++dbe->dso_last_db_id;
  170. if (dbe->export_dso)
  171. return dbe->export_dso(dbe, dso, machine);
  172. return 0;
  173. }
  174. int db_export__symbol(struct db_export *dbe, struct symbol *sym,
  175. struct dso *dso)
  176. {
  177. u64 *sym_db_id = symbol__priv(sym);
  178. if (*sym_db_id)
  179. return 0;
  180. *sym_db_id = ++dbe->symbol_last_db_id;
  181. if (dbe->export_symbol)
  182. return dbe->export_symbol(dbe, sym, dso);
  183. return 0;
  184. }
  185. static struct thread *get_main_thread(struct machine *machine, struct thread *thread)
  186. {
  187. if (thread->pid_ == thread->tid)
  188. return thread__get(thread);
  189. if (thread->pid_ == -1)
  190. return NULL;
  191. return machine__find_thread(machine, thread->pid_, thread->pid_);
  192. }
  193. static int db_ids_from_al(struct db_export *dbe, struct addr_location *al,
  194. u64 *dso_db_id, u64 *sym_db_id, u64 *offset)
  195. {
  196. int err;
  197. if (al->map) {
  198. struct dso *dso = al->map->dso;
  199. err = db_export__dso(dbe, dso, al->machine);
  200. if (err)
  201. return err;
  202. *dso_db_id = dso->db_id;
  203. if (!al->sym) {
  204. al->sym = symbol__new(al->addr, 0, 0, "unknown");
  205. if (al->sym)
  206. symbols__insert(&dso->symbols[al->map->type],
  207. al->sym);
  208. }
  209. if (al->sym) {
  210. u64 *db_id = symbol__priv(al->sym);
  211. err = db_export__symbol(dbe, al->sym, dso);
  212. if (err)
  213. return err;
  214. *sym_db_id = *db_id;
  215. *offset = al->addr - al->sym->start;
  216. }
  217. }
  218. return 0;
  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. int db_export__sample(struct db_export *dbe, union perf_event *event,
  228. struct perf_sample *sample, struct perf_evsel *evsel,
  229. struct addr_location *al)
  230. {
  231. struct thread* thread = al->thread;
  232. struct export_sample es = {
  233. .event = event,
  234. .sample = sample,
  235. .evsel = evsel,
  236. .al = al,
  237. };
  238. struct thread *main_thread;
  239. struct comm *comm = NULL;
  240. int err;
  241. err = db_export__evsel(dbe, evsel);
  242. if (err)
  243. return err;
  244. err = db_export__machine(dbe, al->machine);
  245. if (err)
  246. return err;
  247. main_thread = get_main_thread(al->machine, thread);
  248. if (main_thread)
  249. comm = machine__thread_exec_comm(al->machine, main_thread);
  250. err = db_export__thread(dbe, thread, al->machine, comm);
  251. if (err)
  252. goto out_put;
  253. if (comm) {
  254. err = db_export__comm(dbe, comm, main_thread);
  255. if (err)
  256. goto out_put;
  257. es.comm_db_id = comm->db_id;
  258. }
  259. es.db_id = ++dbe->sample_last_db_id;
  260. err = db_ids_from_al(dbe, al, &es.dso_db_id, &es.sym_db_id, &es.offset);
  261. if (err)
  262. goto out_put;
  263. if ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
  264. sample_addr_correlates_sym(&evsel->attr)) {
  265. struct addr_location addr_al;
  266. perf_event__preprocess_sample_addr(event, sample, thread, &addr_al);
  267. err = db_ids_from_al(dbe, &addr_al, &es.addr_dso_db_id,
  268. &es.addr_sym_db_id, &es.addr_offset);
  269. if (err)
  270. goto out_put;
  271. if (dbe->crp) {
  272. err = thread_stack__process(thread, comm, sample, al,
  273. &addr_al, es.db_id,
  274. dbe->crp);
  275. if (err)
  276. goto out_put;
  277. }
  278. }
  279. if (dbe->export_sample)
  280. err = dbe->export_sample(dbe, &es);
  281. out_put:
  282. thread__put(main_thread);
  283. return err;
  284. }
  285. static struct {
  286. u32 branch_type;
  287. const char *name;
  288. } branch_types[] = {
  289. {0, "no branch"},
  290. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
  291. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
  292. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "conditional jump"},
  293. {PERF_IP_FLAG_BRANCH, "unconditional jump"},
  294. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT,
  295. "software interrupt"},
  296. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT,
  297. "return from interrupt"},
  298. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET,
  299. "system call"},
  300. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET,
  301. "return from system call"},
  302. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "asynchronous branch"},
  303. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
  304. PERF_IP_FLAG_INTERRUPT, "hardware interrupt"},
  305. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "transaction abort"},
  306. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "trace begin"},
  307. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "trace end"},
  308. {0, NULL}
  309. };
  310. int db_export__branch_types(struct db_export *dbe)
  311. {
  312. int i, err = 0;
  313. for (i = 0; branch_types[i].name ; i++) {
  314. err = db_export__branch_type(dbe, branch_types[i].branch_type,
  315. branch_types[i].name);
  316. if (err)
  317. break;
  318. }
  319. return err;
  320. }
  321. int db_export__call_path(struct db_export *dbe, struct call_path *cp)
  322. {
  323. int err;
  324. if (cp->db_id)
  325. return 0;
  326. if (cp->parent) {
  327. err = db_export__call_path(dbe, cp->parent);
  328. if (err)
  329. return err;
  330. }
  331. cp->db_id = ++dbe->call_path_last_db_id;
  332. if (dbe->export_call_path)
  333. return dbe->export_call_path(dbe, cp);
  334. return 0;
  335. }
  336. int db_export__call_return(struct db_export *dbe, struct call_return *cr)
  337. {
  338. int err;
  339. if (cr->db_id)
  340. return 0;
  341. err = db_export__call_path(dbe, cr->cp);
  342. if (err)
  343. return err;
  344. cr->db_id = ++dbe->call_return_last_db_id;
  345. if (dbe->export_call_return)
  346. return dbe->export_call_return(dbe, cr);
  347. return 0;
  348. }