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 int usecsleep(unsigned long long usec) {
32 #ifndef NETDATA_WITH_USLEEP
33 // we expect microseconds (1.000.000 per second)
34 // but timespec is nanoseconds (1.000.000.000 per second)
35 struct timespec req = { .tv_sec = usec / 1000000, .tv_nsec = (usec % 1000000) * 1000 }, rem;
37 while(nanosleep(&req, &rem) == -1) {
38 if(likely(errno == EINTR)) {
39 info("nanosleep() interrupted (while sleeping for %llu microseconds).", usec);
40 req.tv_sec = rem.tv_sec;
41 req.tv_nsec = rem.tv_nsec;
44 error("Cannot nanosleep() for %llu microseconds.", usec);
51 int ret = usleep(usec);
52 if(unlikely(ret == -1 && errno == EINVAL)) {
53 // on certain systems, usec has to be up to 999999
55 int counter = usec / 999999;
59 usleep(usec % 999999);
62 error("Cannot usleep() for %llu microseconds.", usec);
68 error("usleep() failed for %llu microseconds.", usec);
74 unsigned char netdata_map_chart_names[256] = {
167 [92] = '/', // backslash
333 // make sure the supplied string
334 // is good for a netdata chart/dimension ID/NAME
335 void netdata_fix_chart_name(char *s) {
336 while((*s = netdata_map_chart_names[(unsigned char)*s])) s++;
339 unsigned char netdata_map_chart_ids[256] = {
432 [92] = '/', // backslash
598 // make sure the supplied string
599 // is good for a netdata chart/dimension ID/NAME
600 void netdata_fix_chart_id(char *s) {
601 while((*s = netdata_map_chart_ids[(unsigned char)*s])) s++;
605 // http://stackoverflow.com/questions/7666509/hash-function-for-string
606 uint32_t simple_hash(const char *name)
608 const char *s = name;
609 uint32_t hash = 5381;
612 while((i = *s++)) hash = ((hash << 5) + hash) + i;
614 // fprintf(stderr, "HASH: %lu %s\n", hash, name);
621 // http://isthe.com/chongo/tech/comp/fnv/#FNV-1a
622 uint32_t simple_hash(const char *name) {
623 unsigned char *s = (unsigned char *)name;
624 uint32_t hval = 0x811c9dc5;
628 // multiply by the 32 bit FNV magic prime mod 2^32
629 // NOTE: No need to optimize with left shifts.
630 // GCC will use imul instruction anyway.
631 // Tested with 'gcc -O3 -S'
632 //hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
635 // xor the bottom with the current octet
636 hval ^= (uint32_t)*s++;
639 // fprintf(stderr, "HASH: %u = %s\n", hval, name);
643 uint32_t simple_uhash(const char *name) {
644 unsigned char *s = (unsigned char *)name;
645 uint32_t hval = 0x811c9dc5, c;
649 if(unlikely(c >= 'A' && c <= 'Z')) c += 'a' - 'A';
657 // http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
658 // one at a time hash
659 uint32_t simple_hash(const char *name) {
660 unsigned char *s = (unsigned char *)name;
673 // fprintf(stderr, "HASH: %u = %s\n", h, name);
679 void strreverse(char* begin, char* end)
692 char *mystrsep(char **ptr, char *s)
695 while ( p && !p[0] && *ptr ) p = strsep(ptr, s);
701 // skip leading spaces
702 // and 'comments' as well!?
703 while(*s && isspace(*s)) s++;
704 if(!*s || *s == '#') return NULL;
706 // skip tailing spaces
707 // this way is way faster. Writes only one NUL char.
708 ssize_t l = strlen(s);
712 while (p > s && isspace(*p)) p--;
721 void *mymmap(const char *filename, size_t size, int flags, int ksm)
727 fd = open(filename, O_RDWR|O_CREAT|O_NOATIME, 0664);
729 if(lseek(fd, size, SEEK_SET) == (off_t)size) {
730 if(write(fd, "", 1) == 1) {
731 if(ftruncate(fd, size))
732 error("Cannot truncate file '%s' to size %zu. Will use the larger file.", filename, size);
734 #ifdef MADV_MERGEABLE
735 if(flags & MAP_SHARED || !enable_ksm || !ksm) {
737 mem = mmap(NULL, size, PROT_READ|PROT_WRITE, flags, fd, 0);
738 if(mem != MAP_FAILED) {
739 int advise = MADV_SEQUENTIAL|MADV_DONTFORK;
740 if(flags & MAP_SHARED) advise |= MADV_WILLNEED;
742 if(madvise(mem, size, advise) != 0)
743 error("Cannot advise the kernel about the memory usage of file '%s'.", filename);
745 #ifdef MADV_MERGEABLE
749 // test - load the file into memory
750 mem = calloc(1, size);
752 if(lseek(fd, 0, SEEK_SET) == 0) {
753 if(read(fd, mem, size) != (ssize_t)size)
754 error("Cannot read from file '%s'", filename);
757 error("Cannot seek to beginning of file '%s'.", filename);
760 mem = mmap(NULL, size, PROT_READ|PROT_WRITE, flags|MAP_ANONYMOUS, -1, 0);
761 if(mem != MAP_FAILED) {
762 if(lseek(fd, 0, SEEK_SET) == 0) {
763 if(read(fd, mem, size) != (ssize_t)size)
764 error("Cannot read from file '%s'", filename);
767 error("Cannot seek to beginning of file '%s'.", filename);
769 // don't use MADV_SEQUENTIAL|MADV_DONTFORK, they disable MADV_MERGEABLE
770 if(madvise(mem, size, MADV_SEQUENTIAL|MADV_DONTFORK) != 0)
771 error("Cannot advise the kernel about the memory usage (MADV_SEQUENTIAL|MADV_DONTFORK) of file '%s'.", filename);
773 if(madvise(mem, size, MADV_MERGEABLE) != 0)
774 error("Cannot advise the kernel about the memory usage (MADV_MERGEABLE) of file '%s'.", filename);
777 error("Cannot allocate PRIVATE ANONYMOUS memory for KSM for file '%s'.", filename);
781 else error("Cannot write to file '%s' at position %zu.", filename, size);
783 else error("Cannot seek file '%s' to size %zu.", filename, size);
787 else error("Cannot create/open file '%s'.", filename);
792 int savememory(const char *filename, void *mem, size_t size)
794 char tmpfilename[FILENAME_MAX + 1];
796 snprintfz(tmpfilename, FILENAME_MAX, "%s.%ld.tmp", filename, (long)getpid());
798 int fd = open(tmpfilename, O_RDWR|O_CREAT|O_NOATIME, 0664);
800 error("Cannot create/open file '%s'.", filename);
804 if(write(fd, mem, size) != (ssize_t)size) {
805 error("Cannot write to file '%s' %ld bytes.", filename, (long)size);
812 if(rename(tmpfilename, filename)) {
813 error("Cannot rename '%s' to '%s'", tmpfilename, filename);
820 int fd_is_valid(int fd) {
821 return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
825 ***************************************************************************
826 * Get number of clock ticks per second.
827 ***************************************************************************
835 if ((ticks = sysconf(_SC_CLK_TCK)) == -1) {
839 hz = (unsigned int) ticks;
844 return syscall(SYS_gettid);
847 char *fgets_trim_len(char *buf, size_t buf_size, FILE *fp, size_t *len) {
848 char *s = fgets(buf, buf_size, fp);
853 // find the string end
854 while (*++t != '\0');
856 // trim trailing spaces/newlines/tabs
857 while (--t > s && *t == '\n')
867 char *strncpyz(char *dst, const char *src, size_t n) {
878 int vsnprintfz(char *dst, size_t n, const char *fmt, va_list args) {
881 size = vsnprintf(dst, n, fmt, args);
883 if(unlikely((size_t)size > n)) {
884 // there is bug in vsnprintf() and it returns
885 // a number higher to len, but it does not
886 // overflow the buffer.
894 int snprintfz(char *dst, size_t n, const char *fmt, ...) {
898 int ret = vsnprintfz(dst, n, fmt, args);