tstCollector.cpp revision fc2ea0e4530238466c837e6eb98a3cbc5650bbfe
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync/* $Id$ */
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync/** @file
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync *
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync * Collector classes test cases.
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync */
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync/*
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync * Copyright (C) 2008 Sun Microsystems, Inc.
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync *
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync * available from http://www.virtualbox.org. This file is free software;
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync * you can redistribute it and/or modify it under the terms of the GNU
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync * General Public License (GPL) as published by the Free Software
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync *
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync * additional information or have any questions.
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync */
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#include <iprt/runtime.h>
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#include <iprt/stream.h>
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#include <iprt/err.h>
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#include <iprt/process.h>
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#ifdef RT_OS_SOLARIS
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#include "../solaris/PerformanceSolaris.cpp"
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#endif
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#ifdef RT_OS_LINUX
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#include "../linux/PerformanceLinux.cpp"
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#endif
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#ifdef RT_OS_WINDOWS
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#define _WIN32_DCOM
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#include <objidl.h>
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#include <objbase.h>
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#include "../win/PerformanceWin.cpp"
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#endif
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#ifdef RT_OS_OS2
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#include "../os2/PerformanceOS2.cpp"
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#endif
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#ifdef RT_OS_DARWIN
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#include "../darwin/PerformanceDarwin.cpp"
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#endif
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsyncpm::CollectorHAL *createCollector()
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync{
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#ifdef RT_OS_SOLARIS
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync return new pm::CollectorSolaris();
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#endif
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#ifdef RT_OS_LINUX
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync return new pm::CollectorLinux();
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#endif
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#ifdef RT_OS_WINDOWS
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync return new pm::CollectorWin();
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#endif
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#ifdef RT_OS_OS2
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync return new pm::CollectorOS2();
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#endif
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#ifdef RT_OS_DARWIN
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync return new pm::CollectorDarwin();
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#endif
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync return 0;
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync}
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#define CALLS_PER_SECOND(n,fn) \
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync nCalls = 0; \
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync start = RTTimeMilliTS(); \
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync do { \
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync rc = collector->fn; \
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync ++nCalls; \
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync } while(RTTimeMilliTS() - start < 1000); \
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync if (RT_FAILURE(rc)) \
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync { \
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync RTPrintf("tstCollector: "#fn" -> %Vrc\n", rc); \
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync } \
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync else \
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync RTPrintf("%50s -- %u calls per second\n", #fn, nCalls); \
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync totalTime += n * 1000000 / nCalls
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsyncint main(int argc, char *argv[])
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync{
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync /*
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync * Initialize the VBox runtime without loading
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync * the support driver.
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync */
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync int rc = RTR3Init();
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync if (RT_FAILURE(rc))
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync {
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync RTPrintf("tstCollector: RTR3Init() -> %d\n", rc);
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync return 1;
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync }
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#ifdef RT_OS_WINDOWS
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync HRESULT hRes = CoInitialize(NULL);
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync /*
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync * Need to initialize security to access performance enumerators.
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync */
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync hRes = CoInitializeSecurity(
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync NULL,
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync -1,
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync NULL,
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync NULL,
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync RPC_C_AUTHN_LEVEL_NONE,
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync RPC_C_IMP_LEVEL_IMPERSONATE,
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync NULL, EOAC_NONE, 0);
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#endif
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync uint64_t start;
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync pm::CollectorHAL *collector = createCollector();
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync if (!collector)
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync {
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync RTPrintf("tstCollector: createMetricFactory() failed\n", rc);
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync return 1;
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync }
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync#if 1
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync uint64_t hostUserStart, hostKernelStart, hostIdleStart;
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync uint64_t hostUserStop, hostKernelStop, hostIdleStop, hostTotal;
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync uint64_t processUserStart, processKernelStart, processTotalStart;
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync uint64_t processUserStop, processKernelStop, processTotalStop;
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync RTPrintf("tstCollector: TESTING - CPU load, sleeping for 5 sec\n");
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync rc = collector->getRawHostCpuLoad(&hostUserStart, &hostKernelStart, &hostIdleStart);
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync if (RT_FAILURE(rc))
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync {
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync RTPrintf("tstCollector: getRawHostCpuLoad() -> %Vrc\n", rc);
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync return 1;
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync }
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStart, &processKernelStart, &processTotalStart);
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync if (RT_FAILURE(rc))
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync {
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Vrc\n", rc);
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync return 1;
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync }
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync RTThreadSleep(5000); // Sleep for 5 seconds
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync rc = collector->getRawHostCpuLoad(&hostUserStop, &hostKernelStop, &hostIdleStop);
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync if (RT_FAILURE(rc))
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync {
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync RTPrintf("tstCollector: getRawHostCpuLoad() -> %Vrc\n", rc);
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync return 1;
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync }
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStop, &processKernelStop, &processTotalStop);
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync if (RT_FAILURE(rc))
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync {
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Vrc\n", rc);
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync return 1;
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync }
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync hostTotal = hostUserStop - hostUserStart
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync + hostKernelStop - hostKernelStart
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync + hostIdleStop - hostIdleStart;
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync RTPrintf("tstCollector: host cpu user = %llu %%\n", (hostUserStop - hostUserStart) * 100 / hostTotal);
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync RTPrintf("tstCollector: host cpu kernel = %llu %%\n", (hostKernelStop - hostKernelStart) * 100 / hostTotal);
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync RTPrintf("tstCollector: host cpu idle = %llu %%\n", (hostIdleStop - hostIdleStart) * 100 / hostTotal);
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync RTPrintf("tstCollector: process cpu user = %llu %%\n", (processUserStop - processUserStart) * 100 / (processTotalStop - processTotalStart));
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync RTPrintf("tstCollector: process cpu kernel = %llu %%\n\n", (processKernelStop - processKernelStart) * 100 / (processTotalStop - processTotalStart));
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync RTPrintf("tstCollector: TESTING - CPU load, looping for 5 sec\n");
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync rc = collector->getRawHostCpuLoad(&hostUserStart, &hostKernelStart, &hostIdleStart);
689e8c44f580478449d47bd48c9af52e82a028aevboxsync if (RT_FAILURE(rc))
689e8c44f580478449d47bd48c9af52e82a028aevboxsync {
689e8c44f580478449d47bd48c9af52e82a028aevboxsync RTPrintf("tstCollector: getRawHostCpuLoad() -> %Vrc\n", rc);
689e8c44f580478449d47bd48c9af52e82a028aevboxsync return 1;
689e8c44f580478449d47bd48c9af52e82a028aevboxsync }
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStart, &processKernelStart, &processTotalStart);
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync if (RT_FAILURE(rc))
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync {
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Vrc\n", rc);
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync return 1;
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync }
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync start = RTTimeMilliTS();
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync while(RTTimeMilliTS() - start < 5000); // Loop for 5 seconds
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync rc = collector->getRawHostCpuLoad(&hostUserStop, &hostKernelStop, &hostIdleStop);
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync if (RT_FAILURE(rc))
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync {
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync RTPrintf("tstCollector: getRawHostCpuLoad() -> %Vrc\n", rc);
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync return 1;
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync }
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStop, &processKernelStop, &processTotalStop);
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync if (RT_FAILURE(rc))
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync {
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Vrc\n", rc);
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync return 1;
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync }
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync hostTotal = hostUserStop - hostUserStart
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync + hostKernelStop - hostKernelStart
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync + hostIdleStop - hostIdleStart;
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync RTPrintf("tstCollector: host cpu user = %llu %%\n", (hostUserStop - hostUserStart) * 100 / hostTotal);
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync RTPrintf("tstCollector: host cpu kernel = %llu %%\n", (hostKernelStop - hostKernelStart) * 100 / hostTotal);
ba0080e61ec19fbcb656e9652b099912e5cccc1avboxsync RTPrintf("tstCollector: host cpu idle = %llu %%\n", (hostIdleStop - hostIdleStart) * 100 / hostTotal);
RTPrintf("tstCollector: process cpu user = %llu %%\n", (processUserStop - processUserStart) * 100 / (processTotalStop - processTotalStart));
RTPrintf("tstCollector: process cpu kernel = %llu %%\n\n", (processKernelStop - processKernelStart) * 100 / (processTotalStop - processTotalStart));
RTPrintf("tstCollector: TESTING - Memory usage\n");
ULONG total, used, available, processUsed;
rc = collector->getHostMemoryUsage(&total, &used, &available);
if (RT_FAILURE(rc))
{
RTPrintf("tstCollector: getHostMemoryUsage() -> %Vrc\n", rc);
return 1;
}
rc = collector->getProcessMemoryUsage(RTProcSelf(), &processUsed);
if (RT_FAILURE(rc))
{
RTPrintf("tstCollector: getProcessMemoryUsage() -> %Vrc\n", rc);
return 1;
}
RTPrintf("tstCollector: host mem total = %lu kB\n", total);
RTPrintf("tstCollector: host mem used = %lu kB\n", used);
RTPrintf("tstCollector: host mem available = %lu kB\n", available);
RTPrintf("tstCollector: process mem used = %lu kB\n", processUsed);
#endif
RTPrintf("\ntstCollector: TESTING - Performance\n\n");
unsigned nCalls;
ULONG tmp;
uint64_t tmp64;
uint32_t totalTime = 0;
RTPROCESS pid = RTProcSelf();
/* Host CPU load */
CALLS_PER_SECOND(1, getRawHostCpuLoad(&tmp64, &tmp64, &tmp64));
/* Process CPU load */
CALLS_PER_SECOND(100, getRawProcessCpuLoad(pid, &tmp64, &tmp64, &tmp64));
/* Host CPU speed */
CALLS_PER_SECOND(1, getHostCpuMHz(&tmp));
/* Host RAM usage */
CALLS_PER_SECOND(1, getHostMemoryUsage(&tmp, &tmp, &tmp));
/* Process RAM usage */
CALLS_PER_SECOND(100, getProcessMemoryUsage(pid, &tmp));
printf("%.2f%% of CPU time\n", totalTime / 10000.);
delete collector;
printf ("\ntstCollector FINISHED.\n");
return rc;
}