| 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_async.h" | ||
| 23 | #include "LB_numThreads/numThreads.h" | ||
| 24 | #include "apis/dlb_errors.h" | ||
| 25 | #include "support/queues.h" | ||
| 26 | #include "support/debug.h" | ||
| 27 | #include "support/options.h" | ||
| 28 | |||
| 29 | #include <assert.h> | ||
| 30 | #include <unistd.h> | ||
| 31 | #include <pthread.h> | ||
| 32 | #include <stdbool.h> | ||
| 33 | |||
| 34 | /* Test message queue */ | ||
| 35 | |||
| 36 | static pthread_mutex_t queue_mutex = PTHREAD_MUTEX_INITIALIZER; | ||
| 37 | |||
| 38 | /* Simple test: register two callbaks and check that each one is called */ | ||
| 39 | static int num_cb_called = 0; | ||
| 40 | 1 | static void cb1_enable_cpu(int cpuid, void *arg) { ++num_cb_called; } | |
| 41 | 1 | static void cb1_disable_cpu(int cpuid, void *arg) { ++num_cb_called; } | |
| 42 | 1 | static void test_callbacks(void) { | |
| 43 | 1 | pm_interface_t pm = { | |
| 44 | .dlb_callback_enable_cpu_ptr = cb1_enable_cpu, | ||
| 45 | .dlb_callback_enable_cpu_arg = NULL, | ||
| 46 | .dlb_callback_disable_cpu_ptr = cb1_disable_cpu, | ||
| 47 | .dlb_callback_disable_cpu_arg = NULL | ||
| 48 | }; | ||
| 49 | 1 | pid_t pid1 = 42; | |
| 50 | 1 | cpu_set_t mask = { .__bits = { 0xf } }; | |
| 51 | |||
| 52 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( shmem_async_init(pid1, &pm, &mask, SHMEM_KEY, 1) == DLB_SUCCESS ); |
| 53 | 1 | shmem_async_enable_cpu(pid1, 1); | |
| 54 | 1 | shmem_async_disable_cpu(pid1, 1); | |
| 55 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( shmem_async_finalize(pid1) == DLB_SUCCESS ); |
| 56 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert( num_cb_called == 2 ); |
| 57 | 1 | } | |
| 58 | |||
| 59 | /* Nested callbacks: send a message to a helper thread that forces to | ||
| 60 | * enqueue the same message to himself */ | ||
| 61 | static pid_t pid2; | ||
| 62 | 100 | static void cb2_enable_cpu(int cpuid, void *arg) { | |
| 63 | 100 | queue_proc_reqs_t *queue = (queue_proc_reqs_t*) arg; | |
| 64 | |||
| 65 | /* Simulate a DLB_Lend) -> find_new_guest -> notify_helper */ | ||
| 66 | pid_t new_guest; | ||
| 67 | 100 | pthread_mutex_lock(&queue_mutex); | |
| 68 | 100 | queue_proc_reqs_get(queue, &new_guest, cpuid); | |
| 69 | 100 | pthread_mutex_unlock(&queue_mutex); | |
| 70 |
2/2✓ Branch 0 taken 99 times.
✓ Branch 1 taken 1 times.
|
100 | if (new_guest > 0) { |
| 71 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 99 times.
|
99 | assert( new_guest == pid2 ); |
| 72 | 99 | shmem_async_enable_cpu(new_guest, cpuid); | |
| 73 | } | ||
| 74 | 100 | } | |
| 75 | 1 | static void test_nested_callbacks(void) { | |
| 76 | enum { NUM_REQS = 100 }; | ||
| 77 | 1 | pid2 = 42; | |
| 78 | 1 | cpu_set_t mask = { .__bits = { 0xf } }; | |
| 79 | |||
| 80 | /* Set up queue with 10 requests */ | ||
| 81 | queue_proc_reqs_t queue; | ||
| 82 | 1 | queue_proc_reqs_init(&queue); | |
| 83 | 1 | queue_proc_reqs_push(&queue, pid2, NUM_REQS, NULL); | |
| 84 | |||
| 85 | 1 | pm_interface_t pm = { | |
| 86 | .dlb_callback_enable_cpu_ptr = cb2_enable_cpu, | ||
| 87 | .dlb_callback_enable_cpu_arg = &queue | ||
| 88 | }; | ||
| 89 | |||
| 90 | /* Initialize shmem and helper thread */ | ||
| 91 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( shmem_async_init(pid2, &pm, &mask, SHMEM_KEY, 1) == DLB_SUCCESS ); |
| 92 | |||
| 93 | /* Simulate a DLB_Lend (cpu 0) -> find_new_guest (same pid) -> notify_helper */ | ||
| 94 | pid_t new_guest; | ||
| 95 | 1 | pthread_mutex_lock(&queue_mutex); | |
| 96 | 1 | queue_proc_reqs_get(&queue, &new_guest, 0); | |
| 97 | 1 | pthread_mutex_unlock(&queue_mutex); | |
| 98 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | assert( new_guest == pid2 ); |
| 99 | 1 | shmem_async_enable_cpu(pid2, 0); | |
| 100 | |||
| 101 | /* Wait for the queue to be empty */ | ||
| 102 | 1 | while (true) { | |
| 103 | 2 | pthread_mutex_lock(&queue_mutex); | |
| 104 | 2 | bool done = queue_proc_reqs_size(&queue) == 0; | |
| 105 | 2 | pthread_mutex_unlock(&queue_mutex); | |
| 106 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (done) break; |
| 107 | 1 | else usleep(1000); | |
| 108 | } | ||
| 109 | |||
| 110 | /* Finalize shmem and helper */ | ||
| 111 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | assert( shmem_async_finalize(pid2) == DLB_SUCCESS ); |
| 112 | 1 | } | |
| 113 | |||
| 114 | 1 | int main(int argc, char **argv) { | |
| 115 | options_t options; | ||
| 116 | 1 | options_init(&options, NULL); | |
| 117 | 1 | debug_init(&options); | |
| 118 | |||
| 119 | /* Test that callback are correctly called */ | ||
| 120 | 1 | test_callbacks(); | |
| 121 | |||
| 122 | /* Test message to the same helper */ | ||
| 123 | 1 | test_nested_callbacks(); | |
| 124 | |||
| 125 | 1 | return 0; | |
| 126 | } | ||
| 127 |