| 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 "LB_comm/shmem.h" | ||
| 23 | #include "LB_comm/shmem_procinfo.h" | ||
| 24 | #include "apis/dlb_errors.h" | ||
| 25 | #include "support/mask_utils.h" | ||
| 26 | |||
| 27 | #include <sched.h> | ||
| 28 | #include <sys/types.h> | ||
| 29 | #include <unistd.h> | ||
| 30 | #include <stdio.h> | ||
| 31 | #include <assert.h> | ||
| 32 | |||
| 33 | // Basic checks with 2+ sub-processes | ||
| 34 | |||
| 35 | 1 | int main( int argc, char **argv ) { | |
| 36 | |||
| 37 | enum { SHMEM_SIZE_MULTIPLIER = 1 }; | ||
| 38 | |||
| 39 | 1 | pid_t pid = getpid(); | |
| 40 | 1 | int num_cpus = sysconf(_SC_NPROCESSORS_ONLN); | |
| 41 | cpu_set_t process_mask; | ||
| 42 | 1 | sched_getaffinity(0, sizeof(cpu_set_t), &process_mask); | |
| 43 | |||
| 44 | // Check permission error with two subprocesses sharing mask | ||
| 45 | { | ||
| 46 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( shmem_procinfo__init(pid, 0, &process_mask, NULL, SHMEM_KEY, |
| 47 | SHMEM_SIZE_MULTIPLIER) == DLB_SUCCESS ); | ||
| 48 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (num_cpus > 1) { |
| 49 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( shmem_procinfo__init(pid+1, 0, &process_mask, NULL, SHMEM_KEY, |
| 50 | SHMEM_SIZE_MULTIPLIER) == DLB_ERR_PERM ); | ||
| 51 | } | ||
| 52 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( shmem_procinfo__finalize(pid, false, SHMEM_KEY, SHMEM_SIZE_MULTIPLIER) |
| 53 | == DLB_SUCCESS ); | ||
| 54 | |||
| 55 | // Check that the shared memory has been finalized | ||
| 56 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( shmem_procinfo__getprocessmask(pid, NULL, DLB_SYNC_QUERY) == DLB_ERR_NOSHMEM ); |
| 57 | } | ||
| 58 | |||
| 59 | // Check shared memory capacity | ||
| 60 | { | ||
| 61 | // This loop should completely fill the shared memory | ||
| 62 | // WARNING: this test only works if proces_mask is 0..N-1 | ||
| 63 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | if (CPU_COUNT(&process_mask) == mu_get_system_size()) { |
| 64 | int cpuid; | ||
| 65 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | for (cpuid=0; cpuid<num_cpus; ++cpuid) { |
| 66 | 2 | CPU_ZERO(&process_mask); | |
| 67 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | CPU_SET(cpuid, &process_mask); |
| 68 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | assert( shmem_procinfo__init(pid+cpuid, 0, &process_mask, NULL, SHMEM_KEY, |
| 69 | SHMEM_SIZE_MULTIPLIER) == DLB_SUCCESS ); | ||
| 70 | } | ||
| 71 | |||
| 72 | // Another initialization should return error | ||
| 73 | 1 | CPU_ZERO(&process_mask); | |
| 74 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( shmem_procinfo__init(pid+cpuid, 0, &process_mask, NULL, SHMEM_KEY, |
| 75 | SHMEM_SIZE_MULTIPLIER) == DLB_ERR_NOMEM ); | ||
| 76 | |||
| 77 | // Finalize all | ||
| 78 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | for (cpuid=0; cpuid<num_cpus; ++cpuid) { |
| 79 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | assert( shmem_procinfo__finalize(pid+cpuid, false, SHMEM_KEY, |
| 80 | SHMEM_SIZE_MULTIPLIER) == DLB_SUCCESS ); | ||
| 81 | } | ||
| 82 | |||
| 83 | // Check that the shared memory has been finalized | ||
| 84 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( shmem_procinfo__getprocessmask(pid, NULL, DLB_SYNC_QUERY) == DLB_ERR_NOSHMEM ); |
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | // Check that each subprocess finalizes its own part of the shared memory | ||
| 89 | { | ||
| 90 | // Initialize as many subprocess as number of CPUs | ||
| 91 | int i; | ||
| 92 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | for (i=0; i<num_cpus; ++i) { |
| 93 | 2 | CPU_ZERO(&process_mask); | |
| 94 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | CPU_SET(i, &process_mask); |
| 95 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | assert( shmem_procinfo__init(i+1, 0, &process_mask, NULL, SHMEM_KEY, |
| 96 | SHMEM_SIZE_MULTIPLIER) == DLB_SUCCESS ); | ||
| 97 | } | ||
| 98 | |||
| 99 | // subprocess 1 tries to finalize as many times as number of CPUs | ||
| 100 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( shmem_procinfo__finalize(1, false, SHMEM_KEY, SHMEM_SIZE_MULTIPLIER) |
| 101 | == DLB_SUCCESS ); | ||
| 102 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | for (i=1; i<num_cpus; ++i) { |
| 103 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( shmem_procinfo__finalize(1, false, SHMEM_KEY, SHMEM_SIZE_MULTIPLIER) |
| 104 | == DLB_ERR_NOPROC ); | ||
| 105 | } | ||
| 106 | |||
| 107 | // rest of subprocesses should finalize correctly | ||
| 108 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | for (i=1; i<num_cpus; ++i) { |
| 109 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( shmem_procinfo__finalize(i+1, false, SHMEM_KEY, SHMEM_SIZE_MULTIPLIER) |
| 110 | == DLB_SUCCESS ); | ||
| 111 | } | ||
| 112 | |||
| 113 | // Check that the shared memory has been finalized | ||
| 114 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( shmem_procinfo__getprocessmask(pid, NULL, DLB_SYNC_QUERY) == DLB_ERR_NOSHMEM ); |
| 115 | } | ||
| 116 | |||
| 117 | // Change CPU ownership | ||
| 118 | { | ||
| 119 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (num_cpus >= 4) { |
| 120 | ✗ | dlb_drom_flags_t flags = DLB_DROM_FLAGS_NONE; | |
| 121 | cpu_set_t p1_mask, p2_mask, mask; | ||
| 122 | ✗ | mu_parse_mask("0-1", &p1_mask); | |
| 123 | ✗ | mu_parse_mask("2-3", &p2_mask); | |
| 124 | |||
| 125 | ✗ | assert( shmem_procinfo__init(111, 0, &p1_mask, NULL, SHMEM_KEY, | |
| 126 | SHMEM_SIZE_MULTIPLIER) == DLB_SUCCESS ); | ||
| 127 | ✗ | assert( shmem_procinfo__init(222, 0, &p2_mask, NULL, SHMEM_KEY, | |
| 128 | SHMEM_SIZE_MULTIPLIER) == DLB_SUCCESS ); | ||
| 129 | |||
| 130 | // remove CPU 1 from subprocess 1 | ||
| 131 | ✗ | mu_parse_mask("0", &mask); | |
| 132 | cpu_set_t free_mask; | ||
| 133 | ✗ | assert( shmem_procinfo__setprocessmask(111, &mask, flags, &free_mask) == DLB_SUCCESS ); | |
| 134 | |||
| 135 | // check the free mask | ||
| 136 | ✗ | assert( CPU_COUNT(&free_mask) == 1 ); | |
| 137 | ✗ | assert( CPU_ISSET(1, &free_mask) && !CPU_ISSET(0, &free_mask) ); | |
| 138 | |||
| 139 | ✗ | assert( shmem_procinfo__polldrom(111, NULL, &mask) == DLB_SUCCESS ); | |
| 140 | ✗ | assert( CPU_COUNT(&mask) == 1 && CPU_ISSET(0, &mask) ); | |
| 141 | |||
| 142 | // removing CPU 0 from subprocess 1 is also allowed | ||
| 143 | ✗ | CPU_ZERO(&mask); | |
| 144 | ✗ | assert( shmem_procinfo__setprocessmask(111, &mask, flags, NULL) == DLB_SUCCESS ); | |
| 145 | ✗ | assert( shmem_procinfo__polldrom(111, NULL, &mask) == DLB_SUCCESS ); | |
| 146 | ✗ | assert( CPU_COUNT(&mask) == 0 ); | |
| 147 | |||
| 148 | // test CPU stealing without return_stolen | ||
| 149 | { | ||
| 150 | // set mask 0-2 to subprocess 1 | ||
| 151 | ✗ | mu_parse_mask("0-2", &mask); | |
| 152 | ✗ | assert( shmem_procinfo__setprocessmask(111, &mask, flags, NULL) == DLB_SUCCESS ); | |
| 153 | // check that subprocess 2 has now only CPU 3 | ||
| 154 | ✗ | assert( shmem_procinfo__polldrom(222, NULL, &mask) == DLB_SUCCESS ); | |
| 155 | ✗ | assert( CPU_COUNT(&mask) == 1 && CPU_ISSET(3, &mask) ); | |
| 156 | // check that subprocess 1 has CPUs 0-2 | ||
| 157 | ✗ | assert( shmem_procinfo__polldrom(111, NULL, &mask) == DLB_SUCCESS ); | |
| 158 | ✗ | assert( CPU_COUNT(&mask) == 3 | |
| 159 | && CPU_ISSET(0, &mask) && CPU_ISSET(1, &mask) && CPU_ISSET(2, &mask) ); | ||
| 160 | |||
| 161 | // set original mask to subprocess 1 (WITHOUT return_stolen) | ||
| 162 | ✗ | assert( shmem_procinfo__setprocessmask(111, &p1_mask, flags, NULL) == DLB_SUCCESS ); | |
| 163 | // check that subprocess 2 has not been updated | ||
| 164 | ✗ | assert( shmem_procinfo__polldrom(222, NULL, &mask) == DLB_NOUPDT ); | |
| 165 | // check that subprocess 1 has CPUs 0-1 | ||
| 166 | ✗ | assert( shmem_procinfo__polldrom(111, NULL, &mask) == DLB_SUCCESS ); | |
| 167 | ✗ | assert( CPU_COUNT(&mask) == 2 | |
| 168 | && CPU_ISSET(0, &mask) && CPU_ISSET(1, &mask) ); | ||
| 169 | |||
| 170 | // manually recover CPUs for subprocess 2 | ||
| 171 | ✗ | assert( shmem_procinfo_ext__recover_stolen_cpus(222) == DLB_SUCCESS ); | |
| 172 | ✗ | assert( shmem_procinfo__polldrom(222, NULL, &mask) == DLB_SUCCESS ); | |
| 173 | ✗ | assert( CPU_COUNT(&mask) == 2 | |
| 174 | && CPU_ISSET(2, &mask) && CPU_ISSET(3, &mask) ); | ||
| 175 | |||
| 176 | // check that there are not stolen CPUs | ||
| 177 | ✗ | assert( shmem_procinfo_ext__recover_stolen_cpus(222) == DLB_NOUPDT ); | |
| 178 | } | ||
| 179 | |||
| 180 | // test CPU stealing with return_stolen | ||
| 181 | { | ||
| 182 | // set mask 0-2 to subprocess 1 (again) | ||
| 183 | ✗ | mu_parse_mask("0-2", &mask); | |
| 184 | ✗ | assert( shmem_procinfo__setprocessmask(111, &mask, flags, NULL) == DLB_SUCCESS ); | |
| 185 | // check that subprocess 2 has now only CPU 3 | ||
| 186 | ✗ | assert( shmem_procinfo__polldrom(222, NULL, &mask) == DLB_SUCCESS ); | |
| 187 | ✗ | assert( CPU_COUNT(&mask) == 1 && CPU_ISSET(3, &mask) ); | |
| 188 | // check that subprocess 1 has CPUs 0-2 | ||
| 189 | ✗ | assert( shmem_procinfo__polldrom(111, NULL, &mask) == DLB_SUCCESS ); | |
| 190 | ✗ | assert( CPU_COUNT(&mask) == 3 | |
| 191 | && CPU_ISSET(0, &mask) && CPU_ISSET(1, &mask) && CPU_ISSET(2, &mask) ); | ||
| 192 | |||
| 193 | // set original mask to subprocess 1 (WITH return_stolen) | ||
| 194 | ✗ | flags |= DLB_RETURN_STOLEN; | |
| 195 | ✗ | assert( shmem_procinfo__setprocessmask(111, &p1_mask, flags, NULL) == DLB_SUCCESS ); | |
| 196 | // check that subprocess 2 has CPUs 2-3 | ||
| 197 | ✗ | assert( shmem_procinfo__polldrom(222, NULL, &mask) == DLB_SUCCESS ); | |
| 198 | ✗ | assert( CPU_COUNT(&mask) == 2 | |
| 199 | && CPU_ISSET(2, &mask) && CPU_ISSET(3, &mask) ); | ||
| 200 | // check that subprocess 1 has CPUs 0-1 | ||
| 201 | ✗ | assert( shmem_procinfo__polldrom(111, NULL, &mask) == DLB_SUCCESS ); | |
| 202 | ✗ | assert( CPU_COUNT(&mask) == 2 | |
| 203 | && CPU_ISSET(0, &mask) && CPU_ISSET(1, &mask) ); | ||
| 204 | |||
| 205 | // check that there are not stolen CPUs | ||
| 206 | ✗ | assert( shmem_procinfo_ext__recover_stolen_cpus(222) == DLB_NOUPDT ); | |
| 207 | } | ||
| 208 | |||
| 209 | // test stealing all CPUs | ||
| 210 | { | ||
| 211 | // subprocess 1 gets all CPUs | ||
| 212 | ✗ | mu_parse_mask("0-3", &mask); | |
| 213 | ✗ | assert( shmem_procinfo__setprocessmask(111, &mask, flags, NULL) == DLB_SUCCESS ); | |
| 214 | // check that subprocess 2 has empty mask | ||
| 215 | ✗ | assert( shmem_procinfo__polldrom(222, NULL, &mask) == DLB_SUCCESS ); | |
| 216 | ✗ | assert( CPU_COUNT(&mask) == 0 ); | |
| 217 | // check that subprocess 1 has CPUs 0-3 | ||
| 218 | ✗ | assert( shmem_procinfo__polldrom(111, NULL, &mask) == DLB_SUCCESS ); | |
| 219 | ✗ | assert( CPU_COUNT(&mask) == 4 | |
| 220 | && CPU_ISSET(0, &mask) && CPU_ISSET(1, &mask) | ||
| 221 | && CPU_ISSET(2, &mask) && CPU_ISSET(3, &mask) ); | ||
| 222 | |||
| 223 | // set original mask to subprocess 1 (WITH return_stolen) | ||
| 224 | ✗ | flags |= DLB_RETURN_STOLEN; | |
| 225 | ✗ | assert( shmem_procinfo__setprocessmask(111, &p1_mask, flags, NULL) == DLB_SUCCESS ); | |
| 226 | // check that subprocess 2 has CPUs 2-3 | ||
| 227 | ✗ | assert( shmem_procinfo__polldrom(222, NULL, &mask) == DLB_SUCCESS ); | |
| 228 | ✗ | assert( CPU_COUNT(&mask) == 2 | |
| 229 | && CPU_ISSET(2, &mask) && CPU_ISSET(3, &mask) ); | ||
| 230 | // check that subprocess 1 has CPUs 0-1 | ||
| 231 | ✗ | assert( shmem_procinfo__polldrom(111, NULL, &mask) == DLB_SUCCESS ); | |
| 232 | ✗ | assert( CPU_COUNT(&mask) == 2 | |
| 233 | && CPU_ISSET(0, &mask) && CPU_ISSET(1, &mask) ); | ||
| 234 | |||
| 235 | // check that there are not stolen CPUs | ||
| 236 | ✗ | assert( shmem_procinfo_ext__recover_stolen_cpus(222) == DLB_NOUPDT ); | |
| 237 | } | ||
| 238 | |||
| 239 | // test setting same mask as future | ||
| 240 | { | ||
| 241 | ✗ | flags = DLB_DROM_FLAGS_NONE; | |
| 242 | |||
| 243 | // subprocess 1 steals one CPU from suprocess 2 | ||
| 244 | ✗ | mu_parse_mask("0-1,3", &mask); | |
| 245 | ✗ | assert( shmem_procinfo__setprocessmask(111, &mask, flags, NULL) == DLB_SUCCESS ); | |
| 246 | |||
| 247 | // subprocess 2 changes mask to "2" before resolving previous change | ||
| 248 | ✗ | mu_parse_mask("2", &mask); | |
| 249 | ✗ | assert( shmem_procinfo__setprocessmask(222, &mask, flags, NULL) == DLB_SUCCESS ); | |
| 250 | |||
| 251 | // check that subprocess 1 has CPUs 0-3 | ||
| 252 | ✗ | assert( shmem_procinfo__polldrom(111, NULL, &mask) == DLB_SUCCESS ); | |
| 253 | ✗ | assert( CPU_COUNT(&mask) == 3 | |
| 254 | && CPU_ISSET(0, &mask) && CPU_ISSET(1, &mask) | ||
| 255 | && CPU_ISSET(3, &mask) ); | ||
| 256 | |||
| 257 | // check that subprocess 2 has CPU 2 | ||
| 258 | ✗ | assert( shmem_procinfo__getprocessmask(222, &mask, flags) == DLB_SUCCESS ); | |
| 259 | ✗ | assert( CPU_COUNT(&mask) == 1 && CPU_ISSET(2, &mask) ); | |
| 260 | |||
| 261 | // check that both processes are not dirty | ||
| 262 | ✗ | assert( shmem_procinfo__polldrom(111, NULL, NULL) == DLB_NOUPDT ); | |
| 263 | ✗ | assert( shmem_procinfo__polldrom(222, NULL, NULL) == DLB_NOUPDT ); | |
| 264 | |||
| 265 | // set original mask to subprocess 1 (WITH return_stolen) | ||
| 266 | ✗ | flags |= DLB_RETURN_STOLEN; | |
| 267 | ✗ | assert( shmem_procinfo__setprocessmask(111, &p1_mask, flags, NULL) == DLB_SUCCESS ); | |
| 268 | // check that subprocess 2 has CPUs 2-3 | ||
| 269 | ✗ | assert( shmem_procinfo__polldrom(222, NULL, &mask) == DLB_SUCCESS ); | |
| 270 | ✗ | assert( CPU_COUNT(&mask) == 2 | |
| 271 | && CPU_ISSET(2, &mask) && CPU_ISSET(3, &mask) ); | ||
| 272 | // check that subprocess 1 has CPUs 0-1 | ||
| 273 | ✗ | assert( shmem_procinfo__polldrom(111, NULL, &mask) == DLB_SUCCESS ); | |
| 274 | ✗ | assert( CPU_COUNT(&mask) == 2 | |
| 275 | && CPU_ISSET(0, &mask) && CPU_ISSET(1, &mask) ); | ||
| 276 | |||
| 277 | // check that there are not stolen CPUs | ||
| 278 | ✗ | assert( shmem_procinfo_ext__recover_stolen_cpus(222) == DLB_NOUPDT ); | |
| 279 | } | ||
| 280 | |||
| 281 | ✗ | assert( shmem_procinfo__finalize(111, false, SHMEM_KEY, SHMEM_SIZE_MULTIPLIER) | |
| 282 | == DLB_SUCCESS ); | ||
| 283 | ✗ | assert( shmem_procinfo__finalize(222, false, SHMEM_KEY, SHMEM_SIZE_MULTIPLIER) | |
| 284 | == DLB_SUCCESS ); | ||
| 285 | } | ||
| 286 | } | ||
| 287 | |||
| 288 | // Check incompatible shmem options | ||
| 289 | { | ||
| 290 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( shmem_procinfo__init_with_cpu_sharing(111, 0, &process_mask, SHMEM_KEY, |
| 291 | SHMEM_SIZE_MULTIPLIER) == DLB_SUCCESS ); | ||
| 292 | |||
| 293 | 1 | dlb_drom_flags_t flags = DLB_DROM_FLAGS_NONE; | |
| 294 | cpu_set_t mask; | ||
| 295 | 1 | mu_parse_mask("0", &mask); | |
| 296 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( shmem_procinfo__setprocessmask(111, &mask, flags, NULL) == DLB_ERR_NOCOMP ); |
| 297 | |||
| 298 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( shmem_procinfo__finalize(111, false, SHMEM_KEY, SHMEM_SIZE_MULTIPLIER) |
| 299 | == DLB_SUCCESS ); | ||
| 300 | } | ||
| 301 | |||
| 302 | 1 | return 0; | |
| 303 | } | ||
| 304 |