Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Use sysconf(_SC_NPROCESSORS_CONF) in PAL and GC ONLY on ARM and ARM64 #18289

Merged
merged 1 commit into from
Jun 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/gc/unix/gcenv.unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@
#include <unistd.h> // sysconf
#include "globals.h"

#if defined(_ARM_) || defined(_ARM64_)
#define SYSCONF_GET_NUMPROCS _SC_NPROCESSORS_CONF
#else
#define SYSCONF_GET_NUMPROCS _SC_NPROCESSORS_ONLN
#endif

// The cachced number of logical CPUs observed.
static uint32_t g_logicalCpuCount = 0;

Expand Down Expand Up @@ -67,7 +73,7 @@ bool GCToOSInterface::Initialize()
g_pageSizeUnixInl = uint32_t((pageSize > 0) ? pageSize : 0x1000);

// Calculate and cache the number of processors on this machine
int cpuCount = sysconf(_SC_NPROCESSORS_CONF);
int cpuCount = sysconf(SYSCONF_GET_NUMPROCS);
if (cpuCount == -1)
{
return false;
Expand Down
12 changes: 10 additions & 2 deletions src/pal/src/misc/sysinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,18 @@ PAL_GetLogicalCpuCountFromOS()
int nrcpus = 0;

#if HAVE_SYSCONF
nrcpus = sysconf(_SC_NPROCESSORS_CONF);

#if defined(_ARM_) || defined(_ARM64_)
#define SYSCONF_GET_NUMPROCS _SC_NPROCESSORS_CONF
#define SYSCONF_GET_NUMPROCS_NAME "_SC_NPROCESSORS_CONF"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A nit - instead of defining the macro and its string version, you can define a stringification macro that will make the conversion automatically:

#define STRINGIFY1(s) #s
#define STRINGIFY(s) STRINGIFY1(s)

Then you can write the assert like this:

ASSERT("sysconf failed for %s (%d)\n", STRINGIFY(SYSCONF_GET_NUMPROCS), errno); 

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I though about this once but I could not justify doing stringification instead of hard-coding names of the variables.

Unfortunately, doing stringification your way would not work for this example

#define _SC_NPROCESSORS_CONF 57
#define _SC_NPROCESSORS_ONLN 58

#define SYSCONF_GET_NUMPROCS       _SC_NPROCESSORS_CONF 
//#define SYSCONF_GET_NUMPROCS       _SC_NPROCESSORS_ONLN

#define STRINGIFY1(s) #s
#define STRINGIFY(s) STRINGIFY1(s)

int main()
{
    printf("STRINGIFY(SYSCONF_GET_NUMPROCS) is %s\n", STRINGIFY(SYSCONF_GET_NUMPROCS)); // Output: STRINGIFY(SYSCONF_GET_NUMPROCS) is 57
    return 0;
}

This is how SC_NPROCESSORS_CONF is defined in FreeBSD https://github.com/freebsd/freebsd/blob/06362ad468fc11671364e34ed413ea9c19d858f1/include/unistd.h#L292

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, thanks, forget about it then.

#else
#define SYSCONF_GET_NUMPROCS _SC_NPROCESSORS_ONLN
#define SYSCONF_GET_NUMPROCS_NAME "_SC_NPROCESSORS_ONLN"
#endif
nrcpus = sysconf(SYSCONF_GET_NUMPROCS);
if (nrcpus < 1)
{
ASSERT("sysconf failed for _SC_NPROCESSORS_CONF (%d)\n", errno);
ASSERT("sysconf failed for %s (%d)\n", SYSCONF_GET_NUMPROCS_NAME, errno);
}
#elif HAVE_SYSCTL
int rc;
Expand Down