VirtualBox

source: vbox/trunk/src/VBox/Main/darwin/PerformanceDarwin.cpp@ 11564

Last change on this file since 11564 was 11564, checked in by vboxsync, 17 years ago

PerfAPI: Darwin implementation without CPU/MHz

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.0 KB
Line 
1/* $Id: PerformanceDarwin.cpp 11564 2008-08-22 07:09:03Z vboxsync $ */
2
3/** @file
4 *
5 * VBox Darwin-specific Performance Classes implementation.
6 */
7
8/*
9 * Copyright (C) 2008 Sun Microsystems, Inc.
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
20 * Clara, CA 95054 USA or visit http://www.sun.com if you need
21 * additional information or have any questions.
22 */
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>
37#include "Performance.h"
38
39namespace pm {
40
41class CollectorDarwin : public CollectorHAL
42{
43public:
44 virtual int getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle);
45 virtual int getHostCpuMHz(ULONG *mhz);
46 virtual int getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available);
47 virtual int getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total);
48 virtual int getProcessMemoryUsage(RTPROCESS process, ULONG *used);
49};
50
51MetricFactoryDarwin::MetricFactoryDarwin()
52{
53 mHAL = new CollectorDarwin();
54 Assert(mHAL);
55}
56
57int CollectorDarwin::getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle)
58{
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;
77}
78
79int CollectorDarwin::getHostCpuMHz(ULONG *mhz)
80{
81 return VERR_NOT_IMPLEMENTED;
82}
83
84int CollectorDarwin::getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available)
85{
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;
116}
117
118int CollectorDarwin::getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total)
119{
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;
143}
144
145int CollectorDarwin::getProcessMemoryUsage(RTPROCESS process, ULONG *used)
146{
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;
167}
168
169}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette