GCC Code Coverage Report


Directory: src/
File: src/support/debug.c
Date: 2024-11-22 17:07:10
Exec Total Coverage
Lines: 130 184 70.7%
Functions: 14 20 70.0%
Branches: 53 96 55.2%

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 "LB_MPI/process_MPI.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 110 void debug_init(const options_t *options) {
67 110 quiet = options->quiet;
68 110 silent = options->silent;
69 110 werror = options->debug_opts & DBG_WERROR;
70 110 vb_opts = options->verbose;
71 110 vb_fmt = options->verbose_fmt;
72
73 110 int i = 0;
74
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 2 times.
110 if ( vb_fmt & VBF_NODE ) {
75 char hostname[VBFORMAT_LEN/2];
76 108 gethostname( hostname, VBFORMAT_LEN/2);
77 108 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 108 times.
✓ Branch 1 taken 2 times.
110 if ( i !=0 ) {
86 108 fmt_str[i-1] = '\0';
87 }
88 110 }
89
90 983 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 983 times.
983 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 983 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 427 times.
✓ Branch 1 taken 556 times.
✓ Branch 2 taken 231 times.
✓ Branch 3 taken 196 times.
983 if (vb_fmt & VBF_SPID && thread_spd) {
106 231 snprintf(spid, SPID_MAX_SIZE, ":%d", thread_spd->id);
107 } else {
108 752 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 982 times.
983 if (vb_fmt & VBF_THREAD) {
115 1 snprintf(threadid, THREADID_MAX_SIZE, ":%ld", syscall(SYS_gettid));
116 } else {
117 982 threadid[0] = '\0';
118 }
119
120 // Allocate message in an intermediate buffer and print in one function
121 char *msg;
122 983 vasprintf(&msg, fmt, list);
123 983 fprintf(fp, "%s%s[%s%s%s]: %s\n", timestamp, prefix, fmt_str, spid, threadid, msg);
124 983 free(msg);
125 983 }
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 64 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 32 times.
✓ Branch 1 taken 32 times.
64 if (unlikely(vb_opts == VB_UNDEF)) {
170 32 options_parse_entry("--silent", &silent);
171 }
172
173
1/2
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
64 if (!silent) {
174 64 vprint(stderr, "DLB WARNING", fmt, list);
175 }
176 64 }
177
178 64 void warning(const char *fmt, ...) {
179 va_list list;
180 64 va_start(list, fmt);
181
1/2
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
64 if (!werror) vwarning(fmt, list);
182 else vfatal(fmt, list);
183 64 va_end(list);
184 64 }
185
186 void warning0(const char *fmt, ...) {
187 #ifdef MPI_LIB
188 if (_mpi_rank <= 0) {
189 #endif
190 va_list list;
191 va_start(list, fmt);
192 if (!werror) vwarning(fmt, list);
193 else vfatal(fmt, list);
194 va_end(list);
195 #ifdef MPI_LIB
196 }
197 #endif
198 }
199
200 420 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 36 times.
✓ Branch 1 taken 384 times.
420 if (unlikely(vb_opts == VB_UNDEF)) {
203 36 options_parse_entry("--quiet", &quiet);
204 36 options_parse_entry("--silent", &silent);
205 }
206
207
2/4
✓ Branch 0 taken 420 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 420 times.
✗ Branch 3 not taken.
420 if (!quiet && !silent) {
208 420 vprint(stderr, "DLB", fmt, list);
209 }
210 420 }
211
212 243 void info(const char *fmt, ...) {
213 va_list list;
214 243 va_start(list, fmt);
215 243 vinfo(fmt, list);
216 243 va_end(list);
217 243 }
218
219 177 void info0(const char *fmt, ...) {
220 #ifdef MPI_LIB
221 if (_mpi_rank <= 0) {
222 #endif
223 va_list list;
224 177 va_start(list, fmt);
225 177 vinfo(fmt, list);
226 177 va_end(list);
227 #ifdef MPI_LIB
228 }
229 #endif
230 177 }
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 672 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 48 times.
✓ Branch 1 taken 624 times.
672 if (unlikely(vb_opts == VB_UNDEF)) {
249 48 options_parse_entry("--verbose", &vb_opts);
250 }
251
252
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 672 times.
672 if (quiet) return;
253
254 va_list list;
255 672 va_start(list, fmt);
256
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 672 times.
672 if (vb_opts & flag & VB_API) { vprint(stderr, "DLB API", fmt, list); }
257
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 672 times.
672 else if (vb_opts & flag & VB_MICROLB) { vprint(stderr, "DLB MICROLB", fmt, list); }
258
2/2
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 568 times.
672 else if (vb_opts & flag & VB_SHMEM) { vprint(stderr, "DLB SHMEM", fmt, list); }
259
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 568 times.
568 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 568 times.
568 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 568 times.
568 else if (vb_opts & flag & VB_STATS) { vprint(stderr, "DLB STATS", fmt, list); }
262
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 567 times.
568 else if (vb_opts & flag & VB_DROM) { vprint(stderr, "DLB DROM", fmt, list); }
263
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 567 times.
567 else if (vb_opts & flag & VB_ASYNC) { vprint(stderr, "DLB ASYNC", fmt, list); }
264
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 567 times.
567 else if (vb_opts & flag & VB_OMPT) { vprint(stderr, "DLB OMPT", fmt, list); }
265
2/2
✓ Branch 0 taken 390 times.
✓ Branch 1 taken 177 times.
567 else if (vb_opts & flag & VB_AFFINITY){ vprint(stderr, "DLB AFFINITY", fmt, list); }
266
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 177 times.
177 else if (vb_opts & flag & VB_BARRIER) { vprint(stderr, "DLB BARRIER", fmt, list); }
267
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 177 times.
177 else if (vb_opts & flag & VB_TALP) { vprint(stderr, "DLB TALP", fmt, list); }
268
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 177 times.
177 else if (vb_opts & flag & VB_INSTR) { vprint(stderr, "DLB INSTRUMENT", fmt, list); }
269 672 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, int lewi_color) {
296 if (shmem_cpuinfo__exists()) {
297 shmem_cpuinfo__finalize(id, shmem_key, lewi_color);
298 }
299 if (shmem_procinfo__exists()) {
300 shmem_procinfo__finalize(id, false, shmem_key);
301 }
302 if (shmem_talp__exists()) {
303 shmem_talp__finalize(id);
304 }
305 shmem_async_finalize(id);
306 }
307
308 void dlb_clean(void) {
309 pthread_mutex_lock(&dlb_clean_mutex);
310 {
311 /* First, try to finalize shmems of registered subprocess */
312 const subprocess_descriptor_t** spds = spd_get_spds();
313 const subprocess_descriptor_t** spd = spds;
314 while (*spd) {
315 pid_t id = (*spd)->id;
316 const char *shmem_key = (*spd)->options.shm_key;
317 int lewi_color = (*spd)->options.lewi_color;
318 clean_shmems(id, shmem_key, lewi_color);
319 ++spd;
320 }
321 free(spds);
322
323 /* Then, try to finalize current pid */
324 pid_t pid = thread_spd ? thread_spd->id : getpid();
325 const char *shmem_key = thread_spd ? thread_spd->options.shm_key : NULL;
326 int lewi_color = thread_spd ? thread_spd->options.lewi_color : 0;
327 clean_shmems(pid, shmem_key, lewi_color);
328
329 /* Finalize shared memories that do not support subprocesses */
330 shmem_barrier__finalize(shmem_key);
331 finalize_comm();
332
333 /* Destroy shared memories if they still exist */
334 const char *shmem_names[] = {"cpuinfo", "procinfo", "talp", "async"};
335 enum { shmem_nelems = sizeof(shmem_names) / sizeof(shmem_names[0]) };
336 int i;
337 for (i=0; i<shmem_nelems; ++i) {
338 if (shmem_exists(shmem_names[i], shmem_key)) {
339 shmem_destroy(shmem_names[i], shmem_key);
340 }
341 }
342 }
343 pthread_mutex_unlock(&dlb_clean_mutex);
344 }
345
346 /* Trigger warning on some errors, tipically common or complex errors during init */
347 13 void warn_error(int error) {
348
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
13 switch(error) {
349 2 case DLB_ERR_NOMEM:
350 2 warning("DLB could not be initialized due to insufficient space in the"
351 " shared memory. If you need to register a high amount of processes"
352 " or believe that this is a bug, please contact us at " PACKAGE_BUGREPORT);
353 2 break;
354 8 case DLB_ERR_PERM:
355
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7 times.
8 if (thread_spd != NULL) {
356 1 warning("The process with CPU affinity mask %s failed to initialize DLB."
357 " Please, check that each process initializing DLB has a"
358 1 " non-overlapping set of CPUs.", mu_to_str(&thread_spd->process_mask));
359 } else {
360 7 warning("This process has failed to initialize DLB."
361 " Please, check that each process initializing DLB has a"
362 " non-overlapping set of CPUs.");
363 }
364
4/4
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 4 times.
8 if (shmem_procinfo__exists() && thread_spd != NULL) {
365 1 warning("This is the list of current registered processes and their"
366 " affinity mask:");
367 1 shmem_procinfo__print_info(thread_spd->options.shm_key);
368 }
369 8 break;
370 3 case DLB_ERR_NOCOMP:
371 3 warning("DLB could not initialize the shared memory due to incompatible"
372 " options among processes, likely ones sharing CPUs and others not."
373 " Please, if you believe this is a bug contact us at " PACKAGE_BUGREPORT);
374 3 break;
375 }
376 13 }
377
378
379 /* Print Buffers */
380
381 enum { INITIAL_BUFFER_SIZE = 1024 };
382
383 31 void printbuffer_init(print_buffer_t *buffer) {
384 31 buffer->size = INITIAL_BUFFER_SIZE;
385 31 buffer->addr = malloc(INITIAL_BUFFER_SIZE*sizeof(char));
386 31 buffer->offset = buffer->addr;
387 31 buffer->addr[0] = '\0';
388 31 }
389
390 31 void printbuffer_destroy(print_buffer_t *buffer) {
391 31 free(buffer->addr);
392 31 buffer->addr = NULL;
393 31 buffer->offset = NULL;
394 31 buffer->size = 0;
395 31 }
396
397 298 void printbuffer_append(print_buffer_t *buffer, const char *line) {
398 /* Realloc buffer if needed */
399 298 size_t line_len = strlen(line) + 2; /* + '\n\0' */
400 298 size_t buffer_len = strlen(buffer->addr);
401
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 288 times.
298 if (buffer_len + line_len > buffer->size) {
402 10 buffer->size *= 2;
403 10 void *p = realloc(buffer->addr, buffer->size*sizeof(char));
404
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (p) {
405 10 buffer->addr = p;
406 10 buffer->offset = buffer->addr + buffer_len;
407 } else {
408 fatal("realloc failed");
409 }
410 }
411
412 /* Append line to buffer */
413 298 buffer->offset += sprintf(buffer->offset, "%s\n", line);
414 298 buffer_len = buffer->offset - buffer->addr;
415
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 298 times.
298 ensure(strlen(buffer->addr) == buffer_len, "buffer len is not correctly computed");
416 298 }
417