VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxService/VBoxServiceStats.cpp@ 32503

Last change on this file since 32503 was 29345, checked in by vboxsync, 15 years ago

VBoxService,VBox/err.h: Use a dedicated status code for this, VERR_NOT_IMPLEMENTED and VERR_NOT_SUPPORTED are not suitable. Disable ballooning if either VbglR3MemBalloonRefresh or VBoxServiceBalloonSetUser fails instead of just checking the first. Don't display an error message on non-Windows hosts when ballooning is unavailbe (status code mixup).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 27.7 KB
Line 
1/* $Id: VBoxServiceStats.cpp 29345 2010-05-11 12:22:48Z vboxsync $ */
2/** @file
3 * VBoxStats - Guest statistics notification
4 */
5
6/*
7 * Copyright (C) 2006-2010 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*******************************************************************************/
60typedef 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*******************************************************************************/
80static VBOXSTATSCONTEXT gCtx = {0};
81
82/** The semaphore we're blocking on. */
83static RTSEMEVENTMULTI g_VMStatEvent = NIL_RTSEMEVENTMULTI;
84
85
86/** @copydoc VBOXSERVICE::pfnPreInit */
87static DECLCALLBACK(int) VBoxServiceVMStatsPreInit(void)
88{
89 return VINF_SUCCESS;
90}
91
92
93/** @copydoc VBOXSERVICE::pfnOption */
94static 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 return VINF_SUCCESS;
101}
102
103
104/** @copydoc VBOXSERVICE::pfnInit */
105static DECLCALLBACK(int) VBoxServiceVMStatsInit(void)
106{
107 VBoxServiceVerbose(3, "VBoxServiceVMStatsInit\n");
108
109 int rc = RTSemEventMultiCreate(&g_VMStatEvent);
110 AssertRCReturn(rc, rc);
111
112 gCtx.cMsStatInterval = 0; /* default; update disabled */
113 RT_ZERO(gCtx.au64LastCpuLoad_Idle);
114 RT_ZERO(gCtx.au64LastCpuLoad_Kernel);
115 RT_ZERO(gCtx.au64LastCpuLoad_User);
116 RT_ZERO(gCtx.au64LastCpuLoad_Nice);
117
118 rc = VbglR3StatQueryInterval(&gCtx.cMsStatInterval);
119 if (RT_SUCCESS(rc))
120 VBoxServiceVerbose(3, "VBoxStatsInit: New statistics interval %u seconds\n", gCtx.cMsStatInterval);
121 else
122 VBoxServiceVerbose(3, "VBoxStatsInit: DeviceIoControl failed with %d\n", rc);
123
124#ifdef RT_OS_WINDOWS
125 /** @todo Use RTLdr instead of LoadLibrary/GetProcAddress here! */
126
127 /* NtQuerySystemInformation might be dropped in future releases, so load it dynamically as per Microsoft's recommendation */
128 HMODULE hMod = LoadLibrary("NTDLL.DLL");
129 if (hMod)
130 {
131 *(uintptr_t *)&gCtx.pfnNtQuerySystemInformation = (uintptr_t)GetProcAddress(hMod, "NtQuerySystemInformation");
132 if (gCtx.pfnNtQuerySystemInformation)
133 VBoxServiceVerbose(3, "VBoxStatsInit: gCtx.pfnNtQuerySystemInformation = %x\n", gCtx.pfnNtQuerySystemInformation);
134 else
135 {
136 VBoxServiceVerbose(3, "VBoxStatsInit: NTDLL.NtQuerySystemInformation not found!\n");
137 return VERR_SERVICE_DISABLED;
138 }
139 }
140
141 /* GlobalMemoryStatus is win2k and up, so load it dynamically */
142 hMod = LoadLibrary("KERNEL32.DLL");
143 if (hMod)
144 {
145 *(uintptr_t *)&gCtx.pfnGlobalMemoryStatusEx = (uintptr_t)GetProcAddress(hMod, "GlobalMemoryStatusEx");
146 if (gCtx.pfnGlobalMemoryStatusEx)
147 VBoxServiceVerbose(3, "VBoxStatsInit: gCtx.GlobalMemoryStatusEx = %x\n", gCtx.pfnGlobalMemoryStatusEx);
148 else
149 {
150 /** @todo Now fails in NT4; do we care? */
151 VBoxServiceVerbose(3, "VBoxStatsInit: KERNEL32.GlobalMemoryStatusEx not found!\n");
152 return VERR_SERVICE_DISABLED;
153 }
154 }
155 /* GetPerformanceInfo is xp and up, so load it dynamically */
156 hMod = LoadLibrary("PSAPI.DLL");
157 if (hMod)
158 {
159 *(uintptr_t *)&gCtx.pfnGetPerformanceInfo = (uintptr_t)GetProcAddress(hMod, "GetPerformanceInfo");
160 if (gCtx.pfnGetPerformanceInfo)
161 VBoxServiceVerbose(3, "VBoxStatsInit: gCtx.pfnGetPerformanceInfo= %x\n", gCtx.pfnGetPerformanceInfo);
162 /* failure is not fatal */
163 }
164#endif /* RT_OS_WINDOWS */
165
166 return VINF_SUCCESS;
167}
168
169
170/**
171 * Gathers VM statistics and reports them to the host.
172 *
173 * @returns
174 * @param oid .
175 */
176static void VBoxServiceVMStatsReport(void)
177{
178#if defined(RT_OS_WINDOWS)
179 SYSTEM_INFO systemInfo;
180 PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION pProcInfo;
181 MEMORYSTATUSEX memStatus;
182 uint32_t cbStruct;
183 DWORD cbReturned;
184
185 Assert(gCtx.pfnGlobalMemoryStatusEx && gCtx.pfnNtQuerySystemInformation);
186 if ( !gCtx.pfnGlobalMemoryStatusEx
187 || !gCtx.pfnNtQuerySystemInformation)
188 return;
189
190 /* Clear the report so we don't report garbage should NtQuerySystemInformation
191 behave in an unexpected manner. */
192 VMMDevReportGuestStats req;
193 RT_ZERO(req);
194
195 /* Query and report guest statistics */
196 GetSystemInfo(&systemInfo);
197
198 memStatus.dwLength = sizeof(memStatus);
199 gCtx.pfnGlobalMemoryStatusEx(&memStatus);
200
201 req.guestStats.u32PageSize = systemInfo.dwPageSize;
202 req.guestStats.u32PhysMemTotal = (uint32_t)(memStatus.ullTotalPhys / _4K);
203 req.guestStats.u32PhysMemAvail = (uint32_t)(memStatus.ullAvailPhys / _4K);
204 /* The current size of the committed memory limit, in bytes. This is physical
205 memory plus the size of the page file, minus a small overhead. */
206 req.guestStats.u32PageFileSize = (uint32_t)(memStatus.ullTotalPageFile / _4K) - req.guestStats.u32PhysMemTotal;
207 req.guestStats.u32MemoryLoad = memStatus.dwMemoryLoad;
208 req.guestStats.u32StatCaps = VBOX_GUEST_STAT_PHYS_MEM_TOTAL
209 | VBOX_GUEST_STAT_PHYS_MEM_AVAIL
210 | VBOX_GUEST_STAT_PAGE_FILE_SIZE
211 | VBOX_GUEST_STAT_MEMORY_LOAD;
212#ifdef VBOX_WITH_MEMBALLOON
213 req.guestStats.u32PhysMemBalloon = VBoxServiceBalloonQueryPages(_4K);
214 req.guestStats.u32StatCaps |= VBOX_GUEST_STAT_PHYS_MEM_BALLOON;
215#else
216 req.guestStats.u32PhysMemBalloon = 0;
217#endif
218
219 if (gCtx.pfnGetPerformanceInfo)
220 {
221 PERFORMANCE_INFORMATION perfInfo;
222
223 if (gCtx.pfnGetPerformanceInfo(&perfInfo, sizeof(perfInfo)))
224 {
225 req.guestStats.u32Processes = perfInfo.ProcessCount;
226 req.guestStats.u32Threads = perfInfo.ThreadCount;
227 req.guestStats.u32Handles = perfInfo.HandleCount;
228 req.guestStats.u32MemCommitTotal = perfInfo.CommitTotal; /* already in pages */
229 req.guestStats.u32MemKernelTotal = perfInfo.KernelTotal; /* already in pages */
230 req.guestStats.u32MemKernelPaged = perfInfo.KernelPaged; /* already in pages */
231 req.guestStats.u32MemKernelNonPaged = perfInfo.KernelNonpaged; /* already in pages */
232 req.guestStats.u32MemSystemCache = perfInfo.SystemCache; /* already in pages */
233 req.guestStats.u32StatCaps |= VBOX_GUEST_STAT_PROCESSES | VBOX_GUEST_STAT_THREADS | VBOX_GUEST_STAT_HANDLES
234 | VBOX_GUEST_STAT_MEM_COMMIT_TOTAL | VBOX_GUEST_STAT_MEM_KERNEL_TOTAL
235 | VBOX_GUEST_STAT_MEM_KERNEL_PAGED | VBOX_GUEST_STAT_MEM_KERNEL_NONPAGED
236 | VBOX_GUEST_STAT_MEM_SYSTEM_CACHE;
237 }
238 else
239 VBoxServiceVerbose(3, "GetPerformanceInfo failed with %d\n", GetLastError());
240 }
241
242 /* Query CPU load information */
243 cbStruct = systemInfo.dwNumberOfProcessors * sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION);
244 pProcInfo = (PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION)RTMemAlloc(cbStruct);
245 if (!pProcInfo)
246 return;
247
248 /* Unfortunately GetSystemTimes is XP SP1 and up only, so we need to use the semi-undocumented NtQuerySystemInformation */
249 NTSTATUS rc = gCtx.pfnNtQuerySystemInformation(SystemProcessorPerformanceInformation, pProcInfo, cbStruct, &cbReturned);
250 if ( !rc
251 && cbReturned == cbStruct)
252 {
253 if (gCtx.au64LastCpuLoad_Kernel == 0)
254 {
255 /* first time */
256 gCtx.au64LastCpuLoad_Idle[0] = pProcInfo->IdleTime.QuadPart;
257 gCtx.au64LastCpuLoad_Kernel[0] = pProcInfo->KernelTime.QuadPart;
258 gCtx.au64LastCpuLoad_User[0] = pProcInfo->UserTime.QuadPart;
259
260 Sleep(250);
261
262 rc = gCtx.pfnNtQuerySystemInformation(SystemProcessorPerformanceInformation, pProcInfo, cbStruct, &cbReturned);
263 Assert(!rc);
264 }
265
266 uint64_t deltaIdle = (pProcInfo->IdleTime.QuadPart - gCtx.au64LastCpuLoad_Idle[0]);
267 uint64_t deltaKernel = (pProcInfo->KernelTime.QuadPart - gCtx.au64LastCpuLoad_Kernel[0]);
268 uint64_t deltaUser = (pProcInfo->UserTime.QuadPart - gCtx.au64LastCpuLoad_User[0]);
269 deltaKernel -= deltaIdle; /* idle time is added to kernel time */
270 uint64_t ullTotalTime = deltaIdle + deltaKernel + deltaUser;
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_PAGE_FILE_SIZE;
358#ifdef VBOX_WITH_MEMBALLOON
359 req.guestStats.u32PhysMemBalloon = VBoxServiceBalloonQueryPages(_4K);
360 req.guestStats.u32StatCaps |= VBOX_GUEST_STAT_PHYS_MEM_BALLOON;
361#else
362 req.guestStats.u32PhysMemBalloon = 0;
363#endif
364
365
366 /** @todo req.guestStats.u32Threads */
367 /** @todo req.guestStats.u32Processes */
368 /* req.guestStats.u32Handles doesn't make sense here. */
369 /** @todo req.guestStats.u32MemoryLoad */
370 /** @todo req.guestStats.u32MemCommitTotal */
371 /** @todo req.guestStats.u32MemKernelTotal */
372 /** @todo req.guestStats.u32MemKernelPaged, make any sense? = u32MemKernelTotal? */
373 /** @todo req.guestStats.u32MemKernelNonPaged, make any sense? = 0? */
374
375 bool fCpuInfoAvail = false;
376 rc = RTStrmOpen("/proc/stat", "r", &pStrm);
377 if (RT_SUCCESS(rc))
378 {
379 for (;;)
380 {
381 rc = RTStrmGetLine(pStrm, szLine, sizeof(szLine));
382 if (RT_FAILURE(rc))
383 break;
384 if ( strstr(szLine, "cpu") == szLine
385 && strlen(szLine) > 3
386 && RT_C_IS_DIGIT(szLine[3]))
387 {
388 uint32_t u32CpuId;
389 rc = RTStrToUInt32Ex(&szLine[3], &psz, 0, &u32CpuId);
390 if (u32CpuId < VMM_MAX_CPU_COUNT)
391 {
392 uint64_t u64User = 0;
393 if (RT_SUCCESS(rc))
394 rc = RTStrToUInt64Ex(RTStrStripL(psz), &psz, 0, &u64User);
395
396 uint64_t u64Nice = 0;
397 if (RT_SUCCESS(rc))
398 rc = RTStrToUInt64Ex(RTStrStripL(psz), &psz, 0, &u64Nice);
399
400 uint64_t u64System = 0;
401 if (RT_SUCCESS(rc))
402 rc = RTStrToUInt64Ex(RTStrStripL(psz), &psz, 0, &u64System);
403
404 uint64_t u64Idle = 0;
405 if (RT_SUCCESS(rc))
406 rc = RTStrToUInt64Ex(RTStrStripL(psz), &psz, 0, &u64Idle);
407
408 uint64_t u64DeltaIdle = u64Idle - gCtx.au64LastCpuLoad_Idle[u32CpuId];
409 uint64_t u64DeltaSystem = u64System - gCtx.au64LastCpuLoad_Kernel[u32CpuId];
410 uint64_t u64DeltaUser = u64User - gCtx.au64LastCpuLoad_User[u32CpuId];
411 uint64_t u64DeltaNice = u64Nice - gCtx.au64LastCpuLoad_Nice[u32CpuId];
412
413 uint64_t u64DeltaAll = u64DeltaIdle
414 + u64DeltaSystem
415 + u64DeltaUser
416 + u64DeltaNice;
417
418 gCtx.au64LastCpuLoad_Idle[u32CpuId] = u64Idle;
419 gCtx.au64LastCpuLoad_Kernel[u32CpuId] = u64System;
420 gCtx.au64LastCpuLoad_User[u32CpuId] = u64User;
421 gCtx.au64LastCpuLoad_Nice[u32CpuId] = u64Nice;
422
423 req.guestStats.u32CpuId = u32CpuId;
424 req.guestStats.u32CpuLoad_Idle = (uint32_t)(u64DeltaIdle * 100 / u64DeltaAll);
425 req.guestStats.u32CpuLoad_Kernel = (uint32_t)(u64DeltaSystem * 100 / u64DeltaAll);
426 req.guestStats.u32CpuLoad_User = (uint32_t)((u64DeltaUser
427 + u64DeltaNice) * 100 / u64DeltaAll);
428 req.guestStats.u32StatCaps |= VBOX_GUEST_STAT_CPU_LOAD_IDLE
429 | VBOX_GUEST_STAT_CPU_LOAD_KERNEL
430 | VBOX_GUEST_STAT_CPU_LOAD_USER;
431 fCpuInfoAvail = true;
432 rc = VbglR3StatReport(&req);
433 if (RT_SUCCESS(rc))
434 VBoxServiceVerbose(3, "VBoxStatsReportStatistics: new statistics (CPU %u) reported successfully!\n", u32CpuId);
435 else
436 VBoxServiceVerbose(3, "VBoxStatsReportStatistics: stats report failed with rc=%Rrc\n", rc);
437 }
438 else
439 VBoxServiceVerbose(3, "VBoxStatsReportStatistics: skipping information for CPU%u\n", u32CpuId);
440 }
441 }
442 RTStrmClose(pStrm);
443 }
444 if (!fCpuInfoAvail)
445 {
446 VBoxServiceVerbose(3, "VBoxStatsReportStatistics: CPU info not available!\n");
447 rc = VbglR3StatReport(&req);
448 if (RT_SUCCESS(rc))
449 VBoxServiceVerbose(3, "VBoxStatsReportStatistics: new statistics reported successfully!\n");
450 else
451 VBoxServiceVerbose(3, "VBoxStatsReportStatistics: stats report failed with rc=%Rrc\n", rc);
452 }
453
454#elif defined(RT_OS_SOLARIS)
455 VMMDevReportGuestStats req;
456 RT_ZERO(req);
457 kstat_ctl_t *pStatKern = kstat_open();
458 if (pStatKern)
459 {
460 /*
461 * Memory statistics.
462 */
463 uint64_t u64Total = 0, u64Free = 0, u64Buffers = 0, u64Cached = 0, u64PagedTotal = 0;
464 int rc = -1;
465 kstat_t *pStatPages = kstat_lookup(pStatKern, "unix", 0 /* instance */, "system_pages");
466 if (pStatPages)
467 {
468 rc = kstat_read(pStatKern, pStatPages, NULL /* optional-copy-buf */);
469 if (rc != -1)
470 {
471 kstat_named_t *pStat = NULL;
472 pStat = (kstat_named_t *)kstat_data_lookup(pStatPages, "pagestotal");
473 if (pStat)
474 u64Total = pStat->value.ul;
475
476 pStat = (kstat_named_t *)kstat_data_lookup(pStatPages, "freemem");
477 if (pStat)
478 u64Free = pStat->value.ul;
479 }
480 }
481
482 kstat_t *pStatZFS = kstat_lookup(pStatKern, "zfs", 0 /* instance */, "arcstats");
483 if (pStatZFS)
484 {
485 rc = kstat_read(pStatKern, pStatZFS, NULL /* optional-copy-buf */);
486 if (rc != -1)
487 {
488 kstat_named_t *pStat = (kstat_named_t *)kstat_data_lookup(pStatZFS, "size");
489 if (pStat)
490 u64Cached = pStat->value.ul;
491 }
492 }
493
494 /*
495 * The vminfo are accumulative counters updated every "N" ticks. Let's get the
496 * number of stat updates so far and use that to divide the swap counter.
497 */
498 kstat_t *pStatInfo = kstat_lookup(pStatKern, "unix", 0 /* instance */, "sysinfo");
499 if (pStatInfo)
500 {
501 sysinfo_t SysInfo;
502 rc = kstat_read(pStatKern, pStatInfo, &SysInfo);
503 if (rc != -1)
504 {
505 kstat_t *pStatVMInfo = kstat_lookup(pStatKern, "unix", 0 /* instance */, "vminfo");
506 if (pStatVMInfo)
507 {
508 vminfo_t VMInfo;
509 rc = kstat_read(pStatKern, pStatVMInfo, &VMInfo);
510 if (rc != -1)
511 {
512 u64PagedTotal = VMInfo.swap_avail / SysInfo.updates;
513 }
514 }
515 }
516 }
517
518 req.guestStats.u32PhysMemTotal = u64Total; /* already in pages */
519 req.guestStats.u32PhysMemAvail = u64Free; /* already in pages */
520 req.guestStats.u32MemSystemCache = u64Cached / _4K;
521 req.guestStats.u32PageFileSize = u64PagedTotal; /* already in pages */
522 /** @todo req.guestStats.u32Threads */
523 /** @todo req.guestStats.u32Processes */
524 /** @todo req.guestStats.u32Handles -- ??? */
525 /** @todo req.guestStats.u32MemoryLoad */
526 /** @todo req.guestStats.u32MemCommitTotal */
527 /** @todo req.guestStats.u32MemKernelTotal */
528 /** @todo req.guestStats.u32MemKernelPaged */
529 /** @todo req.guestStats.u32MemKernelNonPaged */
530 req.guestStats.u32PageSize = getpagesize();
531
532 req.guestStats.u32StatCaps = VBOX_GUEST_STAT_PHYS_MEM_TOTAL
533 | VBOX_GUEST_STAT_PHYS_MEM_AVAIL
534 | VBOX_GUEST_STAT_PAGE_FILE_SIZE;
535#ifdef VBOX_WITH_MEMBALLOON
536 req.guestStats.u32PhysMemBalloon = VBoxServiceBalloonQueryPages(_4K);
537 req.guestStats.u32StatCaps |= VBOX_GUEST_STAT_PHYS_MEM_BALLOON;
538#else
539 req.guestStats.u32PhysMemBalloon = 0;
540#endif
541
542 /*
543 * CPU statistics.
544 */
545 cpu_stat_t StatCPU;
546 RT_ZERO(StatCPU);
547 kstat_t *pStatNode = NULL;
548 uint32_t cCPUs = 0;
549 bool fCpuInfoAvail = false;
550 for (pStatNode = pStatKern->kc_chain; pStatNode != NULL; pStatNode = pStatNode->ks_next)
551 {
552 if (!strcmp(pStatNode->ks_module, "cpu_stat"))
553 {
554 rc = kstat_read(pStatKern, pStatNode, &StatCPU);
555 if (rc == -1)
556 break;
557
558 uint64_t u64Idle = StatCPU.cpu_sysinfo.cpu[CPU_IDLE];
559 uint64_t u64User = StatCPU.cpu_sysinfo.cpu[CPU_USER];
560 uint64_t u64System = StatCPU.cpu_sysinfo.cpu[CPU_KERNEL];
561
562 uint64_t u64DeltaIdle = u64Idle - gCtx.au64LastCpuLoad_Idle[cCPUs];
563 uint64_t u64DeltaSystem = u64System - gCtx.au64LastCpuLoad_Kernel[cCPUs];
564 uint64_t u64DeltaUser = u64User - gCtx.au64LastCpuLoad_User[cCPUs];
565
566 uint64_t u64DeltaAll = u64DeltaIdle + u64DeltaSystem + u64DeltaUser;
567
568 gCtx.au64LastCpuLoad_Idle[cCPUs] = u64Idle;
569 gCtx.au64LastCpuLoad_Kernel[cCPUs] = u64System;
570 gCtx.au64LastCpuLoad_User[cCPUs] = u64User;
571
572 req.guestStats.u32CpuId = cCPUs;
573 req.guestStats.u32CpuLoad_Idle = (uint32_t)(u64DeltaIdle * 100 / u64DeltaAll);
574 req.guestStats.u32CpuLoad_Kernel = (uint32_t)(u64DeltaSystem * 100 / u64DeltaAll);
575 req.guestStats.u32CpuLoad_User = (uint32_t)(u64DeltaUser * 100 / u64DeltaAll);
576
577 req.guestStats.u32StatCaps |= VBOX_GUEST_STAT_CPU_LOAD_IDLE
578 | VBOX_GUEST_STAT_CPU_LOAD_KERNEL
579 | VBOX_GUEST_STAT_CPU_LOAD_USER;
580 fCpuInfoAvail = true;
581
582 rc = VbglR3StatReport(&req);
583 if (RT_SUCCESS(rc))
584 VBoxServiceVerbose(3, "VBoxStatsReportStatistics: new statistics (CPU %u) reported successfully!\n", cCPUs);
585 else
586 VBoxServiceVerbose(3, "VBoxStatsReportStatistics: stats report failed with rc=%Rrc\n", rc);
587 cCPUs++;
588 }
589 }
590
591 /*
592 * Report whatever statistics were collected.
593 */
594 if (!fCpuInfoAvail)
595 {
596 VBoxServiceVerbose(3, "VBoxStatsReportStatistics: CPU info not available!\n");
597 rc = VbglR3StatReport(&req);
598 if (RT_SUCCESS(rc))
599 VBoxServiceVerbose(3, "VBoxStatsReportStatistics: new statistics reported successfully!\n");
600 else
601 VBoxServiceVerbose(3, "VBoxStatsReportStatistics: stats report failed with rc=%Rrc\n", rc);
602 }
603
604 kstat_close(pStatKern);
605 }
606
607#else
608 /* todo: implement for other platforms. */
609
610#endif
611}
612
613/** @copydoc VBOXSERVICE::pfnWorker */
614DECLCALLBACK(int) VBoxServiceVMStatsWorker(bool volatile *pfShutdown)
615{
616 int rc = VINF_SUCCESS;
617
618 /* Start monitoring of the stat event change event. */
619 rc = VbglR3CtlFilterMask(VMMDEV_EVENT_STATISTICS_INTERVAL_CHANGE_REQUEST, 0);
620 if (RT_FAILURE(rc))
621 {
622 VBoxServiceVerbose(3, "VBoxServiceVMStatsWorker: VbglR3CtlFilterMask failed with %d\n", rc);
623 return rc;
624 }
625
626 /*
627 * Tell the control thread that it can continue
628 * spawning services.
629 */
630 RTThreadUserSignal(RTThreadSelf());
631
632 /*
633 * Now enter the loop retrieving runtime data continuously.
634 */
635 for (;;)
636 {
637 uint32_t fEvents = 0;
638 RTMSINTERVAL cWaitMillies;
639
640 /* Check if an update interval change is pending. */
641 rc = VbglR3WaitEvent(VMMDEV_EVENT_STATISTICS_INTERVAL_CHANGE_REQUEST, 0 /* no wait */, &fEvents);
642 if ( RT_SUCCESS(rc)
643 && (fEvents & VMMDEV_EVENT_STATISTICS_INTERVAL_CHANGE_REQUEST))
644 {
645 VbglR3StatQueryInterval(&gCtx.cMsStatInterval);
646 }
647
648 if (gCtx.cMsStatInterval)
649 {
650 VBoxServiceVMStatsReport();
651 cWaitMillies = gCtx.cMsStatInterval;
652 }
653 else
654 cWaitMillies = 3000;
655
656 /*
657 * Block for a while.
658 *
659 * The event semaphore takes care of ignoring interruptions and it
660 * allows us to implement service wakeup later.
661 */
662 if (*pfShutdown)
663 break;
664 int rc2 = RTSemEventMultiWait(g_VMStatEvent, cWaitMillies);
665 if (*pfShutdown)
666 break;
667 if (rc2 != VERR_TIMEOUT && RT_FAILURE(rc2))
668 {
669 VBoxServiceError("RTSemEventMultiWait failed; rc2=%Rrc\n", rc2);
670 rc = rc2;
671 break;
672 }
673 }
674
675 /* Cancel monitoring of the stat event change event. */
676 rc = VbglR3CtlFilterMask(0, VMMDEV_EVENT_STATISTICS_INTERVAL_CHANGE_REQUEST);
677 if (RT_FAILURE(rc))
678 VBoxServiceVerbose(3, "VBoxServiceVMStatsWorker: VbglR3CtlFilterMask failed with %d\n", rc);
679
680 RTSemEventMultiDestroy(g_VMStatEvent);
681 g_VMStatEvent = NIL_RTSEMEVENTMULTI;
682
683 VBoxServiceVerbose(3, "VBoxStatsThread: finished statistics change request thread\n");
684 return 0;
685}
686
687
688/** @copydoc VBOXSERVICE::pfnTerm */
689static DECLCALLBACK(void) VBoxServiceVMStatsTerm(void)
690{
691 VBoxServiceVerbose(3, "VBoxServiceVMStatsTerm\n");
692 return;
693}
694
695
696/** @copydoc VBOXSERVICE::pfnStop */
697static DECLCALLBACK(void) VBoxServiceVMStatsStop(void)
698{
699 RTSemEventMultiSignal(g_VMStatEvent);
700}
701
702
703/**
704 * The 'vminfo' service description.
705 */
706VBOXSERVICE g_VMStatistics =
707{
708 /* pszName. */
709 "vmstats",
710 /* pszDescription. */
711 "Virtual Machine Statistics",
712 /* pszUsage. */
713 NULL,
714 /* pszOptions. */
715 NULL,
716 /* methods */
717 VBoxServiceVMStatsPreInit,
718 VBoxServiceVMStatsOption,
719 VBoxServiceVMStatsInit,
720 VBoxServiceVMStatsWorker,
721 VBoxServiceVMStatsStop,
722 VBoxServiceVMStatsTerm
723};
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