VirtualBox

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

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

Stubs for all platforms. Implementation of host CPU load and RAM usage counters for Windows. Locking. Fixes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.1 KB
Line 
1/* $Id: PerformanceLinux.cpp 10753 2008-07-18 19:22:21Z 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 "Performance.h"
26
27namespace pm {
28
29class CollectorLinux : public CollectorHAL
30{
31public:
32 virtual int getHostCpuMHz(unsigned long *mhz);
33 virtual int getHostMemoryUsage(unsigned long *total, unsigned long *used, unsigned long *available);
34 virtual int getProcessMemoryUsage(RTPROCESS process, unsigned long *used);
35
36 virtual int getRawHostCpuLoad(unsigned long *user, unsigned long *kernel, unsigned long *idle);
37 virtual int getRawProcessCpuLoad(RTPROCESS process, unsigned long *user, unsigned long *kernel);
38};
39
40// Linux Metric factory
41
42MetricFactoryLinux::MetricFactoryLinux()
43{
44 mHAL = new CollectorLinux();
45 Assert(mHAL);
46}
47
48BaseMetric *MetricFactoryLinux::createHostCpuLoad(ComPtr<IUnknown> object, SubMetric *user, SubMetric *kernel, SubMetric *idle)
49{
50 Assert(mHAL);
51 return new HostCpuLoadRaw(mHAL, object, user, kernel, idle);
52}
53
54BaseMetric *MetricFactoryLinux::createMachineCpuLoad(ComPtr<IUnknown> object, RTPROCESS process, SubMetric *user, SubMetric *kernel)
55{
56 Assert(mHAL);
57 return new MachineCpuLoadRaw(mHAL, object, process, user, kernel);
58}
59
60// Collector HAL for Linux
61
62int CollectorLinux::getRawHostCpuLoad(unsigned long *user, unsigned long *kernel, unsigned long *idle)
63{
64 int rc = VINF_SUCCESS;
65 unsigned long nice;
66 FILE *f = fopen("/proc/stat", "r");
67
68 if (f)
69 {
70 if (fscanf(f, "cpu %lu %lu %lu %lu", user, &nice, kernel, idle) == 4)
71 *user += nice;
72 else
73 rc = VERR_FILE_IO_ERROR;
74 fclose(f);
75 }
76 else
77 rc = VERR_ACCESS_DENIED;
78
79 return rc;
80}
81
82int CollectorLinux::getRawProcessCpuLoad(RTPROCESS process, unsigned long *user, unsigned long *kernel)
83{
84 int rc = VINF_SUCCESS;
85 char *pszName;
86 pid_t pid2;
87 char c;
88 int iTmp;
89 unsigned uTmp;
90 unsigned long ulTmp;
91 char buf[80]; /* @todo: this should be tied to max allowed proc name. */
92
93 RTStrAPrintf(&pszName, "/proc/%d/stat", process);
94 //printf("Opening %s...\n", pszName);
95 FILE *f = fopen(pszName, "r");
96 RTMemFree(pszName);
97
98 if (f)
99 {
100 if (fscanf(f, "%d %s %c %d %d %d %d %d %u %lu %lu %lu %lu %lu %lu",
101 &pid2, buf, &c, &iTmp, &iTmp, &iTmp, &iTmp, &iTmp, &uTmp,
102 &ulTmp, &ulTmp, &ulTmp, &ulTmp, user, kernel) == 15)
103 {
104 Assert((pid_t)process == pid2);
105 }
106 else
107 rc = VERR_FILE_IO_ERROR;
108 fclose(f);
109 }
110 else
111 rc = VERR_ACCESS_DENIED;
112
113 return rc;
114}
115
116int CollectorLinux::getHostCpuMHz(unsigned long *mhz)
117{
118 return E_NOTIMPL;
119}
120
121int CollectorLinux::getHostMemoryUsage(unsigned long *total, unsigned long *used, unsigned long *available)
122{
123 int rc = VINF_SUCCESS;
124 unsigned long buffers, cached;
125 FILE *f = fopen("/proc/meminfo", "r");
126
127 if (f)
128 {
129 int processed = fscanf(f, "MemTotal: %lu kB", total);
130 processed += fscanf(f, "MemFree: %lu kB", available);
131 processed += fscanf(f, "Buffers: %lu kB", &buffers);
132 processed += fscanf(f, "Cached: %lu kB", &cached);
133 if (processed == 4)
134 *available += buffers + cached;
135 else
136 rc = VERR_FILE_IO_ERROR;
137 fclose(f);
138 }
139 else
140 rc = VERR_ACCESS_DENIED;
141
142 return rc;
143}
144int CollectorLinux::getProcessMemoryUsage(RTPROCESS process, unsigned long *used)
145{
146 return E_NOTIMPL;
147}
148
149}
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