#include "common.h"
-#ifdef __FreeBSD__
+#ifdef __APPLE__
+#define INHERIT_NONE 0
+#endif /* __APPLE__ */
+#if defined(__FreeBSD__) || defined(__APPLE__)
# define O_NOATIME 0
# define MADV_DONTFORK INHERIT_NONE
-#endif /* __FreeBSD__ */
+#endif /* __FreeBSD__ || __APPLE__*/
+
+char *netdata_configured_hostname = NULL;
+char *netdata_configured_config_dir = NULL;
+char *netdata_configured_log_dir = NULL;
+char *netdata_configured_plugins_dir = NULL;
+char *netdata_configured_web_dir = NULL;
+char *netdata_configured_cache_dir = NULL;
+char *netdata_configured_varlib_dir = NULL;
+char *netdata_configured_home_dir = NULL;
+char *netdata_configured_host_prefix = NULL;
-char *global_host_prefix = "";
int enable_ksm = 1;
volatile sig_atomic_t netdata_exit = 0;
+const char *os_type = NETDATA_OS_TYPE;
+const char *program_version = VERSION;
// ----------------------------------------------------------------------------
// memory allocation functions that handle failures
free(ptr);
}
+void json_escape_string(char *dst, const char *src, size_t size) {
+ const char *t;
+ char *d = dst, *e = &dst[size - 1];
+
+ for(t = src; *t && d < e ;t++) {
+ if(unlikely(*t == '\\' || *t == '"')) {
+ if(unlikely(d + 1 >= e)) break;
+ *d++ = '\\';
+ }
+ *d++ = *t;
+ }
+
+ *d = '\0';
+}
+
int sleep_usec(usec_t usec) {
#ifndef NETDATA_WITH_USLEEP
}
*/
-
+/*
// http://isthe.com/chongo/tech/comp/fnv/#FNV-1a
uint32_t simple_hash(const char *name) {
unsigned char *s = (unsigned char *) name;
}
return hval;
}
+*/
/*
// http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
pid_t gettid(void) {
#ifdef __FreeBSD__
return (pid_t)pthread_getthreadid_np();
+#elif defined(__APPLE__)
+ uint64_t curthreadid;
+ pthread_threadid_np(NULL, &curthreadid);
+ return (pid_t)curthreadid;
#else
return (pid_t)syscall(SYS_gettid);
-#endif /* __FreeBSD__ */
+#endif /* __FreeBSD__, __APPLE__*/
}
char *fgets_trim_len(char *buf, size_t buf_size, FILE *fp, size_t *len) {
return s;
}
-char *strncpyz(char *dst, const char *src, size_t n) {
- char *p = dst;
-
- while (*src && n--)
- *dst++ = *src++;
-
- *dst = '\0';
-
- return p;
-}
-
int vsnprintfz(char *dst, size_t n, const char *fmt, va_list args) {
int size = vsnprintf(dst, n, fmt, args);
long get_system_cpus(void) {
processors = 1;
+ #ifdef __APPLE__
+ int32_t tmp_processors;
+
+ if (unlikely(GETSYSCTL("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("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", global_host_prefix);
+ snprintfz(filename, FILENAME_MAX, "%s/proc/stat", netdata_configured_host_prefix);
procfile *ff = procfile_open(filename, NULL, PROCFILE_FLAG_DEFAULT);
if(!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) {
- char filename[FILENAME_MAX + 1];
- snprintfz(filename, FILENAME_MAX, "%s/proc/sys/kernel/pid_max", global_host_prefix);
- procfile *ff = procfile_open(filename, NULL, PROCFILE_FLAG_DEFAULT);
- if(!ff) {
- error("Cannot open file '%s'. Assuming system supports %d pids.", filename, pid_max);
+ #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;
- ff = procfile_readall(ff);
- if(!ff) {
- error("Cannot read file '%s'. Assuming system supports %d pids.", filename, pid_max);
+ if (unlikely(GETSYSCTL("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;
}
- pid_max = (pid_t)atoi(procfile_lineword(ff, 0, 0));
- if(!pid_max) {
- procfile_close(ff);
- pid_max = 32768;
+ if(!max) {
error("Cannot parse file '%s'. Assuming system supports %d pids.", filename, pid_max);
return pid_max;
}
- procfile_close(ff);
- debug(D_SYSTEM, "System supports %d pids.", pid_max);
+ pid_max = (pid_t) max;
return pid_max;
+
+ #endif /* __APPLE__, __FreeBSD__ */
}
unsigned int hz;
long ticks;
if ((ticks = sysconf(_SC_CLK_TCK)) == -1) {
- perror("sysconf");
+ error("Cannot get system clock ticks");
}
hz = (unsigned int) ticks;
}
-int read_single_number_file(const char *filename, unsigned long long *result) {
- char buffer[1024 + 1];
-
- int fd = open(filename, O_RDONLY, 0666);
- if(unlikely(fd == -1)) return 1;
-
- ssize_t r = read(fd, buffer, 1024);
- if(unlikely(r == -1)) {
- close(fd);
- return 2;
- }
-
- close(fd);
- *result = strtoull(buffer, NULL, 0);
- return 0;
+/*
+// poor man cycle counting
+static unsigned long tsc;
+void begin_tsc(void) {
+ unsigned long a, d;
+ asm volatile ("cpuid\nrdtsc" : "=a" (a), "=d" (d) : "0" (0) : "ebx", "ecx");
+ tsc = ((unsigned long)d << 32) | (unsigned long)a;
+}
+unsigned long end_tsc(void) {
+ unsigned long a, d;
+ asm volatile ("rdtscp" : "=a" (a), "=d" (d) : : "ecx");
+ return (((unsigned long)d << 32) | (unsigned long)a) - tsc;
}
+*/