VirtualBox

source: vbox/trunk/src/VBox/Main/linux/PerformanceLinux.cpp@ 10919

Last change on this file since 10919 was 10919, checked in by vboxsync, 17 years ago

replaced getpagesize() with PAGE_SIZE

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.0 KB
Line 
1/* $Id: PerformanceLinux.cpp 10919 2008-07-28 18:41:31Z 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/param.h>
28#include <iprt/string.h>
29#include "Performance.h"
30
31namespace pm {
32
33class CollectorLinux : public CollectorHAL
34{
35public:
36 virtual int getHostCpuMHz(unsigned long *mhz);
37 virtual int getHostMemoryUsage(unsigned long *total, unsigned long *used, unsigned long *available);
38 virtual int getProcessMemoryUsage(RTPROCESS process, unsigned long *used);
39
40 virtual int getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle);
41 virtual int getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total);
42private:
43 int getRawProcessStats(RTPROCESS process, uint64_t *cpuUser, uint64_t *cpuKernel, unsigned long *memPagesUsed);
44};
45
46// Linux Metric factory
47
48MetricFactoryLinux::MetricFactoryLinux()
49{
50 mHAL = new CollectorLinux();
51 Assert(mHAL);
52}
53
54// Collector HAL for Linux
55
56int CollectorLinux::getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle)
57{
58 int rc = VINF_SUCCESS;
59 unsigned long u32user, u32nice, u32kernel, u32idle;
60 FILE *f = fopen("/proc/stat", "r");
61
62 if (f)
63 {
64 if (fscanf(f, "cpu %lu %lu %lu %lu", &u32user, &u32nice, &u32kernel, &u32idle) == 4)
65 {
66 *user = (uint64_t)u32user + u32nice;
67 *kernel = u32kernel;
68 *idle = u32idle;
69 }
70 else
71 rc = VERR_FILE_IO_ERROR;
72 fclose(f);
73 }
74 else
75 rc = VERR_ACCESS_DENIED;
76
77 return rc;
78}
79
80int CollectorLinux::getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total)
81{
82 int rc = VINF_SUCCESS;
83 uint64_t uHostUser, uHostKernel, uHostIdle;
84
85 rc = getRawHostCpuLoad(&uHostUser, &uHostKernel, &uHostIdle);
86 if (RT_SUCCESS(rc))
87 {
88 unsigned long ulTmp;
89 *total = (uint64_t)uHostUser + uHostKernel + uHostIdle;
90 rc = getRawProcessStats(process, user, kernel, &ulTmp);
91 }
92
93 return rc;
94}
95
96int CollectorLinux::getHostCpuMHz(unsigned long *mhz)
97{
98 return E_NOTIMPL;
99}
100
101int CollectorLinux::getHostMemoryUsage(unsigned long *total, unsigned long *used, unsigned long *available)
102{
103 int rc = VINF_SUCCESS;
104 unsigned long buffers, cached;
105 FILE *f = fopen("/proc/meminfo", "r");
106
107 if (f)
108 {
109 int processed = fscanf(f, "MemTotal: %lu kB", total);
110 processed += fscanf(f, "MemFree: %lu kB", available);
111 processed += fscanf(f, "Buffers: %lu kB", &buffers);
112 processed += fscanf(f, "Cached: %lu kB", &cached);
113 if (processed == 4)
114 *available += buffers + cached;
115 else
116 rc = VERR_FILE_IO_ERROR;
117 fclose(f);
118 }
119 else
120 rc = VERR_ACCESS_DENIED;
121
122 return rc;
123}
124
125int CollectorLinux::getProcessMemoryUsage(RTPROCESS process, unsigned long *used)
126{
127 uint64_t u64Tmp;
128 unsigned long nPagesUsed;
129 int rc = getRawProcessStats(process, &u64Tmp, &u64Tmp, &nPagesUsed);
130 if (RT_SUCCESS(rc))
131 {
132 Assert(getpagesize() >= 1024);
133 *used = nPagesUsed * (PAGE_SIZE / 1024);
134 }
135 return rc;
136}
137
138int CollectorLinux::getRawProcessStats(RTPROCESS process, uint64_t *cpuUser, uint64_t *cpuKernel, unsigned long *memPagesUsed)
139{
140 int rc = VINF_SUCCESS;
141 char *pszName;
142 pid_t pid2;
143 char c;
144 int iTmp;
145 uint64_t u64Tmp;
146 unsigned uTmp;
147 unsigned long ulTmp, u32user, u32kernel;
148 char buf[80]; /* @todo: this should be tied to max allowed proc name. */
149
150 RTStrAPrintf(&pszName, "/proc/%d/stat", process);
151 //printf("Opening %s...\n", pszName);
152 FILE *f = fopen(pszName, "r");
153 RTMemFree(pszName);
154
155 if (f)
156 {
157 if (fscanf(f, "%d %s %c %d %d %d %d %d %u %lu %lu %lu %lu %lu %lu "
158 "%ld %ld %ld %ld %ld %ld %llu %lu %ld",
159 &pid2, buf, &c, &iTmp, &iTmp, &iTmp, &iTmp, &iTmp, &uTmp,
160 &ulTmp, &ulTmp, &ulTmp, &ulTmp, &u32user, &u32kernel,
161 &ulTmp, &ulTmp, &ulTmp, &ulTmp, &ulTmp, &ulTmp, &u64Tmp,
162 &ulTmp, memPagesUsed) == 24)
163 {
164 Assert((pid_t)process == pid2);
165 *cpuUser = u32user;
166 *cpuKernel = u32kernel;
167 }
168 else
169 rc = VERR_FILE_IO_ERROR;
170 fclose(f);
171 }
172 else
173 rc = VERR_ACCESS_DENIED;
174
175 return rc;
176}
177
178}
179
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette