VirtualBox

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

Last change on this file since 11162 was 10938, checked in by vboxsync, 16 years ago

Test case for collector HAL. Fixes for memory usage stats.

  • 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 10938 2008-07-29 16:27:06Z 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.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 * 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\n", total);
110 processed += fscanf(f, "MemFree: %lu kB\n", available);
111 processed += fscanf(f, "Buffers: %lu kB\n", &buffers);
112 processed += fscanf(f, "Cached: %lu kB\n", &cached);
113 if (processed == 4)
114 {
115 *available += buffers + cached;
116 *used = *total - *available;
117 }
118 else
119 rc = VERR_FILE_IO_ERROR;
120 fclose(f);
121 }
122 else
123 rc = VERR_ACCESS_DENIED;
124
125 return rc;
126}
127
128int CollectorLinux::getProcessMemoryUsage(RTPROCESS process, unsigned long *used)
129{
130 uint64_t u64Tmp;
131 unsigned long nPagesUsed;
132 int rc = getRawProcessStats(process, &u64Tmp, &u64Tmp, &nPagesUsed);
133 if (RT_SUCCESS(rc))
134 {
135 Assert(PAGE_SIZE >= 1024);
136 *used = nPagesUsed * (PAGE_SIZE / 1024);
137 }
138 return rc;
139}
140
141int CollectorLinux::getRawProcessStats(RTPROCESS process, uint64_t *cpuUser, uint64_t *cpuKernel, unsigned long *memPagesUsed)
142{
143 int rc = VINF_SUCCESS;
144 char *pszName;
145 pid_t pid2;
146 char c;
147 int iTmp;
148 uint64_t u64Tmp;
149 unsigned uTmp;
150 unsigned long ulTmp, u32user, u32kernel;
151 char buf[80]; /* @todo: this should be tied to max allowed proc name. */
152
153 RTStrAPrintf(&pszName, "/proc/%d/stat", process);
154 //printf("Opening %s...\n", pszName);
155 FILE *f = fopen(pszName, "r");
156 RTMemFree(pszName);
157
158 if (f)
159 {
160 if (fscanf(f, "%d %s %c %d %d %d %d %d %u %lu %lu %lu %lu %lu %lu "
161 "%ld %ld %ld %ld %ld %ld %llu %lu %ld",
162 &pid2, buf, &c, &iTmp, &iTmp, &iTmp, &iTmp, &iTmp, &uTmp,
163 &ulTmp, &ulTmp, &ulTmp, &ulTmp, &u32user, &u32kernel,
164 &ulTmp, &ulTmp, &ulTmp, &ulTmp, &ulTmp, &ulTmp, &u64Tmp,
165 &ulTmp, memPagesUsed) == 24)
166 {
167 Assert((pid_t)process == pid2);
168 *cpuUser = u32user;
169 *cpuKernel = u32kernel;
170 }
171 else
172 rc = VERR_FILE_IO_ERROR;
173 fclose(f);
174 }
175 else
176 rc = VERR_ACCESS_DENIED;
177
178 return rc;
179}
180
181}
182
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