VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/solaris/PerformanceSolaris.cpp@ 42699

Last change on this file since 42699 was 40504, checked in by vboxsync, 13 years ago

Solaris 11 build fixes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.2 KB
Line 
1/* $Id: PerformanceSolaris.cpp 40504 2012-03-16 16:38:06Z vboxsync $ */
2
3/** @file
4 *
5 * VBox Solaris-specific Performance Classes implementation.
6 */
7
8/*
9 * Copyright (C) 2008 Oracle Corporation
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
20#undef _FILE_OFFSET_BITS
21#include <procfs.h>
22#include <stdio.h>
23#include <errno.h>
24#include <fcntl.h>
25#include <kstat.h>
26#include <unistd.h>
27#include <sys/sysinfo.h>
28#include <sys/time.h>
29
30#include <iprt/err.h>
31#include <iprt/string.h>
32#include <iprt/alloc.h>
33#include <iprt/param.h>
34#include <VBox/log.h>
35#include "Performance.h"
36
37namespace pm {
38
39class CollectorSolaris : public CollectorHAL
40{
41public:
42 CollectorSolaris();
43 virtual ~CollectorSolaris();
44 virtual int getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available);
45 virtual int getProcessMemoryUsage(RTPROCESS process, ULONG *used);
46
47 virtual int getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle);
48 virtual int getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total);
49private:
50 kstat_ctl_t *mKC;
51 kstat_t *mSysPages;
52 kstat_t *mZFSCache;
53};
54
55CollectorHAL *createHAL()
56{
57 return new CollectorSolaris();
58}
59
60// Collector HAL for Solaris
61
62
63CollectorSolaris::CollectorSolaris()
64 : mKC(0),
65 mSysPages(0),
66 mZFSCache(0)
67{
68 if ((mKC = kstat_open()) == 0)
69 {
70 Log(("kstat_open() -> %d\n", errno));
71 return;
72 }
73
74 if ((mSysPages = kstat_lookup(mKC, (char *)"unix", 0, (char *)"system_pages")) == 0)
75 {
76 Log(("kstat_lookup(system_pages) -> %d\n", errno));
77 return;
78 }
79
80 if ((mZFSCache = kstat_lookup(mKC, (char *)"zfs", 0, (char *)"arcstats")) == 0)
81 {
82 Log(("kstat_lookup(system_pages) -> %d\n", errno));
83 }
84}
85
86CollectorSolaris::~CollectorSolaris()
87{
88 if (mKC)
89 kstat_close(mKC);
90}
91
92int CollectorSolaris::getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle)
93{
94 int rc = VINF_SUCCESS;
95 kstat_t *ksp;
96 uint64_t tmpUser, tmpKernel, tmpIdle;
97 int cpus;
98 cpu_stat_t cpu_stats;
99
100 if (mKC == 0)
101 return VERR_INTERNAL_ERROR;
102
103 tmpUser = tmpKernel = tmpIdle = cpus = 0;
104 for (ksp = mKC->kc_chain; ksp != NULL; ksp = ksp->ks_next) {
105 if (strcmp(ksp->ks_module, "cpu_stat") == 0) {
106 if (kstat_read(mKC, ksp, &cpu_stats) == -1)
107 {
108 Log(("kstat_read() -> %d\n", errno));
109 return VERR_INTERNAL_ERROR;
110 }
111 ++cpus;
112 tmpUser += cpu_stats.cpu_sysinfo.cpu[CPU_USER];
113 tmpKernel += cpu_stats.cpu_sysinfo.cpu[CPU_KERNEL];
114 tmpIdle += cpu_stats.cpu_sysinfo.cpu[CPU_IDLE];
115 }
116 }
117
118 if (cpus == 0)
119 {
120 Log(("no cpu stats found!\n"));
121 return VERR_INTERNAL_ERROR;
122 }
123
124 if (user) *user = tmpUser;
125 if (kernel) *kernel = tmpKernel;
126 if (idle) *idle = tmpIdle;
127
128 return rc;
129}
130
131int CollectorSolaris::getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total)
132{
133 int rc = VINF_SUCCESS;
134 char *pszName;
135 prusage_t prusage;
136
137 RTStrAPrintf(&pszName, "/proc/%d/usage", process);
138 Log(("Opening %s...\n", pszName));
139 int h = open(pszName, O_RDONLY);
140 RTStrFree(pszName);
141
142 if (h != -1)
143 {
144 if (read(h, &prusage, sizeof(prusage)) == sizeof(prusage))
145 {
146 //Assert((pid_t)process == pstatus.pr_pid);
147 //Log(("user=%u kernel=%u total=%u\n", prusage.pr_utime.tv_sec, prusage.pr_stime.tv_sec, prusage.pr_tstamp.tv_sec));
148 *user = (uint64_t)prusage.pr_utime.tv_sec * 1000000000 + prusage.pr_utime.tv_nsec;
149 *kernel = (uint64_t)prusage.pr_stime.tv_sec * 1000000000 + prusage.pr_stime.tv_nsec;
150 *total = (uint64_t)prusage.pr_tstamp.tv_sec * 1000000000 + prusage.pr_tstamp.tv_nsec;
151 //Log(("user=%llu kernel=%llu total=%llu\n", *user, *kernel, *total));
152 }
153 else
154 {
155 Log(("read() -> %d\n", errno));
156 rc = VERR_FILE_IO_ERROR;
157 }
158 close(h);
159 }
160 else
161 {
162 Log(("open() -> %d\n", errno));
163 rc = VERR_ACCESS_DENIED;
164 }
165
166 return rc;
167}
168
169int CollectorSolaris::getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available)
170{
171 int rc = VINF_SUCCESS;
172
173 kstat_named_t *kn;
174
175 if (mKC == 0 || mSysPages == 0)
176 return VERR_INTERNAL_ERROR;
177
178 if (kstat_read(mKC, mSysPages, 0) == -1)
179 {
180 Log(("kstat_read(sys_pages) -> %d\n", errno));
181 return VERR_INTERNAL_ERROR;
182 }
183 if ((kn = (kstat_named_t *)kstat_data_lookup(mSysPages, (char *)"freemem")) == 0)
184 {
185 Log(("kstat_data_lookup(freemem) -> %d\n", errno));
186 return VERR_INTERNAL_ERROR;
187 }
188 *available = kn->value.ul * (PAGE_SIZE/1024);
189
190 if (kstat_read(mKC, mZFSCache, 0) != -1)
191 {
192 if (mZFSCache)
193 {
194 if ((kn = (kstat_named_t *)kstat_data_lookup(mZFSCache, (char *)"size")))
195 {
196 ulong_t ulSize = kn->value.ul;
197
198 if ((kn = (kstat_named_t *)kstat_data_lookup(mZFSCache, (char *)"c_min")))
199 {
200 /*
201 * Account for ZFS minimum arc cache size limit.
202 * "c_min" is the target minimum size of the ZFS cache, and not the hard limit. It's possible
203 * for "size" to shrink below "c_min" (e.g: during boot & high memory consumption).
204 */
205 ulong_t ulMin = kn->value.ul;
206 *available += ulSize > ulMin ? (ulSize - ulMin) / 1024 : 0;
207 }
208 else
209 Log(("kstat_data_lookup(c_min) ->%d\n", errno));
210 }
211 else
212 Log(("kstat_data_lookup(size) -> %d\n", errno));
213 }
214 else
215 Log(("mZFSCache missing.\n"));
216 }
217
218 if ((kn = (kstat_named_t *)kstat_data_lookup(mSysPages, (char *)"physmem")) == 0)
219 {
220 Log(("kstat_data_lookup(physmem) -> %d\n", errno));
221 return VERR_INTERNAL_ERROR;
222 }
223 *total = kn->value.ul * (PAGE_SIZE/1024);
224 *used = *total - *available;
225
226 return rc;
227}
228
229int CollectorSolaris::getProcessMemoryUsage(RTPROCESS process, ULONG *used)
230{
231 int rc = VINF_SUCCESS;
232 char *pszName = NULL;
233 psinfo_t psinfo;
234
235 RTStrAPrintf(&pszName, "/proc/%d/psinfo", process);
236 Log(("Opening %s...\n", pszName));
237 int h = open(pszName, O_RDONLY);
238 RTStrFree(pszName);
239
240 if (h != -1)
241 {
242 if (read(h, &psinfo, sizeof(psinfo)) == sizeof(psinfo))
243 {
244 Assert((pid_t)process == psinfo.pr_pid);
245 *used = psinfo.pr_rssize;
246 }
247 else
248 {
249 Log(("read() -> %d\n", errno));
250 rc = VERR_FILE_IO_ERROR;
251 }
252 close(h);
253 }
254 else
255 {
256 Log(("open() -> %d\n", errno));
257 rc = VERR_ACCESS_DENIED;
258 }
259
260 return rc;
261}
262
263}
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