Changeset 48015 in vbox for trunk/src/VBox
- Timestamp:
- Aug 23, 2013 9:02:51 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Main/src-server/linux/PerformanceLinux.cpp
r48009 r48015 32 32 #include <iprt/system.h> 33 33 #include <iprt/mp.h> 34 #include <iprt/linux/sysfs.h> 34 35 35 36 #include <map> … … 62 63 virtual int _getRawHostCpuLoad(); 63 64 int getRawProcessStats(RTPROCESS process, uint64_t *cpuUser, uint64_t *cpuKernel, ULONG *memPagesUsed); 64 char *getDiskName(char *pszDiskName, size_t cbDiskName, const char *pszDevName, bool fTrimDigits);65 void getDiskName(char *pszDiskName, size_t cbDiskName, const char *pszDevName, bool fTrimDigits); 65 66 void addVolumeDependencies(const char *pcszVolume, DiskList& listDisks); 66 67 void addRaidDisks(const char *pcszDevice, DiskList& listDisks); … … 240 241 } 241 242 242 int CollectorLinux::getHostDiskSize(const char *name, uint64_t *size) 243 { 243 int CollectorLinux::getHostDiskSize(const char *pszFile, uint64_t *size) 244 { 245 char *pszPath = NULL; 246 247 RTStrAPrintf(&pszPath, "/sys/block/%s/size", pszFile); 248 Assert(pszPath); 249 244 250 int rc = VINF_SUCCESS; 245 char *pszName = NULL; 246 long long unsigned int u64Size; 247 248 RTStrAPrintf(&pszName, "/sys/block/%s/size", name); 249 Assert(pszName); 250 FILE *f = fopen(pszName, "r"); 251 RTMemFree(pszName); 252 253 if (f) 254 { 255 if (fscanf(f, "%llu", &u64Size) == 1) 256 *size = u64Size * 512; 251 if (!RTLinuxSysFsExists(pszPath)) 252 rc = VERR_FILE_NOT_FOUND; 253 else 254 { 255 int64_t cSize = RTLinuxSysFsReadIntFile(0, pszPath); 256 if (cSize < 0) 257 rc = VERR_ACCESS_DENIED; 257 258 else 258 rc = VERR_FILE_IO_ERROR; 259 fclose(f); 260 } 261 else 262 rc = VERR_ACCESS_DENIED; 263 259 *size = cSize * 512; 260 } 261 RTStrFree(pszPath); 264 262 return rc; 265 263 } … … 293 291 294 292 RTStrAPrintf(&pszName, "/proc/%d/stat", process); 295 //printf("Opening %s...\n", pszName);296 293 FILE *f = fopen(pszName, "r"); 297 RT MemFree(pszName);294 RTStrFree(pszName); 298 295 299 296 if (f) … … 320 317 } 321 318 322 int CollectorLinux::getRawHostNetworkLoad(const char * name, uint64_t *rx, uint64_t *tx)319 int CollectorLinux::getRawHostNetworkLoad(const char *pszFile, uint64_t *rx, uint64_t *tx) 323 320 { 324 321 int rc = VINF_SUCCESS; 325 322 char szIfName[/*IFNAMSIZ*/ 16 + 36]; 326 long long unsigned int u64Rx, u64Tx; 327 328 RTStrPrintf(szIfName, sizeof(szIfName), "/sys/class/net/%s/statistics/rx_bytes", name); 329 FILE *f = fopen(szIfName, "r"); 330 if (f) 331 { 332 if (fscanf(f, "%llu", &u64Rx) == 1) 333 *rx = u64Rx; 334 else 335 rc = VERR_FILE_IO_ERROR; 336 fclose(f); 337 RTStrPrintf(szIfName, sizeof(szIfName), "/sys/class/net/%s/statistics/tx_bytes", name); 338 f = fopen(szIfName, "r"); 339 if (f) 340 { 341 if (fscanf(f, "%llu", &u64Tx) == 1) 342 *tx = u64Tx; 343 else 344 rc = VERR_FILE_IO_ERROR; 345 fclose(f); 346 } 347 else 348 rc = VERR_ACCESS_DENIED; 349 } 350 else 351 rc = VERR_ACCESS_DENIED; 352 353 return rc; 323 324 RTStrPrintf(szIfName, sizeof(szIfName), "/sys/class/net/%s/statistics/rx_bytes", pszFile); 325 if (!RTLinuxSysFsExists(szIfName)) 326 return VERR_FILE_NOT_FOUND; 327 328 int64_t cSize = RTLinuxSysFsReadIntFile(0, szIfName); 329 if (cSize < 0) 330 return VERR_ACCESS_DENIED; 331 332 *rx = cSize; 333 334 RTStrPrintf(szIfName, sizeof(szIfName), "/sys/class/net/%s/statistics/tx_bytes", pszFile); 335 if (!RTLinuxSysFsExists(szIfName)) 336 return VERR_FILE_NOT_FOUND; 337 338 cSize = RTLinuxSysFsReadIntFile(0, szIfName); 339 if (cSize < 0) 340 return VERR_ACCESS_DENIED; 341 342 *tx = cSize; 343 return VINF_SUCCESS; 354 344 } 355 345 … … 450 440 } 451 441 452 char *CollectorLinux::getDiskName(char *pszDiskName, size_t cbDiskName, const char *pszDevName, bool fTrimDigits) 442 /** 443 * Use the partition name to get the name of the disk. Any path component is stripped. 444 * if fTrimDigits is true, trailing digits are stripped as well, for example '/dev/sda5' 445 * is converted to 'sda'. 446 * 447 * @param pszDiskName Where to store the name of the disk. 448 * @param cbDiskName The size of the buffer pszDiskName points to. 449 * @param pszDevName The device name used to get the disk name. 450 * @param fTrimDigits Trim trailing digits (e.g. /dev/sda5) 451 */ 452 void CollectorLinux::getDiskName(char *pszDiskName, size_t cbDiskName, const char *pszDevName, bool fTrimDigits) 453 453 { 454 454 unsigned cbName = 0; … … 464 464 } 465 465 RTStrCopy(pszDiskName, RT_MIN(cbName + 1, cbDiskName), pszEnd + 1); 466 return pszDiskName;467 466 } 468 467 … … 533 532 534 533 while (fgets(szBuf, sizeof(szBuf), fp)) 535 if (strncmp(szBuf, "dm-", 3))534 if (strncmp(szBuf, RT_STR_TUPLE("dm-"))) 536 535 listDisks.push_back(RTCString(trimTrailingDigits(szBuf))); 537 536 else … … 559 558 char szDevName[128]; 560 559 char szFsName[1024]; 561 /* Try to resolve symbolic link if necessary */ 562 ssize_t cbFsName = readlink(mntent->mnt_fsname, szFsName, sizeof(szFsName) - 1); 563 if (cbFsName != -1) 564 szFsName[cbFsName] = '\0'; 565 else 566 strcpy(szFsName, mntent->mnt_fsname); 567 if (!strncmp(szFsName, "/dev/mapper", 11)) 560 /* Try to resolve symbolic link if necessary. Yes, we access the file system here! */ 561 int rc = RTPathReal(mntent->mnt_fsname, szFsName, sizeof(szFsName)); 562 if (RT_FAILURE(rc)) 563 continue; /* something got wrong, just ignore this path */ 564 if (!strncmp(szFsName, RT_STR_TUPLE("/dev/mapper"))) 568 565 { 569 566 /* LVM */ 570 getDiskName(szDevName, sizeof(szDevName), szFsName, false );567 getDiskName(szDevName, sizeof(szDevName), szFsName, false /*=fTrimDigits*/); 571 568 addVolumeDependencies(szDevName, listUsage); 572 569 listLoad = listUsage; 573 570 } 574 else if (!strncmp(szFsName, "/dev/md", 7))571 else if (!strncmp(szFsName, RT_STR_TUPLE("/dev/md"))) 575 572 { 576 573 /* Software RAID */ 577 getDiskName(szDevName, sizeof(szDevName), szFsName, false );574 getDiskName(szDevName, sizeof(szDevName), szFsName, false /*=fTrimDigits*/); 578 575 listUsage.push_back(RTCString(szDevName)); 579 576 addRaidDisks(szDevName, listLoad); … … 581 578 else 582 579 { 583 /* Plain disk partition */584 getDiskName(szDevName, sizeof(szDevName), mntent->mnt_fsname, true);580 /* Plain disk partition. Trim the trailing digits to get the drive name */ 581 getDiskName(szDevName, sizeof(szDevName), szFsName, true /*=fTrimDigits*/); 585 582 listUsage.push_back(RTCString(szDevName)); 586 583 listLoad.push_back(RTCString(szDevName)); … … 588 585 if (listUsage.empty() || listLoad.empty()) 589 586 { 590 LogRel(("Failed to retrive disk info: getDiskName(%s) --> %s\n", mntent->mnt_fsname, szDevName)); 587 LogRel(("Failed to retrive disk info: getDiskName(%s) --> %s\n", 588 mntent->mnt_fsname, szDevName)); 591 589 } 592 590 break;
Note:
See TracChangeset
for help on using the changeset viewer.