blob: c17d59d7f7bd2bf65f7b00e26c54694d6a42409d [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>
17#endif
18#if defined(WEBRTC_MAC)
19#include <sys/sysctl.h>
20#endif
21
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/logging.h"
kthelgason62ffe9a2017-01-09 01:22:16 -080023
24namespace internal {
25static int DetectNumberOfCores() {
26 // We fall back on assuming a single core in case of errors.
27 int number_of_cores = 1;
28
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));
35#elif defined(WEBRTC_MAC)
36 int name[] = {CTL_HW, HW_AVAILCPU};
37 size_t size = sizeof(number_of_cores);
38 if (0 != sysctl(name, 2, &number_of_cores, &size, NULL, 0)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010039 RTC_LOG(LS_ERROR) << "Failed to get number of cores";
kthelgason62ffe9a2017-01-09 01:22:16 -080040 number_of_cores = 1;
41 }
42#else
Mirko Bonadei675513b2017-11-09 11:09:25 +010043 RTC_LOG(LS_ERROR) << "No function to get number of cores";
kthelgason62ffe9a2017-01-09 01:22:16 -080044#endif
45
Mirko Bonadei675513b2017-11-09 11:09:25 +010046 RTC_LOG(LS_INFO) << "Available number of cores: " << number_of_cores;
kthelgason62ffe9a2017-01-09 01:22:16 -080047
48 return number_of_cores;
49}
Karl Wiberg79eb1d92017-11-08 12:26:07 +010050} // namespace internal
henrikg@webrtc.orgc58ef082011-11-08 08:44:17 +000051
52namespace webrtc {
53
pbos@webrtc.org046deb92013-04-09 09:06:11 +000054uint32_t CpuInfo::DetectNumberOfCores() {
kthelgason62ffe9a2017-01-09 01:22:16 -080055 // Statically cache the number of system cores available since if the process
56 // is running in a sandbox, we may only be able to read the value once (before
57 // the sandbox is initialized) and not thereafter.
58 // For more information see crbug.com/176522.
59 static uint32_t logical_cpus = 0;
60 if (!logical_cpus)
61 logical_cpus = static_cast<uint32_t>(internal::DetectNumberOfCores());
62 return logical_cpus;
henrikg@webrtc.orgc58ef082011-11-08 08:44:17 +000063}
64
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +000065} // namespace webrtc