summaryrefslogtreecommitdiffstats
path: root/src/VBox/Main/include/PerformanceImpl.h
blob: bc01d8a31a88378649cfeaefeba1ab39d391ddf0 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* $Id: PerformanceImpl.h $ */

/** @file
 *
 * VBox Performance COM class implementation.
 */

/*
 * 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
 */

#ifndef MAIN_INCLUDED_PerformanceImpl_h
#define MAIN_INCLUDED_PerformanceImpl_h
#ifndef RT_WITHOUT_PRAGMA_ONCE
# pragma once
#endif

#include "PerformanceCollectorWrap.h"
#include "PerformanceMetricWrap.h"

#include <VBox/com/com.h>
#include <VBox/com/array.h>
//#ifdef VBOX_WITH_RESOURCE_USAGE_API
#include <iprt/timer.h>
//#endif /* VBOX_WITH_RESOURCE_USAGE_API */

#include <list>

namespace pm
{
    class Metric;
    class BaseMetric;
    class CollectorHAL;
    class CollectorGuest;
    class CollectorGuestManager;
}

#undef min
#undef max

/* Each second we obtain new CPU load stats. */
#define VBOX_USAGE_SAMPLER_MIN_INTERVAL 1000

class ATL_NO_VTABLE PerformanceMetric :
    public PerformanceMetricWrap
{
public:

    DECLARE_COMMON_CLASS_METHODS(PerformanceMetric)

    HRESULT FinalConstruct();
    void FinalRelease();

    // public initializer/uninitializer for internal purposes only
    HRESULT init(pm::Metric *aMetric);
    HRESULT init(pm::BaseMetric *aMetric);
    void uninit();

private:

    // wrapped IPerformanceMetric properties
    HRESULT getMetricName(com::Utf8Str &aMetricName);
    HRESULT getObject(ComPtr<IUnknown> &aObject);
    HRESULT getDescription(com::Utf8Str &aDescription);
    HRESULT getPeriod(ULONG *aPeriod);
    HRESULT getCount(ULONG *aCount);
    HRESULT getUnit(com::Utf8Str &aUnit);
    HRESULT getMinimumValue(LONG *aMinimumValue);
    HRESULT getMaximumValue(LONG *aMaximumValue);

    struct Data
    {
        /* Constructor. */
        Data()
            : period(0), count(0), min(0), max(0)
        {
        }

        Utf8Str          name;
        ComPtr<IUnknown> object;
        Utf8Str          description;
        ULONG            period;
        ULONG            count;
        Utf8Str          unit;
        LONG             min;
        LONG             max;
    };

    Data m;
};


class ATL_NO_VTABLE PerformanceCollector :
    public PerformanceCollectorWrap
{
public:

    DECLARE_COMMON_CLASS_METHODS(PerformanceCollector)

    HRESULT FinalConstruct();
    void FinalRelease();

    // public initializers/uninitializers only for internal purposes
    HRESULT init();
    void uninit();

    // public methods only for internal purposes

    void registerBaseMetric(pm::BaseMetric *baseMetric);
    void registerMetric(pm::Metric *metric);
    void unregisterBaseMetricsFor(const ComPtr<IUnknown> &object, const Utf8Str name = "*");
    void unregisterMetricsFor(const ComPtr<IUnknown> &object, const Utf8Str name = "*");
    void registerGuest(pm::CollectorGuest* pGuest);
    void unregisterGuest(pm::CollectorGuest* pGuest);

    void suspendSampling();
    void resumeSampling();

    // public methods for internal purposes only
    // (ensure there is a caller and a read lock before calling them!)

    pm::CollectorHAL          *getHAL()          { return m.hal; };
    pm::CollectorGuestManager *getGuestManager() { return m.gm; };

private:

    // wrapped IPerformanceCollector properties
    HRESULT getMetricNames(std::vector<com::Utf8Str> &aMetricNames);

    // wrapped IPerformanceCollector methods
    HRESULT getMetrics(const std::vector<com::Utf8Str> &aMetricNames,
                       const std::vector<ComPtr<IUnknown> > &aObjects,
                       std::vector<ComPtr<IPerformanceMetric> > &aMetrics);
    HRESULT setupMetrics(const std::vector<com::Utf8Str> &aMetricNames,
                         const std::vector<ComPtr<IUnknown> > &aObjects,
                         ULONG aPeriod,
                         ULONG aCount,
                         std::vector<ComPtr<IPerformanceMetric> > &aAffectedMetrics);
    HRESULT enableMetrics(const std::vector<com::Utf8Str> &aMetricNames,
                          const std::vector<ComPtr<IUnknown> > &aObjects,
                          std::vector<ComPtr<IPerformanceMetric> > &aAffectedMetrics);
    HRESULT disableMetrics(const std::vector<com::Utf8Str> &aMetricNames,
                           const std::vector<ComPtr<IUnknown> > &aObjects,
                           std::vector<ComPtr<IPerformanceMetric> > &aAffectedMetrics);
    HRESULT queryMetricsData(const std::vector<com::Utf8Str> &aMetricNames,
                             const std::vector<ComPtr<IUnknown> > &aObjects,
                             std::vector<com::Utf8Str> &aReturnMetricNames,
                             std::vector<ComPtr<IUnknown> > &aReturnObjects,
                             std::vector<com::Utf8Str> &aReturnUnits,
                             std::vector<ULONG> &aReturnScales,
                             std::vector<ULONG> &aReturnSequenceNumbers,
                             std::vector<ULONG> &aReturnDataIndices,
                             std::vector<ULONG> &aReturnDataLengths,
                             std::vector<LONG> &aReturnData);


    HRESULT toIPerformanceMetric(pm::Metric *src, ComPtr<IPerformanceMetric> &dst);
    HRESULT toIPerformanceMetric(pm::BaseMetric *src, ComPtr<IPerformanceMetric> &dst);

    static DECLCALLBACK(void) staticSamplerCallback(RTTIMERLR hTimerLR, void *pvUser, uint64_t iTick);
    void samplerCallback(uint64_t iTick);

    const Utf8Str& getFailedGuestName();

    typedef std::list<pm::Metric*> MetricList;
    typedef std::list<pm::BaseMetric*> BaseMetricList;

/** PerformanceMetric::mMagic value. */
#define PERFORMANCE_METRIC_MAGIC    UINT32_C(0xABBA1972)
    uint32_t mMagic;
    const Utf8Str mUnknownGuest;

    struct Data
    {
        Data() : hal(0) {};

        BaseMetricList             baseMetrics;
        MetricList                 metrics;
        RTTIMERLR                  sampler;
        pm::CollectorHAL          *hal;
        pm::CollectorGuestManager *gm;
    };

    Data m;
};

#endif /* !MAIN_INCLUDED_PerformanceImpl_h */
/* vi: set tabstop=4 shiftwidth=4 expandtab: */