Line data Source code
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 "webrtc/system_wrappers/include/cpu_info.h"
12 :
13 : #if defined(WEBRTC_WIN)
14 : #include <winsock2.h>
15 : #include <windows.h>
16 : #ifndef EXCLUDE_D3D9
17 : #include <d3d9.h>
18 : #endif
19 : #elif defined(WEBRTC_MAC)
20 : #include <sys/sysctl.h>
21 : #else // WEBRTC_POSIX
22 : #include <unistd.h>
23 : #endif
24 :
25 : #include "webrtc/base/logging.h"
26 :
27 : namespace internal {
28 0 : static int DetectNumberOfCores() {
29 : // We fall back on assuming a single core in case of errors.
30 0 : int number_of_cores = 1;
31 :
32 : #if defined(WEBRTC_WIN)
33 : SYSTEM_INFO si;
34 : GetSystemInfo(&si);
35 : number_of_cores = static_cast<int>(si.dwNumberOfProcessors);
36 : #elif defined(WEBRTC_MAC)
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)) {
40 : LOG(LS_ERROR) << "Failed to get number of cores";
41 : number_of_cores = 1;
42 : }
43 : #elif defined(_SC_NPROCESSORS_ONLN)
44 0 : number_of_cores = static_cast<int>(sysconf(_SC_NPROCESSORS_ONLN));
45 : #else
46 : LOG(LS_ERROR) << "No function to get number of cores";
47 : #endif
48 :
49 0 : LOG(LS_INFO) << "Available number of cores: " << number_of_cores;
50 :
51 0 : return number_of_cores;
52 : }
53 : }
54 :
55 : namespace webrtc {
56 :
57 0 : uint32_t CpuInfo::DetectNumberOfCores() {
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 0 : if (!logical_cpus)
64 0 : logical_cpus = static_cast<uint32_t>(internal::DetectNumberOfCores());
65 0 : return logical_cpus;
66 : }
67 :
68 : } // namespace webrtc
|