Chromium Code Reviews| 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_placeholder = 0; | |
| 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 | |
|
nfullagar1
2013/07/18 21:36:00
nit: there's a mix of #endif and #endif /* XRAY_D
grosse
2013/07/18 23:00:06
Done.
| |
| 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); | |
|
nfullagar1
2013/07/18 21:36:00
above: see earlier comment re: indent as it was be
grosse
2013/07/18 23:00:06
Done.
| |
| 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 XRayFrameGetStartTimestampPair( | |
| 383 struct XRayTraceCapture* capture, int frame) { | |
| 384 return capture->frame.entry[frame].start_time; | |
| 385 } | |
| 386 | |
| 387 struct XRayTimestampPair XRayFrameGetEndTimestampPair( | |
| 388 struct XRayTraceCapture* capture, int frame) { | |
| 389 return capture->frame.entry[frame].end_time; | |
| 390 } | |
| 391 #endif | |
| 353 | 392 |
| 354 /* Special case appending annotation string to trace buffer */ | 393 /* Special case appending annotation string to trace buffer */ |
| 355 /* this function should only ever be called from __cyg_profile_func_exit() */ | 394 /* this function should only ever be called from __cyg_profile_func_exit() */ |
| 356 void __xray_profile_append_annotation(struct XRayTraceCapture* capture, | 395 void __xray_profile_append_annotation(struct XRayTraceCapture* capture, |
| 357 struct XRayTraceStackEntry* se, | 396 struct XRayTraceStackEntry* se, |
| 358 struct XRayTraceBufferEntry* be) { | 397 struct XRayTraceBufferEntry* be) { |
| 359 struct XRayTraceStackEntry* parent = se - 1; | 398 struct XRayTraceStackEntry* parent = se - 1; |
| 360 int start = parent->annotation_index; | 399 int start = parent->annotation_index; |
| 361 be->annotation_index = capture->buffer_index; | 400 be->annotation_index = capture->buffer_index; |
| 362 char* str = &capture->annotation[start]; | 401 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 */ | 605 /* The trace stack[0] is reserved */ |
| 567 memset(&capture->stack[0], 0, sizeof(capture->stack[0])); | 606 memset(&capture->stack[0], 0, sizeof(capture->stack[0])); |
| 568 /* Annotation index 0 is reserved to indicate no annotation */ | 607 /* Annotation index 0 is reserved to indicate no annotation */ |
| 569 capture->stack[0].annotation_index = 1; | 608 capture->stack[0].annotation_index = 1; |
| 570 capture->annotation[0] = 0; | 609 capture->annotation[0] = 0; |
| 571 capture->annotation[1] = 0; | 610 capture->annotation[1] = 0; |
| 572 capture->annotation_count = 0; | 611 capture->annotation_count = 0; |
| 573 capture->recording = true; | 612 capture->recording = true; |
| 574 GTSC(capture->frame.entry[i].start_tsc); | 613 GTSC(capture->frame.entry[i].start_tsc); |
| 575 g_xray_capture = capture; | 614 g_xray_capture = capture; |
| 615 | |
| 616 #ifndef XRAY_DISABLE_BROWSER_INTEGRATION | |
| 617 capture->frame.entry[i].start_time = XRayGenerateTimestampsNow(); | |
| 618 #endif /* XRAY_DISABLE_BROWSER_INTEGRATION */ | |
| 619 | |
| 576 } | 620 } |
| 577 | 621 |
| 578 | 622 |
| 579 /* Ends a frame and disables capturing. Advances to the next frame. */ | 623 /* Ends a frame and disables capturing. Advances to the next frame. */ |
| 580 /* Must be paired with XRayStartFrame(), and called from the same thread. */ | 624 /* Must be paired with XRayStartFrame(), and called from the same thread. */ |
| 581 void XRayEndFrame(struct XRayTraceCapture* capture) { | 625 void XRayEndFrame(struct XRayTraceCapture* capture) { |
| 582 int i; | 626 int i; |
| 583 assert(capture); | 627 assert(capture); |
| 584 assert(capture->initialized); | 628 assert(capture->initialized); |
| 585 assert(capture->recording); | 629 assert(capture->recording); |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 605 if (be->depth_addr != XRAY_FRAME_MARKER) { | 649 if (be->depth_addr != XRAY_FRAME_MARKER) { |
| 606 fprintf(stderr, | 650 fprintf(stderr, |
| 607 "XRay: XRayStopFrame() detects insufficient trace buffer size!\n"); | 651 "XRay: XRayStopFrame() detects insufficient trace buffer size!\n"); |
| 608 XRayReset(capture); | 652 XRayReset(capture); |
| 609 } else { | 653 } else { |
| 610 /* Replace marker with an empty annotation string. */ | 654 /* Replace marker with an empty annotation string. */ |
| 611 be->depth_addr = XRAY_NULL_ANNOTATION; | 655 be->depth_addr = XRAY_NULL_ANNOTATION; |
| 612 XRayCheckGuards(capture); | 656 XRayCheckGuards(capture); |
| 613 } | 657 } |
| 614 g_xray_capture = NULL; | 658 g_xray_capture = NULL; |
| 659 | |
| 660 #ifndef XRAY_DISABLE_BROWSER_INTEGRATION | |
| 661 capture->frame.entry[i].end_time = XRayGenerateTimestampsNow(); | |
| 662 #endif /* XRAY_DISABLE_BROWSER_INTEGRATION */ | |
| 615 } | 663 } |
| 616 | 664 |
| 617 | 665 |
| 618 /* Get the last frame captured. Do not call while capturing. */ | 666 /* Get the last frame captured. Do not call while capturing. */ |
| 619 /* (ie call outside of XRayStartFrame() / XRayStopFrame() pair) */ | 667 /* (ie call outside of XRayStartFrame() / XRayStopFrame() pair) */ |
| 620 int XRayGetLastFrame(struct XRayTraceCapture* capture) { | 668 int XRayGetLastFrame(struct XRayTraceCapture* capture) { |
| 621 assert(capture); | 669 assert(capture); |
| 622 assert(capture->initialized); | 670 assert(capture->initialized); |
| 623 assert(!capture->recording); | 671 assert(!capture->recording); |
| 624 assert(0 == capture->disabled); | 672 assert(0 == capture->disabled); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 692 capture->recording = false; | 740 capture->recording = false; |
| 693 XRaySetMaxStackDepth(capture, stack_depth); | 741 XRaySetMaxStackDepth(capture, stack_depth); |
| 694 XRayReset(capture); | 742 XRayReset(capture); |
| 695 | 743 |
| 696 /* Mapfile is optional; we don't need it for captures, only for reports. */ | 744 /* Mapfile is optional; we don't need it for captures, only for reports. */ |
| 697 capture->symbols = | 745 capture->symbols = |
| 698 XRaySymbolTableCreate(XRAY_DEFAULT_SYMBOL_TABLE_SIZE); | 746 XRaySymbolTableCreate(XRAY_DEFAULT_SYMBOL_TABLE_SIZE); |
| 699 if (NULL != mapfilename) | 747 if (NULL != mapfilename) |
| 700 XRaySymbolTableParseMapfile(capture->symbols, mapfilename); | 748 XRaySymbolTableParseMapfile(capture->symbols, mapfilename); |
| 701 | 749 |
| 750 #ifndef XRAY_DISABLE_BROWSER_INTEGRATION | |
| 751 /* Use the address of a thread local variable as a fake thread id. */ | |
| 752 capture->thread_id = (int32_t)(&g_xray_thread_id_placeholder); | |
| 753 #endif | |
| 754 | |
| 702 return capture; | 755 return capture; |
| 703 } | 756 } |
| 704 | 757 |
| 705 | 758 |
| 706 /* Shut down and free memory used by XRay. */ | 759 /* Shut down and free memory used by XRay. */ |
| 707 void XRayShutdown(struct XRayTraceCapture* capture) { | 760 void XRayShutdown(struct XRayTraceCapture* capture) { |
| 708 assert(capture); | 761 assert(capture); |
| 709 assert(capture->initialized); | 762 assert(capture->initialized); |
| 710 assert(!capture->recording); | 763 assert(!capture->recording); |
| 711 XRayCheckGuards(capture); | 764 XRayCheckGuards(capture); |
| 712 if (NULL != capture->symbols) { | 765 if (NULL != capture->symbols) { |
| 713 XRaySymbolTableFree(capture->symbols); | 766 XRaySymbolTableFree(capture->symbols); |
| 714 } | 767 } |
| 715 XRayFree(capture->frame.entry); | 768 XRayFree(capture->frame.entry); |
| 716 XRayFree(capture->buffer); | 769 XRayFree(capture->buffer); |
| 717 capture->initialized = false; | 770 capture->initialized = false; |
| 718 XRayFree(capture); | 771 XRayFree(capture); |
| 719 } | 772 } |
| 720 | 773 |
| 721 #endif /* XRAY */ | 774 #endif /* XRAY */ |
| OLD | NEW |