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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
// SPDX-License-Identifier: GPL-3.0-or-later
#include "os.h"
// ----------------------------------------------------------------------------
// system functions
// to retrieve settings of the system
int processors = 1;
long get_system_cpus(void) {
processors = 1;
#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;
}
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;
}
return processors;
#else
char filename[FILENAME_MAX + 1];
snprintfz(filename, FILENAME_MAX, "%s/proc/stat", 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);
return processors;
}
ff = procfile_readall(ff);
if(!ff) {
error("Cannot open file '%s'. Assuming system has %d processors.", filename, processors);
return processors;
}
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++;
}
processors--;
if(processors < 1) processors = 1;
procfile_close(ff);
debug(D_SYSTEM, "System has %d processors.", processors);
return processors;
#endif /* __APPLE__, __FreeBSD__ */
}
pid_t pid_max = 32768;
pid_t get_system_pid_max(void) {
#ifdef __APPLE__
// As we currently do not know a solution to query pid_max from the os
// we use the number defined in bsd/sys/proc_internal.h in XNU sources
pid_max = 99999;
return pid_max;
#elif __FreeBSD__
int32_t tmp_pid_max;
if (unlikely(GETSYSCTL_BY_NAME("kern.pid_max", tmp_pid_max))) {
pid_max = 99999;
error("Assuming system's maximum pid is %d.", pid_max);
} else {
pid_max = tmp_pid_max;
}
return pid_max;
#else
static char read = 0;
if(unlikely(read)) return pid_max;
read = 1;
char filename[FILENAME_MAX + 1];
snprintfz(filename, FILENAME_MAX, "%s/proc/sys/kernel/pid_max", netdata_configured_host_prefix);
unsigned long long max = 0;
if(read_single_number_file(filename, &max) != 0) {
error("Cannot open file '%s'. Assuming system supports %d pids.", filename, pid_max);
return pid_max;
}
if(!max) {
error("Cannot parse file '%s'. Assuming system supports %d pids.", filename, pid_max);
return pid_max;
}
pid_max = (pid_t) max;
return pid_max;
#endif /* __APPLE__, __FreeBSD__ */
}
unsigned int system_hz;
void get_system_HZ(void) {
long ticks;
if ((ticks = sysconf(_SC_CLK_TCK)) == -1) {
error("Cannot get system clock ticks");
}
system_hz = (unsigned int) ticks;
}
// =====================================================================================================================
// FreeBSD
#if __FreeBSD__
const char *os_type = "freebsd";
int getsysctl_by_name(const char *name, void *ptr, size_t len) {
size_t nlen = len;
if (unlikely(sysctlbyname(name, ptr, &nlen, NULL, 0) == -1)) {
error("FREEBSD: sysctl(%s...) failed: %s", name, strerror(errno));
return 1;
}
if (unlikely(nlen != len)) {
error("FREEBSD: sysctl(%s...) expected %lu, got %lu", name, (unsigned long)len, (unsigned long)nlen);
return 1;
}
return 0;
}
int getsysctl_simple(const char *name, int *mib, size_t miblen, void *ptr, size_t len) {
size_t nlen = len;
if (unlikely(!mib[0]))
if (unlikely(getsysctl_mib(name, mib, miblen)))
return 1;
if (unlikely(sysctl(mib, miblen, ptr, &nlen, NULL, 0) == -1)) {
error("FREEBSD: sysctl(%s...) failed: %s", name, strerror(errno));
return 1;
}
if (unlikely(nlen != len)) {
error("FREEBSD: sysctl(%s...) expected %lu, got %lu", name, (unsigned long)len, (unsigned long)nlen);
return 1;
}
return 0;
}
int getsysctl(const char *name, int *mib, size_t miblen, void *ptr, size_t *len) {
size_t nlen = *len;
if (unlikely(!mib[0]))
if (unlikely(getsysctl_mib(name, mib, miblen)))
return 1;
if (unlikely(sysctl(mib, miblen, ptr, len, NULL, 0) == -1)) {
error("FREEBSD: sysctl(%s...) failed: %s", name, strerror(errno));
return 1;
}
if (unlikely(ptr != NULL && nlen != *len)) {
error("FREEBSD: sysctl(%s...) expected %lu, got %lu", name, (unsigned long)*len, (unsigned long)nlen);
return 1;
}
return 0;
}
int getsysctl_mib(const char *name, int *mib, size_t len) {
size_t nlen = len;
if (unlikely(sysctlnametomib(name, mib, &nlen) == -1)) {
error("FREEBSD: sysctl(%s...) failed: %s", name, strerror(errno));
return 1;
}
if (unlikely(nlen != len)) {
error("FREEBSD: sysctl(%s...) expected %lu, got %lu", name, (unsigned long)len, (unsigned long)nlen);
return 1;
}
return 0;
}
#endif
// =====================================================================================================================
// MacOS
#if __APPLE__
const char *os_type = "macos";
int getsysctl_by_name(const char *name, void *ptr, size_t len) {
size_t nlen = len;
if (unlikely(sysctlbyname(name, ptr, &nlen, NULL, 0) == -1)) {
error("MACOS: sysctl(%s...) failed: %s", name, strerror(errno));
return 1;
}
if (unlikely(nlen != len)) {
error("MACOS: sysctl(%s...) expected %lu, got %lu", name, (unsigned long)len, (unsigned long)nlen);
return 1;
}
return 0;
}
#endif
// =====================================================================================================================
// Linux
#if __linux__
const char *os_type = "linux";
#endif
|