|
13 | 13 |
|
14 | 14 | #include "resctrl.h" |
15 | 15 |
|
| 16 | +int snc_unreliable; |
| 17 | + |
16 | 18 | static int find_resctrl_mount(char *buffer) |
17 | 19 | { |
18 | 20 | FILE *mounts; |
@@ -156,6 +158,98 @@ int get_domain_id(const char *resource, int cpu_no, int *domain_id) |
156 | 158 | return 0; |
157 | 159 | } |
158 | 160 |
|
| 161 | +/* |
| 162 | + * Count number of CPUs in a /sys bitmap |
| 163 | + */ |
| 164 | +static unsigned int count_sys_bitmap_bits(char *name) |
| 165 | +{ |
| 166 | + FILE *fp = fopen(name, "r"); |
| 167 | + int count = 0, c; |
| 168 | + |
| 169 | + if (!fp) |
| 170 | + return 0; |
| 171 | + |
| 172 | + while ((c = fgetc(fp)) != EOF) { |
| 173 | + if (!isxdigit(c)) |
| 174 | + continue; |
| 175 | + switch (c) { |
| 176 | + case 'f': |
| 177 | + count++; |
| 178 | + fallthrough; |
| 179 | + case '7': case 'b': case 'd': case 'e': |
| 180 | + count++; |
| 181 | + fallthrough; |
| 182 | + case '3': case '5': case '6': case '9': case 'a': case 'c': |
| 183 | + count++; |
| 184 | + fallthrough; |
| 185 | + case '1': case '2': case '4': case '8': |
| 186 | + count++; |
| 187 | + break; |
| 188 | + } |
| 189 | + } |
| 190 | + fclose(fp); |
| 191 | + |
| 192 | + return count; |
| 193 | +} |
| 194 | + |
| 195 | +static bool cpus_offline_empty(void) |
| 196 | +{ |
| 197 | + char offline_cpus_str[64]; |
| 198 | + FILE *fp; |
| 199 | + |
| 200 | + fp = fopen("/sys/devices/system/cpu/offline", "r"); |
| 201 | + if (!fp) { |
| 202 | + ksft_perror("Could not open /sys/devices/system/cpu/offline"); |
| 203 | + return 0; |
| 204 | + } |
| 205 | + |
| 206 | + if (fscanf(fp, "%63s", offline_cpus_str) < 0) { |
| 207 | + if (!errno) { |
| 208 | + fclose(fp); |
| 209 | + return 1; |
| 210 | + } |
| 211 | + ksft_perror("Could not read /sys/devices/system/cpu/offline"); |
| 212 | + } |
| 213 | + |
| 214 | + fclose(fp); |
| 215 | + |
| 216 | + return 0; |
| 217 | +} |
| 218 | + |
| 219 | +/* |
| 220 | + * Detect SNC by comparing #CPUs in node0 with #CPUs sharing LLC with CPU0. |
| 221 | + * If any CPUs are offline declare the detection as unreliable. |
| 222 | + */ |
| 223 | +int snc_nodes_per_l3_cache(void) |
| 224 | +{ |
| 225 | + int node_cpus, cache_cpus; |
| 226 | + static int snc_mode; |
| 227 | + |
| 228 | + if (!snc_mode) { |
| 229 | + snc_mode = 1; |
| 230 | + if (!cpus_offline_empty()) { |
| 231 | + ksft_print_msg("Runtime SNC detection unreliable due to offline CPUs.\n"); |
| 232 | + ksft_print_msg("Setting SNC mode to disabled.\n"); |
| 233 | + snc_unreliable = 1; |
| 234 | + return snc_mode; |
| 235 | + } |
| 236 | + node_cpus = count_sys_bitmap_bits("/sys/devices/system/node/node0/cpumap"); |
| 237 | + cache_cpus = count_sys_bitmap_bits("/sys/devices/system/cpu/cpu0/cache/index3/shared_cpu_map"); |
| 238 | + |
| 239 | + if (!node_cpus || !cache_cpus) { |
| 240 | + ksft_print_msg("Could not determine Sub-NUMA Cluster mode.\n"); |
| 241 | + snc_unreliable = 1; |
| 242 | + return snc_mode; |
| 243 | + } |
| 244 | + snc_mode = cache_cpus / node_cpus; |
| 245 | + |
| 246 | + if (snc_mode > 1) |
| 247 | + ksft_print_msg("SNC-%d mode discovered.\n", snc_mode); |
| 248 | + } |
| 249 | + |
| 250 | + return snc_mode; |
| 251 | +} |
| 252 | + |
159 | 253 | /* |
160 | 254 | * get_cache_size - Get cache size for a specified CPU |
161 | 255 | * @cpu_no: CPU number |
@@ -211,6 +305,17 @@ int get_cache_size(int cpu_no, const char *cache_type, unsigned long *cache_size |
211 | 305 | break; |
212 | 306 | } |
213 | 307 |
|
| 308 | + /* |
| 309 | + * The amount of cache represented by each bit in the masks |
| 310 | + * in the schemata file is reduced by a factor equal to SNC |
| 311 | + * nodes per L3 cache. |
| 312 | + * E.g. on a SNC-2 system with a 100MB L3 cache a test that |
| 313 | + * allocates memory from its local SNC node (default behavior |
| 314 | + * without using libnuma) will only see 50 MB llc_occupancy |
| 315 | + * with a fully populated L3 mask in the schemata file. |
| 316 | + */ |
| 317 | + if (cache_num == 3) |
| 318 | + *cache_size /= snc_nodes_per_l3_cache(); |
214 | 319 | return 0; |
215 | 320 | } |
216 | 321 |
|
|
0 commit comments