Skip to content

SR-10611: ProcessInfo.activeProcessorCount too high in containerised Linux #2215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 8, 2019
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: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ include(SwiftSupport)
include(GNUInstallDirs)
include(ExternalProject)

if(CMAKE_SYSTEM_NAME STREQUAL Linux)
include(CheckSymbolExists)
include(CheckIncludeFile)
list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
check_include_file("sched.h" HAVE_SCHED_H)
check_symbol_exists(sched_getaffinity "sched.h" HAVE_SCHED_GETAFFINITY)
endif()

string(TOLOWER ${CMAKE_SYSTEM_NAME} swift_os)
get_swift_host_arch(swift_arch)

Expand Down
13 changes: 13 additions & 0 deletions CoreFoundation/Base.subproj/CFUtilities.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@
#include <pthread.h>
#endif

#if DEPLOYMENT_TARGET_LINUX
#ifdef HAVE_SCHED_GETAFFINITY
#include <sched.h>
#endif
#endif


#if !defined(CF_HAVE_HW_CONFIG_COMMPAGE) && defined(_COMM_PAGE_LOGICAL_CPUS) && defined(_COMM_PAGE_PHYSICAL_CPUS) && defined(_COMM_PAGE_ACTIVE_CPUS) && !__has_feature(address_sanitizer)
Expand Down Expand Up @@ -451,6 +456,14 @@ CF_PRIVATE CFIndex __CFActiveProcessorCount() {
pcnt = 0;
}
#elif DEPLOYMENT_TARGET_LINUX

#ifdef HAVE_SCHED_GETAFFINITY
cpu_set_t set;
if (sched_getaffinity (getpid(), sizeof(set), &set) == 0) {
return CPU_COUNT (&set);
}
#endif

pcnt = sysconf(_SC_NPROCESSORS_ONLN);
#else
// Assume the worst
Expand Down
6 changes: 5 additions & 1 deletion CoreFoundation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,11 @@ if(CMAKE_SYSTEM_NAME STREQUAL Linux OR CMAKE_SYSTEM_NAME STREQUAL Android)
PRIVATE
-DDEPLOYMENT_TARGET_LINUX
-D_GNU_SOURCE)
if(HAVE_SCHED_GETAFFINITY)
target_compile_definitions(CoreFoundation
PRIVATE
-DHAVE_SCHED_GETAFFINITY)
endif()
elseif(CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
target_compile_definitions(CoreFoundation
PRIVATE
Expand Down Expand Up @@ -516,4 +521,3 @@ install(DIRECTORY
${CMAKE_INSTALL_PREFIX}/System/Library/Frameworks
USE_SOURCE_PERMISSIONS
PATTERN PrivateHeaders EXCLUDE)