VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/darwin/PerformanceDarwin.cpp@ 44466

Last change on this file since 44466 was 44466, checked in by vboxsync, 12 years ago

Main/Metrics: Per-VM CPU load on Darwin brought in line with other platforms (#6345)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.7 KB
Line 
1/* $Id: PerformanceDarwin.cpp 44466 2013-01-30 14:16:31Z vboxsync $ */
2/** @file
3 * VBox Darwin-specific Performance Classes implementation.
4 */
5
6/*
7 * Copyright (C) 2008 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#include <mach/mach_error.h>
19#include <mach/mach_host.h>
20#include <mach/mach_init.h>
21#include <mach/mach_time.h>
22#include <mach/vm_statistics.h>
23#include <sys/sysctl.h>
24#include <sys/errno.h>
25#include <iprt/err.h>
26#include <iprt/log.h>
27#include <iprt/mp.h>
28#include <iprt/param.h>
29#include <iprt/system.h>
30#include "Performance.h"
31
32/* The following declarations are missing in 10.4.x SDK */
33/* @todo Replace them with libproc.h and sys/proc_info.h when 10.4 is no longer supported */
34extern "C" int proc_pidinfo(int pid, int flavor, uint64_t arg, void *buffer, int buffersize);
35struct proc_taskinfo {
36 uint64_t pti_virtual_size; /* virtual memory size (bytes) */
37 uint64_t pti_resident_size; /* resident memory size (bytes) */
38 uint64_t pti_total_user; /* total time */
39 uint64_t pti_total_system;
40 uint64_t pti_threads_user; /* existing threads only */
41 uint64_t pti_threads_system;
42 int32_t pti_policy; /* default policy for new threads */
43 int32_t pti_faults; /* number of page faults */
44 int32_t pti_pageins; /* number of actual pageins */
45 int32_t pti_cow_faults; /* number of copy-on-write faults */
46 int32_t pti_messages_sent; /* number of messages sent */
47 int32_t pti_messages_received; /* number of messages received */
48 int32_t pti_syscalls_mach; /* number of mach system calls */
49 int32_t pti_syscalls_unix; /* number of unix system calls */
50 int32_t pti_csw; /* number of context switches */
51 int32_t pti_threadnum; /* number of threads in the task */
52 int32_t pti_numrunning; /* number of running threads */
53 int32_t pti_priority; /* task priority*/
54};
55#define PROC_PIDTASKINFO 4
56
57namespace pm {
58
59class CollectorDarwin : public CollectorHAL
60{
61public:
62 CollectorDarwin();
63 virtual int getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle);
64 virtual int getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available);
65 virtual int getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total);
66 virtual int getProcessMemoryUsage(RTPROCESS process, ULONG *used);
67private:
68 ULONG totalRAM;
69};
70
71CollectorHAL *createHAL()
72{
73 return new CollectorDarwin();
74}
75
76CollectorDarwin::CollectorDarwin()
77{
78 uint64_t cb;
79 int rc = RTSystemQueryTotalRam(&cb);
80 if (RT_FAILURE(rc))
81 totalRAM = 0;
82 else
83 totalRAM = (ULONG)(cb / 1024);
84}
85
86int CollectorDarwin::getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle)
87{
88 kern_return_t krc;
89 mach_msg_type_number_t count;
90 host_cpu_load_info_data_t info;
91
92 count = HOST_CPU_LOAD_INFO_COUNT;
93
94 krc = host_statistics(mach_host_self(), HOST_CPU_LOAD_INFO, (host_info_t)&info, &count);
95 if (krc != KERN_SUCCESS)
96 {
97 Log(("host_statistics() -> %s", mach_error_string(krc)));
98 return RTErrConvertFromDarwinKern(krc);
99 }
100
101 int nCpus = RTMpGetOnlineCount();
102 Assert(nCpus);
103 if (nCpus)
104 {
105 *user = ((uint64_t)info.cpu_ticks[CPU_STATE_USER]
106 + info.cpu_ticks[CPU_STATE_NICE]) / nCpus;
107 *kernel = ((uint64_t)info.cpu_ticks[CPU_STATE_SYSTEM]) / nCpus;
108 *idle = ((uint64_t)info.cpu_ticks[CPU_STATE_IDLE]) / nCpus;
109 }
110 else
111 {
112 /* It is rather unsual to have no CPUs, but the show must go on. */
113 *user = (uint64_t)info.cpu_ticks[CPU_STATE_USER]
114 + info.cpu_ticks[CPU_STATE_NICE];
115 *kernel = (uint64_t)info.cpu_ticks[CPU_STATE_SYSTEM];
116 *idle = (uint64_t)info.cpu_ticks[CPU_STATE_IDLE];
117 }
118
119 return VINF_SUCCESS;
120}
121
122int CollectorDarwin::getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available)
123{
124 uint64_t cb;
125 int rc = RTSystemQueryAvailableRam(&cb);
126 if (RT_SUCCESS(rc))
127 {
128 *total = totalRAM;
129 *available = cb / 1024;
130 *used = *total - *available;
131 }
132 return rc;
133}
134
135static int getProcessInfo(RTPROCESS process, struct proc_taskinfo *tinfo)
136{
137 LogAleksey(("getProcessInfo() getting info for %d", process));
138 int nb = proc_pidinfo(process, PROC_PIDTASKINFO, 0, tinfo, sizeof(*tinfo));
139 if (nb <= 0)
140 {
141 int rc = errno;
142 Log(("proc_pidinfo() -> %s", strerror(rc)));
143 return RTErrConvertFromDarwin(rc);
144 }
145 else if ((unsigned int)nb < sizeof(*tinfo))
146 {
147 Log(("proc_pidinfo() -> too few bytes %d", nb));
148 return VERR_INTERNAL_ERROR;
149 }
150 return VINF_SUCCESS;
151}
152
153int CollectorDarwin::getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total)
154{
155 struct proc_taskinfo tinfo;
156
157 int rc = getProcessInfo(process, &tinfo);
158 if (RT_SUCCESS(rc))
159 {
160 *user = tinfo.pti_total_user;
161 *kernel = tinfo.pti_total_system;
162 *total = mach_absolute_time();
163 }
164 return rc;
165}
166
167int CollectorDarwin::getProcessMemoryUsage(RTPROCESS process, ULONG *used)
168{
169 struct proc_taskinfo tinfo;
170
171 int rc = getProcessInfo(process, &tinfo);
172 if (RT_SUCCESS(rc))
173 {
174 *used = tinfo.pti_resident_size / 1024;
175 }
176 return rc;
177}
178
179}
180
Note: See TracBrowser for help on using the repository browser.

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