YAC 3.21.0
Yet Another Coupler
Loading...
Searching...
No Matches
test_yac_pak_basic.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 <sys/stat.h>
10#include <mpi.h>
11#include "io_utils.h"
12
13#include "tests.h"
14#include "yac_pak.h"
15#include "test_yac_pak_common.h"
16
26/* Test 1: Basic new/delete cycle */
27static void utest_new_delete(void) {
28
29 { // Test 1a: Tracing disabled
30 utest_clear_trace_env();
31 struct yac_pak *pak = yac_pak_new(MPI_COMM_SELF, 8);
32 if (pak) PUT_ERR("yac_pak_new should return NULL when tracing is disabled");
33 yac_pak_delete(pak);
34 }
35
36 { // Test 1b: Tracing enabled
37 utest_clear_trace_env();
38 setenv("YAC_PAK_TRACE", "1", 1);
39 setenv("YAC_PAK_TRACE_LEVEL", "1", 1);
40 setenv("YAC_PAK_TRACE_DIR", "test_new_delete_traces", 1);
41
42 struct yac_pak *pak = yac_pak_new(MPI_COMM_SELF, 8);
43 if (!pak) PUT_ERR("yac_pak_new failed");
44 yac_pak_delete(pak);
45
46 utest_clear_trace_env();
47 yac_remove_directory("test_new_delete_traces");
48 }
49}
50
51/* Test 2: Trace-enabled constructor */
52static void utest_enable_tracing(void) {
53
54 { // Test 2a: Tracing disabled
55 utest_clear_trace_env();
56 struct yac_pak *pak = yac_pak_new(MPI_COMM_SELF, 8);
58 PUT_ERR("yac_pak_new should return NULL when YAC_PAK_TRACE is unset");
59 yac_pak_delete(pak);
60 }
61
62 { // Test 2b: Tracing enabled
63 utest_clear_trace_env();
64 setenv("YAC_PAK_TRACE", "1", 1);
65 setenv("YAC_PAK_TRACE_LEVEL", "1", 1);
66 setenv("YAC_PAK_TRACE_DIR", "test_basic_traces", 1);
67
68 struct yac_pak *pak = yac_pak_new(MPI_COMM_SELF, 8);
69
71 PUT_ERR("Tracing not enabled after yac_pak_new");
72
74 yac_pak_delete(pak);
75
76 utest_clear_trace_env();
77 yac_remove_directory("test_basic_traces");
78 }
79}
80
81/* Test 3: Buffer growth */
82static void utest_buffer_growth(void) {
83
84 { // Test 3a: Open-event buffer growth
85 utest_clear_trace_env();
86 setenv("YAC_PAK_TRACE", "1", 1);
87 setenv("YAC_PAK_TRACE_LEVEL", "1", 1);
88 setenv("YAC_PAK_TRACE_SETUP", "1", 1);
89 setenv("YAC_PAK_TRACE_DIR", "test_growth_traces", 1);
90
91 int initial_capacity = 4;
92 struct yac_pak *pak = yac_pak_new(MPI_COMM_SELF, initial_capacity);
93
94 int num_events = 10;
95 for (int i = 0; i < num_events; i++) {
96 char field_name[32];
97 snprintf(field_name, sizeof(field_name), "field_%d", i);
98 double tstart = 1.0 + i * 0.1;
99 double tend = tstart + 0.05;
100 if (yac_pak_trace_record(pak, field_name, "test_comp", "test_grid",
101 YAC_PAK_EVENT_TYPE_PUT, tstart, tend) != 0)
102 PUT_ERR(" FAIL: yac_pak_trace_record failed");
103 }
104
106
107 char const *expected_file = "test_growth_traces/test_comp_r0.csv";
108 if (!yac_file_exists(expected_file)) {
109 PUT_ERR(" FAIL: Output file not created");
110 } else {
111 if (utest_count_data_lines(expected_file) != num_events)
112 PUT_ERR(" FAIL: wrong event count (buffer growth)");
113 }
114
115 yac_pak_delete(pak);
116 utest_clear_trace_env();
117 yac_remove_directory("test_growth_traces");
118 }
119}
120
121/* Test 4: Edge cases */
122static void utest_edge_cases(void) {
123 { /* Test 4a: Capacity = 1 (minimal buffer) */
124 utest_clear_trace_env();
125 setenv("YAC_PAK_TRACE", "1", 1);
126 setenv("YAC_PAK_TRACE_LEVEL", "1", 1);
127 setenv("YAC_PAK_TRACE_SETUP", "1", 1);
128 setenv("YAC_PAK_TRACE_DIR", "test_edge_traces_4a", 1);
129
130 struct yac_pak *pak = yac_pak_new(MPI_COMM_SELF, 1);
131
132 for (int i = 0; i < 3; i++) {
133 char field[16];
134 snprintf(field, sizeof(field), "f%d", i);
135 double t = 1.0 + i;
137 pak, field, "comp", "grid", YAC_PAK_EVENT_TYPE_PUT, t, t + 0.1);
138 }
139
141 int count = utest_count_data_lines("test_edge_traces_4a/comp_r0.csv");
142 if (count != 3)
143 PUT_ERR("capacity=1 test: wrong event count");
144
145 yac_pak_delete(pak);
146 utest_clear_trace_env();
147 yac_remove_directory("test_edge_traces_4a");
148 }
149
150 { /* Test 4b: Capacity=0, reading from YAC_PAK_TRACE_CAPACITY */
151 utest_clear_trace_env();
152 setenv("YAC_PAK_TRACE", "1", 1);
153 setenv("YAC_PAK_TRACE_LEVEL", "1", 1);
154 setenv("YAC_PAK_TRACE_DIR", "test_edge_traces_4b", 1);
155 setenv("YAC_PAK_TRACE_CAPACITY", "16", 1);
156
157 struct yac_pak *pak = yac_pak_new(MPI_COMM_SELF, 0);
158
159 if (!yac_pak_trace_is_enabled(pak)) {
160 PUT_ERR("capacity=0 did not enable tracing");
161 }
162
163 yac_pak_delete(pak);
164 utest_clear_trace_env();
165 yac_remove_directory("test_edge_traces_4b");
166 }
167
168 { /* Test 4c: Invalid handles */
169 utest_clear_trace_env();
170 setenv("YAC_PAK_TRACE", "1", 1);
171 setenv("YAC_PAK_TRACE_LEVEL", "1", 1);
172 setenv("YAC_PAK_TRACE_DIR", "test_edge_traces_4c", 1);
173
174 struct yac_pak *pak = yac_pak_new(MPI_COMM_SELF, 16);
175
176 if (yac_pak_trace_stop(pak, -1) == 0)
177 PUT_ERR("yac_pak_trace_stop(-1) should fail");
178 if (yac_pak_trace_stop(pak, 999) == 0)
179 PUT_ERR("yac_pak_trace_stop(999) should fail");
180
181 yac_pak_delete(pak);
182 utest_clear_trace_env();
183 yac_remove_directory("test_edge_traces_4c");
184 }
185}
186
187/* Test 5: Start/stop event pairing */
188static void utest_start_stop_pairing(void) {
189
190 { // Test 5a: Start multiple events and stop in different order
191 utest_clear_trace_env();
192 setenv("YAC_PAK_TRACE", "1", 1);
193 setenv("YAC_PAK_TRACE_LEVEL", "1", 1);
194 setenv("YAC_PAK_TRACE_DIR", "test_pairing_traces", 1);
195
196 struct yac_pak *pak = yac_pak_new(MPI_COMM_SELF, 32);
197
198 int h0 = yac_pak_trace_start_ex(
199 pak, "field_a", "comp", "grid", YAC_PAK_EVENT_TYPE_PUT, -1, NULL, 0, YAC_PAK_TRACE_LEVEL_COUPLING);
200 int h1 = yac_pak_trace_start_ex(
201 pak, "field_b", "comp", "grid", YAC_PAK_EVENT_TYPE_GET, -1, NULL, 0, YAC_PAK_TRACE_LEVEL_COUPLING);
202 int h2 = yac_pak_trace_start_ex(
203 pak, "field_c", "comp", "grid", YAC_PAK_EVENT_TYPE_PUT, -1, NULL, 0, YAC_PAK_TRACE_LEVEL_COUPLING);
204
205 if (h0 < 0 || h1 < 0 || h2 < 0)
206 PUT_ERR("Could not start concurrent events");
207
208 /* Stop in different order */
209 if (yac_pak_trace_stop(pak, h1) != 0)
210 PUT_ERR(" FAIL: Could not stop event h1");
211 if (yac_pak_trace_stop(pak, h0) != 0)
212 PUT_ERR("Could not stop event h0");
213 if (yac_pak_trace_stop(pak, h2) != 0)
214 PUT_ERR("Could not stop event h2");
215
217 if (utest_count_data_lines("test_pairing_traces/comp_r0.csv") != 3)
218 PUT_ERR("wrong paired event count (expected 3)");
219
220 if (yac_pak_trace_stop(pak, h0) == 0) {
221 PUT_ERR("Stopping already-stopped handle should fail");
222 }
223
224 yac_pak_delete(pak);
225 utest_clear_trace_env();
226 yac_remove_directory("test_pairing_traces");
227 }
228}
229
230/* Test 6: Trace with tracing disabled (no-op) */
231static void utest_tracing_disabled(void) {
232
233 { // Test 6a: Tracing disabled, no PAK instance
234 utest_clear_trace_env();
235 setenv("YAC_PAK_TRACE_DIR", "test_disabled_traces", 1);
236
237 struct yac_pak *pak = NULL;
238
240 PUT_ERR("Tracing enabled when YAC_PAK_TRACE not set");
241
243 pak, "field", "comp", "grid", YAC_PAK_EVENT_TYPE_PUT, -1, NULL, 0, YAC_PAK_TRACE_LEVEL_COUPLING);
244 if (h != -1)
245 PUT_ERR("trace_start_ex should return -1 when disabled");
246
248 if (yac_file_exists("test_disabled_traces/comp_r0.csv"))
249 PUT_ERR("File created when tracing disabled");
250
251 utest_clear_trace_env();
252 }
253}
254
255/* Test 7: Pre-existing YAC_PAK_TRACE_DIR to trigger suffix-based rename */
256static void utest_existing_trace_dir(void) {
257
258 { // Test 7a: Pre-existing directory should trigger suffix-based rename
259 utest_clear_trace_env();
260 setenv("YAC_PAK_TRACE", "1", 1);
261 setenv("YAC_PAK_TRACE_LEVEL", "1", 1);
262 setenv("YAC_PAK_TRACE_DIR", "test_exist_dir", 1);
263
264 // Ensure no pre-existing directories
265 if (yac_file_exists("test_exist_dir"))
266 yac_remove_directory("test_exist_dir");
267 if (yac_file_exists("test_exist_dir_1"))
268 yac_remove_directory("test_exist_dir_1");
269
270 // Create pre-existing directory
271 mkdir("test_exist_dir", 0755);
272
273 struct yac_pak *pak = yac_pak_new(MPI_COMM_SELF, 8);
274 if (!pak) PUT_ERR("yac_pak_new failed for pre-existing dir");
275
276 double t = 1.0;
278 pak, "f", "ec", "g", YAC_PAK_EVENT_TYPE_PUT, t, t + 0.1);
280
281 if (!yac_file_exists("test_exist_dir"))
282 PUT_ERR("original dir was removed");
283 if (!yac_file_exists("test_exist_dir_1"))
284 PUT_ERR("suffix dir was not created");
285
286 yac_pak_delete(pak);
287 utest_clear_trace_env();
288 yac_remove_directory("test_exist_dir");
289 yac_remove_directory("test_exist_dir_1");
290 }
291}
292
293/* Test 8: yac_pak_unknown fallback when no component is provided */
294static void utest_unknown_component(void) {
295
296 { // Test 8a: No component provided, should fallback to yac_pak_unknown
297 utest_clear_trace_env();
298 setenv("YAC_PAK_TRACE", "1", 1);
299 setenv("YAC_PAK_TRACE_LEVEL", "1", 1);
300 setenv("YAC_PAK_TRACE_DIR", "test_unknown_comp_traces", 1);
301
302 struct yac_pak *pak = yac_pak_new(MPI_COMM_SELF, 8);
303
304 int h = yac_pak_trace_start_ex(pak, "field", NULL, "grid",
306 if (h < 0) {
307 PUT_ERR("trace_start_ex failed");
308 } else if (yac_pak_trace_stop(pak, h) != 0) {
309 PUT_ERR("trace_stop failed");
310 }
311
313
314 if (!yac_file_exists("test_unknown_comp_traces/yac_pak_unknown_r0.csv"))
315 PUT_ERR("yac_pak_unknown fallback filename not used");
316
317 yac_pak_delete(pak);
318 utest_clear_trace_env();
319 yac_remove_directory("test_unknown_comp_traces");
320 }
321}
322
323/* Test 9: Basic timer API + growth */
324static void utest_timer_api(void) {
325
326 { // Test 9a: Basic timer API usage
327 utest_clear_trace_env();
328 setenv("YAC_PAK_TRACE", "1", 1);
329 setenv("YAC_PAK_TRACE_LEVEL", "1", 1);
330 setenv("YAC_PAK_TRACE_DIR", "test_timer_api_traces", 1);
331
332 struct yac_pak *pak = yac_pak_new(MPI_COMM_SELF, 8);
333
334 int handles[9];
335 for (int i = 0; i < 9; i++) {
336 char name[16];
337 snprintf(name, sizeof(name), "timer_%d", i);
338 handles[i] = yac_pak_register(pak, name);
339 if (handles[i] < 0)
340 PUT_ERR("Could not register timer");
341 }
342
343 if (yac_pak_start(pak, handles[0]) != 0)
344 PUT_ERR("Could not start timer");
345 usleep(1000);
346 if (yac_pak_stop(pak, handles[0]) != 0)
347 PUT_ERR("Could not stop timer");
348
349 yac_pak_report(pak, NULL);
350
351 yac_pak_delete(pak);
352 utest_clear_trace_env();
353 yac_remove_directory("test_timer_api_traces");
354 }
355}
356
357/* Test 10: Multiple independent YAC PAK instances */
358static void utest_multi_instance(void) {
359
360 { // Test 10a: Two independent YAC PAK instances with different trace dirs
361 utest_clear_trace_env();
362 setenv("YAC_PAK_TRACE", "1", 1);
363 setenv("YAC_PAK_TRACE_LEVEL", "1", 1);
364 setenv("YAC_PAK_TRACE_SETUP", "1", 1);
365
366 setenv("YAC_PAK_TRACE_DIR", "test_multi_A", 1);
367 struct yac_pak *pak_a = yac_pak_new(MPI_COMM_SELF, 16);
368
369 setenv("YAC_PAK_TRACE_DIR", "test_multi_B", 1);
370 struct yac_pak *pak_b = yac_pak_new(MPI_COMM_SELF, 16);
371
373 pak_a, "temp", "atm", "atm_grid", YAC_PAK_EVENT_TYPE_PUT, 1.0, 1.1);
375 pak_a, "temp", "atm", "atm_grid", YAC_PAK_EVENT_TYPE_PUT, 2.0, 2.1);
377 pak_b, "salt", "ocn", "ocn_grid", YAC_PAK_EVENT_TYPE_GET, 3.0, 3.1);
378
379 yac_pak_flush_trace(pak_a);
380 yac_pak_flush_trace(pak_b);
381
382 if (utest_count_data_lines("test_multi_A/atm_r0.csv") != 2)
383 PUT_ERR("pak_a should have 2 events");
384 if (yac_file_exists("test_multi_A/ocn_r0.csv"))
385 PUT_ERR("pak_a dir should not contain ocn file");
386
387 if (utest_count_data_lines("test_multi_B/ocn_r0.csv") != 1)
388 PUT_ERR("pak_b should have 1 event");
389 if (yac_file_exists("test_multi_B/atm_r0.csv"))
390 PUT_ERR("pak_b dir should not contain atm file");
391
392 yac_pak_delete(pak_a);
393 yac_pak_delete(pak_b);
394
395 utest_clear_trace_env();
396 yac_remove_directory("test_multi_A");
397 yac_remove_directory("test_multi_B");
398 }
399}
400
401int main(void) {
402 MPI_Init(NULL, NULL);
403
404 utest_new_delete();
405 utest_enable_tracing();
406 utest_buffer_growth();
407 utest_edge_cases();
408 utest_start_stop_pairing();
409 utest_tracing_disabled();
410 utest_existing_trace_dir();
411 utest_unknown_component();
412 utest_timer_api();
413 utest_multi_instance();
414
415 MPI_Finalize();
416 return TEST_EXIT_CODE;
417}
int yac_pak_register(struct yac_pak *pak, char const *name)
Register a named timer.
Definition yac_pak.c:1151
int yac_pak_trace_is_enabled(struct yac_pak const *pak)
Event tracing support.
Definition yac_pak.c:905
int yac_pak_start(struct yac_pak *pak, int idx)
Start a registered timer.
Definition yac_pak.c:1162
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_report(struct yac_pak *pak, FILE *out)
Generate collective timer report.
Definition yac_pak.c:1181
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_stop(struct yac_pak *pak, int idx)
Stop a registered timer.
Definition yac_pak.c:1170
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_TRACE_LEVEL_COUPLING
PUT/GET/AGET/UPDATE/WAIT events.
Definition yac_pak.h:73
void yac_remove_directory(char const *path)
Remove a directory and its contents recursively.
Definition io_utils.c:500
int yac_file_exists(const char *filename)
Check whether a file exists.
Definition io_utils.c:394
#define TEST_EXIT_CODE
Definition tests.h:15
#define PUT_ERR(string)
Definition tests.h:10
char const * name
Definition toy_scrip.c:114