Debian Codegraph @

lib/progress_logger.c

raw symbols SCIP
1 /*
2 Copyright (C) 2019-2025 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_progress_logger.h"cxx . . $ `<file>/lib/grn_progress_logger.h`/
20
21 #include "grn_float.h"cxx . . $ `<file>/lib/grn_float.h`/
22
23 #include <math.h>
24
25 static const grn_log_levelcxx . . $ grn_log_level# grn_progress_logger_log_level_defaultcxx . . $ grn_progress_logger_log_level_default. =
26 GRN_LOG_DEBUGcxx . . $ GRN_LOG_DEBUG.;
27
28 void
29 grn_progress_logger_initcxx . . $ grn_progress_logger_init(3078940d0ac97cf3).(grn_ctxcxx . . $ grn_ctx# *ctx,
30 grn_progress_loggercxx . . $ grn_progress_logger# *logger,
31 const char *tag,
32 const char *targets_label,
33 uint32_tcxx . . $ uint32_t# n_targets)
34 {
35 logger->tagcxx . . $ grn_progress_logger#tag. = tag; /* Don't own this. */
36 logger->targets_labelcxx . . $ grn_progress_logger#targets_label. = targets_label; /* Don't own this. */
37 logger->n_targetscxx . . $ grn_progress_logger#n_targets. = n_targets;
38 logger->n_processed_targetscxx . . $ grn_progress_logger#n_processed_targets. = 0;
39 logger->previous_n_processed_targetscxx . . $ grn_progress_logger#previous_n_processed_targets. = 0;
40 logger->last_logged_n_processed_targetscxx . . $ grn_progress_logger#last_logged_n_processed_targets. = 0;
41 logger->intervalcxx . . $ grn_progress_logger#interval. = 1000;
42 logger->log_levelcxx . . $ grn_progress_logger#log_level. = grn_progress_logger_log_level_defaultcxx . . $ grn_progress_logger_log_level_default.;
43 double n_targets_digits = ceil(log10(logger->n_targetscxx . . $ grn_progress_logger#n_targets. + 1));
44 logger->n_targets_digitscxx . . $ grn_progress_logger#n_targets_digits. = (uint32_tcxx . . $ uint32_t#)n_targets_digits;
45 grn_timeval_nowcxx . . $ grn_timeval_now(b116eeadcb699bd1).(ctx, &(logger->start_timecxx . . $ grn_progress_logger#start_time.));
46 logger->previous_timecxx . . $ grn_progress_logger#previous_time. = logger->start_timecxx . . $ grn_progress_logger#start_time.;
47 }
48
49 void
50 grn_progress_logger_fincxx . . $ grn_progress_logger_fin(96a8a32589463576).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_progress_loggercxx . . $ grn_progress_logger# *logger)
51 {
52 }
53
54 static double
55 grn_progress_logger_format_timecxx . . $ grn_progress_logger_format_time(31e312bb503e2b1b).(grn_ctxcxx . . $ grn_ctx# *ctx, double seconds, const char **unit)
56 {
57 if (seconds < 60) {
58 *unit = "s";
59 return seconds;
60 } else if (seconds < (60 * 60)) {
61 *unit = "m";
62 return seconds / 60;
63 } else if (seconds < (60 * 60 * 24)) {
64 *unit = "h";
65 return seconds / 60 / 60;
66 } else {
67 *unit = "d";
68 return seconds / 60 / 60 / 24;
69 }
70 }
71
72 static double
73 grn_progress_logger_format_memorycxx . . $ grn_progress_logger_format_memory(82807cb9385b0554).(grn_ctxcxx . . $ grn_ctx# *ctx,
74 uint64_tcxx . . $ uint64_t# usage,
75 const char **unit)
76 {
77 if (usage < 1024) {
78 *unit = "B";
79 return (double)usage;
80 } else if (usage < (1024 * 1024)) {
81 *unit = "KiB";
82 return (double)usage / 1024.0;
83 } else if (usage < (1024 * 1024 * 1024)) {
84 *unit = "MiB";
85 return (double)usage / 1024.0 / 1024.0;
86 } else {
87 *unit = "GiB";
88 return (double)usage / 1024.0 / 1024.0 / 1024.0;
89 }
90 }
91
92 static void
93 grn_progress_logger_adjust_intervalcxx . . $ grn_progress_logger_adjust_interval(96a8a32589463576).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_progress_loggercxx . . $ grn_progress_logger# *logger)
94 {
95 if (logger->n_processed_targetscxx . . $ grn_progress_logger#n_processed_targets. >= 100000) {
96 logger->intervalcxx . . $ grn_progress_logger#interval. = 100000;
97 } else if (logger->n_processed_targetscxx . . $ grn_progress_logger#n_processed_targets. >= 10000) {
98 logger->intervalcxx . . $ grn_progress_logger#interval. = 10000;
99 } else if (logger->n_processed_targetscxx . . $ grn_progress_logger#n_processed_targets. >= 5000) {
100 logger->intervalcxx . . $ grn_progress_logger#interval. = 5000;
101 }
102 }
103
104 void
105 grn_progress_logger_logcxx . . $ grn_progress_logger_log(96a8a32589463576).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_progress_loggercxx . . $ grn_progress_logger# *logger)
106 {
107 if (!grn_logger_passcxx . . $ grn_logger_pass(188766080ec9c0f2).(ctx, logger->log_levelcxx . . $ grn_progress_logger#log_level.)) {
108 return;
109 }
110
111 const uint32_tcxx . . $ uint32_t# n_processed_targets = logger->n_processed_targetscxx . . $ grn_progress_logger#n_processed_targets.;
112 const uint32_tcxx . . $ uint32_t# previous_n_processed_targets =
113 logger->previous_n_processed_targetscxx . . $ grn_progress_logger#previous_n_processed_targets.;
114 const uint32_tcxx . . $ uint32_t# interval = logger->intervalcxx . . $ grn_progress_logger#interval.;
115 const uint32_tcxx . . $ uint32_t# n_targets = logger->n_targetscxx . . $ grn_progress_logger#n_targets.;
116 logger->previous_n_processed_targetscxx . . $ grn_progress_logger#previous_n_processed_targets. = logger->n_processed_targetscxx . . $ grn_progress_logger#n_processed_targets.;
117 if (n_processed_targets != n_targets) {
118 if (n_processed_targets == previous_n_processed_targets + 1) {
119 /* Step by step logging. */
120 if ((n_processed_targets % interval) != 0) {
121 return;
122 }
123 } else {
124 /* Bulk logging. */
125 if (n_processed_targets <
126 logger->last_logged_n_processed_targetscxx . . $ grn_progress_logger#last_logged_n_processed_targets. + interval) {
127 return;
128 }
129 }
130 }
131
132 grn_timevalcxx . . $ grn_timeval# current_time;
133 grn_timeval_nowcxx . . $ grn_timeval_now(b116eeadcb699bd1).(ctx, &current_time);
134 const grn_timevalcxx . . $ grn_timeval# *start_time = &(logger->start_timecxx . . $ grn_progress_logger#start_time.);
135 const grn_timevalcxx . . $ grn_timeval# *previous_time = &(logger->previous_timecxx . . $ grn_progress_logger#previous_time.);
136 const double elapsed_seconds =
137 ((double)(current_time.tv_seccxx . . $ $anonymous_type_1555d7cc1d218aa4_15#tv_sec.) +
138 current_time.tv_nseccxx . . $ $anonymous_type_1555d7cc1d218aa4_15#tv_nsec. / GRN_TIME_NSEC_PER_SEC_Fcxx . . $ `include/groonga/time.h:36:9`!) -
139 ((double)(start_time->tv_seccxx . . $ $anonymous_type_1555d7cc1d218aa4_15#tv_sec.) +
140 start_time->tv_nseccxx . . $ $anonymous_type_1555d7cc1d218aa4_15#tv_nsec. / GRN_TIME_NSEC_PER_SEC_Fcxx . . $ `include/groonga/time.h:36:9`!);
141 const double current_interval_seconds =
142 ((double)(current_time.tv_seccxx . . $ $anonymous_type_1555d7cc1d218aa4_15#tv_sec.) +
143 current_time.tv_nseccxx . . $ $anonymous_type_1555d7cc1d218aa4_15#tv_nsec. / GRN_TIME_NSEC_PER_SEC_Fcxx . . $ `include/groonga/time.h:36:9`!) -
144 ((double)(previous_time->tv_seccxx . . $ $anonymous_type_1555d7cc1d218aa4_15#tv_sec.) +
145 previous_time->tv_nseccxx . . $ $anonymous_type_1555d7cc1d218aa4_15#tv_nsec. / GRN_TIME_NSEC_PER_SEC_Fcxx . . $ `include/groonga/time.h:36:9`!);
146 double throughput;
147 if (grn_float_is_zerocxx . . $ grn_float_is_zero(f8197d536af80c96).(current_interval_seconds)) {
148 throughput = interval;
149 } else {
150 throughput = interval / current_interval_seconds;
151 }
152 const double remained_seconds =
153 elapsed_seconds + ((n_targets - n_processed_targets) / throughput);
154 const char *elapsed_unit = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
155 const double elapsed_time =
156 grn_progress_logger_format_timecxx . . $ grn_progress_logger_format_time(31e312bb503e2b1b).(ctx, elapsed_seconds, &elapsed_unit);
157 const char *remained_unit = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
158 const double remained_time =
159 grn_progress_logger_format_timecxx . . $ grn_progress_logger_format_time(31e312bb503e2b1b).(ctx, remained_seconds, &remained_unit);
160 const char *interval_unit = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
161 const double interval_time =
162 grn_progress_logger_format_timecxx . . $ grn_progress_logger_format_time(31e312bb503e2b1b).(ctx,
163 current_interval_seconds,
164 &interval_unit);
165 const char *memory_unit = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
166 const double memory_usage =
167 grn_progress_logger_format_memorycxx . . $ grn_progress_logger_format_memory(82807cb9385b0554).(ctx,
168 grn_memory_get_usagecxx . . $ grn_memory_get_usage(a974fc13268e0b36).(ctx),
169 &memory_unit);
170
171 GRN_LOGcxx . . $ `include/groonga/groonga.h:2493:9`!(ctx,
172 logger->log_level,
173 "%s %*u/%u %3.0f%% %.2f%s/%.2f%s %.2f%s(%.2f%s/s) %.2f%s",
174 logger->tag,
175 logger->n_targets_digits,
176 n_processed_targets,
177 n_targets,
178 ((double)n_processed_targets / (double)n_targets) * 100,
179 elapsed_time,
180 elapsed_unit,
181 remained_time,
182 remained_unit,
183 interval_time,
184 interval_unit,
185 throughput,
186 logger->targets_label,
187 memory_usage,
188 memory_unit);
189 logger->previous_timecxx . . $ grn_progress_logger#previous_time. = current_time;
190 logger->last_logged_n_processed_targetscxx . . $ grn_progress_logger#last_logged_n_processed_targets. = n_processed_targets;
191 grn_progress_logger_adjust_intervalcxx . . $ grn_progress_logger_adjust_interval(96a8a32589463576).(ctx, logger);
192 }