Line data Source code
1 : /*
2 : *
3 : * Copyright 2015, Google Inc.
4 : * All rights reserved.
5 : *
6 : * Redistribution and use in source and binary forms, with or without
7 : * modification, are permitted provided that the following conditions are
8 : * met:
9 : *
10 : * * Redistributions of source code must retain the above copyright
11 : * notice, this list of conditions and the following disclaimer.
12 : * * Redistributions in binary form must reproduce the above
13 : * copyright notice, this list of conditions and the following disclaimer
14 : * in the documentation and/or other materials provided with the
15 : * distribution.
16 : * * Neither the name of Google Inc. nor the names of its
17 : * contributors may be used to endorse or promote products derived from
18 : * this software without specific prior written permission.
19 : *
20 : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 : * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 : * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 : * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 : * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 : * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 : * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 : * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 : * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 : * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 : * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 : *
32 : */
33 :
34 : #include <grpc/support/port_platform.h>
35 :
36 : #ifdef GRPC_BASIC_PROFILER
37 :
38 : #include "src/core/profiling/timers.h"
39 :
40 : #include <grpc/support/alloc.h>
41 : #include <grpc/support/log.h>
42 : #include <grpc/support/time.h>
43 : #include <grpc/support/sync.h>
44 : #include <grpc/support/thd.h>
45 : #include <stdio.h>
46 :
47 : typedef enum {
48 : BEGIN = '{',
49 : END = '}',
50 : MARK = '.',
51 : IMPORTANT = '!'
52 : } marker_type;
53 :
54 : typedef struct grpc_timer_entry {
55 : gpr_timespec tm;
56 : int tag;
57 : const char *tagstr;
58 : marker_type type;
59 : void *id;
60 : const char *file;
61 : int line;
62 : } grpc_timer_entry;
63 :
64 : #define MAX_COUNT (1024 * 1024 / sizeof(grpc_timer_entry))
65 :
66 : static __thread grpc_timer_entry log[MAX_COUNT];
67 : static __thread int count;
68 :
69 : static void log_report() {
70 : int i;
71 : for (i = 0; i < count; i++) {
72 : grpc_timer_entry *entry = &(log[i]);
73 : printf("GRPC_LAT_PROF %ld.%09d %p %c %d(%s) %p %s %d\n", entry->tm.tv_sec,
74 : entry->tm.tv_nsec, (void *)(gpr_intptr)gpr_thd_currentid(),
75 : entry->type, entry->tag, entry->tagstr, entry->id, entry->file,
76 : entry->line);
77 : }
78 :
79 : /* Now clear out the log */
80 : count = 0;
81 : }
82 :
83 : static void grpc_timers_log_add(int tag, const char *tagstr, marker_type type,
84 : void *id, const char *file, int line) {
85 : grpc_timer_entry *entry;
86 :
87 : /* TODO (vpai) : Improve concurrency */
88 : if (count == MAX_COUNT) {
89 : log_report();
90 : }
91 :
92 : entry = &log[count++];
93 :
94 : entry->tm = gpr_now(GPR_CLOCK_PRECISE);
95 : entry->tag = tag;
96 : entry->tagstr = tagstr;
97 : entry->type = type;
98 : entry->id = id;
99 : entry->file = file;
100 : entry->line = line;
101 : }
102 :
103 : /* Latency profiler API implementation. */
104 : void grpc_timer_add_mark(int tag, const char *tagstr, void *id,
105 : const char *file, int line) {
106 : if (tag < GRPC_PTAG_IGNORE_THRESHOLD) {
107 : grpc_timers_log_add(tag, tagstr, MARK, id, file, line);
108 : }
109 : }
110 :
111 : void grpc_timer_add_important_mark(int tag, const char *tagstr, void *id,
112 : const char *file, int line) {
113 : if (tag < GRPC_PTAG_IGNORE_THRESHOLD) {
114 : grpc_timers_log_add(tag, tagstr, IMPORTANT, id, file, line);
115 : }
116 : }
117 :
118 : void grpc_timer_begin(int tag, const char *tagstr, void *id, const char *file,
119 : int line) {
120 : if (tag < GRPC_PTAG_IGNORE_THRESHOLD) {
121 : grpc_timers_log_add(tag, tagstr, BEGIN, id, file, line);
122 : }
123 : }
124 :
125 : void grpc_timer_end(int tag, const char *tagstr, void *id, const char *file,
126 : int line) {
127 : if (tag < GRPC_PTAG_IGNORE_THRESHOLD) {
128 : grpc_timers_log_add(tag, tagstr, END, id, file, line);
129 : }
130 : }
131 :
132 : /* Basic profiler specific API functions. */
133 : void grpc_timers_global_init(void) {}
134 :
135 : void grpc_timers_global_destroy(void) {}
136 :
137 : #else /* !GRPC_BASIC_PROFILER */
138 2502 : void grpc_timers_global_init(void) {}
139 :
140 2502 : void grpc_timers_global_destroy(void) {}
141 : #endif /* GRPC_BASIC_PROFILER */
|