]> arthur.barton.de Git - netdata.git/blobdiff - src/common.c
Minor style cleanups
[netdata.git] / src / common.c
index 8abd2d447ba44213981a029a50882936a5c620de..72412baae286abb540b85fa44bf7543151a4b39b 100644 (file)
@@ -8,10 +8,21 @@
 #    define MADV_DONTFORK INHERIT_NONE
 #endif /* __FreeBSD__ || __APPLE__*/
 
-char *global_host_prefix = "";
+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;
+
 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
@@ -807,7 +818,7 @@ uint32_t simple_hash(const char *name)
 }
 */
 
-
+/*
 // http://isthe.com/chongo/tech/comp/fnv/#FNV-1a
 uint32_t simple_hash(const char *name) {
     unsigned char *s = (unsigned char *) name;
@@ -842,6 +853,7 @@ uint32_t simple_uhash(const char *name) {
     }
     return hval;
 }
+*/
 
 /*
 // http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
@@ -1059,17 +1071,6 @@ 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);
 
@@ -1100,8 +1101,30 @@ 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", 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) {
@@ -1129,35 +1152,52 @@ long get_system_cpus(void) {
 
     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_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;
     }
 
-    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;
@@ -1165,150 +1205,23 @@ void get_system_HZ(void) {
     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;
-}
-
-// ----------------------------------------------------------------------------
-// simple_pattern_match
-
-struct simple_pattern {
-    const char *match;
-    size_t len;
-    NETDATA_SIMPLE_PREFIX_MODE mode;
-    struct simple_pattern *next;
-};
-
-NETDATA_SIMPLE_PATTERN *netdata_simple_pattern_list_create(const char *list, NETDATA_SIMPLE_PREFIX_MODE default_mode) {
-    struct simple_pattern *root = NULL;
-
-    if(unlikely(!list || !*list)) return root;
-
-    char *a = strdupz(list);
-    if(a && *a) {
-        char *s = a;
-
-        while(s && *s) {
-            // skip all spaces
-            while(isspace(*s)) s++;
-
-            // empty string
-            if(unlikely(!*s)) break;
-
-            // find the next space
-            char *c = s;
-            while(*c && !isspace(*c)) c++;
-
-            // find the next word
-            char *n;
-            if(likely(*c)) n = c + 1;
-            else n = NULL;
-
-            // terminate our string
-            *c = '\0';
-
-            char buf[100 + 1];
-            strncpy(buf, s, 100);
-            buf[100] = '\0';
-            if(likely(n)) *c = ' ';
-            s = buf;
-
-            NETDATA_SIMPLE_PREFIX_MODE mode;
-            size_t len = strlen(s);
-            if(len >= 2 && *s == '*' && s[len - 1] == '*') {
-                s[len - 1] = '\0';
-                s++;
-                len -= 2;
-                mode = NETDATA_SIMPLE_PATTERN_MODE_SUBSTRING;
-            }
-            else if(len >= 1 && *s == '*') {
-                s++;
-                len--;
-                mode = NETDATA_SIMPLE_PATTERN_MODE_SUFFIX;
-            }
-            else if(len >= 1 && s[len - 1] == '*') {
-                s[len - 1] = '\0';
-                len--;
-                mode = NETDATA_SIMPLE_PATTERN_MODE_PREFIX;
-            }
-            else
-                mode = default_mode;
-
-            if(len) {
-                if(*s == '*')
-                    error("simple pattern '%s' includes '%s' that is invalid", a, s);
-
-                // allocate the structure
-                struct simple_pattern *m = mallocz(sizeof(struct simple_pattern));
-                m->match = strdup(s);
-                m->len = strlen(m->match);
-                m->mode = mode;
-                m->next = root;
-                root = m;
-            }
-            else
-                error("simple pattern '%s' includes invalid matches", a);
-
-            // prepare for next loop
-            s = n;
-        }
-    }
-
-    free(a);
-    return (NETDATA_SIMPLE_PATTERN *)root;
+/*
+// 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;
 }
-
-int netdata_simple_pattern_list_matches(NETDATA_SIMPLE_PATTERN *list, const char *str) {
-    struct simple_pattern *m, *root = (struct simple_pattern *)list;
-
-    if(unlikely(!root)) return 0;
-
-    size_t len = strlen(str);
-    for(m = root; m ; m = m->next) {
-        if(m->len <= len) {
-            switch(m->mode) {
-                case NETDATA_SIMPLE_PATTERN_MODE_SUBSTRING:
-                    if(unlikely(strstr(str, m->match)))
-                        return 1;
-                    break;
-
-                case NETDATA_SIMPLE_PATTERN_MODE_PREFIX:
-                    if(unlikely(strncmp(str, m->match, m->len) == 0))
-                        return 1;
-                    break;
-
-                case NETDATA_SIMPLE_PATTERN_MODE_SUFFIX:
-                    if(unlikely(strcmp(&str[len - m->len], m->match) == 0))
-                        return 1;
-                    break;
-
-                case NETDATA_SIMPLE_PATTERN_MODE_EXACT:
-                default:
-                    if(unlikely(strcmp(str, m->match) == 0))
-                        return 1;
-                    break;
-            }
-        }
-    }
-
-    return 0;
+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;
 }
+*/