| 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 | /* Test CPU sorting routines */ | ||
| 21 | |||
| 22 | #include "support/mask_utils.h" | ||
| 23 | |||
| 24 | #include <sched.h> | ||
| 25 | #include <stdlib.h> | ||
| 26 | #include <stdio.h> | ||
| 27 | |||
| 28 | #define _INPUTS_ \ | ||
| 29 | {0,1,2,3,4,5,6,7}, \ | ||
| 30 | {1,0,3,2,5,4,7,6}, \ | ||
| 31 | {7,6,5,4,3,2,1,0}, \ | ||
| 32 | {6,7,4,5,2,3,0,1}, \ | ||
| 33 | {4,5,6,7,0,1,2,3}, \ | ||
| 34 | {5,4,7,6,1,0,3,2}, \ | ||
| 35 | {0,1,4,5,2,3,6,7}, \ | ||
| 36 | {1,0,5,4,3,2,7,6}, \ | ||
| 37 | {0,2,1,4,3,6,7,5}, \ | ||
| 38 | {7,0,1,2,3,4,5,6}, \ | ||
| 39 | {3,0,7,6,5,1,2,4}, \ | ||
| 40 | {4,5,7,6,0,1,2,3} | ||
| 41 | |||
| 42 | enum { SYS_SIZE = 8 }; | ||
| 43 | |||
| 44 | /* just for printing */ | ||
| 45 | int current_topology = -1; | ||
| 46 | |||
| 47 | 16 | static void setup_topology(int topology) { | |
| 48 | /* Each test is run with these topologies */ | ||
| 49 | /* 1: [0,1,2,3,4,5,6,7]; no threads per core nor numa nodes | ||
| 50 | * 2: [0,4], [1,5], [2,6], [3,7]; 2 threads per core, no numa nodes | ||
| 51 | */ | ||
| 52 | |||
| 53 | /* Topology 1 */ | ||
| 54 | enum { T1_SYS_SIZE = SYS_SIZE }; | ||
| 55 | |||
| 56 | /* Topology 2 */ | ||
| 57 | enum { T2_NUM_CORES = 4 }; | ||
| 58 | enum { T2_NUM_NODES = 1 }; | ||
| 59 | static cpu_set_t t2_sys_mask = {}; | ||
| 60 | static cpu_set_t t2_core_masks[T2_NUM_CORES]; | ||
| 61 | static cpu_set_t t2_node_masks[T2_NUM_NODES]; | ||
| 62 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 15 times.
|
16 | if (CPU_COUNT(&t2_sys_mask) == 0) { |
| 63 | 1 | mu_parse_mask("0-7", &t2_sys_mask); | |
| 64 | 1 | mu_parse_mask("0,4", &t2_core_masks[0]); | |
| 65 | 1 | mu_parse_mask("1,5", &t2_core_masks[1]); | |
| 66 | 1 | mu_parse_mask("2,6", &t2_core_masks[2]); | |
| 67 | 1 | mu_parse_mask("3,7", &t2_core_masks[3]); | |
| 68 | 1 | mu_parse_mask("0-7", &t2_node_masks[0]); | |
| 69 | } | ||
| 70 | |||
| 71 |
2/3✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
16 | switch(topology) { |
| 72 | 8 | case 1: | |
| 73 | 8 | mu_testing_set_sys_size(T1_SYS_SIZE); | |
| 74 | 8 | break; | |
| 75 | 8 | case 2: | |
| 76 | 8 | mu_testing_set_sys_masks(&t2_sys_mask, | |
| 77 | t2_core_masks, T2_NUM_CORES, | ||
| 78 | t2_node_masks, T2_NUM_NODES); | ||
| 79 | 8 | break; | |
| 80 | ✗ | default: | |
| 81 | ✗ | abort(); | |
| 82 | } | ||
| 83 | |||
| 84 | 16 | current_topology = topology; | |
| 85 | 16 | } | |
| 86 | |||
| 87 | 192 | static void check_array(int *array, int n, const int *expected, | |
| 88 | cpu_set_t *process_mask) { | ||
| 89 | int i; | ||
| 90 |
2/2✓ Branch 0 taken 1536 times.
✓ Branch 1 taken 192 times.
|
1728 | for (i=0; i<n; ++i) { |
| 91 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1536 times.
|
1536 | if (array[i] != expected[i]) { |
| 92 | ✗ | printf("Array comparison failed with mask: %s in topology %d:\n", | |
| 93 | mu_to_str(process_mask), current_topology); | ||
| 94 | int j; | ||
| 95 | ✗ | printf("Got: "); for (j=0; j<n; ++j) {printf("%d ", array[j]);} printf("\n"); | |
| 96 | ✗ | printf("Expected: "); for (j=0; j<n; ++j) {printf("%d ", expected[j]);} printf("\n"); | |
| 97 | ✗ | exit(1); | |
| 98 | } | ||
| 99 | } | ||
| 100 | 192 | } | |
| 101 | |||
| 102 | 96 | static void test_sort_by_ownership(int *array, int n, const int *expected, | |
| 103 | cpu_set_t *process_mask) { | ||
| 104 | /* Invoke qsort */ | ||
| 105 | 96 | qsort_r(array, n, sizeof(int), mu_cmp_cpuids_by_ownership, process_mask); | |
| 106 | |||
| 107 | /* Check sorted array */ | ||
| 108 | 96 | check_array(array, n, expected, process_mask); | |
| 109 | 96 | } | |
| 110 | |||
| 111 | 96 | static void test_sort_by_affinity(int *array, int n, const int *expected, | |
| 112 | cpu_set_t affinity[]) { | ||
| 113 | /* Invoke qsort */ | ||
| 114 | 96 | qsort_r(array, n, sizeof(int), mu_cmp_cpuids_by_affinity, affinity); | |
| 115 | |||
| 116 | /* Check sorted array */ | ||
| 117 | 96 | check_array(array, n, expected, affinity); | |
| 118 | 96 | } | |
| 119 | |||
| 120 | 8 | static void test_sort_by_ownership_in_topology(int topology, const int *expected, | |
| 121 | cpu_set_t *process_mask) { | ||
| 122 | |||
| 123 | 8 | setup_topology(topology); | |
| 124 | |||
| 125 | 8 | int inputs[][SYS_SIZE] = { _INPUTS_ }; | |
| 126 | enum { NUM_INPUTS = sizeof(inputs) / sizeof(inputs[0]) }; | ||
| 127 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 8 times.
|
104 | for (int i = 0; i < NUM_INPUTS; ++i) { |
| 128 | 96 | test_sort_by_ownership(inputs[i], SYS_SIZE, expected, process_mask); | |
| 129 | } | ||
| 130 | 8 | } | |
| 131 | |||
| 132 | 8 | static void test_sort_by_affinity_in_topology(int topology, const int *expected, | |
| 133 | cpu_set_t affinity[]) { | ||
| 134 | |||
| 135 | 8 | setup_topology(topology); | |
| 136 | |||
| 137 | 8 | int inputs[][SYS_SIZE] = { _INPUTS_ }; | |
| 138 | enum { NUM_INPUTS = sizeof(inputs) / sizeof(inputs[0]) }; | ||
| 139 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 8 times.
|
104 | for (int i = 0; i < NUM_INPUTS; ++i) { |
| 140 | 96 | test_sort_by_affinity(inputs[i], SYS_SIZE, expected, affinity); | |
| 141 | } | ||
| 142 | 8 | } | |
| 143 | |||
| 144 | 1 | int main(int argc, char *argv[]) { | |
| 145 | |||
| 146 | 1 | mu_init(); | |
| 147 | 1 | mu_testing_set_sys_size(SYS_SIZE); | |
| 148 | |||
| 149 | /***** Process mask tests *****/ | ||
| 150 | cpu_set_t process_mask; | ||
| 151 | |||
| 152 | /* Full process mask */ | ||
| 153 | { | ||
| 154 | 1 | mu_parse_mask("0xFF", &process_mask); | |
| 155 | |||
| 156 | 1 | int expected_1[SYS_SIZE] = {0,1,2,3,4,5,6,7}; | |
| 157 | 1 | test_sort_by_ownership_in_topology(1, expected_1, &process_mask); | |
| 158 | |||
| 159 | 1 | int expected_2[SYS_SIZE] = {0,4,1,5,2,6,3,7}; | |
| 160 | 1 | test_sort_by_ownership_in_topology(2, expected_2, &process_mask); | |
| 161 | } | ||
| 162 | |||
| 163 | /* Process mask is a subset in the firsts CPUs */ | ||
| 164 | { | ||
| 165 | 1 | mu_parse_mask("0,4", &process_mask); | |
| 166 | |||
| 167 | 1 | int expected_1[SYS_SIZE] = {0,4,1,2,3,5,6,7}; | |
| 168 | 1 | test_sort_by_ownership_in_topology(1, expected_1, &process_mask); | |
| 169 | |||
| 170 | 1 | int expected_2[SYS_SIZE] = {0,4,1,5,2,6,3,7}; | |
| 171 | 1 | test_sort_by_ownership_in_topology(2, expected_2, &process_mask); | |
| 172 | } | ||
| 173 | |||
| 174 | /* Process mask is a subset at the end */ | ||
| 175 | { | ||
| 176 | 1 | mu_parse_mask("3,7", &process_mask); | |
| 177 | |||
| 178 | 1 | int expected_1[SYS_SIZE] = {3,7,4,5,6,0,1,2}; | |
| 179 | 1 | test_sort_by_ownership_in_topology(1, expected_1, &process_mask); | |
| 180 | |||
| 181 | 1 | int expected_2[SYS_SIZE] = {3,7,0,4,1,5,2,6}; | |
| 182 | 1 | test_sort_by_ownership_in_topology(2, expected_2, &process_mask); | |
| 183 | } | ||
| 184 | |||
| 185 | /* Process mask is a subset in the middle */ | ||
| 186 | { | ||
| 187 | 1 | mu_parse_mask("1-2,5-6", &process_mask); | |
| 188 | |||
| 189 | 1 | int expected_1[SYS_SIZE] = {1,2,5,6,3,4,7,0}; | |
| 190 | 1 | test_sort_by_ownership_in_topology(1, expected_1, &process_mask); | |
| 191 | |||
| 192 | 1 | int expected_2[SYS_SIZE] = {1,5,2,6,3,7,0,4}; | |
| 193 | 1 | test_sort_by_ownership_in_topology(2, expected_2, &process_mask); | |
| 194 | } | ||
| 195 | |||
| 196 | /***** Affinity tests *****/ | ||
| 197 | cpu_set_t affinity[4]; | ||
| 198 | |||
| 199 | /* Affinity of only one level */ | ||
| 200 | { | ||
| 201 | 1 | mu_parse_mask("0-7", &affinity[0]); | |
| 202 | 1 | CPU_ZERO(&affinity[1]); | |
| 203 | |||
| 204 | 1 | int expected_1[SYS_SIZE] = {0,1,2,3,4,5,6,7}; | |
| 205 | 1 | test_sort_by_affinity_in_topology(1, expected_1, affinity); | |
| 206 | |||
| 207 | 1 | int expected_2[SYS_SIZE] = {0,4,1,5,2,6,3,7}; | |
| 208 | 1 | test_sort_by_affinity_in_topology(2, expected_2, affinity); | |
| 209 | } | ||
| 210 | |||
| 211 | /* Basic affinity of 3 levels */ | ||
| 212 | { | ||
| 213 | 1 | mu_parse_mask("0-1", &affinity[0]); | |
| 214 | 1 | mu_parse_mask("0-3", &affinity[1]); | |
| 215 | 1 | mu_parse_mask("0-7", &affinity[2]); | |
| 216 | 1 | CPU_ZERO(&affinity[3]); | |
| 217 | |||
| 218 | 1 | int expected_1[SYS_SIZE] = {0,1,2,3,4,5,6,7}; | |
| 219 | 1 | test_sort_by_affinity_in_topology(1, expected_1, affinity); | |
| 220 | |||
| 221 | 1 | mu_parse_mask("0,4", &affinity[0]); | |
| 222 | 1 | mu_parse_mask("0-1,4-5", &affinity[1]); | |
| 223 | 1 | mu_parse_mask("0-7", &affinity[2]); | |
| 224 | 1 | CPU_ZERO(&affinity[3]); | |
| 225 | |||
| 226 | 1 | int expected_2[SYS_SIZE] = {0,4,1,5,2,6,3,7}; | |
| 227 | 1 | test_sort_by_affinity_in_topology(2, expected_2, affinity); | |
| 228 | } | ||
| 229 | |||
| 230 | /* Basic affinity of 3 levels, starting from the end */ | ||
| 231 | { | ||
| 232 | 1 | mu_parse_mask("7", &affinity[0]); | |
| 233 | 1 | mu_parse_mask("4-7", &affinity[1]); | |
| 234 | 1 | mu_parse_mask("0-7", &affinity[2]); | |
| 235 | 1 | CPU_ZERO(&affinity[3]); | |
| 236 | |||
| 237 | 1 | int expected_1[SYS_SIZE] = {7,4,5,6,0,1,2,3}; | |
| 238 | 1 | test_sort_by_affinity_in_topology(1, expected_1, affinity); | |
| 239 | |||
| 240 | 1 | mu_parse_mask("3,7", &affinity[0]); | |
| 241 | 1 | mu_parse_mask("2-3,6-7", &affinity[1]); | |
| 242 | 1 | mu_parse_mask("0-7", &affinity[2]); | |
| 243 | 1 | CPU_ZERO(&affinity[3]); | |
| 244 | |||
| 245 | 1 | int expected_2[SYS_SIZE] = {3,7,2,6,0,4,1,5}; | |
| 246 | 1 | test_sort_by_affinity_in_topology(2, expected_2, affinity); | |
| 247 | } | ||
| 248 | |||
| 249 | /* Level 0 is in the middle of Level 1 mask */ | ||
| 250 | { | ||
| 251 | 1 | mu_parse_mask("5-6", &affinity[0]); | |
| 252 | 1 | mu_parse_mask("4-7", &affinity[1]); | |
| 253 | 1 | mu_parse_mask("0-7", &affinity[2]); | |
| 254 | 1 | CPU_ZERO(&affinity[3]); | |
| 255 | |||
| 256 | 1 | int expected_1[SYS_SIZE] = {5,6,7,4,0,1,2,3}; | |
| 257 | 1 | test_sort_by_affinity_in_topology(1, expected_1, affinity); | |
| 258 | |||
| 259 | 1 | mu_parse_mask("1,5", &affinity[0]); | |
| 260 | 1 | mu_parse_mask("0-2,4-6", &affinity[1]); | |
| 261 | 1 | mu_parse_mask("0-7", &affinity[2]); | |
| 262 | 1 | CPU_ZERO(&affinity[3]); | |
| 263 | |||
| 264 | 1 | int expected_2[SYS_SIZE] = {1,5,2,6,0,4,3,7}; | |
| 265 | 1 | test_sort_by_affinity_in_topology(2, expected_2, affinity); | |
| 266 | } | ||
| 267 | |||
| 268 | 1 | mu_finalize(); | |
| 269 | 1 | return 0; | |
| 270 | } | ||
| 271 |