GCC Code Coverage Report


Directory: ./
File: tests/unit/02_shmem/cpuinfo_get_binding_01.c
Date: 2026-11-30 15:03:27
Exec Total Coverage
Lines: 79 79 100.0%
Functions: 1 1 100.0%
Branches: 39 78 50.0%

Line Branch Exec Source
1 /*********************************************************************************/
2 /* Copyright 2009-2024 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_cpuinfo.h"
23 #include "apis/dlb_errors.h"
24 #include "support/mask_utils.h"
25
26 #include <assert.h>
27
28 /* array_cpuinfo_task_t */
29 #define ARRAY_T cpuinfo_task_t
30 #define ARRAY_KEY_T pid_t
31 #include "support/array_template.h"
32
33 /* Thread binding tests involving LeWI */
34
35 1 int main(int argc, char *argv[]) {
36 // This test needs at least room for 4 CPUs
37 enum { SYS_SIZE = 4 };
38 1 mu_init();
39 1 mu_testing_set_sys_size(SYS_SIZE);
40
41 array_cpuinfo_task_t tasks;
42 1 array_cpuinfo_task_t_init(&tasks, SYS_SIZE);
43
44 // Initialize local masks to [1100] and [0011]
45 1 pid_t p1_pid = 111;
46 cpu_set_t p1_mask;
47 1 CPU_ZERO(&p1_mask);
48
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 CPU_SET(0, &p1_mask);
49
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 CPU_SET(1, &p1_mask);
50 1 pid_t p2_pid = 222;
51 cpu_set_t p2_mask;
52 1 CPU_ZERO(&p2_mask);
53
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 CPU_SET(2, &p2_mask);
54
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 CPU_SET(3, &p2_mask);
55
56 // Init
57
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__init(p1_pid, 0, &p1_mask, SHMEM_KEY, 0) == DLB_SUCCESS );
58
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__init(p2_pid, 0, &p2_mask, SHMEM_KEY, 0) == DLB_SUCCESS );
59
60 // Get initial rebindings (disordered calls should not matter)
61
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__get_thread_binding(p1_pid, 1) == 1 );
62
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__get_thread_binding(p1_pid, 0) == 0 );
63
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__get_thread_binding(p2_pid, 0) == 2 );
64
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__get_thread_binding(p2_pid, 1) == 3 );
65
66 // P2 lends CPU 3 (thread 1), CPU 2 (thread 0) does not need to rebind
67 1 shmem_cpuinfo__lend_cpu(p2_pid, 3, &tasks);
68 1 array_cpuinfo_task_t_clear(&tasks);
69
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__get_thread_binding(p2_pid, 0) == 2 );
70
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__get_thread_binding(p2_pid, 1) == -1 );
71
72 // P2 recovers CPU 3
73 1 shmem_cpuinfo__acquire_cpu(p2_pid, 3, &tasks);
74 1 array_cpuinfo_task_t_clear(&tasks);
75
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__get_thread_binding(p2_pid, 0) == 2 );
76
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__get_thread_binding(p2_pid, 1) == 3 );
77
78 // P2 lends CPU 2 (thread 0 needs reassigning)
79 1 shmem_cpuinfo__lend_cpu(p2_pid, 2, &tasks);
80 1 array_cpuinfo_task_t_clear(&tasks);
81
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__get_thread_binding(p2_pid, 0) == 3 );
82
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__get_thread_binding(p2_pid, 1) == -1 );
83
84 // P1 acquires CPU 2
85 1 shmem_cpuinfo__acquire_cpu(p1_pid, 2, &tasks);
86 1 array_cpuinfo_task_t_clear(&tasks);
87
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__get_thread_binding(p1_pid, 0) == 0 );
88
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__get_thread_binding(p1_pid, 1) == 1 );
89
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__get_thread_binding(p1_pid, 2) == 2 );
90
91 // P1 lends CPUs 1 and 2
92 1 shmem_cpuinfo__lend_cpu(p1_pid, 1, &tasks);
93 1 shmem_cpuinfo__lend_cpu(p1_pid, 2, &tasks);
94 1 array_cpuinfo_task_t_clear(&tasks);
95
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__get_thread_binding(p1_pid, 0) == 0 );
96
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__get_thread_binding(p1_pid, 1) == -1 );
97
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__get_thread_binding(p1_pid, 2) == -1 );
98
99 // P2 bind check
100
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__get_thread_binding(p2_pid, 0) == 3 );
101
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__get_thread_binding(p2_pid, 1) == -1 );
102
103 // P2 acquires CPUs 1 and 2 (owned CPUs first)
104 1 shmem_cpuinfo__acquire_cpu(p2_pid, 1, &tasks);
105 1 shmem_cpuinfo__acquire_cpu(p2_pid, 2, &tasks);
106 1 array_cpuinfo_task_t_clear(&tasks);
107
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__get_thread_binding(p2_pid, 0) == 2 );
108
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__get_thread_binding(p2_pid, 1) == 3 );
109
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__get_thread_binding(p2_pid, 2) == 1 );
110
111 // P1 acquires CPU 1 forcing oversubscription
112 1 shmem_cpuinfo__acquire_cpu(p1_pid, 1, &tasks);
113 1 array_cpuinfo_task_t_clear(&tasks);
114
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__get_thread_binding(p1_pid, 1) == 1 );
115
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__get_thread_binding(p2_pid, 2) == 1 );
116 1 shmem_cpuinfo__lend_cpu(p2_pid, 1, &tasks);
117 1 array_cpuinfo_task_t_clear(&tasks);
118
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__get_thread_binding(p1_pid, 1) == 1 );
119
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__get_thread_binding(p2_pid, 2) == -1 );
120
121 // P2 acquires CPUs 1 (lent by P1), 2 (reclaimed) and 3 (borrowed)
122 1 shmem_cpuinfo__lend_cpu(p1_pid, 1, &tasks);
123 1 shmem_cpuinfo__acquire_cpu(p2_pid, 1, &tasks); /* lent */
124 1 array_cpuinfo_task_t_clear(&tasks);
125 1 shmem_cpuinfo__lend_cpu(p2_pid, 2, &tasks);
126 1 shmem_cpuinfo__acquire_cpu(p1_pid, 2, &tasks);
127 1 shmem_cpuinfo__acquire_cpu(p2_pid, 2, &tasks); /* force OS */
128 1 array_cpuinfo_task_t_clear(&tasks);
129 1 shmem_cpuinfo__lend_cpu(p2_pid, 3, &tasks);
130 1 shmem_cpuinfo__acquire_cpu(p2_pid, 3, &tasks); /* borrow */
131 1 array_cpuinfo_task_t_clear(&tasks);
132
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__get_thread_binding(p2_pid, 0) == 2 );
133
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__get_thread_binding(p2_pid, 1) == 3 );
134
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__get_thread_binding(p2_pid, 2) == 1 );
135 /* P1 leaves and CPU 2 is no longer oversubscribed */
136
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__get_thread_binding(p1_pid, 1) == 2 );
137 1 shmem_cpuinfo__lend_cpu(p1_pid, 2, &tasks);
138 1 array_cpuinfo_task_t_clear(&tasks);
139
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__get_thread_binding(p1_pid, 1) == -1 );
140
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__get_thread_binding(p2_pid, 0) == 2 );
141
142 // Finalize
143
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__finalize(p1_pid, SHMEM_KEY, 0) == DLB_SUCCESS );
144
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__finalize(p2_pid, SHMEM_KEY, 0) == DLB_SUCCESS );
145
146 1 mu_finalize();
147
148 1 return 0;
149 }
150