VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/linux/PerformanceLinux.cpp@ 48009

Last change on this file since 48009 was 48009, checked in by vboxsync, 11 years ago

1024 * 1024 => _1M

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.5 KB
Line 
1/* $Id: PerformanceLinux.cpp 48009 2013-08-23 07:38:52Z vboxsync $ */
2
3/** @file
4 *
5 * VBox Linux-specific Performance Classes implementation.
6 */
7
8/*
9 * Copyright (C) 2008-2012 Oracle Corporation
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 */
19
20#include <stdio.h>
21#include <unistd.h>
22#include <sys/statvfs.h>
23#include <errno.h>
24#include <mntent.h>
25#include <iprt/alloc.h>
26#include <iprt/cdefs.h>
27#include <iprt/ctype.h>
28#include <iprt/err.h>
29#include <iprt/param.h>
30#include <iprt/path.h>
31#include <iprt/string.h>
32#include <iprt/system.h>
33#include <iprt/mp.h>
34
35#include <map>
36#include <vector>
37
38#include "Logging.h"
39#include "Performance.h"
40
41#define VBOXVOLINFO_NAME "VBoxVolInfo"
42
43namespace pm {
44
45class CollectorLinux : public CollectorHAL
46{
47public:
48 CollectorLinux();
49 virtual int preCollect(const CollectorHints& hints, uint64_t /* iTick */);
50 virtual int getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available);
51 virtual int getHostFilesystemUsage(const char *name, ULONG *total, ULONG *used, ULONG *available);
52 virtual int getHostDiskSize(const char *name, uint64_t *size);
53 virtual int getProcessMemoryUsage(RTPROCESS process, ULONG *used);
54
55 virtual int getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle);
56 virtual int getRawHostNetworkLoad(const char *name, uint64_t *rx, uint64_t *tx);
57 virtual int getRawHostDiskLoad(const char *name, uint64_t *disk_ms, uint64_t *total_ms);
58 virtual int getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total);
59
60 virtual int getDiskListByFs(const char *name, DiskList& listUsage, DiskList& listLoad);
61private:
62 virtual int _getRawHostCpuLoad();
63 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 addVolumeDependencies(const char *pcszVolume, DiskList& listDisks);
66 void addRaidDisks(const char *pcszDevice, DiskList& listDisks);
67 char *trimTrailingDigits(char *pszName);
68 char *trimNewline(char *pszName);
69
70 struct VMProcessStats
71 {
72 uint64_t cpuUser;
73 uint64_t cpuKernel;
74 ULONG pagesUsed;
75 };
76
77 typedef std::map<RTPROCESS, VMProcessStats> VMProcessMap;
78
79 VMProcessMap mProcessStats;
80 uint64_t mUser, mKernel, mIdle;
81 uint64_t mSingleUser, mSingleKernel, mSingleIdle;
82 uint32_t mHZ;
83 ULONG totalRAM;
84};
85
86CollectorHAL *createHAL()
87{
88 return new CollectorLinux();
89}
90
91// Collector HAL for Linux
92
93CollectorLinux::CollectorLinux()
94{
95 long hz = sysconf(_SC_CLK_TCK);
96 if (hz == -1)
97 {
98 LogRel(("CollectorLinux failed to obtain HZ from kernel, assuming 100.\n"));
99 mHZ = 100;
100 }
101 else
102 mHZ = hz;
103 LogFlowThisFunc(("mHZ=%u\n", mHZ));
104
105 uint64_t cb;
106 int rc = RTSystemQueryTotalRam(&cb);
107 if (RT_FAILURE(rc))
108 totalRAM = 0;
109 else
110 totalRAM = (ULONG)(cb / 1024);
111}
112
113int CollectorLinux::preCollect(const CollectorHints& hints, uint64_t /* iTick */)
114{
115 std::vector<RTPROCESS> processes;
116 hints.getProcesses(processes);
117
118 std::vector<RTPROCESS>::iterator it;
119 for (it = processes.begin(); it != processes.end(); it++)
120 {
121 VMProcessStats vmStats;
122 int rc = getRawProcessStats(*it, &vmStats.cpuUser, &vmStats.cpuKernel, &vmStats.pagesUsed);
123 /* On failure, do NOT stop. Just skip the entry. Having the stats for
124 * one (probably broken) process frozen/zero is a minor issue compared
125 * to not updating many process stats and the host cpu stats. */
126 if (RT_SUCCESS(rc))
127 mProcessStats[*it] = vmStats;
128 }
129 if (hints.isHostCpuLoadCollected() || mProcessStats.size())
130 {
131 _getRawHostCpuLoad();
132 }
133 return VINF_SUCCESS;
134}
135
136int CollectorLinux::_getRawHostCpuLoad()
137{
138 int rc = VINF_SUCCESS;
139 long long unsigned uUser, uNice, uKernel, uIdle, uIowait, uIrq, uSoftirq;
140 FILE *f = fopen("/proc/stat", "r");
141
142 if (f)
143 {
144 char szBuf[128];
145 if (fgets(szBuf, sizeof(szBuf), f))
146 {
147 if (sscanf(szBuf, "cpu %llu %llu %llu %llu %llu %llu %llu",
148 &uUser, &uNice, &uKernel, &uIdle, &uIowait,
149 &uIrq, &uSoftirq) == 7)
150 {
151 mUser = uUser + uNice;
152 mKernel = uKernel + uIrq + uSoftirq;
153 mIdle = uIdle + uIowait;
154 }
155 /* Try to get single CPU stats. */
156 if (fgets(szBuf, sizeof(szBuf), f))
157 {
158 if (sscanf(szBuf, "cpu0 %llu %llu %llu %llu %llu %llu %llu",
159 &uUser, &uNice, &uKernel, &uIdle, &uIowait,
160 &uIrq, &uSoftirq) == 7)
161 {
162 mSingleUser = uUser + uNice;
163 mSingleKernel = uKernel + uIrq + uSoftirq;
164 mSingleIdle = uIdle + uIowait;
165 }
166 else
167 {
168 /* Assume that this is not an SMP system. */
169 Assert(RTMpGetCount() == 1);
170 mSingleUser = mUser;
171 mSingleKernel = mKernel;
172 mSingleIdle = mIdle;
173 }
174 }
175 else
176 rc = VERR_FILE_IO_ERROR;
177 }
178 else
179 rc = VERR_FILE_IO_ERROR;
180 fclose(f);
181 }
182 else
183 rc = VERR_ACCESS_DENIED;
184
185 return rc;
186}
187
188int CollectorLinux::getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle)
189{
190 *user = mUser;
191 *kernel = mKernel;
192 *idle = mIdle;
193 return VINF_SUCCESS;
194}
195
196int CollectorLinux::getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total)
197{
198 VMProcessMap::const_iterator it = mProcessStats.find(process);
199
200 if (it == mProcessStats.end())
201 {
202 Log (("No stats pre-collected for process %x\n", process));
203 return VERR_INTERNAL_ERROR;
204 }
205 *user = it->second.cpuUser;
206 *kernel = it->second.cpuKernel;
207 *total = mUser + mKernel + mIdle;
208 return VINF_SUCCESS;
209}
210
211int CollectorLinux::getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available)
212{
213 AssertReturn(totalRAM, VERR_INTERNAL_ERROR);
214 uint64_t cb;
215 int rc = RTSystemQueryAvailableRam(&cb);
216 if (RT_SUCCESS(rc))
217 {
218 *total = totalRAM;
219 *available = cb / 1024;
220 *used = *total - *available;
221 }
222 return rc;
223}
224
225int CollectorLinux::getHostFilesystemUsage(const char *path, ULONG *total, ULONG *used, ULONG *available)
226{
227 struct statvfs stats;
228
229 if (statvfs(path, &stats) == -1)
230 {
231 LogRel(("Failed to collect %s filesystem usage: errno=%d.\n", path, errno));
232 return VERR_ACCESS_DENIED;
233 }
234 uint64_t cbBlock = stats.f_frsize ? stats.f_frsize : stats.f_bsize;
235 *total = (ULONG)(cbBlock * stats.f_blocks / _1M);
236 *used = (ULONG)(cbBlock * (stats.f_blocks - stats.f_bfree) / _1M);
237 *available = (ULONG)(cbBlock * stats.f_bavail / _1M);
238
239 return VINF_SUCCESS;
240}
241
242int CollectorLinux::getHostDiskSize(const char *name, uint64_t *size)
243{
244 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;
257 else
258 rc = VERR_FILE_IO_ERROR;
259 fclose(f);
260 }
261 else
262 rc = VERR_ACCESS_DENIED;
263
264 return rc;
265}
266
267int CollectorLinux::getProcessMemoryUsage(RTPROCESS process, ULONG *used)
268{
269 VMProcessMap::const_iterator it = mProcessStats.find(process);
270
271 if (it == mProcessStats.end())
272 {
273 Log (("No stats pre-collected for process %x\n", process));
274 return VERR_INTERNAL_ERROR;
275 }
276 *used = it->second.pagesUsed * (PAGE_SIZE / 1024);
277 return VINF_SUCCESS;
278}
279
280int CollectorLinux::getRawProcessStats(RTPROCESS process, uint64_t *cpuUser, uint64_t *cpuKernel, ULONG *memPagesUsed)
281{
282 int rc = VINF_SUCCESS;
283 char *pszName;
284 pid_t pid2;
285 char c;
286 int iTmp;
287 long long unsigned int u64Tmp;
288 unsigned uTmp;
289 unsigned long ulTmp;
290 signed long ilTmp;
291 ULONG u32user, u32kernel;
292 char buf[80]; /* @todo: this should be tied to max allowed proc name. */
293
294 RTStrAPrintf(&pszName, "/proc/%d/stat", process);
295 //printf("Opening %s...\n", pszName);
296 FILE *f = fopen(pszName, "r");
297 RTMemFree(pszName);
298
299 if (f)
300 {
301 if (fscanf(f, "%d %79s %c %d %d %d %d %d %u %lu %lu %lu %lu %u %u "
302 "%ld %ld %ld %ld %ld %ld %llu %lu %u",
303 &pid2, buf, &c, &iTmp, &iTmp, &iTmp, &iTmp, &iTmp, &uTmp,
304 &ulTmp, &ulTmp, &ulTmp, &ulTmp, &u32user, &u32kernel,
305 &ilTmp, &ilTmp, &ilTmp, &ilTmp, &ilTmp, &ilTmp, &u64Tmp,
306 &ulTmp, memPagesUsed) == 24)
307 {
308 Assert((pid_t)process == pid2);
309 *cpuUser = u32user;
310 *cpuKernel = u32kernel;
311 }
312 else
313 rc = VERR_FILE_IO_ERROR;
314 fclose(f);
315 }
316 else
317 rc = VERR_ACCESS_DENIED;
318
319 return rc;
320}
321
322int CollectorLinux::getRawHostNetworkLoad(const char *name, uint64_t *rx, uint64_t *tx)
323{
324 int rc = VINF_SUCCESS;
325 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;
354}
355
356int CollectorLinux::getRawHostDiskLoad(const char *name, uint64_t *disk_ms, uint64_t *total_ms)
357{
358#if 0
359 int rc = VINF_SUCCESS;
360 char szIfName[/*IFNAMSIZ*/ 16 + 36];
361 long long unsigned int u64Busy, tmp;
362
363 RTStrPrintf(szIfName, sizeof(szIfName), "/sys/class/block/%s/stat", name);
364 FILE *f = fopen(szIfName, "r");
365 if (f)
366 {
367 if (fscanf(f, "%llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu",
368 &tmp, &tmp, &tmp, &tmp, &tmp, &tmp, &tmp, &tmp, &tmp, &u64Busy, &tmp) == 11)
369 {
370 *disk_ms = u64Busy;
371 *total_ms = (uint64_t)(mSingleUser + mSingleKernel + mSingleIdle) * 1000 / mHZ;
372 }
373 else
374 rc = VERR_FILE_IO_ERROR;
375 fclose(f);
376 }
377 else
378 rc = VERR_ACCESS_DENIED;
379#else
380 int rc = VERR_MISSING;
381 FILE *f = fopen("/proc/diskstats", "r");
382 if (f)
383 {
384 char szBuf[128];
385 while (fgets(szBuf, sizeof(szBuf), f))
386 {
387 char *pszBufName = szBuf;
388 while (*pszBufName == ' ') ++pszBufName; /* Skip spaces */
389 while (RT_C_IS_DIGIT(*pszBufName)) ++pszBufName; /* Skip major */
390 while (*pszBufName == ' ') ++pszBufName; /* Skip spaces */
391 while (RT_C_IS_DIGIT(*pszBufName)) ++pszBufName; /* Skip minor */
392 while (*pszBufName == ' ') ++pszBufName; /* Skip spaces */
393
394 char *pszBufData = strchr(pszBufName, ' ');
395 if (!pszBufData)
396 {
397 LogRel(("CollectorLinux::getRawHostDiskLoad() failed to parse disk stats: %s\n", szBuf));
398 continue;
399 }
400 *pszBufData++ = '\0';
401 if (!strcmp(name, pszBufName))
402 {
403 long long unsigned int u64Busy, tmp;
404
405 if (sscanf(pszBufData, "%llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu",
406 &tmp, &tmp, &tmp, &tmp, &tmp, &tmp, &tmp, &tmp, &tmp, &u64Busy, &tmp) == 11)
407 {
408 *disk_ms = u64Busy;
409 *total_ms = (uint64_t)(mSingleUser + mSingleKernel + mSingleIdle) * 1000 / mHZ;
410 rc = VINF_SUCCESS;
411 }
412 else
413 rc = VERR_FILE_IO_ERROR;
414 break;
415 }
416 }
417 fclose(f);
418 }
419#endif
420
421 return rc;
422}
423
424char *CollectorLinux::trimNewline(char *pszName)
425{
426 unsigned cbName = strlen(pszName);
427 if (cbName == 0)
428 return pszName;
429
430 char *pszEnd = pszName + cbName - 1;
431 while (pszEnd > pszName && *pszEnd == '\n')
432 pszEnd--;
433 pszEnd[1] = '\0';
434
435 return pszName;
436}
437
438char *CollectorLinux::trimTrailingDigits(char *pszName)
439{
440 unsigned cbName = strlen(pszName);
441 if (cbName == 0)
442 return pszName;
443
444 char *pszEnd = pszName + cbName - 1;
445 while (pszEnd > pszName && (RT_C_IS_DIGIT(*pszEnd) || *pszEnd == '\n'))
446 pszEnd--;
447 pszEnd[1] = '\0';
448
449 return pszName;
450}
451
452char *CollectorLinux::getDiskName(char *pszDiskName, size_t cbDiskName, const char *pszDevName, bool fTrimDigits)
453{
454 unsigned cbName = 0;
455 unsigned cbDevName = strlen(pszDevName);
456 const char *pszEnd = pszDevName + cbDevName - 1;
457 if (fTrimDigits)
458 while (pszEnd > pszDevName && RT_C_IS_DIGIT(*pszEnd))
459 pszEnd--;
460 while (pszEnd > pszDevName && *pszEnd != '/')
461 {
462 cbName++;
463 pszEnd--;
464 }
465 RTStrCopy(pszDiskName, RT_MIN(cbName + 1, cbDiskName), pszEnd + 1);
466 return pszDiskName;
467}
468
469void CollectorLinux::addRaidDisks(const char *pcszDevice, DiskList& listDisks)
470{
471 FILE *f = fopen("/proc/mdstat", "r");
472 if (f)
473 {
474 char szBuf[128];
475 while (fgets(szBuf, sizeof(szBuf), f))
476 {
477 char *pszBufName = szBuf;
478
479 char *pszBufData = strchr(pszBufName, ' ');
480 if (!pszBufData)
481 {
482 LogRel(("CollectorLinux::addRaidDisks() failed to parse disk stats: %s\n", szBuf));
483 continue;
484 }
485 *pszBufData++ = '\0';
486 if (!strcmp(pcszDevice, pszBufName))
487 {
488 while (*pszBufData == ':') ++pszBufData; /* Skip delimiter */
489 while (*pszBufData == ' ') ++pszBufData; /* Skip spaces */
490 while (RT_C_IS_ALNUM(*pszBufData)) ++pszBufData; /* Skip status */
491 while (*pszBufData == ' ') ++pszBufData; /* Skip spaces */
492 while (RT_C_IS_ALNUM(*pszBufData)) ++pszBufData; /* Skip type */
493
494 while (*pszBufData != '\0')
495 {
496 while (*pszBufData == ' ') ++pszBufData; /* Skip spaces */
497 char *pszDisk = pszBufData;
498 while (RT_C_IS_ALPHA(*pszBufData))
499 ++pszBufData;
500 if (*pszBufData)
501 {
502 *pszBufData++ = '\0';
503 listDisks.push_back(RTCString(pszDisk));
504 while (*pszBufData != '\0' && *pszBufData != ' ')
505 ++pszBufData;
506 }
507 else
508 listDisks.push_back(RTCString(pszDisk));
509 }
510 break;
511 }
512 }
513 fclose(f);
514 }
515}
516
517void CollectorLinux::addVolumeDependencies(const char *pcszVolume, DiskList& listDisks)
518{
519 char szVolInfo[RTPATH_MAX];
520 int rc = RTPathExecDir(szVolInfo, sizeof(szVolInfo) - sizeof("/" VBOXVOLINFO_NAME " ") - strlen(pcszVolume));
521 if (RT_FAILURE(rc))
522 {
523 LogRel(("VolInfo: Failed to get program path, rc=%Rrc\n", rc));
524 return;
525 }
526 strcat(szVolInfo, "/" VBOXVOLINFO_NAME " ");
527 strcat(szVolInfo, pcszVolume);
528
529 FILE *fp = popen(szVolInfo, "r");
530 if (fp)
531 {
532 char szBuf[128];
533
534 while (fgets(szBuf, sizeof(szBuf), fp))
535 if (strncmp(szBuf, "dm-", 3))
536 listDisks.push_back(RTCString(trimTrailingDigits(szBuf)));
537 else
538 listDisks.push_back(RTCString(trimNewline(szBuf)));
539
540 pclose(fp);
541 }
542 else
543 listDisks.push_back(RTCString(pcszVolume));
544}
545
546int CollectorLinux::getDiskListByFs(const char *pszPath, DiskList& listUsage, DiskList& listLoad)
547{
548 FILE *mtab = setmntent("/etc/mtab", "r");
549 if (mtab)
550 {
551 struct mntent *mntent;
552 while ((mntent = getmntent(mtab)))
553 {
554 /* Skip rootfs entry, there must be another root mount. */
555 if (strcmp(mntent->mnt_fsname, "rootfs") == 0)
556 continue;
557 if (strcmp(pszPath, mntent->mnt_dir) == 0)
558 {
559 char szDevName[128];
560 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))
568 {
569 /* LVM */
570 getDiskName(szDevName, sizeof(szDevName), szFsName, false);
571 addVolumeDependencies(szDevName, listUsage);
572 listLoad = listUsage;
573 }
574 else if (!strncmp(szFsName, "/dev/md", 7))
575 {
576 /* Software RAID */
577 getDiskName(szDevName, sizeof(szDevName), szFsName, false);
578 listUsage.push_back(RTCString(szDevName));
579 addRaidDisks(szDevName, listLoad);
580 }
581 else
582 {
583 /* Plain disk partition */
584 getDiskName(szDevName, sizeof(szDevName), mntent->mnt_fsname, true);
585 listUsage.push_back(RTCString(szDevName));
586 listLoad.push_back(RTCString(szDevName));
587 }
588 if (listUsage.empty() || listLoad.empty())
589 {
590 LogRel(("Failed to retrive disk info: getDiskName(%s) --> %s\n", mntent->mnt_fsname, szDevName));
591 }
592 break;
593 }
594 }
595 endmntent(mtab);
596 }
597 return VINF_SUCCESS;
598}
599
600}
601
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