niklase@google.com | f0779a2 | 2011-05-30 11:39:38 +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 | |
| 11 | #include "cpu_wrapper.h" |
| 12 | |
| 13 | #if defined(_WIN32) |
| 14 | #include <Windows.h> |
| 15 | #include "engine_configurations.h" |
| 16 | #include "cpu_windows.h" |
| 17 | #elif defined(WEBRTC_MAC) |
| 18 | #include <sys/types.h> |
| 19 | #include <sys/sysctl.h> |
| 20 | #include "cpu_mac.h" |
| 21 | #elif defined(WEBRTC_MAC_INTEL) |
| 22 | #include "cpu_mac.h" |
| 23 | #elif defined(ANDROID) |
| 24 | // Not implemented yet, might be possible to use Linux implementation |
| 25 | #else // defined(WEBRTC_LINUX) |
| 26 | #include <sys/sysinfo.h> |
| 27 | #include "cpu_linux.h" |
| 28 | #endif |
| 29 | |
| 30 | #include "trace.h" |
| 31 | |
| 32 | namespace webrtc { |
| 33 | WebRtc_UWord32 CpuWrapper::_numberOfCores = 0; |
| 34 | |
| 35 | WebRtc_UWord32 CpuWrapper::DetectNumberOfCores() |
| 36 | { |
| 37 | if (!_numberOfCores) |
| 38 | { |
| 39 | #if defined(_WIN32) |
| 40 | SYSTEM_INFO si; |
| 41 | GetSystemInfo(&si); |
| 42 | _numberOfCores = static_cast<WebRtc_UWord32>(si.dwNumberOfProcessors); |
| 43 | WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, -1, |
| 44 | "Available number of cores:%d", _numberOfCores); |
| 45 | |
| 46 | #elif defined(WEBRTC_LINUX) && !defined(ANDROID) |
| 47 | _numberOfCores = get_nprocs(); |
| 48 | WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, -1, |
| 49 | "Available number of cores:%d", _numberOfCores); |
| 50 | |
| 51 | #elif (defined(WEBRTC_MAC) || defined(WEBRTC_MAC_INTEL)) |
| 52 | int name[] = {CTL_HW, HW_AVAILCPU}; |
| 53 | int ncpu; |
| 54 | size_t size = sizeof(ncpu); |
| 55 | if(0 == sysctl(name, 2, &ncpu, &size, NULL, 0)) |
| 56 | { |
| 57 | _numberOfCores = static_cast<WebRtc_UWord32>(ncpu); |
| 58 | WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, -1, |
| 59 | "Available number of cores:%d", _numberOfCores); |
| 60 | } else |
| 61 | { |
| 62 | WEBRTC_TRACE(kTraceError, kTraceUtility, -1, |
| 63 | "Failed to get number of cores"); |
| 64 | _numberOfCores = 1; |
| 65 | } |
| 66 | #else |
| 67 | WEBRTC_TRACE(kTraceWarning, kTraceUtility, -1, |
| 68 | "No function to get number of cores"); |
| 69 | _numberOfCores = 1; |
| 70 | #endif |
| 71 | } |
| 72 | return _numberOfCores; |
| 73 | } |
| 74 | |
| 75 | CpuWrapper* CpuWrapper::CreateCpu() |
| 76 | { |
| 77 | #if defined(_WIN32) |
| 78 | return new CpuWindows(); |
| 79 | #elif (defined(WEBRTC_MAC) || defined(WEBRTC_MAC_INTEL)) |
| 80 | return new CpuWrapperMac(); |
| 81 | #elif defined(ANDROID) |
| 82 | return 0; |
| 83 | #else |
| 84 | return new CpuLinux(); |
| 85 | #endif |
| 86 | } |
| 87 | } // namespace webrtc |