| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #ifndef NETDATA_ML_HOST_H |
| 4 | #define NETDATA_ML_HOST_H |
| 5 | |
| 6 | #include "ml_calculated_number.h" |
| 7 | |
| 8 | #include "database/rrd.h" |
| 9 | |
| 10 | #include <atomic> |
| 11 | #include <unordered_map> |
| 12 | |
| 13 | struct ml_queue_t; |
| 14 | |
| 15 | typedef struct machine_learning_stats_t { |
| 16 | uint32_t num_machine_learning_status_enabled; |
| 17 | uint32_t num_machine_learning_status_disabled_sp; |
| 18 | |
| 19 | uint32_t num_metric_type_constant; |
| 20 | uint32_t num_metric_type_variable; |
| 21 | |
| 22 | uint32_t num_training_status_untrained; |
| 23 | uint32_t num_training_status_pending_without_model; |
| 24 | uint32_t num_training_status_trained; |
| 25 | uint32_t num_training_status_pending_with_model; |
| 26 | uint32_t num_training_status_silenced; |
| 27 | |
| 28 | uint32_t num_anomalous_dimensions; |
| 29 | uint32_t num_normal_dimensions; |
| 30 | } ml_machine_learning_stats_t; |
| 31 | |
| 32 | typedef struct { |
| 33 | RRDDIM *rd; |
| 34 | uint32_t normal_dimensions; |
| 35 | uint32_t anomalous_dimensions; |
| 36 | } ml_context_anomaly_rate_t; |
| 37 | |
| 38 | typedef struct { |
| 39 | RRDHOST *rh; |
| 40 | |
| 41 | std::atomic<bool> ml_running; |
| 42 | |
| 43 | // Incremented at the END of every ml_host_stop, after all chart/dim resets |
| 44 | // have committed. ml_host_detect_once samples this before and after its |
| 45 | // unlocked chart walk; a change means a stop completed during the walk |
| 46 | // (so detect raced with stop's chart->mls writes) or a stop+start cycle |
| 47 | // happened around the walk. In either case the accumulated snapshot must |
| 48 | // be discarded even if ml_running is back to true at the re-check. |
| 49 | std::atomic<uint64_t> ml_stop_generation; |
| 50 | |
| 51 | ml_machine_learning_stats_t mls; |
| 52 | |
| 53 | calculated_number_t host_anomaly_rate; |
| 54 | |
| 55 | netdata_mutex_t mutex; |
| 56 | |
| 57 | // Serializes ml_host_start() against ml_host_stop(). Stop holds it across |
| 58 | // its full chart/dim reset walk and the final stop-generation bump (it |
| 59 | // cannot carry host->mutex into that walk), so a racing start cannot |
| 60 | // re-enable ml_running while stop is mid-reset. Without it, a detect walk |
| 61 | // could observe ml_running==true with an unchanged stop generation and |
| 62 | // publish a snapshot torn by stop's in-flight chart->mls resets. |
| 63 | netdata_mutex_t start_stop_mutex; |
| 64 | |
| 65 | ml_queue_t *queue; |
| 66 | |
| 67 | /* |
| 68 | * bookkeeping for anomaly detection charts |
| 69 | */ |
| 70 | |
| 71 | RRDSET *ml_running_rs; |
| 72 | RRDDIM *ml_running_rd; |
| 73 | |
| 74 | RRDSET *machine_learning_status_rs; |
| 75 | RRDDIM *machine_learning_status_enabled_rd; |
| 76 | RRDDIM *machine_learning_status_disabled_sp_rd; |
| 77 | |
| 78 | RRDSET *metric_type_rs; |
| 79 | RRDDIM *metric_type_constant_rd; |
| 80 | RRDDIM *metric_type_variable_rd; |
| 81 | |
| 82 | RRDSET *training_status_rs; |
| 83 | RRDDIM *training_status_untrained_rd; |
| 84 | RRDDIM *training_status_pending_without_model_rd; |
| 85 | RRDDIM *training_status_trained_rd; |
| 86 | RRDDIM *training_status_pending_with_model_rd; |
| 87 | RRDDIM *training_status_silenced_rd; |
| 88 | |
| 89 | RRDSET *dimensions_rs; |
| 90 | RRDDIM *dimensions_anomalous_rd; |
| 91 | RRDDIM *dimensions_normal_rd; |
| 92 | |
| 93 | RRDSET *anomaly_rate_rs; |
| 94 | RRDDIM *anomaly_rate_rd; |
| 95 | |
| 96 | RRDSET *detector_events_rs; |
| 97 | RRDDIM *detector_events_above_threshold_rd; |
| 98 | RRDDIM *detector_events_new_anomaly_event_rd; |
| 99 | |
| 100 | RRDSET *context_anomaly_rate_rs; |
| 101 | SPINLOCK context_anomaly_rate_spinlock; |
| 102 | std::unordered_map<STRING *, ml_context_anomaly_rate_t> context_anomaly_rate; |
| 103 | |
| 104 | bool reset_pointers; |
| 105 | } ml_host_t; |
| 106 | |
| 107 | #endif /* NETDATA_ML_HOST_H */ |