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);
600 // http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
601 // one at a time hash
602 uint32_t simple_hash(const char *name) {
603 unsigned char *s = (unsigned char *)name;
616 // fprintf(stderr, "HASH: %u = %s\n", h, name);
622 void strreverse(char* begin, char* end)
635 char *mystrsep(char **ptr, char *s)
638 while ( p && !p[0] && *ptr ) p = strsep(ptr, s);
644 // skip leading spaces
645 // and 'comments' as well!?
646 while(*s && isspace(*s)) s++;
647 if(!*s || *s == '#') return NULL;
649 // skip tailing spaces
650 // this way is way faster. Writes only one NUL char.
651 ssize_t l = strlen(s);
655 while (p > s && isspace(*p)) p--;
664 void *mymmap(const char *filename, size_t size, int flags, int ksm)
670 fd = open(filename, O_RDWR|O_CREAT|O_NOATIME, 0664);
672 if(lseek(fd, size, SEEK_SET) == (long)size) {
673 if(write(fd, "", 1) == 1) {
674 if(ftruncate(fd, size))
675 error("Cannot truncate file '%s' to size %ld. Will use the larger file.", filename, size);
677 #ifdef MADV_MERGEABLE
678 if(flags & MAP_SHARED || !enable_ksm || !ksm) {
680 mem = mmap(NULL, size, PROT_READ|PROT_WRITE, flags, fd, 0);
682 int advise = MADV_SEQUENTIAL|MADV_DONTFORK;
683 if(flags & MAP_SHARED) advise |= MADV_WILLNEED;
685 if(madvise(mem, size, advise) != 0)
686 error("Cannot advise the kernel about the memory usage of file '%s'.", filename);
688 #ifdef MADV_MERGEABLE
691 mem = mmap(NULL, size, PROT_READ|PROT_WRITE, flags|MAP_ANONYMOUS, -1, 0);
693 if(lseek(fd, 0, SEEK_SET) == 0) {
694 if(read(fd, mem, size) != (ssize_t)size)
695 error("Cannot read from file '%s'", filename);
698 error("Cannot seek to beginning of file '%s'.", filename);
700 // don't use MADV_SEQUENTIAL|MADV_DONTFORK, they disable MADV_MERGEABLE
701 if(madvise(mem, size, MADV_SEQUENTIAL|MADV_DONTFORK) != 0)
702 error("Cannot advise the kernel about the memory usage (MADV_SEQUENTIAL|MADV_DONTFORK) of file '%s'.", filename);
704 if(madvise(mem, size, MADV_MERGEABLE) != 0)
705 error("Cannot advise the kernel about the memory usage (MADV_MERGEABLE) of file '%s'.", filename);
708 error("Cannot allocate PRIVATE ANONYMOUS memory for KSM for file '%s'.", filename);
712 else error("Cannot write to file '%s' at position %ld.", filename, size);
714 else error("Cannot seek file '%s' to size %ld.", filename, size);
718 else error("Cannot create/open file '%s'.", filename);
723 int savememory(const char *filename, void *mem, unsigned long size)
725 char tmpfilename[FILENAME_MAX + 1];
727 snprintfz(tmpfilename, FILENAME_MAX, "%s.%ld.tmp", filename, (long)getpid());
729 int fd = open(tmpfilename, O_RDWR|O_CREAT|O_NOATIME, 0664);
731 error("Cannot create/open file '%s'.", filename);
735 if(write(fd, mem, size) != (long)size) {
736 error("Cannot write to file '%s' %ld bytes.", filename, (long)size);
744 if(rename(tmpfilename, filename)) {
745 error("Cannot rename '%s' to '%s'", tmpfilename, filename);
752 int fd_is_valid(int fd) {
753 return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
757 ***************************************************************************
758 * Get number of clock ticks per second.
759 ***************************************************************************
767 if ((ticks = sysconf(_SC_CLK_TCK)) == -1) {
771 hz = (unsigned int) ticks;
776 return syscall(SYS_gettid);
779 char *fgets_trim_len(char *buf, size_t buf_size, FILE *fp, size_t *len) {
780 char *s = fgets(buf, buf_size, fp);
785 // find the string end
786 while (*++t != '\0');
788 // trim trailing spaces/newlines/tabs
789 while (--t > s && *t == '\n')
799 char *strncpyz(char *dest, const char *src, size_t n)
810 int vsnprintfz(char *sout, size_t n, const char *fmt, va_list args)
814 size = vsnprintf(sout, n, fmt, args);
819 int snprintfz(char *sout, size_t n, const char *fmt, ...)
824 return vsnprintfz(sout, n, fmt, args);