PerformanceWin.cpp revision e6e249a765867ec61336e3c64c3fee1845a67857
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * VBox Windows-specific Performance Classes implementation.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * Copyright (C) 2008 Sun Microsystems, Inc.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * available from http://www.virtualbox.org. This file is free software;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * you can redistribute it and/or modify it under the terms of the GNU
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * General Public License (GPL) as published by the Free Software
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync * additional information or have any questions.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsyncextern "C" {
590bfe12ce22cd3716448fbb9f4dc51664bfe5e2vboxsync virtual int getHostCpuLoad(ULONG *user, ULONG *kernel, ULONG *idle);
43747b1f0bc8302a238fb35e55857a5e9aa1933dvboxsync virtual int getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available);
43747b1f0bc8302a238fb35e55857a5e9aa1933dvboxsync virtual int getProcessCpuLoad(RTPROCESS process, ULONG *user, ULONG *kernel);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync virtual int getProcessMemoryUsage(RTPROCESS process, ULONG *used);
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync virtual int getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync virtual int getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync long getPropertyHandle(IWbemObjectAccess *objAccess, LPCWSTR name);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync int getObjects(IWbemHiPerfEnum *mEnum, IWbemObjectAccess ***objArray, DWORD *numReturned);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsyncCollectorWin::CollectorWin() : mRefresher(0), mNameSpace(0), mEnumProcessor(0), mEnumProcess(0)
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync // Connect to the desired namespace.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync bstrNameSpace = SysAllocString(L"\\\\.\\root\\cimv2");
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync 0L, // Security flags
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync (void **)&pConfig)))
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync // Add an enumerator to the refresher.
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync L"Win32_PerfRawData_PerfOS_Processor",
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync L"Win32_PerfRawData_PerfProc_Process",
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync Log (("Failed to add enumerators. HR = %x\n", hr));
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync // Retrieve property handles
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync if (RT_FAILURE(getObjects(mEnumProcessor, &apEnumAccess, &dwNumReturned)))
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync mHostCpuLoadNameHandle = getPropertyHandle(apEnumAccess[0], L"Name");
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync mHostCpuLoadUserHandle = getPropertyHandle(apEnumAccess[0], L"PercentUserTime");
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync mHostCpuLoadKernelHandle = getPropertyHandle(apEnumAccess[0], L"PercentPrivilegedTime");
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync mHostCpuLoadIdleHandle = getPropertyHandle(apEnumAccess[0], L"PercentProcessorTime");
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync if (RT_FAILURE(getObjects(mEnumProcess, &apEnumAccess, &dwNumReturned)))
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync mProcessPIDHandle = getPropertyHandle(apEnumAccess[0], L"IDProcess");
25747178cb66800d8386c20b8ffd87f78f24f4e5vboxsync mProcessCpuLoadUserHandle = getPropertyHandle(apEnumAccess[0], L"PercentUserTime");
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync mProcessCpuLoadKernelHandle = getPropertyHandle(apEnumAccess[0], L"PercentPrivilegedTime");
25747178cb66800d8386c20b8ffd87f78f24f4e5vboxsync mProcessCpuLoadTimestampHandle = getPropertyHandle(apEnumAccess[0], L"Timestamp_Sys100NS");
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync mProcessMemoryUsedHandle = getPropertyHandle(apEnumAccess[0], L"WorkingSet");
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsynclong CollectorWin::getPropertyHandle(IWbemObjectAccess *objAccess, LPCWSTR name)
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync Log (("Failed to get property handle for '%ls'. HR = %x\n", name, hr));
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync return 0; /// @todo use throw
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsyncint CollectorWin::getObjects(IWbemHiPerfEnum *mEnum, IWbemObjectAccess ***objArray, DWORD *numReturned)
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsync hr = mEnum->GetObjects(0L, dwNumObjects, *objArray, numReturned);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync // If the buffer was not big enough,
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsync // allocate a bigger buffer and retry.
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsync Log (("Could not allocate enumerator access objects\n"));
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsync Log (("Failed to get objects from enumerator. HR = %x\n", hr));
4fcfe0bd966753617b7ab5fb81fb24709914fc1cvboxsync Log (("Failed to get objects from enumerator. HR = %x\n", hr));
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsyncint CollectorWin::getHostCpuLoad(ULONG *user, ULONG *kernel, ULONG *idle)
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsyncint CollectorWin::getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle)
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync int rc = getObjects(mEnumProcessor, &apEnumAccess, &dwNumReturned);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync for (unsigned i = 0; i < dwNumReturned; i++)
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync if (FAILED (hr = apEnumAccess[i]->ReadPropertyValue(
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync Log (("Failed to read 'Name' property. HR = %x\n", hr));
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync Log (("Failed to read 'PercentUserTime' property. HR = %x\n", hr));
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync Log (("Failed to read 'PercentPrivilegedTime' property. HR = %x\n", hr));
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync Log (("Failed to read 'PercentProcessorTime' property. HR = %x\n", hr));
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync LogFlowThisFunc(("user=%lu kernel=%lu idle=%lu\n", *user, *kernel, *idle));
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync} PROCESSOR_POWER_INFORMATION , *PPROCESSOR_POWER_INFORMATION;
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync PPROCESSOR_POWER_INFORMATION ppi = new PROCESSOR_POWER_INFORMATION[nProcessors];
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync LONG ns = CallNtPowerInformation(ProcessorInformation, NULL, 0, ppi,
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync nProcessors * sizeof(PROCESSOR_POWER_INFORMATION));
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync /* Compute an average over all CPUs */
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync for (unsigned i = 0; i < nProcessors; i++)
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsyncint CollectorWin::getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available)
fbf08fabb4c4b383d6aa2830c2bd5b943a26f10cvboxsyncint CollectorWin::getProcessCpuLoad(RTPROCESS process, ULONG *user, ULONG *kernel)
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsyncint CollectorWin::getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total)
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync int rc = getObjects(mEnumProcess, &apEnumAccess, &dwNumReturned);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync for (unsigned i = 0; i < dwNumReturned; i++)
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync Log (("Failed to read 'IDProcess' property. HR = %x\n", hr));
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync LogFlowThisFunc (("Matching machine process %x against %x...\n", process, dwIDProcess));
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync Log (("Failed to read 'PercentUserTime' property. HR = %x\n", hr));
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync Log (("Failed to read 'PercentPrivilegedTime' property. HR = %x\n", hr));
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync Log (("Failed to read 'Timestamp_Sys100NS' property. HR = %x\n", hr));
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync LogFlowThisFunc(("user=%lu kernel=%lu total=%lu\n", *user, *kernel, *total));
c28fa006ba669ad8f26ae31d00a338379c04ea1bvboxsyncint CollectorWin::getProcessMemoryUsage(RTPROCESS process, ULONG *used)
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync int rc = getObjects(mEnumProcess, &apEnumAccess, &dwNumReturned);
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync for (unsigned i = 0; i < dwNumReturned; i++)
a7d402dcc23137e7b9527be6de80400043a5fbf4vboxsync Log (("Failed to read 'IDProcess' property. HR = %x\n", hr));
24ab761b2beb5af0393c6f2585b6351d8b3085f0vboxsync Log (("Failed to read 'WorkingSet' property. HR = %x\n", hr));