blob: f059a8cd3e130717c4fe81f328678ba624bd3ba2 [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
11#include "cpu_info.h"
12
13#if defined(_WIN32)
14#include <Windows.h>
15#elif defined(WEBRTC_MAC)
16#include <sys/types.h>
17#include <sys/sysctl.h>
henrikg@webrtc.orgc58ef082011-11-08 08:44:17 +000018#elif defined(WEBRTC_ANDROID)
19// Not implemented yet, might be possible to use Linux implementation
20#else // defined(WEBRTC_LINUX)
21#include <sys/sysinfo.h>
22#endif
23
24#include "trace.h"
25
26namespace webrtc {
27
28WebRtc_UWord32 CpuInfo::_numberOfCores = 0;
29
30WebRtc_UWord32 CpuInfo::DetectNumberOfCores()
31{
32 if (!_numberOfCores)
33 {
34#if defined(_WIN32)
35 SYSTEM_INFO si;
36 GetSystemInfo(&si);
37 _numberOfCores = static_cast<WebRtc_UWord32>(si.dwNumberOfProcessors);
38 WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, -1,
39 "Available number of cores:%d", _numberOfCores);
40
41#elif defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID)
42 _numberOfCores = get_nprocs();
43 WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, -1,
44 "Available number of cores:%d", _numberOfCores);
45
andrew@webrtc.orgf3b65db2012-09-06 18:17:00 +000046#elif defined(WEBRTC_MAC)
henrikg@webrtc.orgc58ef082011-11-08 08:44:17 +000047 int name[] = {CTL_HW, HW_AVAILCPU};
48 int ncpu;
49 size_t size = sizeof(ncpu);
50 if(0 == sysctl(name, 2, &ncpu, &size, NULL, 0))
51 {
52 _numberOfCores = static_cast<WebRtc_UWord32>(ncpu);
53 WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, -1,
54 "Available number of cores:%d", _numberOfCores);
55 } else
56 {
57 WEBRTC_TRACE(kTraceError, kTraceUtility, -1,
58 "Failed to get number of cores");
59 _numberOfCores = 1;
60 }
61#else
62 WEBRTC_TRACE(kTraceWarning, kTraceUtility, -1,
63 "No function to get number of cores");
64 _numberOfCores = 1;
65#endif
66 }
67 return _numberOfCores;
68}
69
70} // namespace webrtc