GCC Code Coverage Report


Directory: ./
File: tests/unit/00_support/options_00.c
Date: 2026-12-01 10:07:21
Exec Total Coverage
Lines: 163 163 100.0%
Functions: 1 1 100.0%
Branches: 87 174 50.0%

Line Branch Exec Source
1 /*********************************************************************************/
2 /* Copyright 2009-2021 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 "apis/dlb_errors.h"
21 #include "support/types.h"
22 #include "support/options.h"
23 #include "LB_core/spd.h"
24
25 #include <limits.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <assert.h>
30
31 1 int main( int argc, char **argv ) {
32
33 options_t options_1, options_2;
34
35 // Check initialization equivalency
36 1 const char *dlb_args = "--lewi --no-drom this_should_be_ignored --verbose=api ";
37 1 options_init(&options_1, dlb_args); // options_1 initialized with dlb_args
38 1 setenv("DLB_ARGS", "--lewi --non-existent-flag --no-drom --verbose=api", 1);
39 1 options_init(&options_2, NULL); // options_2 initialized with env. vars
40
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.lewi == options_2.lewi);
41
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.drom == options_2.drom);
42
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.lewi_keep_cpu_on_blocking_call == options_2.lewi_keep_cpu_on_blocking_call);
43
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.verbose == options_2.verbose);
44
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.verbose_fmt == options_2.verbose_fmt);
45
46 // Check dlb_args precedence
47 1 setenv("DLB_ARGS", "--no-lewi", 1);
48 1 options_init(&options_1, "--lewi");
49
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.lewi == false);
50
51 // Check that different options are parsed using both methods
52 1 setenv("DLB_ARGS", "--drom", 1);
53 1 options_init(&options_1, "--lewi");
54
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.lewi == true);
55
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.drom == true);
56
57 1 unsetenv("DLB_ARGS");
58
59 // Check some values
60 1 options_init(&options_1, "--lewi=yes --drom=1 --lewi-affinity=nearby-only");
61
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.lewi == true);
62
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.drom == true);
63
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.lewi_affinity == LEWI_AFFINITY_NEARBY_ONLY);
64 1 options_init(&options_1, "--drom");
65
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.drom == true);
66
67 // Check option overwrite
68 1 options_init(&options_1, "--drom=1 --drom=0");
69
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.drom == false);
70
71 // Check option overwrite (werror because both arguments need to be cleaned)
72 1 options_init(&options_1, "--debug-opts=werror --drom=1 --drom=0");
73
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.drom == false);
74 1 options_init(&options_1, " --debug-opts=werror --drom=1 --drom=0 ");
75
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.drom == false);
76
77 // Check different forms of parsing a boolean
78 1 options_init(&options_1, "--lewi");
79
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.lewi == true);
80 1 options_init(&options_1, "--no-lewi");
81
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.lewi == false);
82 1 options_init(&options_1, "--lewi=yes");
83
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.lewi == true);
84 1 options_init(&options_1, "--lewi=no");
85
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.lewi == false);
86
87 // Check multi-value
88 1 options_init(&options_1, "--verbose=drom:ompt:api");
89
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.verbose == (VB_DROM | VB_OMPT | VB_API));
90 1 options_init(&options_1, "--verbose=drom,ompt,api");
91
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.verbose == (VB_DROM | VB_OMPT | VB_API));
92
93 // Check TALP components
94 1 options_init(&options_1, "");
95
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.talp == TALP_COMPONENT_NONE);
96 1 options_init(&options_1, "--talp=no");
97
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.talp == TALP_COMPONENT_NONE);
98 1 options_init(&options_1, "--talp=none");
99
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.talp == TALP_COMPONENT_NONE);
100 1 options_init(&options_1, "--talp");
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.talp == TALP_COMPONENT_DEFAULT);
102 1 options_init(&options_1, "--talp=default");
103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.talp == TALP_COMPONENT_DEFAULT);
104 1 options_init(&options_1, "--talp=openmp");
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.talp == TALP_COMPONENT_OPENMP);
106 1 options_init(&options_1, "--talp=openmp:gpu:hwc");
107
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.talp == (TALP_COMPONENT_OPENMP|TALP_COMPONENT_HWC|TALP_COMPONENT_GPU));
108
109 // Test deprecated TALP variables that set non-deprecated fields
110 1 options_init(&options_1, "--talp-openmp");
111
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.talp == TALP_COMPONENT_OPENMP);
112 1 options_init(&options_1, "--talp --talp-openmp");
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.talp == (TALP_COMPONENT_DEFAULT|TALP_COMPONENT_OPENMP));
114 1 options_init(&options_1, "--talp-papi");
115
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.talp == TALP_COMPONENT_HWC);
116 1 options_init(&options_1, "--plugin=cupti");
117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.talp == TALP_COMPONENT_GPU);
118 1 options_init(&options_1, "--talp-openmp --talp-papi --plugin=rocprofiler-sdk");
119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.talp == (TALP_COMPONENT_OPENMP|TALP_COMPONENT_HWC|TALP_COMPONENT_GPU));
120
121 // Deprecated and unused variables must not fail, nor change any value
122 1 memset(&options_1, 0, sizeof(options_t));
123 1 memset(&options_2, 0, sizeof(options_t));
124 1 options_init(&options_1, "--lewi-mpi");
125 1 options_init(&options_2, NULL);
126
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(memcmp(&options_1, &options_2, sizeof(options_t)) == 0);
127
128 // Check setter and getter
129 char value[MAX_OPTION_LENGTH];
130 1 options_init(&options_1, "--lewi-mpi-calls=barrier --shm-key=key");
131
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.lewi_mpi_calls == MPISET_BARRIER);
132 1 options_get_variable(&options_1, "--lewi-mpi-calls", value);
133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(strcasecmp(value, "barrier") == 0);
134 1 options_set_variable(&options_1, "--lewi-mpi-calls", "all");
135
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.lewi_mpi_calls == MPISET_ALL);
136 1 options_get_variable(&options_1, "--lewi-mpi-calls", value);
137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(strcasecmp(value, "all") == 0);
138 1 options_get_variable(&options_1, "--shm-key", value);
139
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(strcasecmp(value, "key") == 0);
140 1 int error_readonly_var = options_set_variable(&options_1, "--shm-key", "new_key");
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(error_readonly_var == DLB_ERR_PERM);
142
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(strcasecmp(value, "key") == 0);
143 1 options_get_variable(&options_1,"--lewi-keep-one-cpu", value);
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(strcasecmp(value, "no") == 0);
145 1 options_set_variable(&options_1,"--lewi-keep-one-cpu", "1");
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.lewi_keep_cpu_on_blocking_call == true);
147 1 options_get_variable(&options_1,"--lewi-keep-one-cpu", value);
148
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(strcasecmp(value, "yes") == 0);
149 1 int error_unexisting_var = options_set_variable(&options_1, "FAKEVAR", "fakevalue");
150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(error_unexisting_var);
151 1 value[0] = '\0';
152 1 error_unexisting_var = options_get_variable(&options_1, "FAKEVAR", value);
153
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(error_unexisting_var == DLB_ERR_NOENT);
154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(strcasecmp(value, "") == 0);
155
156 // Print variables
157 1 options_init(&options_1, "");
158 1 options_print_variables(&options_1, /* extended */ false);
159 1 options_print_variables(&options_1, /* extended */ true);
160
161 // Unknown variables are silently ignored
162 //options_init(&options_1, "--polic");
163
164 // Bad format options trigger a SIGABRT
165 //options_init(&options_1, "--policy");
166 //options_init(&options_1, "--policy=");
167
168 // TODO: some variables are still not being checked
169 1 options_init(&options_1, "--mode=async");
170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.mode == MODE_ASYNC);
171
172 // Unset all variables and check that default values are preserved
173 1 options_init(&options_1, NULL);
174 1 options_init(&options_2, "");
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.verbose_fmt == options_2.verbose_fmt);
176
177 // Test particular options for --verbose and --instrument
178 1 options_init(&options_1, "--instrument");
179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.instrument == INST_ALL);
180 1 options_init(&options_1, "--instrument=yes");
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.instrument == INST_ALL);
182 1 options_init(&options_1, "--verbose");
183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.verbose == VB_ALL);
184 1 options_init(&options_1, "--verbose=yes");
185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.verbose == VB_ALL);
186
187 // Test --talp-output-file
188 1 options_init(&options_1, "");
189
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.talp_output_file == NULL);
190 1 options_finalize(&options_1);
191 1 options_init(&options_1, "--talp-output-file=myfile.txt");
192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(strcmp(options_1.talp_output_file, "myfile.txt") == 0);
193 1 options_finalize(&options_1);
194
195 // bug: "--lewi --lewi-greedy" is well parsed but "--lewi-greedy --lewi" is not.
196 1 options_init(&options_1, "--lewi --lewi-greedy");
197
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 assert(options_1.lewi && options_1.lewi_greedy);
198 1 options_init(&options_1, "--lewi-greedy --lewi");
199
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 assert(options_1.lewi && options_1.lewi_greedy);
200
201 // bug: "--option=arg" is well parsed but "=arg" remains in the argument
202 // (werror will cause "Unrecognized options" warning to fail)
203 1 options_init(&options_1, "--debug-opts=werror");
204
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.debug_opts & DBG_WERROR);
205
206 // bug: "--option=" should not cause fatal errors, just warnings,
207 // and the value must be the default
208 1 options_init(&options_1, "--lewi-mpi-calls=");
209 1 options_init(&options_2, "");
210
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(options_1.lewi_mpi_calls == options_2.lewi_mpi_calls);
211
212 // Test individual entry parsing
213 bool lewi;
214 bool lewi_mpi;
215 int barrier_id;
216 1 char *shm_key = malloc(sizeof(char)*MAX_OPTION_LENGTH);
217 1 char *talp_file = malloc(sizeof(char)*PATH_MAX);
218 verbose_opts_t vb_opts;
219 verbose_fmt_t vb_fmt;
220 omptool_opts_t ompt_opts;
221 instrument_items_t instr_items;
222 mpi_set_t mpiset;
223 lewi_affinity_t aff;
224 interaction_mode_t mode;
225 talp_summary_t talp_sum;
226 talp_model_t talp_model;
227 // 1) without thread_spd->options
228 1 setenv("DLB_ARGS", "--lewi --lewi-mpi --barrier-id=3 --shm-key=custom_key"
229 " --verbose=talp --lewi-ompt=borrow", 1);
230
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 options_parse_entry("--lewi", &lewi); assert(lewi == true);
231
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 options_parse_entry("--lewi-mpi", &lewi_mpi); assert(lewi_mpi == false);
232
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 options_parse_entry("--barrier-id", &barrier_id); assert(barrier_id == 3);
233
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 options_parse_entry("--shm-key", shm_key); assert(strcmp(shm_key, "custom_key") == 0);
234
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 options_parse_entry("--verbose", &vb_opts); assert(vb_opts == VB_TALP);
235
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 options_parse_entry("--lewi-ompt", &ompt_opts); assert(ompt_opts == OMPTOOL_OPTS_BORROW);
236 // other default values
237
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 options_parse_entry("--verbose-format", &vb_fmt); assert(vb_fmt == (VBF_NODE|VBF_SPID));
238
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 options_parse_entry("--instrument", &instr_items); assert(instr_items == INST_ALL);
239
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 options_parse_entry("--lewi-mpi-calls", &mpiset); assert(mpiset == MPISET_ALL);
240
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 options_parse_entry("--lewi-affinity", &aff); assert(aff == LEWI_AFFINITY_AUTO);
241
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 options_parse_entry("--mode", &mode); assert(mode == MODE_POLLING);
242
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 options_parse_entry("--talp-summary", &talp_sum); assert(talp_sum == SUMMARY_POP_METRICS);
243
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 options_parse_entry("--talp-model", &talp_model); assert(talp_model == TALP_MODEL_HYBRID_V2);
244 // 2) with existing thread_spd
245 1 spd_enter_dlb(NULL);
246 1 options_init(&thread_spd->options, NULL);
247 1 thread_spd->dlb_initialized = true;
248 1 unsetenv("DLB_ARGS");
249
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 options_parse_entry("--lewi", &lewi); assert(lewi == true);
250
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 options_parse_entry("--lewi-mpi", &lewi_mpi); assert(lewi_mpi == false);
251
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 options_parse_entry("--barrier-id", &barrier_id); assert(barrier_id == 3);
252
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 options_parse_entry("--shm-key", shm_key); assert(strcmp(shm_key, "custom_key") == 0);
253
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 options_parse_entry("--verbose", &vb_opts); assert(vb_opts == VB_TALP);
254
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 options_parse_entry("--lewi-ompt", &ompt_opts); assert(ompt_opts == OMPTOOL_OPTS_BORROW);
255 // other default values
256
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 options_parse_entry("--verbose-format", &vb_fmt); assert(vb_fmt == (VBF_NODE|VBF_SPID));
257
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 options_parse_entry("--instrument", &instr_items); assert(instr_items == INST_ALL);
258
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 options_parse_entry("--lewi-mpi-calls", &mpiset); assert(mpiset == MPISET_ALL);
259
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 options_parse_entry("--lewi-affinity", &aff); assert(aff == LEWI_AFFINITY_AUTO);
260
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 options_parse_entry("--mode", &mode); assert(mode == MODE_POLLING);
261
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 options_parse_entry("--talp-summary", &talp_sum); assert(talp_sum == SUMMARY_POP_METRICS);
262
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 options_parse_entry("--talp-model", &talp_model); assert(talp_model == TALP_MODEL_HYBRID_V2);
263 1 free(shm_key);
264 1 free(talp_file);
265
266 1 return 0;
267 }
268