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"cxx . . $ `<file>/lib/grn_ctx_impl.h`/
20 #include "grn_vector.h"cxx . . $ `<file>/lib/grn_vector.h`/
21
22 #include <chrono>
23
24 namespacecxx . . $ `$anonymous_namespace_lib/trace_log.cpp`/ {
25 uint64_tcxx . . $ uint64_t#
26 now_nanosecondscxx . . $ `$anonymous_namespace_lib/trace_log.cpp`/now_nanoseconds(f708dce9a8856c70).()
27 {
28 return stdcxx . . $ std/::chronocxx . . $ std/chrono/::duration_castcxx . . $ std/chrono/duration_cast(b7f67bf9c2f19806).<stdcxx . . $ std/::chronocxx . . $ std/chrono/::nanosecondscxx . . $ std/chrono/nanoseconds#>(
29 stdcxx . . $ std/::chronocxx . . $ std/chrono/::steady_clockcxx . . $ std/chrono/_V2/steady_clock#::nowcxx . . $ std/chrono/_V2/steady_clock#now(edffc6b45db65c20).().time_since_epochcxx . . $ std/chrono/time_point#time_since_epoch(7b7646ca7c721626).())
30 .countcxx . . $ std/chrono/duration#count(8f5363d873dd4781).();
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->implcxx . . $ _grn_ctx#impl.->trace_logcxx . . $ _grn_ctx_impl#trace_log..start_timecxx . . $ _grn_ctx_impl#$anonymous_type_10#start_time. = now_nanosecondscxx . . $ `$anonymous_namespace_lib/trace_log.cpp`/now_nanoseconds(f708dce9a8856c70).();
42 GRN_UINT16_PUTcxx . . $ `include/groonga/groonga.h:3172:9`!(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->implcxx . . $ _grn_ctx#impl.->trace_logcxx . . $ _grn_ctx_impl#trace_log..start_timecxx . . $ _grn_ctx_impl#$anonymous_type_10#start_time. = 0;
52 ctx->implcxx . . $ _grn_ctx#impl.->trace_logcxx . . $ _grn_ctx_impl#trace_log..current_depthcxx . . $ _grn_ctx_impl#$anonymous_type_10#current_depth. = 0;
53 GRN_BULK_REWINDcxx . . $ `include/groonga/groonga.h:2599:9`!(&(ctx->impl->trace_log.depths));
54 GRN_BULK_REWINDcxx . . $ `include/groonga/groonga.h:2599:9`!(&(ctx->impl->trace_log.sequence_stack));
55 GRN_BULK_REWINDcxx . . $ `include/groonga/groonga.h:2599:9`!(&(ctx->impl->trace_log.sequences));
56 GRN_BULK_REWINDcxx . . $ `include/groonga/groonga.h:2599:9`!(&(ctx->impl->trace_log.names));
57 GRN_BULK_REWINDcxx . . $ `include/groonga/groonga.h:2599:9`!(&(ctx->impl->trace_log.values));
58 GRN_BULK_REWINDcxx . . $ `include/groonga/groonga.h:2599:9`!(&(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->implcxx . . $ _grn_ctx#impl.->trace_logcxx . . $ _grn_ctx_impl#trace_log..depthscxx . . $ _grn_ctx_impl#$anonymous_type_10#depths.);
70 auto sequences = &(ctx->implcxx . . $ _grn_ctx#impl.->trace_logcxx . . $ _grn_ctx_impl#trace_log..sequencescxx . . $ _grn_ctx_impl#$anonymous_type_10#sequences.);
71 auto names = &(ctx->implcxx . . $ _grn_ctx#impl.->trace_logcxx . . $ _grn_ctx_impl#trace_log..namescxx . . $ _grn_ctx_impl#$anonymous_type_10#names.);
72 auto values = &(ctx->implcxx . . $ _grn_ctx#impl.->trace_logcxx . . $ _grn_ctx_impl#trace_log..valuescxx . . $ _grn_ctx_impl#$anonymous_type_10#values.);
73 auto elapsed_times = &(ctx->implcxx . . $ _grn_ctx#impl.->trace_logcxx . . $ _grn_ctx_impl#trace_log..elapsed_timescxx . . $ _grn_ctx_impl#$anonymous_type_10#elapsed_times.);
74 size_t i;
75 size_t n = GRN_UINT16_VECTOR_SIZEcxx . . $ `include/groonga/groonga.h:3255:9`!(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_ATcxx . . $ `include/groonga/groonga.h:3132:9`!(depths, i));
79 }
80 for (i = 0; i < n; ++i) {
81 grn_text_benc(dump_ctx, output, GRN_UINT16_VALUE_ATcxx . . $ `include/groonga/groonga.h:3132:9`!(sequences, i));
82 }
83 grn_obj footer;
84 GRN_TEXT_INITcxx . . $ `include/groonga/groonga.h:2750:9`!(&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_PUTcxx . . $ `include/groonga/groonga.h:2773:9`!(ctx, output, GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(body), GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(body));
93 GRN_TEXT_PUTcxx . . $ `include/groonga/groonga.h:2773:9`!(ctx, output, GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(&footer), GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(&footer));
94 GRN_BULK_REWINDcxx . . $ `include/groonga/groonga.h:2599:9`!(&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_PUTcxx . . $ `include/groonga/groonga.h:2773:9`!(dump_ctx, output, GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(body), GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(body));
103 GRN_TEXT_PUTcxx . . $ `include/groonga/groonga.h:2773:9`!(dump_ctx,
104 output,
105 GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(&footer),
106 GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(&footer));
107 GRN_OBJ_FINcxx . . $ `include/groonga/groonga.h:958:9`!(ctx, &footer);
108 for (i = 0; i < n; ++i) {
109 uint64_tcxx . . $ uint64_t# elapsed_time = GRN_UINT64_VALUE_ATcxx . . $ `include/groonga/groonga.h:3140:9`!(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_tcxx . . $ uint8_t# *data, size_t data_size)
117 {
118 auto start = data;
119 auto current = start;
120 auto end = current + data_size;
121 uint32_tcxx . . $ uint32_t# n;
122 GRN_B_DECcxx . . $ `lib/grn_io.h:598:9`!(n, current);
123 if (n == 0) {
124 return current - start;
125 }
126
127 auto depths = &(ctx->implcxx . . $ _grn_ctx#impl.->trace_logcxx . . $ _grn_ctx_impl#trace_log..depthscxx . . $ _grn_ctx_impl#$anonymous_type_10#depths.);
128 auto sequences = &(ctx->implcxx . . $ _grn_ctx#impl.->trace_logcxx . . $ _grn_ctx_impl#trace_log..sequencescxx . . $ _grn_ctx_impl#$anonymous_type_10#sequences.);
129 auto names = &(ctx->implcxx . . $ _grn_ctx#impl.->trace_logcxx . . $ _grn_ctx_impl#trace_log..namescxx . . $ _grn_ctx_impl#$anonymous_type_10#names.);
130 auto values = &(ctx->implcxx . . $ _grn_ctx#impl.->trace_logcxx . . $ _grn_ctx_impl#trace_log..valuescxx . . $ _grn_ctx_impl#$anonymous_type_10#values.);
131 auto elapsed_times = &(ctx->implcxx . . $ _grn_ctx#impl.->trace_logcxx . . $ _grn_ctx_impl#trace_log..elapsed_timescxx . . $ _grn_ctx_impl#$anonymous_type_10#elapsed_times.);
132 uint32_tcxx . . $ uint32_t# i;
133 for (i = 0; i < n; ++i) {
134 uint32_tcxx . . $ uint32_t# depth;
135 GRN_B_DECcxx . . $ `lib/grn_io.h:598:9`!(depth, current);
136 GRN_UINT16_PUTcxx . . $ `include/groonga/groonga.h:3172:9`!(ctx, depths, depth);
137 }
138 for (i = 0; i < n; ++i) {
139 uint32_tcxx . . $ uint32_t# sequence;
140 GRN_B_DECcxx . . $ `lib/grn_io.h:598:9`!(sequence, current);
141 GRN_UINT16_PUTcxx . . $ `include/groonga/groonga.h:3172:9`!(ctx, sequences, sequence);
142 }
143 uint32_tcxx . . $ 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_tcxx . . $ uint32_t# elapsed_time_upper;
150 GRN_B_DECcxx . . $ `lib/grn_io.h:598:9`!(elapsed_time_upper, current);
151 uint32_tcxx . . $ uint32_t# elapsed_time_lower;
152 GRN_B_DECcxx . . $ `lib/grn_io.h:598:9`!(elapsed_time_lower, current);
153 GRN_UINT64_PUTcxx . . $ `include/groonga/groonga.h:3192:9`!(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_REWINDcxx . . $ `include/groonga/groonga.h:2599:9`!(depths);
160 GRN_BULK_REWINDcxx . . $ `include/groonga/groonga.h:2599:9`!(sequences);
161 GRN_BULK_REWINDcxx . . $ `include/groonga/groonga.h:2599:9`!(names);
162 GRN_BULK_REWINDcxx . . $ `include/groonga/groonga.h:2599:9`!(values);
163 GRN_BULK_REWINDcxx . . $ `include/groonga/groonga.h:2599:9`!(elapsed_times);
164 }
165 return current - start;
166 }
167
168 uint16_tcxx . . $ 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->implcxx . . $ _grn_ctx#impl.->trace_logcxx . . $ _grn_ctx_impl#trace_log..current_depthcxx . . $ _grn_ctx_impl#$anonymous_type_10#current_depth.;
175 }
176
177 static inline void
178 grn_ctx_trace_log_push_common(grn_ctx *ctx)
179 {
180 GRN_UINT16_PUTcxx . . $ `include/groonga/groonga.h:3172:9`!(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->implcxx . . $ _grn_ctx#impl.->trace_logcxx . . $ _grn_ctx_impl#trace_log..current_depthcxx . . $ _grn_ctx_impl#$anonymous_type_10#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_tcxx . . $ uint16_t# sequence;
197 GRN_UINT16_POPcxx . . $ `include/groonga/groonga.h:3238:9`!(&(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->implcxx . . $ _grn_ctx#impl.->trace_logcxx . . $ _grn_ctx_impl#trace_log..current_depthcxx . . $ _grn_ctx_impl#$anonymous_type_10#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_tcxx . . $ uint16_t# depth)
212 {
213 if (!grn_ctx_trace_log_is_enabled(ctx)) {
214 return;
215 }
216 if (depth == ctx->implcxx . . $ _grn_ctx#impl.->trace_logcxx . . $ _grn_ctx_impl#trace_log..current_depthcxx . . $ _grn_ctx_impl#$anonymous_type_10#current_depth.) {
217 return;
218 }
219 uint16_tcxx . . $ uint16_t# i;
220 if (depth > ctx->implcxx . . $ _grn_ctx#impl.->trace_logcxx . . $ _grn_ctx_impl#trace_log..current_depthcxx . . $ _grn_ctx_impl#$anonymous_type_10#current_depth.) {
221 uint16_tcxx . . $ uint16_t# n = depth - ctx->implcxx . . $ _grn_ctx#impl.->trace_logcxx . . $ _grn_ctx_impl#trace_log..current_depthcxx . . $ _grn_ctx_impl#$anonymous_type_10#current_depth.;
222 for (i = 0; i < n; i++) {
223 grn_ctx_trace_log_push_common(ctx);
224 }
225 } else {
226 uint16_tcxx . . $ uint16_t# n = ctx->implcxx . . $ _grn_ctx#impl.->trace_logcxx . . $ _grn_ctx_impl#trace_log..current_depthcxx . . $ _grn_ctx_impl#$anonymous_type_10#current_depth. - depth;
227 for (i = 0; i < n; i++) {
228 grn_ctx_trace_log_pop_common(ctx);
229 }
230 }
231 ctx->implcxx . . $ _grn_ctx#impl.->trace_logcxx . . $ _grn_ctx_impl#trace_log..current_depthcxx . . $ _grn_ctx_impl#$anonymous_type_10#current_depth. = depth;
232 }
233
234 static void
235 grn_ctx_trace_log_emit_common(grn_ctx *ctx, const char *name)
236 {
237 uint64_tcxx . . $ uint64_t# elapsed_time = now_nanosecondscxx . . $ `$anonymous_namespace_lib/trace_log.cpp`/now_nanoseconds(f708dce9a8856c70).() - ctx->implcxx . . $ _grn_ctx#impl.->trace_logcxx . . $ _grn_ctx_impl#trace_log..start_timecxx . . $ _grn_ctx_impl#$anonymous_type_10#start_time.;
238 GRN_UINT16_PUTcxx . . $ `include/groonga/groonga.h:3172:9`!(ctx,
239 &(ctx->impl->trace_log.depths),
240 ctx->impl->trace_log.current_depth);
241 grn_obj *sequence_stack = &(ctx->implcxx . . $ _grn_ctx#impl.->trace_logcxx . . $ _grn_ctx_impl#trace_log..sequence_stackcxx . . $ _grn_ctx_impl#$anonymous_type_10#sequence_stack.);
242 uint16_tcxx . . $ uint16_t# sequence =
243 GRN_UINT16_VALUE_ATcxx . . $ `include/groonga/groonga.h:3132:9`!(sequence_stack,
244 GRN_UINT16_VECTOR_SIZEcxx . . $ `include/groonga/groonga.h:3255:9`!(sequence_stack) - 1)++;
245 GRN_UINT16_PUTcxx . . $ `include/groonga/groonga.h:3172:9`!(ctx, &(ctx->impl->trace_log.sequences), sequence);
246 grn_vector_add_element_float(ctx,
247 &(ctx->implcxx . . $ _grn_ctx#impl.->trace_logcxx . . $ _grn_ctx_impl#trace_log..namescxx . . $ _grn_ctx_impl#$anonymous_type_10#names.),
248 name,
249 strlen(name),
250 0.0,
251 GRN_DB_TEXT);
252 GRN_UINT64_PUTcxx . . $ `include/groonga/groonga.h:3192:9`!(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->implcxx . . $ _grn_ctx#impl.->trace_logcxx . . $ _grn_ctx_impl#trace_log..valuescxx . . $ _grn_ctx_impl#$anonymous_type_10#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_tcxx . . $ 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->implcxx . . $ _grn_ctx#impl.->trace_logcxx . . $ _grn_ctx_impl#trace_log..valuescxx . . $ _grn_ctx_impl#$anonymous_type_10#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_SIZEcxx . . $ `include/groonga/table.h:26:9`!];
309 int key_size;
310 key_size = grn_table_get_key(ctx, table, id, key, GRN_TABLE_MAX_KEY_SIZEcxx . . $ `include/groonga/table.h:26:9`!);
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_INITcxx . . $ `include/groonga/groonga.h:2750:9`!(&inspected, 0);
326 grn_inspect(ctx, &inspected, object);
327 grn_ctx_trace_log_emit_string(ctx,
328 name,
329 GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(&inspected),
330 GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(&inspected));
331 GRN_OBJ_FINcxx . . $ `include/groonga/groonga.h:958:9`!(ctx, &inspected);
332 }
333 }