OLD | NEW |
---|---|
1 /* Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 /* Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 * Use of this source code is governed by a BSD-style license that can be | 2 * Use of this source code is governed by a BSD-style license that can be |
3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
4 */ | 4 */ |
5 | 5 |
6 | 6 |
7 /* XRay -- a simple profiler for Native Client */ | 7 /* XRay -- a simple profiler for Native Client */ |
8 | 8 |
9 #include <assert.h> | 9 #include <assert.h> |
10 #include <errno.h> | 10 #include <errno.h> |
(...skipping 22 matching lines...) Expand all Loading... | |
33 #define GTSC(_x) __asm__ __volatile__ ("rdtsc" : "=A" (_x)); | 33 #define GTSC(_x) __asm__ __volatile__ ("rdtsc" : "=A" (_x)); |
34 #else | 34 #else |
35 FORCE_INLINE uint64_t GTOD() { | 35 FORCE_INLINE uint64_t GTOD() { |
36 struct timeval tv; | 36 struct timeval tv; |
37 gettimeofday(&tv, NULL); | 37 gettimeofday(&tv, NULL); |
38 return (uint64_t)tv.tv_sec * 1000000 + (uint64_t)tv.tv_usec; | 38 return (uint64_t)tv.tv_sec * 1000000 + (uint64_t)tv.tv_usec; |
39 } | 39 } |
40 #define GTSC(_x) _x = GTOD(); | 40 #define GTSC(_x) _x = GTOD(); |
41 #endif | 41 #endif |
42 | 42 |
43 | |
44 /* Use a TLS variable for cheap thread uid. */ | 43 /* Use a TLS variable for cheap thread uid. */ |
45 __thread struct XRayTraceCapture* g_xray_capture = NULL; | 44 __thread struct XRayTraceCapture* g_xray_capture = NULL; |
45 __thread int g_xray_thread_id_dummy = 0; | |
nfullagar1
2013/07/18 18:21:06
nit: another word besides dummy
grosse
2013/07/18 20:45:37
Done.
| |
46 | 46 |
47 | 47 |
48 struct XRayTraceStackEntry { | 48 struct XRayTraceStackEntry { |
49 uint32_t depth_addr; | 49 uint32_t depth_addr; |
50 uint64_t tsc; | 50 uint64_t tsc; |
51 uint32_t dest; | 51 uint32_t dest; |
52 uint32_t annotation_index; | 52 uint32_t annotation_index; |
53 }; | 53 }; |
54 | 54 |
55 | 55 |
56 struct XRayTraceFrameEntry { | 56 struct XRayTraceFrameEntry { |
57 /* Indices into global tracebuffer */ | 57 /* Indices into global tracebuffer */ |
58 int start; | 58 int start; |
59 int end; | 59 int end; |
60 uint64_t start_tsc; | 60 uint64_t start_tsc; |
61 uint64_t end_tsc; | 61 uint64_t end_tsc; |
62 uint64_t total_ticks; | 62 uint64_t total_ticks; |
63 int annotation_count; | 63 int annotation_count; |
64 bool valid; | 64 bool valid; |
65 | |
66 #ifndef XRAY_DISABLE_BROWSER_INTEGRATION | |
67 struct XRayTimestampPair start_time; | |
68 struct XRayTimestampPair end_time; | |
69 #endif | |
65 }; | 70 }; |
66 | 71 |
67 | 72 |
68 struct XRayTraceFrame { | 73 struct XRayTraceFrame { |
69 struct XRayTraceFrameEntry* entry; | 74 struct XRayTraceFrameEntry* entry; |
70 int head; | 75 int head; |
71 int tail; | 76 int tail; |
72 int count; | 77 int count; |
73 }; | 78 }; |
74 | 79 |
(...skipping 11 matching lines...) Expand all Loading... | |
86 bool initialized; | 91 bool initialized; |
87 uint32_t annotation_filter; | 92 uint32_t annotation_filter; |
88 uint32_t guard0; | 93 uint32_t guard0; |
89 struct XRayTraceStackEntry stack[XRAY_TRACE_STACK_SIZE] XRAY_ALIGN64; | 94 struct XRayTraceStackEntry stack[XRAY_TRACE_STACK_SIZE] XRAY_ALIGN64; |
90 uint32_t guard1; | 95 uint32_t guard1; |
91 uint32_t guard2; | 96 uint32_t guard2; |
92 char annotation[XRAY_ANNOTATION_STACK_SIZE] XRAY_ALIGN64; | 97 char annotation[XRAY_ANNOTATION_STACK_SIZE] XRAY_ALIGN64; |
93 uint32_t guard3; | 98 uint32_t guard3; |
94 struct XRayTraceBufferEntry* buffer; | 99 struct XRayTraceBufferEntry* buffer; |
95 struct XRayTraceFrame frame; | 100 struct XRayTraceFrame frame; |
101 | |
102 #ifndef XRAY_DISABLE_BROWSER_INTEGRATION | |
103 int32_t thread_id; | |
104 #endif | |
96 } XRAY_ALIGN64; | 105 } XRAY_ALIGN64; |
97 | 106 |
98 | 107 |
99 #ifdef __cplusplus | 108 #ifdef __cplusplus |
100 extern "C" { | 109 extern "C" { |
101 #endif | 110 #endif |
102 | 111 |
103 XRAY_NO_INSTRUMENT void __cyg_profile_func_enter(void* this_fn, | 112 #if defined(__pnacl__) |
104 void* call_site); | 113 XRAY_NO_INSTRUMENT void __pnacl_profile_func_enter(const char* fname); |
105 XRAY_NO_INSTRUMENT void __cyg_profile_func_exit(void* this_fn, | 114 XRAY_NO_INSTRUMENT void __pnacl_profile_func_exit(const char* fname); |
106 void* call_site); | 115 #else |
116 XRAY_NO_INSTRUMENT void __cyg_profile_func_enter( | |
117 void* this_fn, void* call_site); | |
118 XRAY_NO_INSTRUMENT void __cyg_profile_func_exit(void* this_fn, void* call_site); | |
119 #endif | |
120 | |
107 XRAY_NO_INSTRUMENT void __xray_profile_append_annotation( | 121 XRAY_NO_INSTRUMENT void __xray_profile_append_annotation( |
108 struct XRayTraceCapture* capture, | 122 struct XRayTraceCapture* capture, |
109 struct XRayTraceStackEntry* se, | 123 struct XRayTraceStackEntry* se, |
110 struct XRayTraceBufferEntry* be); | 124 struct XRayTraceBufferEntry* be); |
111 | 125 |
112 #ifdef __cplusplus | 126 #ifdef __cplusplus |
113 } | 127 } |
114 #endif | 128 #endif |
115 | 129 |
116 | |
117 /* Asserts that the guard values haven't changed. */ | 130 /* Asserts that the guard values haven't changed. */ |
118 void XRayCheckGuards(struct XRayTraceCapture* capture) { | 131 void XRayCheckGuards(struct XRayTraceCapture* capture) { |
119 assert(capture->guard0 == XRAY_GUARD_VALUE_0x12345678); | 132 assert(capture->guard0 == XRAY_GUARD_VALUE_0x12345678); |
120 assert(capture->guard1 == XRAY_GUARD_VALUE_0x12345678); | 133 assert(capture->guard1 == XRAY_GUARD_VALUE_0x12345678); |
121 assert(capture->guard2 == XRAY_GUARD_VALUE_0x87654321); | 134 assert(capture->guard2 == XRAY_GUARD_VALUE_0x87654321); |
122 assert(capture->guard3 == XRAY_GUARD_VALUE_0x12345678); | 135 assert(capture->guard3 == XRAY_GUARD_VALUE_0x12345678); |
123 } | 136 } |
124 | 137 |
125 /* Decrements the trace index, wrapping around if needed. */ | 138 /* Decrements the trace index, wrapping around if needed. */ |
126 XRAY_FORCE_INLINE int XRayTraceDecrementIndexInline( | 139 XRAY_FORCE_INLINE int XRayTraceDecrementIndexInline( |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
296 } | 309 } |
297 | 310 |
298 | 311 |
299 /* Generic memory free for XRay */ | 312 /* Generic memory free for XRay */ |
300 void XRayFree(void* data) { | 313 void XRayFree(void* data) { |
301 assert(NULL != data); | 314 assert(NULL != data); |
302 free(data); | 315 free(data); |
303 } | 316 } |
304 | 317 |
305 | 318 |
306 | |
307 /* Main profile capture function that is called at the start */ | 319 /* Main profile capture function that is called at the start */ |
308 /* of every instrumented function. This function is implicitly */ | 320 /* of every instrumented function. This function is implicitly */ |
309 /* called when code is compilied with the -finstrument-functions option */ | 321 /* called when code is compilied with the -finstrument-functions option */ |
322 #if defined(__pnacl__) | |
323 void __pnacl_profile_func_enter(const char* this_fn) { | |
324 #else | |
310 void __cyg_profile_func_enter(void* this_fn, void* call_site) { | 325 void __cyg_profile_func_enter(void* this_fn, void* call_site) { |
326 #endif | |
311 struct XRayTraceCapture* capture = g_xray_capture; | 327 struct XRayTraceCapture* capture = g_xray_capture; |
312 if (capture && capture->recording) { | 328 if (capture && capture->recording) { |
313 uint32_t depth = capture->stack_depth; | 329 uint32_t depth = capture->stack_depth; |
314 if (depth < capture->max_stack_depth) { | 330 if (depth < capture->max_stack_depth) { |
315 struct XRayTraceStackEntry* se = &capture->stack[depth]; | 331 struct XRayTraceStackEntry* se = &capture->stack[depth]; |
316 uint32_t addr = (uint32_t)this_fn; | 332 uint32_t addr = (uint32_t)this_fn; |
317 se->depth_addr = XRAY_PACK_DEPTH_ADDR(depth, addr); | 333 se->depth_addr = XRAY_PACK_DEPTH_ADDR(depth, addr); |
318 se->dest = capture->buffer_index; | 334 se->dest = capture->buffer_index; |
319 se->annotation_index = 0; | 335 se->annotation_index = 0; |
320 GTSC(se->tsc); | 336 GTSC(se->tsc); |
321 capture->buffer_index = | 337 capture->buffer_index = |
322 XRayTraceIncrementIndexInline(capture, capture->buffer_index); | 338 XRayTraceIncrementIndexInline(capture, capture->buffer_index); |
323 } | 339 } |
324 ++capture->stack_depth; | 340 ++capture->stack_depth; |
325 } | 341 } |
326 } | 342 } |
327 | 343 |
328 | 344 |
329 /* Main profile capture function that is called at the exit of */ | 345 /* Main profile capture function that is called at the exit of */ |
330 /* every instrumented function. This function is implicity called */ | 346 /* every instrumented function. This function is implicity called */ |
331 /* when the code is compiled with the -finstrument-functions option */ | 347 /* when the code is compiled with the -finstrument-functions option */ |
348 #if defined(__pnacl__) | |
349 void __pnacl_profile_func_exit(const char* this_fn) { | |
350 #else | |
332 void __cyg_profile_func_exit(void* this_fn, void* call_site) { | 351 void __cyg_profile_func_exit(void* this_fn, void* call_site) { |
352 #endif | |
333 struct XRayTraceCapture* capture = g_xray_capture; | 353 struct XRayTraceCapture* capture = g_xray_capture; |
334 if (capture && capture->recording) { | 354 if (capture && capture->recording) { |
335 --capture->stack_depth; | 355 --capture->stack_depth; |
336 if (capture->stack_depth < capture->max_stack_depth) { | 356 if (capture->stack_depth < capture->max_stack_depth) { |
337 uint32_t depth = capture->stack_depth; | 357 uint32_t depth = capture->stack_depth; |
338 struct XRayTraceStackEntry* se = &capture->stack[depth]; | 358 struct XRayTraceStackEntry* se = &capture->stack[depth]; |
339 uint32_t buffer_index = se->dest; | 359 uint32_t buffer_index = se->dest; |
340 uint64_t tsc; | 360 uint64_t tsc; |
341 struct XRayTraceBufferEntry* be = &capture->buffer[buffer_index]; | 361 struct XRayTraceBufferEntry* be = &capture->buffer[buffer_index]; |
342 GTSC(tsc); | 362 GTSC(tsc); |
343 be->depth_addr = se->depth_addr; | 363 be->depth_addr = se->depth_addr; |
344 be->start_tick = se->tsc; | 364 be->start_tick = se->tsc; |
345 be->end_tick = tsc; | 365 be->end_tick = tsc; |
346 be->annotation_index = 0; | 366 be->annotation_index = 0; |
347 if (0 != se->annotation_index) | 367 if (0 != se->annotation_index) |
348 __xray_profile_append_annotation(capture, se, be); | 368 __xray_profile_append_annotation(capture, se, be); |
349 } | 369 } |
350 } | 370 } |
351 } | 371 } |
352 | 372 |
373 #ifndef XRAY_DISABLE_BROWSER_INTEGRATION | |
374 void XRayGetTSC(uint64_t* tsc) { | |
375 GTSC(*tsc); | |
376 } | |
377 | |
378 int32_t XRayGetSavedThreadID(struct XRayTraceCapture* capture) { | |
379 return capture->thread_id; | |
380 } | |
381 | |
382 struct XRayTimestampPair* XRayGetTimestamp( | |
nfullagar1
2013/07/18 18:21:07
Not sure I agree here returning a pointer into the
grosse
2013/07/18 20:45:37
Done.
| |
383 struct XRayTraceCapture* capture, int frame, bool end) { | |
384 | |
385 struct XRayTraceFrameEntry* entry = &(capture->frame.entry[frame]); | |
386 return end ? &entry->end_time : &entry->start_time; | |
387 } | |
388 #endif | |
353 | 389 |
354 /* Special case appending annotation string to trace buffer */ | 390 /* Special case appending annotation string to trace buffer */ |
355 /* this function should only ever be called from __cyg_profile_func_exit() */ | 391 /* this function should only ever be called from __cyg_profile_func_exit() */ |
356 void __xray_profile_append_annotation(struct XRayTraceCapture* capture, | 392 void __xray_profile_append_annotation(struct XRayTraceCapture* capture, |
357 struct XRayTraceStackEntry* se, | 393 struct XRayTraceStackEntry* se, |
358 struct XRayTraceBufferEntry* be) { | 394 struct XRayTraceBufferEntry* be) { |
359 struct XRayTraceStackEntry* parent = se - 1; | 395 struct XRayTraceStackEntry* parent = se - 1; |
360 int start = parent->annotation_index; | 396 int start = parent->annotation_index; |
361 be->annotation_index = capture->buffer_index; | 397 be->annotation_index = capture->buffer_index; |
362 char* str = &capture->annotation[start]; | 398 char* str = &capture->annotation[start]; |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
566 /* The trace stack[0] is reserved */ | 602 /* The trace stack[0] is reserved */ |
567 memset(&capture->stack[0], 0, sizeof(capture->stack[0])); | 603 memset(&capture->stack[0], 0, sizeof(capture->stack[0])); |
568 /* Annotation index 0 is reserved to indicate no annotation */ | 604 /* Annotation index 0 is reserved to indicate no annotation */ |
569 capture->stack[0].annotation_index = 1; | 605 capture->stack[0].annotation_index = 1; |
570 capture->annotation[0] = 0; | 606 capture->annotation[0] = 0; |
571 capture->annotation[1] = 0; | 607 capture->annotation[1] = 0; |
572 capture->annotation_count = 0; | 608 capture->annotation_count = 0; |
573 capture->recording = true; | 609 capture->recording = true; |
574 GTSC(capture->frame.entry[i].start_tsc); | 610 GTSC(capture->frame.entry[i].start_tsc); |
575 g_xray_capture = capture; | 611 g_xray_capture = capture; |
612 | |
613 #ifndef XRAY_DISABLE_BROWSER_INTEGRATION | |
614 capture->frame.entry[i].start_time = XRayGenerateTimestampsNow(); | |
615 #endif /* XRAY_DISABLE_BROWSER_INTEGRATION */ | |
616 | |
576 } | 617 } |
577 | 618 |
578 | 619 |
579 /* Ends a frame and disables capturing. Advances to the next frame. */ | 620 /* Ends a frame and disables capturing. Advances to the next frame. */ |
580 /* Must be paired with XRayStartFrame(), and called from the same thread. */ | 621 /* Must be paired with XRayStartFrame(), and called from the same thread. */ |
581 void XRayEndFrame(struct XRayTraceCapture* capture) { | 622 void XRayEndFrame(struct XRayTraceCapture* capture) { |
582 int i; | 623 int i; |
583 assert(capture); | 624 assert(capture); |
584 assert(capture->initialized); | 625 assert(capture->initialized); |
585 assert(capture->recording); | 626 assert(capture->recording); |
(...skipping 19 matching lines...) Expand all Loading... | |
605 if (be->depth_addr != XRAY_FRAME_MARKER) { | 646 if (be->depth_addr != XRAY_FRAME_MARKER) { |
606 fprintf(stderr, | 647 fprintf(stderr, |
607 "XRay: XRayStopFrame() detects insufficient trace buffer size!\n"); | 648 "XRay: XRayStopFrame() detects insufficient trace buffer size!\n"); |
608 XRayReset(capture); | 649 XRayReset(capture); |
609 } else { | 650 } else { |
610 /* Replace marker with an empty annotation string. */ | 651 /* Replace marker with an empty annotation string. */ |
611 be->depth_addr = XRAY_NULL_ANNOTATION; | 652 be->depth_addr = XRAY_NULL_ANNOTATION; |
612 XRayCheckGuards(capture); | 653 XRayCheckGuards(capture); |
613 } | 654 } |
614 g_xray_capture = NULL; | 655 g_xray_capture = NULL; |
656 | |
657 #ifndef XRAY_DISABLE_BROWSER_INTEGRATION | |
658 capture->frame.entry[i].end_time = XRayGenerateTimestampsNow(); | |
659 #endif /* XRAY_DISABLE_BROWSER_INTEGRATION */ | |
615 } | 660 } |
616 | 661 |
617 | 662 |
618 /* Get the last frame captured. Do not call while capturing. */ | 663 /* Get the last frame captured. Do not call while capturing. */ |
619 /* (ie call outside of XRayStartFrame() / XRayStopFrame() pair) */ | 664 /* (ie call outside of XRayStartFrame() / XRayStopFrame() pair) */ |
620 int XRayGetLastFrame(struct XRayTraceCapture* capture) { | 665 int XRayGetLastFrame(struct XRayTraceCapture* capture) { |
621 assert(capture); | 666 assert(capture); |
622 assert(capture->initialized); | 667 assert(capture->initialized); |
623 assert(!capture->recording); | 668 assert(!capture->recording); |
624 assert(0 == capture->disabled); | 669 assert(0 == capture->disabled); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
692 capture->recording = false; | 737 capture->recording = false; |
693 XRaySetMaxStackDepth(capture, stack_depth); | 738 XRaySetMaxStackDepth(capture, stack_depth); |
694 XRayReset(capture); | 739 XRayReset(capture); |
695 | 740 |
696 /* Mapfile is optional; we don't need it for captures, only for reports. */ | 741 /* Mapfile is optional; we don't need it for captures, only for reports. */ |
697 capture->symbols = | 742 capture->symbols = |
698 XRaySymbolTableCreate(XRAY_DEFAULT_SYMBOL_TABLE_SIZE); | 743 XRaySymbolTableCreate(XRAY_DEFAULT_SYMBOL_TABLE_SIZE); |
699 if (NULL != mapfilename) | 744 if (NULL != mapfilename) |
700 XRaySymbolTableParseMapfile(capture->symbols, mapfilename); | 745 XRaySymbolTableParseMapfile(capture->symbols, mapfilename); |
701 | 746 |
747 #ifndef XRAY_DISABLE_BROWSER_INTEGRATION | |
748 /* Use address of a thread local variable as for our fake thread id. */ | |
nfullagar1
2013/07/18 18:21:07
above comment: re-word '...variable as for...'
grosse
2013/07/18 20:45:37
Done.
| |
749 capture->thread_id = (int32_t)(&g_xray_thread_id_dummy); | |
750 #endif | |
751 | |
702 return capture; | 752 return capture; |
703 } | 753 } |
704 | 754 |
705 | 755 |
706 /* Shut down and free memory used by XRay. */ | 756 /* Shut down and free memory used by XRay. */ |
707 void XRayShutdown(struct XRayTraceCapture* capture) { | 757 void XRayShutdown(struct XRayTraceCapture* capture) { |
708 assert(capture); | 758 assert(capture); |
709 assert(capture->initialized); | 759 assert(capture->initialized); |
710 assert(!capture->recording); | 760 assert(!capture->recording); |
711 XRayCheckGuards(capture); | 761 XRayCheckGuards(capture); |
712 if (NULL != capture->symbols) { | 762 if (NULL != capture->symbols) { |
713 XRaySymbolTableFree(capture->symbols); | 763 XRaySymbolTableFree(capture->symbols); |
714 } | 764 } |
715 XRayFree(capture->frame.entry); | 765 XRayFree(capture->frame.entry); |
716 XRayFree(capture->buffer); | 766 XRayFree(capture->buffer); |
717 capture->initialized = false; | 767 capture->initialized = false; |
718 XRayFree(capture); | 768 XRayFree(capture); |
719 } | 769 } |
720 | 770 |
721 #endif /* XRAY */ | 771 #endif /* XRAY */ |
OLD | NEW |