VirtualBox

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

Last change on this file since 37989 was 37713, checked in by vboxsync, 13 years ago

Main/linux/Performance: if collecting the stats for a process fails keep going, otherwise many stats updates are missing

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.2 KB
Line 
1/* $Id: PerformanceLinux.cpp 37713 2011-06-30 15:14:13Z vboxsync $ */
2
3/** @file
4 *
5 * VBox Linux-specific Performance Classes implementation.
6 */
7
8/*
9 * Copyright (C) 2008-2011 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 <iprt/alloc.h>
22#include <iprt/err.h>
23#include <iprt/param.h>
24#include <iprt/string.h>
25
26#include <map>
27#include <vector>
28
29#include "Logging.h"
30#include "Performance.h"
31
32namespace pm {
33
34class CollectorLinux : public CollectorHAL
35{
36public:
37 virtual int preCollect(const CollectorHints& hints, uint64_t /* iTick */);
38 virtual int getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available);
39 virtual int getProcessMemoryUsage(RTPROCESS process, ULONG *used);
40
41 virtual int getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle);
42 virtual int getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total);
43private:
44 virtual int _getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle);
45 int getRawProcessStats(RTPROCESS process, uint64_t *cpuUser, uint64_t *cpuKernel, ULONG *memPagesUsed);
46
47 struct VMProcessStats
48 {
49 uint64_t cpuUser;
50 uint64_t cpuKernel;
51 ULONG pagesUsed;
52 };
53
54 typedef std::map<RTPROCESS, VMProcessStats> VMProcessMap;
55
56 VMProcessMap mProcessStats;
57 uint64_t mUser, mKernel, mIdle;
58};
59
60CollectorHAL *createHAL()
61{
62 return new CollectorLinux();
63}
64
65// Collector HAL for Linux
66
67int CollectorLinux::preCollect(const CollectorHints& hints, uint64_t /* iTick */)
68{
69 std::vector<RTPROCESS> processes;
70 hints.getProcesses(processes);
71
72 std::vector<RTPROCESS>::iterator it;
73 for (it = processes.begin(); it != processes.end(); it++)
74 {
75 VMProcessStats vmStats;
76 int rc = getRawProcessStats(*it, &vmStats.cpuUser, &vmStats.cpuKernel, &vmStats.pagesUsed);
77 /* On failure, do NOT stop. Just skip the entry. Having the stats for
78 * one (probably broken) process frozen/zero is a minor issue compared
79 * to not updating many process stats and the host cpu stats. */
80 if (RT_SUCCESS(rc))
81 mProcessStats[*it] = vmStats;
82 }
83 if (hints.isHostCpuLoadCollected() || mProcessStats.size())
84 {
85 _getRawHostCpuLoad(&mUser, &mKernel, &mIdle);
86 }
87 return VINF_SUCCESS;
88}
89
90int CollectorLinux::_getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle)
91{
92 int rc = VINF_SUCCESS;
93 ULONG u32user, u32nice, u32kernel, u32idle;
94 FILE *f = fopen("/proc/stat", "r");
95
96 if (f)
97 {
98 if (fscanf(f, "cpu %u %u %u %u", &u32user, &u32nice, &u32kernel, &u32idle) == 4)
99 {
100 *user = (uint64_t)u32user + u32nice;
101 *kernel = u32kernel;
102 *idle = u32idle;
103 }
104 else
105 rc = VERR_FILE_IO_ERROR;
106 fclose(f);
107 }
108 else
109 rc = VERR_ACCESS_DENIED;
110
111 return rc;
112}
113
114int CollectorLinux::getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle)
115{
116 *user = mUser;
117 *kernel = mKernel;
118 *idle = mIdle;
119 return VINF_SUCCESS;
120}
121
122int CollectorLinux::getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total)
123{
124 VMProcessMap::const_iterator it = mProcessStats.find(process);
125
126 if (it == mProcessStats.end())
127 {
128 Log (("No stats pre-collected for process %x\n", process));
129 return VERR_INTERNAL_ERROR;
130 }
131 *user = it->second.cpuUser;
132 *kernel = it->second.cpuKernel;
133 *total = mUser + mKernel + mIdle;
134 return VINF_SUCCESS;
135}
136
137int CollectorLinux::getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available)
138{
139 int rc = VINF_SUCCESS;
140 ULONG buffers, cached;
141 FILE *f = fopen("/proc/meminfo", "r");
142
143 if (f)
144 {
145 int processed = fscanf(f, "MemTotal: %u kB\n", total);
146 processed += fscanf(f, "MemFree: %u kB\n", available);
147 processed += fscanf(f, "Buffers: %u kB\n", &buffers);
148 processed += fscanf(f, "Cached: %u kB\n", &cached);
149 if (processed == 4)
150 {
151 *available += buffers + cached;
152 *used = *total - *available;
153 }
154 else
155 rc = VERR_FILE_IO_ERROR;
156 fclose(f);
157 }
158 else
159 rc = VERR_ACCESS_DENIED;
160
161 return rc;
162}
163
164int CollectorLinux::getProcessMemoryUsage(RTPROCESS process, ULONG *used)
165{
166 VMProcessMap::const_iterator it = mProcessStats.find(process);
167
168 if (it == mProcessStats.end())
169 {
170 Log (("No stats pre-collected for process %x\n", process));
171 return VERR_INTERNAL_ERROR;
172 }
173 *used = it->second.pagesUsed * (PAGE_SIZE / 1024);
174 return VINF_SUCCESS;
175}
176
177int CollectorLinux::getRawProcessStats(RTPROCESS process, uint64_t *cpuUser, uint64_t *cpuKernel, ULONG *memPagesUsed)
178{
179 int rc = VINF_SUCCESS;
180 char *pszName;
181 pid_t pid2;
182 char c;
183 int iTmp;
184 long long unsigned int u64Tmp;
185 unsigned uTmp;
186 unsigned long ulTmp;
187 signed long ilTmp;
188 ULONG u32user, u32kernel;
189 char buf[80]; /* @todo: this should be tied to max allowed proc name. */
190
191 RTStrAPrintf(&pszName, "/proc/%d/stat", process);
192 //printf("Opening %s...\n", pszName);
193 FILE *f = fopen(pszName, "r");
194 RTMemFree(pszName);
195
196 if (f)
197 {
198 if (fscanf(f, "%d %79s %c %d %d %d %d %d %u %lu %lu %lu %lu %u %u "
199 "%ld %ld %ld %ld %ld %ld %llu %lu %u",
200 &pid2, buf, &c, &iTmp, &iTmp, &iTmp, &iTmp, &iTmp, &uTmp,
201 &ulTmp, &ulTmp, &ulTmp, &ulTmp, &u32user, &u32kernel,
202 &ilTmp, &ilTmp, &ilTmp, &ilTmp, &ilTmp, &ilTmp, &u64Tmp,
203 &ulTmp, memPagesUsed) == 24)
204 {
205 Assert((pid_t)process == pid2);
206 *cpuUser = u32user;
207 *cpuKernel = u32kernel;
208 }
209 else
210 rc = VERR_FILE_IO_ERROR;
211 fclose(f);
212 }
213 else
214 rc = VERR_ACCESS_DENIED;
215
216 return rc;
217}
218
219}
220
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