PerformanceSolaris.cpp revision 4ffdae046e05f397b6552d5489685f5802bbd249
2N/A/* $Id$ */
2N/A
2N/A/** @file
2N/A *
2N/A * VBox Solaris-specific Performance Classes implementation.
2N/A */
2N/A
2N/A/*
2N/A * Copyright (C) 2008 Sun Microsystems, Inc.
2N/A *
2N/A * This file is part of VirtualBox Open Source Edition (OSE), as
2N/A * available from http://www.virtualbox.org. This file is free software;
2N/A * you can redistribute it and/or modify it under the terms of the GNU
2N/A * General Public License (GPL) as published by the Free Software
2N/A * Foundation, in version 2 as it comes in the "COPYING" file of the
2N/A * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
2N/A * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
2N/A *
2N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
2N/A * Clara, CA 95054 USA or visit http://www.sun.com if you need
2N/A * additional information or have any questions.
2N/A */
2N/A
2N/A#undef _FILE_OFFSET_BITS
2N/A#include <procfs.h>
2N/A#include <stdio.h>
2N/A#include <errno.h>
2N/A#include <fcntl.h>
2N/A#include <kstat.h>
2N/A#include <sys/sysinfo.h>
2N/A#include <sys/time.h>
2N/A
2N/A#include <iprt/err.h>
2N/A#include <iprt/string.h>
2N/A#include <iprt/alloc.h>
2N/A#include <iprt/param.h>
2N/A#include <VBox/log.h>
2N/A#include "Performance.h"
2N/A
2N/Anamespace pm {
2N/A
2N/Aclass CollectorSolaris : public CollectorHAL
2N/A{
2N/Apublic:
2N/A CollectorSolaris();
2N/A virtual ~CollectorSolaris();
2N/A virtual int getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available);
2N/A virtual int getProcessMemoryUsage(RTPROCESS process, ULONG *used);
2N/A
2N/A virtual int getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle);
2N/A virtual int getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total);
2N/Aprivate:
2N/A kstat_ctl_t *mKC;
2N/A kstat_t *mSysPages;
2N/A};
2N/A
2N/ACollectorHAL *createHAL()
2N/A{
2N/A return new CollectorSolaris();
2N/A}
2N/A
2N/A// Collector HAL for Solaris
2N/A
2N/A
2N/ACollectorSolaris::CollectorSolaris() : mKC(0), mSysPages(0)
2N/A{
2N/A if ((mKC = kstat_open()) == 0)
2N/A {
2N/A Log(("kstat_open() -> %d\n", errno));
2N/A return;
2N/A }
2N/A
2N/A if ((mSysPages = kstat_lookup(mKC, "unix", 0, "system_pages")) == 0)
2N/A {
2N/A Log(("kstat_lookup(system_pages) -> %d\n", errno));
2N/A return;
2N/A }
2N/A}
2N/A
2N/ACollectorSolaris::~CollectorSolaris()
2N/A{
2N/A kstat_close(mKC);
2N/A}
2N/A
2N/Aint CollectorSolaris::getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle)
2N/A{
2N/A int rc = VINF_SUCCESS;
2N/A kstat_t *ksp;
2N/A uint64_t tmpUser, tmpKernel, tmpIdle;
2N/A int cpus;
2N/A cpu_stat_t cpu_stats;
2N/A
2N/A if (mKC == 0)
2N/A return VERR_INTERNAL_ERROR;
2N/A
2N/A tmpUser = tmpKernel = tmpIdle = cpus = 0;
2N/A for (ksp = mKC->kc_chain; ksp != NULL; ksp = ksp->ks_next) {
2N/A if (strcmp(ksp->ks_module, "cpu_stat") == 0) {
2N/A if (kstat_read(mKC, ksp, &cpu_stats) == -1)
2N/A {
2N/A Log(("kstat_read() -> %d\n", errno));
2N/A return VERR_INTERNAL_ERROR;
2N/A }
2N/A ++cpus;
2N/A tmpUser += cpu_stats.cpu_sysinfo.cpu[CPU_USER];
2N/A tmpKernel += cpu_stats.cpu_sysinfo.cpu[CPU_KERNEL];
2N/A tmpIdle += cpu_stats.cpu_sysinfo.cpu[CPU_IDLE];
2N/A }
2N/A }
2N/A
2N/A if (cpus == 0)
2N/A {
2N/A Log(("no cpu stats found!\n"));
2N/A return VERR_INTERNAL_ERROR;
2N/A }
2N/A
2N/A if (user) *user = tmpUser;
2N/A if (kernel) *kernel = tmpKernel;
2N/A if (idle) *idle = tmpIdle;
2N/A
2N/A return rc;
2N/A}
2N/A
2N/Aint CollectorSolaris::getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total)
2N/A{
2N/A int rc = VINF_SUCCESS;
2N/A char *pszName;
2N/A prusage_t prusage;
2N/A
2N/A RTStrAPrintf(&pszName, "/proc/%d/usage", process);
2N/A Log(("Opening %s...\n", pszName));
2N/A int h = open(pszName, O_RDONLY);
2N/A RTMemFree(pszName);
2N/A
2N/A if (h != -1)
2N/A {
2N/A if (read(h, &prusage, sizeof(prusage)) == sizeof(prusage))
{
//Assert((pid_t)process == pstatus.pr_pid);
//Log(("user=%u kernel=%u total=%u\n", prusage.pr_utime.tv_sec, prusage.pr_stime.tv_sec, prusage.pr_tstamp.tv_sec));
*user = (uint64_t)prusage.pr_utime.tv_sec * 1000000000 + prusage.pr_utime.tv_nsec;
*kernel = (uint64_t)prusage.pr_stime.tv_sec * 1000000000 + prusage.pr_stime.tv_nsec;
*total = (uint64_t)prusage.pr_tstamp.tv_sec * 1000000000 + prusage.pr_tstamp.tv_nsec;
//Log(("user=%llu kernel=%llu total=%llu\n", *user, *kernel, *total));
}
else
{
Log(("read() -> %d\n", errno));
rc = VERR_FILE_IO_ERROR;
}
close(h);
}
else
{
Log(("open() -> %d\n", errno));
rc = VERR_ACCESS_DENIED;
}
return rc;
}
int CollectorSolaris::getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available)
{
int rc = VINF_SUCCESS;
kstat_named_t *kn;
if (mKC == 0 || mSysPages == 0)
return VERR_INTERNAL_ERROR;
if (kstat_read(mKC, mSysPages, 0) == -1)
{
Log(("kstat_read(sys_pages) -> %d\n", errno));
return VERR_INTERNAL_ERROR;
}
if ((kn = (kstat_named_t *)kstat_data_lookup(mSysPages, "freemem")) == 0)
{
Log(("kstat_data_lookup(freemem) -> %d\n", errno));
return VERR_INTERNAL_ERROR;
}
*available = kn->value.ul * (PAGE_SIZE/1024);
if ((kn = (kstat_named_t *)kstat_data_lookup(mSysPages, "physmem")) == 0)
{
Log(("kstat_data_lookup(physmem) -> %d\n", errno));
return VERR_INTERNAL_ERROR;
}
*total = kn->value.ul * (PAGE_SIZE/1024);
*used = *total - *available;
return rc;
}
int CollectorSolaris::getProcessMemoryUsage(RTPROCESS process, ULONG *used)
{
int rc = VINF_SUCCESS;
char *pszName;
pid_t pid2;
char buf[80]; /* @todo: this should be tied to max allowed proc name. */
psinfo_t psinfo;
RTStrAPrintf(&pszName, "/proc/%d/psinfo", process);
Log(("Opening %s...\n", pszName));
int h = open(pszName, O_RDONLY);
RTMemFree(pszName);
if (h != -1)
{
if (read(h, &psinfo, sizeof(psinfo)) == sizeof(psinfo))
{
Assert((pid_t)process == psinfo.pr_pid);
*used = psinfo.pr_rssize;
}
else
{
Log(("read() -> %d\n", errno));
rc = VERR_FILE_IO_ERROR;
}
close(h);
}
else
{
Log(("open() -> %d\n", errno));
rc = VERR_ACCESS_DENIED;
}
return rc;
}
}