@cryptotaxi247 / netdata-1 / commits / c728bfc1e

fix get_system_cpus() (#14116)

Costa Tsaousis committed Dec 9, 2022 at 18:56 UTC c728bfc1eb00a662a24010d2b33e287d44d2a09b
7 files changed +47 -35
aclk/aclk.c
+1 -1
@@ -288,7 +288,7 @@ static void puback_callback(uint16_t packet_id)
288
289 static int read_query_thread_count()
290 {
291 - int threads = MIN(processors/2, 6);
291 + int threads = MIN(get_system_cpus()/2, 6);
292 threads = MAX(threads, 2);
293 threads = config_get_number(CONFIG_SECTION_CLOUD, "query thread count", threads);
294 if(threads < 1) {
collectors/apps.plugin/apps_plugin.c
+2 -2
@@ -3359,7 +3359,7 @@ static void normalize_utilization(struct target *root) {
3359 // here we try to eliminate them by disabling childs processing either for specific dimensions
3360 // or entirely. Of course, either way, we disable it just a single iteration.
3361
3362 - kernel_uint_t max_time = processors * time_factor * RATES_DETAIL;
3362 + kernel_uint_t max_time = get_system_cpus() * time_factor * RATES_DETAIL;
3363 kernel_uint_t utime = 0, cutime = 0, stime = 0, cstime = 0, gtime = 0, cgtime = 0, minflt = 0, cminflt = 0, majflt = 0, cmajflt = 0;
3364
3365 if(global_utime > max_time) global_utime = max_time;
@@ -4898,7 +4898,7 @@ int main(int argc, char **argv) {
4898 #endif
4899
4900 get_system_pid_max();
4901 - get_system_cpus();
4901 + get_system_cpus_uncached();
4902
4903 parse_args(argc, argv);
4904
collectors/proc.plugin/proc_stat.c
+3 -3
@@ -483,7 +483,7 @@ int do_proc_stat(int update_every, usec_t dt) {
483 *time_in_state_filename = NULL, *schedstat_filename = NULL, *cpuidle_name_filename = NULL, *cpuidle_time_filename = NULL;
484 static const RRDVAR_ACQUIRED *cpus_var = NULL;
485 static int accurate_freq_avail = 0, accurate_freq_is_used = 0;
486 - size_t cores_found = (size_t)processors;
486 + size_t cores_found = (size_t)get_system_cpus();
487
488 if(unlikely(do_cpu == -1)) {
489 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) {
494 do_processes = config_get_boolean("plugin:proc:/proc/stat", "processes running", CONFIG_BOOLEAN_YES);
495
496 // give sane defaults based on the number of processors
497 - if(unlikely(processors > 50)) {
497 + if(unlikely(get_system_cpus() > 50)) {
498 // the system has too many processors
499 keep_per_core_fds_open = CONFIG_BOOLEAN_NO;
500 do_core_throttle_count = CONFIG_BOOLEAN_NO;
@@ -510,7 +510,7 @@ int do_proc_stat(int update_every, usec_t dt) {
510 do_cpu_freq = CONFIG_BOOLEAN_YES;
511 do_cpuidle = CONFIG_BOOLEAN_YES;
512 }
513 - if(unlikely(processors > 24)) {
513 + if(unlikely(get_system_cpus() > 24)) {
514 // the system has too many processors
515 keep_cpuidle_fds_open = CONFIG_BOOLEAN_NO;
516 }
daemon/main.c
+1 -1
@@ -751,7 +751,7 @@ static void get_netdata_configured_variables() {
751 // get various system parameters
752
753 get_system_HZ();
754 - get_system_cpus();
754 + get_system_cpus_uncached();
755 get_system_pid_max();
756
757
libnetdata/os.c
+36 -25
@@ -6,60 +6,71 @@
6 // system functions
7 // to retrieve settings of the system
8
9 -int processors = 1;
10 -long get_system_cpus(void) {
11 - processors = 1;
9 +long get_system_cpus_with_cache(bool cache) {
10 + static long processors = 0;
11 +
12 + if(likely(cache && processors > 0))
13 + return processors;
14
15 #ifdef __APPLE__
16 int32_t tmp_processors;
17
16 - if (unlikely(GETSYSCTL_BY_NAME("hw.logicalcpu", tmp_processors))) {
17 - error("Assuming system has %d processors.", processors);
18 - } else {
19 - processors = tmp_processors;
20 - }
18 + if (unlikely(GETSYSCTL_BY_NAME("hw.logicalcpu", tmp_processors)))
19 + error("Assuming system has %d processors.", processors);
20 + else
21 + processors = tmp_processors;
22
22 - return processors;
23 + if(processors < 1)
24 + processors = 1;
25 +
26 + return processors;
27 #elif __FreeBSD__
28 int32_t tmp_processors;
29
26 - if (unlikely(GETSYSCTL_BY_NAME("hw.ncpu", tmp_processors))) {
27 - error("Assuming system has %d processors.", processors);
28 - } else {
29 - processors = tmp_processors;
30 - }
30 + if (unlikely(GETSYSCTL_BY_NAME("hw.ncpu", tmp_processors)))
31 + error("Assuming system has %d processors.", processors);
32 + else
33 + processors = tmp_processors;
34
32 - return processors;
35 + if(processors < 1)
36 + processors = 1;
37 +
38 + return processors;
39 #else
40
41 char filename[FILENAME_MAX + 1];
36 - snprintfz(filename, FILENAME_MAX, "%s/proc/stat", netdata_configured_host_prefix);
42 + snprintfz(filename, FILENAME_MAX, "%s/proc/stat", netdata_configured_host_prefix?netdata_configured_host_prefix:"");
43
44 procfile *ff = procfile_open(filename, NULL, PROCFILE_FLAG_DEFAULT);
45 if(!ff) {
40 - error("Cannot open file '%s'. Assuming system has %d processors.", filename, processors);
46 + processors = 1;
47 + error("Cannot open file '%s'. Assuming system has %ld processors.", filename, processors);
48 return processors;
49 }
50
51 ff = procfile_readall(ff);
52 if(!ff) {
46 - error("Cannot open file '%s'. Assuming system has %d processors.", filename, processors);
53 + processors = 1;
54 + error("Cannot open file '%s'. Assuming system has %ld processors.", filename, processors);
55 return processors;
56 }
57
50 - processors = 0;
58 + long tmp_processors = 0;
59 unsigned int i;
60 for(i = 0; i < procfile_lines(ff); i++) {
61 if(!procfile_linewords(ff, i)) continue;
62
55 - if(strncmp(procfile_lineword(ff, i, 0), "cpu", 3) == 0) processors++;
63 + if(strncmp(procfile_lineword(ff, i, 0), "cpu", 3) == 0)
64 + tmp_processors++;
65 }
57 - processors--;
58 - if(processors < 1) processors = 1;
59 -
66 procfile_close(ff);
67
62 - debug(D_SYSTEM, "System has %d processors.", processors);
68 + processors = --tmp_processors;
69 +
70 + if(processors < 1)
71 + processors = 1;
72 +
73 + debug(D_SYSTEM, "System has %ld processors.", processors);
74 return processors;
75
76 #endif /* __APPLE__, __FreeBSD__ */
@@ -90,7 +101,7 @@ pid_t get_system_pid_max(void) {
101 read = 1;
102
103 char filename[FILENAME_MAX + 1];
93 - snprintfz(filename, FILENAME_MAX, "%s/proc/sys/kernel/pid_max", netdata_configured_host_prefix);
104 + snprintfz(filename, FILENAME_MAX, "%s/proc/sys/kernel/pid_max", netdata_configured_host_prefix?netdata_configured_host_prefix:"");
105
106 unsigned long long max = 0;
107 if(read_single_number_file(filename, &max) != 0) {
libnetdata/os.h
+3 -2
@@ -48,8 +48,9 @@ int getsysctl_by_name(const char *name, void *ptr, size_t len);
48
49 extern const char *os_type;
50
51 -extern int processors;
52 -long get_system_cpus(void);
51 +#define get_system_cpus() get_system_cpus_with_cache(true)
52 +#define get_system_cpus_uncached() get_system_cpus_with_cache(false)
53 +long get_system_cpus_with_cache(bool cache);
54
55 extern pid_t pid_max;
56 pid_t get_system_pid_max(void);
web/server/static/static-threaded.c
+1 -1
@@ -502,7 +502,7 @@ void *socket_listen_main_static_threaded(void *ptr) {
502 // 6 threads is the optimal value
503 // since 6 are the parallel connections browsers will do
504 // so, if the machine has more CPUs, avoid using resources unnecessarily
505 - int def_thread_count = (processors > 6) ? 6 : processors;
505 + int def_thread_count = (get_system_cpus() > 6) ? 6 : (int)get_system_cpus();
506
507 if (!strcmp(config_get(CONFIG_SECTION_WEB, "mode", ""),"single-threaded")) {
508 info("Running web server with one thread, because mode is single-threaded");