henrikg@webrtc.org | c58ef08 | 2011-11-08 08:44:17 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "system_wrappers/include/cpu_info.h" |
henrikg@webrtc.org | c58ef08 | 2011-11-08 08:44:17 +0000 | [diff] [blame] | 12 | |
kthelgason | 62ffe9a | 2017-01-09 01:22:16 -0800 | [diff] [blame] | 13 | #if defined(WEBRTC_WIN) |
kthelgason | 62ffe9a | 2017-01-09 01:22:16 -0800 | [diff] [blame] | 14 | #include <windows.h> |
kthelgason | 62ffe9a | 2017-01-09 01:22:16 -0800 | [diff] [blame] | 15 | #elif defined(WEBRTC_LINUX) |
| 16 | #include <unistd.h> |
Fabrice de Gans-Riberi | 7d8c27e | 2018-12-14 16:13:23 -0800 | [diff] [blame] | 17 | #elif defined(WEBRTC_MAC) |
kthelgason | 62ffe9a | 2017-01-09 01:22:16 -0800 | [diff] [blame] | 18 | #include <sys/sysctl.h> |
Fabrice de Gans-Riberi | 7d8c27e | 2018-12-14 16:13:23 -0800 | [diff] [blame] | 19 | #elif defined(WEBRTC_FUCHSIA) |
| 20 | #include <zircon/syscalls.h> |
kthelgason | 62ffe9a | 2017-01-09 01:22:16 -0800 | [diff] [blame] | 21 | #endif |
| 22 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "rtc_base/logging.h" |
kthelgason | 62ffe9a | 2017-01-09 01:22:16 -0800 | [diff] [blame] | 24 | |
| 25 | namespace internal { |
| 26 | static int DetectNumberOfCores() { |
| 27 | // We fall back on assuming a single core in case of errors. |
| 28 | int number_of_cores = 1; |
| 29 | |
| 30 | #if defined(WEBRTC_WIN) |
| 31 | SYSTEM_INFO si; |
brucedawson | 2178f70 | 2017-07-13 10:06:12 -0700 | [diff] [blame] | 32 | GetNativeSystemInfo(&si); |
kthelgason | 62ffe9a | 2017-01-09 01:22:16 -0800 | [diff] [blame] | 33 | number_of_cores = static_cast<int>(si.dwNumberOfProcessors); |
| 34 | #elif defined(WEBRTC_LINUX) || defined(WEBRTC_ANDROID) |
| 35 | number_of_cores = static_cast<int>(sysconf(_SC_NPROCESSORS_ONLN)); |
Jiawei Ou | aa7bc7e | 2018-12-11 14:25:25 -0800 | [diff] [blame] | 36 | #elif defined(WEBRTC_MAC) || defined(WEBRTC_IOS) |
kthelgason | 62ffe9a | 2017-01-09 01:22:16 -0800 | [diff] [blame] | 37 | int name[] = {CTL_HW, HW_AVAILCPU}; |
| 38 | size_t size = sizeof(number_of_cores); |
| 39 | if (0 != sysctl(name, 2, &number_of_cores, &size, NULL, 0)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 40 | RTC_LOG(LS_ERROR) << "Failed to get number of cores"; |
kthelgason | 62ffe9a | 2017-01-09 01:22:16 -0800 | [diff] [blame] | 41 | number_of_cores = 1; |
| 42 | } |
Fabrice de Gans-Riberi | 7d8c27e | 2018-12-14 16:13:23 -0800 | [diff] [blame] | 43 | #elif defined(WEBRTC_FUCHSIA) |
| 44 | number_of_cores = zx_system_get_num_cpus(); |
kthelgason | 62ffe9a | 2017-01-09 01:22:16 -0800 | [diff] [blame] | 45 | #else |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 46 | RTC_LOG(LS_ERROR) << "No function to get number of cores"; |
kthelgason | 62ffe9a | 2017-01-09 01:22:16 -0800 | [diff] [blame] | 47 | #endif |
| 48 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 49 | RTC_LOG(LS_INFO) << "Available number of cores: " << number_of_cores; |
kthelgason | 62ffe9a | 2017-01-09 01:22:16 -0800 | [diff] [blame] | 50 | |
| 51 | return number_of_cores; |
| 52 | } |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 53 | } // namespace internal |
henrikg@webrtc.org | c58ef08 | 2011-11-08 08:44:17 +0000 | [diff] [blame] | 54 | |
| 55 | namespace webrtc { |
| 56 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 57 | uint32_t CpuInfo::DetectNumberOfCores() { |
kthelgason | 62ffe9a | 2017-01-09 01:22:16 -0800 | [diff] [blame] | 58 | // Statically cache the number of system cores available since if the process |
| 59 | // is running in a sandbox, we may only be able to read the value once (before |
| 60 | // the sandbox is initialized) and not thereafter. |
| 61 | // For more information see crbug.com/176522. |
| 62 | static uint32_t logical_cpus = 0; |
| 63 | if (!logical_cpus) |
| 64 | logical_cpus = static_cast<uint32_t>(internal::DetectNumberOfCores()); |
| 65 | return logical_cpus; |
henrikg@webrtc.org | c58ef08 | 2011-11-08 08:44:17 +0000 | [diff] [blame] | 66 | } |
| 67 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 68 | } // namespace webrtc |