VirtualBox

source: vbox/trunk/src/VBox/Main/solaris/PerformanceSolaris.cpp@ 11099

Last change on this file since 11099 was 11099, checked in by vboxsync, 16 years ago

Workaround for procfs problem. PerfAPI re-enabled for solaris

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.4 KB
Line 
1/* $Id: PerformanceSolaris.cpp 11099 2008-08-04 12:56:21Z vboxsync $ */
2
3/** @file
4 *
5 * VBox Solaris-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#undef _FILE_OFFSET_BITS
25#include <procfs.h>
26#include <stdio.h>
27#include <errno.h>
28#include <fcntl.h>
29#include <kstat.h>
30#include <sys/sysinfo.h>
31#include <sys/time.h>
32
33#include <iprt/err.h>
34#include <iprt/string.h>
35#include <iprt/alloc.h>
36#include <iprt/param.h>
37#include <VBox/log.h>
38#include "Performance.h"
39
40namespace pm {
41
42class CollectorSolaris : public CollectorHAL
43{
44public:
45 CollectorSolaris();
46 ~CollectorSolaris();
47 virtual int getHostCpuMHz(unsigned long *mhz);
48 virtual int getHostMemoryUsage(unsigned long *total, unsigned long *used, unsigned long *available);
49 virtual int getProcessMemoryUsage(RTPROCESS process, unsigned long *used);
50
51 virtual int getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle);
52 virtual int getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total);
53private:
54 kstat_ctl_t *mKC;
55 kstat_t *mSysPages;
56};
57
58// Solaris Metric factory
59
60MetricFactorySolaris::MetricFactorySolaris()
61{
62 mHAL = new CollectorSolaris();
63 Assert(mHAL);
64}
65
66// Collector HAL for Solaris
67
68
69CollectorSolaris::CollectorSolaris() : mKC(0), mSysPages(0)
70{
71 if ((mKC = kstat_open()) == 0)
72 {
73 Log(("kstat_open() -> %d\n", errno));
74 return;
75 }
76
77 if ((mSysPages = kstat_lookup(mKC, "unix", 0, "system_pages")) == 0)
78 {
79 Log(("kstat_lookup(system_pages) -> %d\n", errno));
80 return;
81 }
82}
83
84CollectorSolaris::~CollectorSolaris()
85{
86 kstat_close(mKC);
87}
88
89int CollectorSolaris::getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle)
90{
91 int rc = VINF_SUCCESS;
92 kstat_t *ksp;
93 uint64_t tmpUser, tmpKernel, tmpIdle;
94 int cpus;
95 cpu_stat_t cpu_stats;
96
97 if (mKC == 0)
98 return VERR_INTERNAL_ERROR;
99
100 tmpUser = tmpKernel = tmpIdle = cpus = 0;
101 for (ksp = mKC->kc_chain; ksp != NULL; ksp = ksp->ks_next) {
102 if (strcmp(ksp->ks_module, "cpu_stat") == 0) {
103 if (kstat_read(mKC, ksp, &cpu_stats) == -1)
104 {
105 Log(("kstat_read() -> %d", errno));
106 return VERR_INTERNAL_ERROR;
107 }
108 ++cpus;
109 tmpUser += cpu_stats.cpu_sysinfo.cpu[CPU_USER];
110 tmpKernel += cpu_stats.cpu_sysinfo.cpu[CPU_KERNEL];
111 tmpIdle += cpu_stats.cpu_sysinfo.cpu[CPU_IDLE];
112 }
113 }
114
115 if (cpus == 0)
116 {
117 Log(("no cpu stats found!\n"));
118 return VERR_INTERNAL_ERROR;
119 }
120
121 if (user) *user = tmpUser;
122 if (kernel) *kernel = tmpKernel;
123 if (idle) *idle = tmpIdle;
124
125 return rc;
126}
127
128int CollectorSolaris::getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total)
129{
130 int rc = VINF_SUCCESS;
131 char *pszName;
132 prusage_t prusage;
133
134 RTStrAPrintf(&pszName, "/proc/%d/usage", process);
135 Log(("Opening %s...\n", pszName));
136 int h = open(pszName, O_RDONLY);
137 RTMemFree(pszName);
138
139 if (h != -1)
140 {
141 if (read(h, &prusage, sizeof(prusage)) == sizeof(prusage))
142 {
143 //Assert((pid_t)process == pstatus.pr_pid);
144 //Log(("user=%u kernel=%u total=%u\n", prusage.pr_utime.tv_sec, prusage.pr_stime.tv_sec, prusage.pr_tstamp.tv_sec));
145 *user = (uint64_t)prusage.pr_utime.tv_sec * 1000000000 + prusage.pr_utime.tv_nsec;
146 *kernel = (uint64_t)prusage.pr_stime.tv_sec * 1000000000 + prusage.pr_stime.tv_nsec;
147 *total = (uint64_t)prusage.pr_tstamp.tv_sec * 1000000000 + prusage.pr_tstamp.tv_nsec;
148 //Log(("user=%llu kernel=%llu total=%llu\n", *user, *kernel, *total));
149 }
150 else
151 {
152 Log(("read() -> %d", errno));
153 rc = VERR_FILE_IO_ERROR;
154 }
155 close(h);
156 }
157 else
158 {
159 Log(("open() -> %d", errno));
160 rc = VERR_ACCESS_DENIED;
161 }
162
163 return rc;
164}
165
166int CollectorSolaris::getHostCpuMHz(unsigned long *mhz)
167{
168 return VERR_NOT_IMPLEMENTED;
169}
170
171int CollectorSolaris::getHostMemoryUsage(unsigned long *total, unsigned long *used, unsigned long *available)
172{
173 int rc = VINF_SUCCESS;
174
175 kstat_named_t *kn;
176
177 if (mKC == 0 || mSysPages == 0)
178 return VERR_INTERNAL_ERROR;
179
180 if (kstat_read(mKC, mSysPages, 0) == -1)
181 {
182 Log(("kstat_read(sys_pages) -> %d", errno));
183 return VERR_INTERNAL_ERROR;
184 }
185 if ((kn = (kstat_named_t *)kstat_data_lookup(mSysPages, "freemem")) == 0)
186 {
187 Log(("kstat_data_lookup(freemem) -> %d", errno));
188 return VERR_INTERNAL_ERROR;
189 }
190 *available = kn->value.ul * (PAGE_SIZE/1024);
191 if ((kn = (kstat_named_t *)kstat_data_lookup(mSysPages, "physmem")) == 0)
192 {
193 Log(("kstat_data_lookup(physmem) -> %d", errno));
194 return VERR_INTERNAL_ERROR;
195 }
196 *total = kn->value.ul * (PAGE_SIZE/1024);
197 *used = *total - *available;
198
199 return rc;
200}
201int CollectorSolaris::getProcessMemoryUsage(RTPROCESS process, unsigned long *used)
202{
203 int rc = VINF_SUCCESS;
204 char *pszName;
205 pid_t pid2;
206 char buf[80]; /* @todo: this should be tied to max allowed proc name. */
207 psinfo_t psinfo;
208
209 RTStrAPrintf(&pszName, "/proc/%d/psinfo", process);
210 Log(("Opening %s...\n", pszName));
211 int h = open(pszName, O_RDONLY);
212 RTMemFree(pszName);
213
214 if (h != -1)
215 {
216 if (read(h, &psinfo, sizeof(psinfo)) == sizeof(psinfo))
217 {
218 Assert((pid_t)process == psinfo.pr_pid);
219 *used = psinfo.pr_rssize;
220 }
221 else
222 {
223 Log(("read() -> %d", errno));
224 rc = VERR_FILE_IO_ERROR;
225 }
226 close(h);
227 }
228 else
229 {
230 Log(("open() -> %d", errno));
231 rc = VERR_ACCESS_DENIED;
232 }
233
234 return rc;
235}
236
237}
238
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