| 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 "extra_tests.h" | ||
| 21 | #include "unique_shmem.h" | ||
| 22 | #include "test_process.h" | ||
| 23 | |||
| 24 | #include <apis/dlb.h> | ||
| 25 | #include <LB_core/spd.h> | ||
| 26 | #include "LB_comm/shmem_barrier.h" | ||
| 27 | #include "LB_comm/shmem.h" | ||
| 28 | #include "LB_core/spd.h" | ||
| 29 | #include "support/atomic.h" | ||
| 30 | #include "support/debug.h" | ||
| 31 | #include "support/mask_utils.h" | ||
| 32 | #include "support/options.h" | ||
| 33 | |||
| 34 | #include <assert.h> | ||
| 35 | #include <unistd.h> | ||
| 36 | #include <stdio.h> | ||
| 37 | #include <string.h> | ||
| 38 | #include <sys/types.h> | ||
| 39 | #include <sys/wait.h> | ||
| 40 | |||
| 41 | |||
| 42 | /* Test node barrier */ | ||
| 43 | |||
| 44 | struct data { | ||
| 45 | pthread_barrier_t barrier; | ||
| 46 | atomic_int ntimes; | ||
| 47 | }; | ||
| 48 | |||
| 49 | 4 | static shmem_handler_t* open_shmem(void **shdata) { | |
| 50 | 8 | return shmem_init(shdata, | |
| 51 | 4 | &(const shmem_props_t) { | |
| 52 | .size = sizeof(struct data), | ||
| 53 | .name = "test", | ||
| 54 | .key = SHMEM_KEY, | ||
| 55 | }); | ||
| 56 | } | ||
| 57 | |||
| 58 | /* Callback to count how many times the LeWI module has invoked it */ | ||
| 59 | static int ntimes = 0; | ||
| 60 | ✗ | static void cb_count(int num_threads, void *arg) { | |
| 61 | ✗ | ++ntimes; | |
| 62 | } | ||
| 63 | |||
| 64 | 1 | int main(int argc, char **argv) { | |
| 65 | |||
| 66 | 1 | char options[64] = "--barrier --shm-key="; | |
| 67 | 1 | strcat(options, SHMEM_KEY); | |
| 68 | |||
| 69 | 1 | int system_size = mu_get_system_size(); | |
| 70 | |||
| 71 | /* Test barrier with one process */ | ||
| 72 | { | ||
| 73 | 1 | printf("Testing barrier with one process\n"); | |
| 74 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( DLB_Init(0, 0, options) == DLB_SUCCESS ); |
| 75 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( DLB_Barrier() == DLB_SUCCESS ); |
| 76 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( DLB_PrintShmem(0, DLB_COLOR_AUTO) == DLB_SUCCESS ); |
| 77 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( DLB_Finalize() == DLB_SUCCESS ); |
| 78 | |||
| 79 | // other calls after finalize | ||
| 80 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( DLB_Barrier() == DLB_ERR_NOCOMP ); |
| 81 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( DLB_BarrierAttach() == DLB_ERR_NOCOMP ); |
| 82 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( DLB_BarrierDetach() == DLB_ERR_NOCOMP ); |
| 83 | } | ||
| 84 | |||
| 85 | /* Test that multiple attach or detach does not cause errors */ | ||
| 86 | { | ||
| 87 | 1 | printf("Testing multiple attach/detach\n"); | |
| 88 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( DLB_Init(0, 0, options) == DLB_SUCCESS ); |
| 89 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( DLB_BarrierAttach() == DLB_ERR_PERM ); |
| 90 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( shmem_barrier__exists() ); |
| 91 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( DLB_BarrierDetach() == 0 ); |
| 92 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( shmem_barrier__exists() ); |
| 93 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( DLB_BarrierDetach() == DLB_ERR_PERM ); |
| 94 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( DLB_BarrierAttach() == 1 ); |
| 95 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( shmem_barrier__exists() ); |
| 96 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( DLB_BarrierDetach() == 0 ); |
| 97 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( DLB_BarrierDetach() == DLB_ERR_PERM ); |
| 98 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( DLB_BarrierDetach() == DLB_ERR_PERM ); |
| 99 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( DLB_Finalize() == DLB_SUCCESS ); |
| 100 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( !shmem_barrier__exists() ); |
| 101 | } | ||
| 102 | |||
| 103 | /* Test barrier with N processes */ | ||
| 104 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | if (DLB_EXTRA_TESTS && system_size > 1) |
| 105 | { | ||
| 106 | 1 | printf("Testing barrier with N processes\n"); | |
| 107 | struct data *shdata; | ||
| 108 | shmem_handler_t *handler; | ||
| 109 | |||
| 110 | // Master process creates shmem and initializes barrier | ||
| 111 | 1 | handler = open_shmem((void**)&shdata); | |
| 112 | pthread_barrierattr_t attr; | ||
| 113 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( pthread_barrierattr_init(&attr) == 0 ); |
| 114 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( pthread_barrierattr_setpshared(&attr, PTHREAD_PROCESS_SHARED) == 0 ); |
| 115 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( pthread_barrier_init(&shdata->barrier, &attr, system_size-1) == 0 ); |
| 116 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( pthread_barrierattr_destroy(&attr) == 0 ); |
| 117 | |||
| 118 | // Create a child process per CPU-1 | ||
| 119 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | for(int child = 1; child < system_size; ++child) { |
| 120 | 1 | pid_t pid = fork(); | |
| 121 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | assert( pid >= 0 ); |
| 122 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (pid == 0) { |
| 123 | // All children initialize DLB, LeWI enabled for this test | ||
| 124 | char options_plus_lewi[72]; | ||
| 125 | 1 | snprintf(options_plus_lewi, 72, "--lewi %s", options); | |
| 126 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( DLB_Init(0, 0, options_plus_lewi) == DLB_SUCCESS ); |
| 127 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( DLB_CallbackSet(dlb_callback_set_num_threads, |
| 128 | (dlb_callback_t)cb_count, NULL) == DLB_SUCCESS); | ||
| 129 | |||
| 130 | // Attach to the "test" shared memory and synchronize | ||
| 131 | 1 | handler = open_shmem((void**)&shdata); | |
| 132 | 1 | int error = pthread_barrier_wait(&shdata->barrier); | |
| 133 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | assert(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD); |
| 134 | |||
| 135 | // All processes do multiple DLB_Barriers once they have initialized | ||
| 136 | 1 | printf("Child %d successfully initialized\n", child); | |
| 137 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( DLB_Barrier() == DLB_SUCCESS ); |
| 138 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( DLB_Barrier() == DLB_SUCCESS ); |
| 139 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( DLB_Barrier() == DLB_SUCCESS ); |
| 140 | 1 | printf("Child %d completed all barriers\n", child); | |
| 141 | |||
| 142 | // Accumulate the number of times that the LeWI callbacks have | ||
| 143 | // been triggered | ||
| 144 | 1 | DLB_ATOMIC_ADD(&shdata->ntimes, ntimes); | |
| 145 | 1 | error = pthread_barrier_wait(&shdata->barrier); | |
| 146 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | assert(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD); |
| 147 | |||
| 148 | // Check that the barriers have triggered the LeWI callbacks | ||
| 149 | // (if only one participant (system_size = 2), the callback is not triggered) | ||
| 150 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (system_size > 2) { |
| 151 | ✗ | assert( shdata->ntimes > 0 ); | |
| 152 | } | ||
| 153 | |||
| 154 | // Only one process prints shmem info | ||
| 155 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (child == 1) { |
| 156 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( DLB_PrintShmem(0, DLB_COLOR_AUTO) == DLB_SUCCESS ); |
| 157 | } | ||
| 158 | |||
| 159 | // Finalize DLB and "test" shared memory | ||
| 160 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( DLB_Finalize() == DLB_SUCCESS ); |
| 161 | 1 | shmem_finalize(handler, NULL); | |
| 162 | |||
| 163 | // We need to call _exit so that children don't call assert_shmem destructors | ||
| 164 | 1 | dlb_test__exit(EXIT_SUCCESS); | |
| 165 | ✗ | break; | |
| 166 | } | ||
| 167 | } | ||
| 168 | |||
| 169 | // Wait for all child processes | ||
| 170 | int wstatus; | ||
| 171 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
|
2 | while(wait(&wstatus) > 0) { |
| 172 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!WIFEXITED(wstatus)) |
| 173 | ✗ | exit(EXIT_FAILURE); | |
| 174 | 1 | int rc = WEXITSTATUS(wstatus); | |
| 175 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (rc != 0) { |
| 176 | ✗ | printf("Child return status: %d\n", rc); | |
| 177 | ✗ | exit(EXIT_FAILURE); | |
| 178 | } | ||
| 179 | } | ||
| 180 | |||
| 181 | // Master process finalizes everything | ||
| 182 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( pthread_barrier_destroy(&shdata->barrier) == 0 ); |
| 183 | 1 | shmem_finalize(handler, NULL); | |
| 184 | } | ||
| 185 | |||
| 186 | /* Test barrier with N processes where 1 of them detaches from the barrier */ | ||
| 187 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | if (DLB_EXTRA_TESTS && system_size > 1) |
| 188 | { | ||
| 189 | 1 | printf("Testing barrier with N processes and one of them detaches\n"); | |
| 190 | struct data *shdata; | ||
| 191 | shmem_handler_t *handler; | ||
| 192 | |||
| 193 | // Master process creates shmem and initializes barrier | ||
| 194 | 1 | handler = open_shmem((void**)&shdata); | |
| 195 | pthread_barrierattr_t attr; | ||
| 196 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( pthread_barrierattr_init(&attr) == 0 ); |
| 197 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( pthread_barrierattr_setpshared(&attr, PTHREAD_PROCESS_SHARED) == 0 ); |
| 198 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( pthread_barrier_init(&shdata->barrier, &attr, system_size-1) == 0 ); |
| 199 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( pthread_barrierattr_destroy(&attr) == 0 ); |
| 200 | |||
| 201 | // Create a child process per CPU-1 | ||
| 202 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | for(int child = 1; child < system_size; ++child) { |
| 203 | 1 | pid_t pid = fork(); | |
| 204 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | assert( pid >= 0 ); |
| 205 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (pid == 0) { |
| 206 | // All children initialize DLB | ||
| 207 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( DLB_Init(0, 0, options) == DLB_SUCCESS ); |
| 208 | |||
| 209 | // Child 1 does DLB_BarrierDetach | ||
| 210 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (child == 1) { |
| 211 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( DLB_BarrierDetach() >= 0 ); |
| 212 | } | ||
| 213 | |||
| 214 | // Attach to the "test" shared memory and synchronize | ||
| 215 | 1 | handler = open_shmem((void**)&shdata); | |
| 216 | 1 | int error = pthread_barrier_wait(&shdata->barrier); | |
| 217 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | assert(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD); |
| 218 | |||
| 219 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (child != 1) { |
| 220 | // All other processes do multiple DLB_Barriers once they have initialized | ||
| 221 | ✗ | assert( DLB_Barrier() == DLB_SUCCESS ); | |
| 222 | ✗ | assert( DLB_Barrier() == DLB_SUCCESS ); | |
| 223 | ✗ | assert( DLB_Barrier() == DLB_SUCCESS ); | |
| 224 | } | ||
| 225 | |||
| 226 | // Only one process prints shmem info | ||
| 227 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (child == 2) { |
| 228 | ✗ | assert( DLB_PrintShmem(0, DLB_COLOR_AUTO) == DLB_SUCCESS ); | |
| 229 | } | ||
| 230 | |||
| 231 | // Finalize DLB and "test" shared memory | ||
| 232 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( DLB_Finalize() == DLB_SUCCESS ); |
| 233 | 1 | shmem_finalize(handler, NULL); | |
| 234 | |||
| 235 | // We need to call _exit so that children don't call assert_shmem destructors | ||
| 236 | 1 | dlb_test__exit(EXIT_SUCCESS); | |
| 237 | ✗ | break; | |
| 238 | } | ||
| 239 | } | ||
| 240 | |||
| 241 | // Wait for all child processes | ||
| 242 | int wstatus; | ||
| 243 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
|
2 | while(wait(&wstatus) > 0) { |
| 244 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!WIFEXITED(wstatus)) |
| 245 | ✗ | exit(EXIT_FAILURE); | |
| 246 | 1 | int rc = WEXITSTATUS(wstatus); | |
| 247 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (rc != 0) { |
| 248 | ✗ | printf("Child return status: %d\n", rc); | |
| 249 | ✗ | exit(EXIT_FAILURE); | |
| 250 | } | ||
| 251 | } | ||
| 252 | |||
| 253 | // Master process finalizes everything | ||
| 254 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( pthread_barrier_destroy(&shdata->barrier) == 0 ); |
| 255 | 1 | shmem_finalize(handler, NULL); | |
| 256 | } | ||
| 257 | |||
| 258 | 1 | return EXIT_SUCCESS; | |
| 259 | } | ||
| 260 |