summaryrefslogtreecommitdiffstats
path: root/src/VBox/Main/src-server/freebsd/PerformanceFreeBSD.cpp
blob: fc55de1769d742c5a56995dbcce6c3adb5f55138 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* $Id: PerformanceFreeBSD.cpp $ */
/** @file
 * VirtualBox Performance Collector, FreeBSD Specialization.
 */

/*
 * Copyright (C) 2008-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#include <sys/types.h>
#include <sys/sysctl.h>
#include "Performance.h"

namespace pm {

class CollectorFreeBSD : public CollectorHAL
{
public:
    virtual int getHostCpuLoad(ULONG *user, ULONG *kernel, ULONG *idle);
    virtual int getHostCpuMHz(ULONG *mhz);
    virtual int getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available);
    virtual int getProcessCpuLoad(RTPROCESS process, ULONG *user, ULONG *kernel);
    virtual int getProcessMemoryUsage(RTPROCESS process, ULONG *used);
};


CollectorHAL *createHAL()
{
    return new CollectorFreeBSD();
}

int CollectorFreeBSD::getHostCpuLoad(ULONG *user, ULONG *kernel, ULONG *idle)
{
    return VERR_NOT_IMPLEMENTED;
}

int CollectorFreeBSD::getHostCpuMHz(ULONG *mhz)
{
    int CpuMHz = 0;
    size_t cbParameter = sizeof(CpuMHz);

    /** @todo Howto support more than one CPU? */
    if (sysctlbyname("dev.cpu.0.freq", &CpuMHz, &cbParameter, NULL, 0))
        return VERR_NOT_SUPPORTED;

    *mhz = CpuMHz;

    return VINF_SUCCESS;
}

int CollectorFreeBSD::getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available)
{
    int vrc = VINF_SUCCESS;
    u_long cbMemPhys = 0;
    u_int cPagesMemFree = 0;
    u_int cPagesMemInactive = 0;
    u_int cPagesMemCached = 0;
    u_int cPagesMemUsed = 0;
    int cbPage = 0;
    size_t cbParameter = sizeof(cbMemPhys);
    int cProcessed = 0;

    if (!sysctlbyname("hw.physmem", &cbMemPhys, &cbParameter, NULL, 0))
        cProcessed++;

    cbParameter = sizeof(cPagesMemFree);
    if (!sysctlbyname("vm.stats.vm.v_free_count", &cPagesMemFree, &cbParameter, NULL, 0))
        cProcessed++;
    cbParameter = sizeof(cPagesMemUsed);
    if (!sysctlbyname("vm.stats.vm.v_active_count", &cPagesMemUsed, &cbParameter, NULL, 0))
        cProcessed++;
    cbParameter = sizeof(cPagesMemInactive);
    if (!sysctlbyname("vm.stats.vm.v_inactive_count", &cPagesMemInactive, &cbParameter, NULL, 0))
        cProcessed++;
    cbParameter = sizeof(cPagesMemCached);
    if (!sysctlbyname("vm.stats.vm.v_cache_count", &cPagesMemCached, &cbParameter, NULL, 0))
        cProcessed++;
    cbParameter = sizeof(cbPage);
    if (!sysctlbyname("hw.pagesize", &cbPage, &cbParameter, NULL, 0))
        cProcessed++;

    if (cProcessed == 6)
    {
        *total     = cbMemPhys / _1K;
        *used      = cPagesMemUsed * (cbPage / _1K);
        *available = (cPagesMemFree + cPagesMemInactive + cPagesMemCached ) * (cbPage / _1K);
    }
    else
        vrc = VERR_NOT_SUPPORTED;

    return vrc;
}

int CollectorFreeBSD::getProcessCpuLoad(RTPROCESS process, ULONG *user, ULONG *kernel)
{
    return VERR_NOT_IMPLEMENTED;
}

int CollectorFreeBSD::getProcessMemoryUsage(RTPROCESS process, ULONG *used)
{
    return VERR_NOT_IMPLEMENTED;
}

int getDiskListByFs(const char *name, DiskList& list)
{
    return VERR_NOT_IMPLEMENTED;
}

} /* namespace pm */