Line data Source code
1 : /*
2 : * If not stated otherwise in this file or this component's LICENSE file the
3 : * following copyright and licenses apply:
4 : *
5 : * Copyright 2026 Sky UK
6 : *
7 : * Licensed under the Apache License, Version 2.0 (the "License");
8 : * you may not use this file except in compliance with the License.
9 : * You may obtain a copy of the License at
10 : *
11 : * http://www.apache.org/licenses/LICENSE-2.0
12 : *
13 : * Unless required by applicable law or agreed to in writing, software
14 : * distributed under the License is distributed on an "AS IS" BASIS,
15 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 : * See the License for the specific language governing permissions and
17 : * limitations under the License.
18 : */
19 :
20 : #ifndef FIREBOLT_RIALTO_SERVER_STATE_METRICS_AGGREGATOR_H_
21 : #define FIREBOLT_RIALTO_SERVER_STATE_METRICS_AGGREGATOR_H_
22 :
23 : #include "MetricsAccumulator.h"
24 : #include <cstdint>
25 : #include <string>
26 :
27 : namespace firebolt::rialto::server
28 : {
29 : /**
30 : * @brief A single metrics sample to be fed into the aggregator.
31 : */
32 : struct MetricsSample
33 : {
34 : double clientCpuPercent{0.0};
35 : double serverCpuPercent{0.0};
36 : double combinedCpuPercent{0.0};
37 : std::uint64_t clientMemoryKb{0};
38 : std::uint64_t serverMemoryKb{0};
39 : std::uint64_t cgroupMemoryUsageKb{0};
40 : std::uint64_t cgroupMemoryLimitKb{0};
41 : };
42 :
43 : /**
44 : * @brief Aggregated statistics report produced when a state is finalized.
45 : */
46 : struct StateMetricsReport
47 : {
48 : std::string stateName;
49 : std::uint64_t durationMs{0};
50 : MetricsStatistics clientCpu;
51 : MetricsStatistics serverCpu;
52 : MetricsStatistics combinedCpu;
53 : MetricsStatistics clientMemoryKb;
54 : MetricsStatistics serverMemoryKb;
55 : MetricsStatistics cgroupMemoryUsageKb;
56 : MetricsStatistics cgroupMemoryLimitKb;
57 : };
58 :
59 : /**
60 : * @brief Accumulates metrics samples for a single state period and produces a
61 : * statistical report on finalization.
62 : */
63 : class StateMetricsAggregator
64 : {
65 : public:
66 6 : StateMetricsAggregator() = default;
67 8 : ~StateMetricsAggregator() = default;
68 :
69 7 : void begin(const std::string &stateName, std::uint64_t monotonicTimeMs)
70 : {
71 7 : reset();
72 7 : m_stateName = stateName;
73 7 : m_startTimeMs = monotonicTimeMs;
74 : }
75 :
76 5 : void addSample(const MetricsSample &sample)
77 : {
78 5 : m_clientCpu.addSample(sample.clientCpuPercent);
79 5 : m_serverCpu.addSample(sample.serverCpuPercent);
80 5 : m_combinedCpu.addSample(sample.combinedCpuPercent);
81 5 : m_clientMemory.addSample(static_cast<double>(sample.clientMemoryKb));
82 5 : m_serverMemory.addSample(static_cast<double>(sample.serverMemoryKb));
83 5 : m_cgroupUsage.addSample(static_cast<double>(sample.cgroupMemoryUsageKb));
84 5 : m_cgroupLimit.addSample(static_cast<double>(sample.cgroupMemoryLimitKb));
85 : }
86 :
87 5 : StateMetricsReport finalize(std::uint64_t monotonicTimeMs) const
88 : {
89 5 : StateMetricsReport report;
90 5 : report.stateName = m_stateName;
91 5 : report.durationMs = (monotonicTimeMs > m_startTimeMs) ? (monotonicTimeMs - m_startTimeMs) : 0;
92 5 : report.clientCpu = m_clientCpu.getStats();
93 5 : report.serverCpu = m_serverCpu.getStats();
94 5 : report.combinedCpu = m_combinedCpu.getStats();
95 5 : report.clientMemoryKb = m_clientMemory.getStats();
96 5 : report.serverMemoryKb = m_serverMemory.getStats();
97 5 : report.cgroupMemoryUsageKb = m_cgroupUsage.getStats();
98 5 : report.cgroupMemoryLimitKb = m_cgroupLimit.getStats();
99 5 : return report;
100 : }
101 :
102 9 : void reset()
103 : {
104 9 : m_stateName.clear();
105 9 : m_startTimeMs = 0;
106 9 : m_clientCpu.reset();
107 9 : m_serverCpu.reset();
108 9 : m_combinedCpu.reset();
109 9 : m_clientMemory.reset();
110 9 : m_serverMemory.reset();
111 9 : m_cgroupUsage.reset();
112 9 : m_cgroupLimit.reset();
113 : }
114 :
115 7 : bool hasData() const { return m_clientCpu.getCount() > 0; }
116 1 : const std::string &getStateName() const { return m_stateName; }
117 :
118 : private:
119 : std::string m_stateName;
120 : std::uint64_t m_startTimeMs{0};
121 : MetricsAccumulator m_clientCpu;
122 : MetricsAccumulator m_serverCpu;
123 : MetricsAccumulator m_combinedCpu;
124 : MetricsAccumulator m_clientMemory;
125 : MetricsAccumulator m_serverMemory;
126 : MetricsAccumulator m_cgroupUsage;
127 : MetricsAccumulator m_cgroupLimit;
128 : };
129 : } // namespace firebolt::rialto::server
130 :
131 : #endif // FIREBOLT_RIALTO_SERVER_STATE_METRICS_AGGREGATOR_H_
|