YAC 3.21.0
Yet Another Coupler
Loading...
Searching...
No Matches
test_yac_pak_tracing.c
Go to the documentation of this file.
1// Copyright (c) 2026 The YAC Authors
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#include <stdlib.h>
6#include <stdio.h>
7#include <string.h>
8#include <unistd.h>
9#include <dirent.h>
10#include <sys/stat.h>
11#include <mpi.h>
12#include "io_utils.h"
13
14#include "tests.h"
15#include "yac_pak.h"
16#include "test_yac_pak_common.h"
17
41static int utest_check_csv_rank_column(char const *path, int expected_rank) {
42 FILE *f = fopen(path, "r");
43 if (!f) return -1;
44 int header_seen = 0;
45 int ok = 1;
46 char line[1024];
47 while (ok && fgets(line, sizeof(line), f)) {
48 if (line[0] == '#' || line[0] == '\n' || line[0] == '\r') continue;
49 if (!header_seen) { header_seen = 1; continue; }
50 char rankbuf[32];
51 int pos = 0;
52 while (line[pos] && line[pos] != '\t' && pos < 31) {
53 rankbuf[pos] = line[pos];
54 pos++;
55 }
56 rankbuf[pos] = '\0';
57 if (atoi(rankbuf) != expected_rank) ok = 0;
58 }
59 fclose(f);
60 return ok ? 0 : -1;
61}
62
63static int utest_file_has_line_prefix(char const *path, char const *prefix) {
64 FILE *f = fopen(path, "r");
65 if (!f) return -1;
66 size_t const plen = strlen(prefix);
67 char line[1024];
68 int found = 0;
69 while (fgets(line, sizeof(line), f)) {
70 if (strncmp(line, prefix, plen) == 0) { found = 1; break; }
71 }
72 fclose(f);
73 return found;
74}
75
76static int utest_csv_has_type(char const *path, char const *type) {
77 FILE *f = fopen(path, "r");
78 if (!f) return -1;
79 int header_seen = 0;
80 char line[2048];
81 int found = 0;
82 size_t const rlen = strlen(type);
83 while (!found && fgets(line, sizeof(line), f)) {
84 if (line[0] == '#' || line[0] == '\n' || line[0] == '\r') continue;
85 if (!header_seen) { header_seen = 1; continue; }
86 char const *p = line;
87 int tabs = 0;
88 while (*p && tabs < 4) {
89 if (*p == '\t') tabs++;
90 p++;
91 }
92 if (tabs < 4) continue;
93 if (strncmp(p, type, rlen) == 0 && p[rlen] == '\t')
94 found = 1;
95 }
96 fclose(f);
97 return found;
98}
99
100static char *utest_find_dir_with_affixes(char const *prefix, char const *suffix) {
101 DIR *d = opendir(".");
102 if (!d) return NULL;
103 size_t const plen = strlen(prefix);
104 size_t const slen = strlen(suffix);
105 struct dirent *ent;
106 char *result = NULL;
107 while ((ent = readdir(d)) != NULL) {
108 char const *name = ent->d_name;
109 size_t const nlen = strlen(name);
110 if (nlen < plen + slen) continue;
111 if (strncmp(name, prefix, plen) != 0) continue;
112 if (strcmp(name + nlen - slen, suffix) != 0) continue;
113 struct stat st;
114 if (stat(name, &st) != 0 || !S_ISDIR(st.st_mode)) continue;
115 result = strdup(name);
116 break;
117 }
118 closedir(d);
119 return result;
120}
121
122/* Test 1: Verify per-rank CSV and trace_metadata.json */
123static void utest_multirank_basic(int rank) {
124 setenv("YAC_PAK_TRACE", "1", 1);
125 setenv("YAC_PAK_TRACE_LEVEL", "1", 1);
126 setenv("YAC_PAK_TRACE_DIR", "tracing_basic_traces", 1);
127
128 struct yac_pak *pak = yac_pak_new(MPI_COMM_WORLD, 64);
129
130 int const n_events = 2 * rank + 2;
131 for (int i = 0; i < n_events; i++) {
132 int h = yac_pak_trace_start_ex(pak,
133 "field_a", "tcomp", "tgrid", YAC_PAK_EVENT_TYPE_PUT, -1, NULL, 0, YAC_PAK_TRACE_LEVEL_COUPLING);
134 if (h < 0) { INC_ERR; continue; }
135 if (yac_pak_trace_stop(pak, h) != 0) { INC_ERR; }
136 }
137
139 MPI_Barrier(MPI_COMM_WORLD);
140
141 if (rank == 0) {
142 for (int r = 0; r < 4; r++) {
143 char path[128];
144 snprintf(path, sizeof(path), "tracing_basic_traces/tcomp_r%d.csv", r);
145 if (!yac_file_exists(path)) { PUT_ERR(" FAIL: per-rank CSV not found"); continue; }
146 if (utest_count_data_lines(path) != 2 * r + 2)
147 PUT_ERR(" FAIL: wrong event count in per-rank CSV");
148 if (utest_check_csv_rank_column(path, r) != 0)
149 PUT_ERR(" FAIL: wrong rank value in rank column");
150 }
151 if (utest_file_has_line_prefix(
152 "tracing_basic_traces/tcomp_r0.csv", "# flush_duration_s=") != 1)
153 PUT_ERR(" FAIL: flush_duration_s= header missing (default=1)");
154 char const *meta = "tracing_basic_traces/trace_metadata.json";
155 if (!yac_file_exists(meta)) {
156 PUT_ERR(" FAIL: trace_metadata.json not found");
157 } else {
158 if (yac_file_contains(meta, "\"mpi_size\": 4") != 1)
159 PUT_ERR(" FAIL: trace_metadata.json missing \"mpi_size\": 4");
161 meta,
162 "\"yac_pak_version\": \"v" YAC_PAK_VERSION_STRING "\"") != 1)
163 PUT_ERR(" FAIL: trace_metadata.json missing yac_pak_version");
164 }
165 }
166
167 yac_pak_delete(pak);
168 utest_clear_trace_env();
169 if (rank == 0) yac_remove_directory("tracing_basic_traces");
170 MPI_Barrier(MPI_COMM_WORLD);
171}
172
173/* Test 2: Auto-generated output directory name */
174static void utest_autodir(int rank) {
175 if (rank == 0) {
176 char *old;
177 while ((old = utest_find_dir_with_affixes(
178 "traces_", "_N4_v" YAC_PAK_VERSION_STRING)) != NULL) {
180 free(old);
181 }
182 }
183 MPI_Barrier(MPI_COMM_WORLD);
184
185 setenv("YAC_PAK_TRACE", "1", 1);
186 setenv("YAC_PAK_TRACE_LEVEL", "1", 1);
187 unsetenv("YAC_PAK_TRACE_DIR");
188
189 struct yac_pak *pak = yac_pak_new(MPI_COMM_WORLD, 16);
190
191 int h = yac_pak_trace_start_ex(pak,
192 "field", "tcomp", "tgrid", YAC_PAK_EVENT_TYPE_PUT, -1, NULL, 0, YAC_PAK_TRACE_LEVEL_COUPLING);
193 if (h < 0) { INC_ERR; }
194 else if (yac_pak_trace_stop(pak, h) != 0) { INC_ERR; }
195
197 MPI_Barrier(MPI_COMM_WORLD);
198
199 if (rank == 0) {
200 char *dir = utest_find_dir_with_affixes(
201 "traces_", "_N4_v" YAC_PAK_VERSION_STRING);
202 if (!dir) {
203 PUT_ERR(" FAIL: auto-generated trace dir not found");
204 } else {
205 for (int r = 0; r < 4; r++) {
206 char path[512];
207 snprintf(path, sizeof(path), "%s/tcomp_r%d.csv", dir, r);
208 if (!yac_file_exists(path))
209 PUT_ERR(" FAIL: per-rank CSV not found in auto-generated dir");
210 }
211 char meta[512];
212 snprintf(meta, sizeof(meta), "%s/trace_metadata.json", dir);
213 if (!yac_file_exists(meta))
214 PUT_ERR(" FAIL: autodir: trace_metadata.json not found");
216 free(dir);
217 }
218 }
219
220 yac_pak_delete(pak);
221 utest_clear_trace_env();
222 MPI_Barrier(MPI_COMM_WORLD);
223}
224
225/* Test 3: Stride-based selective writing, stride=2 */
226static void utest_stride(int rank) {
227 setenv("YAC_PAK_TRACE", "1", 1);
228 setenv("YAC_PAK_TRACE_LEVEL", "1", 1);
229 setenv("YAC_PAK_TRACE_STRIDE", "2", 1);
230 setenv("YAC_PAK_TRACE_DIR", "tracing_stride_traces", 1);
231
232 struct yac_pak *pak = yac_pak_new(MPI_COMM_WORLD, 16);
233
234 int h = yac_pak_trace_start_ex(pak,
235 "field", "tcomp", "tgrid", YAC_PAK_EVENT_TYPE_PUT, -1, NULL, 0, YAC_PAK_TRACE_LEVEL_COUPLING);
236 if (h < 0) { INC_ERR; }
237 else if (yac_pak_trace_stop(pak, h) != 0) { INC_ERR; }
238
240 MPI_Barrier(MPI_COMM_WORLD);
241
242 if (rank == 0) {
243 char path[128];
244 for (int r = 0; r < 4; r += 2) {
245 snprintf(path, sizeof(path), "tracing_stride_traces/tcomp_r%d.csv", r);
246 if (!yac_file_exists(path))
247 PUT_ERR(" FAIL: stride=2: even-rank CSV missing");
248 }
249 for (int r = 1; r < 4; r += 2) {
250 snprintf(path, sizeof(path), "tracing_stride_traces/tcomp_r%d.csv", r);
251 if (yac_file_exists(path))
252 PUT_ERR(" FAIL: stride=2: odd-rank CSV exists (should not)");
253 }
254 }
255
256 yac_pak_delete(pak);
257 utest_clear_trace_env();
258 if (rank == 0) yac_remove_directory("tracing_stride_traces");
259 MPI_Barrier(MPI_COMM_WORLD);
260}
261
262/* Test 4: Stride=100 (cap to mpi_size=4) */
263static void utest_stride_cap(int rank) {
264 setenv("YAC_PAK_TRACE", "1", 1);
265 setenv("YAC_PAK_TRACE_LEVEL", "1", 1);
266 setenv("YAC_PAK_TRACE_STRIDE", "100", 1);
267 setenv("YAC_PAK_TRACE_DIR", "tracing_stridecap_traces", 1);
268
269 struct yac_pak *pak = yac_pak_new(MPI_COMM_WORLD, 16);
270
271 int h = yac_pak_trace_start_ex(pak,
272 "field", "tcomp", "tgrid", YAC_PAK_EVENT_TYPE_PUT, -1, NULL, 0, YAC_PAK_TRACE_LEVEL_COUPLING);
273 if (h < 0) { INC_ERR; }
274 else if (yac_pak_trace_stop(pak, h) != 0) { INC_ERR; }
275
277 MPI_Barrier(MPI_COMM_WORLD);
278
279 if (rank == 0) {
280 if (!yac_file_exists("tracing_stridecap_traces/tcomp_r0.csv"))
281 PUT_ERR(" FAIL: stride_cap: tcomp_r0.csv missing");
282 for (int r = 1; r < 4; r++) {
283 char path[128];
284 snprintf(path, sizeof(path),
285 "tracing_stridecap_traces/tcomp_r%d.csv", r);
286 if (yac_file_exists(path))
287 PUT_ERR(" FAIL: stride_cap: non-rank-0 CSV exists (should not)");
288 }
289 }
290
291 yac_pak_delete(pak);
292 utest_clear_trace_env();
293 if (rank == 0) yac_remove_directory("tracing_stridecap_traces");
294 MPI_Barrier(MPI_COMM_WORLD);
295}
296
297/* Test 5: Setup-phase event tracing */
298static void utest_setup_events(int rank) {
299 /* 5a: YAC_PAK_TRACE_SETUP=1 */
300 setenv("YAC_PAK_TRACE", "1", 1);
301 setenv("YAC_PAK_TRACE_LEVEL", "1", 1);
302 setenv("YAC_PAK_TRACE_SETUP", "1", 1);
303 setenv("YAC_PAK_TRACE_DIR", "tracing_setup_traces", 1);
304
305 struct yac_pak *pak = yac_pak_new(MPI_COMM_WORLD, 64);
306
307 double const t0 = MPI_Wtime();
308 double const t1 = MPI_Wtime();
309 if (yac_pak_trace_record(pak, "init", "tcomp", NULL, YAC_PAK_EVENT_TYPE_INIT, t0, t1) != 0)
310 INC_ERR;
311
312 int h = yac_pak_trace_start_ex(pak,
313 "def_comp", "tcomp", NULL, YAC_PAK_EVENT_TYPE_DEF_COMP, -1, NULL, 0, YAC_PAK_TRACE_LEVEL_OFF);
314 if (h < 0) { INC_ERR; }
315 else if (yac_pak_trace_stop(pak, h) != 0) { INC_ERR; }
316
318 "field", "tcomp", "tgrid", YAC_PAK_EVENT_TYPE_PUT, -1, NULL, 0, YAC_PAK_TRACE_LEVEL_COUPLING);
319 if (h < 0) { INC_ERR; }
320 else if (yac_pak_trace_stop(pak, h) != 0) { INC_ERR; }
321
323 MPI_Barrier(MPI_COMM_WORLD);
324
325 if (rank == 0) {
326 if (utest_count_data_lines("tracing_setup_traces/tcomp_r0.csv") != 3)
327 PUT_ERR(" FAIL: setup_events: wrong event count (expected 3)");
328 }
329
330 yac_pak_delete(pak);
331 utest_clear_trace_env();
332 if (rank == 0) yac_remove_directory("tracing_setup_traces");
333 MPI_Barrier(MPI_COMM_WORLD);
334 /* 5b: YAC_PAK_TRACE_SETUP=0 */
335 setenv("YAC_PAK_TRACE", "1", 1);
336 setenv("YAC_PAK_TRACE_LEVEL", "1", 1);
337 setenv("YAC_PAK_TRACE_DIR", "tracing_setup_off_traces", 1);
338
339 pak = yac_pak_new(MPI_COMM_WORLD, 64);
340
341 double const t2 = MPI_Wtime();
342 double const t3 = MPI_Wtime();
343 if (yac_pak_trace_record(pak, "init", "tcomp", NULL, YAC_PAK_EVENT_TYPE_INIT, t2, t3) == 0)
344 PUT_ERR(" FAIL: trace_record should return -1 without YAC_PAK_TRACE_SETUP");
345
346 int h2 = yac_pak_trace_start_ex(pak,
347 "def_comp", "tcomp", NULL, YAC_PAK_EVENT_TYPE_DEF_COMP, -1, NULL, 0, YAC_PAK_TRACE_LEVEL_OFF);
348 if (h2 >= 0)
349 PUT_ERR(" FAIL: level=0 start_ex should return -1 without YAC_PAK_TRACE_SETUP");
350
351 h2 = yac_pak_trace_start_ex(pak,
352 "field", "tcomp", "tgrid", YAC_PAK_EVENT_TYPE_PUT, -1, NULL, 0, YAC_PAK_TRACE_LEVEL_COUPLING);
353 if (h2 < 0) { INC_ERR; }
354 else if (yac_pak_trace_stop(pak, h2) != 0) { INC_ERR; }
355
357 MPI_Barrier(MPI_COMM_WORLD);
358
359 if (rank == 0) {
360 if (utest_count_data_lines("tracing_setup_off_traces/tcomp_r0.csv") != 1)
361 PUT_ERR(" FAIL: setup_off: wrong event count (expected 1 coupling event)");
362 }
363
364 yac_pak_delete(pak);
365 utest_clear_trace_env();
366 if (rank == 0) yac_remove_directory("tracing_setup_off_traces");
367 MPI_Barrier(MPI_COMM_WORLD);
368}
369
370/* Test 6: Test flush timer */
371static void utest_flush_time_off(int rank) {
372 setenv("YAC_PAK_TRACE", "1", 1);
373 setenv("YAC_PAK_TRACE_LEVEL", "1", 1);
374 setenv("YAC_PAK_TRACE_FLUSH_TIME", "0", 1);
375 setenv("YAC_PAK_TRACE_DIR", "tracing_flushtime_traces", 1);
376
377 struct yac_pak *pak = yac_pak_new(MPI_COMM_WORLD, 16);
378
379 int h = yac_pak_trace_start_ex(pak,
380 "field", "tcomp", "tgrid", YAC_PAK_EVENT_TYPE_PUT, -1, NULL, 0, YAC_PAK_TRACE_LEVEL_COUPLING);
381 if (h < 0) { INC_ERR; }
382 else if (yac_pak_trace_stop(pak, h) != 0) { INC_ERR; }
383
385 MPI_Barrier(MPI_COMM_WORLD);
386
387 if (rank == 0) {
388 char const *path = "tracing_flushtime_traces/tcomp_r0.csv";
389 if (utest_file_has_line_prefix(path, "# flush_duration_s=") == 1)
390 PUT_ERR(" FAIL: flush_time=0 but flush_duration_s= found in header");
391 if (utest_file_has_line_prefix(path, "# yac_pak_version=") != 1)
392 PUT_ERR(" FAIL: yac_pak_version= header missing");
393 if (utest_file_has_line_prefix(path, "# wtime_ref=") != 1)
394 PUT_ERR(" FAIL: wtime_ref= header missing");
395 if (utest_file_has_line_prefix(path, "# epoch_ref=") != 1)
396 PUT_ERR(" FAIL: epoch_ref= header missing");
397 }
398
399 yac_pak_delete(pak);
400 utest_clear_trace_env();
401 if (rank == 0) yac_remove_directory("tracing_flushtime_traces");
402 MPI_Barrier(MPI_COMM_WORLD);
403}
404
405/* Test 7: Open-event buffer growth */
406static void utest_open_event_growth(int rank) {
407 setenv("YAC_PAK_TRACE", "1", 1);
408 setenv("YAC_PAK_TRACE_LEVEL", "1", 1);
409 setenv("YAC_PAK_TRACE_DIR", "tracing_opengrowth_traces", 1);
410
411 struct yac_pak *pak = yac_pak_new(MPI_COMM_WORLD, 256);
412
413 int handles[33];
414 for (int i = 0; i < 33; i++) {
415 handles[i] = yac_pak_trace_start_ex(pak,
416 "field", "tcomp", "tgrid", YAC_PAK_EVENT_TYPE_PUT, -1, NULL, 0, YAC_PAK_TRACE_LEVEL_COUPLING);
417 if (handles[i] < 0) { INC_ERR; }
418 }
419 for (int i = 0; i < 33; i++) {
420 if (handles[i] >= 0 && yac_pak_trace_stop(pak, handles[i]) != 0) { INC_ERR; }
421 }
422
424 MPI_Barrier(MPI_COMM_WORLD);
425
426 if (rank == 0) {
427 if (utest_count_data_lines("tracing_opengrowth_traces/tcomp_r0.csv") != 33)
428 PUT_ERR(" FAIL: open_event growth: wrong event count");
429 }
430
431 yac_pak_delete(pak);
432 utest_clear_trace_env();
433 if (rank == 0) yac_remove_directory("tracing_opengrowth_traces");
434 MPI_Barrier(MPI_COMM_WORLD);
435}
436
437/* Test 8: level=2 REGION event nested inside a level=1 PUT event. */
438static void utest_level2_nested(int rank) {
439 setenv("YAC_PAK_TRACE", "1", 1);
440 setenv("YAC_PAK_TRACE_LEVEL", "2", 1);
441 setenv("YAC_PAK_TRACE_DIR", "tracing_level2_traces", 1);
442
443 struct yac_pak *pak = yac_pak_new(MPI_COMM_WORLD, 32);
444
445 int h_put = yac_pak_trace_start_ex(pak,
446 "field", "tcomp", "tgrid", YAC_PAK_EVENT_TYPE_PUT, -1, NULL, 0, YAC_PAK_TRACE_LEVEL_COUPLING);
447 if (h_put < 0) { INC_ERR; }
448
449 /* nested region event (region name goes in the field column) */
450 int h_region = yac_pak_trace_start_ex(pak,
451 "target field computation (SpMV)", "tcomp", "tgrid",
453 if (h_region < 0) { INC_ERR; }
454
455 if (h_region >= 0 && yac_pak_trace_stop(pak, h_region) != 0) { INC_ERR; }
456 if (h_put >= 0 && yac_pak_trace_stop(pak, h_put) != 0) { INC_ERR; }
457
459 MPI_Barrier(MPI_COMM_WORLD);
460
461 if (rank == 0) {
462 char const *path = "tracing_level2_traces/tcomp_r0.csv";
463 if (!yac_file_exists(path))
464 PUT_ERR(" FAIL: level2_nested: CSV not found");
465 if (utest_count_data_lines(path) != 2)
466 PUT_ERR(" FAIL: level2_nested: expected 2 events (PUT + REGION)");
467 if (utest_csv_has_type(path, "PUT") != 1)
468 PUT_ERR(" FAIL: level2_nested: PUT event missing");
469 if (utest_csv_has_type(path, "REGION") != 1)
470 PUT_ERR(" FAIL: level2_nested: REGION event missing");
471 }
472
473 yac_pak_delete(pak);
474 utest_clear_trace_env();
475 if (rank == 0) yac_remove_directory("tracing_level2_traces");
476 MPI_Barrier(MPI_COMM_WORLD);
477}
478
479/* Test 9: level gating. YAC_PAK_TRACE_LEVEL=1 discards level-2 REGION events.
480 * YAC_PAK_TRACE_LEVEL=2 accepts both level-1 and level-2 events. */
481static void utest_level_filtering(int rank) {
482 /* Case A: YAC_PAK_TRACE_LEVEL=1 rejects a level-2 event */
483 setenv("YAC_PAK_TRACE", "1", 1);
484 setenv("YAC_PAK_TRACE_LEVEL", "1", 1);
485 setenv("YAC_PAK_TRACE_DIR", "tracing_filter_a_traces", 1);
486
487 struct yac_pak *pak = yac_pak_new(MPI_COMM_WORLD, 16);
488
489 int h1 = yac_pak_trace_start_ex(pak,
490 "field", "tcomp", "tgrid", YAC_PAK_EVENT_TYPE_PUT, -1, NULL, 0, YAC_PAK_TRACE_LEVEL_COUPLING);
491 if (h1 < 0) { INC_ERR; }
492 else if (yac_pak_trace_stop(pak, h1) != 0) { INC_ERR; }
493
494 int h2 = yac_pak_trace_start_ex(pak,
495 "region", "tcomp", "tgrid", YAC_PAK_EVENT_TYPE_REGION, -1, NULL, -1, YAC_PAK_TRACE_LEVEL_REGIONS);
496 if (h2 != -1)
497 PUT_ERR(" FAIL: filter_a: level=2 should return -1 with "
498 "YAC_PAK_TRACE_LEVEL=1");
499
501 MPI_Barrier(MPI_COMM_WORLD);
502
503 if (rank == 0) {
504 if (utest_count_data_lines("tracing_filter_a_traces/tcomp_r0.csv") != 1)
505 PUT_ERR(" FAIL: filter_a: expected exactly 1 event (level=1 PUT only)");
506 }
507
508 yac_pak_delete(pak);
509 utest_clear_trace_env();
510 if (rank == 0) yac_remove_directory("tracing_filter_a_traces");
511 MPI_Barrier(MPI_COMM_WORLD);
512
513 /* Case B: YAC_PAK_TRACE_LEVEL=2 accepts both level-1 and level-2 events */
514 setenv("YAC_PAK_TRACE", "1", 1);
515 setenv("YAC_PAK_TRACE_LEVEL", "2", 1);
516 setenv("YAC_PAK_TRACE_DIR", "tracing_filter_b_traces", 1);
517
518 pak = yac_pak_new(MPI_COMM_WORLD, 16);
519
520 int ha = yac_pak_trace_start_ex(pak,
521 "field", "tcomp", "tgrid", YAC_PAK_EVENT_TYPE_PUT, -1, NULL, 0, YAC_PAK_TRACE_LEVEL_COUPLING);
522 if (ha < 0) { INC_ERR; }
523
524 int hb = yac_pak_trace_start_ex(pak,
525 "region", "tcomp", "tgrid", YAC_PAK_EVENT_TYPE_REGION, -1, NULL, -1, YAC_PAK_TRACE_LEVEL_REGIONS);
526 if (hb < 0)
527 PUT_ERR(" FAIL: filter_b: level=2 should succeed with "
528 "YAC_PAK_TRACE_LEVEL=2");
529
530 if (hb >= 0 && yac_pak_trace_stop(pak, hb) != 0) { INC_ERR; }
531 if (ha >= 0 && yac_pak_trace_stop(pak, ha) != 0) { INC_ERR; }
532
534 MPI_Barrier(MPI_COMM_WORLD);
535
536 if (rank == 0) {
537 if (utest_count_data_lines("tracing_filter_b_traces/tcomp_r0.csv") != 2)
538 PUT_ERR(" FAIL: filter_b: expected 2 events (PUT + REGION) with "
539 "YAC_PAK_TRACE_LEVEL=2");
540 }
541
542 yac_pak_delete(pak);
543 utest_clear_trace_env();
544 if (rank == 0) yac_remove_directory("tracing_filter_b_traces");
545 MPI_Barrier(MPI_COMM_WORLD);
546}
547
548/* Test 10: YAC_PAK_TRACE_LEVEL=0 ("coupling off"): yac_pak_new does
549 * not abort, level-1 coupling events are discarded, but level-0 setup events
550 * are still recorded when YAC_PAK_TRACE_SETUP=1 (setup-only tracing). */
551static void utest_level0_coupling_off(int rank) {
552 setenv("YAC_PAK_TRACE", "1", 1);
553 setenv("YAC_PAK_TRACE_LEVEL", "0", 1);
554 setenv("YAC_PAK_TRACE_SETUP", "1", 1);
555 setenv("YAC_PAK_TRACE_DIR", "tracing_level0_traces", 1);
556
557 struct yac_pak *pak = yac_pak_new(MPI_COMM_WORLD, 16);
558 if (!pak) { INC_ERR; }
559
560 /* a level-1 coupling event must be discarded at LEVEL=0 */
561 int h_put = yac_pak_trace_start_ex(pak,
562 "field", "tcomp", "tgrid", YAC_PAK_EVENT_TYPE_PUT, -1, NULL, 0, YAC_PAK_TRACE_LEVEL_COUPLING);
563 if (h_put != -1)
564 PUT_ERR(" FAIL: level0: level-1 PUT should not be recorded at LEVEL=0");
565
566 /* a level-0 setup event is still recorded (YAC_PAK_TRACE_SETUP=1) */
567 int h_setup =
569 if (h_setup < 0) {
570 PUT_ERR(" FAIL: level0: setup event should be recorded with SETUP=1");
571 } else if (yac_pak_trace_stop(pak, h_setup) != 0) { INC_ERR; }
572
574 MPI_Barrier(MPI_COMM_WORLD);
575
576 yac_pak_delete(pak);
577 utest_clear_trace_env();
578 if (rank == 0) yac_remove_directory("tracing_level0_traces");
579 MPI_Barrier(MPI_COMM_WORLD);
580}
581
582/* Test 11: YAC_PAK_TRACE_WRITE_MODE=instant, keep a
583 * .open file with the events in progress */
584static void utest_write_mode_instant(int rank) {
585 setenv("YAC_PAK_TRACE", "1", 1);
586 setenv("YAC_PAK_TRACE_LEVEL", "1", 1);
587 setenv("YAC_PAK_TRACE_WRITE_MODE", "instant", 1);
588 setenv("YAC_PAK_TRACE_DIR", "tracing_instant_traces", 1);
589
590 struct yac_pak *pak = yac_pak_new(MPI_COMM_WORLD, 16);
591
592 MPI_Barrier(MPI_COMM_WORLD);
593 if (rank == 0) {
594 for (int r = 0; r < 4; r++) {
595 char path[128];
596 snprintf(path, sizeof(path),
597 "tracing_instant_traces/yac_pak_unknown_r%d.csv", r);
598 if (!yac_file_exists(path))
599 PUT_ERR(" FAIL: instant: trace file not created by yac_pak_new");
600 snprintf(path, sizeof(path),
601 "tracing_instant_traces/yac_pak_unknown_r%d.open", r);
602 if (!yac_file_exists(path))
603 PUT_ERR(" FAIL: instant: .open file not created by yac_pak_new");
604 }
605 }
606 MPI_Barrier(MPI_COMM_WORLD);
607
608 int h = yac_pak_trace_start_ex(pak,
609 "field", "tcomp", "tgrid", YAC_PAK_EVENT_TYPE_GET, -1, NULL, 0,
611 if (h < 0) { INC_ERR; }
612
613 /* while the event runs it is listed in .open, absent from the CSV */
614 MPI_Barrier(MPI_COMM_WORLD);
615 if (rank == 0) {
616 for (int r = 0; r < 4; r++) {
617 char csv[128], open[128];
618 snprintf(csv, sizeof(csv), "tracing_instant_traces/tcomp_r%d.csv", r);
619 snprintf(open, sizeof(open), "tracing_instant_traces/tcomp_r%d.open", r);
620 if (!yac_file_exists(csv) || !yac_file_exists(open)) {
621 PUT_ERR(" FAIL: instant: files not renamed to the component name");
622 continue;
623 }
624 if (utest_count_data_lines(csv) != 0)
625 PUT_ERR(" FAIL: instant: unfinished event already in the CSV");
626 if (utest_count_data_lines(open) != 1)
627 PUT_ERR(" FAIL: instant: event in progress not listed in .open");
628 if (utest_csv_has_type(open, "GET") != 1)
629 PUT_ERR(" FAIL: instant: .open does not name the stuck operation");
630 }
631 }
632 MPI_Barrier(MPI_COMM_WORLD);
633
634 if (yac_pak_trace_stop(pak, h) != 0) { INC_ERR; }
635
636 /* once it ends it is in the CSV, without any flush and gone from .open */
637 MPI_Barrier(MPI_COMM_WORLD);
638 if (rank == 0) {
639 if (utest_count_data_lines("tracing_instant_traces/tcomp_r0.csv") != 1)
640 PUT_ERR(" FAIL: instant: finished event not written immediately");
641 if (utest_count_data_lines("tracing_instant_traces/tcomp_r0.open") != 0)
642 PUT_ERR(" FAIL: instant: finished event still listed in .open");
643 }
644 MPI_Barrier(MPI_COMM_WORLD);
645
646 if (yac_pak_flush_trace(pak) != 0)
647 PUT_ERR(" FAIL: instant: flush_trace reported an error");
648
649 yac_pak_delete(pak);
650 MPI_Barrier(MPI_COMM_WORLD);
651
652 if (rank == 0) {
653 char const *csv = "tracing_instant_traces/tcomp_r0.csv";
654 if (utest_count_data_lines(csv) != 1)
655 PUT_ERR(" FAIL: instant: event rows changed by flush/delete");
656 if (yac_file_exists("tracing_instant_traces/tcomp_r0.open"))
657 PUT_ERR(" FAIL: instant: .open file not removed on clean shutdown");
658 if (!yac_file_exists("tracing_instant_traces/trace_metadata.json"))
659 PUT_ERR(" FAIL: instant: trace_metadata.json missing");
660 }
661
662 utest_clear_trace_env();
663 if (rank == 0) yac_remove_directory("tracing_instant_traces");
664 MPI_Barrier(MPI_COMM_WORLD);
665}
666
667/* Test 12: write mode "instant" also covers yac_pak_trace_record, which
668 * records an already-finished event (used for yac_init) */
669static void utest_write_mode_instant_record(int rank) {
670 setenv("YAC_PAK_TRACE", "1", 1);
671 setenv("YAC_PAK_TRACE_LEVEL", "1", 1);
672 setenv("YAC_PAK_TRACE_SETUP", "1", 1);
673 setenv("YAC_PAK_TRACE_WRITE_MODE", "instant", 1);
674 setenv("YAC_PAK_TRACE_DIR", "tracing_instant_record_traces", 1);
675
676 struct yac_pak *pak = yac_pak_new(MPI_COMM_WORLD, 16);
677
679 pak, "yac_init", "tcomp", NULL,
680 YAC_PAK_EVENT_TYPE_INIT, 1.0, 2.0) != 0)
681 PUT_ERR(" FAIL: instant record: trace_record reported an error");
682
683 /* the row is on disk without any flush and the files carry the
684 * component name that trace_record supplied */
685 MPI_Barrier(MPI_COMM_WORLD);
686 if (rank == 0) {
687 char const *csv = "tracing_instant_record_traces/tcomp_r0.csv";
688 if (!yac_file_exists(csv)) {
689 PUT_ERR(" FAIL: instant record: files not renamed by trace_record");
690 } else {
691 if (utest_count_data_lines(csv) != 1)
692 PUT_ERR(" FAIL: instant record: event not written immediately");
693 if (utest_csv_has_type(csv, "INIT") != 1)
694 PUT_ERR(" FAIL: instant record: INIT event missing");
695 }
696 }
697 MPI_Barrier(MPI_COMM_WORLD);
698
699 yac_pak_delete(pak);
700 utest_clear_trace_env();
701 if (rank == 0) yac_remove_directory("tracing_instant_record_traces");
702 MPI_Barrier(MPI_COMM_WORLD);
703}
704
705/* Test 13: YAC_PAK_TRACE_VERBOSE=1 prints the progress messages to stderr
706 * and leaves the trace itself unchanged */
707static void utest_verbose(int rank) {
708 setenv("YAC_PAK_TRACE", "1", 1);
709 setenv("YAC_PAK_TRACE_LEVEL", "1", 1);
710 setenv("YAC_PAK_TRACE_VERBOSE", "1", 1);
711 setenv("YAC_PAK_TRACE_DIR", "tracing_verbose_traces", 1);
712
713 /* capture stderr on rank 0 (only rank that reports it) */
714 int saved_stderr = -1;
715 if (rank == 0) {
716 fflush(stderr);
717 saved_stderr = dup(fileno(stderr));
718 if (!freopen("tracing_verbose_stderr.txt", "w", stderr))
719 PUT_ERR(" FAIL: verbose: could not redirect stderr");
720 }
721
722 struct yac_pak *pak = yac_pak_new(MPI_COMM_WORLD, 16);
723
724 int h = yac_pak_trace_start_ex(pak,
725 "field", "tcomp", "tgrid", YAC_PAK_EVENT_TYPE_PUT, -1, NULL, 0,
727 if (h < 0) { INC_ERR; }
728 else if (yac_pak_trace_stop(pak, h) != 0) { INC_ERR; }
729
731 yac_pak_delete(pak);
732
733 if (rank == 0) {
734 fflush(stderr);
735 dup2(saved_stderr, fileno(stderr));
736 close(saved_stderr);
737 }
738 MPI_Barrier(MPI_COMM_WORLD);
739
740 if (rank == 0) {
742 "tracing_verbose_stderr.txt", "YAC PAK trace: writing to") != 1)
743 PUT_ERR(" FAIL: verbose: trace directory message missing");
745 "tracing_verbose_stderr.txt", "Writing per-rank traces") != 1)
746 PUT_ERR(" FAIL: verbose: flush message missing");
747 /* the trace itself is unaffected by the flag */
748 if (utest_count_data_lines("tracing_verbose_traces/tcomp_r0.csv") != 1)
749 PUT_ERR(" FAIL: verbose: event row missing");
750 remove("tracing_verbose_stderr.txt");
751 }
752
753 utest_clear_trace_env();
754 if (rank == 0) yac_remove_directory("tracing_verbose_traces");
755 MPI_Barrier(MPI_COMM_WORLD);
756
757 /* The closing message only exists in write mode "instant", where the
758 * trace file is closed at the end instead of being written at flush */
759 setenv("YAC_PAK_TRACE", "1", 1);
760 setenv("YAC_PAK_TRACE_LEVEL", "1", 1);
761 setenv("YAC_PAK_TRACE_VERBOSE", "1", 1);
762 setenv("YAC_PAK_TRACE_WRITE_MODE", "instant", 1);
763 setenv("YAC_PAK_TRACE_DIR", "tracing_verbose_instant_traces", 1);
764
765 if (rank == 0) {
766 fflush(stderr);
767 saved_stderr = dup(fileno(stderr));
768 if (!freopen("tracing_verbose_instant_stderr.txt", "w", stderr))
769 PUT_ERR(" FAIL: verbose: could not redirect stderr");
770 }
771
772 pak = yac_pak_new(MPI_COMM_WORLD, 16);
774 "field", "tcomp", "tgrid", YAC_PAK_EVENT_TYPE_PUT, -1, NULL, 0,
776 if (h < 0) { INC_ERR; }
777 else if (yac_pak_trace_stop(pak, h) != 0) { INC_ERR; }
778 yac_pak_delete(pak);
779
780 if (rank == 0) {
781 fflush(stderr);
782 dup2(saved_stderr, fileno(stderr));
783 close(saved_stderr);
784 }
785 MPI_Barrier(MPI_COMM_WORLD);
786
787 if (rank == 0) {
789 "tracing_verbose_instant_stderr.txt",
790 "per-rank traces written") != 1)
791 PUT_ERR(" FAIL: verbose: closing message missing in instant mode");
792 remove("tracing_verbose_instant_stderr.txt");
793 }
794
795 utest_clear_trace_env();
796 if (rank == 0) yac_remove_directory("tracing_verbose_instant_traces");
797 MPI_Barrier(MPI_COMM_WORLD);
798}
799
800/* Test 14: YAC_PAK_TRACE_WRITE_MODE=instant with YAC_PAK_TRACE_STRIDE,
801 a rank that does not write records nothing at all */
802static void utest_write_mode_instant_stride(int rank) {
803 setenv("YAC_PAK_TRACE", "1", 1);
804 setenv("YAC_PAK_TRACE_LEVEL", "2", 1);
805 setenv("YAC_PAK_TRACE_WRITE_MODE", "instant", 1);
806 setenv("YAC_PAK_TRACE_STRIDE", "2", 1);
807 setenv("YAC_PAK_TRACE_DIR", "tracing_instant_stride_traces", 1);
808
809 struct yac_pak *pak = yac_pak_new(MPI_COMM_WORLD, 16);
810
811 int const is_writer = (rank % 2 == 0);
812
814 pak, YAC_PAK_TRACE_LEVEL_REGIONS) != 0) != is_writer)
815 PUT_ERR(" FAIL: instant+stride: wrong region tracing state for rank");
816
817 int h = yac_pak_trace_start_ex(pak,
818 "field", "tcomp", "tgrid", YAC_PAK_EVENT_TYPE_PUT, -1, NULL, 0,
820 if (is_writer) {
821 if (h < 0) { PUT_ERR(" FAIL: instant+stride: writer refused an event"); }
822 else if (yac_pak_trace_stop(pak, h) != 0) { INC_ERR; }
823 } else {
824 if (h >= 0)
825 PUT_ERR(" FAIL: instant+stride: non-writer accepted an event");
826 /* stopping a refused event must stay harmless */
827 if (yac_pak_trace_stop(pak, h) != -1)
828 PUT_ERR(" FAIL: instant+stride: trace_stop of a refused event");
829 }
830
831 MPI_Barrier(MPI_COMM_WORLD);
832
833 if (rank == 0) {
834 for (int r = 0; r < 4; r++) {
835 char csv[128], open[128];
836 snprintf(csv, sizeof(csv),
837 "tracing_instant_stride_traces/tcomp_r%d.csv", r);
838 snprintf(open, sizeof(open),
839 "tracing_instant_stride_traces/tcomp_r%d.open", r);
840 if (r % 2 == 0) {
841 if (!yac_file_exists(csv)) {
842 PUT_ERR(" FAIL: instant+stride: writer has no trace file");
843 } else if (utest_count_data_lines(csv) != 1) {
844 PUT_ERR(" FAIL: instant+stride: writer row missing");
845 }
846 } else {
847 if (yac_file_exists(csv) || yac_file_exists(open))
848 PUT_ERR(" FAIL: instant+stride: non-writer wrote a file");
849 }
850 }
851 }
852
853 yac_pak_delete(pak);
854 utest_clear_trace_env();
855 if (rank == 0) yac_remove_directory("tracing_instant_stride_traces");
856 MPI_Barrier(MPI_COMM_WORLD);
857}
858
859int main(void) {
860 MPI_Init(NULL, NULL);
861
862 int rank, size;
863 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
864 MPI_Comm_size(MPI_COMM_WORLD, &size);
865
866 if (size != 4) {
867 fputs("Wrong number of processes (must be 4)\n", stderr);
868 MPI_Finalize();
869 return EXIT_FAILURE;
870 }
871
872 utest_multirank_basic(rank);
873 utest_autodir(rank);
874 utest_stride(rank);
875 utest_stride_cap(rank);
876 utest_setup_events(rank);
877 utest_flush_time_off(rank);
878 utest_open_event_growth(rank);
879 utest_level2_nested(rank);
880 utest_level_filtering(rank);
881 utest_level0_coupling_off(rank);
882 utest_write_mode_instant(rank);
883 utest_write_mode_instant_record(rank);
884 utest_verbose(rank);
885 utest_write_mode_instant_stride(rank);
886
887 MPI_Finalize();
888 return TEST_EXIT_CODE;
889}
#define YAC_PAK_VERSION_STRING
Definition yac_pak.h:39
int yac_pak_region_trace_is_enabled(struct yac_pak const *pak, yac_pak_trace_level_t level)
Check whether region events at a given trace level will be recorded.
Definition yac_pak.c:913
int yac_pak_trace_start_setup(struct yac_pak *pak, char const *name, yac_pak_event_type_t type)
Setup-phase event tracing.
Definition yac_pak.c:972
void yac_pak_delete(struct yac_pak *pak)
Destroy a YAC PAK object.
Definition yac_pak.c:847
struct yac_pak * yac_pak_new(MPI_Comm comm, int capacity)
Construct a new YAC PAK object.
Definition yac_pak.c:737
int yac_pak_trace_record(struct yac_pak *pak, char const *field, char const *component, char const *grid, yac_pak_event_type_t type, double tstart, double tend)
Record a complete trace entry with explicit timestamps.
Definition yac_pak.c:979
int yac_pak_trace_stop(struct yac_pak *pak, int handle)
Stop a trace event.
Definition yac_pak.c:1055
int yac_pak_flush_trace(struct yac_pak *pak)
Flush trace data to disk.
Definition yac_pak.c:1092
int yac_pak_trace_start_ex(struct yac_pak *pak, char const *field, char const *component, char const *grid, yac_pak_event_type_t type, int put_idx, char const *datetime, int action, yac_pak_trace_level_t level)
Start a trace event with full metadata.
Definition yac_pak.c:1017
@ YAC_PAK_EVENT_TYPE_GET
Definition yac_pak.h:54
@ YAC_PAK_EVENT_TYPE_PUT
Definition yac_pak.h:56
@ YAC_PAK_EVENT_TYPE_INIT
Definition yac_pak.h:49
@ YAC_PAK_EVENT_TYPE_DEF_COMP
Definition yac_pak.h:50
@ YAC_PAK_EVENT_TYPE_ENDDEF
Definition yac_pak.h:52
@ YAC_PAK_EVENT_TYPE_REGION
Definition yac_pak.h:57
@ YAC_PAK_TRACE_LEVEL_OFF
no coupling events
Definition yac_pak.h:72
@ YAC_PAK_TRACE_LEVEL_REGIONS
Definition yac_pak.h:74
@ YAC_PAK_TRACE_LEVEL_COUPLING
PUT/GET/AGET/UPDATE/WAIT events.
Definition yac_pak.h:73
enum callback_type type
void yac_remove_directory(char const *path)
Remove a directory and its contents recursively.
Definition io_utils.c:500
int yac_file_contains(char const *path, char const *needle)
Check whether a file contains a specific string.
Definition io_utils.c:399
int yac_file_exists(const char *filename)
Check whether a file exists.
Definition io_utils.c:394
int rank
Definition yac_pak.c:137
int size
Definition yac_pak.c:138
#define TEST_EXIT_CODE
Definition tests.h:15
#define INC_ERR
Definition tests.h:13
#define PUT_ERR(string)
Definition tests.h:10
char const * name
Definition toy_scrip.c:114
double(* p)(double lon, double lat)
Definition toy_scrip.c:119