Skip to content

Commit 2eb2197

Browse files
eradmanDimCitus
andauthored
Use sysctl(3) on BSD (#733)
* Use sysctl(3) on BSD sysctlbyname(3) is a convenience wrapper for sysctl(3) that is implemented on FreeBSD and MacOS, but not on OpenBSD. Include sys/param.h to ensure that BSD is defined. Tests on OpenBSD: - make check - initialize a monitor node and 2 nodes - perform failover Tests on MacOS: - make check * Use 64-bit sysctl flags to detect total RAM Tested on - MacOS 11.4 - FreeBSD 11.4 - OpenBSD 6.9 PG_AUTOCTL_DEBUG=1 pg_autoctl do pgsetup tune --pgdata /tmp -vv Co-authored-by: Dimitri Fontaine <dimitri@citusdata.com>
1 parent 7a878a4 commit 2eb2197

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

src/bin/pg_autoctl/system_utils.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#else
1313
#include <sys/types.h>
1414
#include <sys/sysctl.h>
15+
#include <sys/param.h>
1516
#endif
1617

1718
#include <math.h>
@@ -73,26 +74,34 @@ get_system_info_linux(SystemInfo *sysInfo)
7374

7475

7576
/*
76-
* FreeBSD, OpenBSD, and darwin use the sysctlbyname(3) API.
77+
* FreeBSD, OpenBSD, and darwin use the sysctl(3) API.
7778
*/
7879
#if defined(__APPLE__) || defined(BSD)
7980
static bool
8081
get_system_info_bsd(SystemInfo *sysInfo)
8182
{
8283
unsigned int ncpu = 0; /* the API requires an integer here */
84+
int ncpuMIB[2] = { CTL_HW, HW_NCPU };
85+
#if defined(HW_MEMSIZE)
86+
int ramMIB[2] = { CTL_HW, HW_MEMSIZE }; /* MacOS */
87+
#elif defined(HW_PHYSMEM64)
88+
int ramMIB[2] = { CTL_HW, HW_PHYSMEM64 }; /* OpenBSD */
89+
#else
90+
int ramMIB[2] = { CTL_HW, HW_PHYSMEM }; /* FreeBSD */
91+
#endif
8392

8493
size_t cpuSize = sizeof(ncpu);
8594
size_t memSize = sizeof(sysInfo->totalram);
8695

87-
if (sysctlbyname("hw.ncpu", &ncpu, &cpuSize, NULL, 0) != 0)
96+
if (sysctl(ncpuMIB, 2, &ncpu, &cpuSize, NULL, 0) == -1)
8897
{
8998
log_error("Failed to probe number of CPUs: %m");
9099
return false;
91100
}
92101

93102
sysInfo->ncpu = (unsigned short) ncpu;
94103

95-
if (sysctlbyname("hw.memsize", &(sysInfo->totalram), &memSize, NULL, 0) != 0)
104+
if (sysctl(ramMIB, 2, &(sysInfo->totalram), &memSize, NULL, 0) == -1)
96105
{
97106
log_error("Failed to probe Physical Memory: %m");
98107
return false;

0 commit comments

Comments
 (0)