1 | /* $Id: PerformanceFreeBSD.cpp 23150 2009-09-18 22:36:15Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox Performance Collector, FreeBSD Specialization.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2009 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #include <sys/types.h>
|
---|
23 | #include <sys/sysctl.h>
|
---|
24 | #include "Performance.h"
|
---|
25 |
|
---|
26 | namespace pm {
|
---|
27 |
|
---|
28 | class CollectorFreeBSD : public CollectorHAL
|
---|
29 | {
|
---|
30 | public:
|
---|
31 | virtual int getHostCpuLoad(ULONG *user, ULONG *kernel, ULONG *idle);
|
---|
32 | virtual int getHostCpuMHz(ULONG *mhz);
|
---|
33 | virtual int getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available);
|
---|
34 | virtual int getProcessCpuLoad(RTPROCESS process, ULONG *user, ULONG *kernel);
|
---|
35 | virtual int getProcessMemoryUsage(RTPROCESS process, ULONG *used);
|
---|
36 | };
|
---|
37 |
|
---|
38 |
|
---|
39 | CollectorHAL *createHAL()
|
---|
40 | {
|
---|
41 | return new CollectorFreeBSD();
|
---|
42 | }
|
---|
43 |
|
---|
44 | int CollectorFreeBSD::getHostCpuLoad(ULONG *user, ULONG *kernel, ULONG *idle)
|
---|
45 | {
|
---|
46 | return E_NOTIMPL;
|
---|
47 | }
|
---|
48 |
|
---|
49 | int CollectorFreeBSD::getHostCpuMHz(ULONG *mhz)
|
---|
50 | {
|
---|
51 | int CpuMHz = 0;
|
---|
52 | size_t cbParameter = sizeof(CpuMHz);
|
---|
53 |
|
---|
54 | /** @todo: Howto support more than one CPU? */
|
---|
55 | if (sysctlbyname("dev.cpu.0.freq", &CpuMHz, &cbParameter, NULL, 0))
|
---|
56 | return VERR_NOT_SUPPORTED;
|
---|
57 |
|
---|
58 | *mhz = CpuMHz;
|
---|
59 |
|
---|
60 | return VINF_SUCCESS;
|
---|
61 | }
|
---|
62 |
|
---|
63 | int CollectorFreeBSD::getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available)
|
---|
64 | {
|
---|
65 | int rc = VINF_SUCCESS;
|
---|
66 | u_long cbMemPhys = 0;
|
---|
67 | u_int cPagesMemFree = 0;
|
---|
68 | u_int cPagesMemInactive = 0;
|
---|
69 | u_int cPagesMemCached = 0;
|
---|
70 | u_int cPagesMemUsed = 0;
|
---|
71 | int cbPage = 0;
|
---|
72 | size_t cbParameter = sizeof(cbMemPhys);
|
---|
73 | int cProcessed = 0;
|
---|
74 |
|
---|
75 | if (!sysctlbyname("hw.physmem", &cbMemPhys, &cbParameter, NULL, 0))
|
---|
76 | cProcessed++;
|
---|
77 |
|
---|
78 | cbParameter = sizeof(cPagesMemFree);
|
---|
79 | if (!sysctlbyname("vm.stats.vm.v_free_count", &cPagesMemFree, &cbParameter, NULL, 0))
|
---|
80 | cProcessed++;
|
---|
81 | cbParameter = sizeof(cPagesMemUsed);
|
---|
82 | if (!sysctlbyname("vm.stats.vm.v_active_count", &cPagesMemUsed, &cbParameter, NULL, 0))
|
---|
83 | cProcessed++;
|
---|
84 | cbParameter = sizeof(cPagesMemInactive);
|
---|
85 | if (!sysctlbyname("vm.stats.vm.v_inactive_count", &cPagesMemInactive, &cbParameter, NULL, 0))
|
---|
86 | cProcessed++;
|
---|
87 | cbParameter = sizeof(cPagesMemCached);
|
---|
88 | if (!sysctlbyname("vm.stats.vm.v_cache_count", &cPagesMemCached, &cbParameter, NULL, 0))
|
---|
89 | cProcessed++;
|
---|
90 | cbParameter = sizeof(cbPage);
|
---|
91 | if (!sysctlbyname("hw.pagesize", &cbPage, &cbParameter, NULL, 0))
|
---|
92 | cProcessed++;
|
---|
93 |
|
---|
94 | if (cProcessed == 6)
|
---|
95 | {
|
---|
96 | *total = cbMemPhys / _1K;
|
---|
97 | *used = cPagesMemUsed * (cbPage / _1K);
|
---|
98 | *available = (cPagesMemFree + cPagesMemInactive + cPagesMemCached ) * (cbPage / _1K);
|
---|
99 | }
|
---|
100 | else
|
---|
101 | rc = VERR_NOT_SUPPORTED;
|
---|
102 |
|
---|
103 | return rc;
|
---|
104 | }
|
---|
105 |
|
---|
106 | int CollectorFreeBSD::getProcessCpuLoad(RTPROCESS process, ULONG *user, ULONG *kernel)
|
---|
107 | {
|
---|
108 | return E_NOTIMPL;
|
---|
109 | }
|
---|
110 |
|
---|
111 | int CollectorFreeBSD::getProcessMemoryUsage(RTPROCESS process, ULONG *used)
|
---|
112 | {
|
---|
113 | return E_NOTIMPL;
|
---|
114 | }
|
---|
115 |
|
---|
116 | } /* namespace pm */
|
---|
117 |
|
---|