X-Git-Url: https://arthur.barton.de/gitweb/?a=blobdiff_plain;f=src%2Fcommon.c;h=2df676f90216253f4e7584e242ba697d6e354d1f;hb=6372ed837298a34f6d16e0cde929822879863195;hp=5061080cf7db2471350259d3656260a1fe07ecf2;hpb=037bbc245f57e45f9e6f56c705957e38586d7218;p=netdata.git diff --git a/src/common.c b/src/common.c index 5061080c..2df676f9 100755 --- a/src/common.c +++ b/src/common.c @@ -1,6 +1,7 @@ -// enable O_NOATIME -#define _GNU_SOURCE - +#ifdef HAVE_CONFIG_H +#include +#endif +#include #include #include #include @@ -9,23 +10,71 @@ #include #include #include -#include #include "log.h" #include "common.h" -#include "web_client.h" -// TODO: this can be optimized to avoid strlen() -unsigned long simple_hash(const char *name) +char *global_host_prefix = ""; + +/* +// http://stackoverflow.com/questions/7666509/hash-function-for-string +uint32_t simple_hash(const char *name) { - int i, len = strlen(name); - unsigned long hash = 0; + const char *s = name; + uint32_t hash = 5381; + int i; + + while((i = *s++)) hash = ((hash << 5) + hash) + i; - for(i = 0; i < len ;i++) hash += (i * name[i]) + i + name[i]; + // fprintf(stderr, "HASH: %lu %s\n", hash, name); return hash; } +*/ + + +// http://isthe.com/chongo/tech/comp/fnv/#FNV-1a +uint32_t simple_hash(const char *name) { + unsigned char *s = (unsigned char *)name; + uint32_t hval = 0x811c9dc5; + + // FNV-1a algorithm + while (*s) { + // multiply by the 32 bit FNV magic prime mod 2^32 + // gcc optimized + hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24); + + // xor the bottom with the current octet + hval ^= (uint32_t)*s++; + } + + // fprintf(stderr, "HASH: %u = %s\n", hval, name); + return hval; +} + +/* +// http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx +// one at a time hash +uint32_t simple_hash(const char *name) { + unsigned char *s = (unsigned char *)name; + uint32_t h = 0; + + while(*s) { + h += *s++; + h += (h << 10); + h ^= (h >> 6); + } + + h += (h << 3); + h ^= (h >> 11); + h += (h << 15); + + // fprintf(stderr, "HASH: %u = %s\n", h, name); + + return h; +} +*/ void strreverse(char* begin, char* end) { @@ -41,44 +90,6 @@ char *mystrsep(char **ptr, char *s) return(p); } -// like strsep() but: -// it trims spaces before and after each value -// it accepts quoted values in single or double quotes -char *qstrsep(char **ptr) -{ - if(!*ptr || !**ptr) return NULL; - - char *s, *p = *ptr; - - // skip leading spaces - while(isspace(*p)) p++; - - // if the first char is a quote, assume quoted - if(*p == '"' || *p == '\'') { - char q = *p; - s = ++p; - while(*p && *p != q) p++; - - if(*p == q) { - *p = '\0'; - p++; - } - - *ptr = p; - return s; - } - - s = p; - while(*p && !isspace(*p)) p++; - if(!*p) *ptr = NULL; - else { - *p = '\0'; - *ptr = ++p; - } - - return s; -} - char *trim(char *s) { // skip leading spaces @@ -156,24 +167,30 @@ int savememory(const char *filename, void *mem, unsigned long size) return ret; } -void log_allocations(void) -{ - static int mem = 0; +int fd_is_valid(int fd) { + return fcntl(fd, F_GETFD) != -1 || errno != EBADF; +} - struct mallinfo mi; +/* + *************************************************************************** + * Get number of clock ticks per second. + *************************************************************************** + */ +unsigned int hz; - mi = mallinfo(); - if(mi.uordblks > mem) { - int clients = 0; - struct web_client *w; - for(w = web_clients; w ; w = w->next) clients++; +void get_HZ(void) +{ + long ticks; - info("Allocated memory increased from %d to %d (increased by %d bytes). There are %d web clients connected.", mem, mi.uordblks, mi.uordblks - mem, clients); - mem = mi.uordblks; + if ((ticks = sysconf(_SC_CLK_TCK)) == -1) { + perror("sysconf"); } + + hz = (unsigned int) ticks; } -int fd_is_valid(int fd) { - return fcntl(fd, F_GETFD) != -1 || errno != EBADF; +pid_t gettid(void) +{ + return syscall(SYS_gettid); }