| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "ad_charts.h" |
| 4 | #include "ml_config.h" |
| 5 | |
| 6 | void ml_update_dimensions_chart(ml_host_t *host, const ml_machine_learning_stats_t &mls) { |
| 7 | |
| 8 | if(__atomic_load_n(&host->reset_pointers, __ATOMIC_RELAXED)) { |
| 9 | __atomic_store_n(&host->reset_pointers, false, __ATOMIC_RELAXED); |
| 10 | |
| 11 | host->ml_running_rs = nullptr; |
| 12 | host->ml_running_rd = nullptr; |
| 13 | host->machine_learning_status_rs = nullptr; |
| 14 | host->machine_learning_status_enabled_rd = nullptr; |
| 15 | host->machine_learning_status_disabled_sp_rd = nullptr; |
| 16 | host->metric_type_rs = nullptr; |
| 17 | host->metric_type_constant_rd = nullptr; |
| 18 | host->metric_type_variable_rd = nullptr; |
| 19 | host->training_status_rs = nullptr; |
| 20 | host->training_status_untrained_rd = nullptr; |
| 21 | host->training_status_pending_without_model_rd = nullptr; |
| 22 | host->training_status_trained_rd = nullptr; |
| 23 | host->training_status_pending_with_model_rd = nullptr; |
| 24 | host->training_status_silenced_rd = nullptr; |
| 25 | host->dimensions_rs = nullptr; |
| 26 | host->dimensions_anomalous_rd = nullptr; |
| 27 | host->dimensions_normal_rd = nullptr; |
| 28 | host->anomaly_rate_rs = nullptr; |
| 29 | host->anomaly_rate_rd = nullptr; |
| 30 | host->detector_events_rs = nullptr; |
| 31 | host->detector_events_above_threshold_rd = nullptr; |
| 32 | host->detector_events_new_anomaly_event_rd = nullptr; |
| 33 | host->context_anomaly_rate_rs = nullptr; |
| 34 | } |
| 35 | |
| 36 | /* |
| 37 | * Machine learning status |
| 38 | */ |
| 39 | if (Cfg.enable_statistics_charts) { |
| 40 | if (!host->machine_learning_status_rs) { |
| 41 | char id_buf[1024]; |
| 42 | char name_buf[1024]; |
| 43 | |
| 44 | snprintfz(id_buf, 1024, "machine_learning_status_on_%s", localhost->machine_guid); |
| 45 | snprintfz(name_buf, 1024, "machine_learning_status_on_%s", rrdhost_hostname(localhost)); |
| 46 | |
| 47 | host->machine_learning_status_rs = rrdset_create( |
| 48 | host->rh, |
| 49 | "netdata", // type |
| 50 | id_buf, |
| 51 | name_buf, // name |
| 52 | NETDATA_ML_CHART_FAMILY, // family |
| 53 | "netdata.ml_status", // ctx |
| 54 | "Machine learning status", // title |
| 55 | "dimensions", // units |
| 56 | NETDATA_ML_PLUGIN, // plugin |
| 57 | NETDATA_ML_MODULE_TRAINING, // module |
| 58 | NETDATA_ML_CHART_PRIO_MACHINE_LEARNING_STATUS, // priority |
| 59 | localhost->rrd_update_every, // update_every |
| 60 | RRDSET_TYPE_LINE // chart_type |
| 61 | ); |
| 62 | rrdset_flag_set(host->machine_learning_status_rs , RRDSET_FLAG_ANOMALY_DETECTION); |
| 63 | |
| 64 | host->machine_learning_status_enabled_rd = |
| 65 | rrddim_add(host->machine_learning_status_rs, "enabled", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE); |
| 66 | host->machine_learning_status_disabled_sp_rd = |
| 67 | rrddim_add(host->machine_learning_status_rs, "disabled-sp", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE); |
| 68 | } |
| 69 | |
| 70 | rrddim_set_by_pointer(host->machine_learning_status_rs, |
| 71 | host->machine_learning_status_enabled_rd, mls.num_machine_learning_status_enabled); |
| 72 | rrddim_set_by_pointer(host->machine_learning_status_rs, |
| 73 | host->machine_learning_status_disabled_sp_rd, mls.num_machine_learning_status_disabled_sp); |
| 74 | |
| 75 | rrdset_done(host->machine_learning_status_rs); |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * Metric type |
| 80 | */ |
| 81 | if (Cfg.enable_statistics_charts) { |
| 82 | if (!host->metric_type_rs) { |
| 83 | char id_buf[1024]; |
| 84 | char name_buf[1024]; |
| 85 | |
| 86 | snprintfz(id_buf, 1024, "metric_types_on_%s", localhost->machine_guid); |
| 87 | snprintfz(name_buf, 1024, "metric_types_on_%s", rrdhost_hostname(localhost)); |
| 88 | |
| 89 | host->metric_type_rs = rrdset_create( |
| 90 | host->rh, |
| 91 | "netdata", // type |
| 92 | id_buf, // id |
| 93 | name_buf, // name |
| 94 | NETDATA_ML_CHART_FAMILY, // family |
| 95 | "netdata.ml_metric_types", // ctx |
| 96 | "Dimensions by metric type", // title |
| 97 | "dimensions", // units |
| 98 | NETDATA_ML_PLUGIN, // plugin |
| 99 | NETDATA_ML_MODULE_TRAINING, // module |
| 100 | NETDATA_ML_CHART_PRIO_METRIC_TYPES, // priority |
| 101 | localhost->rrd_update_every, // update_every |
| 102 | RRDSET_TYPE_LINE // chart_type |
| 103 | ); |
| 104 | rrdset_flag_set(host->metric_type_rs, RRDSET_FLAG_ANOMALY_DETECTION); |
| 105 | |
| 106 | host->metric_type_constant_rd = |
| 107 | rrddim_add(host->metric_type_rs, "constant", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE); |
| 108 | host->metric_type_variable_rd = |
| 109 | rrddim_add(host->metric_type_rs, "variable", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE); |
| 110 | } |
| 111 | |
| 112 | rrddim_set_by_pointer(host->metric_type_rs, |
| 113 | host->metric_type_constant_rd, mls.num_metric_type_constant); |
| 114 | rrddim_set_by_pointer(host->metric_type_rs, |
| 115 | host->metric_type_variable_rd, mls.num_metric_type_variable); |
| 116 | |
| 117 | rrdset_done(host->metric_type_rs); |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | * Training status |
| 122 | */ |
| 123 | if (Cfg.enable_statistics_charts) { |
| 124 | if (!host->training_status_rs) { |
| 125 | char id_buf[1024]; |
| 126 | char name_buf[1024]; |
| 127 | |
| 128 | snprintfz(id_buf, 1024, "training_status_on_%s", localhost->machine_guid); |
| 129 | snprintfz(name_buf, 1024, "training_status_on_%s", rrdhost_hostname(localhost)); |
| 130 | |
| 131 | host->training_status_rs = rrdset_create( |
| 132 | host->rh, |
| 133 | "netdata", // type |
| 134 | id_buf, // id |
| 135 | name_buf, // name |
| 136 | NETDATA_ML_CHART_FAMILY, // family |
| 137 | "netdata.ml_training_status", // ctx |
| 138 | "Training status of dimensions", // title |
| 139 | "dimensions", // units |
| 140 | NETDATA_ML_PLUGIN, // plugin |
| 141 | NETDATA_ML_MODULE_TRAINING, // module |
| 142 | NETDATA_ML_CHART_PRIO_TRAINING_STATUS, // priority |
| 143 | localhost->rrd_update_every, // update_every |
| 144 | RRDSET_TYPE_LINE // chart_type |
| 145 | ); |
| 146 | |
| 147 | rrdset_flag_set(host->training_status_rs, RRDSET_FLAG_ANOMALY_DETECTION); |
| 148 | |
| 149 | host->training_status_untrained_rd = |
| 150 | rrddim_add(host->training_status_rs, "untrained", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE); |
| 151 | host->training_status_pending_without_model_rd = |
| 152 | rrddim_add(host->training_status_rs, "pending-without-model", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE); |
| 153 | host->training_status_trained_rd = |
| 154 | rrddim_add(host->training_status_rs, "trained", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE); |
| 155 | host->training_status_pending_with_model_rd = |
| 156 | rrddim_add(host->training_status_rs, "pending-with-model", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE); |
| 157 | host->training_status_silenced_rd = |
| 158 | rrddim_add(host->training_status_rs, "silenced", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE); |
| 159 | } |
| 160 | |
| 161 | rrddim_set_by_pointer(host->training_status_rs, |
| 162 | host->training_status_untrained_rd, mls.num_training_status_untrained); |
| 163 | rrddim_set_by_pointer(host->training_status_rs, |
| 164 | host->training_status_pending_without_model_rd, mls.num_training_status_pending_without_model); |
| 165 | rrddim_set_by_pointer(host->training_status_rs, |
| 166 | host->training_status_trained_rd, mls.num_training_status_trained); |
| 167 | rrddim_set_by_pointer(host->training_status_rs, |
| 168 | host->training_status_pending_with_model_rd, mls.num_training_status_pending_with_model); |
| 169 | rrddim_set_by_pointer(host->training_status_rs, |
| 170 | host->training_status_silenced_rd, mls.num_training_status_silenced); |
| 171 | |
| 172 | rrdset_done(host->training_status_rs); |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * Prediction status |
| 177 | */ |
| 178 | { |
| 179 | if (!host->dimensions_rs) { |
| 180 | char id_buf[1024]; |
| 181 | char name_buf[1024]; |
| 182 | |
| 183 | snprintfz(id_buf, 1024, "dimensions_on_%s", localhost->machine_guid); |
| 184 | snprintfz(name_buf, 1024, "dimensions_on_%s", rrdhost_hostname(localhost)); |
| 185 | |
| 186 | host->dimensions_rs = rrdset_create( |
| 187 | host->rh, |
| 188 | "anomaly_detection", // type |
| 189 | id_buf, // id |
| 190 | name_buf, // name |
| 191 | "dimensions", // family |
| 192 | "anomaly_detection.dimensions", // ctx |
| 193 | "Anomaly detection dimensions", // title |
| 194 | "dimensions", // units |
| 195 | NETDATA_ML_PLUGIN, // plugin |
| 196 | NETDATA_ML_MODULE_TRAINING, // module |
| 197 | ML_CHART_PRIO_DIMENSIONS, // priority |
| 198 | localhost->rrd_update_every, // update_every |
| 199 | RRDSET_TYPE_LINE // chart_type |
| 200 | ); |
| 201 | rrdset_flag_set(host->dimensions_rs, RRDSET_FLAG_ANOMALY_DETECTION); |
| 202 | |
| 203 | host->dimensions_anomalous_rd = |
| 204 | rrddim_add(host->dimensions_rs, "anomalous", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE); |
| 205 | host->dimensions_normal_rd = |
| 206 | rrddim_add(host->dimensions_rs, "normal", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE); |
| 207 | } |
| 208 | |
| 209 | rrddim_set_by_pointer(host->dimensions_rs, |
| 210 | host->dimensions_anomalous_rd, mls.num_anomalous_dimensions); |
| 211 | rrddim_set_by_pointer(host->dimensions_rs, |
| 212 | host->dimensions_normal_rd, mls.num_normal_dimensions); |
| 213 | |
| 214 | rrdset_done(host->dimensions_rs); |
| 215 | } |
| 216 | |
| 217 | // ML running |
| 218 | { |
| 219 | if (!host->ml_running_rs) { |
| 220 | char id_buf[1024]; |
| 221 | char name_buf[1024]; |
| 222 | |
| 223 | snprintfz(id_buf, 1024, "ml_running_on_%s", localhost->machine_guid); |
| 224 | snprintfz(name_buf, 1024, "ml_running_on_%s", rrdhost_hostname(localhost)); |
| 225 | |
| 226 | host->ml_running_rs = rrdset_create( |
| 227 | host->rh, |
| 228 | "anomaly_detection", // type |
| 229 | id_buf, // id |
| 230 | name_buf, // name |
| 231 | "anomaly_detection", // family |
| 232 | "anomaly_detection.ml_running", // ctx |
| 233 | "ML running", // title |
| 234 | "boolean", // units |
| 235 | NETDATA_ML_PLUGIN, // plugin |
| 236 | NETDATA_ML_MODULE_DETECTION, // module |
| 237 | NETDATA_ML_CHART_RUNNING, // priority |
| 238 | localhost->rrd_update_every, // update_every |
| 239 | RRDSET_TYPE_LINE // chart_type |
| 240 | ); |
| 241 | rrdset_flag_set(host->ml_running_rs, RRDSET_FLAG_ANOMALY_DETECTION); |
| 242 | |
| 243 | host->ml_running_rd = |
| 244 | rrddim_add(host->ml_running_rs, "ml_running", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE); |
| 245 | } |
| 246 | |
| 247 | rrddim_set_by_pointer(host->ml_running_rs, |
| 248 | host->ml_running_rd, host->ml_running); |
| 249 | rrdset_done(host->ml_running_rs); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | void ml_update_host_and_detection_rate_charts(ml_host_t *host, collected_number anomaly_rate, ONEWAYALLOC *owa) { |
| 254 | /* |
| 255 | * Host anomaly rate |
| 256 | */ |
| 257 | { |
| 258 | if (!host->anomaly_rate_rs) { |
| 259 | char id_buf[1024]; |
| 260 | char name_buf[1024]; |
| 261 | |
| 262 | snprintfz(id_buf, 1024, "anomaly_rate_on_%s", localhost->machine_guid); |
| 263 | snprintfz(name_buf, 1024, "anomaly_rate_on_%s", rrdhost_hostname(localhost)); |
| 264 | |
| 265 | host->anomaly_rate_rs = rrdset_create( |
| 266 | host->rh, |
| 267 | "anomaly_detection", // type |
| 268 | id_buf, // id |
| 269 | name_buf, // name |
| 270 | "anomaly_rate", // family |
| 271 | "anomaly_detection.anomaly_rate", // ctx |
| 272 | "Percentage of anomalous dimensions", // title |
| 273 | "percentage", // units |
| 274 | NETDATA_ML_PLUGIN, // plugin |
| 275 | NETDATA_ML_MODULE_DETECTION, // module |
| 276 | ML_CHART_PRIO_ANOMALY_RATE, // priority |
| 277 | localhost->rrd_update_every, // update_every |
| 278 | RRDSET_TYPE_LINE // chart_type |
| 279 | ); |
| 280 | rrdset_flag_set(host->anomaly_rate_rs, RRDSET_FLAG_ANOMALY_DETECTION); |
| 281 | |
| 282 | host->anomaly_rate_rd = |
| 283 | rrddim_add(host->anomaly_rate_rs, "anomaly_rate", NULL, 1, 100, RRD_ALGORITHM_ABSOLUTE); |
| 284 | } |
| 285 | |
| 286 | rrddim_set_by_pointer(host->anomaly_rate_rs, host->anomaly_rate_rd, anomaly_rate); |
| 287 | |
| 288 | rrdset_done(host->anomaly_rate_rs); |
| 289 | } |
| 290 | |
| 291 | /* |
| 292 | * Context anomaly rate |
| 293 | */ |
| 294 | { |
| 295 | if (!host->context_anomaly_rate_rs) { |
| 296 | char id_buf[1024]; |
| 297 | char name_buf[1024]; |
| 298 | |
| 299 | snprintfz(id_buf, 1024, "context_anomaly_rate_on_%s", localhost->machine_guid); |
| 300 | snprintfz(name_buf, 1024, "context_anomaly_rate_on_%s", rrdhost_hostname(localhost)); |
| 301 | |
| 302 | host->context_anomaly_rate_rs = rrdset_create( |
| 303 | host->rh, |
| 304 | "anomaly_detection", |
| 305 | id_buf, |
| 306 | name_buf, |
| 307 | "anomaly_rate", |
| 308 | "anomaly_detection.context_anomaly_rate", |
| 309 | "Percentage of anomalous dimensions by context", |
| 310 | "percentage", |
| 311 | NETDATA_ML_PLUGIN, |
| 312 | NETDATA_ML_MODULE_DETECTION, |
| 313 | ML_CHART_PRIO_CONTEXT_ANOMALY_RATE, |
| 314 | localhost->rrd_update_every, |
| 315 | RRDSET_TYPE_STACKED |
| 316 | ); |
| 317 | |
| 318 | rrdset_flag_set(host->context_anomaly_rate_rs, RRDSET_FLAG_ANOMALY_DETECTION); |
| 319 | } |
| 320 | |
| 321 | spinlock_lock(&host->context_anomaly_rate_spinlock); |
| 322 | for (auto &entry : host->context_anomaly_rate) { |
| 323 | ml_context_anomaly_rate_t &context_anomaly_rate = entry.second; |
| 324 | |
| 325 | if (!context_anomaly_rate.rd) |
| 326 | context_anomaly_rate.rd = rrddim_add(host->context_anomaly_rate_rs, string2str(entry.first), NULL, 1, 100, RRD_ALGORITHM_ABSOLUTE); |
| 327 | |
| 328 | double ar = 0.0; |
| 329 | size_t n = context_anomaly_rate.anomalous_dimensions + context_anomaly_rate.normal_dimensions; |
| 330 | if (n) |
| 331 | ar = static_cast<double>(context_anomaly_rate.anomalous_dimensions) / n; |
| 332 | |
| 333 | rrddim_set_by_pointer(host->context_anomaly_rate_rs, context_anomaly_rate.rd, ar * 10000.0); |
| 334 | |
| 335 | context_anomaly_rate.anomalous_dimensions = 0; |
| 336 | context_anomaly_rate.normal_dimensions = 0; |
| 337 | } |
| 338 | spinlock_unlock(&host->context_anomaly_rate_spinlock); |
| 339 | |
| 340 | rrdset_done(host->context_anomaly_rate_rs); |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | * Detector Events |
| 345 | */ |
| 346 | { |
| 347 | if (!host->detector_events_rs) { |
| 348 | char id_buf[1024]; |
| 349 | char name_buf[1024]; |
| 350 | |
| 351 | snprintfz(id_buf, 1024, "anomaly_detection_on_%s", localhost->machine_guid); |
| 352 | snprintfz(name_buf, 1024, "anomaly_detection_on_%s", rrdhost_hostname(localhost)); |
| 353 | |
| 354 | host->detector_events_rs = rrdset_create( |
| 355 | host->rh, |
| 356 | "anomaly_detection", // type |
| 357 | id_buf, // id |
| 358 | name_buf, // name |
| 359 | "anomaly_detection", // family |
| 360 | "anomaly_detection.detector_events", // ctx |
| 361 | "Anomaly detection events", // title |
| 362 | "status", // units |
| 363 | NETDATA_ML_PLUGIN, // plugin |
| 364 | NETDATA_ML_MODULE_DETECTION, // module |
| 365 | ML_CHART_PRIO_DETECTOR_EVENTS, // priority |
| 366 | localhost->rrd_update_every, // update_every |
| 367 | RRDSET_TYPE_LINE // chart_type |
| 368 | ); |
| 369 | rrdset_flag_set(host->detector_events_rs, RRDSET_FLAG_ANOMALY_DETECTION); |
| 370 | |
| 371 | host->detector_events_above_threshold_rd = |
| 372 | rrddim_add(host->detector_events_rs, "above_threshold", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE); |
| 373 | host->detector_events_new_anomaly_event_rd = |
| 374 | rrddim_add(host->detector_events_rs, "new_anomaly_event", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE); |
| 375 | } |
| 376 | |
| 377 | /* |
| 378 | * Compute the values of the dimensions based on the host rate chart |
| 379 | */ |
| 380 | if (host->ml_running) { |
| 381 | // Reclaim the previous host's query scratch before starting the |
| 382 | // next one. Cheap no-op on the first iteration of a fresh arena. |
| 383 | onewayalloc_reset(owa); |
| 384 | |
| 385 | time_t Now = now_realtime_sec(); |
| 386 | time_t Before = Now - host->rh->rrd_update_every; |
| 387 | time_t After = Before - Cfg.anomaly_detection_query_duration; |
| 388 | RRDR_OPTIONS Options = static_cast<RRDR_OPTIONS>(0x00000000); |
| 389 | |
| 390 | RRDR *R = rrd2rrdr_legacy( |
| 391 | owa, |
| 392 | host->anomaly_rate_rs, |
| 393 | 1 /* points wanted */, |
| 394 | After, |
| 395 | Before, |
| 396 | Cfg.anomaly_detection_grouping_method, |
| 397 | 0 /* resampling time */, |
| 398 | Options, "anomaly_rate", |
| 399 | NULL /* group options */, |
| 400 | 0, /* timeout */ |
| 401 | 0, /* tier */ |
| 402 | QUERY_SOURCE_ML, |
| 403 | STORAGE_PRIORITY_SYNCHRONOUS |
| 404 | ); |
| 405 | |
| 406 | if (R) { |
| 407 | if (R->d == 1 && R->n == 1 && R->rows == 1) { |
| 408 | static thread_local bool prev_above_threshold = false; |
| 409 | bool above_threshold = R->v[0] >= Cfg.host_anomaly_rate_threshold; |
| 410 | bool new_anomaly_event = above_threshold && !prev_above_threshold; |
| 411 | prev_above_threshold = above_threshold; |
| 412 | |
| 413 | rrddim_set_by_pointer(host->detector_events_rs, |
| 414 | host->detector_events_above_threshold_rd, above_threshold); |
| 415 | rrddim_set_by_pointer(host->detector_events_rs, |
| 416 | host->detector_events_new_anomaly_event_rd, new_anomaly_event); |
| 417 | |
| 418 | rrdset_done(host->detector_events_rs); |
| 419 | } |
| 420 | |
| 421 | rrdr_free(owa, R); |
| 422 | } |
| 423 | } else { |
| 424 | rrddim_set_by_pointer(host->detector_events_rs, |
| 425 | host->detector_events_above_threshold_rd, 0); |
| 426 | rrddim_set_by_pointer(host->detector_events_rs, |
| 427 | host->detector_events_new_anomaly_event_rd, 0); |
| 428 | rrdset_done(host->detector_events_rs); |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | void ml_update_training_statistics_chart(ml_worker_t *worker, const ml_queue_stats_t &stats) { |
| 434 | /* |
| 435 | * queue stats |
| 436 | */ |
| 437 | { |
| 438 | if (!worker->queue_stats_rs) { |
| 439 | char id_buf[1024]; |
| 440 | char name_buf[1024]; |
| 441 | |
| 442 | snprintfz(id_buf, 1024, "training_queue_%zu_ops", worker->id); |
| 443 | snprintfz(name_buf, 1024, "training_queue_%zu_ops", worker->id); |
| 444 | |
| 445 | worker->queue_stats_rs = rrdset_create( |
| 446 | localhost, |
| 447 | "netdata", // type |
| 448 | id_buf, // id |
| 449 | name_buf, // name |
| 450 | NETDATA_ML_CHART_FAMILY, // family |
| 451 | "netdata.ml_queue_ops", // ctx |
| 452 | "Training queue operations", // title |
| 453 | "count", // units |
| 454 | NETDATA_ML_PLUGIN, // plugin |
| 455 | NETDATA_ML_MODULE_TRAINING, // module |
| 456 | NETDATA_ML_CHART_PRIO_QUEUE_STATS, // priority |
| 457 | localhost->rrd_update_every, // update_every |
| 458 | RRDSET_TYPE_LINE// chart_type |
| 459 | ); |
| 460 | rrdset_flag_set(worker->queue_stats_rs, RRDSET_FLAG_ANOMALY_DETECTION); |
| 461 | |
| 462 | worker->queue_stats_num_create_new_model_requests_rd = |
| 463 | rrddim_add(worker->queue_stats_rs, "pushed create model", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL); |
| 464 | worker->queue_stats_num_create_new_model_requests_completed_rd = |
| 465 | rrddim_add(worker->queue_stats_rs, "popped create model", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL); |
| 466 | |
| 467 | worker->queue_stats_num_add_existing_model_requests_rd = |
| 468 | rrddim_add(worker->queue_stats_rs, "pushed add model", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL); |
| 469 | |
| 470 | worker->queue_stats_num_add_existing_model_requests_completed_rd = |
| 471 | rrddim_add(worker->queue_stats_rs, "popped add models", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL); |
| 472 | } |
| 473 | |
| 474 | rrddim_set_by_pointer(worker->queue_stats_rs, |
| 475 | worker->queue_stats_num_create_new_model_requests_rd, stats.total_create_new_model_requests_pushed); |
| 476 | rrddim_set_by_pointer(worker->queue_stats_rs, |
| 477 | worker->queue_stats_num_create_new_model_requests_completed_rd, stats.total_create_new_model_requests_popped); |
| 478 | |
| 479 | rrddim_set_by_pointer(worker->queue_stats_rs, |
| 480 | worker->queue_stats_num_add_existing_model_requests_rd, stats.total_add_existing_model_requests_pushed); |
| 481 | rrddim_set_by_pointer(worker->queue_stats_rs, |
| 482 | worker->queue_stats_num_add_existing_model_requests_completed_rd, stats.total_add_existing_model_requests_popped); |
| 483 | |
| 484 | rrdset_done(worker->queue_stats_rs); |
| 485 | } |
| 486 | |
| 487 | { |
| 488 | if (!worker->queue_size_rs) { |
| 489 | char id_buf[1024]; |
| 490 | char name_buf[1024]; |
| 491 | |
| 492 | snprintfz(id_buf, 1024, "training_queue_%zu_size", worker->id); |
| 493 | snprintfz(name_buf, 1024, "training_queue_%zu_size", worker->id); |
| 494 | |
| 495 | worker->queue_size_rs = rrdset_create( |
| 496 | localhost, |
| 497 | "netdata", // type |
| 498 | id_buf, // id |
| 499 | name_buf, // name |
| 500 | NETDATA_ML_CHART_FAMILY, // family |
| 501 | "netdata.ml_queue_size", // ctx |
| 502 | "Training queue size", // title |
| 503 | "count", // units |
| 504 | NETDATA_ML_PLUGIN, // plugin |
| 505 | NETDATA_ML_MODULE_TRAINING, // module |
| 506 | NETDATA_ML_CHART_PRIO_QUEUE_STATS, // priority |
| 507 | localhost->rrd_update_every, // update_every |
| 508 | RRDSET_TYPE_LINE// chart_type |
| 509 | ); |
| 510 | rrdset_flag_set(worker->queue_size_rs, RRDSET_FLAG_ANOMALY_DETECTION); |
| 511 | |
| 512 | worker->queue_size_rd = |
| 513 | rrddim_add(worker->queue_size_rs, "items", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE); |
| 514 | } |
| 515 | |
| 516 | ml_queue_size_t qs = ml_queue_size(worker->queue); |
| 517 | collected_number cn = qs.add_exisiting_model + qs.create_new_model; |
| 518 | |
| 519 | rrddim_set_by_pointer(worker->queue_size_rs, worker->queue_size_rd, cn); |
| 520 | rrdset_done(worker->queue_size_rs); |
| 521 | } |
| 522 | |
| 523 | /* |
| 524 | * training stats |
| 525 | */ |
| 526 | { |
| 527 | if (!worker->training_time_stats_rs) { |
| 528 | char id_buf[1024]; |
| 529 | char name_buf[1024]; |
| 530 | |
| 531 | snprintfz(id_buf, 1024, "training_queue_%zu_time_stats", worker->id); |
| 532 | snprintfz(name_buf, 1024, "training_queue_%zu_time_stats", worker->id); |
| 533 | |
| 534 | worker->training_time_stats_rs = rrdset_create( |
| 535 | localhost, |
| 536 | "netdata", // type |
| 537 | id_buf, // id |
| 538 | name_buf, // name |
| 539 | NETDATA_ML_CHART_FAMILY, // family |
| 540 | "netdata.ml_training_time_stats", // ctx |
| 541 | "Training time stats", // title |
| 542 | "microseconds", // units |
| 543 | NETDATA_ML_PLUGIN, // plugin |
| 544 | NETDATA_ML_MODULE_TRAINING, // module |
| 545 | NETDATA_ML_CHART_PRIO_TRAINING_TIME_STATS, // priority |
| 546 | localhost->rrd_update_every, // update_every |
| 547 | RRDSET_TYPE_LINE// chart_type |
| 548 | ); |
| 549 | rrdset_flag_set(worker->training_time_stats_rs, RRDSET_FLAG_ANOMALY_DETECTION); |
| 550 | |
| 551 | worker->training_time_stats_allotted_rd = |
| 552 | rrddim_add(worker->training_time_stats_rs, "allotted", NULL, 1, 1000, RRD_ALGORITHM_INCREMENTAL); |
| 553 | worker->training_time_stats_consumed_rd = |
| 554 | rrddim_add(worker->training_time_stats_rs, "consumed", NULL, 1, 1000, RRD_ALGORITHM_INCREMENTAL); |
| 555 | worker->training_time_stats_remaining_rd = |
| 556 | rrddim_add(worker->training_time_stats_rs, "remaining", NULL, 1, 1000, RRD_ALGORITHM_INCREMENTAL); |
| 557 | } |
| 558 | |
| 559 | rrddim_set_by_pointer(worker->training_time_stats_rs, |
| 560 | worker->training_time_stats_allotted_rd, stats.allotted_ut); |
| 561 | rrddim_set_by_pointer(worker->training_time_stats_rs, |
| 562 | worker->training_time_stats_consumed_rd, stats.consumed_ut); |
| 563 | rrddim_set_by_pointer(worker->training_time_stats_rs, |
| 564 | worker->training_time_stats_remaining_rd, stats.remaining_ut); |
| 565 | |
| 566 | rrdset_done(worker->training_time_stats_rs); |
| 567 | } |
| 568 | |
| 569 | /* |
| 570 | * training result stats |
| 571 | */ |
| 572 | { |
| 573 | if (!worker->training_results_rs) { |
| 574 | char id_buf[1024]; |
| 575 | char name_buf[1024]; |
| 576 | |
| 577 | snprintfz(id_buf, 1024, "training_queue_%zu_results", worker->id); |
| 578 | snprintfz(name_buf, 1024, "training_queue_%zu_results", worker->id); |
| 579 | |
| 580 | worker->training_results_rs = rrdset_create( |
| 581 | localhost, |
| 582 | "netdata", // type |
| 583 | id_buf, // id |
| 584 | name_buf, // name |
| 585 | NETDATA_ML_CHART_FAMILY, // family |
| 586 | "netdata.ml_training_results", // ctx |
| 587 | "Training results", // title |
| 588 | "events", // units |
| 589 | NETDATA_ML_PLUGIN, // plugin |
| 590 | NETDATA_ML_MODULE_TRAINING, // module |
| 591 | NETDATA_ML_CHART_PRIO_TRAINING_RESULTS, // priority |
| 592 | localhost->rrd_update_every, // update_every |
| 593 | RRDSET_TYPE_LINE// chart_type |
| 594 | ); |
| 595 | rrdset_flag_set(worker->training_results_rs, RRDSET_FLAG_ANOMALY_DETECTION); |
| 596 | |
| 597 | worker->training_results_ok_rd = |
| 598 | rrddim_add(worker->training_results_rs, "ok", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL); |
| 599 | worker->training_results_invalid_query_time_range_rd = |
| 600 | rrddim_add(worker->training_results_rs, "invalid-queries", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL); |
| 601 | worker->training_results_not_enough_collected_values_rd = |
| 602 | rrddim_add(worker->training_results_rs, "not-enough-values", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL); |
| 603 | worker->training_results_null_acquired_dimension_rd = |
| 604 | rrddim_add(worker->training_results_rs, "null-acquired-dimensions", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL); |
| 605 | worker->training_results_chart_under_replication_rd = |
| 606 | rrddim_add(worker->training_results_rs, "chart-under-replication", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL); |
| 607 | } |
| 608 | |
| 609 | rrddim_set_by_pointer(worker->training_results_rs, |
| 610 | worker->training_results_ok_rd, stats.item_result_ok); |
| 611 | rrddim_set_by_pointer(worker->training_results_rs, |
| 612 | worker->training_results_invalid_query_time_range_rd, stats.item_result_invalid_query_time_range); |
| 613 | rrddim_set_by_pointer(worker->training_results_rs, |
| 614 | worker->training_results_not_enough_collected_values_rd, stats.item_result_not_enough_collected_values); |
| 615 | rrddim_set_by_pointer(worker->training_results_rs, |
| 616 | worker->training_results_null_acquired_dimension_rd, stats.item_result_null_acquired_dimension); |
| 617 | rrddim_set_by_pointer(worker->training_results_rs, |
| 618 | worker->training_results_chart_under_replication_rd, stats.item_result_chart_under_replication); |
| 619 | |
| 620 | rrdset_done(worker->training_results_rs); |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | void ml_update_global_statistics_charts(uint64_t models_consulted, |
| 625 | uint64_t models_received, |
| 626 | uint64_t models_sent, |
| 627 | uint64_t models_ignored, |
| 628 | uint64_t models_deserialization_failures, |
| 629 | uint64_t memory_consumption, |
| 630 | uint64_t memory_new, |
| 631 | uint64_t memory_delete) |
| 632 | { |
| 633 | if (!Cfg.enable_statistics_charts) |
| 634 | return; |
| 635 | |
| 636 | { |
| 637 | static RRDSET *st = NULL; |
| 638 | static RRDDIM *rd = NULL; |
| 639 | |
| 640 | if (unlikely(!st)) { |
| 641 | st = rrdset_create_localhost( |
| 642 | "netdata" // type |
| 643 | , "ml_models_consulted" // id |
| 644 | , NULL // name |
| 645 | , NETDATA_ML_CHART_FAMILY // family |
| 646 | , NULL // context |
| 647 | , "KMeans models used for prediction" // title |
| 648 | , "models" // units |
| 649 | , NETDATA_ML_PLUGIN // plugin |
| 650 | , NETDATA_ML_MODULE_DETECTION // module |
| 651 | , NETDATA_ML_CHART_PRIO_MACHINE_LEARNING_STATUS // priority |
| 652 | , localhost->rrd_update_every // update_every |
| 653 | , RRDSET_TYPE_AREA // chart_type |
| 654 | ); |
| 655 | |
| 656 | rd = rrddim_add(st, "num_models_consulted", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL); |
| 657 | } |
| 658 | |
| 659 | rrddim_set_by_pointer(st, rd, (collected_number) models_consulted); |
| 660 | |
| 661 | rrdset_done(st); |
| 662 | } |
| 663 | |
| 664 | { |
| 665 | static RRDSET *st = NULL; |
| 666 | static RRDDIM *rd_received = NULL; |
| 667 | static RRDDIM *rd_sent = NULL; |
| 668 | static RRDDIM *rd_ignored = NULL; |
| 669 | static RRDDIM *rd_deserialization_failures = NULL; |
| 670 | |
| 671 | if (unlikely(!st)) { |
| 672 | st = rrdset_create_localhost( |
| 673 | "netdata" // type |
| 674 | , "ml_models_streamed" // id |
| 675 | , NULL // name |
| 676 | , NETDATA_ML_CHART_FAMILY // family |
| 677 | , NULL // context |
| 678 | , "KMeans models streamed" // title |
| 679 | , "models" // units |
| 680 | , NETDATA_ML_PLUGIN // plugin |
| 681 | , NETDATA_ML_MODULE_DETECTION // module |
| 682 | , NETDATA_ML_CHART_PRIO_MACHINE_LEARNING_STATUS // priority |
| 683 | , localhost->rrd_update_every // update_every |
| 684 | , RRDSET_TYPE_LINE // chart_type |
| 685 | ); |
| 686 | |
| 687 | rd_received = rrddim_add(st, "received", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL); |
| 688 | rd_sent = rrddim_add(st, "sent", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL); |
| 689 | rd_ignored = rrddim_add(st, "ignored", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL); |
| 690 | rd_deserialization_failures = rrddim_add(st, "deserialization failures", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL); |
| 691 | } |
| 692 | |
| 693 | rrddim_set_by_pointer(st, rd_received, (collected_number) models_received); |
| 694 | rrddim_set_by_pointer(st, rd_sent, (collected_number) models_sent); |
| 695 | rrddim_set_by_pointer(st, rd_ignored, (collected_number) models_ignored); |
| 696 | rrddim_set_by_pointer(st, rd_deserialization_failures, (collected_number) models_deserialization_failures); |
| 697 | |
| 698 | rrdset_done(st); |
| 699 | } |
| 700 | |
| 701 | { |
| 702 | static RRDSET *st = NULL; |
| 703 | static RRDDIM *rd_memory_consumption = NULL; |
| 704 | |
| 705 | if (unlikely(!st)) { |
| 706 | st = rrdset_create_localhost( |
| 707 | "netdata" // type |
| 708 | , "ml_memory_used" // id |
| 709 | , NULL // name |
| 710 | , NETDATA_ML_CHART_FAMILY // family |
| 711 | , NULL // context |
| 712 | , "ML memory usage" // title |
| 713 | , "bytes" // units |
| 714 | , NETDATA_ML_PLUGIN // plugin |
| 715 | , NETDATA_ML_MODULE_DETECTION // module |
| 716 | , NETDATA_ML_CHART_PRIO_MACHINE_LEARNING_STATUS // priority |
| 717 | , localhost->rrd_update_every // update_every |
| 718 | , RRDSET_TYPE_LINE // chart_type |
| 719 | ); |
| 720 | |
| 721 | rd_memory_consumption = rrddim_add(st, "used", NULL, 1024, 1, RRD_ALGORITHM_ABSOLUTE); |
| 722 | } |
| 723 | |
| 724 | rrddim_set_by_pointer(st, rd_memory_consumption, (collected_number) memory_consumption / (1024)); |
| 725 | rrdset_done(st); |
| 726 | } |
| 727 | |
| 728 | { |
| 729 | static RRDSET *st = NULL; |
| 730 | static RRDDIM *rd_memory_new = NULL; |
| 731 | static RRDDIM *rd_memory_delete = NULL; |
| 732 | |
| 733 | if (unlikely(!st)) { |
| 734 | st = rrdset_create_localhost( |
| 735 | "netdata" // type |
| 736 | , "ml_memory_ops" // id |
| 737 | , NULL // name |
| 738 | , NETDATA_ML_CHART_FAMILY // family |
| 739 | , NULL // context |
| 740 | , "ML memory operations" // title |
| 741 | , "count" // units |
| 742 | , NETDATA_ML_PLUGIN // plugin |
| 743 | , NETDATA_ML_MODULE_DETECTION // module |
| 744 | , NETDATA_ML_CHART_PRIO_MACHINE_LEARNING_STATUS // priority |
| 745 | , localhost->rrd_update_every // update_every |
| 746 | , RRDSET_TYPE_LINE // chart_type |
| 747 | ); |
| 748 | |
| 749 | rd_memory_new = rrddim_add(st, "new", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL); |
| 750 | rd_memory_delete = rrddim_add(st, "delete", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL); |
| 751 | } |
| 752 | |
| 753 | rrddim_set_by_pointer(st, rd_memory_new, (collected_number) memory_new); |
| 754 | rrddim_set_by_pointer(st, rd_memory_delete, (collected_number) memory_delete); |
| 755 | rrdset_done(st); |
| 756 | } |
| 757 | } |