1 | /* $Id: PerformanceDarwin.cpp 43836 2012-11-08 07:26:14Z 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.virtualbox.org. 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/param.h>
|
---|
28 | #include <iprt/system.h>
|
---|
29 | #include "Performance.h"
|
---|
30 |
|
---|
31 | /* The following declarations are missing in 10.4.x SDK */
|
---|
32 | /* @todo Replace them with libproc.h and sys/proc_info.h when 10.4 is no longer supported */
|
---|
33 | extern "C" int proc_pidinfo(int pid, int flavor, uint64_t arg, void *buffer, int buffersize);
|
---|
34 | struct proc_taskinfo {
|
---|
35 | uint64_t pti_virtual_size; /* virtual memory size (bytes) */
|
---|
36 | uint64_t pti_resident_size; /* resident memory size (bytes) */
|
---|
37 | uint64_t pti_total_user; /* total time */
|
---|
38 | uint64_t pti_total_system;
|
---|
39 | uint64_t pti_threads_user; /* existing threads only */
|
---|
40 | uint64_t pti_threads_system;
|
---|
41 | int32_t pti_policy; /* default policy for new threads */
|
---|
42 | int32_t pti_faults; /* number of page faults */
|
---|
43 | int32_t pti_pageins; /* number of actual pageins */
|
---|
44 | int32_t pti_cow_faults; /* number of copy-on-write faults */
|
---|
45 | int32_t pti_messages_sent; /* number of messages sent */
|
---|
46 | int32_t pti_messages_received; /* number of messages received */
|
---|
47 | int32_t pti_syscalls_mach; /* number of mach system calls */
|
---|
48 | int32_t pti_syscalls_unix; /* number of unix system calls */
|
---|
49 | int32_t pti_csw; /* number of context switches */
|
---|
50 | int32_t pti_threadnum; /* number of threads in the task */
|
---|
51 | int32_t pti_numrunning; /* number of running threads */
|
---|
52 | int32_t pti_priority; /* task priority*/
|
---|
53 | };
|
---|
54 | #define PROC_PIDTASKINFO 4
|
---|
55 |
|
---|
56 | namespace pm {
|
---|
57 |
|
---|
58 | class CollectorDarwin : public CollectorHAL
|
---|
59 | {
|
---|
60 | public:
|
---|
61 | CollectorDarwin();
|
---|
62 | virtual int getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle);
|
---|
63 | virtual int getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available);
|
---|
64 | virtual int getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total);
|
---|
65 | virtual int getProcessMemoryUsage(RTPROCESS process, ULONG *used);
|
---|
66 | private:
|
---|
67 | ULONG totalRAM;
|
---|
68 | };
|
---|
69 |
|
---|
70 | CollectorHAL *createHAL()
|
---|
71 | {
|
---|
72 | return new CollectorDarwin();
|
---|
73 | }
|
---|
74 |
|
---|
75 | CollectorDarwin::CollectorDarwin()
|
---|
76 | {
|
---|
77 | uint64_t cb;
|
---|
78 | int rc = RTSystemQueryTotalRam(&cb);
|
---|
79 | if (RT_FAILURE(rc))
|
---|
80 | totalRAM = 0;
|
---|
81 | else
|
---|
82 | totalRAM = (ULONG)(cb / 1024);
|
---|
83 | }
|
---|
84 |
|
---|
85 | int CollectorDarwin::getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle)
|
---|
86 | {
|
---|
87 | kern_return_t krc;
|
---|
88 | mach_msg_type_number_t count;
|
---|
89 | host_cpu_load_info_data_t info;
|
---|
90 |
|
---|
91 | count = HOST_CPU_LOAD_INFO_COUNT;
|
---|
92 |
|
---|
93 | krc = host_statistics(mach_host_self(), HOST_CPU_LOAD_INFO, (host_info_t)&info, &count);
|
---|
94 | if (krc != KERN_SUCCESS)
|
---|
95 | {
|
---|
96 | Log(("host_statistics() -> %s", mach_error_string(krc)));
|
---|
97 | return RTErrConvertFromDarwinKern(krc);
|
---|
98 | }
|
---|
99 |
|
---|
100 | *user = (uint64_t)info.cpu_ticks[CPU_STATE_USER]
|
---|
101 | + info.cpu_ticks[CPU_STATE_NICE];
|
---|
102 | *kernel = (uint64_t)info.cpu_ticks[CPU_STATE_SYSTEM];
|
---|
103 | *idle = (uint64_t)info.cpu_ticks[CPU_STATE_IDLE];
|
---|
104 | return VINF_SUCCESS;
|
---|
105 | }
|
---|
106 |
|
---|
107 | int CollectorDarwin::getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available)
|
---|
108 | {
|
---|
109 | uint64_t cb;
|
---|
110 | int rc = RTSystemQueryAvailableRam(&cb);
|
---|
111 | if (RT_SUCCESS(rc))
|
---|
112 | {
|
---|
113 | *total = totalRAM;
|
---|
114 | *available = cb / 1024;
|
---|
115 | *used = *total - *available;
|
---|
116 | }
|
---|
117 | return rc;
|
---|
118 | }
|
---|
119 |
|
---|
120 | static int getProcessInfo(RTPROCESS process, struct proc_taskinfo *tinfo)
|
---|
121 | {
|
---|
122 | LogAleksey(("getProcessInfo() getting info for %d", process));
|
---|
123 | int nb = proc_pidinfo(process, PROC_PIDTASKINFO, 0, tinfo, sizeof(*tinfo));
|
---|
124 | if (nb <= 0)
|
---|
125 | {
|
---|
126 | int rc = errno;
|
---|
127 | Log(("proc_pidinfo() -> %s", strerror(rc)));
|
---|
128 | return RTErrConvertFromDarwin(rc);
|
---|
129 | }
|
---|
130 | else if ((unsigned int)nb < sizeof(*tinfo))
|
---|
131 | {
|
---|
132 | Log(("proc_pidinfo() -> too few bytes %d", nb));
|
---|
133 | return VERR_INTERNAL_ERROR;
|
---|
134 | }
|
---|
135 | return VINF_SUCCESS;
|
---|
136 | }
|
---|
137 |
|
---|
138 | int CollectorDarwin::getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total)
|
---|
139 | {
|
---|
140 | struct proc_taskinfo tinfo;
|
---|
141 |
|
---|
142 | int rc = getProcessInfo(process, &tinfo);
|
---|
143 | if (RT_SUCCESS(rc))
|
---|
144 | {
|
---|
145 | *user = tinfo.pti_total_user;
|
---|
146 | *kernel = tinfo.pti_total_system;
|
---|
147 | *total = mach_absolute_time();
|
---|
148 | }
|
---|
149 | return rc;
|
---|
150 | }
|
---|
151 |
|
---|
152 | int CollectorDarwin::getProcessMemoryUsage(RTPROCESS process, ULONG *used)
|
---|
153 | {
|
---|
154 | struct proc_taskinfo tinfo;
|
---|
155 |
|
---|
156 | int rc = getProcessInfo(process, &tinfo);
|
---|
157 | if (RT_SUCCESS(rc))
|
---|
158 | {
|
---|
159 | *used = tinfo.pti_resident_size / 1024;
|
---|
160 | }
|
---|
161 | return rc;
|
---|
162 | }
|
---|
163 |
|
---|
164 | }
|
---|
165 |
|
---|