Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 2e19036 | 2016-03-25 14:33:26 -0700 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 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 | |
Hongyu Chen | 1ef06e6 | 2015-02-04 10:36:04 -0800 | [diff] [blame] | 34 | #include "src/core/statistics/census_tracing.h" |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 35 | #include "src/core/statistics/census_interface.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 36 | |
hongyu | 24200d3 | 2015-01-08 15:13:49 -0800 | [diff] [blame] | 37 | #include <stdio.h> |
| 38 | #include <string.h> |
| 39 | |
hongyu | 24200d3 | 2015-01-08 15:13:49 -0800 | [diff] [blame] | 40 | #include <grpc/support/alloc.h> |
| 41 | #include <grpc/support/log.h> |
| 42 | #include <grpc/support/port_platform.h> |
hongyu | 24200d3 | 2015-01-08 15:13:49 -0800 | [diff] [blame] | 43 | #include <grpc/support/sync.h> |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 44 | #include "src/core/statistics/hash_table.h" |
| 45 | #include "src/core/support/string.h" |
hongyu | 24200d3 | 2015-01-08 15:13:49 -0800 | [diff] [blame] | 46 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 47 | void census_trace_obj_destroy(census_trace_obj *obj) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 48 | census_trace_annotation *p = obj->annotations; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 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); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 58 | static void delete_trace_obj(void *obj) { |
| 59 | census_trace_obj_destroy((census_trace_obj *)obj); |
Hongyu Chen | 2bfbfe8 | 2015-02-05 16:12:26 -0800 | [diff] [blame] | 60 | } |
hongyu | 24200d3 | 2015-01-08 15:13:49 -0800 | [diff] [blame] | 61 | |
| 62 | static const census_ht_option ht_opt = { |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 63 | CENSUS_HT_UINT64 /* key type */, |
| 64 | 571 /* n_of_buckets */, |
| 65 | NULL /* hash */, |
| 66 | NULL /* compare_keys */, |
| 67 | delete_trace_obj /* delete data */, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 68 | NULL /* delete key */ |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 69 | }; |
hongyu | 24200d3 | 2015-01-08 15:13:49 -0800 | [diff] [blame] | 70 | |
| 71 | static gpr_once g_init_mutex_once = GPR_ONCE_INIT; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 72 | static gpr_mu g_mu; /* Guards following two static variables. */ |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 73 | static census_ht *g_trace_store = NULL; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 74 | static uint64_t g_id = 0; |
hongyu | 24200d3 | 2015-01-08 15:13:49 -0800 | [diff] [blame] | 75 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 76 | static census_ht_key op_id_as_key(census_op_id *id) { |
| 77 | return *(census_ht_key *)id; |
hongyu | 24200d3 | 2015-01-08 15:13:49 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 80 | static uint64_t op_id_2_uint64(census_op_id *id) { |
| 81 | uint64_t ret; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 82 | memcpy(&ret, id, sizeof(census_op_id)); |
hongyu | 24200d3 | 2015-01-08 15:13:49 -0800 | [diff] [blame] | 83 | return ret; |
| 84 | } |
| 85 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 86 | static void init_mutex(void) { gpr_mu_init(&g_mu); } |
| 87 | |
| 88 | static void init_mutex_once(void) { |
| 89 | gpr_once_init(&g_init_mutex_once, init_mutex); |
Craig Tiller | 3eef2c4 | 2015-01-15 11:37:54 -0800 | [diff] [blame] | 90 | } |
hongyu | 24200d3 | 2015-01-08 15:13:49 -0800 | [diff] [blame] | 91 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 92 | census_op_id census_tracing_start_op(void) { |
| 93 | gpr_mu_lock(&g_mu); |
hongyu | 24200d3 | 2015-01-08 15:13:49 -0800 | [diff] [blame] | 94 | { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 95 | census_trace_obj *ret = gpr_malloc(sizeof(census_trace_obj)); |
| 96 | memset(ret, 0, sizeof(census_trace_obj)); |
hongyu | 24200d3 | 2015-01-08 15:13:49 -0800 | [diff] [blame] | 97 | g_id++; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 98 | memcpy(&ret->id, &g_id, sizeof(census_op_id)); |
hongyu | 24200d3 | 2015-01-08 15:13:49 -0800 | [diff] [blame] | 99 | ret->rpc_stats.cnt = 1; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 100 | ret->ts = gpr_now(GPR_CLOCK_REALTIME); |
| 101 | census_ht_insert(g_trace_store, op_id_as_key(&ret->id), (void *)ret); |
| 102 | gpr_log(GPR_DEBUG, "Start tracing for id %lu", g_id); |
| 103 | gpr_mu_unlock(&g_mu); |
hongyu | 24200d3 | 2015-01-08 15:13:49 -0800 | [diff] [blame] | 104 | return ret->id; |
| 105 | } |
| 106 | } |
| 107 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 108 | int census_add_method_tag(census_op_id op_id, const char *method) { |
hongyu | 24200d3 | 2015-01-08 15:13:49 -0800 | [diff] [blame] | 109 | int ret = 0; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 110 | census_trace_obj *trace = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 111 | gpr_mu_lock(&g_mu); |
| 112 | trace = census_ht_find(g_trace_store, op_id_as_key(&op_id)); |
| 113 | if (trace == NULL) { |
| 114 | ret = 1; |
| 115 | } else { |
| 116 | trace->method = gpr_strdup(method); |
| 117 | } |
| 118 | gpr_mu_unlock(&g_mu); |
hongyu | 24200d3 | 2015-01-08 15:13:49 -0800 | [diff] [blame] | 119 | return ret; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 122 | void census_tracing_print(census_op_id op_id, const char *anno_txt) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 123 | census_trace_obj *trace = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 124 | gpr_mu_lock(&g_mu); |
| 125 | trace = census_ht_find(g_trace_store, op_id_as_key(&op_id)); |
| 126 | if (trace != NULL) { |
| 127 | census_trace_annotation *anno = gpr_malloc(sizeof(census_trace_annotation)); |
| 128 | anno->ts = gpr_now(GPR_CLOCK_REALTIME); |
hongyu | 440d3d8 | 2015-01-09 12:39:36 -0800 | [diff] [blame] | 129 | { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 130 | char *d = anno->txt; |
| 131 | const char *s = anno_txt; |
| 132 | int n = 0; |
| 133 | for (; n < CENSUS_MAX_ANNOTATION_LENGTH && *s != '\0'; ++n) { |
| 134 | *d++ = *s++; |
hongyu | 440d3d8 | 2015-01-09 12:39:36 -0800 | [diff] [blame] | 135 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 136 | *d = '\0'; |
hongyu | 440d3d8 | 2015-01-09 12:39:36 -0800 | [diff] [blame] | 137 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 138 | anno->next = trace->annotations; |
| 139 | trace->annotations = anno; |
| 140 | } |
| 141 | gpr_mu_unlock(&g_mu); |
hongyu | 440d3d8 | 2015-01-09 12:39:36 -0800 | [diff] [blame] | 142 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 143 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 144 | void census_tracing_end_op(census_op_id op_id) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 145 | census_trace_obj *trace = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 146 | gpr_mu_lock(&g_mu); |
| 147 | trace = census_ht_find(g_trace_store, op_id_as_key(&op_id)); |
| 148 | if (trace != NULL) { |
| 149 | trace->rpc_stats.elapsed_time_ms = gpr_timespec_to_micros( |
| 150 | gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), trace->ts)); |
| 151 | gpr_log(GPR_DEBUG, "End tracing for id %lu, method %s, latency %f us", |
| 152 | op_id_2_uint64(&op_id), trace->method, |
| 153 | trace->rpc_stats.elapsed_time_ms); |
| 154 | census_ht_erase(g_trace_store, op_id_as_key(&op_id)); |
| 155 | } |
| 156 | gpr_mu_unlock(&g_mu); |
hongyu | 24200d3 | 2015-01-08 15:13:49 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 159 | void census_tracing_init(void) { |
| 160 | init_mutex_once(); |
| 161 | gpr_mu_lock(&g_mu); |
| 162 | if (g_trace_store == NULL) { |
| 163 | g_id = 1; |
| 164 | g_trace_store = census_ht_create(&ht_opt); |
| 165 | } else { |
| 166 | gpr_log(GPR_ERROR, "Census trace store already initialized."); |
| 167 | } |
| 168 | gpr_mu_unlock(&g_mu); |
hongyu | 24200d3 | 2015-01-08 15:13:49 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 171 | void census_tracing_shutdown(void) { |
| 172 | gpr_mu_lock(&g_mu); |
| 173 | if (g_trace_store != NULL) { |
| 174 | census_ht_destroy(g_trace_store); |
| 175 | g_trace_store = NULL; |
| 176 | } else { |
| 177 | gpr_log(GPR_ERROR, "Census trace store is not initialized."); |
| 178 | } |
| 179 | gpr_mu_unlock(&g_mu); |
hongyu | 24200d3 | 2015-01-08 15:13:49 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 182 | void census_internal_lock_trace_store(void) { gpr_mu_lock(&g_mu); } |
| 183 | |
| 184 | void census_internal_unlock_trace_store(void) { gpr_mu_unlock(&g_mu); } |
| 185 | |
| 186 | census_trace_obj *census_get_trace_obj_locked(census_op_id op_id) { |
| 187 | if (g_trace_store == NULL) { |
| 188 | gpr_log(GPR_ERROR, "Census trace store is not initialized."); |
| 189 | return NULL; |
| 190 | } |
| 191 | return (census_trace_obj *)census_ht_find(g_trace_store, |
| 192 | op_id_as_key(&op_id)); |
hongyu | 24200d3 | 2015-01-08 15:13:49 -0800 | [diff] [blame] | 193 | } |
| 194 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 195 | const char *census_get_trace_method_name(const census_trace_obj *trace) { |
Hongyu Chen | b567454 | 2015-02-05 12:18:05 -0800 | [diff] [blame] | 196 | return trace->method; |
hongyu | 24200d3 | 2015-01-08 15:13:49 -0800 | [diff] [blame] | 197 | } |
Hongyu Chen | 1ef06e6 | 2015-02-04 10:36:04 -0800 | [diff] [blame] | 198 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 199 | static census_trace_annotation *dup_annotation_chain( |
| 200 | census_trace_annotation *from) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 201 | census_trace_annotation *ret = NULL; |
| 202 | census_trace_annotation **to = &ret; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 203 | for (; from != NULL; from = from->next) { |
| 204 | *to = gpr_malloc(sizeof(census_trace_annotation)); |
| 205 | memcpy(*to, from, sizeof(census_trace_annotation)); |
| 206 | to = &(*to)->next; |
| 207 | } |
Hongyu Chen | 1ed726c | 2015-02-05 15:49:09 -0800 | [diff] [blame] | 208 | return ret; |
Hongyu Chen | 1ef06e6 | 2015-02-04 10:36:04 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 211 | static census_trace_obj *trace_obj_dup(census_trace_obj *from) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 212 | census_trace_obj *to = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 213 | GPR_ASSERT(from != NULL); |
| 214 | to = gpr_malloc(sizeof(census_trace_obj)); |
Hongyu Chen | 1ef06e6 | 2015-02-04 10:36:04 -0800 | [diff] [blame] | 215 | to->id = from->id; |
| 216 | to->ts = from->ts; |
| 217 | to->rpc_stats = from->rpc_stats; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 218 | to->method = gpr_strdup(from->method); |
| 219 | to->annotations = dup_annotation_chain(from->annotations); |
Hongyu Chen | 1ef06e6 | 2015-02-04 10:36:04 -0800 | [diff] [blame] | 220 | return to; |
| 221 | } |
| 222 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 223 | census_trace_obj **census_get_active_ops(int *num_active_ops) { |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 224 | census_trace_obj **ret = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 225 | gpr_mu_lock(&g_mu); |
| 226 | if (g_trace_store != NULL) { |
| 227 | size_t n = 0; |
| 228 | census_ht_kv *all_kvs = census_ht_get_all_elements(g_trace_store, &n); |
| 229 | *num_active_ops = (int)n; |
| 230 | if (n != 0) { |
| 231 | size_t i = 0; |
| 232 | ret = gpr_malloc(sizeof(census_trace_obj *) * n); |
| 233 | for (i = 0; i < n; i++) { |
| 234 | ret[i] = trace_obj_dup((census_trace_obj *)all_kvs[i].v); |
| 235 | } |
Hongyu Chen | 1ef06e6 | 2015-02-04 10:36:04 -0800 | [diff] [blame] | 236 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 237 | gpr_free(all_kvs); |
| 238 | } |
| 239 | gpr_mu_unlock(&g_mu); |
Hongyu Chen | 1ef06e6 | 2015-02-04 10:36:04 -0800 | [diff] [blame] | 240 | return ret; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 241 | } |