blob: eff720371afe3e98ffbaea126fe2f4592d4f9431 [file] [log] [blame]
henrikg@webrtc.orgc58ef082011-11-08 08:44:17 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "system_wrappers/include/cpu_info.h"
henrikg@webrtc.orgc58ef082011-11-08 08:44:17 +000012
kthelgason62ffe9a2017-01-09 01:22:16 -080013#if defined(WEBRTC_WIN)
kthelgason62ffe9a2017-01-09 01:22:16 -080014#include <windows.h>
kthelgason62ffe9a2017-01-09 01:22:16 -080015#elif defined(WEBRTC_LINUX)
16#include <unistd.h>
Fabrice de Gans-Riberi7d8c27e2018-12-14 16:13:23 -080017#elif defined(WEBRTC_MAC)
kthelgason62ffe9a2017-01-09 01:22:16 -080018#include <sys/sysctl.h>
Fabrice de Gans-Riberi7d8c27e2018-12-14 16:13:23 -080019#elif defined(WEBRTC_FUCHSIA)
20#include <zircon/syscalls.h>
kthelgason62ffe9a2017-01-09 01:22:16 -080021#endif
22
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "rtc_base/logging.h"
kthelgason62ffe9a2017-01-09 01:22:16 -080024
25namespace internal {
26static int DetectNumberOfCores() {
Mirko Bonadeia8cf3b72019-05-24 13:47:23 +020027 int number_of_cores;
kthelgason62ffe9a2017-01-09 01:22:16 -080028
29#if defined(WEBRTC_WIN)
30 SYSTEM_INFO si;
brucedawson2178f702017-07-13 10:06:12 -070031 GetNativeSystemInfo(&si);
kthelgason62ffe9a2017-01-09 01:22:16 -080032 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));
Mirko Bonadeifb8a3e42022-01-03 16:02:19 +010035 if (number_of_cores <= 0) {
Mirko Bonadeia8cf3b72019-05-24 13:47:23 +020036 RTC_LOG(LS_ERROR) << "Failed to get number of cores";
37 number_of_cores = 1;
38 }
Jiawei Ouaa7bc7e2018-12-11 14:25:25 -080039#elif defined(WEBRTC_MAC) || defined(WEBRTC_IOS)
kthelgason62ffe9a2017-01-09 01:22:16 -080040 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)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010043 RTC_LOG(LS_ERROR) << "Failed to get number of cores";
kthelgason62ffe9a2017-01-09 01:22:16 -080044 number_of_cores = 1;
45 }
Fabrice de Gans-Riberi7d8c27e2018-12-14 16:13:23 -080046#elif defined(WEBRTC_FUCHSIA)
47 number_of_cores = zx_system_get_num_cpus();
kthelgason62ffe9a2017-01-09 01:22:16 -080048#else
Mirko Bonadei675513b2017-11-09 11:09:25 +010049 RTC_LOG(LS_ERROR) << "No function to get number of cores";
Mirko Bonadeia8cf3b72019-05-24 13:47:23 +020050 number_of_cores = 1;
kthelgason62ffe9a2017-01-09 01:22:16 -080051#endif
52
Mirko Bonadei675513b2017-11-09 11:09:25 +010053 RTC_LOG(LS_INFO) << "Available number of cores: " << number_of_cores;
kthelgason62ffe9a2017-01-09 01:22:16 -080054
Mirko Bonadeia8cf3b72019-05-24 13:47:23 +020055 RTC_CHECK_GT(number_of_cores, 0);
kthelgason62ffe9a2017-01-09 01:22:16 -080056 return number_of_cores;
57}
Karl Wiberg79eb1d92017-11-08 12:26:07 +010058} // namespace internal
henrikg@webrtc.orgc58ef082011-11-08 08:44:17 +000059
60namespace webrtc {
61
pbos@webrtc.org046deb92013-04-09 09:06:11 +000062uint32_t CpuInfo::DetectNumberOfCores() {
kthelgason62ffe9a2017-01-09 01:22:16 -080063 // 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.
Mirko Bonadeia8cf3b72019-05-24 13:47:23 +020067 static const uint32_t logical_cpus =
68 static_cast<uint32_t>(internal::DetectNumberOfCores());
kthelgason62ffe9a2017-01-09 01:22:16 -080069 return logical_cpus;
henrikg@webrtc.orgc58ef082011-11-08 08:44:17 +000070}
71
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +000072} // namespace webrtc