blob: f2b6968fbfb6689752e1c5e1b260ab8f2acde21f [file] [log] [blame]
Georgios Katsikas83600982017-05-28 20:41:45 +02001/*
2 * Copyright 2017-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.drivers.server.impl.stats;
18
19import org.onosproject.drivers.server.stats.CpuStatistics;
Georgios Katsikasfda66742018-07-31 20:18:14 +020020import org.onosproject.drivers.server.stats.MonitoringUnit;
Georgios Katsikas83600982017-05-28 20:41:45 +020021
22import org.onosproject.net.DeviceId;
23import com.google.common.base.MoreObjects;
24
Georgios Katsikasfda66742018-07-31 20:18:14 +020025import java.util.Optional;
26
27import static org.onosproject.drivers.server.stats.MonitoringUnit.LatencyUnit;
28import static org.onosproject.drivers.server.stats.MonitoringUnit.ThroughputUnit;
Georgios Katsikas83600982017-05-28 20:41:45 +020029import static com.google.common.base.Preconditions.checkNotNull;
30import static com.google.common.base.Preconditions.checkArgument;
31
32/**
33 * Default implementation for CPU statistics.
34 */
35public final class DefaultCpuStatistics implements CpuStatistics {
36
37 private static final float MIN_CPU_LOAD = (float) 0.0;
38 private static final float MAX_CPU_LOAD = (float) 1.0;
39
Georgios Katsikasfda66742018-07-31 20:18:14 +020040 private static final LatencyUnit DEF_LATENCY_UNIT = LatencyUnit.NANO_SECOND;
41 private static final ThroughputUnit DEF_THROUGHPUT_UNIT = ThroughputUnit.MBPS;
42
Georgios Katsikas83600982017-05-28 20:41:45 +020043 // Upper limit of CPU cores in one machine
44 public static final int MAX_CPU_NB = 512;
45
46 private final DeviceId deviceId;
47
48 private final int id;
49 private final float load;
Georgios Katsikasfda66742018-07-31 20:18:14 +020050 private final int queue;
Georgios Katsikas6dc11c12018-12-20 08:43:29 +010051 private final int busySince;
Georgios Katsikasfda66742018-07-31 20:18:14 +020052 private final Optional<MonitoringUnit> throughputUnit;
53 private final Optional<Float> averageThroughput;
54 private final Optional<MonitoringUnit> latencyUnit;
55 private final Optional<Float> minLatency;
Georgios Katsikas973a2652018-06-28 08:45:47 +020056 private final Optional<Float> averageLatency;
Georgios Katsikasfda66742018-07-31 20:18:14 +020057 private final Optional<Float> maxLatency;
Georgios Katsikas83600982017-05-28 20:41:45 +020058
Georgios Katsikas6dc11c12018-12-20 08:43:29 +010059 private DefaultCpuStatistics(DeviceId deviceId, int id, float load, int queue, int busySince) {
60 this(deviceId, id, load, queue, busySince, null, -1, null, -1, -1, -1);
Georgios Katsikasfda66742018-07-31 20:18:14 +020061 }
62
Georgios Katsikas6dc11c12018-12-20 08:43:29 +010063 private DefaultCpuStatistics(DeviceId deviceId, int id, float load, int queue, int busySince,
Georgios Katsikasfda66742018-07-31 20:18:14 +020064 MonitoringUnit throughputUnit, float averageThroughput, MonitoringUnit latencyUnit,
Georgios Katsikas973a2652018-06-28 08:45:47 +020065 float minLatency, float averageLatency, float maxLatency) {
Georgios Katsikas83600982017-05-28 20:41:45 +020066 checkNotNull(deviceId, "Device ID is NULL");
Georgios Katsikasfda66742018-07-31 20:18:14 +020067 checkArgument((id >= 0) && (id < MAX_CPU_NB),
68 "Invalid CPU core ID " + String.valueOf(id) + ", not in [0, " + String.valueOf(MAX_CPU_NB - 1) + "]");
69 checkArgument((load >= MIN_CPU_LOAD) && (load <= MAX_CPU_LOAD),
70 "Invalid CPU load " + Float.toString(load) + ", not in [" + MIN_CPU_LOAD + ", " + MAX_CPU_LOAD + "]");
Georgios Katsikas83600982017-05-28 20:41:45 +020071
Georgios Katsikas6dc11c12018-12-20 08:43:29 +010072 this.deviceId = deviceId;
73 this.id = id;
74 this.load = load;
75 this.queue = queue;
76 this.busySince = busySince;
Georgios Katsikasfda66742018-07-31 20:18:14 +020077
78 this.throughputUnit = (throughputUnit == null) ?
79 Optional.empty() : Optional.ofNullable(throughputUnit);
80 this.averageThroughput = (averageThroughput < 0) ?
81 Optional.empty() : Optional.ofNullable(averageThroughput);
82 this.latencyUnit = (latencyUnit == null) ?
83 Optional.empty() : Optional.ofNullable(latencyUnit);
84 this.minLatency = (minLatency < 0) ?
85 Optional.empty() : Optional.ofNullable(minLatency);
Georgios Katsikas973a2652018-06-28 08:45:47 +020086 this.averageLatency = (averageLatency < 0) ?
87 Optional.empty() : Optional.ofNullable(averageLatency);
Georgios Katsikasfda66742018-07-31 20:18:14 +020088 this.maxLatency = (maxLatency < 0) ?
89 Optional.empty() : Optional.ofNullable(maxLatency);
Georgios Katsikas83600982017-05-28 20:41:45 +020090 }
91
92 // Constructor for serializer
93 private DefaultCpuStatistics() {
Georgios Katsikas6dc11c12018-12-20 08:43:29 +010094 this.deviceId = null;
95 this.id = 0;
96 this.load = 0;
97 this.queue = 0;
98 this.busySince = -1;
Georgios Katsikasfda66742018-07-31 20:18:14 +020099
100 this.throughputUnit = null;
101 this.averageThroughput = null;
102 this.latencyUnit = null;
103 this.minLatency = null;
Georgios Katsikas973a2652018-06-28 08:45:47 +0200104 this.averageLatency = null;
Georgios Katsikasfda66742018-07-31 20:18:14 +0200105 this.maxLatency = null;
Georgios Katsikas83600982017-05-28 20:41:45 +0200106 }
107
108 /**
109 * Creates a builder for DefaultCpuStatistics object.
110 *
111 * @return builder object for DefaultCpuStatistics object
112 */
113 public static DefaultCpuStatistics.Builder builder() {
114 return new Builder();
115 }
116
117 @Override
118 public int id() {
119 return this.id;
120 }
121
122 @Override
123 public float load() {
124 return this.load;
125 }
126
127 @Override
Georgios Katsikasfda66742018-07-31 20:18:14 +0200128 public int queue() {
129 return this.queue;
130 }
131
132 @Override
Georgios Katsikas83600982017-05-28 20:41:45 +0200133 public boolean busy() {
Georgios Katsikas6dc11c12018-12-20 08:43:29 +0100134 return this.busySince >= 0;
135 }
136
137 @Override
138 public int busySince() {
139 return this.busySince;
Georgios Katsikas83600982017-05-28 20:41:45 +0200140 }
141
142 @Override
Georgios Katsikasfda66742018-07-31 20:18:14 +0200143 public Optional<MonitoringUnit> throughputUnit() {
144 return this.throughputUnit;
145 }
146
147 @Override
148 public Optional<Float> averageThroughput() {
149 return this.averageThroughput;
150 }
151
152 @Override
153 public Optional<MonitoringUnit> latencyUnit() {
154 return this.latencyUnit;
155 }
156
157 @Override
158 public Optional<Float> minLatency() {
159 return this.minLatency;
160 }
161
162 @Override
Georgios Katsikas973a2652018-06-28 08:45:47 +0200163 public Optional<Float> averageLatency() {
164 return this.averageLatency;
Georgios Katsikasfda66742018-07-31 20:18:14 +0200165 }
166
167 @Override
168 public Optional<Float> maxLatency() {
169 return this.maxLatency;
170 }
171
172 @Override
Georgios Katsikas83600982017-05-28 20:41:45 +0200173 public String toString() {
174 return MoreObjects.toStringHelper(this)
175 .omitNullValues()
176 .add("device", deviceId)
177 .add("id", id())
178 .add("load", load())
Georgios Katsikasfda66742018-07-31 20:18:14 +0200179 .add("queue", queue())
Georgios Katsikas6dc11c12018-12-20 08:43:29 +0100180 .add("busySince", busySince())
Georgios Katsikasfda66742018-07-31 20:18:14 +0200181 .add("throughputUnit", throughputUnit.orElse(null))
182 .add("averageThroughput", averageThroughput.orElse(null))
183 .add("latencyUnit", latencyUnit.orElse(null))
184 .add("minLatency", minLatency.orElse(null))
Georgios Katsikas973a2652018-06-28 08:45:47 +0200185 .add("averageLatency", averageLatency.orElse(null))
Georgios Katsikasfda66742018-07-31 20:18:14 +0200186 .add("maxLatency", maxLatency.orElse(null))
Georgios Katsikas83600982017-05-28 20:41:45 +0200187 .toString();
188 }
189
190 public static final class Builder {
191
192 DeviceId deviceId;
193 int id;
Georgios Katsikasfda66742018-07-31 20:18:14 +0200194 float load = 0;
195 int queue = -1;
Georgios Katsikas6dc11c12018-12-20 08:43:29 +0100196 int busySince = -1;
Georgios Katsikasfda66742018-07-31 20:18:14 +0200197
198 MonitoringUnit throughputUnit = DEF_THROUGHPUT_UNIT;
199 float averageThroughput = -1;
200 MonitoringUnit latencyUnit = DEF_LATENCY_UNIT;
201 float minLatency = -1;
Georgios Katsikas973a2652018-06-28 08:45:47 +0200202 float averageLatency = -1;
Georgios Katsikasfda66742018-07-31 20:18:14 +0200203 float maxLatency = -1;
Georgios Katsikas83600982017-05-28 20:41:45 +0200204
205 private Builder() {
206
207 }
208
209 /**
210 * Sets the device identifier.
211 *
212 * @param deviceId device identifier
213 * @return builder object
214 */
215 public Builder setDeviceId(DeviceId deviceId) {
216 this.deviceId = deviceId;
217
218 return this;
219 }
220
221 /**
222 * Sets the CPU ID.
223 *
Georgios Katsikasfda66742018-07-31 20:18:14 +0200224 * @param id CPU ID
Georgios Katsikas83600982017-05-28 20:41:45 +0200225 * @return builder object
226 */
227 public Builder setId(int id) {
228 this.id = id;
229
230 return this;
231 }
232
233 /**
234 * Sets the CPU load.
235 *
236 * @param load CPU load
237 * @return builder object
238 */
239 public Builder setLoad(float load) {
240 this.load = load;
241
242 return this;
243 }
244
245 /**
Georgios Katsikasfda66742018-07-31 20:18:14 +0200246 * Sets the hardware queue ID associated with this core.
247 *
248 * @param queue hardware queue ID
249 * @return builder object
250 */
251 public Builder setQueue(int queue) {
252 this.queue = queue;
253
254 return this;
255 }
256
257 /**
Georgios Katsikas6dc11c12018-12-20 08:43:29 +0100258 * Sets the CPU status (free or busy since some ms).
Georgios Katsikas83600982017-05-28 20:41:45 +0200259 *
Georgios Katsikas6dc11c12018-12-20 08:43:29 +0100260 * @param busySince CPU busy time in ms, -1 if not busy
Georgios Katsikas83600982017-05-28 20:41:45 +0200261 * @return builder object
262 */
Georgios Katsikas6dc11c12018-12-20 08:43:29 +0100263 public Builder setBusySince(int busySince) {
264 this.busySince = busySince;
Georgios Katsikas83600982017-05-28 20:41:45 +0200265
266 return this;
267 }
268
269 /**
Georgios Katsikasfda66742018-07-31 20:18:14 +0200270 * Sets the throughput unit.
271 *
272 * @param throughputUnitStr throughput unit as a string
273 * @return builder object
274 */
275 public Builder setThroughputUnit(String throughputUnitStr) {
276 this.throughputUnit = ThroughputUnit.getByName(throughputUnitStr);
277
278 return this;
279 }
280
281 /**
282 * Sets the average throughput.
283 *
284 * @param averageThroughput average throughput
285 * @return builder object
286 */
287 public Builder setAverageThroughput(float averageThroughput) {
288 this.averageThroughput = averageThroughput;
289
290 return this;
291 }
292
293 /**
294 * Sets the latency unit.
295 *
296 * @param latencyUnitStr latency unit as a string
297 * @return builder object
298 */
299 public Builder setLatencyUnit(String latencyUnitStr) {
300 this.latencyUnit = LatencyUnit.getByName(latencyUnitStr);
301
302 return this;
303 }
304
305 /**
306 * Sets the minimum latency.
307 *
308 * @param minLatency minimum latency
309 * @return builder object
310 */
311 public Builder setMinLatency(float minLatency) {
312 this.minLatency = minLatency;
313
314 return this;
315 }
316
317 /**
Georgios Katsikas973a2652018-06-28 08:45:47 +0200318 * Sets the average latency.
Georgios Katsikasfda66742018-07-31 20:18:14 +0200319 *
Georgios Katsikas973a2652018-06-28 08:45:47 +0200320 * @param averageLatency average latency
Georgios Katsikasfda66742018-07-31 20:18:14 +0200321 * @return builder object
322 */
Georgios Katsikas973a2652018-06-28 08:45:47 +0200323 public Builder setAverageLatency(float averageLatency) {
324 this.averageLatency = averageLatency;
Georgios Katsikasfda66742018-07-31 20:18:14 +0200325
326 return this;
327 }
328
329 /**
330 * Sets the maximum latency.
331 *
332 * @param maxLatency maximum latency
333 * @return builder object
334 */
335 public Builder setMaxLatency(float maxLatency) {
336 this.maxLatency = maxLatency;
337
338 return this;
339 }
340
341 /**
Georgios Katsikas83600982017-05-28 20:41:45 +0200342 * Creates a DefaultCpuStatistics object.
343 *
344 * @return DefaultCpuStatistics object
345 */
346 public DefaultCpuStatistics build() {
347 return new DefaultCpuStatistics(
Georgios Katsikas6dc11c12018-12-20 08:43:29 +0100348 deviceId, id, load, queue, busySince,
Georgios Katsikasfda66742018-07-31 20:18:14 +0200349 throughputUnit, averageThroughput,
Georgios Katsikas973a2652018-06-28 08:45:47 +0200350 latencyUnit, minLatency, averageLatency, maxLatency);
Georgios Katsikas83600982017-05-28 20:41:45 +0200351 }
352 }
353
354}