4 #include <sys/syscall.h>
17 #include "appconfig.h"
18 #include "../config.h"
20 char *global_host_prefix = "";
23 // time(NULL) in milliseconds
24 unsigned long long timems(void) {
26 gettimeofday(&now, NULL);
27 return now.tv_sec * 1000000ULL + now.tv_usec;
30 unsigned char netdata_map_chart_names[256] = {
123 [92] = '/', // backslash
289 // make sure the supplied string
290 // is good for a netdata chart/dimension ID/NAME
291 void netdata_fix_chart_name(char *s) {
292 while((*s = netdata_map_chart_names[(unsigned char)*s])) s++;
295 unsigned char netdata_map_chart_ids[256] = {
388 [92] = '/', // backslash
554 // make sure the supplied string
555 // is good for a netdata chart/dimension ID/NAME
556 void netdata_fix_chart_id(char *s) {
557 while((*s = netdata_map_chart_ids[(unsigned char)*s])) s++;
561 // http://stackoverflow.com/questions/7666509/hash-function-for-string
562 uint32_t simple_hash(const char *name)
564 const char *s = name;
565 uint32_t hash = 5381;
568 while((i = *s++)) hash = ((hash << 5) + hash) + i;
570 // fprintf(stderr, "HASH: %lu %s\n", hash, name);
577 // http://isthe.com/chongo/tech/comp/fnv/#FNV-1a
578 uint32_t simple_hash(const char *name) {
579 unsigned char *s = (unsigned char *)name;
580 uint32_t hval = 0x811c9dc5;
584 // multiply by the 32 bit FNV magic prime mod 2^32
585 // NOTE: No need to optimize with left shifts.
586 // GCC will use imul instruction anyway.
587 // Tested with 'gcc -O3 -S'
588 //hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
591 // xor the bottom with the current octet
592 hval ^= (uint32_t)*s++;
595 // fprintf(stderr, "HASH: %u = %s\n", hval, name);
599 uint32_t simple_uhash(const char *name) {
600 unsigned char *s = (unsigned char *)name;
601 uint32_t hval = 0x811c9dc5, c;
605 if(unlikely(c >= 'A' && c <= 'Z')) c += 'a' - 'A';
613 // http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
614 // one at a time hash
615 uint32_t simple_hash(const char *name) {
616 unsigned char *s = (unsigned char *)name;
629 // fprintf(stderr, "HASH: %u = %s\n", h, name);
635 void strreverse(char* begin, char* end)
648 char *mystrsep(char **ptr, char *s)
651 while ( p && !p[0] && *ptr ) p = strsep(ptr, s);
657 // skip leading spaces
658 // and 'comments' as well!?
659 while(*s && isspace(*s)) s++;
660 if(!*s || *s == '#') return NULL;
662 // skip tailing spaces
663 // this way is way faster. Writes only one NUL char.
664 ssize_t l = strlen(s);
668 while (p > s && isspace(*p)) p--;
677 void *mymmap(const char *filename, size_t size, int flags, int ksm)
683 fd = open(filename, O_RDWR|O_CREAT|O_NOATIME, 0664);
685 if(lseek(fd, size, SEEK_SET) == (off_t)size) {
686 if(write(fd, "", 1) == 1) {
687 if(ftruncate(fd, size))
688 error("Cannot truncate file '%s' to size %ld. Will use the larger file.", filename, size);
690 #ifdef MADV_MERGEABLE
691 if(flags & MAP_SHARED || !enable_ksm || !ksm) {
693 mem = mmap(NULL, size, PROT_READ|PROT_WRITE, flags, fd, 0);
694 if(mem != MAP_FAILED) {
695 int advise = MADV_SEQUENTIAL|MADV_DONTFORK;
696 if(flags & MAP_SHARED) advise |= MADV_WILLNEED;
698 if(madvise(mem, size, advise) != 0)
699 error("Cannot advise the kernel about the memory usage of file '%s'.", filename);
701 #ifdef MADV_MERGEABLE
705 // test - load the file into memory
706 mem = calloc(1, size);
708 if(lseek(fd, 0, SEEK_SET) == 0) {
709 if(read(fd, mem, size) != (ssize_t)size)
710 error("Cannot read from file '%s'", filename);
713 error("Cannot seek to beginning of file '%s'.", filename);
716 mem = mmap(NULL, size, PROT_READ|PROT_WRITE, flags|MAP_ANONYMOUS, -1, 0);
717 if(mem != MAP_FAILED) {
718 if(lseek(fd, 0, SEEK_SET) == 0) {
719 if(read(fd, mem, size) != (ssize_t)size)
720 error("Cannot read from file '%s'", filename);
723 error("Cannot seek to beginning of file '%s'.", filename);
725 // don't use MADV_SEQUENTIAL|MADV_DONTFORK, they disable MADV_MERGEABLE
726 if(madvise(mem, size, MADV_SEQUENTIAL|MADV_DONTFORK) != 0)
727 error("Cannot advise the kernel about the memory usage (MADV_SEQUENTIAL|MADV_DONTFORK) of file '%s'.", filename);
729 if(madvise(mem, size, MADV_MERGEABLE) != 0)
730 error("Cannot advise the kernel about the memory usage (MADV_MERGEABLE) of file '%s'.", filename);
733 error("Cannot allocate PRIVATE ANONYMOUS memory for KSM for file '%s'.", filename);
737 else error("Cannot write to file '%s' at position %ld.", filename, size);
739 else error("Cannot seek file '%s' to size %ld.", filename, size);
743 else error("Cannot create/open file '%s'.", filename);
748 int savememory(const char *filename, void *mem, size_t size)
750 char tmpfilename[FILENAME_MAX + 1];
752 snprintfz(tmpfilename, FILENAME_MAX, "%s.%ld.tmp", filename, (long)getpid());
754 int fd = open(tmpfilename, O_RDWR|O_CREAT|O_NOATIME, 0664);
756 error("Cannot create/open file '%s'.", filename);
760 if(write(fd, mem, size) != (ssize_t)size) {
761 error("Cannot write to file '%s' %ld bytes.", filename, (long)size);
768 if(rename(tmpfilename, filename)) {
769 error("Cannot rename '%s' to '%s'", tmpfilename, filename);
776 int fd_is_valid(int fd) {
777 return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
781 ***************************************************************************
782 * Get number of clock ticks per second.
783 ***************************************************************************
791 if ((ticks = sysconf(_SC_CLK_TCK)) == -1) {
795 hz = (unsigned int) ticks;
800 return syscall(SYS_gettid);
803 char *fgets_trim_len(char *buf, size_t buf_size, FILE *fp, size_t *len) {
804 char *s = fgets(buf, buf_size, fp);
809 // find the string end
810 while (*++t != '\0');
812 // trim trailing spaces/newlines/tabs
813 while (--t > s && *t == '\n')
823 char *strncpyz(char *dst, const char *src, size_t n) {
834 int vsnprintfz(char *dst, size_t n, const char *fmt, va_list args) {
837 size = vsnprintf(dst, n, fmt, args);
839 if(unlikely((size_t)size > n)) {
840 // there is bug in vsnprintf() and it returns
841 // a number higher to len, but it does not
842 // overflow the buffer.
850 int snprintfz(char *dst, size_t n, const char *fmt, ...) {
854 int ret = vsnprintfz(dst, n, fmt, args);