1 | /* $Id: PerformanceLinux.cpp 10868 2008-07-24 18:34:35Z vboxsync $ */
|
---|
2 |
|
---|
3 | /** @file
|
---|
4 | *
|
---|
5 | * VBox Linux-specific Performance Classes implementation.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2008 Sun Microsystems, Inc.
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.215389.xyz. 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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
20 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
21 | * additional information or have any questions.
|
---|
22 | */
|
---|
23 |
|
---|
24 | #include <stdio.h>
|
---|
25 | #include <iprt/alloc.h>
|
---|
26 | #include <iprt/err.h>
|
---|
27 | #include <iprt/string.h>
|
---|
28 | #include "Performance.h"
|
---|
29 |
|
---|
30 | namespace pm {
|
---|
31 |
|
---|
32 | class CollectorLinux : public CollectorHAL
|
---|
33 | {
|
---|
34 | public:
|
---|
35 | virtual int getHostCpuMHz(unsigned long *mhz);
|
---|
36 | virtual int getHostMemoryUsage(unsigned long *total, unsigned long *used, unsigned long *available);
|
---|
37 | virtual int getProcessMemoryUsage(RTPROCESS process, unsigned long *used);
|
---|
38 |
|
---|
39 | virtual int getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle);
|
---|
40 | virtual int getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total);
|
---|
41 | };
|
---|
42 |
|
---|
43 | // Linux Metric factory
|
---|
44 |
|
---|
45 | MetricFactoryLinux::MetricFactoryLinux()
|
---|
46 | {
|
---|
47 | mHAL = new CollectorLinux();
|
---|
48 | Assert(mHAL);
|
---|
49 | }
|
---|
50 |
|
---|
51 | // Collector HAL for Linux
|
---|
52 |
|
---|
53 | int CollectorLinux::getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle)
|
---|
54 | {
|
---|
55 | int rc = VINF_SUCCESS;
|
---|
56 | unsigned long u32user, u32nice, u32kernel, u32idle;
|
---|
57 | FILE *f = fopen("/proc/stat", "r");
|
---|
58 |
|
---|
59 | if (f)
|
---|
60 | {
|
---|
61 | if (fscanf(f, "cpu %lu %lu %lu %lu", &u32user, &u32nice, &u32kernel, &u32idle) == 4)
|
---|
62 | {
|
---|
63 | *user = (uint64_t)u32user + u32nice;
|
---|
64 | *kernel = u32kernel;
|
---|
65 | *idle = u32idle;
|
---|
66 | }
|
---|
67 | else
|
---|
68 | rc = VERR_FILE_IO_ERROR;
|
---|
69 | fclose(f);
|
---|
70 | }
|
---|
71 | else
|
---|
72 | rc = VERR_ACCESS_DENIED;
|
---|
73 |
|
---|
74 | return rc;
|
---|
75 | }
|
---|
76 |
|
---|
77 | int CollectorLinux::getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total)
|
---|
78 | {
|
---|
79 | int rc = VINF_SUCCESS;
|
---|
80 | char *pszName;
|
---|
81 | pid_t pid2;
|
---|
82 | char c;
|
---|
83 | int iTmp;
|
---|
84 | unsigned uTmp;
|
---|
85 | unsigned long ulTmp, u32user, u32kernel;
|
---|
86 | char buf[80]; /* @todo: this should be tied to max allowed proc name. */
|
---|
87 |
|
---|
88 | uint64_t uHostUser, uHostKernel, uHostIdle;
|
---|
89 | rc = getRawHostCpuLoad(uHostUser, uHostKernel, uHostIdle);
|
---|
90 | if (RT_FAILURE(rc))
|
---|
91 | return rc;
|
---|
92 | *total = (uint64_t)uHostUser + uHostKernel + uHostIdle;
|
---|
93 |
|
---|
94 | RTStrAPrintf(&pszName, "/proc/%d/stat", process);
|
---|
95 | //printf("Opening %s...\n", pszName);
|
---|
96 | FILE *f = fopen(pszName, "r");
|
---|
97 | RTMemFree(pszName);
|
---|
98 |
|
---|
99 | if (f)
|
---|
100 | {
|
---|
101 | if (fscanf(f, "%d %s %c %d %d %d %d %d %u %lu %lu %lu %lu %lu %lu",
|
---|
102 | &pid2, buf, &c, &iTmp, &iTmp, &iTmp, &iTmp, &iTmp, &uTmp,
|
---|
103 | &ulTmp, &ulTmp, &ulTmp, &ulTmp, &u32user, &u32kernel) == 15)
|
---|
104 | {
|
---|
105 | Assert((pid_t)process == pid2);
|
---|
106 | *user = u32user;
|
---|
107 | *kernel = u32kernel;
|
---|
108 | }
|
---|
109 | else
|
---|
110 | rc = VERR_FILE_IO_ERROR;
|
---|
111 | fclose(f);
|
---|
112 | }
|
---|
113 | else
|
---|
114 | rc = VERR_ACCESS_DENIED;
|
---|
115 |
|
---|
116 | return rc;
|
---|
117 | }
|
---|
118 |
|
---|
119 | int CollectorLinux::getHostCpuMHz(unsigned long *mhz)
|
---|
120 | {
|
---|
121 | return E_NOTIMPL;
|
---|
122 | }
|
---|
123 |
|
---|
124 | int CollectorLinux::getHostMemoryUsage(unsigned long *total, unsigned long *used, unsigned long *available)
|
---|
125 | {
|
---|
126 | int rc = VINF_SUCCESS;
|
---|
127 | unsigned long buffers, cached;
|
---|
128 | FILE *f = fopen("/proc/meminfo", "r");
|
---|
129 |
|
---|
130 | if (f)
|
---|
131 | {
|
---|
132 | int processed = fscanf(f, "MemTotal: %lu kB", total);
|
---|
133 | processed += fscanf(f, "MemFree: %lu kB", available);
|
---|
134 | processed += fscanf(f, "Buffers: %lu kB", &buffers);
|
---|
135 | processed += fscanf(f, "Cached: %lu kB", &cached);
|
---|
136 | if (processed == 4)
|
---|
137 | *available += buffers + cached;
|
---|
138 | else
|
---|
139 | rc = VERR_FILE_IO_ERROR;
|
---|
140 | fclose(f);
|
---|
141 | }
|
---|
142 | else
|
---|
143 | rc = VERR_ACCESS_DENIED;
|
---|
144 |
|
---|
145 | return rc;
|
---|
146 | }
|
---|
147 | int CollectorLinux::getProcessMemoryUsage(RTPROCESS process, unsigned long *used)
|
---|
148 | {
|
---|
149 | return E_NOTIMPL;
|
---|
150 | }
|
---|
151 |
|
---|
152 | }
|
---|