blob: dc007000813becdf7b0b40f73bff36d5a6b5dd6b [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
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000011#include "system_wrappers/interface/cpu_info.h"
henrikg@webrtc.orgc58ef082011-11-08 08:44:17 +000012
13#if defined(_WIN32)
14#include <Windows.h>
15#elif defined(WEBRTC_MAC)
henrikg@webrtc.orgc58ef082011-11-08 08:44:17 +000016#include <sys/sysctl.h>
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000017#include <sys/types.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
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000028WebRtc_UWord32 CpuInfo::number_of_cores_ = 0;
henrikg@webrtc.orgc58ef082011-11-08 08:44:17 +000029
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000030WebRtc_UWord32 CpuInfo::DetectNumberOfCores() {
31 if (!number_of_cores_) {
henrikg@webrtc.orgc58ef082011-11-08 08:44:17 +000032#if defined(_WIN32)
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000033 SYSTEM_INFO si;
34 GetSystemInfo(&si);
35 number_of_cores_ = static_cast<WebRtc_UWord32>(si.dwNumberOfProcessors);
36 WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, -1,
37 "Available number of cores:%d", number_of_cores_);
henrikg@webrtc.orgc58ef082011-11-08 08:44:17 +000038
39#elif defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID)
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000040 number_of_cores_ = get_nprocs();
41 WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, -1,
42 "Available number of cores:%d", number_of_cores_);
henrikg@webrtc.orgc58ef082011-11-08 08:44:17 +000043
andrew@webrtc.orgf3b65db2012-09-06 18:17:00 +000044#elif defined(WEBRTC_MAC)
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000045 int name[] = {CTL_HW, HW_AVAILCPU};
46 int ncpu;
47 size_t size = sizeof(ncpu);
48 if (0 == sysctl(name, 2, &ncpu, &size, NULL, 0)) {
49 number_of_cores_ = static_cast<WebRtc_UWord32>(ncpu);
50 WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, -1,
51 "Available number of cores:%d", number_of_cores_);
52 } else {
53 WEBRTC_TRACE(kTraceError, kTraceUtility, -1,
54 "Failed to get number of cores");
55 number_of_cores_ = 1;
henrikg@webrtc.orgc58ef082011-11-08 08:44:17 +000056 }
57#else
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000058 WEBRTC_TRACE(kTraceWarning, kTraceUtility, -1,
59 "No function to get number of cores");
60 number_of_cores_ = 1;
henrikg@webrtc.orgc58ef082011-11-08 08:44:17 +000061#endif
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000062 }
63 return number_of_cores_;
henrikg@webrtc.orgc58ef082011-11-08 08:44:17 +000064}
65
66} // namespace webrtc