db-export.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. u64 main_thread_db_id = 0;
  102. int err;
  103. if (thread->db_id)
  104. return 0;
  105. thread->db_id = ++dbe->thread_last_db_id;
  106. if (thread->pid_ != -1) {
  107. struct thread *main_thread;
  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. return err;
  120. if (comm) {
  121. err = db_export__comm_thread(dbe, comm, thread);
  122. if (err)
  123. return err;
  124. }
  125. }
  126. main_thread_db_id = main_thread->db_id;
  127. }
  128. if (dbe->export_thread)
  129. return dbe->export_thread(dbe, thread, main_thread_db_id,
  130. machine);
  131. return 0;
  132. }
  133. int db_export__comm(struct db_export *dbe, struct comm *comm,
  134. struct thread *main_thread)
  135. {
  136. int err;
  137. if (comm->db_id)
  138. return 0;
  139. comm->db_id = ++dbe->comm_last_db_id;
  140. if (dbe->export_comm) {
  141. if (main_thread->comm_set)
  142. err = dbe->export_comm(dbe, comm);
  143. else
  144. err = db_export__defer_comm(dbe, comm);
  145. if (err)
  146. return err;
  147. }
  148. return db_export__comm_thread(dbe, comm, main_thread);
  149. }
  150. int db_export__comm_thread(struct db_export *dbe, struct comm *comm,
  151. struct thread *thread)
  152. {
  153. u64 db_id;
  154. db_id = ++dbe->comm_thread_last_db_id;
  155. if (dbe->export_comm_thread)
  156. return dbe->export_comm_thread(dbe, db_id, comm, thread);
  157. return 0;
  158. }
  159. int db_export__dso(struct db_export *dbe, struct dso *dso,
  160. struct machine *machine)
  161. {
  162. if (dso->db_id)
  163. return 0;
  164. dso->db_id = ++dbe->dso_last_db_id;
  165. if (dbe->export_dso)
  166. return dbe->export_dso(dbe, dso, machine);
  167. return 0;
  168. }
  169. int db_export__symbol(struct db_export *dbe, struct symbol *sym,
  170. struct dso *dso)
  171. {
  172. u64 *sym_db_id = symbol__priv(sym);
  173. if (*sym_db_id)
  174. return 0;
  175. *sym_db_id = ++dbe->symbol_last_db_id;
  176. if (dbe->export_symbol)
  177. return dbe->export_symbol(dbe, sym, dso);
  178. return 0;
  179. }
  180. static struct thread *get_main_thread(struct machine *machine, struct thread *thread)
  181. {
  182. if (thread->pid_ == thread->tid)
  183. return thread;
  184. if (thread->pid_ == -1)
  185. return NULL;
  186. return machine__find_thread(machine, thread->pid_, thread->pid_);
  187. }
  188. static int db_ids_from_al(struct db_export *dbe, struct addr_location *al,
  189. u64 *dso_db_id, u64 *sym_db_id, u64 *offset)
  190. {
  191. int err;
  192. if (al->map) {
  193. struct dso *dso = al->map->dso;
  194. err = db_export__dso(dbe, dso, al->machine);
  195. if (err)
  196. return err;
  197. *dso_db_id = dso->db_id;
  198. if (!al->sym) {
  199. al->sym = symbol__new(al->addr, 0, 0, "unknown");
  200. if (al->sym)
  201. symbols__insert(&dso->symbols[al->map->type],
  202. al->sym);
  203. }
  204. if (al->sym) {
  205. u64 *db_id = symbol__priv(al->sym);
  206. err = db_export__symbol(dbe, al->sym, dso);
  207. if (err)
  208. return err;
  209. *sym_db_id = *db_id;
  210. *offset = al->addr - al->sym->start;
  211. }
  212. }
  213. return 0;
  214. }
  215. int db_export__branch_type(struct db_export *dbe, u32 branch_type,
  216. const char *name)
  217. {
  218. if (dbe->export_branch_type)
  219. return dbe->export_branch_type(dbe, branch_type, name);
  220. return 0;
  221. }
  222. int db_export__sample(struct db_export *dbe, union perf_event *event,
  223. struct perf_sample *sample, struct perf_evsel *evsel,
  224. struct thread *thread, struct addr_location *al)
  225. {
  226. struct export_sample es = {
  227. .event = event,
  228. .sample = sample,
  229. .evsel = evsel,
  230. .thread = thread,
  231. .al = al,
  232. };
  233. struct thread *main_thread;
  234. struct comm *comm = NULL;
  235. int err;
  236. err = db_export__evsel(dbe, evsel);
  237. if (err)
  238. return err;
  239. err = db_export__machine(dbe, al->machine);
  240. if (err)
  241. return err;
  242. main_thread = get_main_thread(al->machine, thread);
  243. if (main_thread)
  244. comm = machine__thread_exec_comm(al->machine, main_thread);
  245. err = db_export__thread(dbe, thread, al->machine, comm);
  246. if (err)
  247. return err;
  248. if (comm) {
  249. err = db_export__comm(dbe, comm, main_thread);
  250. if (err)
  251. return err;
  252. es.comm_db_id = comm->db_id;
  253. }
  254. es.db_id = ++dbe->sample_last_db_id;
  255. err = db_ids_from_al(dbe, al, &es.dso_db_id, &es.sym_db_id, &es.offset);
  256. if (err)
  257. return err;
  258. if ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
  259. sample_addr_correlates_sym(&evsel->attr)) {
  260. struct addr_location addr_al;
  261. perf_event__preprocess_sample_addr(event, sample, thread, &addr_al);
  262. err = db_ids_from_al(dbe, &addr_al, &es.addr_dso_db_id,
  263. &es.addr_sym_db_id, &es.addr_offset);
  264. if (err)
  265. return err;
  266. if (dbe->crp) {
  267. err = thread_stack__process(thread, comm, sample, al,
  268. &addr_al, es.db_id,
  269. dbe->crp);
  270. if (err)
  271. return err;
  272. }
  273. }
  274. if (dbe->export_sample)
  275. return dbe->export_sample(dbe, &es);
  276. return 0;
  277. }
  278. static struct {
  279. u32 branch_type;
  280. const char *name;
  281. } branch_types[] = {
  282. {0, "no branch"},
  283. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
  284. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
  285. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "conditional jump"},
  286. {PERF_IP_FLAG_BRANCH, "unconditional jump"},
  287. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT,
  288. "software interrupt"},
  289. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT,
  290. "return from interrupt"},
  291. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET,
  292. "system call"},
  293. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET,
  294. "return from system call"},
  295. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "asynchronous branch"},
  296. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
  297. PERF_IP_FLAG_INTERRUPT, "hardware interrupt"},
  298. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "transaction abort"},
  299. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "trace begin"},
  300. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "trace end"},
  301. {0, NULL}
  302. };
  303. int db_export__branch_types(struct db_export *dbe)
  304. {
  305. int i, err = 0;
  306. for (i = 0; branch_types[i].name ; i++) {
  307. err = db_export__branch_type(dbe, branch_types[i].branch_type,
  308. branch_types[i].name);
  309. if (err)
  310. break;
  311. }
  312. return err;
  313. }
  314. int db_export__call_path(struct db_export *dbe, struct call_path *cp)
  315. {
  316. int err;
  317. if (cp->db_id)
  318. return 0;
  319. if (cp->parent) {
  320. err = db_export__call_path(dbe, cp->parent);
  321. if (err)
  322. return err;
  323. }
  324. cp->db_id = ++dbe->call_path_last_db_id;
  325. if (dbe->export_call_path)
  326. return dbe->export_call_path(dbe, cp);
  327. return 0;
  328. }
  329. int db_export__call_return(struct db_export *dbe, struct call_return *cr)
  330. {
  331. int err;
  332. if (cr->db_id)
  333. return 0;
  334. err = db_export__call_path(dbe, cr->cp);
  335. if (err)
  336. return err;
  337. cr->db_id = ++dbe->call_return_last_db_id;
  338. if (dbe->export_call_return)
  339. return dbe->export_call_return(dbe, cr);
  340. return 0;
  341. }