niklase@google.com | 470e71d | 2011-07-07 08:21:25 +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 | |
phoglund@webrtc.org | b15d285 | 2012-11-21 08:02:57 +0000 | [diff] [blame] | 11 | #include "system_wrappers/source/cpu_mac.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
| 13 | #include <iostream> |
| 14 | #include <mach/mach.h> |
| 15 | #include <mach/mach_error.h> |
| 16 | |
| 17 | #include "tick_util.h" |
| 18 | |
| 19 | namespace webrtc { |
phoglund@webrtc.org | b15d285 | 2012-11-21 08:02:57 +0000 | [diff] [blame] | 20 | |
henrike@webrtc.org | 0d0037c | 2011-10-24 15:48:14 +0000 | [diff] [blame] | 21 | CpuWrapperMac::CpuWrapperMac() |
phoglund@webrtc.org | b15d285 | 2012-11-21 08:02:57 +0000 | [diff] [blame] | 22 | : 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 30 | |
phoglund@webrtc.org | b15d285 | 2012-11-21 08:02:57 +0000 | [diff] [blame] | 31 | 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 51 | } |
phoglund@webrtc.org | b15d285 | 2012-11-21 08:02:57 +0000 | [diff] [blame] | 52 | 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 56 | } |
| 57 | |
phoglund@webrtc.org | b15d285 | 2012-11-21 08:02:57 +0000 | [diff] [blame] | 58 | CpuWrapperMac::~CpuWrapperMac() { |
| 59 | delete[] cpu_usage_; |
| 60 | delete[] last_tick_count_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 61 | } |
| 62 | |
phoglund@webrtc.org | b15d285 | 2012-11-21 08:02:57 +0000 | [diff] [blame] | 63 | WebRtc_Word32 CpuWrapperMac::CpuUsage() { |
| 64 | WebRtc_UWord32 num_cores; |
| 65 | WebRtc_UWord32* array = NULL; |
| 66 | return CpuUsageMultiCore(num_cores, array); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | WebRtc_Word32 |
phoglund@webrtc.org | b15d285 | 2012-11-21 08:02:57 +0000 | [diff] [blame] | 70 | CpuWrapperMac::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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 82 | } |
phoglund@webrtc.org | b15d285 | 2012-11-21 08:02:57 +0000 | [diff] [blame] | 83 | last_time_ = now; |
| 84 | } |
| 85 | |
| 86 | num_cores = cpu_count_; |
| 87 | array = cpu_usage_; |
| 88 | return total_cpu_usage_ / cpu_count_; |
henrike@webrtc.org | 0d0037c | 2011-10-24 15:48:14 +0000 | [diff] [blame] | 89 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 90 | |
phoglund@webrtc.org | b15d285 | 2012-11-21 08:02:57 +0000 | [diff] [blame] | 91 | WebRtc_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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 113 | } |
phoglund@webrtc.org | b15d285 | 2012-11-21 08:02:57 +0000 | [diff] [blame] | 114 | 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 120 | } |
phoglund@webrtc.org | b15d285 | 2012-11-21 08:02:57 +0000 | [diff] [blame] | 121 | last_tick_count_[cpu] = ticks; |
| 122 | total_cpu_usage_ += cpu_usage_[cpu]; |
| 123 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 124 | |
phoglund@webrtc.org | b15d285 | 2012-11-21 08:02:57 +0000 | [diff] [blame] | 125 | vm_deallocate(mach_task_self(), (vm_address_t)info_array, info_count); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 126 | |
phoglund@webrtc.org | b15d285 | 2012-11-21 08:02:57 +0000 | [diff] [blame] | 127 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 128 | } |
phoglund@webrtc.org | b15d285 | 2012-11-21 08:02:57 +0000 | [diff] [blame] | 129 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 130 | } // namespace webrtc |