Line data Source code
1 : #if !defined(__linux__) 2 : #error "Linux platform file is part of non-Linux build." 3 : #endif 4 : 5 : #include "source/server/options_impl_platform_linux.h" 6 : 7 : #include <sched.h> 8 : 9 : #include <thread> 10 : 11 : #include "source/common/api/os_sys_calls_impl_linux.h" 12 : #include "source/server/options_impl_platform.h" 13 : 14 : namespace Envoy { 15 : 16 0 : uint32_t OptionsImplPlatformLinux::getCpuAffinityCount(unsigned int hw_threads) { 17 0 : unsigned int threads = 0; 18 0 : pid_t pid = getpid(); 19 0 : cpu_set_t mask; 20 0 : auto& linux_os_syscalls = Api::LinuxOsSysCallsSingleton::get(); 21 : 22 0 : CPU_ZERO(&mask); 23 0 : const Api::SysCallIntResult result = 24 0 : linux_os_syscalls.sched_getaffinity(pid, sizeof(cpu_set_t), &mask); 25 0 : if (result.return_value_ == -1) { 26 : // Fall back to number of hardware threads. 27 0 : return hw_threads; 28 0 : } 29 : 30 0 : threads = CPU_COUNT(&mask); 31 : 32 : // Sanity check. 33 0 : if (threads > 0 && threads <= hw_threads) { 34 0 : return threads; 35 0 : } 36 : 37 0 : return hw_threads; 38 0 : } 39 : 40 0 : uint32_t OptionsImplPlatform::getCpuCount() { 41 0 : unsigned int hw_threads = std::max(1U, std::thread::hardware_concurrency()); 42 0 : return OptionsImplPlatformLinux::getCpuAffinityCount(hw_threads); 43 0 : } 44 : 45 : } // namespace Envoy