GCC Code Coverage Report


Directory: ./
File: tests/unit/05_api/api_drom_00.c
Date: 2026-11-30 15:03:27
Exec Total Coverage
Lines: 46 50 92.0%
Functions: 2 2 100.0%
Branches: 9 22 40.9%

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 "unique_shmem.h"
21
22 #include <apis/dlb_drom.h>
23 #include <LB_core/spd.h>
24
25 #include <sched.h>
26 #include <assert.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31
32 /* Basic DROM init / pre / post / finalize within 1 process */
33
34 2 static void test(const cpu_set_t *mask, char ***env) {
35 2 pid_t pid = getpid();
36
37
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( DLB_DROM_Attach() == DLB_SUCCESS );
38
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( DLB_DROM_PreInit(pid, mask, DLB_STEAL_CPUS, env) == DLB_SUCCESS );
39
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( DLB_DROM_Detach() == DLB_SUCCESS );
40
41 /* Test that the spd has the proper shmem key
42 * to check that DLB_ARGS has not been overwritten
43 */
44 2 const subprocess_descriptor_t** spds = spd_get_spds();
45 2 const subprocess_descriptor_t** spd = spds;
46
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 while (*spd) {
47 ✗ if ((*spd)->id == pid) {
48 ✗ assert( strcmp((*spd)->options.shm_key, SHMEM_KEY) == 0 );
49 ✗ break;
50 }
51 }
52 2 free(spds);
53
54
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( DLB_DROM_Attach() == DLB_SUCCESS );
55
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( DLB_DROM_PostFinalize(pid, DLB_DROM_FLAGS_NONE) == DLB_SUCCESS );
56
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( DLB_DROM_Detach() == DLB_SUCCESS );
57 2 }
58
59 1 int main(int argc, char **argv) {
60 /* Options */
61 1 char options[64] = "--shm-key=";
62 1 strcat(options, SHMEM_KEY);
63
64 /* Modify DLB_ARGS to include options */
65 1 const char *dlb_args_env = getenv("DLB_ARGS");
66
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (dlb_args_env) {
67 1 size_t len = strlen(dlb_args_env) + 1 + strlen(options) + 1;
68 1 char *new_dlb_args = malloc(sizeof(char)*len);
69 1 sprintf(new_dlb_args, "%s %s", dlb_args_env, options);
70 1 setenv("DLB_ARGS", new_dlb_args, 1);
71 1 free(new_dlb_args);
72 } else {
73 ✗ setenv("DLB_ARGS", options, 1);
74 }
75
76 /* Process mask is [0] */
77 cpu_set_t mask;
78 1 CPU_ZERO(&mask);
79
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 CPU_SET(0, &mask);
80
81 /* OMP_NUM_THREADS = 4 and it will have to be reduced to 1 because of the mask */
82 1 setenv("OMP_NUM_THREADS", "4", 1);
83
84 /* Fork with CURRENT environment */
85 1 test(&mask, NULL);
86
87 /* Re-set the original environment variables */
88 1 setenv("DLB_ARGS", options, 1);
89 1 setenv("OMP_NUM_THREADS", "4", 1);
90
91 /* Fork with CUSTOM environment */
92 1 char **new_environ = malloc(3*sizeof(char*)); // 2 variables + NULL
93 // OMP_NUM_THREADS
94 1 char var1[32] = "OMP_NUM_THREADS=";
95 1 strcat(var1, getenv("OMP_NUM_THREADS"));
96 1 const size_t var1len = strlen(var1) + 1;
97 1 new_environ[0] = malloc(var1len);
98 1 snprintf(new_environ[0], var1len, "%s", var1);
99 // DLB_ARGS
100 1 char var2[64] = "DLB_ARGS=";
101 1 strcat(var2, getenv("DLB_ARGS"));
102 1 const size_t var2len = strlen(var2) + 1;
103 1 new_environ[1] = malloc(var2len);
104 1 snprintf(new_environ[1], var2len, "%s", var2);
105 //
106 1 new_environ[2] = NULL;
107 1 test(&mask, &new_environ);
108 1 free(new_environ[0]);
109 1 free(new_environ[1]);
110 1 free(new_environ);
111
112 1 return 0;
113 }
114