GCC Code Coverage Report


Directory: ./
File: tests/unit/05_api/api_sp_03.c
Date: 2026-12-01 10:07:21
Exec Total Coverage
Lines: 44 45 97.8%
Functions: 2 2 100.0%
Branches: 47 70 67.1%

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 "assert_loop.h"
21 #include "unique_shmem.h"
22
23 #include "apis/dlb.h"
24 #include "apis/dlb_sp.h"
25 #include "support/mask_utils.h"
26
27 #include <string.h>
28 #include <assert.h>
29
30 enum {NSUBPROCS = 4};
31 static cpu_set_t sp_mask[NSUBPROCS];
32 static dlb_handler_t handlers[NSUBPROCS];
33 static interaction_mode_t mode;
34
35 28 static void cb_enable_cpu(int cpuid, void *arg) {
36 28 int sp_id = *((int*)arg);
37 28 cpu_set_t *mask = &sp_mask[sp_id];
38
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 CPU_SET(cpuid, mask);
39 28 }
40
41 2 int main(int argc, char *argv[]) {
42 int i;
43 int err;
44
45 // Options
46 2 char options[64] = "--lewi --shm-key=";
47 2 strcat(options, SHMEM_KEY);
48
49 // test NSUBPROCS subprocesses, 1 CPU each, in a 16 CPU system
50 enum { SYS_SIZE = 16 };
51 2 mu_init();
52 2 mu_testing_set_sys_size(SYS_SIZE);
53
54 // Initialize subprocesses
55 int sp_ids[NSUBPROCS];
56
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 for (i=0; i<NSUBPROCS; ++i) {
57 8 sp_ids[i] = i;
58 8 CPU_ZERO(&sp_mask[i]);
59
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 CPU_SET(i, &sp_mask[i]);
60 8 handlers[i] = DLB_Init_sp(0, &sp_mask[i], options);
61
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 assert( handlers[i] != NULL );
62
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 assert( DLB_CallbackSet_sp(handlers[i], dlb_callback_enable_cpu,
63 (dlb_callback_t)cb_enable_cpu, &sp_ids[i]) == DLB_SUCCESS);
64 }
65
66 // Get interaction mode (this test is called twice, using mode={polling,async})
67 char value[16];
68 2 DLB_GetVariable_sp(handlers[0], "--mode", value);
69
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (strcmp(value, "polling") == 0) {
70 1 mode = MODE_POLLING;
71
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 } else if (strcmp(value, "async") == 0) {
72 1 mode = MODE_ASYNC;
73 } else {
74 // Unknown value
75 ✗ return -1;
76 }
77
78 // Everyone requests 1 CPU
79
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 for (i=0; i<NSUBPROCS; ++i) {
80 8 err = DLB_AcquireCpus_sp(handlers[i], 1);
81
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
8 assert( mode == MODE_ASYNC ? err == DLB_NOTED : err == DLB_NOUPDT );
82 }
83
84 // Everyone lends their own CPU and their requests are auto resolved
85
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 for (i=0; i<NSUBPROCS; ++i) {
86
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 CPU_CLR(i, &sp_mask[i]);
87
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 assert( DLB_LendCpu_sp(handlers[i], i) == DLB_SUCCESS );
88 // Acquire again if polling
89
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 if (mode == MODE_POLLING) {
90
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 assert( DLB_AcquireCpus_sp(handlers[i], 1) == DLB_SUCCESS );
91 }
92
7/14
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 8 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 8 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✓ Branch 14 taken 8 times.
8 assert_loop( CPU_ISSET(i, &sp_mask[i]) );
93 }
94
95 // Everyone requests SYS_SIZE CPUs
96
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 for (i=0; i<NSUBPROCS; ++i) {
97 8 err = DLB_AcquireCpus_sp(handlers[i], SYS_SIZE);
98
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
8 assert( mode == MODE_ASYNC ? err == DLB_NOTED : err == DLB_NOUPDT );
99 }
100
101 // Everyone lends 1 CPU
102
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 for (i=0; i<NSUBPROCS; ++i) {
103
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 CPU_CLR(i, &sp_mask[i]);
104
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 assert( DLB_LendCpu_sp(handlers[i], i) == DLB_SUCCESS );
105 }
106
107 // Every subprocess should receive all CPUs from the subprocesses still alive
108
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 for (i=0; i<NSUBPROCS; ++i) {
109 // If polling, ask again for as many CPUs are left
110
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 if (mode == MODE_POLLING) {
111
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 assert( DLB_AcquireCpus_sp(handlers[i], SYS_SIZE-i) == DLB_SUCCESS );
112 }
113 // Each subprocess should have all the CPUs left in the system, then finalizes
114
4/6
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 1 times.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 8 times.
9 assert_loop( CPU_COUNT(&sp_mask[i]) == NSUBPROCS-i );
115
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 assert( DLB_Finalize_sp(handlers[i]) == DLB_SUCCESS );
116 }
117
118 2 mu_finalize();
119
120 2 return 0;
121 }
122