@cryptotaxi247 / netdata-1 / commits / a7a35f1a4

limit the max number of threads based on memory too (#20192)

Costa Tsaousis committed Apr 28, 2025 at 11:15 UTC a7a35f1a495bcbf194ae83d5287a01aac7d5e386
3 files changed +44 -15
src/daemon/config/netdata-conf-global.c
+42
@@ -75,8 +75,50 @@ void netdata_conf_glibc_malloc_initialize(size_t wanted_arenas, size_t trim_thre
75 #endif
76 }
77
78 +static size_t default_stacksize = 8ULL * 1024 * 1024; // default is 8MiB
79 +
80 +static void netdata_conf_stack_size(void) {
81 + FUNCTION_RUN_ONCE();
82 +
83 + // initialize thread - this is required before the first nd_thread_create()
84 + default_stacksize = netdata_threads_init();
85 +
86 + // musl default thread stack size is 128k, let's set it to a higher value to avoid random crashes
87 + if (default_stacksize < 1 * 1024 * 1024)
88 + default_stacksize = 1 * 1024 * 1024;
89 +
90 + netdata_threads_set_stack_size(
91 + (size_t)inicfg_get_size_bytes(&netdata_config, CONFIG_SECTION_GLOBAL, "pthread stack size", default_stacksize));
92 +}
93 +
94 +void netdata_conf_reset_stack_size(void) {
95 + netdata_threads_set_stack_size(default_stacksize);
96 +}
97 +
98 void libuv_initialize(void) {
99 + netdata_conf_stack_size();
100 +
101 + // libuv worker threads need to limited by the amount of memory the system has,
102 + // otherwise the system may refuse to create that many threads.
103 +
104 + // our original target
105 libuv_worker_threads = (int)netdata_conf_cpus() * 6;
106 +
107 + OS_SYSTEM_MEMORY mem = os_system_memory(true);
108 + if(OS_SYSTEM_MEMORY_OK(mem)) {
109 + // we have memory information
110 + uint64_t mem_for_threads1 = mem.ram_total_bytes / 20;
111 + uint64_t mem_for_threads2 = mem.ram_available_bytes / 10;
112 + uint64_t mem_for_threads = MIN(mem_for_threads1, mem_for_threads2);
113 +
114 + int max_allowed_threads = (int)HOWMANY(mem_for_threads, default_stacksize);
115 + if(max_allowed_threads < MIN_LIBUV_WORKER_THREADS)
116 + max_allowed_threads = MIN_LIBUV_WORKER_THREADS;
117 +
118 + if(libuv_worker_threads > max_allowed_threads)
119 + libuv_worker_threads = max_allowed_threads;
120 + }
121 +
122 libuv_worker_threads = MIN(MAX_LIBUV_WORKER_THREADS, MAX(MIN_LIBUV_WORKER_THREADS, libuv_worker_threads));
123
124 libuv_worker_threads = (int)inicfg_get_number_range(
src/daemon/config/netdata-conf-global.h
+1
@@ -13,6 +13,7 @@ size_t netdata_conf_cpus(void);
13 void libuv_initialize(void);
14
15 void netdata_conf_glibc_malloc_initialize(size_t wanted_arenas, size_t trim_threshold);
16 +void netdata_conf_reset_stack_size(void);
17
18 #include "netdata-conf.h"
19
src/daemon/main.c
+1 -15
@@ -258,7 +258,6 @@ int netdata_main(int argc, char **argv) {
258 int i;
259 int config_loaded = 0;
260 bool close_open_fds = true; (void)close_open_fds;
261 - size_t default_stacksize;
261 const char *user = NULL;
262
263 #ifdef OS_WINDOWS
@@ -832,18 +831,6 @@ int netdata_main(int argc, char **argv) {
831
832 nd_profile_setup();
833
835 - // ----------------------------------------------------------------------------------------------------------------
836 - delta_startup_time("stack size");
837 -
838 - // initialize thread - this is required before the first nd_thread_create()
839 - default_stacksize = netdata_threads_init();
840 -
841 - // musl default thread stack size is 128k, let's set it to a higher value to avoid random crashes
842 - if (default_stacksize < 1 * 1024 * 1024)
843 - default_stacksize = 1 * 1024 * 1024;
844 -
845 - netdata_threads_set_stack_size(default_stacksize);
846 -
834 // ----------------------------------------------------------------------------------------------------------------
835 delta_startup_time("crash reports");
836
@@ -1021,8 +1008,7 @@ int netdata_main(int argc, char **argv) {
1008 // ----------------------------------------------------------------------------------------------------------------
1009 delta_startup_time("threads after fork");
1010
1024 - netdata_threads_set_stack_size(
1025 - (size_t)inicfg_get_size_bytes(&netdata_config, CONFIG_SECTION_GLOBAL, "pthread stack size", default_stacksize));
1011 + netdata_conf_reset_stack_size();
1012
1013 // ----------------------------------------------------------------------------------------------------------------
1014 delta_startup_time("registry");