Debian Codegraph @

lib/slow_log.c

raw symbols SCIP
1 /*
2 Copyright(C) 2022 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_slow_log.h"
21
22 #ifdef _WIN32
23 static int64_t grn_slow_log_performance_frequency = 1;
24 #endif
25
26 static double grn_slow_log_threshold = 0.0;
27
28 void
29 grn_slow_log_init(void)
30 {
31 #ifdef _WIN32
32 {
33 LARGE_INTEGER frequency;
34 if (QueryPerformanceFrequency(&frequency)) {
35 grn_slow_log_performance_frequency = frequency.QuadPart;
36 }
37 }
38 #endif
39 }
40
41 void
42 grn_slow_log_init_from_env(void)
43 {
44 {
45 char grn_slow_log_threshold_env[GRN_ENV_BUFFER_SIZE];
46 grn_getenv("GRN_SLOW_LOG_THRESHOLD",
47 grn_slow_log_threshold_env,
48 GRN_ENV_BUFFER_SIZE);
49 if (grn_slow_log_threshold_env[0]) {
50 grn_slow_log_threshold = atof(grn_slow_log_threshold_env);
51 }
52 }
53 }
54
55 grn_rc
56 grn_slow_log_push(grn_ctx *ctx)
57 {
58 const char *tag = "[slow-log][push]";
59
60 if (grn_slow_log_threshold <= 0.0) {
61 return GRN_SUCCESS;
62 }
63
64 GRN_API_ENTER;
65
66 if (!ctx->impl) {
67 ERR(GRN_INVALID_ARGUMENT, "%s not initialized grn_ctx", tag);
68 GRN_API_RETURN(ctx->rc);
69 }
70
71 #ifdef _WIN32
72 {
73 int64_t start_counter = 0;
74 LARGE_INTEGER counter;
75 if (QueryPerformanceCounter(&counter)) {
76 start_counter = counter.QuadPart;
77 }
78 GRN_INT64_PUT(ctx, &(ctx->impl->slow_log.start_times), start_counter);
79 }
80 #else
81 {
82 grn_timeval now;
83 grn_timeval_now(ctx, &now);
84 int64_t packed_start_time =
85 GRN_TIME_PACK(now.tv_sec,
86 GRN_TIME_NSEC_TO_USEC(now.tv_nsec));
87 GRN_INT64_PUT(ctx, &(ctx->impl->slow_log.start_times), packed_start_time);
88 }
89 #endif
90
91 GRN_API_RETURN(ctx->rc);
92 }
93
94 double
95 grn_slow_log_pop(grn_ctx *ctx)
96 {
97 const char *tag = "[slow-log][pop]";
98
99 if (grn_slow_log_threshold <= 0.0) {
100 return 0.0;
101 }
102
103 GRN_API_ENTER;
104
105 if (!ctx->impl) {
106 ERR(GRN_INVALID_ARGUMENT, "%s not initialized grn_ctx", tag);
107 GRN_API_RETURN(0.0);
108 }
109
110 grn_obj *start_times = &(ctx->impl->slow_log.start_times);
111 if (GRN_TIME_VECTOR_SIZE(start_times) == 0) {
112 ERR(GRN_INVALID_ARGUMENT,
113 "%s unbalanced grn_slow_log_push()/grn_slow_log_pop() calls",
114 tag);
115 GRN_API_RETURN(0.0);
116 }
117
118 double elapsed_time;
119 #ifdef _WIN32
120 {
121 int64_t start_counter;
122 GRN_INT64_POP(start_times, start_counter);
123 int64_t end_counter = 0;
124 LARGE_INTEGER counter;
125 if (QueryPerformanceCounter(&counter)) {
126 end_counter = counter.QuadPart;
127 }
128 elapsed_time =
129 (double)(end_counter - start_counter) /
130 (double)grn_slow_log_performance_frequency;
131 }
132 #else
133 {
134 int64_t packed_start_time;
135 GRN_INT64_POP(start_times, packed_start_time);
136 grn_timeval now;
137 grn_timeval_now(ctx, &now);
138 int64_t packed_end_time = GRN_TIME_PACK(now.tv_sec,
139 GRN_TIME_NSEC_TO_USEC(now.tv_nsec));
140 int64_t packed_elapsed_time = packed_end_time - packed_start_time;
141 int64_t elapsed_time_sec;
142 int32_t elapsed_time_usec;
143 GRN_TIME_UNPACK(packed_elapsed_time, elapsed_time_sec, elapsed_time_usec);
144 elapsed_time =
145 (double)elapsed_time_sec +
146 GRN_TIME_USEC_TO_SEC((double)(elapsed_time_usec));
147 }
148 #endif
149
150 GRN_API_RETURN(elapsed_time);
151 }
152
153 bool
154 grn_slow_log_is_slow(grn_ctx *ctx, double elapsed_time)
155 {
156 return grn_slow_log_threshold > 0.0 && elapsed_time >= grn_slow_log_threshold;
157 }