1 | /* $Id: VBoxServiceStats.cpp 44528 2013-02-04 14:27:54Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxStats - Guest statistics notification
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2012 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /*******************************************************************************
|
---|
19 | * Header Files *
|
---|
20 | *******************************************************************************/
|
---|
21 | #if defined(RT_OS_WINDOWS)
|
---|
22 | # ifdef TARGET_NT4
|
---|
23 | # undef _WIN32_WINNT
|
---|
24 | # define _WIN32_WINNT 0x501
|
---|
25 | # endif
|
---|
26 | # include <windows.h>
|
---|
27 | # include <psapi.h>
|
---|
28 | # include <winternl.h>
|
---|
29 |
|
---|
30 | #elif defined(RT_OS_LINUX)
|
---|
31 | # include <iprt/ctype.h>
|
---|
32 | # include <iprt/stream.h>
|
---|
33 | # include <unistd.h>
|
---|
34 |
|
---|
35 | #elif defined(RT_OS_SOLARIS)
|
---|
36 | # include <kstat.h>
|
---|
37 | # include <sys/sysinfo.h>
|
---|
38 | # include <unistd.h>
|
---|
39 | #else
|
---|
40 | /** @todo port me. */
|
---|
41 |
|
---|
42 | #endif
|
---|
43 |
|
---|
44 | #include <iprt/assert.h>
|
---|
45 | #include <iprt/mem.h>
|
---|
46 | #include <VBox/param.h>
|
---|
47 | #include <iprt/semaphore.h>
|
---|
48 | #include <iprt/string.h>
|
---|
49 | #include <iprt/system.h>
|
---|
50 | #include <iprt/time.h>
|
---|
51 | #include <iprt/thread.h>
|
---|
52 | #include <VBox/VBoxGuestLib.h>
|
---|
53 | #include "VBoxServiceInternal.h"
|
---|
54 | #include "VBoxServiceUtils.h"
|
---|
55 |
|
---|
56 |
|
---|
57 | /*******************************************************************************
|
---|
58 | * Structures and Typedefs *
|
---|
59 | *******************************************************************************/
|
---|
60 | typedef struct _VBOXSTATSCONTEXT
|
---|
61 | {
|
---|
62 | RTMSINTERVAL cMsStatInterval;
|
---|
63 |
|
---|
64 | uint64_t au64LastCpuLoad_Idle[VMM_MAX_CPU_COUNT];
|
---|
65 | uint64_t au64LastCpuLoad_Kernel[VMM_MAX_CPU_COUNT];
|
---|
66 | uint64_t au64LastCpuLoad_User[VMM_MAX_CPU_COUNT];
|
---|
67 | uint64_t au64LastCpuLoad_Nice[VMM_MAX_CPU_COUNT];
|
---|
68 |
|
---|
69 | #ifdef RT_OS_WINDOWS
|
---|
70 | NTSTATUS (WINAPI *pfnNtQuerySystemInformation)(SYSTEM_INFORMATION_CLASS SystemInformationClass, PVOID SystemInformation, ULONG SystemInformationLength, PULONG ReturnLength);
|
---|
71 | void (WINAPI *pfnGlobalMemoryStatusEx)(LPMEMORYSTATUSEX lpBuffer);
|
---|
72 | BOOL (WINAPI *pfnGetPerformanceInfo)(PPERFORMANCE_INFORMATION pPerformanceInformation, DWORD cb);
|
---|
73 | #endif
|
---|
74 | } VBOXSTATSCONTEXT;
|
---|
75 |
|
---|
76 |
|
---|
77 | /*******************************************************************************
|
---|
78 | * Global Variables *
|
---|
79 | *******************************************************************************/
|
---|
80 | static VBOXSTATSCONTEXT gCtx = {0};
|
---|
81 |
|
---|
82 | /** The semaphore we're blocking on. */
|
---|
83 | static RTSEMEVENTMULTI g_VMStatEvent = NIL_RTSEMEVENTMULTI;
|
---|
84 |
|
---|
85 |
|
---|
86 | /** @copydoc VBOXSERVICE::pfnPreInit */
|
---|
87 | static DECLCALLBACK(int) VBoxServiceVMStatsPreInit(void)
|
---|
88 | {
|
---|
89 | return VINF_SUCCESS;
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 | /** @copydoc VBOXSERVICE::pfnOption */
|
---|
94 | static DECLCALLBACK(int) VBoxServiceVMStatsOption(const char **ppszShort, int argc, char **argv, int *pi)
|
---|
95 | {
|
---|
96 | NOREF(ppszShort);
|
---|
97 | NOREF(argc);
|
---|
98 | NOREF(argv);
|
---|
99 | NOREF(pi);
|
---|
100 |
|
---|
101 | return -1;
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | /** @copydoc VBOXSERVICE::pfnInit */
|
---|
106 | static DECLCALLBACK(int) VBoxServiceVMStatsInit(void)
|
---|
107 | {
|
---|
108 | VBoxServiceVerbose(3, "VBoxServiceVMStatsInit\n");
|
---|
109 |
|
---|
110 | int rc = RTSemEventMultiCreate(&g_VMStatEvent);
|
---|
111 | AssertRCReturn(rc, rc);
|
---|
112 |
|
---|
113 | gCtx.cMsStatInterval = 0; /* default; update disabled */
|
---|
114 | RT_ZERO(gCtx.au64LastCpuLoad_Idle);
|
---|
115 | RT_ZERO(gCtx.au64LastCpuLoad_Kernel);
|
---|
116 | RT_ZERO(gCtx.au64LastCpuLoad_User);
|
---|
117 | RT_ZERO(gCtx.au64LastCpuLoad_Nice);
|
---|
118 |
|
---|
119 | rc = VbglR3StatQueryInterval(&gCtx.cMsStatInterval);
|
---|
120 | if (RT_SUCCESS(rc))
|
---|
121 | VBoxServiceVerbose(3, "VBoxStatsInit: New statistics interval %u seconds\n", gCtx.cMsStatInterval);
|
---|
122 | else
|
---|
123 | VBoxServiceVerbose(3, "VBoxStatsInit: DeviceIoControl failed with %d\n", rc);
|
---|
124 |
|
---|
125 | #ifdef RT_OS_WINDOWS
|
---|
126 | /** @todo Use RTLdr instead of LoadLibrary/GetProcAddress here! */
|
---|
127 |
|
---|
128 | /* NtQuerySystemInformation might be dropped in future releases, so load it dynamically as per Microsoft's recommendation */
|
---|
129 | HMODULE hMod = LoadLibrary("NTDLL.DLL");
|
---|
130 | if (hMod)
|
---|
131 | {
|
---|
132 | *(uintptr_t *)&gCtx.pfnNtQuerySystemInformation = (uintptr_t)GetProcAddress(hMod, "NtQuerySystemInformation");
|
---|
133 | if (gCtx.pfnNtQuerySystemInformation)
|
---|
134 | VBoxServiceVerbose(3, "VBoxStatsInit: gCtx.pfnNtQuerySystemInformation = %x\n", gCtx.pfnNtQuerySystemInformation);
|
---|
135 | else
|
---|
136 | {
|
---|
137 | VBoxServiceVerbose(3, "VBoxStatsInit: NTDLL.NtQuerySystemInformation not found!\n");
|
---|
138 | return VERR_SERVICE_DISABLED;
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | /* GlobalMemoryStatus is win2k and up, so load it dynamically */
|
---|
143 | hMod = LoadLibrary("KERNEL32.DLL");
|
---|
144 | if (hMod)
|
---|
145 | {
|
---|
146 | *(uintptr_t *)&gCtx.pfnGlobalMemoryStatusEx = (uintptr_t)GetProcAddress(hMod, "GlobalMemoryStatusEx");
|
---|
147 | if (gCtx.pfnGlobalMemoryStatusEx)
|
---|
148 | VBoxServiceVerbose(3, "VBoxStatsInit: gCtx.GlobalMemoryStatusEx = %x\n", gCtx.pfnGlobalMemoryStatusEx);
|
---|
149 | else
|
---|
150 | {
|
---|
151 | /** @todo Now fails in NT4; do we care? */
|
---|
152 | VBoxServiceVerbose(3, "VBoxStatsInit: KERNEL32.GlobalMemoryStatusEx not found!\n");
|
---|
153 | return VERR_SERVICE_DISABLED;
|
---|
154 | }
|
---|
155 | }
|
---|
156 | /* GetPerformanceInfo is xp and up, so load it dynamically */
|
---|
157 | hMod = LoadLibrary("PSAPI.DLL");
|
---|
158 | if (hMod)
|
---|
159 | {
|
---|
160 | *(uintptr_t *)&gCtx.pfnGetPerformanceInfo = (uintptr_t)GetProcAddress(hMod, "GetPerformanceInfo");
|
---|
161 | if (gCtx.pfnGetPerformanceInfo)
|
---|
162 | VBoxServiceVerbose(3, "VBoxStatsInit: gCtx.pfnGetPerformanceInfo= %x\n", gCtx.pfnGetPerformanceInfo);
|
---|
163 | /* failure is not fatal */
|
---|
164 | }
|
---|
165 | #endif /* RT_OS_WINDOWS */
|
---|
166 |
|
---|
167 | return VINF_SUCCESS;
|
---|
168 | }
|
---|
169 |
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Gathers VM statistics and reports them to the host.
|
---|
173 | */
|
---|
174 | static void VBoxServiceVMStatsReport(void)
|
---|
175 | {
|
---|
176 | #if defined(RT_OS_WINDOWS)
|
---|
177 | SYSTEM_INFO systemInfo;
|
---|
178 | PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION pProcInfo;
|
---|
179 | MEMORYSTATUSEX memStatus;
|
---|
180 | uint32_t cbStruct;
|
---|
181 | DWORD cbReturned;
|
---|
182 |
|
---|
183 | Assert(gCtx.pfnGlobalMemoryStatusEx && gCtx.pfnNtQuerySystemInformation);
|
---|
184 | if ( !gCtx.pfnGlobalMemoryStatusEx
|
---|
185 | || !gCtx.pfnNtQuerySystemInformation)
|
---|
186 | return;
|
---|
187 |
|
---|
188 | /* Clear the report so we don't report garbage should NtQuerySystemInformation
|
---|
189 | behave in an unexpected manner. */
|
---|
190 | VMMDevReportGuestStats req;
|
---|
191 | RT_ZERO(req);
|
---|
192 |
|
---|
193 | /* Query and report guest statistics */
|
---|
194 | GetSystemInfo(&systemInfo);
|
---|
195 |
|
---|
196 | memStatus.dwLength = sizeof(memStatus);
|
---|
197 | gCtx.pfnGlobalMemoryStatusEx(&memStatus);
|
---|
198 |
|
---|
199 | req.guestStats.u32PageSize = systemInfo.dwPageSize;
|
---|
200 | req.guestStats.u32PhysMemTotal = (uint32_t)(memStatus.ullTotalPhys / _4K);
|
---|
201 | req.guestStats.u32PhysMemAvail = (uint32_t)(memStatus.ullAvailPhys / _4K);
|
---|
202 | /* The current size of the committed memory limit, in bytes. This is physical
|
---|
203 | memory plus the size of the page file, minus a small overhead. */
|
---|
204 | req.guestStats.u32PageFileSize = (uint32_t)(memStatus.ullTotalPageFile / _4K) - req.guestStats.u32PhysMemTotal;
|
---|
205 | req.guestStats.u32MemoryLoad = memStatus.dwMemoryLoad;
|
---|
206 | req.guestStats.u32StatCaps = VBOX_GUEST_STAT_PHYS_MEM_TOTAL
|
---|
207 | | VBOX_GUEST_STAT_PHYS_MEM_AVAIL
|
---|
208 | | VBOX_GUEST_STAT_PAGE_FILE_SIZE
|
---|
209 | | VBOX_GUEST_STAT_MEMORY_LOAD;
|
---|
210 | #ifdef VBOX_WITH_MEMBALLOON
|
---|
211 | req.guestStats.u32PhysMemBalloon = VBoxServiceBalloonQueryPages(_4K);
|
---|
212 | req.guestStats.u32StatCaps |= VBOX_GUEST_STAT_PHYS_MEM_BALLOON;
|
---|
213 | #else
|
---|
214 | req.guestStats.u32PhysMemBalloon = 0;
|
---|
215 | #endif
|
---|
216 |
|
---|
217 | if (gCtx.pfnGetPerformanceInfo)
|
---|
218 | {
|
---|
219 | PERFORMANCE_INFORMATION perfInfo;
|
---|
220 |
|
---|
221 | if (gCtx.pfnGetPerformanceInfo(&perfInfo, sizeof(perfInfo)))
|
---|
222 | {
|
---|
223 | req.guestStats.u32Processes = perfInfo.ProcessCount;
|
---|
224 | req.guestStats.u32Threads = perfInfo.ThreadCount;
|
---|
225 | req.guestStats.u32Handles = perfInfo.HandleCount;
|
---|
226 | req.guestStats.u32MemCommitTotal = perfInfo.CommitTotal; /* already in pages */
|
---|
227 | req.guestStats.u32MemKernelTotal = perfInfo.KernelTotal; /* already in pages */
|
---|
228 | req.guestStats.u32MemKernelPaged = perfInfo.KernelPaged; /* already in pages */
|
---|
229 | req.guestStats.u32MemKernelNonPaged = perfInfo.KernelNonpaged; /* already in pages */
|
---|
230 | req.guestStats.u32MemSystemCache = perfInfo.SystemCache; /* already in pages */
|
---|
231 | req.guestStats.u32StatCaps |= VBOX_GUEST_STAT_PROCESSES | VBOX_GUEST_STAT_THREADS | VBOX_GUEST_STAT_HANDLES
|
---|
232 | | VBOX_GUEST_STAT_MEM_COMMIT_TOTAL | VBOX_GUEST_STAT_MEM_KERNEL_TOTAL
|
---|
233 | | VBOX_GUEST_STAT_MEM_KERNEL_PAGED | VBOX_GUEST_STAT_MEM_KERNEL_NONPAGED
|
---|
234 | | VBOX_GUEST_STAT_MEM_SYSTEM_CACHE;
|
---|
235 | }
|
---|
236 | else
|
---|
237 | VBoxServiceVerbose(3, "VBoxServiceVMStatsReport: GetPerformanceInfo failed with %d\n", GetLastError());
|
---|
238 | }
|
---|
239 |
|
---|
240 | /* Query CPU load information */
|
---|
241 | cbStruct = systemInfo.dwNumberOfProcessors * sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION);
|
---|
242 | pProcInfo = (PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION)RTMemAlloc(cbStruct);
|
---|
243 | if (!pProcInfo)
|
---|
244 | return;
|
---|
245 |
|
---|
246 | /* Unfortunately GetSystemTimes is XP SP1 and up only, so we need to use the semi-undocumented NtQuerySystemInformation */
|
---|
247 | NTSTATUS rc = gCtx.pfnNtQuerySystemInformation(SystemProcessorPerformanceInformation, pProcInfo, cbStruct, &cbReturned);
|
---|
248 | if ( !rc
|
---|
249 | && cbReturned == cbStruct)
|
---|
250 | {
|
---|
251 | if (gCtx.au64LastCpuLoad_Kernel == 0)
|
---|
252 | {
|
---|
253 | /* first time */
|
---|
254 | gCtx.au64LastCpuLoad_Idle[0] = pProcInfo->IdleTime.QuadPart;
|
---|
255 | gCtx.au64LastCpuLoad_Kernel[0] = pProcInfo->KernelTime.QuadPart;
|
---|
256 | gCtx.au64LastCpuLoad_User[0] = pProcInfo->UserTime.QuadPart;
|
---|
257 |
|
---|
258 | Sleep(250);
|
---|
259 |
|
---|
260 | rc = gCtx.pfnNtQuerySystemInformation(SystemProcessorPerformanceInformation, pProcInfo, cbStruct, &cbReturned);
|
---|
261 | Assert(!rc);
|
---|
262 | }
|
---|
263 |
|
---|
264 | uint64_t deltaIdle = (pProcInfo->IdleTime.QuadPart - gCtx.au64LastCpuLoad_Idle[0]);
|
---|
265 | uint64_t deltaKernel = (pProcInfo->KernelTime.QuadPart - gCtx.au64LastCpuLoad_Kernel[0]);
|
---|
266 | uint64_t deltaUser = (pProcInfo->UserTime.QuadPart - gCtx.au64LastCpuLoad_User[0]);
|
---|
267 | deltaKernel -= deltaIdle; /* idle time is added to kernel time */
|
---|
268 | uint64_t ullTotalTime = deltaIdle + deltaKernel + deltaUser;
|
---|
269 | if (ullTotalTime == 0) /* Prevent division through zero. */
|
---|
270 | ullTotalTime = 1;
|
---|
271 |
|
---|
272 | req.guestStats.u32CpuLoad_Idle = (uint32_t)(deltaIdle * 100 / ullTotalTime);
|
---|
273 | req.guestStats.u32CpuLoad_Kernel = (uint32_t)(deltaKernel* 100 / ullTotalTime);
|
---|
274 | req.guestStats.u32CpuLoad_User = (uint32_t)(deltaUser * 100 / ullTotalTime);
|
---|
275 |
|
---|
276 | req.guestStats.u32StatCaps |= VBOX_GUEST_STAT_CPU_LOAD_IDLE | VBOX_GUEST_STAT_CPU_LOAD_KERNEL | VBOX_GUEST_STAT_CPU_LOAD_USER;
|
---|
277 |
|
---|
278 | gCtx.au64LastCpuLoad_Idle[0] = pProcInfo->IdleTime.QuadPart;
|
---|
279 | gCtx.au64LastCpuLoad_Kernel[0] = pProcInfo->KernelTime.QuadPart;
|
---|
280 | gCtx.au64LastCpuLoad_User[0] = pProcInfo->UserTime.QuadPart;
|
---|
281 | /** @todo SMP: report details for each CPU? */
|
---|
282 | }
|
---|
283 |
|
---|
284 | for (uint32_t i = 0; i < systemInfo.dwNumberOfProcessors; i++)
|
---|
285 | {
|
---|
286 | req.guestStats.u32CpuId = i;
|
---|
287 |
|
---|
288 | rc = VbglR3StatReport(&req);
|
---|
289 | if (RT_SUCCESS(rc))
|
---|
290 | VBoxServiceVerbose(3, "VBoxStatsReportStatistics: new statistics (CPU %u) reported successfully!\n", i);
|
---|
291 | else
|
---|
292 | VBoxServiceVerbose(3, "VBoxStatsReportStatistics: DeviceIoControl (stats report) failed with %d\n", GetLastError());
|
---|
293 | }
|
---|
294 |
|
---|
295 | RTMemFree(pProcInfo);
|
---|
296 |
|
---|
297 | #elif defined(RT_OS_LINUX)
|
---|
298 | VMMDevReportGuestStats req;
|
---|
299 | RT_ZERO(req);
|
---|
300 | PRTSTREAM pStrm;
|
---|
301 | char szLine[256];
|
---|
302 | char *psz;
|
---|
303 |
|
---|
304 | int rc = RTStrmOpen("/proc/meminfo", "r", &pStrm);
|
---|
305 | if (RT_SUCCESS(rc))
|
---|
306 | {
|
---|
307 | uint64_t u64Kb;
|
---|
308 | uint64_t u64Total = 0, u64Free = 0, u64Buffers = 0, u64Cached = 0, u64PagedTotal = 0;
|
---|
309 | for (;;)
|
---|
310 | {
|
---|
311 | rc = RTStrmGetLine(pStrm, szLine, sizeof(szLine));
|
---|
312 | if (RT_FAILURE(rc))
|
---|
313 | break;
|
---|
314 | if (strstr(szLine, "MemTotal:") == szLine)
|
---|
315 | {
|
---|
316 | rc = RTStrToUInt64Ex(RTStrStripL(&szLine[9]), &psz, 0, &u64Kb);
|
---|
317 | if (RT_SUCCESS(rc))
|
---|
318 | u64Total = u64Kb * _1K;
|
---|
319 | }
|
---|
320 | else if (strstr(szLine, "MemFree:") == szLine)
|
---|
321 | {
|
---|
322 | rc = RTStrToUInt64Ex(RTStrStripL(&szLine[8]), &psz, 0, &u64Kb);
|
---|
323 | if (RT_SUCCESS(rc))
|
---|
324 | u64Free = u64Kb * _1K;
|
---|
325 | }
|
---|
326 | else if (strstr(szLine, "Buffers:") == szLine)
|
---|
327 | {
|
---|
328 | rc = RTStrToUInt64Ex(RTStrStripL(&szLine[8]), &psz, 0, &u64Kb);
|
---|
329 | if (RT_SUCCESS(rc))
|
---|
330 | u64Buffers = u64Kb * _1K;
|
---|
331 | }
|
---|
332 | else if (strstr(szLine, "Cached:") == szLine)
|
---|
333 | {
|
---|
334 | rc = RTStrToUInt64Ex(RTStrStripL(&szLine[7]), &psz, 0, &u64Kb);
|
---|
335 | if (RT_SUCCESS(rc))
|
---|
336 | u64Cached = u64Kb * _1K;
|
---|
337 | }
|
---|
338 | else if (strstr(szLine, "SwapTotal:") == szLine)
|
---|
339 | {
|
---|
340 | rc = RTStrToUInt64Ex(RTStrStripL(&szLine[10]), &psz, 0, &u64Kb);
|
---|
341 | if (RT_SUCCESS(rc))
|
---|
342 | u64PagedTotal = u64Kb * _1K;
|
---|
343 | }
|
---|
344 | }
|
---|
345 | req.guestStats.u32PhysMemTotal = u64Total / _4K;
|
---|
346 | req.guestStats.u32PhysMemAvail = (u64Free + u64Buffers + u64Cached) / _4K;
|
---|
347 | req.guestStats.u32MemSystemCache = (u64Buffers + u64Cached) / _4K;
|
---|
348 | req.guestStats.u32PageFileSize = u64PagedTotal / _4K;
|
---|
349 | RTStrmClose(pStrm);
|
---|
350 | }
|
---|
351 | else
|
---|
352 | VBoxServiceVerbose(3, "VBoxStatsReportStatistics: memory info not available!\n");
|
---|
353 |
|
---|
354 | req.guestStats.u32PageSize = getpagesize();
|
---|
355 | req.guestStats.u32StatCaps = VBOX_GUEST_STAT_PHYS_MEM_TOTAL
|
---|
356 | | VBOX_GUEST_STAT_PHYS_MEM_AVAIL
|
---|
357 | | VBOX_GUEST_STAT_MEM_SYSTEM_CACHE
|
---|
358 | | VBOX_GUEST_STAT_PAGE_FILE_SIZE;
|
---|
359 | #ifdef VBOX_WITH_MEMBALLOON
|
---|
360 | req.guestStats.u32PhysMemBalloon = VBoxServiceBalloonQueryPages(_4K);
|
---|
361 | req.guestStats.u32StatCaps |= VBOX_GUEST_STAT_PHYS_MEM_BALLOON;
|
---|
362 | #else
|
---|
363 | req.guestStats.u32PhysMemBalloon = 0;
|
---|
364 | #endif
|
---|
365 |
|
---|
366 |
|
---|
367 | /** @todo req.guestStats.u32Threads */
|
---|
368 | /** @todo req.guestStats.u32Processes */
|
---|
369 | /* req.guestStats.u32Handles doesn't make sense here. */
|
---|
370 | /** @todo req.guestStats.u32MemoryLoad */
|
---|
371 | /** @todo req.guestStats.u32MemCommitTotal */
|
---|
372 | /** @todo req.guestStats.u32MemKernelTotal */
|
---|
373 | /** @todo req.guestStats.u32MemKernelPaged, make any sense? = u32MemKernelTotal? */
|
---|
374 | /** @todo req.guestStats.u32MemKernelNonPaged, make any sense? = 0? */
|
---|
375 |
|
---|
376 | bool fCpuInfoAvail = false;
|
---|
377 | rc = RTStrmOpen("/proc/stat", "r", &pStrm);
|
---|
378 | if (RT_SUCCESS(rc))
|
---|
379 | {
|
---|
380 | for (;;)
|
---|
381 | {
|
---|
382 | rc = RTStrmGetLine(pStrm, szLine, sizeof(szLine));
|
---|
383 | if (RT_FAILURE(rc))
|
---|
384 | break;
|
---|
385 | if ( strstr(szLine, "cpu") == szLine
|
---|
386 | && strlen(szLine) > 3
|
---|
387 | && RT_C_IS_DIGIT(szLine[3]))
|
---|
388 | {
|
---|
389 | uint32_t u32CpuId;
|
---|
390 | rc = RTStrToUInt32Ex(&szLine[3], &psz, 0, &u32CpuId);
|
---|
391 | if (u32CpuId < VMM_MAX_CPU_COUNT)
|
---|
392 | {
|
---|
393 | uint64_t u64User = 0;
|
---|
394 | if (RT_SUCCESS(rc))
|
---|
395 | rc = RTStrToUInt64Ex(RTStrStripL(psz), &psz, 0, &u64User);
|
---|
396 |
|
---|
397 | uint64_t u64Nice = 0;
|
---|
398 | if (RT_SUCCESS(rc))
|
---|
399 | rc = RTStrToUInt64Ex(RTStrStripL(psz), &psz, 0, &u64Nice);
|
---|
400 |
|
---|
401 | uint64_t u64System = 0;
|
---|
402 | if (RT_SUCCESS(rc))
|
---|
403 | rc = RTStrToUInt64Ex(RTStrStripL(psz), &psz, 0, &u64System);
|
---|
404 |
|
---|
405 | uint64_t u64Idle = 0;
|
---|
406 | if (RT_SUCCESS(rc))
|
---|
407 | rc = RTStrToUInt64Ex(RTStrStripL(psz), &psz, 0, &u64Idle);
|
---|
408 |
|
---|
409 | uint64_t u64DeltaIdle = u64Idle - gCtx.au64LastCpuLoad_Idle[u32CpuId];
|
---|
410 | uint64_t u64DeltaSystem = u64System - gCtx.au64LastCpuLoad_Kernel[u32CpuId];
|
---|
411 | uint64_t u64DeltaUser = u64User - gCtx.au64LastCpuLoad_User[u32CpuId];
|
---|
412 | uint64_t u64DeltaNice = u64Nice - gCtx.au64LastCpuLoad_Nice[u32CpuId];
|
---|
413 |
|
---|
414 | uint64_t u64DeltaAll = u64DeltaIdle
|
---|
415 | + u64DeltaSystem
|
---|
416 | + u64DeltaUser
|
---|
417 | + u64DeltaNice;
|
---|
418 | if (u64DeltaAll == 0) /* Prevent division through zero. */
|
---|
419 | u64DeltaAll = 1;
|
---|
420 |
|
---|
421 | gCtx.au64LastCpuLoad_Idle[u32CpuId] = u64Idle;
|
---|
422 | gCtx.au64LastCpuLoad_Kernel[u32CpuId] = u64System;
|
---|
423 | gCtx.au64LastCpuLoad_User[u32CpuId] = u64User;
|
---|
424 | gCtx.au64LastCpuLoad_Nice[u32CpuId] = u64Nice;
|
---|
425 |
|
---|
426 | req.guestStats.u32CpuId = u32CpuId;
|
---|
427 | req.guestStats.u32CpuLoad_Idle = (uint32_t)(u64DeltaIdle * 100 / u64DeltaAll);
|
---|
428 | req.guestStats.u32CpuLoad_Kernel = (uint32_t)(u64DeltaSystem * 100 / u64DeltaAll);
|
---|
429 | req.guestStats.u32CpuLoad_User = (uint32_t)((u64DeltaUser
|
---|
430 | + u64DeltaNice) * 100 / u64DeltaAll);
|
---|
431 | req.guestStats.u32StatCaps |= VBOX_GUEST_STAT_CPU_LOAD_IDLE
|
---|
432 | | VBOX_GUEST_STAT_CPU_LOAD_KERNEL
|
---|
433 | | VBOX_GUEST_STAT_CPU_LOAD_USER;
|
---|
434 | fCpuInfoAvail = true;
|
---|
435 | rc = VbglR3StatReport(&req);
|
---|
436 | if (RT_SUCCESS(rc))
|
---|
437 | VBoxServiceVerbose(3, "VBoxStatsReportStatistics: new statistics (CPU %u) reported successfully!\n", u32CpuId);
|
---|
438 | else
|
---|
439 | VBoxServiceVerbose(3, "VBoxStatsReportStatistics: stats report failed with rc=%Rrc\n", rc);
|
---|
440 | }
|
---|
441 | else
|
---|
442 | VBoxServiceVerbose(3, "VBoxStatsReportStatistics: skipping information for CPU%u\n", u32CpuId);
|
---|
443 | }
|
---|
444 | }
|
---|
445 | RTStrmClose(pStrm);
|
---|
446 | }
|
---|
447 | if (!fCpuInfoAvail)
|
---|
448 | {
|
---|
449 | VBoxServiceVerbose(3, "VBoxStatsReportStatistics: CPU info not available!\n");
|
---|
450 | rc = VbglR3StatReport(&req);
|
---|
451 | if (RT_SUCCESS(rc))
|
---|
452 | VBoxServiceVerbose(3, "VBoxStatsReportStatistics: new statistics reported successfully!\n");
|
---|
453 | else
|
---|
454 | VBoxServiceVerbose(3, "VBoxStatsReportStatistics: stats report failed with rc=%Rrc\n", rc);
|
---|
455 | }
|
---|
456 |
|
---|
457 | #elif defined(RT_OS_SOLARIS)
|
---|
458 | VMMDevReportGuestStats req;
|
---|
459 | RT_ZERO(req);
|
---|
460 | kstat_ctl_t *pStatKern = kstat_open();
|
---|
461 | if (pStatKern)
|
---|
462 | {
|
---|
463 | /*
|
---|
464 | * Memory statistics.
|
---|
465 | */
|
---|
466 | uint64_t u64Total = 0, u64Free = 0, u64Buffers = 0, u64Cached = 0, u64PagedTotal = 0;
|
---|
467 | int rc = -1;
|
---|
468 | kstat_t *pStatPages = kstat_lookup(pStatKern, "unix", 0 /* instance */, "system_pages");
|
---|
469 | if (pStatPages)
|
---|
470 | {
|
---|
471 | rc = kstat_read(pStatKern, pStatPages, NULL /* optional-copy-buf */);
|
---|
472 | if (rc != -1)
|
---|
473 | {
|
---|
474 | kstat_named_t *pStat = NULL;
|
---|
475 | pStat = (kstat_named_t *)kstat_data_lookup(pStatPages, "pagestotal");
|
---|
476 | if (pStat)
|
---|
477 | u64Total = pStat->value.ul;
|
---|
478 |
|
---|
479 | pStat = (kstat_named_t *)kstat_data_lookup(pStatPages, "freemem");
|
---|
480 | if (pStat)
|
---|
481 | u64Free = pStat->value.ul;
|
---|
482 | }
|
---|
483 | }
|
---|
484 |
|
---|
485 | kstat_t *pStatZFS = kstat_lookup(pStatKern, "zfs", 0 /* instance */, "arcstats");
|
---|
486 | if (pStatZFS)
|
---|
487 | {
|
---|
488 | rc = kstat_read(pStatKern, pStatZFS, NULL /* optional-copy-buf */);
|
---|
489 | if (rc != -1)
|
---|
490 | {
|
---|
491 | kstat_named_t *pStat = (kstat_named_t *)kstat_data_lookup(pStatZFS, "size");
|
---|
492 | if (pStat)
|
---|
493 | u64Cached = pStat->value.ul;
|
---|
494 | }
|
---|
495 | }
|
---|
496 |
|
---|
497 | /*
|
---|
498 | * The vminfo are accumulative counters updated every "N" ticks. Let's get the
|
---|
499 | * number of stat updates so far and use that to divide the swap counter.
|
---|
500 | */
|
---|
501 | kstat_t *pStatInfo = kstat_lookup(pStatKern, "unix", 0 /* instance */, "sysinfo");
|
---|
502 | if (pStatInfo)
|
---|
503 | {
|
---|
504 | sysinfo_t SysInfo;
|
---|
505 | rc = kstat_read(pStatKern, pStatInfo, &SysInfo);
|
---|
506 | if (rc != -1)
|
---|
507 | {
|
---|
508 | kstat_t *pStatVMInfo = kstat_lookup(pStatKern, "unix", 0 /* instance */, "vminfo");
|
---|
509 | if (pStatVMInfo)
|
---|
510 | {
|
---|
511 | vminfo_t VMInfo;
|
---|
512 | rc = kstat_read(pStatKern, pStatVMInfo, &VMInfo);
|
---|
513 | if (rc != -1)
|
---|
514 | {
|
---|
515 | Assert(SysInfo.updates != 0);
|
---|
516 | u64PagedTotal = VMInfo.swap_avail / SysInfo.updates;
|
---|
517 | }
|
---|
518 | }
|
---|
519 | }
|
---|
520 | }
|
---|
521 |
|
---|
522 | req.guestStats.u32PhysMemTotal = u64Total; /* already in pages */
|
---|
523 | req.guestStats.u32PhysMemAvail = u64Free; /* already in pages */
|
---|
524 | req.guestStats.u32MemSystemCache = u64Cached / _4K;
|
---|
525 | req.guestStats.u32PageFileSize = u64PagedTotal; /* already in pages */
|
---|
526 | /** @todo req.guestStats.u32Threads */
|
---|
527 | /** @todo req.guestStats.u32Processes */
|
---|
528 | /** @todo req.guestStats.u32Handles -- ??? */
|
---|
529 | /** @todo req.guestStats.u32MemoryLoad */
|
---|
530 | /** @todo req.guestStats.u32MemCommitTotal */
|
---|
531 | /** @todo req.guestStats.u32MemKernelTotal */
|
---|
532 | /** @todo req.guestStats.u32MemKernelPaged */
|
---|
533 | /** @todo req.guestStats.u32MemKernelNonPaged */
|
---|
534 | req.guestStats.u32PageSize = getpagesize();
|
---|
535 |
|
---|
536 | req.guestStats.u32StatCaps = VBOX_GUEST_STAT_PHYS_MEM_TOTAL
|
---|
537 | | VBOX_GUEST_STAT_PHYS_MEM_AVAIL
|
---|
538 | | VBOX_GUEST_STAT_MEM_SYSTEM_CACHE
|
---|
539 | | VBOX_GUEST_STAT_PAGE_FILE_SIZE;
|
---|
540 | #ifdef VBOX_WITH_MEMBALLOON
|
---|
541 | req.guestStats.u32PhysMemBalloon = VBoxServiceBalloonQueryPages(_4K);
|
---|
542 | req.guestStats.u32StatCaps |= VBOX_GUEST_STAT_PHYS_MEM_BALLOON;
|
---|
543 | #else
|
---|
544 | req.guestStats.u32PhysMemBalloon = 0;
|
---|
545 | #endif
|
---|
546 |
|
---|
547 | /*
|
---|
548 | * CPU statistics.
|
---|
549 | */
|
---|
550 | cpu_stat_t StatCPU;
|
---|
551 | RT_ZERO(StatCPU);
|
---|
552 | kstat_t *pStatNode = NULL;
|
---|
553 | uint32_t cCPUs = 0;
|
---|
554 | bool fCpuInfoAvail = false;
|
---|
555 | for (pStatNode = pStatKern->kc_chain; pStatNode != NULL; pStatNode = pStatNode->ks_next)
|
---|
556 | {
|
---|
557 | if (!strcmp(pStatNode->ks_module, "cpu_stat"))
|
---|
558 | {
|
---|
559 | rc = kstat_read(pStatKern, pStatNode, &StatCPU);
|
---|
560 | if (rc == -1)
|
---|
561 | break;
|
---|
562 |
|
---|
563 | uint64_t u64Idle = StatCPU.cpu_sysinfo.cpu[CPU_IDLE];
|
---|
564 | uint64_t u64User = StatCPU.cpu_sysinfo.cpu[CPU_USER];
|
---|
565 | uint64_t u64System = StatCPU.cpu_sysinfo.cpu[CPU_KERNEL];
|
---|
566 |
|
---|
567 | uint64_t u64DeltaIdle = u64Idle - gCtx.au64LastCpuLoad_Idle[cCPUs];
|
---|
568 | uint64_t u64DeltaSystem = u64System - gCtx.au64LastCpuLoad_Kernel[cCPUs];
|
---|
569 | uint64_t u64DeltaUser = u64User - gCtx.au64LastCpuLoad_User[cCPUs];
|
---|
570 |
|
---|
571 | uint64_t u64DeltaAll = u64DeltaIdle + u64DeltaSystem + u64DeltaUser;
|
---|
572 | if (u64DeltaAll == 0) /* Prevent division through zero. */
|
---|
573 | u64DeltaAll = 1;
|
---|
574 |
|
---|
575 | gCtx.au64LastCpuLoad_Idle[cCPUs] = u64Idle;
|
---|
576 | gCtx.au64LastCpuLoad_Kernel[cCPUs] = u64System;
|
---|
577 | gCtx.au64LastCpuLoad_User[cCPUs] = u64User;
|
---|
578 |
|
---|
579 | req.guestStats.u32CpuId = cCPUs;
|
---|
580 | req.guestStats.u32CpuLoad_Idle = (uint32_t)(u64DeltaIdle * 100 / u64DeltaAll);
|
---|
581 | req.guestStats.u32CpuLoad_Kernel = (uint32_t)(u64DeltaSystem * 100 / u64DeltaAll);
|
---|
582 | req.guestStats.u32CpuLoad_User = (uint32_t)(u64DeltaUser * 100 / u64DeltaAll);
|
---|
583 |
|
---|
584 | req.guestStats.u32StatCaps |= VBOX_GUEST_STAT_CPU_LOAD_IDLE
|
---|
585 | | VBOX_GUEST_STAT_CPU_LOAD_KERNEL
|
---|
586 | | VBOX_GUEST_STAT_CPU_LOAD_USER;
|
---|
587 | fCpuInfoAvail = true;
|
---|
588 |
|
---|
589 | rc = VbglR3StatReport(&req);
|
---|
590 | if (RT_SUCCESS(rc))
|
---|
591 | VBoxServiceVerbose(3, "VBoxStatsReportStatistics: new statistics (CPU %u) reported successfully!\n", cCPUs);
|
---|
592 | else
|
---|
593 | VBoxServiceVerbose(3, "VBoxStatsReportStatistics: stats report failed with rc=%Rrc\n", rc);
|
---|
594 | cCPUs++;
|
---|
595 | }
|
---|
596 | }
|
---|
597 |
|
---|
598 | /*
|
---|
599 | * Report whatever statistics were collected.
|
---|
600 | */
|
---|
601 | if (!fCpuInfoAvail)
|
---|
602 | {
|
---|
603 | VBoxServiceVerbose(3, "VBoxStatsReportStatistics: CPU info not available!\n");
|
---|
604 | rc = VbglR3StatReport(&req);
|
---|
605 | if (RT_SUCCESS(rc))
|
---|
606 | VBoxServiceVerbose(3, "VBoxStatsReportStatistics: new statistics reported successfully!\n");
|
---|
607 | else
|
---|
608 | VBoxServiceVerbose(3, "VBoxStatsReportStatistics: stats report failed with rc=%Rrc\n", rc);
|
---|
609 | }
|
---|
610 |
|
---|
611 | kstat_close(pStatKern);
|
---|
612 | }
|
---|
613 |
|
---|
614 | #else
|
---|
615 | /* todo: implement for other platforms. */
|
---|
616 |
|
---|
617 | #endif
|
---|
618 | }
|
---|
619 |
|
---|
620 | /** @copydoc VBOXSERVICE::pfnWorker */
|
---|
621 | DECLCALLBACK(int) VBoxServiceVMStatsWorker(bool volatile *pfShutdown)
|
---|
622 | {
|
---|
623 | int rc = VINF_SUCCESS;
|
---|
624 |
|
---|
625 | /* Start monitoring of the stat event change event. */
|
---|
626 | rc = VbglR3CtlFilterMask(VMMDEV_EVENT_STATISTICS_INTERVAL_CHANGE_REQUEST, 0);
|
---|
627 | if (RT_FAILURE(rc))
|
---|
628 | {
|
---|
629 | VBoxServiceVerbose(3, "VBoxServiceVMStatsWorker: VbglR3CtlFilterMask failed with %d\n", rc);
|
---|
630 | return rc;
|
---|
631 | }
|
---|
632 |
|
---|
633 | /*
|
---|
634 | * Tell the control thread that it can continue
|
---|
635 | * spawning services.
|
---|
636 | */
|
---|
637 | RTThreadUserSignal(RTThreadSelf());
|
---|
638 |
|
---|
639 | /*
|
---|
640 | * Now enter the loop retrieving runtime data continuously.
|
---|
641 | */
|
---|
642 | for (;;)
|
---|
643 | {
|
---|
644 | uint32_t fEvents = 0;
|
---|
645 | RTMSINTERVAL cWaitMillies;
|
---|
646 |
|
---|
647 | /* Check if an update interval change is pending. */
|
---|
648 | rc = VbglR3WaitEvent(VMMDEV_EVENT_STATISTICS_INTERVAL_CHANGE_REQUEST, 0 /* no wait */, &fEvents);
|
---|
649 | if ( RT_SUCCESS(rc)
|
---|
650 | && (fEvents & VMMDEV_EVENT_STATISTICS_INTERVAL_CHANGE_REQUEST))
|
---|
651 | {
|
---|
652 | VbglR3StatQueryInterval(&gCtx.cMsStatInterval);
|
---|
653 | }
|
---|
654 |
|
---|
655 | if (gCtx.cMsStatInterval)
|
---|
656 | {
|
---|
657 | VBoxServiceVMStatsReport();
|
---|
658 | cWaitMillies = gCtx.cMsStatInterval;
|
---|
659 | }
|
---|
660 | else
|
---|
661 | cWaitMillies = 3000;
|
---|
662 |
|
---|
663 | /*
|
---|
664 | * Block for a while.
|
---|
665 | *
|
---|
666 | * The event semaphore takes care of ignoring interruptions and it
|
---|
667 | * allows us to implement service wakeup later.
|
---|
668 | */
|
---|
669 | if (*pfShutdown)
|
---|
670 | break;
|
---|
671 | int rc2 = RTSemEventMultiWait(g_VMStatEvent, cWaitMillies);
|
---|
672 | if (*pfShutdown)
|
---|
673 | break;
|
---|
674 | if (rc2 != VERR_TIMEOUT && RT_FAILURE(rc2))
|
---|
675 | {
|
---|
676 | VBoxServiceError("VBoxServiceVMStatsWorker: RTSemEventMultiWait failed; rc2=%Rrc\n", rc2);
|
---|
677 | rc = rc2;
|
---|
678 | break;
|
---|
679 | }
|
---|
680 | }
|
---|
681 |
|
---|
682 | /* Cancel monitoring of the stat event change event. */
|
---|
683 | rc = VbglR3CtlFilterMask(0, VMMDEV_EVENT_STATISTICS_INTERVAL_CHANGE_REQUEST);
|
---|
684 | if (RT_FAILURE(rc))
|
---|
685 | VBoxServiceVerbose(3, "VBoxServiceVMStatsWorker: VbglR3CtlFilterMask failed with %d\n", rc);
|
---|
686 |
|
---|
687 | RTSemEventMultiDestroy(g_VMStatEvent);
|
---|
688 | g_VMStatEvent = NIL_RTSEMEVENTMULTI;
|
---|
689 |
|
---|
690 | VBoxServiceVerbose(3, "VBoxStatsThread: finished statistics change request thread\n");
|
---|
691 | return 0;
|
---|
692 | }
|
---|
693 |
|
---|
694 |
|
---|
695 | /** @copydoc VBOXSERVICE::pfnTerm */
|
---|
696 | static DECLCALLBACK(void) VBoxServiceVMStatsTerm(void)
|
---|
697 | {
|
---|
698 | VBoxServiceVerbose(3, "VBoxServiceVMStatsTerm\n");
|
---|
699 | return;
|
---|
700 | }
|
---|
701 |
|
---|
702 |
|
---|
703 | /** @copydoc VBOXSERVICE::pfnStop */
|
---|
704 | static DECLCALLBACK(void) VBoxServiceVMStatsStop(void)
|
---|
705 | {
|
---|
706 | RTSemEventMultiSignal(g_VMStatEvent);
|
---|
707 | }
|
---|
708 |
|
---|
709 |
|
---|
710 | /**
|
---|
711 | * The 'vminfo' service description.
|
---|
712 | */
|
---|
713 | VBOXSERVICE g_VMStatistics =
|
---|
714 | {
|
---|
715 | /* pszName. */
|
---|
716 | "vmstats",
|
---|
717 | /* pszDescription. */
|
---|
718 | "Virtual Machine Statistics",
|
---|
719 | /* pszUsage. */
|
---|
720 | NULL,
|
---|
721 | /* pszOptions. */
|
---|
722 | NULL,
|
---|
723 | /* methods */
|
---|
724 | VBoxServiceVMStatsPreInit,
|
---|
725 | VBoxServiceVMStatsOption,
|
---|
726 | VBoxServiceVMStatsInit,
|
---|
727 | VBoxServiceVMStatsWorker,
|
---|
728 | VBoxServiceVMStatsStop,
|
---|
729 | VBoxServiceVMStatsTerm
|
---|
730 | };
|
---|