GCC Code Coverage Report


Directory: ./
File: tests/unit/05_api/api_talp_versions_00.c
Date: 2026-12-01 10:07:21
Exec Total Coverage
Lines: 62 81 76.5%
Functions: 8 8 100.0%
Branches: 12 46 26.1%

Line Branch Exec Source
1 /*********************************************************************************/
2 /* Copyright 2009-2026 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 "talp/talp_output.h"
24 #include "talp/talp_types.h"
25
26 #include <ftw.h>
27 #include <limits.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 // Check versions of TALP public structs
34
35
36 // NOTE: uses local variables `buf` and `off` for convenience.
37 #define APPEND(...) \
38 do { off += (size_t)snprintf(buf + off, sizeof(buf) - off, __VA_ARGS__); } while (0)
39
40 enum { GOLDEN_BUF_SIZE = 4096 };
41
42
43 5 static char* slurp(const char *path)
44 {
45 5 FILE *f = fopen(path, "rb");
46
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (!f) return NULL;
47
48 5 fseek(f, 0, SEEK_END);
49 5 long n = ftell(f);
50 5 rewind(f);
51
52 5 char *s = malloc((size_t)n + 1);
53
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (s) {
54 5 size_t i = fread(s, 1, (size_t)n, f);
55 5 s[i] = '\0';
56 }
57 5 fclose(f);
58
59 5 return s;
60 }
61
62 /* Check that `text` is equal to `golden`, otherwise conditionally updates `path`:
63 * - If DLB_ABI_UPDATE is '1' or 'y', add new golden files if needed but does not overwrite.
64 * - If DLB_ABI_UPDATE is 'overwrite', write files if they differ.
65 */
66 4 static int check_golden_impl(const char *path, const char *golden, const char *what, const char *text)
67 {
68 4 bool exists = golden != NULL;
69
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 bool same = exists && strcmp(golden, text) == 0;
70
71
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (same) {
72 4 return 0;
73 }
74
75 ✗ const char *update_env = getenv("DLB_ABI_UPDATE");
76 ✗ bool update = update_env && (update_env[0] == '1' || update_env[0] == 'y');
77 ✗ bool overwrite = update_env && !update && strcmp(update_env, "overwrite") == 0;
78
79 ✗ if (exists && !overwrite) {
80 ✗ fprintf(stderr,
81 "%s: %s already exists and differs.\n"
82 "If its version was already released, bump the version instead.\n"
83 "If this version is still unreleased, rerun with DLB_ABI_UPDATE=overwrite.\n",
84 what, path);
85 ✗ return 1;
86 }
87
88 ✗ if (update || overwrite) {
89 ✗ FILE *f = fopen(path, "wb");
90 ✗ if (!f || fputs(text, f) == EOF) { perror(path); return 1; }
91 ✗ fclose(f);
92 ✗ printf("wrote %s\n", path);
93 ✗ return 0;
94 }
95
96 ✗ if (golden) {
97 ✗ fprintf(stderr, "%s output changed without bumping its version\n"
98 "--- expected (%s)\n"
99 "%s\n"
100 "--- current\n"
101 "%s\n",
102 what, path, golden, text);
103 } else {
104 ✗ fprintf(stderr, "%s: no snapshot. Run with DLB_ABI_UPDATE=1 and commit %s\n", what, path);
105 }
106
107 ✗ return 1;
108 }
109
110 4 static int check_golden(const char *dir, const char *type, int version,
111 const char *arch, const char *ext, const char *text)
112 {
113 char path[PATH_MAX];
114
4/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 1 times.
4 snprintf(path, sizeof(path),
115 "%s/%s.v%d%s%s.%s",
116 dir, type, version,
117 arch ? "." : "",
118 arch ? arch : "",
119 ext);
120
121 4 char *golden = slurp(path);
122
123 4 int ret = check_golden_impl(path, golden, type, text);
124
125 4 free(golden);
126
127 4 return ret;
128 }
129
130 1 static int check_dlb_monitor_t(const char *dir, const char *arch) {
131
132 char buf[GOLDEN_BUF_SIZE];
133 1 size_t off = 0;
134
135 1 APPEND("version %d\nsizeof %zu\nalignof %zu\n",
136 (int)DLB_MONITOR_T_VERSION, sizeof(dlb_monitor_t), _Alignof(dlb_monitor_t));
137
138 #define DUMP(name, c_type, ...) \
139 APPEND("field %s %s offset %zu size %zu\n", #name, #c_type, \
140 offsetof(dlb_monitor_t, name), sizeof(((dlb_monitor_t *)0)->name));
141 1 FOR_DLB_MONITOR_FIELDS(DUMP)
142 #undef DUMP
143
144 1 return check_golden(dir, "dlb_monitor_t", DLB_MONITOR_T_VERSION, arch, "layout", buf);
145 }
146
147 1 static int check_dlb_pop_metrics_t(const char *dir, const char *arch) {
148
149 char buf[GOLDEN_BUF_SIZE];
150 1 size_t off = 0;
151
152 1 APPEND("version %d\nsizeof %zu\nalignof %zu\n",
153 (int)DLB_POP_METRICS_T_VERSION, sizeof(dlb_pop_metrics_t),
154 _Alignof(dlb_pop_metrics_t));
155
156 #define DUMP(name, c_type, ...) \
157 APPEND("field %s %s offset %zu size %zu\n", #name, #c_type, \
158 offsetof(dlb_pop_metrics_t, name), sizeof(((dlb_pop_metrics_t *)0)->name));
159 1 FOR_DLB_POP_METRICS_FIELDS(DUMP, DUMP)
160 #undef DUMP
161
162 1 return check_golden(dir, "dlb_pop_metrics_t", DLB_POP_METRICS_T_VERSION, arch, "layout", buf);
163 }
164
165 1 static int check_dlb_node_metrics_t(const char *dir, const char *arch) {
166
167 char buf[GOLDEN_BUF_SIZE];
168 1 size_t off = 0;
169
170 1 APPEND("version %d\nsizeof %zu\nalignof %zu\n",
171 (int)DLB_NODE_METRICS_T_VERSION, sizeof(dlb_node_metrics_t),
172 _Alignof(dlb_node_metrics_t));
173
174 #define DUMP(name, c_type, ...) \
175 APPEND("field %s %s offset %zu size %zu\n", #name, #c_type, \
176 offsetof(dlb_node_metrics_t, name), sizeof(((dlb_node_metrics_t *)0)->name));
177 1 FOR_DLB_NODE_METRICS_FIELDS(DUMP)
178 #undef DUMP
179
180 1 return check_golden(dir, "dlb_node_metrics_t", DLB_NODE_METRICS_T_VERSION, arch, "layout", buf);
181 }
182
183 1 static int check_json_output(const char *dir) {
184
185 1 const dlb_pop_metrics_t metrics = { .name = "<Name>" };
186 1 talp_output_record_pop_metrics(&metrics);
187
188 1 const process_record_t process_record = {0};
189 1 talp_output_record_process("<Name>", &process_record, 1);
190
191 1 talp_output_testing_use_fixed_metadata(true);
192
193 char json_path[PATH_MAX];
194 1 tmpdir_path(json_path, sizeof(json_path), "talp.json");
195
196 1 bool no_partial_output = false;
197 1 talp_output_finalize(json_path, no_partial_output);
198
199 1 char *text = slurp(json_path);
200
201 1 int rc = check_golden(dir, "talp_output", TALP_OUTPUT_JSON_SCHEMA_VERSION, NULL, "json", text);
202
203 1 free(text);
204
205 1 return rc;
206 }
207
208 1 int main(int argc, char **argv) {
209
210 /* This test compares the ABI library's golden files against the current
211 * build. The golden files capture the x86-64 Linux ABI, so the test must
212 * be skipped on other architectures.
213 *
214 * Keep the architecture check outside the preprocessor macros to avoid
215 * compiler warnings (e.g. unreachable code or unused static functions)
216 * that may become errors when building with -Wall. */
217 #if !defined(__x86_64__) || !defined(__linux__)
218 const char *ABI_ARCH = NULL;
219 #else
220 1 const char *ABI_ARCH = "x86_64";
221 #endif
222
223
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!ABI_ARCH) {
224 /* Skip RC convention */
225 ✗ return 77;
226 }
227
228 const char *dir;
229
230
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (argc > 1) {
231 /* Meson passes dir by argument */
232 ✗ dir = argv[1];
233
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 } else if ((dir = getenv("DLB_ABI_DIR")) != NULL) {
234 /* Automake sets this */
235 } else {
236 ✗ dir = "tests/abi";
237 }
238
239 1 int err = 0;
240
241 1 err += check_dlb_monitor_t(dir, ABI_ARCH);
242 1 err += check_dlb_pop_metrics_t(dir, ABI_ARCH);
243 1 err += check_dlb_node_metrics_t(dir, ABI_ARCH);
244
245 1 err += check_json_output(dir);
246
247 1 return err;
248 }
249