Skip to content

Commit ea88133

Browse files
committed
FreeBSD: Improve CPU core count handling
This improves the CPU core count handling for FreeBSD. This uses sysconf to grab the number of configured processors when logical and physical CPU counts are requested, and then pthread_getaffinity_np and the number of active processors. This takes into account the number of available cores when the process is restricted to a subset of the processors. This also fixes the `dispatch_workqueye` test, which has its own implementation to extract the core count. FreeBSD does not have an `hw.activecpu` syscall, so the syscallbyname was failing and the activecpu count was garbage from the stack. I've updated the test to initialize the value with `0xa1a1a1` to make the garbage clearer. Also update the test for FreeBSD to take advantage of sysconf and pthread_getaffinity_np in the test. Fixes: #897
1 parent a6f0512 commit ea88133

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

src/shims.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,6 @@
5858
#define DISPATCH_WORKQ_MAX_PTHREAD_COUNT 255
5959
#endif
6060

61-
#include "shims/hw_config.h"
62-
#include "shims/priority.h"
63-
6461
#if HAVE_PTHREAD_NP_H
6562
#include <pthread_np.h>
6663
#endif
@@ -69,6 +66,9 @@
6966
#include <pthread/private.h>
7067
#endif
7168

69+
#include "shims/hw_config.h"
70+
#include "shims/priority.h"
71+
7272
#if !HAVE_DECL_FD_COPY
7373
#define FD_COPY(f, t) (void)(*(t) = *(f))
7474
#endif

src/shims/hw_config.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,14 @@ static inline uint32_t
102102
_dispatch_hw_get_config(_dispatch_hw_config_t c)
103103
{
104104
uint32_t val = 1;
105-
#if defined(__linux__) && HAVE_SYSCONF
105+
#if defined(__FreeBSD__) || (defined(__linux__) && HAVE_SYSCONF)
106106
switch (c) {
107107
case _dispatch_hw_config_logical_cpus:
108108
case _dispatch_hw_config_physical_cpus:
109109
return (uint32_t)sysconf(_SC_NPROCESSORS_CONF);
110110
case _dispatch_hw_config_active_cpus:
111111
{
112-
#ifdef __USE_GNU
112+
#if defined(__FreeBSD__) || __USE_GNU
113113
// Prefer pthread_getaffinity_np because it considers
114114
// scheduler cpu affinity. This matters if the program
115115
// is restricted to a subset of the online cpus (eg via numactl).

tests/dispatch_workqueue.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
#if defined(__linux__)
55
// For pthread_getaffinity_np()
66
#include <pthread.h>
7+
#elif defined(__FreeBSD__)
8+
// for pthread_getaffinity_np / cpu_set_t
9+
#include <pthread.h>
10+
#include <pthread_np.h>
711
#endif
812

913
struct test_context {
@@ -37,11 +41,11 @@ spin(void *context)
3741
static uint32_t
3842
activecpu(void)
3943
{
40-
uint32_t activecpu;
41-
#if defined(__linux__) || defined(__OpenBSD__)
44+
uint32_t activecpu = 0xa1a1a1;
45+
#if defined(__linux__) || defined(__OpenBSD__) || defined(__FreeBSD__)
4246
activecpu = (uint32_t)sysconf(_SC_NPROCESSORS_ONLN);
4347

44-
#if defined(__linux__) && __USE_GNU
48+
#if defined(__FreeBSD__) || (defined(__linux__) && __USE_GNU)
4549
cpu_set_t cpuset;
4650
if (pthread_getaffinity_np(pthread_self(),
4751
sizeof(cpu_set_t),

0 commit comments

Comments
 (0)