summaryrefslogtreecommitdiffstats
path: root/ml
diff options
context:
space:
mode:
authorvkalintiris <vasilis@netdata.cloud>2023-02-28 15:53:45 +0200
committerGitHub <noreply@github.com>2023-02-28 15:53:45 +0200
commit45981cb7347a5fed7ebbabba0fe193d0d9471eff (patch)
treedd5690ab7350d3885813bb3709b254e68c2ac7f4 /ml
parentc8b4f19013182b03b6ba727dd784181e1beb8bf9 (diff)
Port ML from C++ to C. (#14567)
* Port ML from C++ to C. Pretty much everything is a non-functional change, ie. the functionality is identical to the one provided by the existing implementation that is written in C++. Performance-wise, this implementation: - Eliminates/reduces the number of allocations and deallocations we have to do for training/detection, - Uses just a single thread to perform detection for *all* the hosts (ie. reduces the number of required threads by 50% on parents), and - Allows training, prediction and detection of dimensions that have an update_every that is different from that of the localhost. The only C++ functionality that we still use is vectors, because they make our life easier and they are pretty much a requirement imposed by dlib. * Remove profile.plugin It was useful only for testing during development. * Limit logs to 200 lines per period * Properly generate ml_info in /api/v1/info endpoint. * Remove resource usage charts since we use worker charts. * Use a temporary to make linters happy. * Rebase. * Fix builds that have ML functionality disabled.
Diffstat (limited to 'ml')
-rw-r--r--ml/ADCharts.cc518
-rw-r--r--ml/ADCharts.h21
-rw-r--r--ml/Chart.cc0
-rw-r--r--ml/Chart.h128
-rw-r--r--ml/Config.cc113
-rw-r--r--ml/Config.h52
-rw-r--r--ml/Dimension.cc346
-rw-r--r--ml/Dimension.h198
-rw-r--r--ml/Host.cc387
-rw-r--r--ml/Host.h70
-rw-r--r--ml/KMeans.cc43
-rw-r--r--ml/KMeans.h41
-rw-r--r--ml/Mutex.h36
-rw-r--r--ml/Query.h57
-rw-r--r--ml/Queue.h66
-rw-r--r--ml/SamplesBuffer.cc183
-rw-r--r--ml/SamplesBuffer.h149
-rw-r--r--ml/Stats.h46
-rw-r--r--ml/ad_charts.cc446
-rw-r--r--ml/ad_charts.h14
-rw-r--r--ml/ml-dummy.c4
-rw-r--r--ml/ml-private.h13
-rw-r--r--ml/ml.cc209
-rw-r--r--ml/ml.h30
-rw-r--r--ml/nml.cc1135
-rw-r--r--ml/nml.h346
26 files changed, 2114 insertions, 2537 deletions
diff --git a/ml/ADCharts.cc b/ml/ADCharts.cc
deleted file mode 100644
index cbb13f5d19..0000000000
--- a/ml/ADCharts.cc
+++ /dev/null
@@ -1,518 +0,0 @@
-// SPDX-License-Identifier: GPL-3.0-or-later
-
-#include "ADCharts.h"
-#include "Config.h"
-
-void ml::updateDimensionsChart(RRDHOST *RH, const MachineLearningStats &MLS) {
- /*
- * Machine learning status
- */
- {
- static thread_local RRDSET *MachineLearningStatusRS = nullptr;
-
- static thread_local RRDDIM *Enabled = nullptr;
- static thread_local RRDDIM *DisabledUE = nullptr;
- static thread_local RRDDIM *DisabledSP = nullptr;
-
- if (!MachineLearningStatusRS) {
- std::stringstream IdSS, NameSS;
-
- IdSS << "machine_learning_status_on_" << localhost->machine_guid;
- NameSS << "machine_learning_status_on_" << rrdhost_hostname(localhost);
-
- MachineLearningStatusRS = rrdset_create(
- RH,
- "netdata", // type
- IdSS.str().c_str(), // id
- NameSS.str().c_str(), // name
- NETDATA_ML_CHART_FAMILY, // family
- "netdata.machine_learning_status", // ctx
- "Machine learning status", // title
- "dimensions", // units
- NETDATA_ML_PLUGIN, // plugin
- NETDATA_ML_MODULE_TRAINING, // module
- NETDATA_ML_CHART_PRIO_MACHINE_LEARNING_STATUS, // priority
- RH->rrd_update_every, // update_every
- RRDSET_TYPE_LINE // chart_type
- );
- rrdset_flag_set(MachineLearningStatusRS , RRDSET_FLAG_ANOMALY_DETECTION);
-
- Enabled = rrddim_add(MachineLearningStatusRS, "enabled", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
- DisabledUE = rrddim_add(MachineLearningStatusRS, "disabled-ue", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
- DisabledSP = rrddim_add(MachineLearningStatusRS, "disabled-sp", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
- }
-
- rrddim_set_by_pointer(MachineLearningStatusRS, Enabled, MLS.NumMachineLearningStatusEnabled);
- rrddim_set_by_pointer(MachineLearningStatusRS, DisabledUE, MLS.NumMachineLearningStatusDisabledUE);
- rrddim_set_by_pointer(MachineLearningStatusRS, DisabledSP, MLS.NumMachineLearningStatusDisabledSP);
-
- rrdset_done(MachineLearningStatusRS);
- }
-
- /*
- * Metric type
- */
- {
- static thread_local RRDSET *MetricTypesRS = nullptr;
-
- static thread_local RRDDIM *Constant = nullptr;
- static thread_local RRDDIM *Variable = nullptr;
-
- if (!MetricTypesRS) {
- std::stringstream IdSS, NameSS;
-
- IdSS << "metric_types_on_" << localhost->machine_guid;
- NameSS << "metric_types_on_" << rrdhost_hostname(localhost);
-
- MetricTypesRS = rrdset_create(
- RH,
- "netdata", // type
- IdSS.str().c_str(), // id
- NameSS.str().c_str(), // name
- NETDATA_ML_CHART_FAMILY, // family
- "netdata.metric_types", // ctx
- "Dimensions by metric type", // title
- "dimensions", // units
- NETDATA_ML_PLUGIN, // plugin
- NETDATA_ML_MODULE_TRAINING, // module
- NETDATA_ML_CHART_PRIO_METRIC_TYPES, // priority
- RH->rrd_update_every, // update_every
- RRDSET_TYPE_LINE // chart_type
- );
- rrdset_flag_set(MetricTypesRS, RRDSET_FLAG_ANOMALY_DETECTION);
-
- Constant = rrddim_add(MetricTypesRS, "constant", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
- Variable = rrddim_add(MetricTypesRS, "variable", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
- }
-
- rrddim_set_by_pointer(MetricTypesRS, Constant, MLS.NumMetricTypeConstant);
- rrddim_set_by_pointer(MetricTypesRS, Variable, MLS.NumMetricTypeVariable);
-
- rrdset_done(MetricTypesRS);
- }
-
- /*
- * Training status
- */
- {
- static thread_local RRDSET *TrainingStatusRS = nullptr;
-
- static thread_local RRDDIM *Untrained = nullptr;
- static thread_local RRDDIM *PendingWithoutModel = nullptr;
- static thread_local RRDDIM *Trained = nullptr;
- static thread_local RRDDIM *PendingWithModel = nullptr;
-
- if (!TrainingStatusRS) {
- std::stringstream IdSS, NameSS;
-
- IdSS << "training_status_on_" << localhost->machine_guid;
- NameSS << "training_status_on_" << rrdhost_hostname(localhost);
-
- TrainingStatusRS = rrdset_create(
- RH,
- "netdata", // type
- IdSS.str().c_str(), // id
- NameSS.str().c_str(), // name
- NETDATA_ML_CHART_FAMILY, // family
- "netdata.training_status", // ctx
- "Training status of dimensions", // title
- "dimensions", // units
- NETDATA_ML_PLUGIN, // plugin
- NETDATA_ML_MODULE_TRAINING, // module
- NETDATA_ML_CHART_PRIO_TRAINING_STATUS, // priority
- RH->rrd_update_every, // update_every
- RRDSET_TYPE_LINE // chart_type
- );
-
- rrdset_flag_set(TrainingStatusRS, RRDSET_FLAG_ANOMALY_DETECTION);
-
- Untrained = rrddim_add(TrainingStatusRS, "untrained", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
- PendingWithoutModel = rrddim_add(TrainingStatusRS, "pending-without-model", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
- Trained = rrddim_add(TrainingStatusRS, "trained", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
- PendingWithModel = rrddim_add(TrainingStatusRS, "pending-with-model", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
- }
-
- rrddim_set_by_pointer(TrainingStatusRS, Untrained, MLS.NumTrainingStatusUntrained);
- rrddim_set_by_pointer(TrainingStatusRS, PendingWithoutModel, MLS.NumTrainingStatusPendingWithoutModel);
- rrddim_set_by_pointer(TrainingStatusRS, Trained, MLS.NumTrainingStatusTrained);
- rrddim_set_by_pointer(TrainingStatusRS, PendingWithModel, MLS.NumTrainingStatusPendingWithModel);
-
- rrdset_done(TrainingStatusRS);
- }
-
- /*
- * Prediction status
- */
- {
- static thread_local RRDSET *PredictionRS = nullptr;
-
- static thread_local RRDDIM *Anomalous = nullptr;
- static thread_local RRDDIM *Normal = nullptr;
-
- if (!PredictionRS) {
- std::stringstream IdSS, NameSS;
-
- IdSS << "dimensions_on_" << localhost->machine_guid;
- NameSS << "dimensions_on_" << rrdhost_hostname(localhost);
-
- PredictionRS = rrdset_create(
- RH,
- "anomaly_detection", // type
- IdSS.str().c_str(), // id
- NameSS.str().c_str(), // name
- "dimensions", // family
- "anomaly_detection.dimensions", // ctx
- "Anomaly detection dimensions", // title
- "dimensions", // units
- NETDATA_ML_PLUGIN, // plugin
- NETDATA_ML_MODULE_TRAINING, // module
- ML_CHART_PRIO_DIMENSIONS, // priority
- RH->rrd_update_every, // update_every
- RRDSET_TYPE_LINE // chart_type
- );
- rrdset_flag_set(PredictionRS, RRDSET_FLAG_ANOMALY_DETECTION);
-
- Anomalous = rrddim_add(PredictionRS, "anomalous", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
- Normal = rrddim_add(PredictionRS, "normal", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
- }
-
- rrddim_set_by_pointer(PredictionRS, Anomalous, MLS.NumAnomalousDimensions);
- rrddim_set_by_pointer(PredictionRS, Normal, MLS.NumNormalDimensions);
-
- rrdset_done(PredictionRS);
- }
-
-}
-
-void ml::updateHostAndDetectionRateCharts(RRDHOST *RH, collected_number AnomalyRate) {
- static thread_local RRDSET *HostRateRS = nullptr;
- static thread_local RRDDIM *AnomalyRateRD = nullptr;
-
- if (!HostRateRS) {
- std::stringstream IdSS, NameSS;
-
- IdSS << "anomaly_rate_on_" << localhost->machine_guid;
- NameSS << "anomaly_rate_on_" << rrdhost_hostname(localhost);
-
- HostRateRS = rrdset_create(
- RH,
- "anomaly_detection", // type
- IdSS.str().c_str(), // id
- NameSS.str().c_str(), // name
- "anomaly_rate", // family
- "anomaly_detection.anomaly_rate", // ctx
- "Percentage of anomalous dimensions", // title
- "percentage", // units
- NETDATA_ML_PLUGIN, // plugin
- NETDATA_ML_MODULE_DETECTION, // module
- ML_CHART_PRIO_ANOMALY_RATE, // priority
- RH->rrd_update_every, // update_every
- RRDSET_TYPE_LINE // chart_type
- );
- rrdset_flag_set(HostRateRS, RRDSET_FLAG_ANOMALY_DETECTION);
-
- AnomalyRateRD = rrddim_add(HostRateRS, "anomaly_rate", NULL,
- 1, 100, RRD_ALGORITHM_ABSOLUTE);
- }
-
- rrddim_set_by_pointer(HostRateRS, AnomalyRateRD, AnomalyRate);
- rrdset_done(HostRateRS);
-
- static thread_local RRDSET *AnomalyDetectionRS = nullptr;
- static thread_local RRDDIM *AboveThresholdRD = nullptr;
- static thread_local RRDDIM *NewAnomalyEventRD = nullptr;
-
- if (!AnomalyDetectionRS) {
- std::stringstream IdSS, NameSS;
-
- IdSS << "anomaly_detection_on_" << localhost->machine_guid;
- NameSS << "anomaly_detection_on_" << rrdhost_hostname(localhost);
-
- AnomalyDetectionRS = rrdset_create(
- RH,
- "anomaly_detection", // type
- IdSS.str().c_str(), // id
- NameSS.str().c_str(), // name
- "anomaly_detection", // family
- "anomaly_detection.detector_events", // ctx
- "Anomaly detection events", // title
- "percentage", // units
- NETDATA_ML_PLUGIN, // plugin
- NETDATA_ML_MODULE_DETECTION, // module
- ML_CHART_PRIO_DETECTOR_EVENTS, // priority
- RH->rrd_update_every, // update_every
- RRDSET_TYPE_LINE // chart_type
- );
- rrdset_flag_set(AnomalyDetectionRS, RRDSET_FLAG_ANOMALY_DETECTION);
-
- AboveThresholdRD = rrddim_add(AnomalyDetectionRS, "above_threshold", NULL,
- 1, 1, RRD_ALGORITHM_ABSOLUTE);
- NewAnomalyEventRD = rrddim_add(AnomalyDetectionRS, "new_anomaly_event", NULL,
- 1, 1, RRD_ALGORITHM_ABSOLUTE);
- }
-
- /*
- * Compute the values of the dimensions based on the host rate chart
- */
- ONEWAYALLOC *OWA = onewayalloc_create(0);
- time_t Now = now_realtime_sec();
- time_t Before = Now - RH->rrd_update_every;
- time_t After = Before - Cfg.AnomalyDetectionQueryDuration;
- RRDR_OPTIONS Options = static_cast<RRDR_OPTIONS>(0x00000000);
-
- RRDR *R = rrd2rrdr_legacy(
- OWA, HostRateRS,
- 1 /* points wanted */,
- After,
- Before,
- Cfg.AnomalyDetectionGroupingMethod,
- 0 /* resampling time */,
- Options, "anomaly_rate",
- NULL /* group options */,
- 0, /* timeout */
- 0, /* tier */
- QUERY_SOURCE_ML,
- STORAGE_PRIORITY_BEST_EFFORT
- );
-
- if(R) {
- if(R->d == 1 && R->n == 1 && R->rows == 1) {
- static thread_local bool PrevAboveThreshold = false;
- bool AboveThreshold = R->v[0] >= Cfg.HostAnomalyRateThreshold;
- bool NewAnomalyEvent = AboveThreshold && !PrevAboveThreshold;
- PrevAboveThreshold = AboveThreshold;
-
- rrddim_set_by_pointer(AnomalyDetectionRS, AboveThresholdRD, AboveThreshold);
- rrddim_set_by_pointer(AnomalyDetectionRS, NewAnomalyEventRD, NewAnomalyEvent);
- rrdset_done(AnomalyDetectionRS);
- }
-
- rrdr_free(OWA, R);
- }
-
- onewayalloc_destroy(OWA);
-}
-
-void ml::updateResourceUsageCharts(RRDHOST *RH, const struct rusage &PredictionRU, const struct rusage &TrainingRU) {
- /*
- * prediction rusage
- */
- {
- static thread_local RRDSET *RS = nullptr;
-
- static thread_local RRDDIM *User = nullptr;
- static thread_local RRDDIM *System = nullptr;
-
- if (!RS) {
- std::stringstream IdSS, NameSS;
-
- IdSS << "prediction_usage_for_" << RH->machine_guid;
- NameSS << "prediction_usage_for_" << rrdhost_hostname(RH);
-
- RS = rrdset_create_localhost(
- "netdata", // type
- IdSS.str().c_str(), // id
- NameSS.str().c_str(), // name
- NETDATA_ML_CHART_FAMILY, // family
- "netdata.prediction_usage", // ctx
- "Prediction resource usage", // title
- "milliseconds/s", // units
- NETDATA_ML_PLUGIN, // plugin
- NETDATA_ML_MODULE_PREDICTION, // module
- NETDATA_ML_CHART_PRIO_PREDICTION_USAGE, // priority
- RH->rrd_update_every, // update_every
- RRDSET_TYPE_STACKED // chart_type
- );
- rrdset_flag_set(RS, RRDSET_FLAG_ANOMALY_DETECTION);
-
- User = rrddim_add(RS, "user", NULL, 1, 1000, RRD_ALGORITHM_INCREMENTAL);
- System = rrddim_add(RS, "system", NULL, 1, 1000, RRD_ALGORITHM_INCREMENTAL);
- }
-
- rrddim_set_by_pointer(RS, User, PredictionRU.ru_utime.tv_sec * 1000000ULL + PredictionRU.ru_utime.tv_usec);
- rrddim_set_by_pointer(RS, System, PredictionRU.ru_stime.tv_sec * 1000000ULL + PredictionRU.ru_stime.tv_usec);
-
- rrdset_done(RS);
- }
-
- /*
- * training rusage
- */
- {
- static thread_local RRDSET *RS = nullptr;
-
- static thread_local RRDDIM *User = nullptr;
- static thread_local RRDDIM *System = nullptr;
-
- if (!RS) {
- std::stringstream IdSS, NameSS;
-
- IdSS << "training_usage_for_" << RH->machine_guid;
- NameSS << "training_usage_for_" << rrdhost_hostname(RH);
-
- RS = rrdset_create_localhost(
- "netdata", // type
- IdSS.str().c_str(), // id
- NameSS.str().c_str(), // name
- NETDATA_ML_CHART_FAMILY, // family
- "netdata.training_usage", // ctx
- "Training resource usage", // title
- "milliseconds/s", // units
- NETDATA_ML_PLUGIN, // plugin
- NETDATA_ML_MODULE_TRAINING, // module
- NETDATA_ML_CHART_PRIO_TRAINING_USAGE, // priority
- RH->rrd_update_every, // update_every
- RRDSET_TYPE_STACKED // chart_type
- );
- rrdset_flag_set(RS, RRDSET_FLAG_ANOMALY_DETECTION);
-
- User = rrddim_add(RS, "user", NULL, 1, 1000, RRD_ALGORITHM_INCREMENTAL);
- System = rrddim_add(RS, "system", NULL, 1, 1000, RRD_ALGORITHM_INCREMENTAL);
- }
-
- rrddim_set_by_pointer(RS, User, TrainingRU.ru_utime.tv_sec * 1000000ULL + TrainingRU.ru_utime.tv_usec);
- rrddim_set_by_pointer(RS, System, TrainingRU.ru_stime.tv_sec * 1000000ULL + TrainingRU.ru_stime.tv_usec);
-
- rrdset_done(RS);
- }
-}
-
-void ml::updateTrainingStatisticsChart(RRDHOST *RH, const TrainingStats &TS) {
- /*
- * queue stats
- */
- {
- static thread_local RRDSET *RS = nullptr;
-
- static thread_local RRDDIM *QueueSize = nullptr;
- static thread_local RRDDIM *PoppedItems = nullptr;
-
- if (!RS) {
- std::stringstream IdSS, NameSS;
-
- IdSS << "queue_stats_on_" << localhost->machine_guid;
- NameSS << "queue_stats_on_" << rrdhost_hostname(localhost);
-
- RS = rrdset_create(
- RH,
- "netdata", // type
- IdSS.str().c_str(), // id
- NameSS.str().c_str(), // name
- NETDATA_ML_CHART_FAMILY, // family
- "netdata.queue_stats", // ctx
- "Training queue stats", // title
- "items", // units
- NETDATA_ML_PLUGIN, // plugin
- NETDATA_ML_MODULE_TRAINING, // module
- NETDATA_ML_CHART_PRIO_QUEUE_STATS, // priority
- RH->rrd_update_every, // update_every
- RRDSET_TYPE_LINE// chart_type
- );
- rrdset_flag_set(RS, RRDSET_FLAG_ANOMALY_DETECTION);
-
- QueueSize = rrddim_add(RS, "queue_size", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
- PoppedItems = rrddim_add(RS, "popped_items", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
- }
-
- rrddim_set_by_pointer(RS, QueueSize, TS.QueueSize);
- rrddim_set_by_pointer(RS, PoppedItems, TS.NumPoppedItems);
-
- rrdset_done(RS);
- }
-
- /*
- * training stats
- */
- {
- static thread_local RRDSET *RS = nullptr;
-
- static thread_local RRDDIM *Allotted = nullptr;
- static thread_local RRDDIM *Consumed = nullptr;
- static thread_local RRDDIM *Remaining = nullptr;
-
- if (!RS) {
- std::stringstream IdSS, NameSS;
-
- IdSS << "training_time_stats_on_" << localhost->machine_guid;
- NameSS << "training_time_stats_on_" << rrdhost_hostname(localhost);
-
- RS = rrdset_create(
- RH,
- "netdata", // type
- IdSS.str().c_str(), // id
- NameSS.str().c_str(), // name
- NETDATA_ML_CHART_FAMILY, // family
- "netdata.training_time_stats", // ctx
- "Training time stats", // title
- "milliseconds", // units
- NETDATA_ML_PLUGIN, // plugin
- NETDATA_ML_MODULE_TRAINING, // module
- NETDATA_ML_CHART_PRIO_TRAINING_TIME_STATS, // priority
- RH->rrd_update_every, // update_every
- RRDSET_TYPE_LINE// chart_type
- );
- rrdset_flag_set(RS, RRDSET_FLAG_ANOMALY_DETECTION);
-
- Allotted = rrddim_add(RS, "allotted", NULL, 1, 1000, RRD_ALGORITHM_ABSOLUTE);
- Consumed = rrddim_add(RS, "consumed", NULL, 1, 1000, RRD_ALGORITHM_ABSOLUTE);
- Remaining = rrddim_add(RS, "remaining", NULL, 1, 1000, RRD_ALGORITHM_ABSOLUTE);
- }
-
- rrddim_set_by_pointer(RS, Allotted, TS.AllottedUT);
- rrddim_set_by_pointer(RS, Consumed, TS.ConsumedUT);
- rrddim_set_by_pointer(RS, Remaining, TS.RemainingUT);
-
- rrdset_done(RS);
- }
-
- /*
- * training result stats
- */
- {
- static thread_local RRDSET *RS = nullptr;
-
- static thread_local RRDDIM *Ok = nullptr;
- static thread_local RRDDIM *InvalidQueryTimeRange = nullptr;
- static thread_local RRDDIM *NotEnoughCollectedValues = nullptr;
- static thread_local RRDDIM *NullAcquiredDimension = nullptr;
- static thread_local RRDDIM *ChartUnderReplication = nullptr;
-
- if (!RS) {
- std::stringstream IdSS, NameSS;
-
- IdSS << "training_results_on_" << localhost->machine_guid;
- NameSS << "training_results_on_" << rrdhost_hostname(localhost);
-
- RS = rrdset_create(
- RH,
- "netdata", // type
- IdSS.str().c_str(), // id
- NameSS.str().c_str(), // name
- NETDATA_ML_CHART_FAMILY, // family
- "netdata.training_results", // ctx
- "Training results", // title
- "events", // units
- NETDATA_ML_PLUGIN, // plugin
- NETDATA_ML_MODULE_TRAINING, // module
- NETDATA_ML_CHART_PRIO_TRAINING_RESULTS, // priority
- RH->rrd_update_every, // update_every
- RRDSET_TYPE_LINE// chart_type
- );
- rrdset_flag_set(RS, RRDSET_FLAG_ANOMALY_DETECTION);
-
- Ok = rrddim_add(RS, "ok", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
- InvalidQueryTimeRange = rrddim_add(RS, "invalid-queries", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
- NotEnoughCollectedValues = rrddim_add(RS, "not-enough-values", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
- NullAcquiredDimension = rrddim_add(RS, "null-acquired-dimensions", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
- ChartUnderReplication = rrddim_add(RS, "chart-under-replication", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
- }
-
- rrddim_set_by_pointer(RS, Ok, TS.TrainingResultOk);
- rrddim_set_by_pointer(RS, InvalidQueryTimeRange, TS.TrainingResultInvalidQueryTimeRange);
- rrddim_set_by_pointer(RS, NotEnoughCollectedValues, TS.TrainingResultNotEnoughCollectedValues);
- rrddim_set_by_pointer(RS, NullAcquiredDimension, TS.TrainingResultNullAcquiredDimension);
- rrddim_set_by_pointer(RS, ChartUnderReplication, TS.TrainingResultChartUnderReplication);
-
- rrdset_done(RS);
- }
-}
diff --git a/ml/ADCharts.h b/ml/ADCharts.h
deleted file mode 100644
index ee09669e22..0000000000
--- a/ml/ADCharts.h
+++ /dev/null
@@ -1,21 +0,0 @@
-// SPDX-License-Identifier: GPL-3.0-or-later
-
-#ifndef ML_ADCHARTS_H
-#define ML_ADCHARTS_H
-
-#include "Stats.h"
-#include "ml-private.h"
-
-namespace ml {
-
-void updateDimensionsChart(RRDHOST *RH, const MachineLearningStats &MLS);
-
-void updateHostAndDetectionRateCharts(RRDHOST *RH, collected_number AnomalyRate);
-
-void updateResourceUsageCharts(RRDHOST *RH, const struct rusage &PredictionRU, const struct rusage &TrainingRU);
-
-void updateTrainingStatisticsChart(RRDHOST *RH, const TrainingStats &TS);
-
-} // namespace ml
-
-#endif /* ML_ADCHARTS_H */
diff --git a/ml/Chart.cc b/ml/Chart.cc
deleted file mode 100644
index e69de29bb2..0000000000
--- a/ml/Chart.cc
+++ /dev/null
diff --git a/ml/Chart.h b/ml/Chart.h
deleted file mode 100644
index dbd6a910f9..0000000000
--- a/ml/Chart.h
+++ /dev/null
@@ -1,128 +0,0 @@
-// SPDX-License-Identifier: GPL-3.0-or-later
-
-#ifndef ML_CHART_H
-#define ML_CHART_H
-
-#include "Config.h"
-#include "Dimension.h"
-
-#include "ml-private.h"
-#include "json/single_include/nlohmann/json.hpp"
-
-namespace ml
-{
-
-class Chart {
-public:
- Chart(RRDSET *RS) :
- RS(RS),
- MLS()
- { }
-
- RRDSET *getRS() const {
- return RS;
- }
-
- bool isAvailableForML() {
- return rrdset_is_available_for_exporting_and_alarms(RS);
- }
-
- void addDimension(Dimension *D) {
- std::lock_guard<Mutex> L(M);
- Dimensions[D->getRD()] = D;
- }
-
- void removeDimension(Dimension *D) {
- std::lock_guard<Mutex> L(M);
- Dimensions.erase(D->getRD());
- }
-
- void getModelsAsJson(nlohmann::json &Json) {
- std::lock_guard<Mutex> L(M);
-
- for (auto &DP : Dimensions) {
- Dimension *D = DP.second;
- nlohmann::json JsonArray = nlohmann::json::array();
- for (const KMeans &KM : D->getModels()) {
- nlohmann::json J;
- KM.toJson(J);
- JsonArray.push_back(J);
- }
-
- Json[getMLDimensionID(D->getRD())] = JsonArray;
- }
- }
-
- void updateBegin() {
- M.lock();
- MLS = {};
- }
-
- void updateDimension(Dimension *D, bool IsAnomalous) {
- switch (D->getMLS()) {
- case MachineLearningStatus::DisabledDueToUniqueUpdateEvery:
- MLS.NumMachineLearningStatusDisabledUE++;
- return;
- case MachineLearningStatus::DisabledDueToExcludedChart:
- MLS.NumMachineLearningStatusDisabledSP++;
- return;
- case MachineLearningStatus::Enabled: {
- MLS.NumMachineLearningStatusEnabled++;
-
- switch (D->getMT()) {
- case MetricType::Constant:
- MLS.NumMetricTypeConstant++;
- MLS.NumTrainingStatusTrained++;
- MLS.NumNormalDimensions++;
- return;
- case MetricType::Variable:
- MLS.NumMetricTypeVariable++;
- break;
- }
-
- switch (D->getTS()) {
- case TrainingStatus::Untrained:
- MLS.NumTrainingStatusUntrained++;
- return;
- case TrainingStatus::PendingWithoutModel:
- MLS.NumTrainingStatusPendingWithoutModel++;
- return;
- case TrainingStatus::Trained:
- MLS.NumTrainingStatusTrained++;
-
- MLS.NumAnomalousDimensions += IsAnomalous;
- MLS.NumNormalDimensions += !IsAnomalous;
- return;
- case TrainingStatus::PendingWithModel:
- MLS.NumTrainingStatusPendingWithModel++;
-
- MLS.NumAnomalousDimensions += IsAnomalous;
- MLS.NumNormalDimensions += !IsAnomalous;
- return;
- }
-
- return;
- }
- }
- }
-
- void updateEnd() {
- M.unlock();
- }
-
- MachineLearningStats getMLS() {
- std::lock_guard<Mutex> L(M);
- return MLS;
- }
-
-private:
- RRDSET *RS;
- MachineLearningStats MLS;
-
- Mutex M;
- std::unordered_map<RRDDIM *, Dimension *> Dimensions;
-};
-
-} // namespace ml
-
-#endif /* ML_CHART_H */
diff --git a/ml/Config.cc b/ml/Config.cc
index cdc43d1655..d7ddff800d 100644
--- a/ml/Config.cc
+++ b/ml/Config.cc
@@ -1,15 +1,12 @@
// SPDX-License-Identifier: GPL-3.0-or-later
-#include "Config.h"
-#include "ml-private.h"
-
-using namespace ml;
+#include "nml.h"
/*
* Global configuration instance to be shared between training and
* prediction threads.
*/
-Config ml::Cfg;
+nml_config_t Cfg;
template <typename T>
static T clamp(const T& Value, const T& Min, const T& Max) {
@@ -19,97 +16,97 @@ static T clamp(const T& Value, const T& Min, const T& Max) {
/*
* Initialize global configuration variable.
*/
-void Config::readMLConfig(void) {
- const char *ConfigSectionML = CONFIG_SECTION_ML;
+void nml_config_load(nml_config_t *cfg) {
+ const char *config_section_ml = CONFIG_SECTION_ML;
- bool EnableAnomalyDetection = config_get_boolean(ConfigSectionML, "enabled", true);
+ bool enable_anomaly_detection = config_get_boolean(config_section_ml, "enabled", true);
/*
* Read values
*/
- unsigned MaxTrainSamples = config_get_number(ConfigSectionML, "maximum num samples to train", 4 * 3600);
- unsigned MinTrainSamples = config_get_number(ConfigSectionML, "minimum num samples to train", 1 * 900);
- unsigned TrainEvery = config_get_number(ConfigSectionML, "train every", 1 * 3600);
- unsigned NumModelsToUse = config_get_number(ConfigSectionML, "number of models per dimension", 1);
+ unsigned max_train_samples = config_get_number(config_section_ml, "maximum num samples to train", 4 * 3600);
+ unsigned min_train_samples = config_get_number(config_section_ml, "minimum num samples to train", 1 * 900);
+ unsigned train_every = config_get_number(config_section_ml, "train every", 1 * 3600);
+ unsigned num_models_to_use = config_get_number(config_section_ml, "number of models per dimension", 1);
- unsigned DiffN = config_get_number(ConfigSectionML, "num samples to diff", 1);
- unsigned SmoothN = config_get_number(ConfigSectionML, "num samples to smooth", 3);
- unsigned LagN = config_get_number(ConfigSectionML, "num samples to lag", 5);
+ unsigned diff_n = config_get_number(config_section_ml, "num samples to diff", 1);
+ unsigned