blob: 2f5305d8409509503d96c0dcba31f61fddbea9b2 [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 Katsikas83600982017-05-28 20:41:45 +020051 private final boolean isBusy;
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;
56 private final Optional<Float> medianLatency;
57 private final Optional<Float> maxLatency;
Georgios Katsikas83600982017-05-28 20:41:45 +020058
Georgios Katsikasfda66742018-07-31 20:18:14 +020059 private DefaultCpuStatistics(DeviceId deviceId, int id, float load, int queue, boolean isBusy) {
60 this(deviceId, id, load, queue, isBusy, null, -1, null, -1, -1, -1);
61 }
62
63 private DefaultCpuStatistics(DeviceId deviceId, int id, float load, int queue, boolean isBusy,
64 MonitoringUnit throughputUnit, float averageThroughput, MonitoringUnit latencyUnit,
65 float minLatency, float medianLatency, 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
72 this.deviceId = deviceId;
73 this.id = id;
74 this.load = load;
Georgios Katsikasfda66742018-07-31 20:18:14 +020075 this.queue = queue;
Georgios Katsikas83600982017-05-28 20:41:45 +020076 this.isBusy = isBusy;
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);
86 this.medianLatency = (medianLatency < 0) ?
87 Optional.empty() : Optional.ofNullable(medianLatency);
88 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() {
94 this.deviceId = null;
95 this.id = 0;
96 this.load = 0;
Georgios Katsikasfda66742018-07-31 20:18:14 +020097 this.queue = 0;
Georgios Katsikas83600982017-05-28 20:41:45 +020098 this.isBusy = false;
Georgios Katsikasfda66742018-07-31 20:18:14 +020099
100 this.throughputUnit = null;
101 this.averageThroughput = null;
102 this.latencyUnit = null;
103 this.minLatency = null;
104 this.medianLatency = null;
105 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() {
134 return this.isBusy;
135 }
136
137 @Override
Georgios Katsikasfda66742018-07-31 20:18:14 +0200138 public Optional<MonitoringUnit> throughputUnit() {
139 return this.throughputUnit;
140 }
141
142 @Override
143 public Optional<Float> averageThroughput() {
144 return this.averageThroughput;
145 }
146
147 @Override
148 public Optional<MonitoringUnit> latencyUnit() {
149 return this.latencyUnit;
150 }
151
152 @Override
153 public Optional<Float> minLatency() {
154 return this.minLatency;
155 }
156
157 @Override
158 public Optional<Float> medianLatency() {
159 return this.medianLatency;
160 }
161
162 @Override
163 public Optional<Float> maxLatency() {
164 return this.maxLatency;
165 }
166
167 @Override
Georgios Katsikas83600982017-05-28 20:41:45 +0200168 public String toString() {
169 return MoreObjects.toStringHelper(this)
170 .omitNullValues()
171 .add("device", deviceId)
172 .add("id", id())
173 .add("load", load())
Georgios Katsikasfda66742018-07-31 20:18:14 +0200174 .add("queue", queue())
Georgios Katsikas83600982017-05-28 20:41:45 +0200175 .add("isBusy", busy())
Georgios Katsikasfda66742018-07-31 20:18:14 +0200176 .add("throughputUnit", throughputUnit.orElse(null))
177 .add("averageThroughput", averageThroughput.orElse(null))
178 .add("latencyUnit", latencyUnit.orElse(null))
179 .add("minLatency", minLatency.orElse(null))
180 .add("medianLatency", medianLatency.orElse(null))
181 .add("maxLatency", maxLatency.orElse(null))
Georgios Katsikas83600982017-05-28 20:41:45 +0200182 .toString();
183 }
184
185 public static final class Builder {
186
187 DeviceId deviceId;
188 int id;
Georgios Katsikasfda66742018-07-31 20:18:14 +0200189 float load = 0;
190 int queue = -1;
191 boolean isBusy = false;
192
193 MonitoringUnit throughputUnit = DEF_THROUGHPUT_UNIT;
194 float averageThroughput = -1;
195 MonitoringUnit latencyUnit = DEF_LATENCY_UNIT;
196 float minLatency = -1;
197 float medianLatency = -1;
198 float maxLatency = -1;
Georgios Katsikas83600982017-05-28 20:41:45 +0200199
200 private Builder() {
201
202 }
203
204 /**
205 * Sets the device identifier.
206 *
207 * @param deviceId device identifier
208 * @return builder object
209 */
210 public Builder setDeviceId(DeviceId deviceId) {
211 this.deviceId = deviceId;
212
213 return this;
214 }
215
216 /**
217 * Sets the CPU ID.
218 *
Georgios Katsikasfda66742018-07-31 20:18:14 +0200219 * @param id CPU ID
Georgios Katsikas83600982017-05-28 20:41:45 +0200220 * @return builder object
221 */
222 public Builder setId(int id) {
223 this.id = id;
224
225 return this;
226 }
227
228 /**
229 * Sets the CPU load.
230 *
231 * @param load CPU load
232 * @return builder object
233 */
234 public Builder setLoad(float load) {
235 this.load = load;
236
237 return this;
238 }
239
240 /**
Georgios Katsikasfda66742018-07-31 20:18:14 +0200241 * Sets the hardware queue ID associated with this core.
242 *
243 * @param queue hardware queue ID
244 * @return builder object
245 */
246 public Builder setQueue(int queue) {
247 this.queue = queue;
248
249 return this;
250 }
251
252 /**
Georgios Katsikas83600982017-05-28 20:41:45 +0200253 * Sets the CPU status (free or busy).
254 *
255 * @param isBusy CPU status
256 * @return builder object
257 */
258 public Builder setIsBusy(boolean isBusy) {
259 this.isBusy = isBusy;
260
261 return this;
262 }
263
264 /**
Georgios Katsikasfda66742018-07-31 20:18:14 +0200265 * Sets the throughput unit.
266 *
267 * @param throughputUnitStr throughput unit as a string
268 * @return builder object
269 */
270 public Builder setThroughputUnit(String throughputUnitStr) {
271 this.throughputUnit = ThroughputUnit.getByName(throughputUnitStr);
272
273 return this;
274 }
275
276 /**
277 * Sets the average throughput.
278 *
279 * @param averageThroughput average throughput
280 * @return builder object
281 */
282 public Builder setAverageThroughput(float averageThroughput) {
283 this.averageThroughput = averageThroughput;
284
285 return this;
286 }
287
288 /**
289 * Sets the latency unit.
290 *
291 * @param latencyUnitStr latency unit as a string
292 * @return builder object
293 */
294 public Builder setLatencyUnit(String latencyUnitStr) {
295 this.latencyUnit = LatencyUnit.getByName(latencyUnitStr);
296
297 return this;
298 }
299
300 /**
301 * Sets the minimum latency.
302 *
303 * @param minLatency minimum latency
304 * @return builder object
305 */
306 public Builder setMinLatency(float minLatency) {
307 this.minLatency = minLatency;
308
309 return this;
310 }
311
312 /**
313 * Sets the median latency.
314 *
315 * @param medianLatency median latency
316 * @return builder object
317 */
318 public Builder setMedianLatency(float medianLatency) {
319 this.medianLatency = medianLatency;
320
321 return this;
322 }
323
324 /**
325 * Sets the maximum latency.
326 *
327 * @param maxLatency maximum latency
328 * @return builder object
329 */
330 public Builder setMaxLatency(float maxLatency) {
331 this.maxLatency = maxLatency;
332
333 return this;
334 }
335
336 /**
Georgios Katsikas83600982017-05-28 20:41:45 +0200337 * Creates a DefaultCpuStatistics object.
338 *
339 * @return DefaultCpuStatistics object
340 */
341 public DefaultCpuStatistics build() {
342 return new DefaultCpuStatistics(
Georgios Katsikasfda66742018-07-31 20:18:14 +0200343 deviceId, id, load, queue, isBusy,
344 throughputUnit, averageThroughput,
345 latencyUnit, minLatency, medianLatency, maxLatency);
Georgios Katsikas83600982017-05-28 20:41:45 +0200346 }
347 }
348
349}