| 1 |
// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
#ifdef _OPENMP |
| 5 |
|
| 6 |
#include <stdbool.h> |
| 7 |
#include <stdio.h> |
| 8 |
#include <stdlib.h> |
| 9 |
|
| 10 |
#include "cleanup.h" |
| 11 |
#include "hash_table.h" |
| 12 |
#include "openmp.h" |
| 13 |
#include "util.h" |
| 14 |
|
| 15 |
DEFINE_HASH_SET(int_set, int, int_key_hash_pair, scalar_key_eq); |
| 16 |
|
| 17 |
int drgn_num_threads; |
| 18 |
|
| 19 |
struct cpu_list_state { |
| 20 |
int current; |
| 21 |
int end; |
| 22 |
}; |
| 23 |
|
| 24 |
static int cpu_list_next(FILE *file, struct cpu_list_state *state) |
| 25 |
{ |
| 26 |
if (state->current >= state->end) { |
| 27 |
if (fscanf(file, "%d", &state->current) < 1) |
| 28 |
return -1; |
| 29 |
if (fscanf(file, "-%d", &state->end) >= 1) |
| 30 |
state->end++; |
| 31 |
else |
| 32 |
state->end = state->current + 1; |
| 33 |
fgetc(file); |
| 34 |
} |
| 35 |
return state->current++; |
| 36 |
} |
| 37 |
|
| 38 |
// Get the number of online CPU cores, ignoring hardware threads. Returns 0 on |
| 39 |
// failure. |
| 40 |
static int drgn_num_online_cpu_cores(void) |
| 41 |
{ |
| 42 |
#define SIBLINGS_FORMAT "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list" |
| 43 |
char siblings_path[sizeof(SIBLINGS_FORMAT) |
| 44 |
- sizeof("%d") |
| 45 |
+ max_decimal_length(int) |
| 46 |
+ 1]; |
| 47 |
|
| 48 |
int num_cores = 0; |
| 49 |
HASH_TABLE(int_set, cpus_seen); |
| 50 |
|
| 51 |
_cleanup_fclose_ FILE *online = fopen("/sys/devices/system/cpu/online", "r"); |
| 52 |
if (!online) |
| 53 |
return 0; |
| 54 |
struct cpu_list_state online_state = {}; |
| 55 |
int cpu; |
| 56 |
while ((cpu = cpu_list_next(online, &online_state)) >= 0) { |
| 57 |
if (int_set_search(&cpus_seen, &cpu).entry) |
| 58 |
continue; |
| 59 |
|
| 60 |
num_cores++; |
| 61 |
|
| 62 |
snprintf(siblings_path, sizeof(siblings_path), SIBLINGS_FORMAT, |
| 63 |
cpu); |
| 64 |
_cleanup_fclose_ FILE *siblings = fopen(siblings_path, "r"); |
| 65 |
if (!siblings) |
| 66 |
continue; |
| 67 |
struct cpu_list_state siblings_state = {}; |
| 68 |
int sibling_cpu; |
| 69 |
while ((sibling_cpu = cpu_list_next(siblings, &siblings_state)) |
| 70 |
>= 0) { |
| 71 |
if (int_set_insert(&cpus_seen, &sibling_cpu, NULL) < 0) |
| 72 |
return 0; |
| 73 |
} |
| 74 |
} |
| 75 |
return num_cores; |
| 76 |
} |
| 77 |
|
| 78 |
void drgn_init_num_threads(void) |
| 79 |
{ |
| 80 |
// Skip if already initialized. |
| 81 |
if (__atomic_load_n(&drgn_num_threads, __ATOMIC_RELAXED) != 0) |
| 82 |
return; |
| 83 |
|
| 84 |
int num_threads = omp_get_max_threads(); |
| 85 |
// If the number of threads was set explicitly, use the current OpenMP |
| 86 |
// setting. |
| 87 |
if (!getenv("OMP_NUM_THREADS")) { |
| 88 |
// Simultaneous multithreading rarely helps, and often slows |
| 89 |
// down, our parallel indexing. Limit the number of threads to |
| 90 |
// the number of CPU cores or the current OpenMP setting, |
| 91 |
// whichever is smaller. If getting the number of cores fails, |
| 92 |
// fall back to the current OpenMP setting. |
| 93 |
int num_cores = drgn_num_online_cpu_cores(); |
| 94 |
if (num_cores > 0 && num_cores < num_threads) |
| 95 |
num_threads = num_cores; |
| 96 |
} |
| 97 |
|
| 98 |
// Multiple threads may be initializing this at the same time, and |
| 99 |
// theoretically they could have decided on a different number of |
| 100 |
// threads if CPUs were hot(un)plugged in the meantime, so make sure |
| 101 |
// this is only set once. |
| 102 |
int expected = 0; |
| 103 |
__atomic_compare_exchange_n(&drgn_num_threads, &expected, num_threads, |
| 104 |
false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); |
| 105 |
} |
| 106 |
|
| 107 |
#endif /* _OPENMP */ |