blob: 514f41f0251be655d47a06bc27668b24feae3970 [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
henrike@webrtc.org93bea512013-03-28 15:58:49 +000011#include "webrtc/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)
phoglund@webrtc.org17b867a2013-03-04 15:09:03 +000021#include <unistd.h> // required for get_nprocs() with uClibc
henrikg@webrtc.orgc58ef082011-11-08 08:44:17 +000022#include <sys/sysinfo.h>
23#endif
24
henrike@webrtc.org93bea512013-03-28 15:58:49 +000025#include "system_wrappers/interface/trace.h"
henrikg@webrtc.orgc58ef082011-11-08 08:44:17 +000026
27namespace webrtc {
28
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000029WebRtc_UWord32 CpuInfo::number_of_cores_ = 0;
henrikg@webrtc.orgc58ef082011-11-08 08:44:17 +000030
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000031WebRtc_UWord32 CpuInfo::DetectNumberOfCores() {
32 if (!number_of_cores_) {
henrikg@webrtc.orgc58ef082011-11-08 08:44:17 +000033#if defined(_WIN32)
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000034 SYSTEM_INFO si;
35 GetSystemInfo(&si);
36 number_of_cores_ = static_cast<WebRtc_UWord32>(si.dwNumberOfProcessors);
37 WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, -1,
38 "Available number of cores:%d", number_of_cores_);
henrikg@webrtc.orgc58ef082011-11-08 08:44:17 +000039
40#elif defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID)
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000041 number_of_cores_ = get_nprocs();
42 WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, -1,
43 "Available number of cores:%d", number_of_cores_);
henrikg@webrtc.orgc58ef082011-11-08 08:44:17 +000044
andrew@webrtc.orgf3b65db2012-09-06 18:17:00 +000045#elif defined(WEBRTC_MAC)
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000046 int name[] = {CTL_HW, HW_AVAILCPU};
47 int ncpu;
48 size_t size = sizeof(ncpu);
49 if (0 == sysctl(name, 2, &ncpu, &size, NULL, 0)) {
50 number_of_cores_ = static_cast<WebRtc_UWord32>(ncpu);
51 WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, -1,
52 "Available number of cores:%d", number_of_cores_);
53 } else {
54 WEBRTC_TRACE(kTraceError, kTraceUtility, -1,
55 "Failed to get number of cores");
56 number_of_cores_ = 1;
henrikg@webrtc.orgc58ef082011-11-08 08:44:17 +000057 }
58#else
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000059 WEBRTC_TRACE(kTraceWarning, kTraceUtility, -1,
60 "No function to get number of cores");
61 number_of_cores_ = 1;
henrikg@webrtc.orgc58ef082011-11-08 08:44:17 +000062#endif
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000063 }
64 return number_of_cores_;
henrikg@webrtc.orgc58ef082011-11-08 08:44:17 +000065}
66
67} // namespace webrtc