blob: c482d879cfbb7052847ec82d2fcfd3be17df3e44 [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>
fischman@webrtc.org2b3a29a2013-06-05 16:37:42 +000018#else // defined(WEBRTC_LINUX) or defined(WEBRTC_ANDROID)
19#include <unistd.h>
henrikg@webrtc.orgc58ef082011-11-08 08:44:17 +000020#endif
21
pbos@webrtc.orgacaf3a12013-05-27 15:07:45 +000022#include "webrtc/system_wrappers/interface/trace.h"
henrikg@webrtc.orgc58ef082011-11-08 08:44:17 +000023
24namespace webrtc {
25
pbos@webrtc.org046deb92013-04-09 09:06:11 +000026uint32_t CpuInfo::number_of_cores_ = 0;
henrikg@webrtc.orgc58ef082011-11-08 08:44:17 +000027
pbos@webrtc.org046deb92013-04-09 09:06:11 +000028uint32_t CpuInfo::DetectNumberOfCores() {
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000029 if (!number_of_cores_) {
henrikg@webrtc.orgc58ef082011-11-08 08:44:17 +000030#if defined(_WIN32)
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000031 SYSTEM_INFO si;
32 GetSystemInfo(&si);
pbos@webrtc.org046deb92013-04-09 09:06:11 +000033 number_of_cores_ = static_cast<uint32_t>(si.dwNumberOfProcessors);
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000034 WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, -1,
35 "Available number of cores:%d", number_of_cores_);
henrikg@webrtc.orgc58ef082011-11-08 08:44:17 +000036
fischman@webrtc.org2b3a29a2013-06-05 16:37:42 +000037#elif defined(WEBRTC_LINUX) || defined(WEBRTC_ANDROID)
38 number_of_cores_ = static_cast<uint32_t>(sysconf(_SC_NPROCESSORS_ONLN));
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000039 WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, -1,
40 "Available number of cores:%d", number_of_cores_);
henrikg@webrtc.orgc58ef082011-11-08 08:44:17 +000041
andrew@webrtc.orgf3b65db2012-09-06 18:17:00 +000042#elif defined(WEBRTC_MAC)
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000043 int name[] = {CTL_HW, HW_AVAILCPU};
44 int ncpu;
45 size_t size = sizeof(ncpu);
46 if (0 == sysctl(name, 2, &ncpu, &size, NULL, 0)) {
pbos@webrtc.org046deb92013-04-09 09:06:11 +000047 number_of_cores_ = static_cast<uint32_t>(ncpu);
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000048 WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, -1,
49 "Available number of cores:%d", number_of_cores_);
50 } else {
51 WEBRTC_TRACE(kTraceError, kTraceUtility, -1,
52 "Failed to get number of cores");
53 number_of_cores_ = 1;
henrikg@webrtc.orgc58ef082011-11-08 08:44:17 +000054 }
55#else
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000056 WEBRTC_TRACE(kTraceWarning, kTraceUtility, -1,
57 "No function to get number of cores");
58 number_of_cores_ = 1;
henrikg@webrtc.orgc58ef082011-11-08 08:44:17 +000059#endif
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000060 }
61 return number_of_cores_;
henrikg@webrtc.orgc58ef082011-11-08 08:44:17 +000062}
63
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +000064} // namespace webrtc