VirtualBox

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

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

Another attempt to fix missing getpagesize() in RHEL3

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