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 committed
Dec 29, 2023 at 19:42 UTC
8479f6d5e4ba72cfe4996ed1568d716b207945f7
5 files changed
+38
-5
daemon/main.c
+9
@@ -2034,6 +2034,15 @@ int main(int argc, char **argv) {
2034
// setup threads configs
2035
default_stacksize = netdata_threads_init();
2036
2037
+#ifdef NETDATA_INTERNAL_CHECKS
2038
+ config_set_boolean(CONFIG_SECTION_PLUGINS, "netdata monitoring", true);
2039
+ config_set_boolean(CONFIG_SECTION_PLUGINS, "netdata monitoring extended", true);
2040
+#endif
2041
+
2042
+ if(config_get_boolean(CONFIG_SECTION_PLUGINS, "netdata monitoring extended", false))
2043
+ // this has to run before starting any other threads that use workers
2044
+ workers_utilization_enable();
2045
+
2046
for (i = 0; static_threads[i].name != NULL ; i++) {
2047
struct netdata_static_thread *st = &static_threads[i];
2048
daemon/static_threads.c
+2
-2
@@ -72,7 +72,7 @@ const struct netdata_static_thread static_threads_common[] = {
72
.config_name = "netdata monitoring extended",
73
.env_name = "NETDATA_INTERNALS_MONITORING",
74
.global_variable = &global_statistics_enabled,
75
- .enabled = 0,
75
+ .enabled = 0, // this is ignored - check main() for "netdata monitoring extended"
76
.thread = NULL,
77
.init_routine = NULL,
78
.start_routine = global_statistics_workers_main
@@ -83,7 +83,7 @@ const struct netdata_static_thread static_threads_common[] = {
83
.config_name = "netdata monitoring extended",
84
.env_name = "NETDATA_INTERNALS_MONITORING",
85
.global_variable = &global_statistics_enabled,
86
- .enabled = 0,
86
+ .enabled = 0, // this is ignored - check main() for "netdata monitoring extended"
87
.thread = NULL,
88
.init_routine = NULL,
89
.start_routine = global_statistics_sqlite3_main
database/engine/rrdengine.c
+8
@@ -1398,6 +1398,14 @@ uint64_t rrdeng_target_data_file_size(struct rrdengine_instance *ctx) {
1398
1399
bool rrdeng_ctx_exceeded_disk_quota(struct rrdengine_instance *ctx)
1400
{
1401
+ if(!ctx->datafiles.first)
1402
+ // no datafiles available
1403
+ return false;
1404
+
1405
+ if(!ctx->datafiles.first->next)
1406
+ // only 1 datafile available
1407
+ return false;
1408
+
1409
uint64_t estimated_disk_space = ctx_current_disk_space_get(ctx) + rrdeng_target_data_file_size(ctx) -
1410
(ctx->datafiles.first->prev ? ctx->datafiles.first->prev->pos : 0);
1411
libnetdata/worker_utilization/worker_utilization.c
+18
-3
@@ -50,13 +50,16 @@ struct workers_workname { // this is what we add to Ju
50
};
51
52
static struct workers_globals {
53
+ bool enabled;
54
+
55
SPINLOCK spinlock;
56
Pvoid_t worknames_JudyHS;
57
size_t memory;
58
59
} workers_globals = { // workers globals, the base of all worknames
58
- .spinlock = NETDATA_SPINLOCK_INITIALIZER, // a lock for the worknames index
59
- .worknames_JudyHS = NULL, // the worknames index
60
+ .enabled = false,
61
+ .spinlock = NETDATA_SPINLOCK_INITIALIZER, // a lock for the worknames index
62
+ .worknames_JudyHS = NULL, // the worknames index
63
};
64
65
static __thread struct worker *worker = NULL; // the current thread worker
@@ -69,7 +72,14 @@ static inline usec_t worker_now_monotonic_usec(void) {
72
#endif
73
}
74
75
+void workers_utilization_enable(void) {
76
+ workers_globals.enabled = true;
77
+}
78
+
79
size_t workers_allocated_memory(void) {
80
+ if(!workers_globals.enabled)
81
+ return 0;
82
+
83
spinlock_lock(&workers_globals.spinlock);
84
size_t memory = workers_globals.memory;
85
spinlock_unlock(&workers_globals.spinlock);
@@ -78,7 +88,8 @@ size_t workers_allocated_memory(void) {
88
}
89
90
void worker_register(const char *name) {
81
- if(unlikely(worker)) return;
91
+ if(unlikely(worker || !workers_globals.enabled))
92
+ return;
93
94
worker = callocz(1, sizeof(struct worker));
95
worker->pid = gettid();
@@ -213,6 +224,7 @@ void worker_is_busy(size_t job_id) {
224
225
void worker_set_metric(size_t job_id, NETDATA_DOUBLE value) {
226
if(unlikely(!worker)) return;
227
+
228
if(unlikely(job_id >= WORKER_UTILIZATION_MAX_JOB_TYPES))
229
return;
230
@@ -247,6 +259,9 @@ void workers_foreach(const char *name, void (*callback)(
259
, NETDATA_DOUBLE *job_custom_values
260
)
261
, void *data) {
262
+ if(!workers_globals.enabled)
263
+ return;
264
+
265
spinlock_lock(&workers_globals.spinlock);
266
usec_t busy_time, delta;
267
size_t i, jobs_started, jobs_running;
libnetdata/worker_utilization/worker_utilization.h
+1
@@ -15,6 +15,7 @@ typedef enum __attribute__((packed)) {
15
WORKER_METRIC_INCREMENTAL_TOTAL = 4,
16
} WORKER_METRIC_TYPE;
17
18
+void workers_utilization_enable(void);
19
size_t workers_allocated_memory(void);
20
void worker_register(const char *name);
21
void worker_register_job_name(size_t job_id, const char *name);