GCC Code Coverage Report


Directory: ./
File: src/talp/talp_output.c
Date: 2026-12-01 10:07:21
Exec Total Coverage
Lines: 646 830 77.8%
Functions: 45 47 95.7%
Branches: 249 398 62.6%

Line Branch Exec Source
1 /*********************************************************************************/
2 /* Copyright 2009-2024 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 "talp/talp_output.h"
25
26 #include "LB_core/spd.h"
27 #include "apis/dlb_talp.h"
28 #include "support/debug.h"
29 #include "support/gslist.h"
30 #include "support/mask_utils.h"
31 #include "support/mytime.h"
32 #include "support/options.h"
33 #include "talp/talp.h"
34 #include "talp/talp_types.h"
35 #include "talp/perf_metrics.h"
36
37 #include <errno.h>
38 #include <float.h>
39 #include <libgen.h>
40 #include <limits.h>
41 #include <locale.h>
42 #include <pthread.h>
43 #include <stddef.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <sys/stat.h>
48 #include <unistd.h>
49
50
51 232 static float sanitized_ipc(float instructions, float cycles) {
52
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 230 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
232 if (instructions > 0 && cycles > 0) {
53 2 return instructions / cycles;
54 } else {
55 230 return 0.0f;
56 }
57 }
58
59 258 static const char* make_header(const char *title) {
60 258 int width = 62;
61 static char buf[80];
62 258 int title_len = strlen(title);
63
64
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 258 times.
258 if (width >= (int)sizeof(buf)) {
65 ✗ width = sizeof(buf) - 1; // prevent overflow
66 }
67
68
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 258 times.
258 if (title_len + 2 > width) {
69 // Title too long: just return title
70 ✗ snprintf(buf, sizeof(buf), "%s", title);
71 ✗ return buf;
72 }
73
74 258 int hashes = width - title_len - 2;
75 258 int left = hashes / 2;
76 258 int right = hashes - left;
77
78 258 memset(buf, '#', left);
79 258 buf[left] = ' ';
80 258 memcpy(buf + left + 1, title, title_len);
81 258 buf[left + 1 + title_len] = ' ';
82 258 memset(buf + left + 1 + title_len + 1, '#', right);
83 258 buf[width] = '\0';
84
85 258 return buf;
86 }
87
88
89 /*********************************************************************************/
90 /* GPU vendor */
91 /*********************************************************************************/
92
93 static gpu_vendor_t gpu_vendor = GPU_VENDOR_NONE;
94
95 ✗ void talp_output_record_gpu_vendor(gpu_vendor_t vendor) {
96 ✗ gpu_vendor = vendor;
97 }
98
99 /*********************************************************************************/
100 /* Monitoring Region */
101 /*********************************************************************************/
102
103 8 void talp_output_print_monitoring_region(
104 const dlb_monitor_t *monitor, talp_flags_t talp_flags) {
105
106 char elapsed_time_str[16];
107 8 ns_to_human(elapsed_time_str, 16, monitor->elapsed_time);
108
109 8 monitor_data_t *monitor_data = monitor->_data;
110 8 const char *cpuset_str = mu_to_str(&monitor_data->cpu_mask);
111
112 8 info("%s", make_header("Monitoring Region Summary"));
113 8 info("### Name: %s", monitor->name);
114 8 info("### Elapsed Time: %s", elapsed_time_str);
115 8 info("### Useful time: %"PRId64" ns",
116 8 monitor->useful_time);
117
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 if (talp_flags.have_mpi) {
118 4 info("### Not useful MPI: %"PRId64" ns",
119 4 monitor->mpi_time);
120
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 if (talp_flags.have_openmp) {
121 1 info("### Not useful MPI in worker threads: %"PRId64" ns",
122 1 monitor->mpi_worker_idle_time);
123 }
124 }
125
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7 times.
8 if (talp_flags.have_openmp) {
126 1 info("### Not useful OMP Load Balance: %"PRId64" ns",
127 1 monitor->omp_load_imbalance_time);
128 1 info("### Not useful OMP Scheduling: %"PRId64" ns",
129 1 monitor->omp_scheduling_time);
130 1 info("### Not useful OMP outside parallel: %"PRId64" ns",
131 1 monitor->omp_outside_parallel_time);
132 }
133
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7 times.
8 if (talp_flags.have_gpu) {
134 1 info("### Not useful GPU runtime: %"PRId64" ns",
135 1 monitor->gpu_runtime_time);
136 1 info("### Device useful time: %"PRId64" ns",
137 1 monitor->gpu_useful_time);
138 1 info("### Device communication time: %"PRId64" ns",
139 1 monitor->gpu_communication_time);
140 }
141 8 info("### CpuSet: %s", cpuset_str);
142
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7 times.
8 if (talp_flags.have_hwc) {
143 1 float ipc = sanitized_ipc(monitor->instructions, monitor->cycles);
144 1 info("### IPC: %.2f ", ipc);
145 }
146
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 if (talp_flags.have_mpi) {
147 4 info("### Number of MPI calls: %"PRId64,
148 4 monitor->num_mpi_calls);
149 }
150
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7 times.
8 if (talp_flags.have_openmp) {
151 1 info("### Number of OpenMP parallels: %"PRId64,
152 1 monitor->num_omp_parallels);
153 1 info("### Number of OpenMP tasks: %"PRId64,
154 1 monitor->num_omp_tasks);
155 }
156
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7 times.
8 if (talp_flags.have_gpu) {
157 1 info("### Number of GPU runtime calls: %"PRId64,
158 1 monitor->num_gpu_runtime_calls);
159 }
160
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (talp_flags.have_energy) {
161 ✗ info("### Accumulated node energy: %.2f J",
162 ✗ monitor->node_energy);
163 ✗ info("### - CPU energy: %.2f J",
164 ✗ monitor->cpu_energy);
165 ✗ info("### - DRAM energy: %.2f J",
166 ✗ monitor->dram_energy);
167 ✗ info("### - Peak node power: %.2f W",
168 ✗ monitor->node_peak_power);
169 ✗ info("### - GPU energy: %.2f J",
170 ✗ monitor->gpu_energy);
171 ✗ info("### - Peak GPU power: %.2f W",
172 ✗ monitor->gpu_peak_power);
173 }
174 8 }
175
176
177 /*********************************************************************************/
178 /* POP Metrics */
179 /*********************************************************************************/
180
181 static GSList *pop_metrics_records = NULL;
182
183 249 void talp_output_record_pop_metrics(const dlb_pop_metrics_t *metrics) {
184
185 /* Copy structure */
186 249 dlb_pop_metrics_t *new_record = malloc(sizeof(dlb_pop_metrics_t));
187 249 *new_record = *metrics;
188
189 /* Add record to list */
190 249 pop_metrics_records = g_slist_prepend(pop_metrics_records, new_record);
191 249 }
192
193 26 static void pop_metrics_print(void) {
194
195 26 for (GSList *node = pop_metrics_records;
196
2/2
✓ Branch 0 taken 234 times.
✓ Branch 1 taken 26 times.
260 node != NULL;
197 234 node = node->next) {
198
199 234 dlb_pop_metrics_t *record = node->data;
200
201
2/2
✓ Branch 0 taken 231 times.
✓ Branch 1 taken 3 times.
234 if (record->elapsed_time > 0) {
202
203 231 float avg_ipc = sanitized_ipc(record->instructions, record->cycles);
204 char elapsed_time_str[16];
205 231 ns_to_human(elapsed_time_str, 16, record->elapsed_time);
206
207
2/2
✓ Branch 0 taken 230 times.
✓ Branch 1 taken 1 times.
461 bool have_gpu_activity = record->num_gpu_runtime_calls > 0 ||
208
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 230 times.
230 record->gpu_useful_time > 0;
209
210 231 bool have_node_energy = record->node_energy > 0;
211 231 bool have_cpu_energy = record->cpu_energy > 0;
212 231 bool have_dram_energy = record->dram_energy > 0;
213
3/6
✓ Branch 0 taken 231 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 231 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 231 times.
231 bool have_host_energy_activity = have_node_energy || have_cpu_energy || have_dram_energy;
214 231 bool have_gpu_energy_activity = record->gpu_energy > 0;
215
2/4
✓ Branch 0 taken 231 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 231 times.
231 bool have_energy_activity = have_host_energy_activity || have_gpu_energy_activity;
216
217 231 info("%s", make_header("Monitoring Region POP Metrics"));
218 231 info("### Name: %s", record->name);
219 231 info("### Elapsed Time: %s", elapsed_time_str);
220
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 230 times.
231 if (have_gpu_activity) {
221 1 info("### Host");
222 1 info("### ----");
223 }
224
1/2
✓ Branch 0 taken 231 times.
✗ Branch 1 not taken.
231 if (record->mpi_parallel_efficiency > 0.0f &&
225
1/2
✓ Branch 0 taken 231 times.
✗ Branch 1 not taken.
231 record->omp_parallel_efficiency > 0.0f) {
226 231 info("### Parallel efficiency: %1.2f",
227 231 record->parallel_efficiency);
228 }
229
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 230 times.
231 if (record->num_mpi_calls > 0) {
230 1 info("### - MPI Parallel efficiency: %1.2f",
231 1 record->mpi_parallel_efficiency);
232 1 info("### - Communication efficiency: %1.2f",
233 1 record->mpi_communication_efficiency);
234 1 info("### - Load Balance: %1.2f",
235 1 record->mpi_load_balance);
236 1 info("### - In: %1.2f",
237 1 record->mpi_load_balance_in);
238 1 info("### - Out: %1.2f",
239 1 record->mpi_load_balance_out);
240 }
241
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 229 times.
231 if (record->num_omp_parallels + record->num_omp_tasks > 0) {
242 2 info("### - OpenMP Parallel efficiency: %1.2f",
243 2 record->omp_parallel_efficiency);
244 2 info("### - Load Balance: %1.2f",
245 2 record->omp_load_balance);
246 2 info("### - Scheduling efficiency: %1.2f",
247 2 record->omp_scheduling_efficiency);
248 2 info("### - Coverage efficiency: %1.2f",
249 2 record->omp_coverage_efficiency);
250 }
251
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 230 times.
231 if (have_gpu_activity) {
252 1 info("### - Device Offload efficiency: %1.2f",
253 1 record->device_offload_efficiency);
254 }
255
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 230 times.
231 if (avg_ipc > 0.0f) {
256 1 float avg_freq = record->cycles / record->useful_time;
257 1 info("### Computational metrics:");
258 1 info("### - Average useful IPC: %1.2f", avg_ipc);
259 1 info("### - Average useful frequency: %1.2f GHz", avg_freq);
260 1 info("### - Number of instructions: %1.2E", record->instructions);
261 }
262
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 230 times.
231 if (have_gpu_activity) {
263 1 info("###");
264 1 info("### %s Device",
265
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 gpu_vendor == GPU_VENDOR_NVIDIA ? "NVIDIA"
266 1 : gpu_vendor == GPU_VENDOR_AMD ? "AMD"
267
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 : "GPU");
268 1 info("### %s-------",
269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 gpu_vendor == GPU_VENDOR_NVIDIA ? "------"
270 : gpu_vendor == GPU_VENDOR_AMD ? "---"
271 : "---");
272 1 info("### Parallel efficiency: %1.2f",
273 1 record->gpu_parallel_efficiency);
274 1 info("### - Load Balance: %1.2f",
275 1 record->gpu_load_balance);
276 1 info("### - Communication efficiency: %1.2f",
277 1 record->gpu_communication_efficiency);
278 1 info("### - Orchestration efficiency: %1.2f",
279 1 record->gpu_orchestration_efficiency);
280 }
281
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 231 times.
231 if (have_energy_activity) {
282 ✗ info("### Energy metrics:");
283 ✗ if (have_host_energy_activity) {
284 ✗ if (have_gpu_energy_activity) {
285 ✗ info("### - Host");
286 ✗ if (have_node_energy) {
287 ✗ info("### - Node Energy: %1.2f J",
288 record->node_energy);
289 }
290 ✗ if (have_cpu_energy) {
291 ✗ info("### - CPU Energy: %1.2f J",
292 record->cpu_energy);
293 }
294 ✗ if (have_dram_energy) {
295 ✗ info("### - DRAM Energy: %1.2f J",
296 record->dram_energy);
297 }
298 } else {
299 ✗ if (have_node_energy) {
300 ✗ info("### - Node Energy: %1.2f J",
301 record->node_energy);
302 }
303 ✗ if (have_cpu_energy) {
304 ✗ info("### - CPU Energy: %1.2f J",
305 record->cpu_energy);
306 }
307 ✗ if (have_dram_energy) {
308 ✗ info("### - DRAM Energy: %1.2f J",
309 record->dram_energy);
310 }
311 }
312 }
313 ✗ if (have_gpu_energy_activity) {
314 ✗ if (have_host_energy_activity) {
315 ✗ info("### - GPU");
316 ✗ info("### - GPU Energy: %1.2f J",
317 record->gpu_energy);
318 } else {
319 ✗ info("### - GPU Energy: %1.2f J",
320 record->gpu_energy);
321 }
322 }
323
324 ✗ if (have_node_energy || have_gpu_energy_activity) {
325 ✗ info("### Power metrics:");
326 }
327 ✗ if (have_node_energy) {
328 ✗ double node_power = record->node_energy / (record->elapsed_time / 1e9);
329 ✗ if (have_gpu_energy_activity) {
330 ✗ info("### - Host");
331 ✗ info("### - Average Node Power: %1.2f W",
332 node_power);
333 ✗ info("### - Peak Node Power: %1.2f W",
334 record->node_peak_power);
335 } else {
336 ✗ info("### - Average Node Power: %1.2f W",
337 node_power);
338 ✗ info("### - Peak Node Power: %1.2f W",
339 record->node_peak_power);
340 }
341 }
342 ✗ if (have_gpu_energy_activity) {
343 ✗ double gpu_power = record->gpu_energy / (record->elapsed_time / 1e9);
344 ✗ if (have_node_energy) {
345 ✗ info("### - GPU");
346 ✗ info("### - Average GPU Power: %1.2f W",
347 gpu_power);
348 ✗ info("### - Peak GPU Power: %1.2f W",
349 record->gpu_peak_power);
350 } else {
351 ✗ info("### - Average GPU Power: %1.2f W",
352 gpu_power);
353 ✗ info("### - Peak GPU Power: %1.2f W",
354 record->gpu_peak_power);
355 }
356 }
357 }
358 } else {
359 3 info("%s", make_header("Monitoring Region POP Metrics"));
360 3 info("### Name: %s", record->name);
361 3 info("### No data ###");
362 }
363 }
364 26 }
365
366 7 static void pop_metrics_to_json(FILE *out_file) {
367
368
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if (pop_metrics_records != NULL) {
369 7 fprintf(out_file,
370 " \"Application\": {\n");
371
372 7 for (GSList *node = pop_metrics_records;
373
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
14 node != NULL;
374 7 node = node->next) {
375
376 7 dlb_pop_metrics_t *record = node->data;
377
378 7 fprintf(out_file,
379 " \"%s\": {\n"
380 #define PRINT_FMT(var_name, c_type, json_name, fmt) \
381 " \"" #json_name "\": " fmt ",\n"
382 #define PRINT_FMT_LAST(var_name, c_type, json_name, fmt) \
383 " \"" #json_name "\": " fmt "\n"
384 FOR_DLB_POP_METRICS_FIELDS(PRINT_FMT, PRINT_FMT_LAST)
385 #undef PRINT_FMT
386 #undef PRINT_FMT_LAST
387 " }%s\n",
388 7 record->name
389 #define PRINT_ARG(var_name, c_type, json_name, fmt) \
390 , record->var_name
391 7 FOR_DLB_POP_METRICS_FIELDS(PRINT_ARG, PRINT_ARG)
392 #undef PRINT_ARG
393
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 , node->next != NULL ? "," : "");
394 }
395 7 fprintf(out_file,
396 " }"); /* no eol */
397 }
398 7 }
399
400 3 static void pop_metrics_to_txt(FILE *out_file) {
401
402 3 for (GSList *node = pop_metrics_records;
403
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 node != NULL;
404 3 node = node->next) {
405
406 3 dlb_pop_metrics_t *record = node->data;
407
408
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (record->elapsed_time > 0) {
409 3 fprintf(out_file,
410 "%s\n"
411 "### Name: %s\n"
412 "### Number of CPUs: %d\n"
413 "### Number of OpenMP threads: %d\n"
414 "### Number of MPI processes: %d\n"
415 "### Number of nodes: %d\n"
416 "### Number of GPUs: %d\n"
417 "### Cycles: %.0f\n"
418 "### Instructions: %.0f\n"
419 "### Number of measurements: %"PRId64"\n"
420 "### Number of MPI calls: %"PRId64"\n"
421 "### Number of OpenMP parallel regions: %"PRId64"\n"
422 "### Number of OpenMP explicit tasks: %"PRId64"\n"
423 "### Number of GPU runtime calls: %"PRId64"\n"
424 "### Elapsed Time (ns): %"PRId64"\n"
425 "### Useful Time (ns): %"PRId64"\n"
426 "### MPI Time (ns): %"PRId64"\n"
427 "### OpenMP Load Imbalance Time (ns): %"PRId64"\n"
428 "### OpenMP Scheduling Time (ns): %"PRId64"\n"
429 "### OpenMP Outside Parallel Time (ns): %"PRId64"\n"
430 "### GPU Runtime Time (ns): %"PRId64"\n"
431 "### MPI time normalized at process level of\n"
432 "### the process with the max non-MPI time: %.0f\n"
433 "### MPI time normalized at node level of\n"
434 "### the process with the max non-MPI time: %.0f\n"
435 "### Device useful time: %"PRId64"\n"
436 "### Device communication time: %"PRId64"\n"
437 "### Device max useful time: %"PRId64"\n"
438 "### Device max active time: %"PRId64"\n"
439 "### CPU Energy (J): %.2f\n"
440 "### DRAM Energy (J): %.2f\n"
441 "### Node Energy (J): %.2f\n"
442 "### Peak Node Power (W): %.2f\n"
443 "### GPU Energy (J): %.2f\n"
444 "### Peak GPU Power (W) %.2f\n"
445 "### --- Host metrics ---\n"
446 "### Parallel efficiency: %.2f\n"
447 "### MPI Parallel efficiency: %.2f\n"
448 "### - MPI Communication efficiency: %.2f\n"
449 "### - MPI Load Balance: %.2f\n"
450 "### - MPI Load Balance in: %.2f\n"
451 "### - MPI Load Balance out: %.2f\n"
452 "### OpenMP Parallel efficiency: %.2f\n"
453 "### - OpenMP Load Balance: %.2f\n"
454 "### - OpenMP Scheduling efficiency: %.2f\n"
455 "### - OpenMP Coverage efficiency: %.2f\n"
456 "### Device Offload efficiency: %.2f\n"
457 "### --- Device metrics ---\n"
458 "### Device Parallel efficiency: %.2f\n"
459 "### - Device Load Balance: %.2f\n"
460 "### - Device Communication efficiency: %.2f\n"
461 "### - Device Orchestration efficiency: %.2f\n",
462 make_header("Monitoring Region POP Metrics"),
463 3 record->name,
464 record->num_cpus,
465 record->num_omp_threads,
466 record->num_mpi_ranks,
467 record->num_nodes,
468 record->num_gpus,
469 record->cycles,
470 record->instructions,
471 record->num_measurements,
472 record->num_mpi_calls,
473 record->num_omp_parallels,
474 record->num_omp_tasks,
475 record->num_gpu_runtime_calls,
476 record->elapsed_time,
477 record->useful_time,
478 record->mpi_time,
479 record->omp_load_imbalance_time,
480 record->omp_scheduling_time,
481 record->omp_outside_parallel_time,
482 record->gpu_runtime_time,
483 record->min_mpi_normd_proc,
484 record->min_mpi_normd_node,
485 record->gpu_useful_time,
486 record->gpu_communication_time,
487 record->max_gpu_useful_time,
488 record->max_gpu_active_time,
489 record->cpu_energy,
490 record->dram_energy,
491 record->node_energy,
492 record->node_peak_power,
493 record->gpu_energy,
494 record->gpu_peak_power,
495 3 record->parallel_efficiency,
496 3 record->mpi_parallel_efficiency,
497 3 record->mpi_communication_efficiency,
498 3 record->mpi_load_balance,
499 3 record->mpi_load_balance_in,
500 3 record->mpi_load_balance_out,
501 3 record->omp_parallel_efficiency,
502 3 record->omp_load_balance,
503 3 record->omp_scheduling_efficiency,
504 3 record->omp_coverage_efficiency,
505 3 record->device_offload_efficiency,
506 3 record->gpu_parallel_efficiency,
507 3 record->gpu_load_balance,
508 3 record->gpu_communication_efficiency,
509 3 record->gpu_orchestration_efficiency
510 );
511 } else {
512 ✗ fprintf(out_file,
513 "%s\n"
514 "### Name: %s\n"
515 "### No data ###\n",
516 make_header("Monitoring Region POP Metrics"),
517 ✗ record->name);
518 }
519 }
520 3 }
521
522 static const char* pop_metrics_csv_schema =
523 "name,"
524 #define PRINT_FMT(var_name, c_type, json_name, fmt) \
525 #json_name ","
526 #define PRINT_FMT_LAST(var_name, c_type, json_name, fmt) \
527 #json_name
528 FOR_DLB_POP_METRICS_FIELDS(PRINT_FMT, PRINT_FMT_LAST);
529 #undef PRINT_FMT
530 #undef PRINT_FMT_LAST
531
532 5 static void pop_metrics_to_csv(FILE *out_file, bool append) {
533
534
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (pop_metrics_records == NULL) return;
535
536
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
5 if (!append) {
537 /* Print header */
538 3 fprintf(out_file, "%s\n", pop_metrics_csv_schema);
539 }
540
541 5 for (GSList *node = pop_metrics_records;
542
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
10 node != NULL;
543 5 node = node->next) {
544
545 5 dlb_pop_metrics_t *record = node->data;
546
547 5 fprintf(out_file,
548 "\"%s\","
549 #define PRINT_FMT(var_name, c_type, json_name, fmt) \
550 fmt ","
551 #define PRINT_FMT_LAST(var_name, c_type, json_name, fmt) \
552 fmt "\n"
553 FOR_DLB_POP_METRICS_FIELDS(PRINT_FMT, PRINT_FMT_LAST)
554 #undef PRINT_FMT
555 #undef PRINT_FMT_LAST
556 5 , record->name
557 #define PRINT_ARG(var_name, c_type, json_name, fmt) \
558 , record->var_name
559 5 FOR_DLB_POP_METRICS_FIELDS(PRINT_ARG, PRINT_ARG)
560 #undef PRINT_ARG
561 );
562 }
563 }
564
565 41 static void pop_metrics_finalize(void) {
566
567 /* Free every record data */
568 41 for (GSList *node = pop_metrics_records;
569
2/2
✓ Branch 0 taken 249 times.
✓ Branch 1 taken 41 times.
290 node != NULL;
570 249 node = node->next) {
571
572 249 dlb_pop_metrics_t *record = node->data;
573 249 free(record);
574 }
575
576 /* Free list */
577 41 g_slist_free(pop_metrics_records);
578 41 pop_metrics_records = NULL;
579 41 }
580
581
582 /*********************************************************************************/
583 /* Node */
584 /*********************************************************************************/
585
586 static GSList *node_records = NULL;
587
588 ✗ void talp_output_record_node(const node_record_t *node_record) {
589
590 ✗ int nelems = node_record->nelems;
591
592 /* Allocate new record */
593 ✗ size_t process_records_size = sizeof(process_in_node_record_t) * nelems;
594 ✗ size_t node_record_size = sizeof(node_record_t) + process_records_size;
595 ✗ node_record_t *new_record = malloc(node_record_size);
596
597 /* Memcpy the entire struct */
598 ✗ memcpy(new_record, node_record, node_record_size);
599
600 /* Insert to list */
601 ✗ node_records = g_slist_prepend(node_records, new_record);
602 }
603
604 26 static void node_print(void) {
605
606 26 for (GSList *node = node_records;
607
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 node != NULL;
608 ✗ node = node->next) {
609
610 ✗ node_record_t *node_record = node->data;
611
612 ✗ info(" |----------------------------------------------------------|");
613 ✗ info(" | Extended Report Node %4d |",
614 node_record->node_id);
615 ✗ info(" |----------------------------------------------------------|");
616 ✗ info(" | Process | Useful Time | MPI Time |");
617 ✗ info(" |------------|----------------------|----------------------|");
618
619 ✗ for (int i = 0; i < node_record->nelems; ++i) {
620 ✗ info(" | %-10d | %18e s | %18e s |",
621 node_record->processes[i].pid,
622 nsecs_to_secs(node_record->processes[i].useful_time),
623 nsecs_to_secs(node_record->processes[i].mpi_time));
624 ✗ info(" |------------|----------------------|----------------------|");
625 }
626
627 ✗ if (node_record->nelems > 0) {
628 ✗ info(" |------------|----------------------|----------------------|");
629 ✗ info(" | %-10s | %18e s | %18e s |", "Node Avg",
630 nsecs_to_secs(node_record->avg_useful_time),
631 nsecs_to_secs(node_record->avg_mpi_time));
632 ✗ info(" |------------|----------------------|----------------------|");
633 ✗ info(" | %-10s | %18e s | %18e s |", "Node Max",
634 nsecs_to_secs(node_record->max_useful_time),
635 nsecs_to_secs(node_record->max_mpi_time));
636 ✗ info(" |------------|----------------------|----------------------|");
637 }
638 }
639 26 }
640
641 7 static void node_to_json(FILE *out_file) {
642
643
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if (node_records == NULL) return;
644
645 /* If there are pop_metrics, append to the existing dictionary */
646 ✗ if (pop_metrics_records != NULL) {
647 ✗ fprintf(out_file,",\n");
648 }
649
650 ✗ fprintf(out_file,
651 " \"node\": [\n");
652
653 ✗ for (GSList *node = node_records;
654 ✗ node != NULL;
655 ✗ node = node->next) {
656
657 ✗ node_record_t *node_record = node->data;
658
659 ✗ fprintf(out_file,
660 " {\n"
661 " \"id\": \"%d\",\n"
662 " \"process\": [\n",
663 node_record->node_id);
664
665 ✗ for (int i = 0; i < node_record->nelems; ++i) {
666 ✗ fprintf(out_file,
667 " {\n"
668 " \"id\": %d,\n"
669 " \"usefulTime\": %"PRId64",\n"
670 " \"mpiTime\": %"PRId64"\n"
671 " }%s\n",
672 node_record->processes[i].pid,
673 node_record->processes[i].useful_time,
674 node_record->processes[i].mpi_time,
675 ✗ i+1 < node_record->nelems ? "," : "");
676 }
677
678 ✗ fprintf(out_file,
679 " ],\n"
680 " \"nodeAvg\": {\n"
681 " \"usefulTime\": %"PRId64",\n"
682 " \"mpiTime\": %"PRId64"\n"
683 " },\n"
684 " \"nodeMax\": {\n"
685 " \"usefulTime\": %"PRId64",\n"
686 " \"mpiTime\": %"PRId64"\n"
687 " }\n"
688 " }%s\n",
689 node_record->avg_useful_time,
690 node_record->avg_mpi_time,
691 node_record->max_useful_time,
692 node_record->max_mpi_time,
693 ✗ node->next != NULL ? "," : "");
694 }
695 ✗ fprintf(out_file,
696 " ]"); /* no eol */
697 }
698
699 4 static void node_to_csv(FILE *out_file, bool append) {
700
701
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (node_records == NULL) return;
702
703 ✗ if (!append) {
704 /* Print header */
705 ✗ fprintf(out_file,
706 "NodeId,"
707 "ProcessId,"
708 "ProcessUsefulTime,"
709 "ProcessMPITime,"
710 "NodeAvgUsefulTime,"
711 "NodeAvgMPITime,"
712 "NodeMaxUsefulTime,"
713 "NodeMaxMPITime\n");
714 }
715
716 ✗ for (GSList *node = node_records;
717 ✗ node != NULL;
718 ✗ node = node->next) {
719
720 ✗ node_record_t *node_record = node->data;
721
722 ✗ for (int i = 0; i < node_record->nelems; ++i) {
723 ✗ fprintf(out_file,
724 "%d," /* NodeId */
725 "%d," /* ProcessId */
726 "%"PRId64"," /* ProcessUsefulTime */
727 "%"PRId64"," /* ProcessMPITime */
728 "%"PRId64"," /* NodeAvgUsefulTime */
729 "%"PRId64"," /* NodeAvgMPITime*/
730 "%"PRId64"," /* NodeMaxUsefulTime */
731 "%"PRId64"\n", /* NodeMaxMPITime*/
732 node_record->node_id,
733 node_record->processes[i].pid,
734 node_record->processes[i].useful_time,
735 node_record->processes[i].mpi_time,
736 node_record->avg_useful_time,
737 node_record->avg_mpi_time,
738 node_record->max_useful_time,
739 node_record->max_mpi_time);
740
741 }
742 }
743 }
744
745 3 static void node_to_txt(FILE *out_file) {
746
747 3 for (GSList *node = node_records;
748
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 node != NULL;
749 ✗ node = node->next) {
750
751 ✗ node_record_t *node_record = node->data;
752
753 ✗ fprintf(out_file,
754 " |----------------------------------------------------------|\n"
755 " | Extended Report Node %4d |\n"
756 " |----------------------------------------------------------|\n"
757 " | Process | Useful Time | MPI Time |\n"
758 " |------------|----------------------|----------------------|\n",
759 node_record->node_id);
760
761 ✗ for (int i = 0; i < node_record->nelems; ++i) {
762 ✗ fprintf(out_file,
763 " | %-10d | %18e s | %18e s |\n"
764 " |------------|----------------------|----------------------|\n",
765 node_record->processes[i].pid,
766 nsecs_to_secs(node_record->processes[i].useful_time),
767 nsecs_to_secs(node_record->processes[i].mpi_time));
768 }
769
770 ✗ if (node_record->nelems > 0) {
771 ✗ fprintf(out_file,
772 " |------------|----------------------|----------------------|\n"
773 " | %-10s | %18e s | %18e s |\n"
774 " |------------|----------------------|----------------------|\n"
775 " | %-10s | %18e s | %18e s |\n"
776 " |------------|----------------------|----------------------|\n",
777 "Node Avg",
778 nsecs_to_secs(node_record->avg_useful_time),
779 nsecs_to_secs(node_record->avg_mpi_time),
780 "Node Max",
781 nsecs_to_secs(node_record->max_useful_time),
782 nsecs_to_secs(node_record->max_mpi_time));
783 }
784 }
785 3 }
786
787 41 static void node_finalize(void) {
788
789 /* Free every record data */
790 41 for (GSList *node = node_records;
791
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
41 node != NULL;
792 ✗ node = node->next) {
793
794 ✗ node_record_t *record = node->data;
795 ✗ free(record);
796 }
797
798 /* Free list */
799 41 g_slist_free(node_records);
800 41 node_records = NULL;
801 41 }
802
803
804 /*********************************************************************************/
805 /* Process */
806 /*********************************************************************************/
807
808 typedef struct region_record_t {
809 char name[DLB_MONITOR_NAME_MAX];
810 int num_mpi_ranks;
811 process_record_t process_records[];
812 } region_record_t;
813
814 static GSList *region_records = NULL;
815
816 15 void talp_output_record_process(const char *region_name,
817 const process_record_t *process_record, int num_mpi_ranks) {
818
819 15 int rank = process_record->rank;
820
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 13 times.
15 if (num_mpi_ranks < 1) {
821 /* special value to dissociate record->rank if the argument has a
822 * proper value but still we're only recording one element.
823 * (usually the case if --talp-partial-output) */
824 2 num_mpi_ranks = 1;
825 2 rank = 0;
826 }
827
828 15 region_record_t *region_record = NULL;
829
830 /* Find region or allocate new one */
831 15 for (GSList *node = region_records;
832
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 15 times.
16 node != NULL;
833 1 node = node->next) {
834
835 1 region_record_t *record = node->data;
836
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (strcmp(record->name, region_name) == 0) {
837 ✗ region_record = record;
838 ✗ break;
839 }
840 }
841
842 /* Allocate if not found */
843
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 if (region_record == NULL) {
844 /* Allocate and initialize new region */
845 15 size_t region_record_size = sizeof(region_record_t) +
846 15 sizeof(process_record_t) * num_mpi_ranks;
847 15 region_record = malloc(region_record_size);
848 15 *region_record = (const region_record_t) {
849 .num_mpi_ranks = num_mpi_ranks,
850 };
851 15 snprintf(region_record->name, DLB_MONITOR_NAME_MAX, "%s",
852 region_name);
853
854 /* Insert to list */
855 15 region_records = g_slist_prepend(region_records, region_record);
856 }
857
858 /* Copy process_record */
859
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 ensure(rank < num_mpi_ranks, "Wrong rank number in %s", __func__);
860 15 memcpy(&region_record->process_records[rank], process_record, sizeof(process_record_t));
861 15 }
862
863 26 static void process_print(void) {
864
865 26 for (GSList *node = region_records;
866
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 26 times.
30 node != NULL;
867 4 node = node->next) {
868
869 4 region_record_t *region_record = node->data;
870
871
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 for (int i = 0; i < region_record->num_mpi_ranks; ++i) {
872
873 4 process_record_t *process_record = &region_record->process_records[i];
874
875 4 info("%s", make_header("Monitoring Region Summary"));
876 4 info("### Name: %s",
877 4 region_record->name);
878 4 info("### Process: %d (%s)",
879 4 process_record->pid, process_record->hostname);
880 4 info("### Rank: %d",
881 process_record->rank);
882 4 info("### CpuSet: %s",
883 4 process_record->cpuset);
884 4 info("### Elapsed time: %"PRId64" ns",
885 process_record->monitor.elapsed_time);
886 4 info("### Useful time: %"PRId64" ns",
887 process_record->monitor.useful_time);
888
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 if (process_record->monitor.mpi_time > 0) {
889 1 info("### Not useful MPI: %"PRId64" ns",
890 process_record->monitor.mpi_time);
891 }
892
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (process_record->monitor.mpi_worker_idle_time > 0) {
893 ✗ info("### Not useful MPI in worker threads: %"PRId64" ns",
894 process_record->monitor.mpi_worker_idle_time);
895 }
896
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (process_record->monitor.omp_load_imbalance_time > 0
897
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 || process_record->monitor.omp_scheduling_time > 0
898
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 || process_record->monitor.omp_outside_parallel_time > 0) {
899 ✗ info("### Not useful OMP Load Imbalance: %"PRId64" ns",
900 process_record->monitor.omp_load_imbalance_time);
901 ✗ info("### Not useful OMP Scheduling: %"PRId64" ns",
902 process_record->monitor.omp_scheduling_time);
903 ✗ info("### Not useful OMP outside parallel: %"PRId64" ns",
904 process_record->monitor.omp_outside_parallel_time);
905 }
906
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (process_record->monitor.gpu_runtime_time > 0) {
907 ✗ info("### Not useful GPU runtime: %"PRId64" ns",
908 process_record->monitor.gpu_runtime_time);
909 ✗ info("### Device useful time: %"PRId64" ns",
910 process_record->monitor.gpu_useful_time);
911 ✗ info("### Device communication time: %"PRId64" ns",
912 process_record->monitor.gpu_communication_time);
913 }
914
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (process_record->monitor.instructions > 0
915 ✗ && process_record->monitor.cycles > 0) {
916 ✗ info("### IPC : %.2f",
917 ✗ (float)process_record->monitor.instructions
918 ✗ / process_record->monitor.cycles);
919 }
920 }
921 }
922 26 }
923
924 7 static void process_to_json(FILE *out_file) {
925
926
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (region_records == NULL) return;
927
928 /* If there are pop_metrics or node_metrics, append to the existing dictionary */
929
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (pop_metrics_records != NULL
930 ✗ || node_records != NULL) {
931 7 fprintf(out_file,",\n");
932 }
933
934 7 fprintf(out_file,
935 " \"Process\": {\n");
936
937 7 for (GSList *node = region_records;
938
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
14 node != NULL;
939 7 node = node->next) {
940
941 7 region_record_t *region_record = node->data;
942
943 7 fprintf(out_file,
944 " \"%s\": [\n",
945 7 region_record->name);
946
947
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
14 for (int i = 0; i < region_record->num_mpi_ranks; ++i) {
948
949 7 process_record_t *process_record = &region_record->process_records[i];
950
951 7 fprintf(out_file,
952 " {\n"
953 " \"rank\": %d,\n"
954 " \"pid\": %d,\n"
955 " \"nodeId\": %d,\n"
956 " \"hostname\": \"%s\",\n"
957 " \"cpuset\": %s,\n"
958 #define PRINT_FMT(var_name, c_type, json_name, fmt) \
959 " \"" #json_name "\": " fmt ",\n"
960 #define PRINT_FMT_LAST(var_name, c_type, json_name, fmt) \
961 " \"" #json_name "\": " fmt "\n"
962 FOR_DLB_MONITOR_PRINTABLE_FIELDS(PRINT_FMT, PRINT_FMT_LAST)
963 #undef PRINT_FMT
964 #undef PRINT_FMT_LAST
965 " }%s\n",
966 process_record->rank,
967 process_record->pid,
968 process_record->node_id,
969 7 process_record->hostname,
970 7 process_record->cpuset_quoted,
971 #define PRINT_ARG(var_name, c_type, json_name, fmt) \
972 process_record->monitor.var_name,
973 FOR_DLB_MONITOR_PRINTABLE_FIELDS(PRINT_ARG, PRINT_ARG)
974 #undef PRINT_ARG
975
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 i + 1 < region_record->num_mpi_ranks ? "," : "");
976 }
977 7 fprintf(out_file,
978 " ]%s\n",
979
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 node->next != NULL ? "," : "");
980 }
981 7 fprintf(out_file,
982 " }"); /* no eol */
983 }
984
985 static const char *process_csv_schema =
986 "Region," "Rank," "PID," "NodeId," "Hostname," "CpuSet,"
987 #define PRINT_FMT(var_name, c_type, json_name, fmt) \
988 #json_name ","
989 #define PRINT_FMT_LAST(var_name, c_type, json_name, fmt) \
990 #json_name
991 FOR_DLB_MONITOR_PRINTABLE_FIELDS(PRINT_FMT, PRINT_FMT_LAST);
992 #undef PRINT_FMT
993 #undef PRINT_FMT_LAST
994
995 5 static void process_to_csv(FILE *out_file, bool append) {
996
997
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 if (region_records == NULL) return;
998
999
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!append) {
1000 /* Print header */
1001 1 fprintf(out_file, "%s\n", process_csv_schema);
1002 }
1003
1004 1 for (GSList *node = region_records;
1005
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 node != NULL;
1006 1 node = node->next) {
1007
1008 1 region_record_t *region_record = node->data;
1009
1010
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (int i = 0; i < region_record->num_mpi_ranks; ++i) {
1011
1012 1 process_record_t *process_record = &region_record->process_records[i];
1013
1014 1 fprintf(out_file,
1015 "%s," /* Region */
1016 "%d," /* Rank */
1017 "%d," /* PID */
1018 "%d," /* NodeId */
1019 "%s," /* Hostname */
1020 "%s," /* CpuSet */
1021 #define PRINT_FMT(var_name, c_type, json_name, fmt) \
1022 fmt ","
1023 #define PRINT_FMT_LAST(var_name, c_type, json_name, fmt) \
1024 fmt "\n"
1025 FOR_DLB_MONITOR_PRINTABLE_FIELDS(PRINT_FMT, PRINT_FMT_LAST)
1026 #undef PRINT_FMT
1027 #undef PRINT_FMT_LAST
1028 1 , region_record->name
1029 , process_record->rank
1030 , process_record->pid
1031 , process_record->node_id
1032 1 , process_record->hostname
1033 1 , process_record->cpuset_quoted
1034 #define PRINT_ARG(var_name, c_type, json_name, fmt) \
1035 , process_record->monitor.var_name
1036 FOR_DLB_MONITOR_PRINTABLE_FIELDS(PRINT_ARG, PRINT_ARG)
1037 #undef PRINT_ARG
1038 );
1039 }
1040 }
1041 }
1042
1043 3 static void process_to_txt(FILE *out_file) {
1044
1045 3 for (GSList *node = region_records;
1046
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 node != NULL;
1047 3 node = node->next) {
1048
1049 3 region_record_t *region_record = node->data;
1050
1051
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 for (int i = 0; i < region_record->num_mpi_ranks; ++i) {
1052
1053 3 process_record_t *process_record = &region_record->process_records[i];
1054
1055 6 float ipc = process_record->monitor.cycles > 0
1056 ✗ ? (float)process_record->monitor.instructions
1057 ✗ / process_record->monitor.cycles
1058
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 : 0.0f;
1059
1060 3 fprintf(out_file,
1061 "%s\n"
1062 "### Name: %s\n"
1063 "### Process: %d (%s)\n"
1064 "### Rank: %d\n"
1065 "### CpuSet: %s\n"
1066 "### Elapsed time: %"PRId64" ns\n"
1067 "### Useful time: %"PRId64" ns\n"
1068 "### Not useful MPI: %"PRId64" ns\n"
1069 "### Not useful MPI in worker threads: %"PRId64" ns\n"
1070 "### Not useful OMP Load Imbalance: %"PRId64" ns\n"
1071 "### Not useful OMP Scheduling: %"PRId64" ns\n"
1072 "### Not useful OMP outside parallel: %"PRId64" ns\n"
1073 "### Not useful GPU runtime: %"PRId64" ns\n"
1074 "### Device useful time: %"PRId64" ns\n"
1075 "### Device communication time: %"PRId64" ns\n"
1076 "### IPC: %.2f\n",
1077 make_header("Monitoring Region Summary"),
1078 3 region_record->name,
1079 3 process_record->pid, process_record->hostname,
1080 process_record->rank,
1081 3 process_record->cpuset,
1082 process_record->monitor.elapsed_time,
1083 process_record->monitor.useful_time,
1084 process_record->monitor.mpi_time,
1085 process_record->monitor.mpi_worker_idle_time,
1086 process_record->monitor.omp_load_imbalance_time,
1087 process_record->monitor.omp_scheduling_time,
1088 process_record->monitor.omp_outside_parallel_time,
1089 process_record->monitor.gpu_runtime_time,
1090 process_record->monitor.gpu_useful_time,
1091 process_record->monitor.gpu_communication_time,
1092 ipc);
1093 }
1094 }
1095 3 }
1096
1097 41 static void process_finalize(void) {
1098
1099 /* Free every record data */
1100 41 for (GSList *node = region_records;
1101
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 41 times.
56 node != NULL;
1102 15 node = node->next) {
1103
1104 15 region_record_t *record = node->data;
1105 15 free(record);
1106 }
1107
1108 /* Free list */
1109 41 g_slist_free(region_records);
1110 41 region_records = NULL;
1111 41 }
1112
1113
1114 /*********************************************************************************/
1115 /* TALP Common */
1116 /*********************************************************************************/
1117 typedef struct TALPCommonRecord {
1118 char *dlb_version; // Version X.Y.Z[-#-hash]
1119 char *time_of_creation; // ISO 8601 string
1120 } talp_common_record_t;
1121 static talp_common_record_t common_record;
1122
1123 static bool use_fixed_metadata = false;
1124
1125 41 static void talp_output_record_common(void) {
1126 /* Initialize structure */
1127
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 1 times.
41 if (!use_fixed_metadata) {
1128 40 time_t now = time(NULL);
1129 40 common_record = (const talp_common_record_t) {
1130 .dlb_version = PACKAGE_VERSION,
1131 40 .time_of_creation = get_iso_8601_string(localtime(&now)),
1132 };
1133 } else {
1134 1 common_record = (const talp_common_record_t) {
1135 .dlb_version = "<X.Y.Z>",
1136 .time_of_creation = "2000-01-01T00:00:00",
1137 };
1138 }
1139 41 }
1140
1141 7 static void common_to_json(FILE *out_file) {
1142 7 fprintf(out_file,
1143 " \"dlbVersion\": \"%s\",\n"
1144 " \"jsonSchema\": \"%d\",\n"
1145 " \"dlbMonitorVersion\": \"%d\",\n"
1146 " \"dlbPOPMetricsVersion\": \"%d\",\n"
1147 " \"timestamp\": \"%s\",\n",
1148 common_record.dlb_version,
1149 (int)TALP_OUTPUT_JSON_SCHEMA_VERSION,
1150 (int)DLB_MONITOR_T_VERSION,
1151 (int)DLB_POP_METRICS_T_VERSION,
1152 common_record.time_of_creation);
1153 7 }
1154
1155 3 static void common_to_txt(FILE *out_file) {
1156
1157 3 fprintf(out_file,
1158 "%s\n"
1159 "### DLB Version: %s\n"
1160 "### Timestamp: %s\n",
1161 make_header("TALP Common Data"),
1162 common_record.dlb_version,
1163 common_record.time_of_creation);
1164 3 }
1165
1166 41 static void common_finalize(void) {
1167
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 1 times.
41 if (!use_fixed_metadata) {
1168 40 free(common_record.time_of_creation);
1169 }
1170 41 }
1171
1172
1173
1174
1175 /*********************************************************************************/
1176 /* TALP Resources */
1177 /*********************************************************************************/
1178 typedef struct TALPResourcesRecord {
1179 unsigned int num_cpus;
1180 unsigned int num_available_cpus;
1181 unsigned int num_nodes;
1182 unsigned int num_mpi_ranks;
1183 unsigned int num_gpus;
1184 } talp_resources_record_t;
1185 static talp_resources_record_t resources_record;
1186
1187 23 void talp_output_record_resources(int num_cpus, int num_available_cpus, int num_nodes,
1188 int num_mpi_ranks, int num_gpus) {
1189
1190 23 resources_record = (const talp_resources_record_t) {
1191 23 .num_cpus = (unsigned int) num_cpus,
1192 23 .num_available_cpus = (unsigned int) num_available_cpus,
1193 23 .num_nodes = (unsigned int) num_nodes,
1194 23 .num_mpi_ranks = (unsigned int) num_mpi_ranks,
1195 23 .num_gpus = (unsigned int) num_gpus,
1196 };
1197 23 }
1198
1199 7 static void resources_to_json(FILE *out_file) {
1200 7 fprintf(out_file,
1201 " \"resources\": {\n"
1202 " \"numCpus\": %u,\n"
1203 " \"numAvailableCpus\": %u,\n"
1204 " \"numNodes\": %u,\n"
1205 " \"numMpiRanks\": %u,\n"
1206 " \"numGpus\": %u\n"
1207 " },\n",
1208 resources_record.num_cpus,
1209 resources_record.num_available_cpus,
1210 resources_record.num_nodes,
1211 resources_record.num_mpi_ranks,
1212 resources_record.num_gpus);
1213 7 }
1214
1215 3 static void resources_to_txt(FILE *out_file) {
1216
1217 3 fprintf(out_file,
1218 "%s\n"
1219 "### Number of CPUs: %u\n"
1220 "### Number of available CPUs: %u\n"
1221 "### Number of Nodes: %u\n"
1222 "### Number of MPI processes: %u\n"
1223 "### Number of GPUs: %u\n",
1224 make_header("TALP Resources"),
1225 resources_record.num_cpus,
1226 resources_record.num_available_cpus,
1227 resources_record.num_nodes,
1228 resources_record.num_mpi_ranks,
1229 resources_record.num_gpus);
1230 3 }
1231
1232
1233 /*********************************************************************************/
1234 /* TALP Process info */
1235 /*********************************************************************************/
1236
1237 typedef struct talp_process_info_record_t {
1238 char hostname[HOST_NAME_MAX];
1239 pid_t pid;
1240 } talp_process_info_record_t;
1241
1242 static talp_process_info_record_t process_info_record = {0};
1243
1244 233 void talp_output_record_process_info(void) {
1245
1246 233 gethostname(process_info_record.hostname, HOST_NAME_MAX);
1247 233 process_info_record.pid = getpid();
1248 233 }
1249
1250 7 static void process_info_to_json(FILE *out_file) {
1251
1252
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2 times.
7 if (process_info_record.pid != 0) {
1253 5 fprintf(out_file,
1254 " \"processInfo\": {\n"
1255 " \"hostname\": \"%s\",\n"
1256 " \"pid\": %d\n"
1257 " },\n",
1258 process_info_record.hostname,
1259 process_info_record.pid);
1260 }
1261 7 }
1262
1263 /*********************************************************************************/
1264 /* Helper functions */
1265 /*********************************************************************************/
1266
1267 7 static void json_header(FILE *out_file) {
1268 7 fprintf(out_file, "{\n");
1269 7 }
1270
1271 7 static void json_footer(FILE *out_file) {
1272 7 fprintf(out_file, "\n}\n");
1273 7 }
1274
1275
1276 /*********************************************************************************/
1277 /* Output directory/file logic */
1278 /*********************************************************************************/
1279
1280 // Helper: recursively create directories (mkdir -p equivalent)
1281 11 static int mkdir_p(const char *path, mode_t mode) {
1282 char tmp[PATH_MAX];
1283 11 char *p = NULL;
1284
1285 11 snprintf(tmp, sizeof(tmp), "%s", path);
1286 11 size_t len = strlen(tmp);
1287
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (len == 0) return -1;
1288
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (tmp[len - 1] == '/') {
1289 ✗ tmp[len - 1] = '\0';
1290 }
1291
1292
2/2
✓ Branch 0 taken 203 times.
✓ Branch 1 taken 11 times.
214 for (p = tmp + 1; *p; p++) {
1293
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 192 times.
203 if (*p == '/') {
1294 11 *p = '\0';
1295
2/4
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
11 if (mkdir(tmp, mode) != 0 && errno != EEXIST) {
1296 ✗ return -1;
1297 }
1298 11 *p = '/';
1299 }
1300 }
1301
1302
4/4
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 9 times.
11 if (mkdir(tmp, mode) != 0 && errno != EEXIST) {
1303 1 return -1;
1304 }
1305
1306 10 return 0;
1307 }
1308
1309 // open file for appending or writing, creating dirs as needed
1310 16 static FILE *open_file_with_dirs(const char *filename, bool *append) {
1311
2/2
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 11 times.
16 if (access(filename, F_OK) == 0) {
1312 FILE *f;
1313
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
5 if (append) {
1314 2 *append = true;
1315 2 f = fopen(filename, "a");
1316 } else {
1317 3 f = fopen(filename, "w");
1318 }
1319
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4 times.
5 if (!f) {
1320 1 warning("Cannot open existing file %s: %s", filename, strerror(errno));
1321 }
1322 5 return f;
1323 } else {
1324 // Ensure parent directories exist
1325 char pathbuf[PATH_MAX];
1326 11 snprintf(pathbuf, sizeof(pathbuf), "%s", filename);
1327 11 char *dir = dirname(pathbuf);
1328
1329
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 10 times.
11 if (mkdir_p(dir, 0755) != 0) {
1330 1 warning("Cannot create directory %s: %s", dir, strerror(errno));
1331 1 return NULL;
1332 }
1333
1334
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 6 times.
10 if (append) {
1335 4 *append = false;
1336 }
1337 10 FILE *f = fopen(filename, "w");
1338
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!f) {
1339 ✗ warning("Cannot create file %s: %s", filename, strerror(errno));
1340 }
1341 10 return f;
1342 }
1343 }
1344
1345 1 static char* find_old_schema_name(const char *filename) {
1346
1347 1 size_t orig_len = strlen(filename);
1348
1349 // We should have already checked this, but just in case
1350
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (orig_len < 4 || strncmp(&filename[orig_len - 4], ".csv", 4) != 0) {
1351 ✗ return NULL;
1352 }
1353
1354 // Allocate enough for appending up to the following string:
1355 1 size_t max_extra_len = strlen(".old-schema.99");
1356 1 size_t len =
1357 1 orig_len +
1358 max_extra_len +
1359 1;
1360
1361 1 char *out = malloc(len);
1362
1363
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 for (int i = 1; i < 99; ++i) {
1364 1 snprintf(out, len,
1365 "%.*s.old-schema.%d.csv",
1366 1 (int)orig_len - 4,
1367 filename,
1368 i);
1369
1370
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (access(out, F_OK) != 0) {
1371 1 return out;
1372 }
1373 }
1374
1375 ✗ free(out);
1376 ✗ return NULL;
1377 }
1378
1379 6 static void ensure_csv_schema(const char *filename, const char *schema) {
1380
1381
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
8 if (access(filename, F_OK) != 0) return;
1382
1383 3 FILE *f = fopen(filename, "r");
1384
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!f) {
1385 ✗ warning("Cannot open %s: %s", filename, strerror(errno));
1386 ✗ return;
1387 }
1388
1389 // max header is about 800 characters at the time of writing
1390 char header[2048];
1391
1392 // read first line
1393
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (!fgets(header, sizeof(header), f)) {
1394 ✗ if (ferror(f)) {
1395 ✗ warning("Cannot read %s: %s",
1396 ✗ filename, strerror(errno));
1397 ✗ fclose(f);
1398 ✗ return;
1399 }
1400
1401 // Empty file: consider it an old/incompatible schema.
1402 ✗ header[0] = '\0';
1403 }
1404
1405 3 fclose(f);
1406
1407 // Remove CR/LF
1408 3 size_t len = strlen(header);
1409 3 while(len > 0
1410
3/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 3 times.
6 && (header[len - 1] == '\n'
1411
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 || header[len - 1] == '\r')) {
1412 3 header[--len] = '\0';
1413 }
1414
1415
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (strcmp(header, schema) == 0) {
1416 // same schema
1417 2 return;
1418 }
1419
1420 1 char *old_schema_filename = find_old_schema_name(filename);
1421
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (old_schema_filename == NULL) {
1422 ✗ warning("Could not find a suitable old-schema name for %s\n", filename);
1423 ✗ return;
1424 }
1425
1426
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (rename(filename, old_schema_filename) != 0) {
1427 ✗ warning("Cannot move %s to %s: %s", filename, old_schema_filename, strerror(errno));
1428 }
1429
1430 1 warning("The CSV schema changed; the existing data was preserved as %s."
1431 " New data is being written to %s.\n",
1432 old_schema_filename, filename);
1433
1434 1 free(old_schema_filename);
1435 }
1436
1437 5 static inline void ensure_pop_metrics_csv_schema(const char *filename) {
1438
1439
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (pop_metrics_records != NULL) {
1440 5 ensure_csv_schema(filename, pop_metrics_csv_schema);
1441 }
1442 5 }
1443
1444 5 static inline void ensure_process_csv_schema(const char *filename) {
1445
1446
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4 times.
5 if (region_records != NULL) {
1447 1 ensure_csv_schema(filename, process_csv_schema);
1448 }
1449 5 }
1450
1451 /*********************************************************************************/
1452 /* Finalize */
1453 /*********************************************************************************/
1454
1455 static int warn_count = 0;
1456 enum { MAX_DETAILED_WARNINGS = 10 };
1457
1458 528 static void sanitize_counter(const char *value_name, double *value, const char *region_name) {
1459
1460
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 526 times.
528 if (*value < 0) {
1461
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (warn_count < MAX_DETAILED_WARNINGS) {
1462 2 warning("Region '%s': counter '%s' had value '%f' (set to 0)",
1463 region_name, value_name, *value);
1464 ✗ } else if (warn_count == MAX_DETAILED_WARNINGS) {
1465 ✗ warning("Further coefficient warnings suppressed (already reported %d).",
1466 warn_count);
1467 }
1468 2 ++warn_count;
1469 2 *value = 0.0;
1470 }
1471
1472 528 }
1473
1474 3735 static void sanitize_coefficient(const char *value_name, float *value, const char *region_name) {
1475
1476
4/4
✓ Branch 0 taken 3734 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 3725 times.
3735 if (*value < -FLT_EPSILON || *value > 1.0f + FLT_EPSILON) {
1477
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 if (warn_count < MAX_DETAILED_WARNINGS) {
1478 8 warning("Region '%s': metric '%s' = %f is outside the allowed range"
1479 " [0.0, 1.0]. If you think this is unexpected, please report"
1480 8 " it to %s", region_name, value_name, *value, PACKAGE_BUGREPORT);
1481
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 } else if (warn_count == MAX_DETAILED_WARNINGS) {
1482 1 warning("Further coefficient warnings suppressed (already reported %d).",
1483 warn_count);
1484 }
1485 10 ++warn_count;
1486 }
1487 3735 }
1488
1489 41 static void sanitize_records(void) {
1490
1491 typedef struct {
1492 const char *name;
1493 size_t offset;
1494 } field_t;
1495
1496 /* pop_metrics_records:
1497 * - instructions and cycles need to be >= 0
1498 * - computed efficiencies need to be [0.0, 1.0]
1499 */
1500
1501 41 const field_t counters_fields[] = {
1502 { "cycles", offsetof(dlb_pop_metrics_t, cycles) },
1503 { "instructions", offsetof(dlb_pop_metrics_t, instructions) },
1504 };
1505 enum { COUNTERS_FIELDS_SIZE = sizeof(counters_fields)/sizeof(counters_fields[0]) };
1506
1507 41 const field_t coeff_fields[] = {
1508 { "parallel_efficiency", offsetof(dlb_pop_metrics_t, parallel_efficiency) },
1509 { "mpi_parallel_efficiency", offsetof(dlb_pop_metrics_t, mpi_parallel_efficiency) },
1510 { "mpi_communication_efficiency", offsetof(dlb_pop_metrics_t, mpi_communication_efficiency) },
1511 { "mpi_load_balance", offsetof(dlb_pop_metrics_t, mpi_load_balance) },
1512 { "mpi_load_balance_in", offsetof(dlb_pop_metrics_t, mpi_load_balance_in) },
1513 { "mpi_load_balance_out", offsetof(dlb_pop_metrics_t, mpi_load_balance_out) },
1514 { "omp_parallel_efficiency", offsetof(dlb_pop_metrics_t, omp_parallel_efficiency) },
1515 { "omp_load_balance", offsetof(dlb_pop_metrics_t, omp_load_balance) },
1516 { "omp_scheduling_efficiency", offsetof(dlb_pop_metrics_t, omp_scheduling_efficiency) },
1517 { "omp_coverage_efficiency", offsetof(dlb_pop_metrics_t, omp_coverage_efficiency) },
1518 { "device_offload_efficiency", offsetof(dlb_pop_metrics_t, device_offload_efficiency) },
1519 { "gpu_parallel_efficiency", offsetof(dlb_pop_metrics_t, gpu_parallel_efficiency) },
1520 { "gpu_load_balance", offsetof(dlb_pop_metrics_t, gpu_load_balance) },
1521 { "gpu_communication_efficiency", offsetof(dlb_pop_metrics_t, gpu_communication_efficiency) },
1522 { "gpu_orchestration_efficiency", offsetof(dlb_pop_metrics_t, gpu_orchestration_efficiency) },
1523 };
1524 enum { COEFF_FIELDS_SIZE = sizeof(coeff_fields)/sizeof(coeff_fields[0]) };
1525
1526 41 for (GSList *node = pop_metrics_records;
1527
2/2
✓ Branch 0 taken 249 times.
✓ Branch 1 taken 41 times.
290 node != NULL;
1528 249 node = node->next) {
1529
1530 249 dlb_pop_metrics_t *record = node->data;
1531
1532
2/2
✓ Branch 0 taken 498 times.
✓ Branch 1 taken 249 times.
747 for (size_t i = 0; i < COUNTERS_FIELDS_SIZE; ++i) {
1533 498 double *value = (double *)((char *)record + counters_fields[i].offset);
1534 498 sanitize_counter(counters_fields[i].name, value, record->name);
1535 }
1536
1537
2/2
✓ Branch 0 taken 3735 times.
✓ Branch 1 taken 249 times.
3984 for (size_t i = 0; i < COEFF_FIELDS_SIZE; ++i) {
1538 3735 float *value = (float *)((char *)record + coeff_fields[i].offset);
1539 3735 sanitize_coefficient(coeff_fields[i].name, value, record->name);
1540 }
1541 }
1542
1543 /* node_records: nothing to sanitize for now */
1544
1545 /* region_records: */
1546
1547 41 const field_t monitor_counters_fields[] = {
1548 { "cycles", offsetof(dlb_monitor_t, cycles) },
1549 { "instructions", offsetof(dlb_monitor_t, instructions) },
1550 };
1551 enum { MONITOR_COUNTERS_FIELDS_SIZE =
1552 sizeof(monitor_counters_fields)/sizeof(monitor_counters_fields[0]) };
1553
1554 41 for (GSList *node = region_records;
1555
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 41 times.
56 node != NULL;
1556 15 node = node->next) {
1557
1558 15 region_record_t *region_record = node->data;
1559
1560
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
30 for (int i = 0; i < region_record->num_mpi_ranks; ++i) {
1561
1562 15 dlb_monitor_t *monitor = &region_record->process_records[i].monitor;
1563
1564
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 15 times.
45 for (size_t j = 0; j < MONITOR_COUNTERS_FIELDS_SIZE; ++j) {
1565 30 double *value = (double *)((char *)monitor + monitor_counters_fields[j].offset);
1566 30 sanitize_counter(counters_fields[i].name, value, monitor->name);
1567 }
1568 }
1569 }
1570 41 }
1571
1572 /* Return an allocated string: base + "_%h_%p.partial" + extension */
1573 1 static char *build_partial_template(const char *filename) {
1574
1575 1 const char *dot = strrchr(filename, '.');
1576 1 size_t base_len = dot - filename;
1577 1 const char *ext = dot;
1578
1579
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 ensure(dot != NULL, "expected filename with extension %s. Please report bug.", __func__);
1580
1581 1 size_t len =
1582 base_len +
1583 1 strlen("_%h_%p.partial") +
1584 1 strlen(ext) +
1585 1;
1586
1587 1 char *out = malloc(len);
1588
1589 1 snprintf(out, len,
1590 "%.*s_%%h_%%p.partial%s",
1591 (int)base_len,
1592 filename,
1593 ext);
1594
1595 1 return out;
1596 }
1597
1598 /* Detect job ID across schedulers */
1599 2 static const char *get_job_id(void) {
1600
1601 2 const char *vars[] = {
1602 "SLURM_JOB_ID",
1603 "SLURM_JOBID",
1604 "FLUX_JOB_ID",
1605 "PBS_JOBID",
1606 "LSB_JOBID",
1607 "JOB_ID",
1608 NULL
1609 };
1610
1611
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 2 times.
14 for (int i = 0; vars[i]; i++) {
1612 12 const char *v = getenv(vars[i]);
1613
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
12 if (v && *v)
1614 ✗ return v;
1615 }
1616
1617 2 return NULL;
1618 }
1619
1620 /* Expands output file, e.g.: talp_%p.json -> talp_123.json */
1621 15 static char *expand_output_filename(const char *template)
1622 {
1623
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 2 times.
15 if (strchr(template, '%') == NULL) return NULL;
1624
1625 char hostname[HOST_NAME_MAX];
1626 2 gethostname(hostname, HOST_NAME_MAX);
1627
1628 2 pid_t pid = getpid();
1629 char pid_buf[32];
1630 2 snprintf(pid_buf, sizeof(pid_buf), "%d", pid);
1631
1632 2 const char *job = get_job_id();
1633
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 const char *job_str = job ? job : "0";
1634
1635 2 size_t hst_len = strlen(hostname);
1636 2 size_t pid_len = strlen(pid_buf);
1637 2 size_t job_len = strlen(job_str);
1638
1639 /* Compute output length */
1640
1641 2 size_t out_len = 0;
1642
1643
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 2 times.
78 for (size_t i = 0; template[i]; i++) {
1644
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 72 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
76 if (template[i] == '%' && template[i+1]) {
1645
2/5
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
4 switch (template[i+1]) {
1646 2 case 'h':
1647 2 out_len += hst_len;
1648 2 i++;
1649 2 continue;
1650 2 case 'p':
1651 2 out_len += pid_len;
1652 2 i++;
1653 2 continue;
1654 ✗ case 'j':
1655 ✗ out_len += job_len;
1656 ✗ i++;
1657 ✗ continue;
1658 ✗ case '%':
1659 ✗ out_len += 1;
1660 ✗ i++;
1661 ✗ continue;
1662 }
1663 }
1664 72 out_len += 1;
1665 }
1666
1667
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (out_len > PATH_MAX) return NULL;
1668
1669 /* Fill buffer */
1670
1671 2 char *output_filename = malloc(out_len + 1);
1672
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!output_filename) return NULL;
1673
1674 2 char *out = output_filename;
1675
1676
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 2 times.
78 for (size_t i = 0; template[i]; ++i) {
1677
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 72 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
76 if (template[i] == '%' && template[i+1]) {
1678
2/5
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
4 switch (template[i+1]) {
1679 2 case 'h':
1680 2 memcpy(out, hostname, hst_len);
1681 2 out += hst_len;
1682 2 ++i;
1683 2 continue;
1684
1685 2 case 'p':
1686 2 memcpy(out, pid_buf, pid_len);
1687 2 out += pid_len;
1688 2 ++i;
1689 2 continue;
1690
1691 ✗ case 'j':
1692 ✗ memcpy(out, job_str, job_len);
1693 ✗ out += job_len;
1694 ✗ ++i;
1695 ✗ continue;
1696
1697 ✗ case '%':
1698 ✗ *out++ = '%';
1699 ✗ ++i;
1700 ✗ continue;
1701 }
1702 }
1703
1704 72 *out++ = template[i];
1705 }
1706
1707 2 *out = '\0';
1708
1709 2 return output_filename;
1710 }
1711
1712 56 void talp_output_finalize(const char *output_file, bool partial_output) {
1713
1714 /* Skip output if process has no data */
1715
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 41 times.
56 if (pop_metrics_records == NULL
1716
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 && node_records == NULL
1717
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 && region_records == NULL) return;
1718
1719 /* For efficiency when adding records, they are prepended to their respective lists.
1720 * Then, they are reversed here to print them in alphabetical order. */
1721 41 pop_metrics_records = g_slist_reverse(pop_metrics_records);
1722 41 node_records = g_slist_reverse(node_records);
1723 41 region_records = g_slist_reverse(region_records);
1724
1725 /* Sanitize erroneous values */
1726 41 sanitize_records();
1727
1728 41 talp_output_record_common();
1729
1730 /* If the process has changed the locale, temporarily push the C locale to
1731 * print floats with the expected notation (a comma as a decimal separator
1732 * will break CSV and JSON files). The object associated with the locale
1733 * can be safely freed after it has been set. */
1734 41 locale_t new_locale = newlocale(LC_ALL, "C", 0);
1735 41 uselocale(new_locale);
1736 41 freelocale(new_locale);
1737
1738
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 15 times.
41 if (output_file == NULL) {
1739 /* No output file, just print all records */
1740 26 pop_metrics_print();
1741 26 node_print();
1742 26 process_print();
1743 } else {
1744 /* Check file extension */
1745 typedef enum Extension {
1746 EXT_JSON,
1747 EXT_XML,
1748 EXT_CSV,
1749 EXT_TXT,
1750 } extension_t;
1751 15 extension_t extension = EXT_TXT;
1752 15 const char *ext = strrchr(output_file, '.');
1753
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 1 times.
15 if (ext != NULL) {
1754
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
14 if (strcmp(ext+1, "json") == 0) {
1755 7 extension = EXT_JSON;
1756
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 } else if (strcmp(ext+1, "xml") == 0) {
1757 ✗ extension = EXT_XML;
1758
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2 times.
7 } else if (strcmp(ext+1, "csv") == 0) {
1759 5 extension = EXT_CSV;
1760 }
1761 }
1762
1763 /* Deprecation warning*/
1764
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if(extension == EXT_XML){
1765 ✗ warning("Deprecated: The support for XML output is deprecated.");
1766 }
1767
1768 /* Flag check */
1769
3/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
15 if (extension != EXT_JSON && partial_output) {
1770 ✗ warning("Option --talp-partial-output is only supported for JSON format."
1771 " Disabling option");
1772 ✗ partial_output = false;
1773 }
1774
1775 /* Obtain the real filename depending on whether output_file is a template,
1776 * or the user requested partial output */
1777 15 char *template = NULL;
1778 15 char *filename = NULL;
1779
1780
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 14 times.
15 if (partial_output) {
1781 1 template = build_partial_template(output_file);
1782 } else {
1783 14 template = strdup(output_file);
1784 }
1785
1786 15 filename = expand_output_filename(template);
1787
1788
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 13 times.
15 if (filename != NULL) {
1789 2 output_file = filename;
1790 }
1791
1792 15 free(template);
1793
1794 /* Specific case where output file needs to be split */
1795
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 10 times.
15 if (extension == EXT_CSV
1796 5 && !!(pop_metrics_records != NULL)
1797 5 + !!(node_records != NULL)
1798
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4 times.
6 + !!(region_records != NULL) > 1) {
1799
1800 /* Length without extension */
1801 1 int filename_useful_len = ext - output_file;
1802
1803 /* POP */
1804
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (pop_metrics_records != NULL) {
1805 1 const char *pop_ext = "-pop.csv";
1806 1 size_t pop_file_len = filename_useful_len + strlen(pop_ext) + 1;
1807 1 char *pop_filename = malloc(sizeof(char)*pop_file_len);
1808 1 sprintf(pop_filename, "%.*s%s", filename_useful_len, output_file, pop_ext);
1809 1 ensure_pop_metrics_csv_schema(pop_filename);
1810 bool append_to_csv;
1811 1 FILE *pop_file = open_file_with_dirs(pop_filename, &append_to_csv);
1812
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (pop_file) {
1813 1 pop_metrics_to_csv(pop_file, append_to_csv);
1814 1 fclose(pop_file);
1815 } else {
1816 ✗ warning("Writing metrics to stdout instead:");
1817 ✗ pop_metrics_to_csv(stdout, /* append: */ false);
1818 }
1819 }
1820
1821 /* Node */
1822
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (node_records != NULL) {
1823 ✗ const char *node_ext = "-node.csv";
1824 ✗ size_t node_file_len = filename_useful_len + strlen(node_ext) + 1;
1825 ✗ char *node_filename = malloc(sizeof(char)*node_file_len);
1826 ✗ sprintf(node_filename, "%.*s%s", filename_useful_len, output_file, node_ext);
1827 bool append_to_csv;
1828 ✗ FILE *node_file = open_file_with_dirs(node_filename, &append_to_csv);
1829 ✗ if (node_file) {
1830 ✗ node_to_csv(node_file, append_to_csv);
1831 ✗ fclose(node_file);
1832 } else {
1833 ✗ warning("Writing metrics to stdout instead:");
1834 ✗ node_to_csv(stdout, /* append: */ false);
1835 }
1836 }
1837
1838 /* Process */
1839
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (region_records != NULL) {
1840 1 const char *process_ext = "-process.csv";
1841 1 size_t process_file_len = filename_useful_len + strlen(process_ext) + 1;
1842 1 char *process_filename = malloc(sizeof(char)*process_file_len);
1843 1 sprintf(process_filename, "%.*s%s", filename_useful_len, output_file, process_ext);
1844 1 ensure_process_csv_schema(process_filename);
1845 bool append_to_csv;
1846 1 FILE *process_file = open_file_with_dirs(process_filename, &append_to_csv);
1847
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (process_file) {
1848 1 process_to_csv(process_file, append_to_csv);
1849 1 fclose(process_file);
1850 } else {
1851 ✗ warning("Writing metrics to stdout instead:");
1852 ✗ process_to_csv(stdout, /* append: */ false);
1853 }
1854 }
1855 }
1856
1857 /* Write to file */
1858 else {
1859 /* If CSV, ensure that if the file is to be appended, belongs
1860 * to the current schema. */
1861
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 10 times.
14 if (extension == EXT_CSV) {
1862 4 ensure_pop_metrics_csv_schema(output_file);
1863 4 ensure_process_csv_schema(output_file);
1864 }
1865
1866 /* Open file */
1867 bool append_to_csv;
1868
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 10 times.
14 FILE *out_file = open_file_with_dirs(output_file,
1869 extension == EXT_CSV ? &append_to_csv : NULL);
1870
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 12 times.
14 if (!out_file) {
1871 2 warning("Writing metrics to stdout instead:");
1872 2 out_file = stdout;
1873 2 append_to_csv = false;
1874 }
1875
1876 /* Write records to file */
1877
3/4
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
14 switch(extension) {
1878 7 case EXT_JSON:
1879 7 json_header(out_file);
1880 7 common_to_json(out_file);
1881 7 resources_to_json(out_file);
1882 7 process_info_to_json(out_file);
1883 7 pop_metrics_to_json(out_file);
1884 7 node_to_json(out_file);
1885 7 process_to_json(out_file);
1886 7 json_footer(out_file);
1887 7 break;
1888 4 case EXT_CSV:
1889 4 pop_metrics_to_csv(out_file, append_to_csv);
1890 4 node_to_csv(out_file, append_to_csv);
1891 4 process_to_csv(out_file, append_to_csv);
1892 4 break;
1893 3 case EXT_XML:
1894 case EXT_TXT:
1895 3 common_to_txt(out_file);
1896 3 resources_to_txt(out_file);
1897 3 pop_metrics_to_txt(out_file);
1898 3 node_to_txt(out_file);
1899 3 process_to_txt(out_file);
1900 3 break;
1901 }
1902
1903 /* Close file */
1904
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 2 times.
14 if (out_file != stdout) {
1905 12 fclose(out_file);
1906 }
1907 }
1908
1909
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 13 times.
15 if (filename != NULL) {
1910 2 free(filename);
1911 2 filename = NULL;
1912 }
1913 }
1914
1915 // Restore locale
1916 41 uselocale(LC_GLOBAL_LOCALE);
1917
1918 // De-allocate all records
1919 41 common_finalize();
1920 41 pop_metrics_finalize();
1921 41 node_finalize();
1922 41 process_finalize();
1923 }
1924
1925 /* Testing */
1926
1927 1 void talp_output_testing_use_fixed_metadata(bool fixed) {
1928 1 use_fixed_metadata = fixed;
1929 1 }
1930