henrikg@webrtc.org | c58ef08 | 2011-11-08 08:44:17 +0000 | [diff] [blame] | 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 | |
henrike@webrtc.org | 93bea51 | 2013-03-28 15:58:49 +0000 | [diff] [blame] | 11 | #include "webrtc/system_wrappers/interface/cpu_info.h" |
henrikg@webrtc.org | c58ef08 | 2011-11-08 08:44:17 +0000 | [diff] [blame] | 12 | |
| 13 | #if defined(_WIN32) |
| 14 | #include <Windows.h> |
| 15 | #elif defined(WEBRTC_MAC) |
henrikg@webrtc.org | c58ef08 | 2011-11-08 08:44:17 +0000 | [diff] [blame] | 16 | #include <sys/sysctl.h> |
phoglund@webrtc.org | b15d285 | 2012-11-21 08:02:57 +0000 | [diff] [blame] | 17 | #include <sys/types.h> |
fischman@webrtc.org | 2b3a29a | 2013-06-05 16:37:42 +0000 | [diff] [blame^] | 18 | #else // defined(WEBRTC_LINUX) or defined(WEBRTC_ANDROID) |
| 19 | #include <unistd.h> |
henrikg@webrtc.org | c58ef08 | 2011-11-08 08:44:17 +0000 | [diff] [blame] | 20 | #endif |
| 21 | |
pbos@webrtc.org | acaf3a1 | 2013-05-27 15:07:45 +0000 | [diff] [blame] | 22 | #include "webrtc/system_wrappers/interface/trace.h" |
henrikg@webrtc.org | c58ef08 | 2011-11-08 08:44:17 +0000 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
| 25 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 26 | uint32_t CpuInfo::number_of_cores_ = 0; |
henrikg@webrtc.org | c58ef08 | 2011-11-08 08:44:17 +0000 | [diff] [blame] | 27 | |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 28 | uint32_t CpuInfo::DetectNumberOfCores() { |
phoglund@webrtc.org | b15d285 | 2012-11-21 08:02:57 +0000 | [diff] [blame] | 29 | if (!number_of_cores_) { |
henrikg@webrtc.org | c58ef08 | 2011-11-08 08:44:17 +0000 | [diff] [blame] | 30 | #if defined(_WIN32) |
phoglund@webrtc.org | b15d285 | 2012-11-21 08:02:57 +0000 | [diff] [blame] | 31 | SYSTEM_INFO si; |
| 32 | GetSystemInfo(&si); |
pbos@webrtc.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 33 | number_of_cores_ = static_cast<uint32_t>(si.dwNumberOfProcessors); |
phoglund@webrtc.org | b15d285 | 2012-11-21 08:02:57 +0000 | [diff] [blame] | 34 | WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, -1, |
| 35 | "Available number of cores:%d", number_of_cores_); |
henrikg@webrtc.org | c58ef08 | 2011-11-08 08:44:17 +0000 | [diff] [blame] | 36 | |
fischman@webrtc.org | 2b3a29a | 2013-06-05 16:37:42 +0000 | [diff] [blame^] | 37 | #elif defined(WEBRTC_LINUX) || defined(WEBRTC_ANDROID) |
| 38 | number_of_cores_ = static_cast<uint32_t>(sysconf(_SC_NPROCESSORS_ONLN)); |
phoglund@webrtc.org | b15d285 | 2012-11-21 08:02:57 +0000 | [diff] [blame] | 39 | WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, -1, |
| 40 | "Available number of cores:%d", number_of_cores_); |
henrikg@webrtc.org | c58ef08 | 2011-11-08 08:44:17 +0000 | [diff] [blame] | 41 | |
andrew@webrtc.org | f3b65db | 2012-09-06 18:17:00 +0000 | [diff] [blame] | 42 | #elif defined(WEBRTC_MAC) |
phoglund@webrtc.org | b15d285 | 2012-11-21 08:02:57 +0000 | [diff] [blame] | 43 | 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.org | 046deb9 | 2013-04-09 09:06:11 +0000 | [diff] [blame] | 47 | number_of_cores_ = static_cast<uint32_t>(ncpu); |
phoglund@webrtc.org | b15d285 | 2012-11-21 08:02:57 +0000 | [diff] [blame] | 48 | 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.org | c58ef08 | 2011-11-08 08:44:17 +0000 | [diff] [blame] | 54 | } |
| 55 | #else |
phoglund@webrtc.org | b15d285 | 2012-11-21 08:02:57 +0000 | [diff] [blame] | 56 | WEBRTC_TRACE(kTraceWarning, kTraceUtility, -1, |
| 57 | "No function to get number of cores"); |
| 58 | number_of_cores_ = 1; |
henrikg@webrtc.org | c58ef08 | 2011-11-08 08:44:17 +0000 | [diff] [blame] | 59 | #endif |
phoglund@webrtc.org | b15d285 | 2012-11-21 08:02:57 +0000 | [diff] [blame] | 60 | } |
| 61 | return number_of_cores_; |
henrikg@webrtc.org | c58ef08 | 2011-11-08 08:44:17 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | } // namespace webrtc |