Changeset 27938 in vbox
- Timestamp:
- Apr 1, 2010 1:25:50 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Additions/common/VBoxService/VBoxServiceStats.cpp
r27080 r27938 24 24 #endif 25 25 26 #if def RT_OS_WINDOWS26 #if defined(RT_OS_WINDOWS) 27 27 # include <windows.h> 28 28 # include <psapi.h> 29 29 # include <winternl.h> 30 #elif defined(RT_OS_LINUX) 31 # include <iprt/ctype.h> 32 # include <iprt/stream.h> 30 33 #endif 31 34 … … 148 151 static void VBoxServiceVMStatsReport() 149 152 { 150 #if def RT_OS_WINDOWS153 #if defined(RT_OS_WINDOWS) 151 154 SYSTEM_INFO systemInfo; 152 155 PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION pProcInfo; … … 238 241 } 239 242 240 for (uint32_t i=0; i<systemInfo.dwNumberOfProcessors;i++)243 for (uint32_t i=0; i<systemInfo.dwNumberOfProcessors; i++) 241 244 { 242 245 req.guestStats.u32CpuId = i; … … 244 247 rc = VbglR3StatReport(&req); 245 248 if (RT_SUCCESS(rc)) 246 {247 249 VBoxServiceVerbose(3, "VBoxStatsReportStatistics: new statistics reported successfully!\n"); 248 }249 250 else 250 251 VBoxServiceVerbose(3, "VBoxStatsReportStatistics: DeviceIoControl (stats report) failed with %d\n", GetLastError()); … … 252 253 253 254 free(pProcInfo); 255 256 #elif defined(RT_OS_LINUX) 257 VMMDevReportGuestStats req; 258 RT_ZERO(req); 259 PRTSTREAM pStrm; 260 char szLine[256]; 261 char *psz; 262 263 int rc = RTStrmOpen("/proc/meminfo", "r", &pStrm); 264 if (RT_SUCCESS(rc)) 265 { 266 uint64_t u64Kb; 267 uint64_t u64Total = 0, u64Free = 0, u64Buffers = 0, u64Cached = 0, u64PagedTotal = 0; 268 for (;;) 269 { 270 rc = RTStrmGetLine(pStrm, szLine, sizeof(szLine)); 271 if (RT_FAILURE(rc)) 272 break; 273 if (strstr("MemTotal:", szLine) == szLine) 274 { 275 rc = RTStrToUInt64Ex(RTStrStripL(&szLine[9]), &psz, 0, &u64Kb); 276 if (RT_SUCCESS(rc)) 277 u64Total = u64Kb * _1K; 278 } 279 else if (strstr("MemFree:", szLine) == szLine) 280 { 281 rc = RTStrToUInt64Ex(RTStrStripL(&szLine[8]), &psz, 0, &u64Kb); 282 if (RT_SUCCESS(rc)) 283 u64Free = u64Kb * _1K; 284 } 285 else if (strstr("Buffers:", szLine) == szLine) 286 { 287 rc = RTStrToUInt64Ex(RTStrStripL(&szLine[8]), &psz, 0, &u64Kb); 288 if (RT_SUCCESS(rc)) 289 u64Buffers = u64Kb * _1K; 290 } 291 else if (strstr("Cached:", szLine) == szLine) 292 { 293 rc = RTStrToUInt64Ex(RTStrStripL(&szLine[7]), &psz, 0, &u64Kb); 294 if (RT_SUCCESS(rc)) 295 u64Cached = u64Kb * _1K; 296 } 297 else if (strstr("SwapTotal:", szLine) == szLine) 298 { 299 rc = RTStrToUInt64Ex(RTStrStripL(&szLine[10]), &psz, 0, &u64Kb); 300 if (RT_SUCCESS(rc)) 301 u64PagedTotal = u64Kb * _1K; 302 } 303 } 304 req.guestStats.u32PhysMemTotal = u64Total / _4K; 305 req.guestStats.u32PhysMemAvail = (u64Free + u64Buffers + u64Cached) / _4K; 306 req.guestStats.u32MemSystemCache = (u64Buffers + u64Cached) / _4K; 307 req.guestStats.u32PageFileSize = u64PagedTotal / _4K; 308 RTStrmClose(pStrm); 309 } 310 311 req.guestStats.u32PhysMemBalloon = VBoxServiceBalloonQueryChunks() * (_1M/_4K); 312 req.guestStats.u32StatCaps = VBOX_GUEST_STAT_PHYS_MEM_TOTAL \ 313 | VBOX_GUEST_STAT_PHYS_MEM_AVAIL \ 314 | VBOX_GUEST_STAT_PHYS_MEM_BALLOON \ 315 | VBOX_GUEST_STAT_PAGE_FILE_SIZE; 316 317 rc = RTStrmOpen("/proc/stat", "r", &pStrm); 318 if (RT_SUCCESS(rc)) 319 { 320 for (;;) 321 { 322 uint64_t u64CpuId, u64User = 0, u64Nice = 0, u64System = 0, u64Idle = 0; 323 rc = RTStrmGetLine(pStrm, szLine, sizeof(szLine)); 324 if (RT_FAILURE(rc)) 325 break; 326 if ( strstr("cpu", szLine) == szLine 327 && strlen(szLine) > 3 328 && RT_C_IS_DIGIT(szLine[3])) 329 { 330 rc = RTStrToUInt64Ex(&szLine[3], &psz, 0, &u64CpuId); 331 if (RT_SUCCESS(rc)) 332 rc = RTStrToUInt64Ex(RTStrStripL(psz), &psz, 0, &u64User); 333 if (RT_SUCCESS(rc)) 334 rc = RTStrToUInt64Ex(RTStrStripL(psz), &psz, 0, &u64Nice); 335 if (RT_SUCCESS(rc)) 336 rc = RTStrToUInt64Ex(RTStrStripL(psz), &psz, 0, &u64System); 337 if (RT_SUCCESS(rc)) 338 rc = RTStrToUInt64Ex(RTStrStripL(psz), &psz, 0, &u64Idle); 339 uint64_t u64All = u64Idle + u64System + u64User + u64Nice; 340 req.guestStats.u32CpuId = u64CpuId; 341 req.guestStats.u32CpuLoad_Idle = (uint32_t)(u64Idle * 100 / u64All); 342 req.guestStats.u32CpuLoad_Kernel = (uint32_t)(u64System * 100 / u64All); 343 req.guestStats.u32CpuLoad_User = (uint32_t)((u64User + u64Nice) * 100 / u64All); 344 req.guestStats.u32StatCaps |= VBOX_GUEST_STAT_CPU_LOAD_IDLE \ 345 | VBOX_GUEST_STAT_CPU_LOAD_KERNEL \ 346 | VBOX_GUEST_STAT_CPU_LOAD_USER; 347 rc = VbglR3StatReport(&req); 348 if (RT_SUCCESS(rc)) 349 VBoxServiceVerbose(3, "VBoxStatsReportStatistics: new statistics reported successfully!\n"); 350 else 351 VBoxServiceVerbose(3, "VBoxStatsReportStatistics: stats report failed with rc=%Rrc\n", rc); 352 } 353 } 354 RTStrmClose(pStrm); 355 } 356 254 357 #else 255 358 /* todo: implement for other platforms. */ 256 return; 359 257 360 #endif 258 361 }
Note:
See TracChangeset
for help on using the changeset viewer.

