summaryrefslogtreecommitdiffstats
path: root/libnetdata
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2023-09-04 08:57:49 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2023-09-04 08:57:49 +0000
commit5f647647b2683875bdbed970d955a9e5123284bd (patch)
tree66dcf1d9f7a60cd19dd9ac410e27d5f6ead521d3 /libnetdata
parentReleasing debian version 1.42.1-1. (diff)
downloadnetdata-5f647647b2683875bdbed970d955a9e5123284bd.tar.xz
netdata-5f647647b2683875bdbed970d955a9e5123284bd.zip
Merging upstream version 1.42.2.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'libnetdata')
-rw-r--r--libnetdata/dyn_conf/dyn_conf.c64
-rw-r--r--libnetdata/threads/threads.c21
-rw-r--r--libnetdata/threads/threads.h34
3 files changed, 103 insertions, 16 deletions
diff --git a/libnetdata/dyn_conf/dyn_conf.c b/libnetdata/dyn_conf/dyn_conf.c
index 3e098fb7f..00289fdf5 100644
--- a/libnetdata/dyn_conf/dyn_conf.c
+++ b/libnetdata/dyn_conf/dyn_conf.c
@@ -20,10 +20,19 @@ struct deferred_cfg_send {
struct deferred_cfg_send *next;
};
+bool dyncfg_shutdown = false;
struct deferred_cfg_send *deferred_configs = NULL;
pthread_mutex_t deferred_configs_lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t deferred_configs_cond = PTHREAD_COND_INITIALIZER;
+static void deferred_config_free(struct deferred_cfg_send *dcs)
+{
+ freez(dcs->plugin_name);
+ freez(dcs->module_name);
+ freez(dcs->job_name);
+ freez(dcs);
+}
+
static void deferred_config_push_back(const char *plugin_name, const char *module_name, const char *job_name)
{
struct deferred_cfg_send *deferred = callocz(1, sizeof(struct deferred_cfg_send));
@@ -34,6 +43,11 @@ static void deferred_config_push_back(const char *plugin_name, const char *modul
deferred->job_name = strdupz(job_name);
}
pthread_mutex_lock(&deferred_configs_lock);
+ if (dyncfg_shutdown) {
+ pthread_mutex_unlock(&deferred_configs_lock);
+ deferred_config_free(deferred);
+ return;
+ }
struct deferred_cfg_send *last = deferred_configs;
if (last == NULL)
deferred_configs = deferred;
@@ -46,25 +60,29 @@ static void deferred_config_push_back(const char *plugin_name, const char *modul
pthread_mutex_unlock(&deferred_configs_lock);
}
-static struct deferred_cfg_send *deferred_config_pop()
+static void deferred_configs_unlock()
+{
+ dyncfg_shutdown = true;
+ // if we get cancelled in pthread_cond_wait
+ // we will arrive at cancelled cleanup handler
+ // with mutex locked we need to unlock it
+ pthread_mutex_unlock(&deferred_configs_lock);
+}
+
+static struct deferred_cfg_send *deferred_config_pop(void *ptr)
{
pthread_mutex_lock(&deferred_configs_lock);
- while (deferred_configs == NULL)
+ while (deferred_configs == NULL) {
+ netdata_thread_cleanup_push(deferred_configs_unlock, ptr);
pthread_cond_wait(&deferred_configs_cond, &deferred_configs_lock);
+ netdata_thread_cleanup_pop(0);
+ }
struct deferred_cfg_send *deferred = deferred_configs;
deferred_configs = deferred_configs->next;
pthread_mutex_unlock(&deferred_configs_lock);
return deferred;
}
-static void deferred_config_free(struct deferred_cfg_send *dcs)
-{
- freez(dcs->plugin_name);
- freez(dcs->module_name);
- freez(dcs->job_name);
- freez(dcs);
-}
-
static int _get_list_of_plugins_json_cb(const DICTIONARY_ITEM *item, void *entry, void *data)
{
UNUSED(item);
@@ -870,10 +888,30 @@ int dyn_conf_init(void)
return 0;
}
-void *dyncfg_main(void *in)
+static void dyncfg_cleanup(void *ptr) {
+ struct netdata_static_thread *static_thread = (struct netdata_static_thread *) ptr;
+ static_thread->enabled = NETDATA_MAIN_THREAD_EXITING;
+
+ netdata_log_info("cleaning up...");
+
+ pthread_mutex_lock(&deferred_configs_lock);
+ dyncfg_shutdown = true;
+ while (deferred_configs != NULL) {
+ struct deferred_cfg_send *dcs = deferred_configs;
+ deferred_configs = dcs->next;
+ deferred_config_free(dcs);
+ }
+ pthread_mutex_unlock(&deferred_configs_lock);
+
+ static_thread->enabled = NETDATA_MAIN_THREAD_EXITED;
+}
+
+void *dyncfg_main(void *ptr)
{
+ netdata_thread_cleanup_push(dyncfg_cleanup, ptr);
+
while (!netdata_exit) {
- struct deferred_cfg_send *dcs = deferred_config_pop();
+ struct deferred_cfg_send *dcs = deferred_config_pop(ptr);
const DICTIONARY_ITEM *plugin_item = dictionary_get_and_acquire_item(plugins_dict, dcs->plugin_name);
if (plugin_item == NULL) {
error_report("DYNCFG, plugin %s not found", dcs->plugin_name);
@@ -905,5 +943,7 @@ void *dyncfg_main(void *in)
deferred_config_free(dcs);
dictionary_acquired_item_release(plugins_dict, plugin_item);
}
+
+ netdata_thread_cleanup_pop(1);
return NULL;
}
diff --git a/libnetdata/threads/threads.c b/libnetdata/threads/threads.c
index adce0463c..ae3c7106d 100644
--- a/libnetdata/threads/threads.c
+++ b/libnetdata/threads/threads.c
@@ -123,10 +123,12 @@ size_t netdata_threads_init(void) {
// --------------------------------------------------------------------
// get the required stack size of the threads of netdata
- netdata_threads_attr = callocz(1, sizeof(pthread_attr_t));
- i = pthread_attr_init(netdata_threads_attr);
- if(i != 0)
- fatal("pthread_attr_init() failed with code %d.", i);
+ if(!netdata_threads_attr) {
+ netdata_threads_attr = callocz(1, sizeof(pthread_attr_t));
+ i = pthread_attr_init(netdata_threads_attr);
+ if (i != 0)
+ fatal("pthread_attr_init() failed with code %d.", i);
+ }
size_t stacksize = 0;
i = pthread_attr_getstacksize(netdata_threads_attr, &stacksize);
@@ -159,6 +161,17 @@ void netdata_threads_init_after_fork(size_t stacksize) {
}
// ----------------------------------------------------------------------------
+// threads init for external plugins
+
+void netdata_threads_init_for_external_plugins(size_t stacksize) {
+ size_t default_stacksize = netdata_threads_init();
+ if(default_stacksize < 1 * 1024 * 1024)
+ default_stacksize = 1 * 1024 * 1024;
+
+ netdata_threads_init_after_fork(stacksize ? stacksize : default_stacksize);
+}
+
+// ----------------------------------------------------------------------------
// netdata_thread_create
void rrdset_thread_rda_free(void);
diff --git a/libnetdata/threads/threads.h b/libnetdata/threads/threads.h
index ad31b8816..acb4e6ba2 100644
--- a/libnetdata/threads/threads.h
+++ b/libnetdata/threads/threads.h
@@ -20,12 +20,46 @@ typedef enum {
typedef pthread_t netdata_thread_t;
+struct netdata_static_thread {
+ // the name of the thread as it should appear in the logs
+ char *name;
+
+ // the section of netdata.conf to check if this is enabled or not
+ char *config_section;
+
+ // the name of the config option to check if it is true or false
+ char *config_name;
+
+ // the current status of the thread
+ volatile sig_atomic_t enabled;
+
+ // internal use, to maintain a pointer to the created thread
+ netdata_thread_t *thread;
+
+ // an initialization function to run before spawning the thread
+ void (*init_routine) (void);
+
+ // the threaded worker
+ void *(*start_routine) (void *);
+
+ // the environment variable to create
+ char *env_name;
+
+ // global variable
+ bool *global_variable;
+};
+
+#define NETDATA_MAIN_THREAD_RUNNING CONFIG_BOOLEAN_YES
+#define NETDATA_MAIN_THREAD_EXITING (CONFIG_BOOLEAN_YES + 1)
+#define NETDATA_MAIN_THREAD_EXITED CONFIG_BOOLEAN_NO
+
#define NETDATA_THREAD_TAG_MAX 100
const char *netdata_thread_tag(void);
int netdata_thread_tag_exists(void);
size_t netdata_threads_init(void);
void netdata_threads_init_after_fork(size_t stacksize);
+void netdata_threads_init_for_external_plugins(size_t stacksize);
int netdata_thread_create(netdata_thread_t *thread, const char *tag, NETDATA_THREAD_OPTIONS options, void *(*start_routine) (void *), void *arg);