Browse Source

fix quota calculation when the the db is empty (#16699)

* fix quota calculation when the the db is empty

* do not compute workers utilization if extented statistics is not enabled
Costa Tsaousis 1 year ago
parent
commit
8479f6d5e4

+ 9 - 0
daemon/main.c

@@ -2034,6 +2034,15 @@ int main(int argc, char **argv) {
         // setup threads configs
         default_stacksize = netdata_threads_init();
 
+#ifdef NETDATA_INTERNAL_CHECKS
+        config_set_boolean(CONFIG_SECTION_PLUGINS, "netdata monitoring", true);
+        config_set_boolean(CONFIG_SECTION_PLUGINS, "netdata monitoring extended", true);
+#endif
+
+        if(config_get_boolean(CONFIG_SECTION_PLUGINS, "netdata monitoring extended", false))
+            // this has to run before starting any other threads that use workers
+            workers_utilization_enable();
+
         for (i = 0; static_threads[i].name != NULL ; i++) {
             struct netdata_static_thread *st = &static_threads[i];
 

+ 2 - 2
daemon/static_threads.c

@@ -72,7 +72,7 @@ const struct netdata_static_thread static_threads_common[] = {
         .config_name = "netdata monitoring extended",
         .env_name = "NETDATA_INTERNALS_MONITORING",
         .global_variable = &global_statistics_enabled,
-        .enabled = 0,
+        .enabled = 0, // this is ignored - check main() for "netdata monitoring extended"
         .thread = NULL,
         .init_routine = NULL,
         .start_routine = global_statistics_workers_main
@@ -83,7 +83,7 @@ const struct netdata_static_thread static_threads_common[] = {
         .config_name = "netdata monitoring extended",
         .env_name = "NETDATA_INTERNALS_MONITORING",
         .global_variable = &global_statistics_enabled,
-        .enabled = 0,
+        .enabled = 0, // this is ignored - check main() for "netdata monitoring extended"
         .thread = NULL,
         .init_routine = NULL,
         .start_routine = global_statistics_sqlite3_main

+ 8 - 0
database/engine/rrdengine.c

@@ -1398,6 +1398,14 @@ uint64_t rrdeng_target_data_file_size(struct rrdengine_instance *ctx) {
 
 bool rrdeng_ctx_exceeded_disk_quota(struct rrdengine_instance *ctx)
 {
+    if(!ctx->datafiles.first)
+        // no datafiles available
+        return false;
+
+    if(!ctx->datafiles.first->next)
+        // only 1 datafile available
+        return false;
+
     uint64_t estimated_disk_space = ctx_current_disk_space_get(ctx) + rrdeng_target_data_file_size(ctx) -
                                     (ctx->datafiles.first->prev ? ctx->datafiles.first->prev->pos : 0);
 

+ 18 - 3
libnetdata/worker_utilization/worker_utilization.c

@@ -50,13 +50,16 @@ struct workers_workname {                           // this is what we add to Ju
 };
 
 static struct workers_globals {
+    bool enabled;
+
     SPINLOCK spinlock;
     Pvoid_t worknames_JudyHS;
     size_t memory;
 
 } workers_globals = {                               // workers globals, the base of all worknames
-        .spinlock = NETDATA_SPINLOCK_INITIALIZER,   // a lock for the worknames index
-        .worknames_JudyHS = NULL,                   // the worknames index
+    .enabled = false,
+    .spinlock = NETDATA_SPINLOCK_INITIALIZER,   // a lock for the worknames index
+    .worknames_JudyHS = NULL,                   // the worknames index
 };
 
 static __thread struct worker *worker = NULL; // the current thread worker
@@ -69,7 +72,14 @@ static inline usec_t worker_now_monotonic_usec(void) {
 #endif
 }
 
+void workers_utilization_enable(void) {
+    workers_globals.enabled = true;
+}
+
 size_t workers_allocated_memory(void) {
+    if(!workers_globals.enabled)
+        return 0;
+
     spinlock_lock(&workers_globals.spinlock);
     size_t memory = workers_globals.memory;
     spinlock_unlock(&workers_globals.spinlock);
@@ -78,7 +88,8 @@ size_t workers_allocated_memory(void) {
 }
 
 void worker_register(const char *name) {
-    if(unlikely(worker)) return;
+    if(unlikely(worker || !workers_globals.enabled))
+        return;
 
     worker = callocz(1, sizeof(struct worker));
     worker->pid = gettid();
@@ -213,6 +224,7 @@ void worker_is_busy(size_t job_id) {
 
 void worker_set_metric(size_t job_id, NETDATA_DOUBLE value) {
     if(unlikely(!worker)) return;
+
     if(unlikely(job_id >= WORKER_UTILIZATION_MAX_JOB_TYPES))
         return;
 
@@ -247,6 +259,9 @@ void workers_foreach(const char *name, void (*callback)(
                                                , NETDATA_DOUBLE *job_custom_values
                                                )
                                                , void *data) {
+    if(!workers_globals.enabled)
+        return;
+
     spinlock_lock(&workers_globals.spinlock);
     usec_t busy_time, delta;
     size_t i, jobs_started, jobs_running;

+ 1 - 0
libnetdata/worker_utilization/worker_utilization.h

@@ -15,6 +15,7 @@ typedef enum __attribute__((packed)) {
     WORKER_METRIC_INCREMENTAL_TOTAL = 4,
 } WORKER_METRIC_TYPE;
 
+void workers_utilization_enable(void);
 size_t workers_allocated_memory(void);
 void worker_register(const char *name);
 void worker_register_job_name(size_t job_id, const char *name);