blob: 0342802207edfb8664c9a87eaad66cf9fc110dad [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +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
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000011#include "system_wrappers/source/cpu_mac.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
13#include <iostream>
14#include <mach/mach.h>
15#include <mach/mach_error.h>
16
17#include "tick_util.h"
18
19namespace webrtc {
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000020
henrike@webrtc.org0d0037c2011-10-24 15:48:14 +000021CpuWrapperMac::CpuWrapperMac()
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000022 : cpu_count_(0),
23 cpu_usage_(NULL),
24 total_cpu_usage_(0),
25 last_tick_count_(NULL),
26 last_time_(0) {
27 natural_t cpu_count;
28 processor_info_array_t info_array;
29 mach_msg_type_number_t info_count;
niklase@google.com470e71d2011-07-07 08:21:25 +000030
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000031 kern_return_t error = host_processor_info(mach_host_self(),
32 PROCESSOR_CPU_LOAD_INFO,
33 &cpu_count,
34 &info_array,
35 &info_count);
36 if (error) {
37 return;
38 }
39
40 cpu_count_ = cpu_count;
41 cpu_usage_ = new WebRtc_UWord32[cpu_count];
42 last_tick_count_ = new WebRtc_Word64[cpu_count];
43 last_time_ = TickTime::MillisecondTimestamp();
44
45 processor_cpu_load_info_data_t* cpu_load_info =
46 (processor_cpu_load_info_data_t*) info_array;
47 for (unsigned int cpu = 0; cpu < cpu_count; ++cpu) {
48 WebRtc_Word64 ticks = 0;
49 for (int state = 0; state < 2; ++state) {
50 ticks += cpu_load_info[cpu].cpu_ticks[state];
niklase@google.com470e71d2011-07-07 08:21:25 +000051 }
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000052 last_tick_count_[cpu] = ticks;
53 cpu_usage_[cpu] = 0;
54 }
55 vm_deallocate(mach_task_self(), (vm_address_t)info_array, info_count);
niklase@google.com470e71d2011-07-07 08:21:25 +000056}
57
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000058CpuWrapperMac::~CpuWrapperMac() {
59 delete[] cpu_usage_;
60 delete[] last_tick_count_;
niklase@google.com470e71d2011-07-07 08:21:25 +000061}
62
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000063WebRtc_Word32 CpuWrapperMac::CpuUsage() {
64 WebRtc_UWord32 num_cores;
65 WebRtc_UWord32* array = NULL;
66 return CpuUsageMultiCore(num_cores, array);
niklase@google.com470e71d2011-07-07 08:21:25 +000067}
68
69WebRtc_Word32
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000070CpuWrapperMac::CpuUsageMultiCore(WebRtc_UWord32& num_cores,
71 WebRtc_UWord32*& array) {
72 // sanity check
73 if (cpu_usage_ == NULL) {
74 return -1;
75 }
76
77 WebRtc_Word64 now = TickTime::MillisecondTimestamp();
78 WebRtc_Word64 time_diff_ms = now - last_time_;
79 if (time_diff_ms >= 500) {
80 if (Update(time_diff_ms) != 0) {
81 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +000082 }
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000083 last_time_ = now;
84 }
85
86 num_cores = cpu_count_;
87 array = cpu_usage_;
88 return total_cpu_usage_ / cpu_count_;
henrike@webrtc.org0d0037c2011-10-24 15:48:14 +000089}
niklase@google.com470e71d2011-07-07 08:21:25 +000090
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +000091WebRtc_Word32 CpuWrapperMac::Update(WebRtc_Word64 time_diff_ms) {
92 natural_t cpu_count;
93 processor_info_array_t info_array;
94 mach_msg_type_number_t info_count;
95
96 kern_return_t error = host_processor_info(mach_host_self(),
97 PROCESSOR_CPU_LOAD_INFO,
98 &cpu_count,
99 &info_array,
100 &info_count);
101 if (error) {
102 return -1;
103 }
104
105 processor_cpu_load_info_data_t* cpu_load_info =
106 (processor_cpu_load_info_data_t*) info_array;
107
108 total_cpu_usage_ = 0;
109 for (unsigned int cpu = 0; cpu < cpu_count; ++cpu) {
110 WebRtc_Word64 ticks = 0;
111 for (int state = 0; state < 2; ++state) {
112 ticks += cpu_load_info[cpu].cpu_ticks[state];
niklase@google.com470e71d2011-07-07 08:21:25 +0000113 }
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +0000114 if (time_diff_ms <= 0) {
115 cpu_usage_[cpu] = 0;
116 } else {
117 cpu_usage_[cpu] = (WebRtc_UWord32)((1000 *
118 (ticks - last_tick_count_[cpu])) /
119 time_diff_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +0000120 }
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +0000121 last_tick_count_[cpu] = ticks;
122 total_cpu_usage_ += cpu_usage_[cpu];
123 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000124
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +0000125 vm_deallocate(mach_task_self(), (vm_address_t)info_array, info_count);
niklase@google.com470e71d2011-07-07 08:21:25 +0000126
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +0000127 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000128}
phoglund@webrtc.orgb15d2852012-11-21 08:02:57 +0000129
niklase@google.com470e71d2011-07-07 08:21:25 +0000130} // namespace webrtc