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"
20
21 #include "grn_float.h"
22
23 #include <math.h>
24
25 static const grn_log_level grn_progress_logger_log_level_default =
26 GRN_LOG_DEBUG;
27
28 void
29 grn_progress_logger_init(grn_ctx *ctx,
30 grn_progress_logger *logger,
31 const char *tag,
32 const char *targets_label,
33 uint32_t n_targets)
34 {
35 logger->tag = tag; /* Don't own this. */
36 logger->targets_label = targets_label; /* Don't own this. */
37 logger->n_targets = n_targets;
38 logger->n_processed_targets = 0;
39 logger->previous_n_processed_targets = 0;
40 logger->last_logged_n_processed_targets = 0;
41 logger->interval = 1000;
42 logger->log_level = grn_progress_logger_log_level_default;
43 double n_targets_digits = ceil(log10(logger->n_targets + 1));
44 logger->n_targets_digits = (uint32_t)n_targets_digits;
45 grn_timeval_now(ctx, &(logger->start_time));
46 logger->previous_time = logger->start_time;
47 }
48
49 void
50 grn_progress_logger_fin(grn_ctx *ctx, grn_progress_logger *logger)
51 {
52 }
53
54 static double
55 grn_progress_logger_format_time(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_memory(grn_ctx *ctx,
74 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_interval(grn_ctx *ctx, grn_progress_logger *logger)
94 {
95 if (logger->n_processed_targets >= 100000) {
96 logger->interval = 100000;
97 } else if (logger->n_processed_targets >= 10000) {
98 logger->interval = 10000;
99 } else if (logger->n_processed_targets >= 5000) {
100 logger->interval = 5000;
101 }
102 }
103
104 void
105 grn_progress_logger_log(grn_ctx *ctx, grn_progress_logger *logger)
106 {
107 if (!grn_logger_pass(ctx, logger->log_level)) {
108 return;
109 }
110
111 const uint32_t n_processed_targets = logger->n_processed_targets;
112 const uint32_t previous_n_processed_targets =
113 logger->previous_n_processed_targets;
114 const uint32_t interval = logger->interval;
115 const uint32_t n_targets = logger->n_targets;
116 logger->previous_n_processed_targets = 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_targets + interval) {
127 return;
128 }
129 }
130 }
131
132 grn_timeval current_time;
133 grn_timeval_now(ctx, &current_time);
134 const grn_timeval *start_time = &(logger->start_time);
135 const grn_timeval *previous_time = &(logger->previous_time);
136 const double elapsed_seconds =
137 ((double)(current_time.tv_sec) +
138 current_time.tv_nsec / GRN_TIME_NSEC_PER_SEC_F) -
139 ((double)(start_time->tv_sec) +
140 start_time->tv_nsec / GRN_TIME_NSEC_PER_SEC_F);
141 const double current_interval_seconds =
142 ((double)(current_time.tv_sec) +
143 current_time.tv_nsec / GRN_TIME_NSEC_PER_SEC_F) -
144 ((double)(previous_time->tv_sec) +
145 previous_time->tv_nsec / GRN_TIME_NSEC_PER_SEC_F);
146 double throughput;
147 if (grn_float_is_zero(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 = NULL;
155 const double elapsed_time =
156 grn_progress_logger_format_time(ctx, elapsed_seconds, &elapsed_unit);
157 const char *remained_unit = NULL;
158 const double remained_time =
159 grn_progress_logger_format_time(ctx, remained_seconds, &remained_unit);
160 const char *interval_unit = NULL;
161 const double interval_time =
162 grn_progress_logger_format_time(ctx,
163 current_interval_seconds,
164 &interval_unit);
165 const char *memory_unit = NULL;
166 const double memory_usage =
167 grn_progress_logger_format_memory(ctx,
168 grn_memory_get_usage(ctx),
169 &memory_unit);
170
171 GRN_LOG(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_time = current_time;
190 logger->last_logged_n_processed_targets = n_processed_targets;
191 grn_progress_logger_adjust_interval(ctx, logger);
192 }