Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.drivers.server.impl.stats; |
| 18 | |
| 19 | import org.onosproject.drivers.server.stats.CpuStatistics; |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 20 | import org.onosproject.drivers.server.stats.MonitoringUnit; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 21 | |
| 22 | import org.onosproject.net.DeviceId; |
| 23 | import com.google.common.base.MoreObjects; |
| 24 | |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 25 | import java.util.Optional; |
| 26 | |
| 27 | import static org.onosproject.drivers.server.stats.MonitoringUnit.LatencyUnit; |
| 28 | import static org.onosproject.drivers.server.stats.MonitoringUnit.ThroughputUnit; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 29 | import static com.google.common.base.Preconditions.checkNotNull; |
| 30 | import static com.google.common.base.Preconditions.checkArgument; |
| 31 | |
| 32 | /** |
| 33 | * Default implementation for CPU statistics. |
| 34 | */ |
| 35 | public 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 Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 40 | private static final LatencyUnit DEF_LATENCY_UNIT = LatencyUnit.NANO_SECOND; |
| 41 | private static final ThroughputUnit DEF_THROUGHPUT_UNIT = ThroughputUnit.MBPS; |
| 42 | |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 43 | // 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 Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 50 | private final int queue; |
Georgios Katsikas | 6dc11c1 | 2018-12-20 08:43:29 +0100 | [diff] [blame^] | 51 | private final int busySince; |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 52 | private final Optional<MonitoringUnit> throughputUnit; |
| 53 | private final Optional<Float> averageThroughput; |
| 54 | private final Optional<MonitoringUnit> latencyUnit; |
| 55 | private final Optional<Float> minLatency; |
Georgios Katsikas | 973a265 | 2018-06-28 08:45:47 +0200 | [diff] [blame] | 56 | private final Optional<Float> averageLatency; |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 57 | private final Optional<Float> maxLatency; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 58 | |
Georgios Katsikas | 6dc11c1 | 2018-12-20 08:43:29 +0100 | [diff] [blame^] | 59 | 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 Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 61 | } |
| 62 | |
Georgios Katsikas | 6dc11c1 | 2018-12-20 08:43:29 +0100 | [diff] [blame^] | 63 | private DefaultCpuStatistics(DeviceId deviceId, int id, float load, int queue, int busySince, |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 64 | MonitoringUnit throughputUnit, float averageThroughput, MonitoringUnit latencyUnit, |
Georgios Katsikas | 973a265 | 2018-06-28 08:45:47 +0200 | [diff] [blame] | 65 | float minLatency, float averageLatency, float maxLatency) { |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 66 | checkNotNull(deviceId, "Device ID is NULL"); |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 67 | 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 Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 71 | |
Georgios Katsikas | 6dc11c1 | 2018-12-20 08:43:29 +0100 | [diff] [blame^] | 72 | this.deviceId = deviceId; |
| 73 | this.id = id; |
| 74 | this.load = load; |
| 75 | this.queue = queue; |
| 76 | this.busySince = busySince; |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 77 | |
| 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 Katsikas | 973a265 | 2018-06-28 08:45:47 +0200 | [diff] [blame] | 86 | this.averageLatency = (averageLatency < 0) ? |
| 87 | Optional.empty() : Optional.ofNullable(averageLatency); |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 88 | this.maxLatency = (maxLatency < 0) ? |
| 89 | Optional.empty() : Optional.ofNullable(maxLatency); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | // Constructor for serializer |
| 93 | private DefaultCpuStatistics() { |
Georgios Katsikas | 6dc11c1 | 2018-12-20 08:43:29 +0100 | [diff] [blame^] | 94 | this.deviceId = null; |
| 95 | this.id = 0; |
| 96 | this.load = 0; |
| 97 | this.queue = 0; |
| 98 | this.busySince = -1; |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 99 | |
| 100 | this.throughputUnit = null; |
| 101 | this.averageThroughput = null; |
| 102 | this.latencyUnit = null; |
| 103 | this.minLatency = null; |
Georgios Katsikas | 973a265 | 2018-06-28 08:45:47 +0200 | [diff] [blame] | 104 | this.averageLatency = null; |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 105 | this.maxLatency = null; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 106 | } |
| 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 Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 128 | public int queue() { |
| 129 | return this.queue; |
| 130 | } |
| 131 | |
| 132 | @Override |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 133 | public boolean busy() { |
Georgios Katsikas | 6dc11c1 | 2018-12-20 08:43:29 +0100 | [diff] [blame^] | 134 | return this.busySince >= 0; |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public int busySince() { |
| 139 | return this.busySince; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Override |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 143 | 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 Katsikas | 973a265 | 2018-06-28 08:45:47 +0200 | [diff] [blame] | 163 | public Optional<Float> averageLatency() { |
| 164 | return this.averageLatency; |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public Optional<Float> maxLatency() { |
| 169 | return this.maxLatency; |
| 170 | } |
| 171 | |
| 172 | @Override |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 173 | public String toString() { |
| 174 | return MoreObjects.toStringHelper(this) |
| 175 | .omitNullValues() |
| 176 | .add("device", deviceId) |
| 177 | .add("id", id()) |
| 178 | .add("load", load()) |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 179 | .add("queue", queue()) |
Georgios Katsikas | 6dc11c1 | 2018-12-20 08:43:29 +0100 | [diff] [blame^] | 180 | .add("busySince", busySince()) |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 181 | .add("throughputUnit", throughputUnit.orElse(null)) |
| 182 | .add("averageThroughput", averageThroughput.orElse(null)) |
| 183 | .add("latencyUnit", latencyUnit.orElse(null)) |
| 184 | .add("minLatency", minLatency.orElse(null)) |
Georgios Katsikas | 973a265 | 2018-06-28 08:45:47 +0200 | [diff] [blame] | 185 | .add("averageLatency", averageLatency.orElse(null)) |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 186 | .add("maxLatency", maxLatency.orElse(null)) |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 187 | .toString(); |
| 188 | } |
| 189 | |
| 190 | public static final class Builder { |
| 191 | |
| 192 | DeviceId deviceId; |
| 193 | int id; |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 194 | float load = 0; |
| 195 | int queue = -1; |
Georgios Katsikas | 6dc11c1 | 2018-12-20 08:43:29 +0100 | [diff] [blame^] | 196 | int busySince = -1; |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 197 | |
| 198 | MonitoringUnit throughputUnit = DEF_THROUGHPUT_UNIT; |
| 199 | float averageThroughput = -1; |
| 200 | MonitoringUnit latencyUnit = DEF_LATENCY_UNIT; |
| 201 | float minLatency = -1; |
Georgios Katsikas | 973a265 | 2018-06-28 08:45:47 +0200 | [diff] [blame] | 202 | float averageLatency = -1; |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 203 | float maxLatency = -1; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 204 | |
| 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 Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 224 | * @param id CPU ID |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 225 | * @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 Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 246 | * 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 Katsikas | 6dc11c1 | 2018-12-20 08:43:29 +0100 | [diff] [blame^] | 258 | * Sets the CPU status (free or busy since some ms). |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 259 | * |
Georgios Katsikas | 6dc11c1 | 2018-12-20 08:43:29 +0100 | [diff] [blame^] | 260 | * @param busySince CPU busy time in ms, -1 if not busy |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 261 | * @return builder object |
| 262 | */ |
Georgios Katsikas | 6dc11c1 | 2018-12-20 08:43:29 +0100 | [diff] [blame^] | 263 | public Builder setBusySince(int busySince) { |
| 264 | this.busySince = busySince; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 265 | |
| 266 | return this; |
| 267 | } |
| 268 | |
| 269 | /** |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 270 | * 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 Katsikas | 973a265 | 2018-06-28 08:45:47 +0200 | [diff] [blame] | 318 | * Sets the average latency. |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 319 | * |
Georgios Katsikas | 973a265 | 2018-06-28 08:45:47 +0200 | [diff] [blame] | 320 | * @param averageLatency average latency |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 321 | * @return builder object |
| 322 | */ |
Georgios Katsikas | 973a265 | 2018-06-28 08:45:47 +0200 | [diff] [blame] | 323 | public Builder setAverageLatency(float averageLatency) { |
| 324 | this.averageLatency = averageLatency; |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 325 | |
| 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 Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 342 | * Creates a DefaultCpuStatistics object. |
| 343 | * |
| 344 | * @return DefaultCpuStatistics object |
| 345 | */ |
| 346 | public DefaultCpuStatistics build() { |
| 347 | return new DefaultCpuStatistics( |
Georgios Katsikas | 6dc11c1 | 2018-12-20 08:43:29 +0100 | [diff] [blame^] | 348 | deviceId, id, load, queue, busySince, |
Georgios Katsikas | fda6674 | 2018-07-31 20:18:14 +0200 | [diff] [blame] | 349 | throughputUnit, averageThroughput, |
Georgios Katsikas | 973a265 | 2018-06-28 08:45:47 +0200 | [diff] [blame] | 350 | latencyUnit, minLatency, averageLatency, maxLatency); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | |
| 354 | } |