1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
// SPDX-License-Identifier: GPL-3.0-or-later
#include "../libnetdata.h"
#define CPUS_FOR_COLLECTORS 0
#define CPUS_FOR_NETDATA 1
long os_get_system_cpus_cached(bool cache, bool for_netdata) {
static long processors[2] = { 0, 0 };
int index = for_netdata ? CPUS_FOR_NETDATA : CPUS_FOR_COLLECTORS;
if(likely(cache && processors[index] > 0))
return processors[index];
#if defined(OS_FREEBSD) || defined(OS_MACOS)
#if defined(OS_MACOS)
#define HW_CPU_NAME "hw.logicalcpu"
#else
#define HW_CPU_NAME "hw.ncpu"
#endif
int32_t tmp_processors;
bool error = false;
if (unlikely(GETSYSCTL_BY_NAME(HW_CPU_NAME, tmp_processors)))
error = true;
else
processors[index] = tmp_processors;
if(processors[index] < 1) {
processors[index] = 1;
if(error)
netdata_log_error("Assuming system has %ld processors.", processors[index]);
}
return processors[index];
#elif defined(OS_LINUX)
char filename[FILENAME_MAX + 1];
snprintfz(filename, FILENAME_MAX, "%s/proc/stat",
(!for_netdata && netdata_configured_host_prefix) ? netdata_configured_host_prefix : "");
procfile *ff = procfile_open(filename, NULL, PROCFILE_FLAG_DEFAULT);
if(!ff) {
processors[index] = 1;
netdata_log_error("Cannot open file '%s'. Assuming system has %ld processors.", filename, processors[index]);
return processors[index];
}
ff = procfile_readall(ff);
if(!ff) {
processors[index] = 1;
netdata_log_error("Cannot open file '%s'. Assuming system has %ld processors.", filename, processors[index]);
return processors[index];
}
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)
tmp_processors++;
}
procfile_close(ff);
processors[index] = --tmp_processors;
if(processors[index] < 1)
processors[index] = 1;
netdata_log_debug(D_SYSTEM, "System has %ld processors.", processors[index]);
return processors[index];
#elif defined(OS_WINDOWS)
SYSTEM_INFO sysInfo;
GetSystemInfo(&sysInfo);
processors[index] = sysInfo.dwNumberOfProcessors;
if(processors[index] < 1) {
processors[index] = 1;
netdata_log_error("Assuming system has %ld processors.", processors[index]);
}
return processors[index];
#else
processors[index] = 1;
return processors[index];
#endif
}
|