}
*/
-
+/*
// 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
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;
+ #else
+
char filename[FILENAME_MAX + 1];
snprintfz(filename, FILENAME_MAX, "%s/proc/stat", global_host_prefix);
debug(D_SYSTEM, "System has %d processors.", processors);
return processors;
+
+ #endif /* __APPLE__ */
}
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;
+ #else
+
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);
procfile_close(ff);
debug(D_SYSTEM, "System supports %d pids.", pid_max);
return pid_max;
+
+ #endif /* __APPLE__ */
}
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;
-}
-
-// ----------------------------------------------------------------------------
-// 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, *last = 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++;
- mode = NETDATA_SIMPLE_PATTERN_MODE_SUBSTRING;
- }
- else if(len >= 1 && *s == '*') {
- s++;
- mode = NETDATA_SIMPLE_PATTERN_MODE_SUFFIX;
- }
- else if(len >= 1 && s[len - 1] == '*') {
- s[len - 1] = '\0';
- mode = NETDATA_SIMPLE_PATTERN_MODE_PREFIX;
- }
- else
- mode = default_mode;
-
- // allocate the structure
- struct simple_pattern *m = callocz(1, sizeof(struct simple_pattern));
- if(*s) {
- m->match = strdup(s);
- m->len = strlen(m->match);
- m->mode = mode;
- }
- else {
- m->mode = NETDATA_SIMPLE_PATTERN_MODE_SUBSTRING;
- }
-
- // link it at the end
- if(unlikely(!root))
- root = last = m;
- else {
- last->next = m;
- last = m;
- }
-
- // prepare for next loop
- s = n;
- }
- }
-
- free(a);
- return (NETDATA_SIMPLE_PATTERN *)root;
-}
-
-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(!m->len || 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;
-}