summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--aclk/aclk.c2
-rw-r--r--collectors/apps.plugin/apps_plugin.c4
-rw-r--r--collectors/proc.plugin/proc_stat.c6
-rw-r--r--daemon/main.c2
-rw-r--r--libnetdata/os.c61
-rw-r--r--libnetdata/os.h5
-rw-r--r--web/server/static/static-threaded.c2
7 files changed, 47 insertions, 35 deletions
diff --git a/aclk/aclk.c b/aclk/aclk.c
index 3b035b849d..fb47f2347b 100644
--- a/aclk/aclk.c
+++ b/aclk/aclk.c
@@ -288,7 +288,7 @@ static void puback_callback(uint16_t packet_id)
static int read_query_thread_count()
{
- int threads = MIN(processors/2, 6);
+ int threads = MIN(get_system_cpus()/2, 6);
threads = MAX(threads, 2);
threads = config_get_number(CONFIG_SECTION_CLOUD, "query thread count", threads);
if(threads < 1) {
diff --git a/collectors/apps.plugin/apps_plugin.c b/collectors/apps.plugin/apps_plugin.c
index 4de5915fd4..75f4a50685 100644
--- a/collectors/apps.plugin/apps_plugin.c
+++ b/collectors/apps.plugin/apps_plugin.c
@@ -3359,7 +3359,7 @@ static void normalize_utilization(struct target *root) {
// here we try to eliminate them by disabling childs processing either for specific dimensions
// or entirely. Of course, either way, we disable it just a single iteration.
- kernel_uint_t max_time = processors * time_factor * RATES_DETAIL;
+ kernel_uint_t max_time = get_system_cpus() * time_factor * RATES_DETAIL;
kernel_uint_t utime = 0, cutime = 0, stime = 0, cstime = 0, gtime = 0, cgtime = 0, minflt = 0, cminflt = 0, majflt = 0, cmajflt = 0;
if(global_utime > max_time) global_utime = max_time;
@@ -4898,7 +4898,7 @@ int main(int argc, char **argv) {
#endif
get_system_pid_max();
- get_system_cpus();
+ get_system_cpus_uncached();
parse_args(argc, argv);
diff --git a/collectors/proc.plugin/proc_stat.c b/collectors/proc.plugin/proc_stat.c
index 33fe932343..4fe332e524 100644
--- a/collectors/proc.plugin/proc_stat.c
+++ b/collectors/proc.plugin/proc_stat.c
@@ -483,7 +483,7 @@ int do_proc_stat(int update_every, usec_t dt) {
*time_in_state_filename = NULL, *schedstat_filename = NULL, *cpuidle_name_filename = NULL, *cpuidle_time_filename = NULL;
static const RRDVAR_ACQUIRED *cpus_var = NULL;
static int accurate_freq_avail = 0, accurate_freq_is_used = 0;
- size_t cores_found = (size_t)processors;
+ size_t cores_found = (size_t)get_system_cpus();
if(unlikely(do_cpu == -1)) {
do_cpu = config_get_boolean("plugin:proc:/proc/stat", "cpu utilization", CONFIG_BOOLEAN_YES);
@@ -494,7 +494,7 @@ int do_proc_stat(int update_every, usec_t dt) {
do_processes = config_get_boolean("plugin:proc:/proc/stat", "processes running", CONFIG_BOOLEAN_YES);
// give sane defaults based on the number of processors
- if(unlikely(processors > 50)) {
+ if(unlikely(get_system_cpus() > 50)) {
// the system has too many processors
keep_per_core_fds_open = CONFIG_BOOLEAN_NO;
do_core_throttle_count = CONFIG_BOOLEAN_NO;
@@ -510,7 +510,7 @@ int do_proc_stat(int update_every, usec_t dt) {
do_cpu_freq = CONFIG_BOOLEAN_YES;
do_cpuidle = CONFIG_BOOLEAN_YES;
}
- if(unlikely(processors > 24)) {
+ if(unlikely(get_system_cpus() > 24)) {
// the system has too many processors
keep_cpuidle_fds_open = CONFIG_BOOLEAN_NO;
}
diff --git a/daemon/main.c b/daemon/main.c
index 6b591385d6..97f3e85fa6 100644
--- a/daemon/main.c
+++ b/daemon/main.c
@@ -751,7 +751,7 @@ static void get_netdata_configured_variables() {
// get various system parameters
get_system_HZ();
- get_system_cpus();
+ get_system_cpus_uncached();
get_system_pid_max();
diff --git a/libnetdata/os.c b/libnetdata/os.c
index 196288a6ae..ad0211672d 100644
--- a/libnetdata/os.c
+++ b/libnetdata/os.c
@@ -6,60 +6,71 @@
// system functions
// to retrieve settings of the system
-int processors = 1;
-long get_system_cpus(void) {
- processors = 1;
+long get_system_cpus_with_cache(bool cache) {
+ static long processors = 0;
+
+ if(likely(cache && processors > 0))
+ return processors;
#ifdef __APPLE__
int32_t tmp_processors;
- if (unlikely(GETSYSCTL_BY_NAME("hw.logicalcpu", tmp_processors))) {
- error("Assuming system has %d processors.", processors);
- } else {
- processors = tmp_processors;
- }
+ if (unlikely(GETSYSCTL_BY_NAME("hw.logicalcpu", tmp_processors)))
+ error("Assuming system has %d processors.", processors);
+ else
+ processors = tmp_processors;
- return processors;
+ if(processors < 1)
+ processors = 1;
+
+ return processors;
#elif __FreeBSD__
int32_t tmp_processors;
- if (unlikely(GETSYSCTL_BY_NAME("hw.ncpu", tmp_processors))) {
- error("Assuming system has %d processors.", processors);
- } else {
- processors = tmp_processors;
- }
+ if (unlikely(GETSYSCTL_BY_NAME("hw.ncpu", tmp_processors)))
+ error("Assuming system has %d processors.", processors);
+ else
+ processors = tmp_processors;
- return processors;
+ if(processors < 1)
+ processors = 1;
+
+ return processors;
#else
char filename[FILENAME_MAX + 1];
- snprintfz(filename, FILENAME_MAX, "%s/proc/stat", netdata_configured_host_prefix);
+ snprintfz(filename, FILENAME_MAX, "%s/proc/stat", netdata_configured_host_prefix?netdata_configured_host_prefix:"");
procfile *ff = procfile_open(filename, NULL, PROCFILE_FLAG_DEFAULT);
if(!ff) {
- error("Cannot open file '%s'. Assuming system has %d processors.", filename, processors);
+ processors = 1;
+ error("Cannot open file '%s'. Assuming system has %ld processors.", filename, processors);
return processors;
}
ff = procfile_readall(ff);
if(!ff) {
- error("Cannot open file '%s'. Assuming system has %d processors.", filename, processors);
+ processors = 1;
+ error("Cannot open file '%s'. Assuming system has %ld processors.", filename, processors);
return processors;
}
- processors = 0;
+ long tmp_processors = 0;
unsigned int i;
for(i = 0; i < procfile_lines(ff); i++) {
if(!procfile_linewords(ff, i)) continue;
- if(strncmp(procfile_lineword(ff, i, 0), "cpu", 3) == 0) processors++;
+ if(strncmp(procfile_lineword(ff, i, 0), "cpu", 3) == 0)
+ tmp_processors++;
}
- processors--;
- if(processors < 1) processors = 1;
-
procfile_close(ff);
- debug(D_SYSTEM, "System has %d processors.", processors);
+ processors = --tmp_processors;
+
+ if(processors < 1)
+ processors = 1;
+
+ debug(D_SYSTEM, "System has %ld processors.", processors);
return processors;
#endif /* __APPLE__, __FreeBSD__ */
@@ -90,7 +101,7 @@ pid_t get_system_pid_max(void) {
read = 1;
char filename[FILENAME_MAX + 1];
- snprintfz(filename, FILENAME_MAX, "%s/proc/sys/kernel/pid_max", netdata_configured_host_prefix);
+ snprintfz(filename, FILENAME_MAX, "%s/proc/sys/kernel/pid_max", netdata_configured_host_prefix?netdata_configured_host_prefix:"");
unsigned long long max = 0;
if(read_single_number_file(filename, &max) != 0) {
diff --git a/libnetdata/os.h b/libnetdata/os.h
index 67abf0be45..cfccc0f56b 100644
--- a/libnetdata/os.h
+++ b/libnetdata/os.h
@@ -48,8 +48,9 @@ int getsysctl_by_name(const char *name, void *ptr, size_t len);
extern const char *os_type;
-extern int processors;
-long get_system_cpus(void);
+#define get_system_cpus() get_system_cpus_with_cache(true)
+#define get_system_cpus_uncached() get_system_cpus_with_cache(false)
+long get_system_cpus_with_cache(bool cache);
extern pid_t pid_max;
pid_t get_system_pid_max(void);
diff --git a/web/server/static/static-threaded.c b/web/server/static/static-threaded.c
index 26e9a47bda..f4f6a11cfe 100644
--- a/web/server/static/static-threaded.c
+++ b/web/server/static/static-threaded.c
@@ -502,7 +502,7 @@ void *socket_listen_main_static_threaded(void *ptr) {
// 6 threads is the optimal value
// since 6 are the parallel connections browsers will do
// so, if the machine has more CPUs, avoid using resources unnecessarily
- int def_thread_count = (processors > 6) ? 6 : processors;
+ int def_thread_count = (get_system_cpus() > 6) ? 6 : (int)get_system_cpus();
if (!strcmp(config_get(CONFIG_SECTION_WEB, "mode", ""),"single-threaded")) {
info("Running web server with one thread, because mode is single-threaded");