Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(135)

Side by Side Diff: native_client_sdk/src/libraries/xray/xray.c

Issue 19409003: Update Xray for PNaCl (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Change to perframe calibration Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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;
46 45
47 46
48 struct XRayTraceStackEntry { 47 struct XRayTraceStackEntry {
49 uint32_t depth_addr; 48 uint32_t depth_addr;
50 uint64_t tsc; 49 uint64_t tsc;
51 uint32_t dest; 50 uint32_t dest;
52 uint32_t annotation_index; 51 uint32_t annotation_index;
53 }; 52 };
54 53
55 54
56 struct XRayTraceFrameEntry { 55 struct XRayTraceFrameEntry {
57 /* Indices into global tracebuffer */ 56 /* Indices into global tracebuffer */
58 int start; 57 int start;
59 int end; 58 int end;
60 uint64_t start_tsc; 59 uint64_t start_tsc;
61 uint64_t end_tsc; 60 uint64_t end_tsc;
62 uint64_t total_ticks; 61 uint64_t total_ticks;
63 int annotation_count; 62 int annotation_count;
64 bool valid; 63 bool valid;
64
65 #ifndef XRAY_DISABLE_BROWSER_INTEGRATION
66 struct XRayTimestampPair start_time;
67 struct XRayTimestampPair end_time;
68 #endif
65 }; 69 };
66 70
67 71
68 struct XRayTraceFrame { 72 struct XRayTraceFrame {
69 struct XRayTraceFrameEntry* entry; 73 struct XRayTraceFrameEntry* entry;
70 int head; 74 int head;
71 int tail; 75 int tail;
72 int count; 76 int count;
73 }; 77 };
74 78
(...skipping 18 matching lines...) Expand all
93 uint32_t guard3; 97 uint32_t guard3;
94 struct XRayTraceBufferEntry* buffer; 98 struct XRayTraceBufferEntry* buffer;
95 struct XRayTraceFrame frame; 99 struct XRayTraceFrame frame;
96 } XRAY_ALIGN64; 100 } XRAY_ALIGN64;
97 101
98 102
99 #ifdef __cplusplus 103 #ifdef __cplusplus
100 extern "C" { 104 extern "C" {
101 #endif 105 #endif
102 106
103 XRAY_NO_INSTRUMENT void __cyg_profile_func_enter(void* this_fn, 107 #if defined(__pnacl__)
104 void* call_site); 108 XRAY_NO_INSTRUMENT void __pnacl_profile_func_enter(const char* fname);
105 XRAY_NO_INSTRUMENT void __cyg_profile_func_exit(void* this_fn, 109 XRAY_NO_INSTRUMENT void __pnacl_profile_func_exit(const char* fname);
106 void* call_site); 110 #else
111 XRAY_NO_INSTRUMENT void __cyg_profile_func_enter(
112 void* this_fn, void* call_site);
113 XRAY_NO_INSTRUMENT void __cyg_profile_func_exit(void* this_fn, void* call_site);
nfullagar1 2013/07/18 00:47:22 plz format/indent above as it was before
114 #endif
115
107 XRAY_NO_INSTRUMENT void __xray_profile_append_annotation( 116 XRAY_NO_INSTRUMENT void __xray_profile_append_annotation(
108 struct XRayTraceCapture* capture, 117 struct XRayTraceCapture* capture,
109 struct XRayTraceStackEntry* se, 118 struct XRayTraceStackEntry* se,
110 struct XRayTraceBufferEntry* be); 119 struct XRayTraceBufferEntry* be);
111 120
112 #ifdef __cplusplus 121 #ifdef __cplusplus
113 } 122 }
114 #endif 123 #endif
115 124
116
117 /* Asserts that the guard values haven't changed. */ 125 /* Asserts that the guard values haven't changed. */
118 void XRayCheckGuards(struct XRayTraceCapture* capture) { 126 void XRayCheckGuards(struct XRayTraceCapture* capture) {
119 assert(capture->guard0 == XRAY_GUARD_VALUE_0x12345678); 127 assert(capture->guard0 == XRAY_GUARD_VALUE_0x12345678);
120 assert(capture->guard1 == XRAY_GUARD_VALUE_0x12345678); 128 assert(capture->guard1 == XRAY_GUARD_VALUE_0x12345678);
121 assert(capture->guard2 == XRAY_GUARD_VALUE_0x87654321); 129 assert(capture->guard2 == XRAY_GUARD_VALUE_0x87654321);
122 assert(capture->guard3 == XRAY_GUARD_VALUE_0x12345678); 130 assert(capture->guard3 == XRAY_GUARD_VALUE_0x12345678);
123 } 131 }
124 132
125 /* Decrements the trace index, wrapping around if needed. */ 133 /* Decrements the trace index, wrapping around if needed. */
126 XRAY_FORCE_INLINE int XRayTraceDecrementIndexInline( 134 XRAY_FORCE_INLINE int XRayTraceDecrementIndexInline(
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
296 } 304 }
297 305
298 306
299 /* Generic memory free for XRay */ 307 /* Generic memory free for XRay */
300 void XRayFree(void* data) { 308 void XRayFree(void* data) {
301 assert(NULL != data); 309 assert(NULL != data);
302 free(data); 310 free(data);
303 } 311 }
304 312
305 313
306 314 #if defined(__pnacl__)
307 /* Main profile capture function that is called at the start */ 315 /* Main profile capture function that is called at the start */
308 /* of every instrumented function. This function is implicitly */ 316 /* of every instrumented function. This function is implicitly */
309 /* called when code is compilied with the -finstrument-functions option */ 317 /* called when code is compilied with the -finstrument-functions option */
318 void __pnacl_profile_func_enter(const char* fname) {
319 struct XRayTraceCapture* capture = g_xray_capture;
320 if (capture && capture->recording) {
321 uint32_t depth = capture->stack_depth;
322 if (depth < capture->max_stack_depth) {
323 struct XRayTraceStackEntry* se = &capture->stack[depth];
324 uint32_t addr = (uint32_t)fname;
325 se->depth_addr = XRAY_PACK_DEPTH_ADDR(depth, addr);
326 se->dest = capture->buffer_index;
327 se->annotation_index = 0;
328 GTSC(se->tsc);
329 capture->buffer_index =
330 XRayTraceIncrementIndexInline(capture, capture->buffer_index);
331 }
332 ++capture->stack_depth;
333 }
334 }
335
336
337 /* Main profile capture function that is called at the exit of */
338 /* every instrumented function. This function is implicity called */
339 /* when the code is compiled with the -finstrument-functions option */
340 void __pnacl_profile_func_exit(const char* fname) {
341 struct XRayTraceCapture* capture = g_xray_capture;
342 if (capture && capture->recording) {
343 --capture->stack_depth;
344 if (capture->stack_depth < capture->max_stack_depth) {
345 uint32_t depth = capture->stack_depth;
346 struct XRayTraceStackEntry* se = &capture->stack[depth];
347 uint32_t buffer_index = se->dest;
348 uint64_t tsc;
349 struct XRayTraceBufferEntry* be = &capture->buffer[buffer_index];
350 GTSC(tsc);
351 be->depth_addr = se->depth_addr;
352 be->start_tick = se->tsc;
353 be->end_tick = tsc;
354 be->annotation_index = 0;
355 if (0 != se->annotation_index)
356 __xray_profile_append_annotation(capture, se, be);
357 }
358 }
359 }
360 #else
361 /* Main profile capture function that is called at the start */
362 /* of every instrumented function. This function is implicitly */
363 /* called when code is compilied with the -finstrument-functions option */
310 void __cyg_profile_func_enter(void* this_fn, void* call_site) { 364 void __cyg_profile_func_enter(void* this_fn, void* call_site) {
311 struct XRayTraceCapture* capture = g_xray_capture; 365 struct XRayTraceCapture* capture = g_xray_capture;
312 if (capture && capture->recording) { 366 if (capture && capture->recording) {
313 uint32_t depth = capture->stack_depth; 367 uint32_t depth = capture->stack_depth;
314 if (depth < capture->max_stack_depth) { 368 if (depth < capture->max_stack_depth) {
315 struct XRayTraceStackEntry* se = &capture->stack[depth]; 369 struct XRayTraceStackEntry* se = &capture->stack[depth];
316 uint32_t addr = (uint32_t)this_fn; 370 uint32_t addr = (uint32_t)this_fn;
317 se->depth_addr = XRAY_PACK_DEPTH_ADDR(depth, addr); 371 se->depth_addr = XRAY_PACK_DEPTH_ADDR(depth, addr);
318 se->dest = capture->buffer_index; 372 se->dest = capture->buffer_index;
319 se->annotation_index = 0; 373 se->annotation_index = 0;
(...skipping 22 matching lines...) Expand all
342 GTSC(tsc); 396 GTSC(tsc);
343 be->depth_addr = se->depth_addr; 397 be->depth_addr = se->depth_addr;
344 be->start_tick = se->tsc; 398 be->start_tick = se->tsc;
345 be->end_tick = tsc; 399 be->end_tick = tsc;
346 be->annotation_index = 0; 400 be->annotation_index = 0;
347 if (0 != se->annotation_index) 401 if (0 != se->annotation_index)
348 __xray_profile_append_annotation(capture, se, be); 402 __xray_profile_append_annotation(capture, se, be);
349 } 403 }
350 } 404 }
351 } 405 }
406 #endif /* __pnacl__ */
352 407
408 #ifndef XRAY_DISABLE_BROWSER_INTEGRATION
409 void XRayGetTSC(uint64_t* tsc) {GTSC(*tsc);}
410
411 struct XRayTimestampPair* XRayGetTimestamp(
412 struct XRayTraceCapture* capture, int frame, bool end) {
413
414 struct XRayTraceFrameEntry* entry = &(capture->frame.entry[frame]);
415 return end ? &entry->end_time : &entry->start_time;
416 }
417 #endif
353 418
354 /* Special case appending annotation string to trace buffer */ 419 /* Special case appending annotation string to trace buffer */
355 /* this function should only ever be called from __cyg_profile_func_exit() */ 420 /* this function should only ever be called from __cyg_profile_func_exit() */
356 void __xray_profile_append_annotation(struct XRayTraceCapture* capture, 421 void __xray_profile_append_annotation(struct XRayTraceCapture* capture,
357 struct XRayTraceStackEntry* se, 422 struct XRayTraceStackEntry* se,
358 struct XRayTraceBufferEntry* be) { 423 struct XRayTraceBufferEntry* be) {
359 struct XRayTraceStackEntry* parent = se - 1; 424 struct XRayTraceStackEntry* parent = se - 1;
360 int start = parent->annotation_index; 425 int start = parent->annotation_index;
361 be->annotation_index = capture->buffer_index; 426 be->annotation_index = capture->buffer_index;
362 char* str = &capture->annotation[start]; 427 char* str = &capture->annotation[start];
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
566 /* The trace stack[0] is reserved */ 631 /* The trace stack[0] is reserved */
567 memset(&capture->stack[0], 0, sizeof(capture->stack[0])); 632 memset(&capture->stack[0], 0, sizeof(capture->stack[0]));
568 /* Annotation index 0 is reserved to indicate no annotation */ 633 /* Annotation index 0 is reserved to indicate no annotation */
569 capture->stack[0].annotation_index = 1; 634 capture->stack[0].annotation_index = 1;
570 capture->annotation[0] = 0; 635 capture->annotation[0] = 0;
571 capture->annotation[1] = 0; 636 capture->annotation[1] = 0;
572 capture->annotation_count = 0; 637 capture->annotation_count = 0;
573 capture->recording = true; 638 capture->recording = true;
574 GTSC(capture->frame.entry[i].start_tsc); 639 GTSC(capture->frame.entry[i].start_tsc);
575 g_xray_capture = capture; 640 g_xray_capture = capture;
641
642 #ifndef XRAY_DISABLE_BROWSER_INTEGRATION
643 capture->frame.entry[i].start_time = XRayGenerateTimestampsNow();
644 #endif /* XRAY_DISABLE_BROWSER_INTEGRATION */
645
576 } 646 }
577 647
578 648
579 /* Ends a frame and disables capturing. Advances to the next frame. */ 649 /* Ends a frame and disables capturing. Advances to the next frame. */
580 /* Must be paired with XRayStartFrame(), and called from the same thread. */ 650 /* Must be paired with XRayStartFrame(), and called from the same thread. */
581 void XRayEndFrame(struct XRayTraceCapture* capture) { 651 void XRayEndFrame(struct XRayTraceCapture* capture) {
582 int i; 652 int i;
583 assert(capture); 653 assert(capture);
584 assert(capture->initialized); 654 assert(capture->initialized);
585 assert(capture->recording); 655 assert(capture->recording);
(...skipping 19 matching lines...) Expand all
605 if (be->depth_addr != XRAY_FRAME_MARKER) { 675 if (be->depth_addr != XRAY_FRAME_MARKER) {
606 fprintf(stderr, 676 fprintf(stderr,
607 "XRay: XRayStopFrame() detects insufficient trace buffer size!\n"); 677 "XRay: XRayStopFrame() detects insufficient trace buffer size!\n");
608 XRayReset(capture); 678 XRayReset(capture);
609 } else { 679 } else {
610 /* Replace marker with an empty annotation string. */ 680 /* Replace marker with an empty annotation string. */
611 be->depth_addr = XRAY_NULL_ANNOTATION; 681 be->depth_addr = XRAY_NULL_ANNOTATION;
612 XRayCheckGuards(capture); 682 XRayCheckGuards(capture);
613 } 683 }
614 g_xray_capture = NULL; 684 g_xray_capture = NULL;
685
686 #ifndef XRAY_DISABLE_BROWSER_INTEGRATION
687 capture->frame.entry[i].end_time = XRayGenerateTimestampsNow();
688 #endif /* XRAY_DISABLE_BROWSER_INTEGRATION */
615 } 689 }
616 690
617 691
618 /* Get the last frame captured. Do not call while capturing. */ 692 /* Get the last frame captured. Do not call while capturing. */
619 /* (ie call outside of XRayStartFrame() / XRayStopFrame() pair) */ 693 /* (ie call outside of XRayStartFrame() / XRayStopFrame() pair) */
620 int XRayGetLastFrame(struct XRayTraceCapture* capture) { 694 int XRayGetLastFrame(struct XRayTraceCapture* capture) {
621 assert(capture); 695 assert(capture);
622 assert(capture->initialized); 696 assert(capture->initialized);
623 assert(!capture->recording); 697 assert(!capture->recording);
624 assert(0 == capture->disabled); 698 assert(0 == capture->disabled);
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
712 if (NULL != capture->symbols) { 786 if (NULL != capture->symbols) {
713 XRaySymbolTableFree(capture->symbols); 787 XRaySymbolTableFree(capture->symbols);
714 } 788 }
715 XRayFree(capture->frame.entry); 789 XRayFree(capture->frame.entry);
716 XRayFree(capture->buffer); 790 XRayFree(capture->buffer);
717 capture->initialized = false; 791 capture->initialized = false;
718 XRayFree(capture); 792 XRayFree(capture);
719 } 793 }
720 794
721 #endif /* XRAY */ 795 #endif /* XRAY */
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698