VirtualBox

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

最後變更 在這個檔案從12400是 12400,由 vboxsync 提交於 17 年 前

PerfAPI: Improved Win collector. More realistic performance test. No factories.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 4.8 KB
 
1/* $Id: PerformanceLinux.cpp 12400 2008-09-11 10:34:58Z 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.alldomusa.eu.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 getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available);
37 virtual int getProcessMemoryUsage(RTPROCESS process, ULONG *used);
38
39 virtual int getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle);
40 virtual int getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total);
41private:
42 int getRawProcessStats(RTPROCESS process, uint64_t *cpuUser, uint64_t *cpuKernel, ULONG *memPagesUsed);
43};
44
45CollectorHAL *createHAL()
46{
47 return new CollectorLinux();
48}
49
50// Collector HAL for Linux
51
52int CollectorLinux::getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle)
53{
54 int rc = VINF_SUCCESS;
55 ULONG u32user, u32nice, u32kernel, u32idle;
56 FILE *f = fopen("/proc/stat", "r");
57
58 if (f)
59 {
60 if (fscanf(f, "cpu %u %u %u %u", &u32user, &u32nice, &u32kernel, &u32idle) == 4)
61 {
62 *user = (uint64_t)u32user + u32nice;
63 *kernel = u32kernel;
64 *idle = u32idle;
65 }
66 else
67 rc = VERR_FILE_IO_ERROR;
68 fclose(f);
69 }
70 else
71 rc = VERR_ACCESS_DENIED;
72
73 return rc;
74}
75
76int CollectorLinux::getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total)
77{
78 int rc = VINF_SUCCESS;
79 uint64_t uHostUser, uHostKernel, uHostIdle;
80
81 rc = getRawHostCpuLoad(&uHostUser, &uHostKernel, &uHostIdle);
82 if (RT_SUCCESS(rc))
83 {
84 ULONG ulTmp;
85 *total = (uint64_t)uHostUser + uHostKernel + uHostIdle;
86 rc = getRawProcessStats(process, user, kernel, &ulTmp);
87 }
88
89 return rc;
90}
91
92int CollectorLinux::getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available)
93{
94 int rc = VINF_SUCCESS;
95 ULONG buffers, cached;
96 FILE *f = fopen("/proc/meminfo", "r");
97
98 if (f)
99 {
100 int processed = fscanf(f, "MemTotal: %u kB\n", total);
101 processed += fscanf(f, "MemFree: %u kB\n", available);
102 processed += fscanf(f, "Buffers: %u kB\n", &buffers);
103 processed += fscanf(f, "Cached: %u kB\n", &cached);
104 if (processed == 4)
105 {
106 *available += buffers + cached;
107 *used = *total - *available;
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::getProcessMemoryUsage(RTPROCESS process, ULONG *used)
120{
121 uint64_t u64Tmp;
122 ULONG nPagesUsed;
123 int rc = getRawProcessStats(process, &u64Tmp, &u64Tmp, &nPagesUsed);
124 if (RT_SUCCESS(rc))
125 {
126 Assert(PAGE_SIZE >= 1024);
127 *used = nPagesUsed * (PAGE_SIZE / 1024);
128 }
129 return rc;
130}
131
132int CollectorLinux::getRawProcessStats(RTPROCESS process, uint64_t *cpuUser, uint64_t *cpuKernel, ULONG *memPagesUsed)
133{
134 int rc = VINF_SUCCESS;
135 char *pszName;
136 pid_t pid2;
137 char c;
138 int iTmp;
139 long long unsigned int u64Tmp;
140 unsigned uTmp;
141 unsigned long ulTmp;
142 ULONG u32user, u32kernel;
143 char buf[80]; /* @todo: this should be tied to max allowed proc name. */
144
145 RTStrAPrintf(&pszName, "/proc/%d/stat", process);
146 //printf("Opening %s...\n", pszName);
147 FILE *f = fopen(pszName, "r");
148 RTMemFree(pszName);
149
150 if (f)
151 {
152 if (fscanf(f, "%d %s %c %d %d %d %d %d %u %lu %lu %lu %lu %u %u "
153 "%ld %ld %ld %ld %ld %ld %llu %lu %u",
154 &pid2, buf, &c, &iTmp, &iTmp, &iTmp, &iTmp, &iTmp, &uTmp,
155 &ulTmp, &ulTmp, &ulTmp, &ulTmp, &u32user, &u32kernel,
156 &ulTmp, &ulTmp, &ulTmp, &ulTmp, &ulTmp, &ulTmp, &u64Tmp,
157 &ulTmp, memPagesUsed) == 24)
158 {
159 Assert((pid_t)process == pid2);
160 *cpuUser = u32user;
161 *cpuKernel = u32kernel;
162 }
163 else
164 rc = VERR_FILE_IO_ERROR;
165 fclose(f);
166 }
167 else
168 rc = VERR_ACCESS_DENIED;
169
170 return rc;
171}
172
173}
174
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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