VirtualBox

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

Last change on this file was 106061, checked in by vboxsync, 8 weeks ago

Copyright year updates by scm.

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