GCC Code Coverage Report


Directory: ./
File: tests/unit/02_shmem/cpuinfo_procinfo_sync_00.c
Date: 2026-12-01 10:07:21
Exec Total Coverage
Lines: 55 58 94.8%
Functions: 1 1 100.0%
Branches: 44 66 66.7%

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 #include "test_process.h"
22
23 #include "LB_comm/shmem.h"
24 #include "LB_comm/shmem_cpuinfo.h"
25 #include "LB_comm/shmem_procinfo.h"
26 #include "apis/dlb_errors.h"
27 #include "apis/dlb_types.h"
28 #include "support/error.h"
29 #include "support/mask_utils.h"
30
31 #include <sched.h>
32 #include <sys/types.h>
33 #include <unistd.h>
34 #include <stdio.h>
35 #include <sys/wait.h>
36 #include <pthread.h>
37 #include <assert.h>
38
39 // Test synchronization between procinfo and cpuinfo when the ownership changes
40
41 struct data {
42 pthread_barrier_t barrier;
43 int initialized;
44 };
45
46 1 int main( int argc, char **argv ) {
47
48 enum { SHMEM_SIZE_MULTIPLIER = 1 };
49
50 // This test needs at least room for 4 CPUs
51 enum { SYS_SIZE = 4 };
52 1 mu_init();
53 1 mu_testing_set_sys_size(SYS_SIZE);
54
55 1 pid_t pid = 42;
56 cpu_set_t mask;
57 1 mu_parse_mask("0-3", &mask);
58
59 /* Fork process */
60 1 pid_t new_pid = fork();
61
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (new_pid >= 0) {
62 /* Both parent and child execute concurrently */
63 int error;
64 struct data *shdata;
65 2 shmem_handler_t *handler = shmem_init((void**)&shdata,
66 2 &(const shmem_props_t) {
67 .size = sizeof(struct data),
68 .name = "test",
69 .key = SHMEM_KEY,
70 });
71 2 shmem_lock(handler);
72 {
73
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (!shdata->initialized) {
74 pthread_barrierattr_t attr;
75
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrierattr_init(&attr) == 0 );
76
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrierattr_setpshared(&attr, PTHREAD_PROCESS_SHARED) == 0 );
77
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrier_init(&shdata->barrier, &attr, 2) == 0 );
78
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrierattr_destroy(&attr) == 0 );
79 1 shdata->initialized = 1;
80 }
81 }
82 2 shmem_unlock(handler);
83
84 /* Parent initilizes sub-process */
85
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (new_pid > 0) {
86
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_procinfo__init(pid, 0, &mask, NULL, SHMEM_KEY,
87 SHMEM_SIZE_MULTIPLIER) == DLB_SUCCESS );
88
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__init(pid, 0, &mask, SHMEM_KEY, 0) == DLB_SUCCESS );
89 }
90
91 /* Child initilizes external */
92
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (new_pid == 0) {
93
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_procinfo_ext__init(SHMEM_KEY, SHMEM_SIZE_MULTIPLIER) == DLB_SUCCESS );
94 }
95
96 /* Both processes synchronize */
97 2 error = pthread_barrier_wait(&shdata->barrier);
98
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
2 assert(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD);
99
100 /* Child sets new mask */
101
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (new_pid == 0) {
102 cpu_set_t new_mask;
103 1 mu_parse_mask("0,1", &new_mask);
104
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_procinfo__setprocessmask(pid, &new_mask, DLB_SYNC_QUERY, NULL)
105 == DLB_SUCCESS );
106 }
107
108 /* Parent process loops until new mask is set */
109
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (new_pid > 0) {
110 int err;
111
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
2 while ((err = shmem_procinfo__polldrom(pid, NULL, &mask)) == DLB_NOUPDT)
112 1 usleep(1000);
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( err == DLB_SUCCESS );
114 }
115
116 /* Both processes synchronize */
117 2 error = pthread_barrier_wait(&shdata->barrier);
118
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
2 assert(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD);
119
120 /* Finalize shmem */
121 2 shmem_lock(handler);
122 {
123
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (shdata->initialized) {
124
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrier_destroy(&shdata->barrier) == 0 );
125 1 shdata->initialized = 0;
126 }
127 }
128 2 shmem_unlock(handler);
129 2 shmem_finalize(handler, NULL);
130
131 /* Child finalizes and exits */
132
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (new_pid == 0) {
133
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_procinfo_ext__finalize() == DLB_SUCCESS );
134 1 dlb_test__exit(EXIT_SUCCESS);
135 }
136 }
137
138 /* Wait for child process */
139 int wstatus;
140
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
2 while(wait(&wstatus) > 0) {
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!WIFEXITED(wstatus))
142 ✗ exit(EXIT_FAILURE);
143 1 int rc = WEXITSTATUS(wstatus);
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (rc != 0) {
145 ✗ printf("Child return status: %d\n", rc);
146 ✗ exit(EXIT_FAILURE);
147 }
148 }
149
150 /* Update cpuinfo with the new mask */
151 1 shmem_cpuinfo__update_ownership(pid, &mask, NULL);
152
153 /* Get new bindings */
154
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__get_thread_binding(pid, 1) == 1 );
155
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__get_thread_binding(pid, 0) == 0 );
156
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__get_thread_binding(pid, 2) == -1 );
157
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__get_thread_binding(pid, 3) == -1 );
158
159 /* Finalize sub-process */
160
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__finalize(pid, SHMEM_KEY, 0) == DLB_SUCCESS );
161
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_procinfo__finalize(pid, false, SHMEM_KEY, SHMEM_SIZE_MULTIPLIER)
162 == DLB_SUCCESS );
163
164 1 mu_finalize();
165
166 1 return 0;
167 }
168