VirtualBox

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

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

Linux build fix.

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