VirtualBox

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

Last change on this file since 51570 was 51570, checked in by vboxsync, 10 years ago

Additions/common/VBoxService: provide default/null object implementations for service call-back functions.

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