diff options
author | Stelios Fragkakis <52996999+stelfrag@users.noreply.github.com> | 2023-10-18 17:20:00 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-18 17:20:00 +0300 |
commit | 9caea28bcda541558d0adf611a7f69aafed251cc (patch) | |
tree | fb035ecf81ae4f226c24a52e0a98593055289af6 /ml | |
parent | ee3db23972e92d35d8cfdbe1f0b50eb247747a31 (diff) |
Reuse ML load prepared statement (#16240)
Reuse ML load prepared statement and release resources on each batch load
Fix parameter to ML model load to be in seconds not usec
Diffstat (limited to 'ml')
-rw-r--r-- | ml/ml-dummy.c | 2 | ||||
-rw-r--r-- | ml/ml.cc | 17 | ||||
-rw-r--r-- | ml/ml.h | 2 |
3 files changed, 13 insertions, 8 deletions
diff --git a/ml/ml-dummy.c b/ml/ml-dummy.c index 2ad6cc7266..7058e21cdb 100644 --- a/ml/ml-dummy.c +++ b/ml/ml-dummy.c @@ -100,7 +100,7 @@ bool ml_dimension_is_anomalous(RRDDIM *rd, time_t curr_time, double value, bool return false; } -int ml_dimension_load_models(RRDDIM *rd) { +int ml_dimension_load_models(RRDDIM *rd, sqlite3_stmt **stmp) { UNUSED(rd); return 0; } @@ -621,7 +621,7 @@ bind_fail: return rc; } -int ml_dimension_load_models(RRDDIM *rd) { +int ml_dimension_load_models(RRDDIM *rd, sqlite3_stmt **active_stmt) { ml_dimension_t *dim = (ml_dimension_t *) rd->ml_dimension; if (!dim) return 0; @@ -635,7 +635,7 @@ int ml_dimension_load_models(RRDDIM *rd) { std::vector<ml_kmeans_t> V; - static __thread sqlite3_stmt *res = NULL; + sqlite3_stmt *res = active_stmt ? *active_stmt : NULL; int rc = 0; int param = 0; @@ -645,18 +645,20 @@ int ml_dimension_load_models(RRDDIM *rd) { } if (unlikely(!res)) { - rc = prepare_statement(db, db_models_load, &res); + rc = sqlite3_prepare_v2(db, db_models_load, -1, &res, NULL); if (unlikely(rc != SQLITE_OK)) { error_report("Failed to prepare statement to load models, rc = %d", rc); return 1; } + if (active_stmt) + *active_stmt = res; } rc = sqlite3_bind_blob(res, ++param, &dim->rd->metric_uuid, sizeof(dim->rd->metric_uuid), SQLITE_STATIC); if (unlikely(rc != SQLITE_OK)) goto bind_fail; - rc = sqlite3_bind_int(res, ++param, now_realtime_usec() - (Cfg.num_models_to_use * Cfg.max_train_samples)); + rc = sqlite3_bind_int64(res, ++param, now_realtime_sec() - (Cfg.num_models_to_use * Cfg.max_train_samples)); if (unlikely(rc != SQLITE_OK)) goto bind_fail; @@ -702,9 +704,12 @@ int ml_dimension_load_models(RRDDIM *rd) { if (unlikely(rc != SQLITE_DONE)) error_report("Failed to load models, rc = %d", rc); - rc = sqlite3_reset(res); + if (active_stmt) + rc = sqlite3_reset(res); + else + rc = sqlite3_finalize(res); if (unlikely(rc != SQLITE_OK)) - error_report("Failed to reset statement when loading models, rc = %d", rc); + error_report("Failed to %s statement when loading models, rc = %d", active_stmt ? "reset" : "finalize", rc); return 0; @@ -40,7 +40,7 @@ void ml_dimension_new(RRDDIM *rd); void ml_dimension_delete(RRDDIM *rd); bool ml_dimension_is_anomalous(RRDDIM *rd, time_t curr_time, double value, bool exists); -int ml_dimension_load_models(RRDDIM *rd); +int ml_dimension_load_models(RRDDIM *rd, sqlite3_stmt **stmt); void ml_update_global_statistics_charts(uint64_t models_consulted); |