trace-event-read.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /*
  2. * Copyright (C) 2009, Steven Rostedt <srostedt@redhat.com>
  3. *
  4. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License (not later!)
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  20. */
  21. #define _LARGEFILE64_SOURCE
  22. #include <dirent.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <getopt.h>
  27. #include <stdarg.h>
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <sys/wait.h>
  31. #include <sys/mman.h>
  32. #include <pthread.h>
  33. #include <fcntl.h>
  34. #include <unistd.h>
  35. #include <ctype.h>
  36. #include <errno.h>
  37. #include "../perf.h"
  38. #include "util.h"
  39. #include "trace-event.h"
  40. static int input_fd;
  41. static int read_page;
  42. int file_bigendian;
  43. int host_bigendian;
  44. static int long_size;
  45. static unsigned long page_size;
  46. static int read_or_die(void *data, int size)
  47. {
  48. int r;
  49. r = read(input_fd, data, size);
  50. if (r != size)
  51. die("reading input file (size expected=%d received=%d)",
  52. size, r);
  53. return r;
  54. }
  55. static unsigned int read4(void)
  56. {
  57. unsigned int data;
  58. read_or_die(&data, 4);
  59. return __data2host4(data);
  60. }
  61. static unsigned long long read8(void)
  62. {
  63. unsigned long long data;
  64. read_or_die(&data, 8);
  65. return __data2host8(data);
  66. }
  67. static char *read_string(void)
  68. {
  69. char buf[BUFSIZ];
  70. char *str = NULL;
  71. int size = 0;
  72. int i;
  73. int r;
  74. for (;;) {
  75. r = read(input_fd, buf, BUFSIZ);
  76. if (r < 0)
  77. die("reading input file");
  78. if (!r)
  79. die("no data");
  80. for (i = 0; i < r; i++) {
  81. if (!buf[i])
  82. break;
  83. }
  84. if (i < r)
  85. break;
  86. if (str) {
  87. size += BUFSIZ;
  88. str = realloc(str, size);
  89. if (!str)
  90. die("malloc of size %d", size);
  91. memcpy(str + (size - BUFSIZ), buf, BUFSIZ);
  92. } else {
  93. size = BUFSIZ;
  94. str = malloc_or_die(size);
  95. memcpy(str, buf, size);
  96. }
  97. }
  98. /* move the file descriptor to the end of the string */
  99. r = lseek(input_fd, -(r - (i+1)), SEEK_CUR);
  100. if (r < 0)
  101. die("lseek");
  102. if (str) {
  103. size += i;
  104. str = realloc(str, size);
  105. if (!str)
  106. die("malloc of size %d", size);
  107. memcpy(str + (size - i), buf, i);
  108. } else {
  109. size = i;
  110. str = malloc_or_die(i);
  111. memcpy(str, buf, i);
  112. }
  113. return str;
  114. }
  115. static void read_proc_kallsyms(void)
  116. {
  117. unsigned int size;
  118. char *buf;
  119. size = read4();
  120. if (!size)
  121. return;
  122. buf = malloc_or_die(size);
  123. read_or_die(buf, size);
  124. parse_proc_kallsyms(buf, size);
  125. free(buf);
  126. }
  127. static void read_ftrace_printk(void)
  128. {
  129. unsigned int size;
  130. char *buf;
  131. size = read4();
  132. if (!size)
  133. return;
  134. buf = malloc_or_die(size);
  135. read_or_die(buf, size);
  136. parse_ftrace_printk(buf, size);
  137. free(buf);
  138. }
  139. static void read_header_files(void)
  140. {
  141. unsigned long long size;
  142. char *header_page;
  143. char *header_event;
  144. char buf[BUFSIZ];
  145. read_or_die(buf, 12);
  146. if (memcmp(buf, "header_page", 12) != 0)
  147. die("did not read header page");
  148. size = read8();
  149. header_page = malloc_or_die(size);
  150. read_or_die(header_page, size);
  151. parse_header_page(header_page, size);
  152. free(header_page);
  153. /*
  154. * The size field in the page is of type long,
  155. * use that instead, since it represents the kernel.
  156. */
  157. long_size = header_page_size_size;
  158. read_or_die(buf, 13);
  159. if (memcmp(buf, "header_event", 13) != 0)
  160. die("did not read header event");
  161. size = read8();
  162. header_event = malloc_or_die(size);
  163. read_or_die(header_event, size);
  164. free(header_event);
  165. }
  166. static void read_ftrace_file(unsigned long long size)
  167. {
  168. char *buf;
  169. buf = malloc_or_die(size);
  170. read_or_die(buf, size);
  171. parse_ftrace_file(buf, size);
  172. free(buf);
  173. }
  174. static void read_event_file(char *sys, unsigned long long size)
  175. {
  176. char *buf;
  177. buf = malloc_or_die(size);
  178. read_or_die(buf, size);
  179. parse_event_file(buf, size, sys);
  180. free(buf);
  181. }
  182. static void read_ftrace_files(void)
  183. {
  184. unsigned long long size;
  185. int count;
  186. int i;
  187. count = read4();
  188. for (i = 0; i < count; i++) {
  189. size = read8();
  190. read_ftrace_file(size);
  191. }
  192. }
  193. static void read_event_files(void)
  194. {
  195. unsigned long long size;
  196. char *sys;
  197. int systems;
  198. int count;
  199. int i,x;
  200. systems = read4();
  201. for (i = 0; i < systems; i++) {
  202. sys = read_string();
  203. count = read4();
  204. for (x=0; x < count; x++) {
  205. size = read8();
  206. read_event_file(sys, size);
  207. }
  208. }
  209. }
  210. struct cpu_data {
  211. unsigned long long offset;
  212. unsigned long long size;
  213. unsigned long long timestamp;
  214. struct record *next;
  215. char *page;
  216. int cpu;
  217. int index;
  218. int page_size;
  219. };
  220. static struct cpu_data *cpu_data;
  221. static void update_cpu_data_index(int cpu)
  222. {
  223. cpu_data[cpu].offset += page_size;
  224. cpu_data[cpu].size -= page_size;
  225. cpu_data[cpu].index = 0;
  226. }
  227. static void get_next_page(int cpu)
  228. {
  229. off64_t save_seek;
  230. off64_t ret;
  231. if (!cpu_data[cpu].page)
  232. return;
  233. if (read_page) {
  234. if (cpu_data[cpu].size <= page_size) {
  235. free(cpu_data[cpu].page);
  236. cpu_data[cpu].page = NULL;
  237. return;
  238. }
  239. update_cpu_data_index(cpu);
  240. /* other parts of the code may expect the pointer to not move */
  241. save_seek = lseek64(input_fd, 0, SEEK_CUR);
  242. ret = lseek64(input_fd, cpu_data[cpu].offset, SEEK_SET);
  243. if (ret < 0)
  244. die("failed to lseek");
  245. ret = read(input_fd, cpu_data[cpu].page, page_size);
  246. if (ret < 0)
  247. die("failed to read page");
  248. /* reset the file pointer back */
  249. lseek64(input_fd, save_seek, SEEK_SET);
  250. return;
  251. }
  252. munmap(cpu_data[cpu].page, page_size);
  253. cpu_data[cpu].page = NULL;
  254. if (cpu_data[cpu].size <= page_size)
  255. return;
  256. update_cpu_data_index(cpu);
  257. cpu_data[cpu].page = mmap(NULL, page_size, PROT_READ, MAP_PRIVATE,
  258. input_fd, cpu_data[cpu].offset);
  259. if (cpu_data[cpu].page == MAP_FAILED)
  260. die("failed to mmap cpu %d at offset 0x%llx",
  261. cpu, cpu_data[cpu].offset);
  262. }
  263. static unsigned int type_len4host(unsigned int type_len_ts)
  264. {
  265. if (file_bigendian)
  266. return (type_len_ts >> 27) & ((1 << 5) - 1);
  267. else
  268. return type_len_ts & ((1 << 5) - 1);
  269. }
  270. static unsigned int ts4host(unsigned int type_len_ts)
  271. {
  272. if (file_bigendian)
  273. return type_len_ts & ((1 << 27) - 1);
  274. else
  275. return type_len_ts >> 5;
  276. }
  277. static int calc_index(void *ptr, int cpu)
  278. {
  279. return (unsigned long)ptr - (unsigned long)cpu_data[cpu].page;
  280. }
  281. struct record *trace_peek_data(int cpu)
  282. {
  283. struct record *data;
  284. void *page = cpu_data[cpu].page;
  285. int idx = cpu_data[cpu].index;
  286. void *ptr = page + idx;
  287. unsigned long long extend;
  288. unsigned int type_len_ts;
  289. unsigned int type_len;
  290. unsigned int delta;
  291. unsigned int length = 0;
  292. if (cpu_data[cpu].next)
  293. return cpu_data[cpu].next;
  294. if (!page)
  295. return NULL;
  296. if (!idx) {
  297. /* FIXME: handle header page */
  298. if (header_page_ts_size != 8)
  299. die("expected a long long type for timestamp");
  300. cpu_data[cpu].timestamp = data2host8(ptr);
  301. ptr += 8;
  302. switch (header_page_size_size) {
  303. case 4:
  304. cpu_data[cpu].page_size = data2host4(ptr);
  305. ptr += 4;
  306. break;
  307. case 8:
  308. cpu_data[cpu].page_size = data2host8(ptr);
  309. ptr += 8;
  310. break;
  311. default:
  312. die("bad long size");
  313. }
  314. ptr = cpu_data[cpu].page + header_page_data_offset;
  315. }
  316. read_again:
  317. idx = calc_index(ptr, cpu);
  318. if (idx >= cpu_data[cpu].page_size) {
  319. get_next_page(cpu);
  320. return trace_peek_data(cpu);
  321. }
  322. type_len_ts = data2host4(ptr);
  323. ptr += 4;
  324. type_len = type_len4host(type_len_ts);
  325. delta = ts4host(type_len_ts);
  326. switch (type_len) {
  327. case RINGBUF_TYPE_PADDING:
  328. if (!delta)
  329. die("error, hit unexpected end of page");
  330. length = data2host4(ptr);
  331. ptr += 4;
  332. length *= 4;
  333. ptr += length;
  334. goto read_again;
  335. case RINGBUF_TYPE_TIME_EXTEND:
  336. extend = data2host4(ptr);
  337. ptr += 4;
  338. extend <<= TS_SHIFT;
  339. extend += delta;
  340. cpu_data[cpu].timestamp += extend;
  341. goto read_again;
  342. case RINGBUF_TYPE_TIME_STAMP:
  343. ptr += 12;
  344. break;
  345. case 0:
  346. length = data2host4(ptr);
  347. ptr += 4;
  348. die("here! length=%d", length);
  349. break;
  350. default:
  351. length = type_len * 4;
  352. break;
  353. }
  354. cpu_data[cpu].timestamp += delta;
  355. data = malloc_or_die(sizeof(*data));
  356. memset(data, 0, sizeof(*data));
  357. data->ts = cpu_data[cpu].timestamp;
  358. data->size = length;
  359. data->data = ptr;
  360. ptr += length;
  361. cpu_data[cpu].index = calc_index(ptr, cpu);
  362. cpu_data[cpu].next = data;
  363. return data;
  364. }
  365. struct record *trace_read_data(int cpu)
  366. {
  367. struct record *data;
  368. data = trace_peek_data(cpu);
  369. cpu_data[cpu].next = NULL;
  370. return data;
  371. }
  372. void trace_report (void)
  373. {
  374. const char *input_file = "trace.info";
  375. char buf[BUFSIZ];
  376. char test[] = { 23, 8, 68 };
  377. char *version;
  378. int show_funcs = 0;
  379. int show_printk = 0;
  380. input_fd = open(input_file, O_RDONLY);
  381. if (input_fd < 0)
  382. die("opening '%s'\n", input_file);
  383. read_or_die(buf, 3);
  384. if (memcmp(buf, test, 3) != 0)
  385. die("not an trace data file");
  386. read_or_die(buf, 7);
  387. if (memcmp(buf, "tracing", 7) != 0)
  388. die("not a trace file (missing tracing)");
  389. version = read_string();
  390. printf("version = %s\n", version);
  391. free(version);
  392. read_or_die(buf, 1);
  393. file_bigendian = buf[0];
  394. host_bigendian = bigendian();
  395. read_or_die(buf, 1);
  396. long_size = buf[0];
  397. page_size = read4();
  398. read_header_files();
  399. read_ftrace_files();
  400. read_event_files();
  401. read_proc_kallsyms();
  402. read_ftrace_printk();
  403. if (show_funcs) {
  404. print_funcs();
  405. return;
  406. }
  407. if (show_printk) {
  408. print_printk();
  409. return;
  410. }
  411. return;
  412. }