GCC Code Coverage Report


Directory: src/
File: src/support/debug.c
Date: 2025-11-21 10:34:40
Exec Total Coverage
Lines: 136 188 72.3%
Functions: 15 20 75.0%
Branches: 54 98 55.1%

Line Branch Exec Source
1 /*********************************************************************************/
2 /* Copyright 2009-2021 Barcelona Supercomputing Center */
3 /* */
4 /* This file is part of the DLB library. */
5 /* */
6 /* DLB is free software: you can redistribute it and/or modify */
7 /* it under the terms of the GNU Lesser General Public License as published by */
8 /* the Free Software Foundation, either version 3 of the License, or */
9 /* (at your option) any later version. */
10 /* */
11 /* DLB is distributed in the hope that it will be useful, */
12 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14 /* GNU Lesser General Public License for more details. */
15 /* */
16 /* You should have received a copy of the GNU Lesser General Public License */
17 /* along with DLB. If not, see <https://www.gnu.org/licenses/>. */
18 /*********************************************************************************/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include "support/debug.h"
25
26 #include "apis/dlb_errors.h"
27 #include "support/options.h"
28 #include "support/mask_utils.h"
29 #include "LB_comm/comm_lend_light.h"
30 #include "LB_comm/shmem.h"
31 #include "LB_comm/shmem_async.h"
32 #include "LB_comm/shmem_barrier.h"
33 #include "LB_comm/shmem_cpuinfo.h"
34 #include "LB_comm/shmem_procinfo.h"
35 #include "LB_comm/shmem_talp.h"
36 #include "LB_core/spd.h"
37
38 #ifdef MPI_LIB
39 #include "mpi/mpi_core.h"
40 #endif
41
42 #include <sys/types.h>
43 #include <sys/syscall.h>
44 #include <stdlib.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include <limits.h>
48 #include <unistd.h>
49 #include <stdarg.h>
50 #include <time.h>
51
52 #ifdef HAVE_EXECINFO_H
53 #include <execinfo.h>
54 #endif
55
56 verbose_opts_t vb_opts = VB_UNDEF;
57
58 enum { VBFORMAT_LEN = 128 };
59 static verbose_fmt_t vb_fmt;
60 static char fmt_str[VBFORMAT_LEN];
61 static bool quiet = false;
62 static bool silent = false;
63 static bool werror = false;
64 static pthread_mutex_t dlb_clean_mutex = PTHREAD_MUTEX_INITIALIZER;
65
66 130 void debug_init(const options_t *options) {
67 130 quiet = options->quiet;
68 130 silent = options->silent;
69 130 werror = options->debug_opts & DBG_WERROR;
70 130 vb_opts = options->verbose;
71 130 vb_fmt = options->verbose_fmt;
72
73 130 int i = 0;
74
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 2 times.
130 if ( vb_fmt & VBF_NODE ) {
75 char hostname[VBFORMAT_LEN/2];
76 128 gethostname( hostname, VBFORMAT_LEN/2);
77 128 i += sprintf( &fmt_str[i], "%s:", hostname);
78 }
79 #ifdef MPI_LIB
80 if ( vb_fmt & VBF_MPINODE ) { i += sprintf( &fmt_str[i], "%d:", _node_id); }
81 if ( vb_fmt & VBF_MPIRANK ) { i += sprintf( &fmt_str[i], "%d:", _mpi_rank); }
82 #endif
83
84 // Remove last separator ':' if fmt_str is not empty
85
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 2 times.
130 if ( i !=0 ) {
86 128 fmt_str[i-1] = '\0';
87 }
88 130 }
89
90 1601 static void vprint(FILE *fp, const char *prefix, const char *fmt, va_list list) {
91 // Write timestamp
92 enum { TIMESTAMP_MAX_SIZE = 32 };
93 char timestamp[TIMESTAMP_MAX_SIZE];
94
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1601 times.
1601 if (vb_fmt & VBF_TSTAMP) {
95 time_t t = time(NULL);
96 struct tm *tm = localtime(&t);
97 strftime(timestamp, TIMESTAMP_MAX_SIZE, "[%Y-%m-%dT%T] ", tm);
98 } else {
99 1601 timestamp[0] = '\0';
100 }
101
102 // Write spid
103 enum { SPID_MAX_SIZE = 16 };
104 char spid[SPID_MAX_SIZE];
105
4/4
✓ Branch 0 taken 495 times.
✓ Branch 1 taken 1106 times.
✓ Branch 2 taken 248 times.
✓ Branch 3 taken 247 times.
1601 if (vb_fmt & VBF_SPID && thread_spd) {
106 248 snprintf(spid, SPID_MAX_SIZE, ":%d", thread_spd->id);
107 } else {
108 1353 spid[0] = '\0';
109 }
110
111 // Write thread id
112 enum { THREADID_MAX_SIZE = 24 };
113 char threadid[THREADID_MAX_SIZE];
114
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1600 times.
1601 if (vb_fmt & VBF_THREAD) {
115 1 snprintf(threadid, THREADID_MAX_SIZE, ":%ld", syscall(SYS_gettid));
116 } else {
117 1600 threadid[0] = '\0';
118 }
119
120 // Allocate message in an intermediate buffer and print in one function
121 char *msg;
122 1601 vasprintf(&msg, fmt, list);
123 1601 fprintf(fp, "%s%s[%s%s%s]: %s\n", timestamp, prefix, fmt_str, spid, threadid, msg);
124 1601 free(msg);
125 1601 }
126
127 static void __attribute__((__noreturn__)) vfatal(const char *fmt, va_list list) {
128 /* Parse --silent option if fatal() was invoked before init */
129 if (unlikely(vb_opts == VB_UNDEF)) {
130 /* If fatal() was invoked before debug_init, we want to parse the
131 * --silent option but ensuring that parsing the options does not cause
132 * a recursive fatal error */
133 vb_opts = VB_CLEAR;
134 options_parse_entry("--silent", &silent);
135 }
136
137 if (!silent) {
138 vprint(stderr, "DLB PANIC", fmt, list);
139 }
140 dlb_clean();
141 abort();
142 }
143
144 void fatal(const char *fmt, ...) {
145 va_list list;
146 va_start(list, fmt);
147 vfatal(fmt, list);
148 va_end(list);
149 }
150
151 void fatal0(const char *fmt, ...) {
152 #ifdef MPI_LIB
153 if (_mpi_rank <= 0) {
154 #endif
155 va_list list;
156 va_start(list, fmt);
157 vfatal(fmt, list);
158 va_end(list);
159 #ifdef MPI_LIB
160 } else {
161 dlb_clean();
162 abort();
163 }
164 #endif
165 }
166
167 94 static void vwarning(const char *fmt, va_list list) {
168 /* Parse --silent option if warning() was invoked before init */
169
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 58 times.
94 if (unlikely(vb_opts == VB_UNDEF)) {
170 36 options_parse_entry("--silent", &silent);
171 }
172
173
1/2
✓ Branch 0 taken 94 times.
✗ Branch 1 not taken.
94 if (!silent) {
174 94 vprint(stderr, "DLB WARNING", fmt, list);
175 }
176 94 }
177
178 86 void warning(const char *fmt, ...) {
179 va_list list;
180 86 va_start(list, fmt);
181
1/2
✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
86 if (!werror) vwarning(fmt, list);
182 else vfatal(fmt, list);
183 86 va_end(list);
184 86 }
185
186 8 void warning0(const char *fmt, ...) {
187 #ifdef MPI_LIB
188 if (_mpi_rank <= 0) {
189 #endif
190 va_list list;
191 8 va_start(list, fmt);
192
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (!werror) vwarning(fmt, list);
193 else vfatal(fmt, list);
194 8 va_end(list);
195 #ifdef MPI_LIB
196 }
197 #endif
198 8 }
199
200 429 static void vinfo(const char *fmt, va_list list) {
201 /* Parse --quiet and --silent options if info() was invoked before init */
202
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 399 times.
429 if (unlikely(vb_opts == VB_UNDEF)) {
203 30 options_parse_entry("--quiet", &quiet);
204 30 options_parse_entry("--silent", &silent);
205 }
206
207
2/4
✓ Branch 0 taken 429 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 429 times.
✗ Branch 3 not taken.
429 if (!quiet && !silent) {
208 429 vprint(stderr, "DLB", fmt, list);
209 }
210 429 }
211
212 228 void info(const char *fmt, ...) {
213 va_list list;
214 228 va_start(list, fmt);
215 228 vinfo(fmt, list);
216 228 va_end(list);
217 228 }
218
219 201 void info0(const char *fmt, ...) {
220 #ifdef MPI_LIB
221 if (_mpi_rank <= 0) {
222 #endif
223 va_list list;
224 201 va_start(list, fmt);
225 201 vinfo(fmt, list);
226 201 va_end(list);
227 #ifdef MPI_LIB
228 }
229 #endif
230 201 }
231
232 4 void info0_force_print(const char *fmt, ...) {
233 #ifdef MPI_LIB
234 if (_mpi_rank <= 0) {
235 #endif
236 va_list list;
237 4 va_start(list, fmt);
238 4 vprint(stderr, "DLB", fmt, list);
239 4 va_end(list);
240 #ifdef MPI_LIB
241 }
242 #endif
243 4 }
244
245 #undef verbose
246 1481 void verbose(verbose_opts_t flag, const char *fmt, ...) {
247 /* Parse verbose options if verbose() was invoked before init */
248
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 1424 times.
1481 if (unlikely(vb_opts == VB_UNDEF)) {
249 57 options_parse_entry("--verbose", &vb_opts);
250 }
251
252
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1481 times.
1481 if (quiet) return;
253
254 va_list list;
255 1481 va_start(list, fmt);
256
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1481 times.
1481 if (vb_opts & flag & VB_API) { vprint(stderr, "DLB API", fmt, list); }
257
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1481 times.
1481 else if (vb_opts & flag & VB_MICROLB) { vprint(stderr, "DLB MICROLB", fmt, list); }
258
2/2
✓ Branch 0 taken 155 times.
✓ Branch 1 taken 1326 times.
1481 else if (vb_opts & flag & VB_SHMEM) { vprint(stderr, "DLB SHMEM", fmt, list); }
259
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1326 times.
1326 else if (vb_opts & flag & VB_MPI_API) { vprint(stderr, "DLB MPI API", fmt, list); }
260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1326 times.
1326 else if (vb_opts & flag & VB_MPI_INT) { vprint(stderr, "DLB MPI INT", fmt, list); }
261
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1326 times.
1326 else if (vb_opts & flag & VB_STATS) { vprint(stderr, "DLB STATS", fmt, list); }
262
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1325 times.
1326 else if (vb_opts & flag & VB_DROM) { vprint(stderr, "DLB DROM", fmt, list); }
263
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1325 times.
1325 else if (vb_opts & flag & VB_ASYNC) { vprint(stderr, "DLB ASYNC", fmt, list); }
264
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1325 times.
1325 else if (vb_opts & flag & VB_OMPT) { vprint(stderr, "DLB OMPT", fmt, list); }
265
2/2
✓ Branch 0 taken 918 times.
✓ Branch 1 taken 407 times.
1325 else if (vb_opts & flag & VB_AFFINITY){ vprint(stderr, "DLB AFFINITY", fmt, list); }
266
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 407 times.
407 else if (vb_opts & flag & VB_BARRIER) { vprint(stderr, "DLB BARRIER", fmt, list); }
267
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 407 times.
407 else if (vb_opts & flag & VB_TALP) { vprint(stderr, "DLB TALP", fmt, list); }
268
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 407 times.
407 else if (vb_opts & flag & VB_INSTR) { vprint(stderr, "DLB INSTRUMENT", fmt, list); }
269 1481 va_end(list);
270 }
271
272 1 void print_backtrace(void) {
273 #ifdef HAVE_EXECINFO_H
274 void* trace_ptrs[100];
275 1 int count = backtrace( trace_ptrs, 100 );
276 1 char** func_names = backtrace_symbols( trace_ptrs, count );
277 1 fprintf( stderr, "+--------------------------------------\n" );
278
279 // Print the stack trace
280 int i;
281
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
6 for( i = 0; i < count; i++ ) {
282 5 fprintf( stderr, "| %s\n", func_names[i] );
283 }
284
285 // Free the string pointers
286 1 free( func_names );
287 1 fprintf( stderr, "+--------------------------------------\n" );
288 #else
289 fprintf( stderr, "+--------------------------------------\n" );
290 fprintf( stderr, " Backtrace not supported\n") ;
291 fprintf( stderr, "+--------------------------------------\n" );
292 #endif
293 1 }
294
295 static void clean_shmems(pid_t id, const char *shmem_key,
296 int shmem_size_multiplier, int lewi_color) {
297
298 if (shmem_cpuinfo__exists()) {
299 shmem_cpuinfo__finalize(id, shmem_key, lewi_color);
300 }
301 if (shmem_procinfo__exists()) {
302 shmem_procinfo__finalize(id, false, shmem_key, shmem_size_multiplier);
303 }
304 if (shmem_talp__exists()) {
305 shmem_talp__finalize(id);
306 }
307 shmem_async_finalize(id);
308 }
309
310 void dlb_clean(void) {
311 pthread_mutex_lock(&dlb_clean_mutex);
312 {
313 /* First, try to finalize shmems of registered subprocess */
314 const subprocess_descriptor_t** spds = spd_get_spds();
315 const subprocess_descriptor_t** spd = spds;
316 while (*spd) {
317 pid_t id = (*spd)->id;
318 const char *shmem_key = (*spd)->options.shm_key;
319 int shmem_size_multiplier = (*spd)->options.shm_size_multiplier;
320 int lewi_color = (*spd)->options.lewi_color;
321 clean_shmems(id, shmem_key, shmem_size_multiplier, lewi_color);
322 ++spd;
323 }
324 free(spds);
325
326 /* Then, try to finalize current pid */
327 pid_t pid = thread_spd ? thread_spd->id : getpid();
328 const char *shmem_key = thread_spd ? thread_spd->options.shm_key : NULL;
329 int shmem_size_multiplier = thread_spd ? thread_spd->options.shm_size_multiplier : 1;
330 int lewi_color = thread_spd ? thread_spd->options.lewi_color : 0;
331 clean_shmems(pid, shmem_key, shmem_size_multiplier, lewi_color);
332
333 /* Finalize shared memories that do not support subprocesses */
334 shmem_barrier__finalize(shmem_key, shmem_size_multiplier);
335 finalize_comm();
336
337 /* Destroy shared memories if they still exist */
338 const char *shmem_names[] = {"cpuinfo", "procinfo", "talp", "async"};
339 enum { shmem_nelems = sizeof(shmem_names) / sizeof(shmem_names[0]) };
340 int i;
341 for (i=0; i<shmem_nelems; ++i) {
342 if (shmem_exists(shmem_names[i], shmem_key)) {
343 shmem_destroy(shmem_names[i], shmem_key);
344 }
345 }
346 }
347 pthread_mutex_unlock(&dlb_clean_mutex);
348 }
349
350 /* Trigger warning on some errors, tipically common or complex errors during init */
351 26 void warn_error(int error) {
352
3/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
26 switch(error) {
353 12 case DLB_ERR_NOMEM:
354 12 warning("DLB initialization failed due to insufficient space in shared memory."
355 " This error may be caused by corrupted DLB shared memory. If that's the case,"
356 " try running dlb_shm --delete and then attempt again. Alternatively, if you"
357 " need to register a large number of processes, you can use the"
358 " --shm-size-multiplier flag to increase the default shared memory size."
359 " See dlb -hh for more info.");
360 12 break;
361 11 case DLB_ERR_PERM:
362
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 10 times.
11 if (thread_spd != NULL) {
363 1 warning("The process with CPU affinity mask %s failed to initialize DLB."
364 " Please, check that each process initializing DLB has a"
365 1 " non-overlapping set of CPUs.", mu_to_str(&thread_spd->process_mask));
366 } else {
367 10 warning("This process has failed to initialize DLB."
368 " Please, check that each process initializing DLB has a"
369 " non-overlapping set of CPUs.");
370 }
371
4/4
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 4 times.
11 if (shmem_procinfo__exists() && thread_spd != NULL) {
372 1 warning("This is the list of current registered processes and their"
373 " affinity mask:");
374 1 shmem_procinfo__print_info(thread_spd->options.shm_key,
375 1 thread_spd->options.shm_size_multiplier);
376 }
377 11 break;
378 3 case DLB_ERR_NOCOMP:
379 3 warning("DLB could not initialize the shared memory due to incompatible"
380 " options among processes, likely ones sharing CPUs and others not."
381 " Please, if you believe this is a bug contact us at " PACKAGE_BUGREPORT);
382 3 break;
383 }
384 26 }
385
386
387 /* Print Buffers */
388
389 enum { INITIAL_BUFFER_SIZE = 1024 };
390
391 31 void printbuffer_init(print_buffer_t *buffer) {
392 31 buffer->size = INITIAL_BUFFER_SIZE;
393 31 buffer->addr = malloc(INITIAL_BUFFER_SIZE*sizeof(char));
394 31 buffer->offset = buffer->addr;
395 31 buffer->addr[0] = '\0';
396 31 }
397
398 31 void printbuffer_destroy(print_buffer_t *buffer) {
399 31 free(buffer->addr);
400 31 buffer->addr = NULL;
401 31 buffer->offset = NULL;
402 31 buffer->size = 0;
403 31 }
404
405 300 void printbuffer_append(print_buffer_t *buffer, const char *line) {
406 /* Realloc buffer if needed */
407 300 size_t line_len = strlen(line) + 2; /* + '\n\0' */
408 300 size_t buffer_len = strlen(buffer->addr);
409
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 290 times.
300 if (buffer_len + line_len > buffer->size) {
410 10 buffer->size *= 2;
411 10 void *p = realloc(buffer->addr, buffer->size*sizeof(char));
412
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (p) {
413 10 buffer->addr = p;
414 10 buffer->offset = buffer->addr + buffer_len;
415 } else {
416 fatal("realloc failed");
417 }
418 }
419
420 /* Append line to buffer */
421 300 buffer->offset += sprintf(buffer->offset, "%s\n", line);
422 300 buffer_len = buffer->offset - buffer->addr;
423
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 300 times.
300 ensure(strlen(buffer->addr) == buffer_len, "buffer len is not correctly computed");
424 300 }
425