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_info.h"
12
13#if defined(_WIN32)
14#include <Windows.h>
15#elif defined(WEBRTC_MAC)
16#include <sys/types.h>
17#include <sys/sysctl.h>
18#elif defined(WEBRTC_MAC_INTEL)
19// Intentionally empty
20#elif defined(WEBRTC_ANDROID)
21// Not implemented yet, might be possible to use Linux implementation
22#else // defined(WEBRTC_LINUX)
23#include <sys/sysinfo.h>
24#endif
25
26#include "trace.h"
27
28namespace webrtc {
29
30WebRtc_UWord32 CpuInfo::_numberOfCores = 0;
31
32WebRtc_UWord32 CpuInfo::DetectNumberOfCores()
33{
34    if (!_numberOfCores)
35    {
36#if defined(_WIN32)
37        SYSTEM_INFO si;
38        GetSystemInfo(&si);
39        _numberOfCores = static_cast<WebRtc_UWord32>(si.dwNumberOfProcessors);
40        WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, -1,
41                     "Available number of cores:%d", _numberOfCores);
42
43#elif defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID)
44        _numberOfCores = get_nprocs();
45        WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, -1,
46                     "Available number of cores:%d", _numberOfCores);
47
48#elif (defined(WEBRTC_MAC) || defined(WEBRTC_MAC_INTEL))
49        int name[] = {CTL_HW, HW_AVAILCPU};
50        int ncpu;
51        size_t size = sizeof(ncpu);
52        if(0 == sysctl(name, 2, &ncpu, &size, NULL, 0))
53        {
54            _numberOfCores = static_cast<WebRtc_UWord32>(ncpu);
55            WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, -1,
56                         "Available number of cores:%d", _numberOfCores);
57    } else
58    {
59            WEBRTC_TRACE(kTraceError, kTraceUtility, -1,
60                         "Failed to get number of cores");
61            _numberOfCores = 1;
62    }
63#else
64        WEBRTC_TRACE(kTraceWarning, kTraceUtility, -1,
65                     "No function to get number of cores");
66        _numberOfCores = 1;
67#endif
68    }
69    return _numberOfCores;
70}
71
72} // namespace webrtc
73