Debian Codegraph @

lib/trace_log.cpp

raw symbols SCIP
1 /*
2 Copyright (C) 2023 Sutou Kouhei <[email protected]>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "grn_ctx_impl.h"
20 #include "grn_vector.h"
21
22 #include <chrono>
23
24 namespace {
25 uint64_t
26 now_nanoseconds()
27 {
28 return std::chrono::duration_cast<std::chrono::nanoseconds>(
29 std::chrono::steady_clock::now().time_since_epoch())
30 .count();
31 }
32 }; // namespace
33
34 extern "C" {
35 void
36 grn_ctx_trace_log_enable(grn_ctx *ctx)
37 {
38 if (grn_ctx_trace_log_is_enabled(ctx)) {
39 grn_ctx_trace_log_disable(ctx);
40 }
41 ctx->impl->trace_log.start_time = now_nanoseconds();
42 GRN_UINT16_PUT(ctx, &(ctx->impl->trace_log.sequence_stack), 0);
43 }
44
45 void
46 grn_ctx_trace_log_disable(grn_ctx *ctx)
47 {
48 if (!grn_ctx_trace_log_is_enabled(ctx)) {
49 return;
50 }
51 ctx->impl->trace_log.start_time = 0;
52 ctx->impl->trace_log.current_depth = 0;
53 GRN_BULK_REWIND(&(ctx->impl->trace_log.depths));
54 GRN_BULK_REWIND(&(ctx->impl->trace_log.sequence_stack));
55 GRN_BULK_REWIND(&(ctx->impl->trace_log.sequences));
56 GRN_BULK_REWIND(&(ctx->impl->trace_log.names));
57 GRN_BULK_REWIND(&(ctx->impl->trace_log.values));
58 GRN_BULK_REWIND(&(ctx->impl->trace_log.elapsed_times));
59 }
60
61 void
62 grn_ctx_trace_log_dump(grn_ctx *ctx, grn_ctx *dump_ctx, grn_obj *output)
63 {
64 if (!grn_ctx_trace_log_is_enabled(ctx)) {
65 grn_text_benc(dump_ctx, output, 0);
66 return;
67 }
68
69 auto depths = &(ctx->impl->trace_log.depths);
70 auto sequences = &(ctx->impl->trace_log.sequences);
71 auto names = &(ctx->impl->trace_log.names);
72 auto values = &(ctx->impl->trace_log.values);
73 auto elapsed_times = &(ctx->impl->trace_log.elapsed_times);
74 size_t i;
75 size_t n = GRN_UINT16_VECTOR_SIZE(depths);
76 grn_text_benc(dump_ctx, output, n);
77 for (i = 0; i < n; ++i) {
78 grn_text_benc(dump_ctx, output, GRN_UINT16_VALUE_AT(depths, i));
79 }
80 for (i = 0; i < n; ++i) {
81 grn_text_benc(dump_ctx, output, GRN_UINT16_VALUE_AT(sequences, i));
82 }
83 grn_obj footer;
84 GRN_TEXT_INIT(&footer, 0);
85 auto body = grn_vector_pack(dump_ctx,
86 names,
87 0,
88 grn_vector_size(dump_ctx, names),
89 0,
90 output,
91 &footer);
92 GRN_TEXT_PUT(ctx, output, GRN_TEXT_VALUE(body), GRN_TEXT_LEN(body));
93 GRN_TEXT_PUT(ctx, output, GRN_TEXT_VALUE(&footer), GRN_TEXT_LEN(&footer));
94 GRN_BULK_REWIND(&footer);
95 body = grn_vector_pack(dump_ctx,
96 values,
97 0,
98 grn_vector_size(dump_ctx, values),
99 0,
100 output,
101 &footer);
102 GRN_TEXT_PUT(dump_ctx, output, GRN_TEXT_VALUE(body), GRN_TEXT_LEN(body));
103 GRN_TEXT_PUT(dump_ctx,
104 output,
105 GRN_TEXT_VALUE(&footer),
106 GRN_TEXT_LEN(&footer));
107 GRN_OBJ_FIN(ctx, &footer);
108 for (i = 0; i < n; ++i) {
109 uint64_t elapsed_time = GRN_UINT64_VALUE_AT(elapsed_times, i);
110 grn_text_benc(dump_ctx, output, (elapsed_time >> 32) & 0xffffffff);
111 grn_text_benc(dump_ctx, output, elapsed_time & 0xffffffff);
112 }
113 }
114
115 size_t
116 grn_ctx_trace_log_restore(grn_ctx *ctx, const uint8_t *data, size_t data_size)
117 {
118 auto start = data;
119 auto current = start;
120 auto end = current + data_size;
121 uint32_t n;
122 GRN_B_DEC(n, current);
123 if (n == 0) {
124 return current - start;
125 }
126
127 auto depths = &(ctx->impl->trace_log.depths);
128 auto sequences = &(ctx->impl->trace_log.sequences);
129 auto names = &(ctx->impl->trace_log.names);
130 auto values = &(ctx->impl->trace_log.values);
131 auto elapsed_times = &(ctx->impl->trace_log.elapsed_times);
132 uint32_t i;
133 for (i = 0; i < n; ++i) {
134 uint32_t depth;
135 GRN_B_DEC(depth, current);
136 GRN_UINT16_PUT(ctx, depths, depth);
137 }
138 for (i = 0; i < n; ++i) {
139 uint32_t sequence;
140 GRN_B_DEC(sequence, current);
141 GRN_UINT16_PUT(ctx, sequences, sequence);
142 }
143 uint32_t used_size;
144 grn_vector_unpack(ctx, names, current, end - current, 0, &used_size);
145 current += used_size;
146 grn_vector_unpack(ctx, values, current, end - current, 0, &used_size);
147 current += used_size;
148 for (i = 0; i < n; ++i) {
149 uint32_t elapsed_time_upper;
150 GRN_B_DEC(elapsed_time_upper, current);
151 uint32_t elapsed_time_lower;
152 GRN_B_DEC(elapsed_time_lower, current);
153 GRN_UINT64_PUT(ctx,
154 elapsed_times,
155 (static_cast<uint64_t>(elapsed_time_upper) << 32) +
156 elapsed_time_lower);
157 }
158 if (!grn_ctx_trace_log_is_enabled(ctx)) {
159 GRN_BULK_REWIND(depths);
160 GRN_BULK_REWIND(sequences);
161 GRN_BULK_REWIND(names);
162 GRN_BULK_REWIND(values);
163 GRN_BULK_REWIND(elapsed_times);
164 }
165 return current - start;
166 }
167
168 uint16_t
169 grn_ctx_trace_log_get_current_depth(grn_ctx *ctx)
170 {
171 if (!grn_ctx_trace_log_is_enabled(ctx)) {
172 return 0;
173 }
174 return ctx->impl->trace_log.current_depth;
175 }
176
177 static inline void
178 grn_ctx_trace_log_push_common(grn_ctx *ctx)
179 {
180 GRN_UINT16_PUT(ctx, &(ctx->impl->trace_log.sequence_stack), 0);
181 }
182
183 void
184 grn_ctx_trace_log_push(grn_ctx *ctx)
185 {
186 if (!grn_ctx_trace_log_is_enabled(ctx)) {
187 return;
188 }
189 ctx->impl->trace_log.current_depth++;
190 grn_ctx_trace_log_push_common(ctx);
191 }
192
193 static inline void
194 grn_ctx_trace_log_pop_common(grn_ctx *ctx)
195 {
196 uint16_t sequence;
197 GRN_UINT16_POP(&(ctx->impl->trace_log.sequence_stack), sequence);
198 }
199
200 void
201 grn_ctx_trace_log_pop(grn_ctx *ctx)
202 {
203 if (!grn_ctx_trace_log_is_enabled(ctx)) {
204 return;
205 }
206 ctx->impl->trace_log.current_depth--;
207 grn_ctx_trace_log_pop_common(ctx);
208 }
209
210 void
211 grn_ctx_trace_log_set_current_depth(grn_ctx *ctx, uint16_t depth)
212 {
213 if (!grn_ctx_trace_log_is_enabled(ctx)) {
214 return;
215 }
216 if (depth == ctx->impl->trace_log.current_depth) {
217 return;
218 }
219 uint16_t i;
220 if (depth > ctx->impl->trace_log.current_depth) {
221 uint16_t n = depth - ctx->impl->trace_log.current_depth;
222 for (i = 0; i < n; i++) {
223 grn_ctx_trace_log_push_common(ctx);
224 }
225 } else {
226 uint16_t n = ctx->impl->trace_log.current_depth - depth;
227 for (i = 0; i < n; i++) {
228 grn_ctx_trace_log_pop_common(ctx);
229 }
230 }
231 ctx->impl->trace_log.current_depth = depth;
232 }
233
234 static void
235 grn_ctx_trace_log_emit_common(grn_ctx *ctx, const char *name)
236 {
237 uint64_t elapsed_time = now_nanoseconds() - ctx->impl->trace_log.start_time;
238 GRN_UINT16_PUT(ctx,
239 &(ctx->impl->trace_log.depths),
240 ctx->impl->trace_log.current_depth);
241 grn_obj *sequence_stack = &(ctx->impl->trace_log.sequence_stack);
242 uint16_t sequence =
243 GRN_UINT16_VALUE_AT(sequence_stack,
244 GRN_UINT16_VECTOR_SIZE(sequence_stack) - 1)++;
245 GRN_UINT16_PUT(ctx, &(ctx->impl->trace_log.sequences), sequence);
246 grn_vector_add_element_float(ctx,
247 &(ctx->impl->trace_log.names),
248 name,
249 strlen(name),
250 0.0,
251 GRN_DB_TEXT);
252 GRN_UINT64_PUT(ctx, &(ctx->impl->trace_log.elapsed_times), elapsed_time);
253 }
254
255 void
256 grn_ctx_trace_log_emit_string(grn_ctx *ctx,
257 const char *name,
258 const char *value,
259 size_t value_length)
260 {
261 if (!grn_ctx_trace_log_is_enabled(ctx)) {
262 return;
263 }
264 grn_ctx_trace_log_emit_common(ctx, name);
265 grn_vector_add_element_float(ctx,
266 &(ctx->impl->trace_log.values),
267 value,
268 value_length,
269 0.0,
270 GRN_DB_TEXT);
271 }
272
273 void
274 grn_ctx_trace_log_emit_cstring(grn_ctx *ctx,
275 const char *name,
276 const char *value)
277 {
278 if (!grn_ctx_trace_log_is_enabled(ctx)) {
279 return;
280 }
281 grn_ctx_trace_log_emit_string(ctx, name, value, strlen(value));
282 }
283
284 void
285 grn_ctx_trace_log_emit_uint32(grn_ctx *ctx, const char *name, uint32_t n)
286 {
287 if (!grn_ctx_trace_log_is_enabled(ctx)) {
288 return;
289 }
290 grn_ctx_trace_log_emit_common(ctx, name);
291 grn_vector_add_element_float(ctx,
292 &(ctx->impl->trace_log.values),
293 (const char *)(&n),
294 sizeof(n),
295 0.0,
296 GRN_DB_UINT32);
297 }
298
299 void
300 grn_ctx_trace_log_emit_record_key(grn_ctx *ctx,
301 const char *name,
302 grn_obj *table,
303 grn_id id)
304 {
305 if (!grn_ctx_trace_log_is_enabled(ctx)) {
306 return;
307 }
308 char key[GRN_TABLE_MAX_KEY_SIZE];
309 int key_size;
310 key_size = grn_table_get_key(ctx, table, id, key, GRN_TABLE_MAX_KEY_SIZE);
311 if (key_size == 0) {
312 return;
313 }
314 /* TODO: Support non ShortText key */
315 grn_ctx_trace_log_emit_string(ctx, name, key, key_size);
316 }
317
318 void
319 grn_ctx_trace_log_emit_object(grn_ctx *ctx, const char *name, grn_obj *object)
320 {
321 if (!grn_ctx_trace_log_is_enabled(ctx)) {
322 return;
323 }
324 grn_obj inspected;
325 GRN_TEXT_INIT(&inspected, 0);
326 grn_inspect(ctx, &inspected, object);
327 grn_ctx_trace_log_emit_string(ctx,
328 name,
329 GRN_TEXT_VALUE(&inspected),
330 GRN_TEXT_LEN(&inspected));
331 GRN_OBJ_FIN(ctx, &inspected);
332 }
333 }