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

Side by Side Diff: third_party/grpc/src/core/statistics/census_tracing.c

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 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
(Empty)
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 "src/core/statistics/census_interface.h"
35 #include "src/core/statistics/census_tracing.h"
36
37 #include <stdio.h>
38 #include <string.h>
39
40 #include "src/core/statistics/hash_table.h"
41 #include "src/core/support/string.h"
42 #include <grpc/support/alloc.h>
43 #include <grpc/support/log.h>
44 #include <grpc/support/port_platform.h>
45 #include <grpc/support/sync.h>
46
47 void census_trace_obj_destroy(census_trace_obj *obj) {
48 census_trace_annotation *p = obj->annotations;
49 while (p != NULL) {
50 census_trace_annotation *next = p->next;
51 gpr_free(p);
52 p = next;
53 }
54 gpr_free(obj->method);
55 gpr_free(obj);
56 }
57
58 static void delete_trace_obj(void *obj) {
59 census_trace_obj_destroy((census_trace_obj *)obj);
60 }
61
62 static const census_ht_option ht_opt = {
63 CENSUS_HT_UINT64 /* key type */, 571 /* n_of_buckets */, NULL /* hash */,
64 NULL /* compare_keys */, delete_trace_obj /* delete data */,
65 NULL /* delete key */
66 };
67
68 static gpr_once g_init_mutex_once = GPR_ONCE_INIT;
69 static gpr_mu g_mu; /* Guards following two static variables. */
70 static census_ht *g_trace_store = NULL;
71 static uint64_t g_id = 0;
72
73 static census_ht_key op_id_as_key(census_op_id *id) {
74 return *(census_ht_key *)id;
75 }
76
77 static uint64_t op_id_2_uint64(census_op_id *id) {
78 uint64_t ret;
79 memcpy(&ret, id, sizeof(census_op_id));
80 return ret;
81 }
82
83 static void init_mutex(void) { gpr_mu_init(&g_mu); }
84
85 static void init_mutex_once(void) {
86 gpr_once_init(&g_init_mutex_once, init_mutex);
87 }
88
89 census_op_id census_tracing_start_op(void) {
90 gpr_mu_lock(&g_mu);
91 {
92 census_trace_obj *ret = gpr_malloc(sizeof(census_trace_obj));
93 memset(ret, 0, sizeof(census_trace_obj));
94 g_id++;
95 memcpy(&ret->id, &g_id, sizeof(census_op_id));
96 ret->rpc_stats.cnt = 1;
97 ret->ts = gpr_now(GPR_CLOCK_REALTIME);
98 census_ht_insert(g_trace_store, op_id_as_key(&ret->id), (void *)ret);
99 gpr_log(GPR_DEBUG, "Start tracing for id %lu", g_id);
100 gpr_mu_unlock(&g_mu);
101 return ret->id;
102 }
103 }
104
105 int census_add_method_tag(census_op_id op_id, const char *method) {
106 int ret = 0;
107 census_trace_obj *trace = NULL;
108 gpr_mu_lock(&g_mu);
109 trace = census_ht_find(g_trace_store, op_id_as_key(&op_id));
110 if (trace == NULL) {
111 ret = 1;
112 } else {
113 trace->method = gpr_strdup(method);
114 }
115 gpr_mu_unlock(&g_mu);
116 return ret;
117 }
118
119 void census_tracing_print(census_op_id op_id, const char *anno_txt) {
120 census_trace_obj *trace = NULL;
121 gpr_mu_lock(&g_mu);
122 trace = census_ht_find(g_trace_store, op_id_as_key(&op_id));
123 if (trace != NULL) {
124 census_trace_annotation *anno = gpr_malloc(sizeof(census_trace_annotation));
125 anno->ts = gpr_now(GPR_CLOCK_REALTIME);
126 {
127 char *d = anno->txt;
128 const char *s = anno_txt;
129 int n = 0;
130 for (; n < CENSUS_MAX_ANNOTATION_LENGTH && *s != '\0'; ++n) {
131 *d++ = *s++;
132 }
133 *d = '\0';
134 }
135 anno->next = trace->annotations;
136 trace->annotations = anno;
137 }
138 gpr_mu_unlock(&g_mu);
139 }
140
141 void census_tracing_end_op(census_op_id op_id) {
142 census_trace_obj *trace = NULL;
143 gpr_mu_lock(&g_mu);
144 trace = census_ht_find(g_trace_store, op_id_as_key(&op_id));
145 if (trace != NULL) {
146 trace->rpc_stats.elapsed_time_ms = gpr_timespec_to_micros(
147 gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), trace->ts));
148 gpr_log(GPR_DEBUG, "End tracing for id %lu, method %s, latency %f us",
149 op_id_2_uint64(&op_id), trace->method,
150 trace->rpc_stats.elapsed_time_ms);
151 census_ht_erase(g_trace_store, op_id_as_key(&op_id));
152 }
153 gpr_mu_unlock(&g_mu);
154 }
155
156 void census_tracing_init(void) {
157 init_mutex_once();
158 gpr_mu_lock(&g_mu);
159 if (g_trace_store == NULL) {
160 g_id = 1;
161 g_trace_store = census_ht_create(&ht_opt);
162 } else {
163 gpr_log(GPR_ERROR, "Census trace store already initialized.");
164 }
165 gpr_mu_unlock(&g_mu);
166 }
167
168 void census_tracing_shutdown(void) {
169 gpr_mu_lock(&g_mu);
170 if (g_trace_store != NULL) {
171 census_ht_destroy(g_trace_store);
172 g_trace_store = NULL;
173 } else {
174 gpr_log(GPR_ERROR, "Census trace store is not initialized.");
175 }
176 gpr_mu_unlock(&g_mu);
177 }
178
179 void census_internal_lock_trace_store(void) { gpr_mu_lock(&g_mu); }
180
181 void census_internal_unlock_trace_store(void) { gpr_mu_unlock(&g_mu); }
182
183 census_trace_obj *census_get_trace_obj_locked(census_op_id op_id) {
184 if (g_trace_store == NULL) {
185 gpr_log(GPR_ERROR, "Census trace store is not initialized.");
186 return NULL;
187 }
188 return (census_trace_obj *)census_ht_find(g_trace_store,
189 op_id_as_key(&op_id));
190 }
191
192 const char *census_get_trace_method_name(const census_trace_obj *trace) {
193 return trace->method;
194 }
195
196 static census_trace_annotation *dup_annotation_chain(
197 census_trace_annotation *from) {
198 census_trace_annotation *ret = NULL;
199 census_trace_annotation **to = &ret;
200 for (; from != NULL; from = from->next) {
201 *to = gpr_malloc(sizeof(census_trace_annotation));
202 memcpy(*to, from, sizeof(census_trace_annotation));
203 to = &(*to)->next;
204 }
205 return ret;
206 }
207
208 static census_trace_obj *trace_obj_dup(census_trace_obj *from) {
209 census_trace_obj *to = NULL;
210 GPR_ASSERT(from != NULL);
211 to = gpr_malloc(sizeof(census_trace_obj));
212 to->id = from->id;
213 to->ts = from->ts;
214 to->rpc_stats = from->rpc_stats;
215 to->method = gpr_strdup(from->method);
216 to->annotations = dup_annotation_chain(from->annotations);
217 return to;
218 }
219
220 census_trace_obj **census_get_active_ops(int *num_active_ops) {
221 census_trace_obj **ret = NULL;
222 gpr_mu_lock(&g_mu);
223 if (g_trace_store != NULL) {
224 size_t n = 0;
225 census_ht_kv *all_kvs = census_ht_get_all_elements(g_trace_store, &n);
226 *num_active_ops = (int)n;
227 if (n != 0) {
228 size_t i = 0;
229 ret = gpr_malloc(sizeof(census_trace_obj *) * n);
230 for (i = 0; i < n; i++) {
231 ret[i] = trace_obj_dup((census_trace_obj *)all_kvs[i].v);
232 }
233 }
234 gpr_free(all_kvs);
235 }
236 gpr_mu_unlock(&g_mu);
237 return ret;
238 }
OLDNEW
« no previous file with comments | « third_party/grpc/src/core/statistics/census_tracing.h ('k') | third_party/grpc/src/core/statistics/hash_table.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698