1 /* 2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #include "system_wrappers/include/cpu_info.h" 12 13 #if defined(WEBRTC_WIN) 14 #include <windows.h> 15 #elif defined(WEBRTC_LINUX) 16 #include <unistd.h> 17 #elif defined(WEBRTC_MAC) 18 #include <sys/sysctl.h> 19 #elif defined(WEBRTC_FUCHSIA) 20 #include <zircon/syscalls.h> 21 #endif 22 23 #include "rtc_base/logging.h" 24 25 namespace internal { DetectNumberOfCores()26static int DetectNumberOfCores() { 27 int number_of_cores; 28 29 #if defined(WEBRTC_WIN) 30 SYSTEM_INFO si; 31 GetNativeSystemInfo(&si); 32 number_of_cores = static_cast<int>(si.dwNumberOfProcessors); 33 #elif defined(WEBRTC_LINUX) || defined(WEBRTC_ANDROID) 34 number_of_cores = static_cast<int>(sysconf(_SC_NPROCESSORS_ONLN)); 35 if (number_of_cores < 0) { 36 RTC_LOG(LS_ERROR) << "Failed to get number of cores"; 37 number_of_cores = 1; 38 } 39 #elif defined(WEBRTC_MAC) || defined(WEBRTC_IOS) 40 int name[] = {CTL_HW, HW_AVAILCPU}; 41 size_t size = sizeof(number_of_cores); 42 if (0 != sysctl(name, 2, &number_of_cores, &size, NULL, 0)) { 43 RTC_LOG(LS_ERROR) << "Failed to get number of cores"; 44 number_of_cores = 1; 45 } 46 #elif defined(WEBRTC_FUCHSIA) 47 number_of_cores = zx_system_get_num_cpus(); 48 #else 49 RTC_LOG(LS_ERROR) << "No function to get number of cores"; 50 number_of_cores = 1; 51 #endif 52 53 RTC_LOG(LS_INFO) << "Available number of cores: " << number_of_cores; 54 55 RTC_CHECK_GT(number_of_cores, 0); 56 return number_of_cores; 57 } 58 } // namespace internal 59 60 namespace webrtc { 61 DetectNumberOfCores()62uint32_t CpuInfo::DetectNumberOfCores() { 63 // Statically cache the number of system cores available since if the process 64 // is running in a sandbox, we may only be able to read the value once (before 65 // the sandbox is initialized) and not thereafter. 66 // For more information see crbug.com/176522. 67 static const uint32_t logical_cpus = 68 static_cast<uint32_t>(internal::DetectNumberOfCores()); 69 return logical_cpus; 70 } 71 72 } // namespace webrtc 73