| 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 <apis/dlb_drom.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 | #include <sys/wait.h> | ||
| 32 | |||
| 33 | /* Basic DROM init / pre / post / finalize and child checking the environment */ | ||
| 34 | |||
| 35 | 2 | static void test_fork(const cpu_set_t *mask, char ***env) { | |
| 36 | 2 | pid_t pid = fork(); | |
| 37 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | assert( pid >= 0 ); |
| 38 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | if (pid == 0) { |
| 39 | 2 | pid = getpid(); | |
| 40 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | assert( DLB_DROM_Attach() == DLB_SUCCESS ); |
| 41 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | assert( DLB_DROM_PreInit(pid, mask, DLB_STEAL_CPUS, env) == DLB_SUCCESS ); |
| 42 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | assert( DLB_DROM_Detach() == DLB_SUCCESS ); |
| 43 | |||
| 44 | 2 | dlb_test__gcov_dump(); | |
| 45 | |||
| 46 | // Exec into a shell process to check if the environment is correctly modified | ||
| 47 | ✗ | const char *shell_command = | |
| 48 | "if [ -n \"${DLB_ARGS##*--drom=1*}\" ] || " | ||
| 49 | "[ -n \"${DLB_ARGS##*--preinit-pid=*}\" ] || " | ||
| 50 | "[ -n \"${DLB_ARGS##*--opt=foo*}\" ] || " | ||
| 51 | "[ ${OMP_NUM_THREADS:=0} != 1 ] ; " | ||
| 52 | "then exit 1 ; fi"; | ||
| 53 | |||
| 54 | ✗ | if (env) { | |
| 55 | ✗ | execle("/bin/sh", "sh", "-c", shell_command, NULL, *env); | |
| 56 | } else { | ||
| 57 | ✗ | execl("/bin/sh", "sh", "-c", shell_command, NULL); | |
| 58 | } | ||
| 59 | ✗ | perror("exec failed"); | |
| 60 | ✗ | exit(EXIT_FAILURE); | |
| 61 | } | ||
| 62 | // Wait for all child processes | ||
| 63 | int wstatus; | ||
| 64 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
|
4 | while(wait(&wstatus) > 0) { |
| 65 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!WIFEXITED(wstatus)) |
| 66 | ✗ | exit(EXIT_FAILURE); | |
| 67 | 2 | int rc = WEXITSTATUS(wstatus); | |
| 68 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (rc != 0) { |
| 69 | ✗ | printf("Child return status: %d\n", rc); | |
| 70 | ✗ | exit(EXIT_FAILURE); | |
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | assert( DLB_DROM_Attach() == DLB_SUCCESS ); |
| 75 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | assert( DLB_DROM_PostFinalize(pid, DLB_DROM_FLAGS_NONE) == DLB_SUCCESS ); |
| 76 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | assert( DLB_DROM_Detach() == DLB_SUCCESS ); |
| 77 | 2 | } | |
| 78 | |||
| 79 | 1 | int main(int argc, char **argv) { | |
| 80 | /* Options */ | ||
| 81 | 1 | char options[64] = "--opt=foo --shm-key="; | |
| 82 | 1 | strcat(options, SHMEM_KEY); | |
| 83 | |||
| 84 | /* Modify DLB_ARGS to include options */ | ||
| 85 | 1 | const char *dlb_args_env = getenv("DLB_ARGS"); | |
| 86 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (dlb_args_env) { |
| 87 | 1 | size_t len = strlen(dlb_args_env) + 1 + strlen(options) + 1; | |
| 88 | 1 | char *new_dlb_args = malloc(sizeof(char)*len); | |
| 89 | 1 | sprintf(new_dlb_args, "%s %s", dlb_args_env, options); | |
| 90 | 1 | setenv("DLB_ARGS", new_dlb_args, 1); | |
| 91 | 1 | free(new_dlb_args); | |
| 92 | } else { | ||
| 93 | ✗ | setenv("DLB_ARGS", options, 1); | |
| 94 | } | ||
| 95 | |||
| 96 | /* Process mask is [0] */ | ||
| 97 | cpu_set_t mask; | ||
| 98 | 1 | CPU_ZERO(&mask); | |
| 99 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | CPU_SET(0, &mask); |
| 100 | |||
| 101 | /* OMP_NUM_THREADS = 4 and it will have to be reduced to 1 because of the mask */ | ||
| 102 | 1 | setenv("OMP_NUM_THREADS", "4", 1); | |
| 103 | |||
| 104 | /* Fork with CURRENT environment */ | ||
| 105 | 1 | test_fork(&mask, NULL); | |
| 106 | |||
| 107 | /* Fork with CUSTOM environment */ | ||
| 108 | 1 | char **new_environ = malloc(3*sizeof(char*)); // 2 variables + NULL | |
| 109 | // OMP_NUM_THREADS | ||
| 110 | 1 | const char *env1 = getenv("OMP_NUM_THREADS"); | |
| 111 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!env1) env1 = ""; |
| 112 | 1 | const size_t var1len = strlen(env1) + sizeof("OMP_NUM_THREADS="); | |
| 113 | 1 | new_environ[0] = malloc(var1len); | |
| 114 | 1 | snprintf(new_environ[0], var1len, "OMP_NUM_THREADS=%s", env1); | |
| 115 | // DLB_ARGS | ||
| 116 | 1 | const char *env2 = getenv("DLB_ARGS"); /* previously defined, update pointer */ | |
| 117 | 1 | const size_t var2len = strlen(env2) + sizeof("DLB_ARGS="); | |
| 118 | 1 | new_environ[1] = malloc(var2len); | |
| 119 | 1 | snprintf(new_environ[1], var2len, "DLB_ARGS=%s", env2); | |
| 120 | // | ||
| 121 | 1 | new_environ[2] = NULL; | |
| 122 | 1 | test_fork(&mask, &new_environ); | |
| 123 | 1 | free(new_environ[0]); | |
| 124 | 1 | free(new_environ[1]); | |
| 125 | 1 | free(new_environ); | |
| 126 | |||
| 127 | 1 | return 0; | |
| 128 | } | ||
| 129 |