blob: fa70ced410620fe0607ee107743947017f50062a [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>
Karl Wiberg79eb1d92017-11-08 12:26:07 +010015#include <winsock2.h>
kthelgason62ffe9a2017-01-09 01:22:16 -080016#ifndef EXCLUDE_D3D9
17#include <d3d9.h>
18#endif
19#elif defined(WEBRTC_LINUX)
20#include <unistd.h>
21#endif
22#if defined(WEBRTC_MAC)
23#include <sys/sysctl.h>
24#endif
25
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "rtc_base/logging.h"
kthelgason62ffe9a2017-01-09 01:22:16 -080027
28namespace internal {
29static int DetectNumberOfCores() {
30 // We fall back on assuming a single core in case of errors.
31 int number_of_cores = 1;
32
33#if defined(WEBRTC_WIN)
34 SYSTEM_INFO si;
brucedawson2178f702017-07-13 10:06:12 -070035 GetNativeSystemInfo(&si);
kthelgason62ffe9a2017-01-09 01:22:16 -080036 number_of_cores = static_cast<int>(si.dwNumberOfProcessors);
37#elif defined(WEBRTC_LINUX) || defined(WEBRTC_ANDROID)
38 number_of_cores = static_cast<int>(sysconf(_SC_NPROCESSORS_ONLN));
39#elif defined(WEBRTC_MAC)
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 LOG(LS_ERROR) << "Failed to get number of cores";
44 number_of_cores = 1;
45 }
46#else
47 LOG(LS_ERROR) << "No function to get number of cores";
48#endif
49
50 LOG(LS_INFO) << "Available number of cores: " << number_of_cores;
51
52 return number_of_cores;
53}
Karl Wiberg79eb1d92017-11-08 12:26:07 +010054} // namespace internal
henrikg@webrtc.orgc58ef082011-11-08 08:44:17 +000055
56namespace webrtc {
57
pbos@webrtc.org046deb92013-04-09 09:06:11 +000058uint32_t CpuInfo::DetectNumberOfCores() {
kthelgason62ffe9a2017-01-09 01:22:16 -080059 // Statically cache the number of system cores available since if the process
60 // is running in a sandbox, we may only be able to read the value once (before
61 // the sandbox is initialized) and not thereafter.
62 // For more information see crbug.com/176522.
63 static uint32_t logical_cpus = 0;
64 if (!logical_cpus)
65 logical_cpus = static_cast<uint32_t>(internal::DetectNumberOfCores());
66 return logical_cpus;
henrikg@webrtc.orgc58ef082011-11-08 08:44:17 +000067}
68
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +000069} // namespace webrtc