GCC Code Coverage Report


Directory: ./
File: tests/unit/00_support/talp_output_00.c
Date: 2026-12-01 10:07:21
Exec Total Coverage
Lines: 144 151 95.4%
Functions: 8 8 100.0%
Branches: 33 60 55.0%

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 #include "tmpdir.h"
21
22 #include "apis/dlb_talp.h"
23 #include "support/mask_utils.h"
24 #include "talp/talp_output.h"
25 #include "talp/talp_types.h"
26
27 #include <ftw.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <limits.h>
33 #include <assert.h>
34
35
36 8 static int count_lines(const char *filename) {
37 8 int lines = 0;
38 8 FILE *file = fopen(filename, "r");
39
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (file != NULL) {
40 int c;
41
2/2
✓ Branch 2 taken 10268 times.
✓ Branch 3 taken 8 times.
10276 for (c = getc(file); c != EOF; c = getc(file)) {
42
2/2
✓ Branch 0 taken 228 times.
✓ Branch 1 taken 10040 times.
10268 if (c == '\n') {
43 228 ++lines;
44 }
45 }
46 8 fclose(file);
47 }
48 8 return lines;
49 }
50
51 10 static void cat_file(const char *filename) {
52 10 FILE *f = fopen(filename, "r");
53
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!f) {
54 ✗ perror("fopen");
55 ✗ return;
56 }
57
58 10 fprintf(stderr, "--- File %s\n", filename);
59
60 char buffer[4096];
61 size_t n;
62
2/2
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 10 times.
20 while ((n = fread(buffer, 1, sizeof(buffer), f)) > 0) {
63 // print to stderr so that the output is not cluttered with dlb messages
64
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 if (fwrite(buffer, 1, n, stderr) != n) {
65 ✗ perror("fwrite");
66 ✗ break;
67 }
68 }
69
70
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 if (ferror(f)) {
71 ✗ perror("fread");
72 }
73
74 10 fprintf(stderr, "---\n");
75 10 fclose(f);
76 }
77
78 1 static void write_to_file(const char *filename, const char *contents) {
79 1 FILE *f = fopen(filename, "w");
80
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!f) {
81 ✗ perror("fopen");
82 ✗ return;
83 }
84
85 1 size_t len = strlen(contents);
86 1 fwrite(contents, 1, len, f);
87 1 fclose(f);
88 }
89
90 15 static void record_pop_metrics(void) {
91
92 15 dlb_pop_metrics_t metrics = {
93 .name = "Region 1",
94 .num_cpus = 1,
95 .num_mpi_ranks = 0,
96 .num_nodes = 1,
97 .avg_cpus = 1.0f,
98 .cycles = 1e9,
99 .instructions = 2e9,
100 .num_mpi_calls = 0,
101 .elapsed_time = 1000000000,
102 .useful_time = 500000000,
103 .mpi_time = 0,
104 .omp_load_imbalance_time = 100000000,
105 .omp_scheduling_time = 100000000,
106 .omp_outside_parallel_time = 300000000,
107 .min_mpi_normd_proc = 1000,
108 .min_mpi_normd_node = 1000,
109 .parallel_efficiency = 0.24f,
110 .mpi_parallel_efficiency = 0.25f,
111 .mpi_communication_efficiency = 1.0f,
112 .mpi_load_balance = 0.25f,
113 .mpi_load_balance_in = 0.5f,
114 .mpi_load_balance_out = 0.5f,
115 .omp_parallel_efficiency = 0.85f,
116 .omp_load_balance = 0.95f,
117 .omp_scheduling_efficiency = 0.95f,
118 .omp_coverage_efficiency = 0.95f,
119 };
120
121 15 talp_output_record_pop_metrics(&metrics);
122 15 }
123
124 11 static void record_process_metrics(void) {
125
126 11 const process_record_t process_record = {
127 .rank = 0,
128 .pid = 111,
129 .hostname = "hostname",
130 .cpuset = "[0-3]",
131 .cpuset_quoted = "\"0-3\"",
132 .monitor = {
133 .num_measurements = 1,
134 .elapsed_time = 100,
135 .useful_time = 100,
136 .mpi_time = 100,
137 },
138 };
139
140 11 talp_output_record_process("Region 1", &process_record, 1);
141 11 }
142
143 11 static void record_metrics(void) {
144
145 11 record_pop_metrics();
146 11 record_process_metrics();
147 11 }
148
149 1 static void record_wrong_metrics(void) {
150
151 /* Initialize structure */
152 1 dlb_pop_metrics_t metrics = {
153 .name = "Region 1",
154 .num_cpus = 1,
155 .num_mpi_ranks = 0,
156 .num_nodes = 1,
157 .avg_cpus = 1.0f,
158 .cycles = -100,
159 .instructions = -200,
160 .parallel_efficiency = 1.24f,
161 .mpi_parallel_efficiency = 1.25f,
162 .mpi_communication_efficiency = -1.0f,
163 .mpi_load_balance = 1.25f,
164 .mpi_load_balance_in = 1.5f,
165 .mpi_load_balance_out = 1.5f,
166 .omp_parallel_efficiency = 1.85f,
167 .omp_load_balance = 1.95f,
168 .omp_scheduling_efficiency = 1.95f,
169 .omp_coverage_efficiency = 1.95f,
170 };
171
172 1 talp_output_record_pop_metrics(&metrics);
173
174 1 const process_record_t process_record = {
175 .rank = 0,
176 .pid = 111,
177 .hostname = "hostname",
178 .cpuset = "[0-3]",
179 .cpuset_quoted = "\"0-3\"",
180 .monitor = {
181 .cycles = -1,
182 .instructions = -2,
183 },
184 };
185
186 1 talp_output_record_process("Region 1", &process_record, 1);
187 1 }
188
189 1 int main(int argc, char *argv[]) {
190
191 1 bool no_partial_output = false;
192 char hostname[HOST_NAME_MAX];
193 1 gethostname(hostname, HOST_NAME_MAX);
194 1 pid_t pid = getpid();
195
196 /* JSON */
197 {
198 char json_filename[PATH_MAX];
199 1 tmpdir_path(json_filename, sizeof(json_filename), "talp.json");
200
201 /* Base */
202 1 record_metrics();
203 1 talp_output_finalize(json_filename, no_partial_output);
204
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( access(json_filename, F_OK) == 0);
205 1 cat_file(json_filename);
206 1 int num_lines_in_json = count_lines(json_filename);
207
208 /* Test that processInfo adds 4 lines */
209 1 record_metrics();
210 1 talp_output_record_process_info();
211 1 talp_output_finalize(json_filename, no_partial_output);
212
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( access(json_filename, F_OK) == 0);
213 1 cat_file(json_filename);
214 1 int num_lines_in_json_w_process_info = count_lines(json_filename);
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( num_lines_in_json + 4 == num_lines_in_json_w_process_info );
216 }
217
218 /* JSON with template*/
219 {
220 char json_template[PATH_MAX];
221 1 snprintf(json_template, sizeof(json_template),
222 "%s/talp_%%h_%%p.json", tmpdir_get());
223
224 char expected_filename[PATH_MAX];
225 1 snprintf(expected_filename, sizeof(expected_filename),
226 "%s/talp_%s_%d.json", tmpdir_get(), hostname, pid);
227
228 1 record_metrics();
229 1 talp_output_finalize(json_template, no_partial_output);
230
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( access(expected_filename, F_OK) == 0);
231 1 cat_file(expected_filename);
232 }
233
234 /* JSON with partial output */
235 {
236 1 bool partial_output = true;
237
238 char json_filename[PATH_MAX];
239 1 snprintf(json_filename, sizeof(json_filename),
240 "%s/talp.json", tmpdir_get());
241
242 char expected_filename[PATH_MAX];
243 1 snprintf(expected_filename, sizeof(expected_filename),
244 "%s/talp_%s_%d.partial.json", tmpdir_get(), hostname, pid);
245
246 1 record_metrics();
247 1 talp_output_finalize(json_filename, partial_output);
248
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( access(expected_filename, F_OK) == 0 );
249 1 cat_file(expected_filename);
250 }
251
252 /* CSV multiple files */
253 {
254 char csv_filename[PATH_MAX];
255 1 snprintf(csv_filename, sizeof(csv_filename), "%s/talp.csv", tmpdir_get());
256 1 unlink(csv_filename);
257
258 char expected_csv1[PATH_MAX], expected_csv2[PATH_MAX];
259 1 snprintf(expected_csv1, sizeof(expected_csv1), "%s/talp-pop.csv", tmpdir_get());
260 1 snprintf(expected_csv2, sizeof(expected_csv2), "%s/talp-process.csv", tmpdir_get());
261
262 1 record_metrics();
263 1 talp_output_finalize(csv_filename, no_partial_output);
264
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( access(csv_filename, F_OK) != 0 );
265
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( access(expected_csv1, F_OK) == 0 );
266
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( access(expected_csv2, F_OK) == 0 );
267
268 1 cat_file(expected_csv1);
269 1 cat_file(expected_csv2);
270 }
271
272 /* CSV single file + append */
273 {
274 char csv_filename[PATH_MAX];
275 1 snprintf(csv_filename, sizeof(csv_filename), "%s/talp.csv", tmpdir_get());
276
277 // first record
278 1 record_pop_metrics();
279 1 talp_output_finalize(csv_filename, no_partial_output);
280
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( access(csv_filename, F_OK) == 0 );
281
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( count_lines(csv_filename) == 2 );
282
283 // second record
284 1 record_pop_metrics();
285 1 talp_output_finalize(csv_filename, no_partial_output);
286
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( access(csv_filename, F_OK) == 0 );
287
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( count_lines(csv_filename) == 3 );
288
289 1 cat_file(csv_filename);
290 }
291
292 /* CSV with an old schema */
293 {
294 char existing_csv[PATH_MAX];
295 1 snprintf(existing_csv, sizeof(existing_csv), "%s/talp.csv", tmpdir_get());
296 1 write_to_file(existing_csv, "old_schema,Header1,Header2\n1,2,3\n");
297
298 char expectef_old_csv[PATH_MAX];
299 1 snprintf(expectef_old_csv, sizeof(expectef_old_csv), "%s/talp.old-schema.1.csv", tmpdir_get());
300
301 // first record
302 1 record_pop_metrics();
303 1 talp_output_finalize(existing_csv, no_partial_output);
304
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( access(existing_csv, F_OK) == 0 );
305
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( count_lines(existing_csv) == 2 );
306
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( access(expectef_old_csv, F_OK) == 0 );
307
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( count_lines(expectef_old_csv) == 2 );
308
309 // second record
310 1 record_pop_metrics();
311 1 talp_output_finalize(existing_csv, no_partial_output);
312
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( access(existing_csv, F_OK) == 0 );
313
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( count_lines(existing_csv) == 3 );
314
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( access(expectef_old_csv, F_OK) == 0 );
315
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( count_lines(expectef_old_csv) == 2 );
316
317 1 cat_file(expectef_old_csv);
318 1 cat_file(existing_csv);
319 }
320
321 /* TXT */
322 {
323 char txt_filename[PATH_MAX];
324 1 snprintf(txt_filename, sizeof(txt_filename), "%s/talp.txt", tmpdir_get());
325
326 1 record_metrics();
327 1 talp_output_finalize(txt_filename, no_partial_output);
328
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( access(txt_filename, F_OK) == 0 );
329 }
330
331 /* No file */
332 {
333 1 record_metrics();
334 1 talp_output_finalize(NULL, no_partial_output);
335 }
336
337 /* Output to /dev/null */
338 {
339 1 record_metrics();
340 1 talp_output_finalize("/dev/null", no_partial_output);
341 }
342
343 /* Output to a directory that it does not exist */
344 {
345 char subdir_filename[PATH_MAX];
346 1 snprintf(subdir_filename, sizeof(subdir_filename), "%s/subdir/talp.json", tmpdir_get());
347
348 1 record_metrics();
349 1 talp_output_finalize(subdir_filename, no_partial_output);
350
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( access(subdir_filename, F_OK) == 0 );
351 1 cat_file(subdir_filename);
352 }
353
354 /* Output to an existing file that cannot be written to
355 * (e.g., a directory or a file without writing permissions) */
356 {
357 1 record_metrics();
358 1 fprintf(stdout, "--- Fallback metrics in txt format:\n");
359 1 talp_output_finalize(tmpdir_get(), no_partial_output);
360 1 fflush(stdout);
361 }
362
363 /* Output to a directory that cannot be created */
364 {
365 char wrong_subdir[PATH_MAX];
366 1 snprintf(wrong_subdir, sizeof(wrong_subdir), "/subdir/talp.json");
367
368 1 record_metrics();
369 1 fprintf(stdout, "--- Fallback metrics in json format:\n");
370 1 talp_output_finalize(wrong_subdir, no_partial_output);
371 1 fflush(stdout);
372 }
373
374 /* Report single region */
375 {
376 1 fprintf(stdout, "--- Single region:\n");
377 1 dlb_monitor_t monitor = {
378 .name = "Region 1",
379 .num_cpus = 1,
380 .cycles = 1e9,
381 .instructions = 2e9,
382 .num_measurements = 1,
383 .num_mpi_calls = 5,
384 .num_omp_parallels = 2,
385 .num_omp_tasks = 7,
386 .num_gpu_runtime_calls = 42,
387 .start_time = 1e9,
388 .stop_time = 2e9,
389 .elapsed_time = 1e9,
390 .useful_time = 4e8,
391 .mpi_time = 2e8,
392 .omp_load_imbalance_time = 1e8,
393 .omp_scheduling_time = 1e8,
394 .omp_outside_parallel_time = 1e8,
395 .gpu_runtime_time = 1e8,
396 .gpu_useful_time = 2e8,
397 .gpu_communication_time = 2e8,
398 1 ._data = calloc(1, sizeof(monitor_data_t)),
399 };
400
401 1 talp_flags_t flags = {
402 .have_mpi = true,
403 .have_openmp = true,
404 .have_gpu = true,
405 .have_hwc = true,
406 };
407 1 talp_output_print_monitoring_region(&monitor, flags);
408 1 free(monitor._data);
409 1 fflush(stdout);
410 }
411
412 /* Sanity checks */
413 {
414 1 fprintf(stdout, "--- Wrong metrics:\n");
415 1 record_wrong_metrics();
416 1 talp_output_finalize(NULL, no_partial_output);
417 1 fflush(stdout);
418 }
419
420 1 return EXIT_SUCCESS;
421 }
422