Changeset 11564 in vbox for trunk/src/VBox/Main/darwin
- Timestamp:
- Aug 22, 2008 7:09:03 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Main/darwin/PerformanceDarwin.cpp
r11180 r11564 22 22 */ 23 23 24 #include <iprt/stdint.h> 25 #include <mach/mach_error.h> 26 #include <mach/mach_host.h> 27 #include <mach/mach_init.h> 28 #include <mach/mach_time.h> 29 #include <mach/task.h> 30 #include <mach/task_info.h> 31 #include <mach/vm_statistics.h> 32 #include <sys/sysctl.h> 33 #include <sys/errno.h> 34 #include <iprt/err.h> 35 #include <iprt/log.h> 36 #include <iprt/param.h> 24 37 #include "Performance.h" 25 38 … … 29 42 { 30 43 public: 31 virtual int get HostCpuLoad(ULONG *user, ULONG *kernel, ULONG*idle);44 virtual int getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle); 32 45 virtual int getHostCpuMHz(ULONG *mhz); 33 46 virtual int getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available); 34 virtual int get ProcessCpuLoad(RTPROCESS process, ULONG *user, ULONG *kernel);47 virtual int getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total); 35 48 virtual int getProcessMemoryUsage(RTPROCESS process, ULONG *used); 36 49 }; … … 42 55 } 43 56 44 int CollectorDarwin::get HostCpuLoad(ULONG *user, ULONG *kernel, ULONG*idle)57 int CollectorDarwin::getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle) 45 58 { 46 return E_NOTIMPL; 59 kern_return_t krc; 60 mach_msg_type_number_t count; 61 host_cpu_load_info_data_t info; 62 63 count = HOST_CPU_LOAD_INFO_COUNT; 64 65 krc = host_statistics(mach_host_self(), HOST_CPU_LOAD_INFO, (host_info_t)&info, &count); 66 if (krc != KERN_SUCCESS) 67 { 68 Log(("host_statistics() -> %s", mach_error_string(krc))); 69 return RTErrConvertFromDarwinKern(krc); 70 } 71 72 *user = (uint64_t)info.cpu_ticks[CPU_STATE_USER] 73 + info.cpu_ticks[CPU_STATE_NICE]; 74 *kernel = (uint64_t)info.cpu_ticks[CPU_STATE_SYSTEM]; 75 *idle = (uint64_t)info.cpu_ticks[CPU_STATE_IDLE]; 76 return VINF_SUCCESS; 47 77 } 48 78 49 79 int CollectorDarwin::getHostCpuMHz(ULONG *mhz) 50 80 { 51 return E_NOTIMPL;81 return VERR_NOT_IMPLEMENTED; 52 82 } 53 83 54 84 int CollectorDarwin::getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available) 55 85 { 56 return E_NOTIMPL; 86 kern_return_t krc; 87 mach_msg_type_number_t count; 88 vm_statistics_data_t info; 89 90 count = HOST_VM_INFO_COUNT; 91 92 krc = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&info, &count); 93 if (krc != KERN_SUCCESS) 94 { 95 Log(("host_statistics() -> %s", mach_error_string(krc))); 96 return RTErrConvertFromDarwinKern(krc); 97 } 98 99 uint64_t hostMemory; 100 int mib[2]; 101 size_t size; 102 103 mib[0] = CTL_HW; 104 mib[1] = HW_MEMSIZE; 105 106 size = sizeof(hostMemory); 107 if (sysctl(mib, 2, &hostMemory, &size, NULL, 0) == -1) { 108 int error = errno; 109 Log(("sysctl() -> %s", strerror(error))); 110 return RTErrConvertFromErrno(error); 111 } 112 *total = (ULONG)(hostMemory / 1024); 113 *available = info.free_count * (PAGE_SIZE / 1024); 114 *used = *total - *available; 115 return VINF_SUCCESS; 57 116 } 58 117 59 int CollectorDarwin::get ProcessCpuLoad(RTPROCESS process, ULONG *user, ULONG *kernel)118 int CollectorDarwin::getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total) 60 119 { 61 return E_NOTIMPL; 120 kern_return_t krc; 121 task_absolutetime_info_data_t tinfo; 122 mach_port_name_t vmTask; 123 mach_msg_type_number_t count; 124 125 krc = task_for_pid(task_self_trap(), process, &vmTask); 126 if (krc != KERN_SUCCESS) 127 { 128 Log(("task_for_pid() -> %s", mach_error_string(krc))); 129 return RTErrConvertFromDarwinKern(krc); 130 } 131 132 count = TASK_ABSOLUTETIME_INFO_COUNT; 133 krc = task_info(vmTask, TASK_ABSOLUTETIME_INFO, (task_info_t)&tinfo, &count); 134 if (krc != KERN_SUCCESS) { 135 Log(("task_info() -> %s", mach_error_string(krc))); 136 return RTErrConvertFromDarwinKern(krc); 137 } 138 139 *user = tinfo.total_user; 140 *kernel = tinfo.total_system; 141 *total = mach_absolute_time(); 142 return VINF_SUCCESS; 62 143 } 63 144 64 145 int CollectorDarwin::getProcessMemoryUsage(RTPROCESS process, ULONG *used) 65 146 { 66 return E_NOTIMPL; 147 kern_return_t krc; 148 task_basic_info_64_data_t tinfo; 149 mach_port_name_t vmTask; 150 mach_msg_type_number_t count; 151 152 krc = task_for_pid(task_self_trap(), process, &vmTask); 153 if (krc != KERN_SUCCESS) 154 { 155 Log(("task_for_pid() -> %s", mach_error_string(krc))); 156 return RTErrConvertFromDarwinKern(krc); 157 } 158 count = TASK_BASIC_INFO_64_COUNT; 159 krc = task_info(task_self_trap(), TASK_BASIC_INFO_64, (task_info_t)&tinfo, &count); 160 if (krc != KERN_SUCCESS) { 161 Log(("host_statistics() -> %s", mach_error_string(krc))); 162 return RTErrConvertFromDarwinKern(krc); 163 } 164 165 *used = tinfo.resident_size / 1024; 166 return VINF_SUCCESS; 67 167 } 68 168
Note:
See TracChangeset
for help on using the changeset viewer.