| 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"cxx . . $ `<file>/lib/grn_ctx_impl.h`/ |
| 20 | #include "grn_slow_log.h"cxx . . $ `<file>/lib/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_thresholdcxx . . $ grn_slow_log_threshold. = 0.0; |
| 27 | |
| 28 | void |
| 29 | grn_slow_log_initcxx . . $ grn_slow_log_init(49f6e7a06ebc5aa8).(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_envcxx . . $ grn_slow_log_init_from_env(49f6e7a06ebc5aa8).(void) |
| 43 | { |
| 44 | { |
| 45 | char grn_slow_log_threshold_env[GRN_ENV_BUFFER_SIZEcxx . . $ `include/groonga/portability.h:42:9`!]; |
| 46 | grn_getenvcxx . . $ `include/groonga/portability.h:58:11`!("GRN_SLOW_LOG_THRESHOLD", |
| 47 | grn_slow_log_threshold_env, |
| 48 | GRN_ENV_BUFFER_SIZEcxx . . $ `include/groonga/portability.h:42:9`!); |
| 49 | if (grn_slow_log_threshold_env[0]) { |
| 50 | grn_slow_log_thresholdcxx . . $ grn_slow_log_threshold. = atofcxx . . $ atof(9a18bdb0e56c1bc2).(grn_slow_log_threshold_env); |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | grn_rccxx . . $ grn_rc# |
| 56 | grn_slow_log_pushcxx . . $ grn_slow_log_push(2b4fa601bf4f1e5b).(grn_ctxcxx . . $ grn_ctx# *ctx) |
| 57 | { |
| 58 | const char *tag = "[slow-log][push]"; |
| 59 | |
| 60 | if (grn_slow_log_thresholdcxx . . $ grn_slow_log_threshold. <= 0.0) { |
| 61 | return GRN_SUCCESScxx . . $ GRN_SUCCESS.; |
| 62 | } |
| 63 | |
| 64 | GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!; |
| 65 | |
| 66 | if (!ctx->implcxx . . $ _grn_ctx#impl.) { |
| 67 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_INVALID_ARGUMENT, "%s not initialized grn_ctx", tag); |
| 68 | GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(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_timevalcxx . . $ grn_timeval# now; |
| 83 | grn_timeval_nowcxx . . $ grn_timeval_now(b116eeadcb699bd1).(ctx, &now); |
| 84 | int64_tcxx . . $ int64_t# packed_start_time = |
| 85 | GRN_TIME_PACKcxx . . $ `include/groonga/time.h:51:9`!(now.tv_sec, |
| 86 | GRN_TIME_NSEC_TO_USECcxx . . $ `include/groonga/time.h:39:9`!(now.tv_nsec)); |
| 87 | GRN_INT64_PUTcxx . . $ `include/groonga/groonga.h:3187:9`!(ctx, &(ctx->impl->slow_log.start_times), packed_start_time); |
| 88 | } |
| 89 | #endif |
| 90 | |
| 91 | GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc); |
| 92 | } |
| 93 | |
| 94 | double |
| 95 | grn_slow_log_popcxx . . $ grn_slow_log_pop(9c97590772bf46e3).(grn_ctxcxx . . $ grn_ctx# *ctx) |
| 96 | { |
| 97 | const char *tag = "[slow-log][pop]"; |
| 98 | |
| 99 | if (grn_slow_log_thresholdcxx . . $ grn_slow_log_threshold. <= 0.0) { |
| 100 | return 0.0; |
| 101 | } |
| 102 | |
| 103 | GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!; |
| 104 | |
| 105 | if (!ctx->implcxx . . $ _grn_ctx#impl.) { |
| 106 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_INVALID_ARGUMENT, "%s not initialized grn_ctx", tag); |
| 107 | GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(0.0); |
| 108 | } |
| 109 | |
| 110 | grn_objcxx . . $ grn_obj# *start_times = &(ctx->implcxx . . $ _grn_ctx#impl.->slow_logcxx . . $ _grn_ctx_impl#slow_log..start_timescxx . . $ _grn_ctx_impl#$anonymous_type_5#start_times.); |
| 111 | if (GRN_TIME_VECTOR_SIZEcxx . . $ `include/groonga/groonga.h:3263:9`!(start_times) == 0) { |
| 112 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_INVALID_ARGUMENT, |
| 113 | "%s unbalanced grn_slow_log_push()/grn_slow_log_pop() calls", |
| 114 | tag); |
| 115 | GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(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_tcxx . . $ int64_t# packed_start_time; |
| 135 | GRN_INT64_POPcxx . . $ `include/groonga/groonga.h:3241:9`!(start_times, packed_start_time); |
| 136 | grn_timevalcxx . . $ grn_timeval# now; |
| 137 | grn_timeval_nowcxx . . $ grn_timeval_now(b116eeadcb699bd1).(ctx, &now); |
| 138 | int64_tcxx . . $ int64_t# packed_end_time = GRN_TIME_PACKcxx . . $ `include/groonga/time.h:51:9`!(now.tv_sec, |
| 139 | GRN_TIME_NSEC_TO_USECcxx . . $ `include/groonga/time.h:39:9`!(now.tv_nsec)); |
| 140 | int64_tcxx . . $ int64_t# packed_elapsed_time = packed_end_time - packed_start_time; |
| 141 | int64_tcxx . . $ int64_t# elapsed_time_sec; |
| 142 | int32_tcxx . . $ int32_t# elapsed_time_usec; |
| 143 | GRN_TIME_UNPACKcxx . . $ `include/groonga/time.h:52:9`!(packed_elapsed_time, elapsed_time_sec, elapsed_time_usec); |
| 144 | elapsed_time = |
| 145 | (double)elapsed_time_sec + |
| 146 | GRN_TIME_USEC_TO_SECcxx . . $ `include/groonga/time.h:47:9`!((double)(elapsed_time_usec)); |
| 147 | } |
| 148 | #endif |
| 149 | |
| 150 | GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(elapsed_time); |
| 151 | } |
| 152 | |
| 153 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! |
| 154 | grn_slow_log_is_slowcxx . . $ grn_slow_log_is_slow(df7bc0a04eefd643).(grn_ctxcxx . . $ grn_ctx# *ctx, double elapsed_time) |
| 155 | { |
| 156 | return grn_slow_log_thresholdcxx . . $ grn_slow_log_threshold. > 0.0 && elapsed_time >= grn_slow_log_thresholdcxx . . $ grn_slow_log_threshold.; |
| 157 | } |