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 #ifdef NETDATA_WITH_NANOSLEEP
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 error("nanosleep() failed for %llu microseconds.", usec);
40 if(likely(errno == EINTR)) {
41 req.tv_sec = rem.tv_sec;
42 req.tv_nsec = rem.tv_nsec;
45 error("Cannot nanosleep() for %llu microseconds.", usec);
52 int ret = usleep(usec);
53 if(unlikely(ret == -1 && errno == EINVAL)) {
54 // on certain systems, usec has to be up to 999999
56 int counter = usec / 999999;
60 usleep(usec % 999999);
63 error("Cannot usleep() for %llu microseconds.", usec);
69 error("usleep() failed for %llu microseconds.", usec);
75 unsigned char netdata_map_chart_names[256] = {
168 [92] = '/', // backslash
334 // make sure the supplied string
335 // is good for a netdata chart/dimension ID/NAME
336 void netdata_fix_chart_name(char *s) {
337 while((*s = netdata_map_chart_names[(unsigned char)*s])) s++;
340 unsigned char netdata_map_chart_ids[256] = {
433 [92] = '/', // backslash
599 // make sure the supplied string
600 // is good for a netdata chart/dimension ID/NAME
601 void netdata_fix_chart_id(char *s) {
602 while((*s = netdata_map_chart_ids[(unsigned char)*s])) s++;
606 // http://stackoverflow.com/questions/7666509/hash-function-for-string
607 uint32_t simple_hash(const char *name)
609 const char *s = name;
610 uint32_t hash = 5381;
613 while((i = *s++)) hash = ((hash << 5) + hash) + i;
615 // fprintf(stderr, "HASH: %lu %s\n", hash, name);
622 // http://isthe.com/chongo/tech/comp/fnv/#FNV-1a
623 uint32_t simple_hash(const char *name) {
624 unsigned char *s = (unsigned char *)name;
625 uint32_t hval = 0x811c9dc5;
629 // multiply by the 32 bit FNV magic prime mod 2^32
630 // NOTE: No need to optimize with left shifts.
631 // GCC will use imul instruction anyway.
632 // Tested with 'gcc -O3 -S'
633 //hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
636 // xor the bottom with the current octet
637 hval ^= (uint32_t)*s++;
640 // fprintf(stderr, "HASH: %u = %s\n", hval, name);
644 uint32_t simple_uhash(const char *name) {
645 unsigned char *s = (unsigned char *)name;
646 uint32_t hval = 0x811c9dc5, c;
650 if(unlikely(c >= 'A' && c <= 'Z')) c += 'a' - 'A';
658 // http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
659 // one at a time hash
660 uint32_t simple_hash(const char *name) {
661 unsigned char *s = (unsigned char *)name;
674 // fprintf(stderr, "HASH: %u = %s\n", h, name);
680 void strreverse(char* begin, char* end)
693 char *mystrsep(char **ptr, char *s)
696 while ( p && !p[0] && *ptr ) p = strsep(ptr, s);
702 // skip leading spaces
703 // and 'comments' as well!?
704 while(*s && isspace(*s)) s++;
705 if(!*s || *s == '#') return NULL;
707 // skip tailing spaces
708 // this way is way faster. Writes only one NUL char.
709 ssize_t l = strlen(s);
713 while (p > s && isspace(*p)) p--;
722 void *mymmap(const char *filename, size_t size, int flags, int ksm)
728 fd = open(filename, O_RDWR|O_CREAT|O_NOATIME, 0664);
730 if(lseek(fd, size, SEEK_SET) == (off_t)size) {
731 if(write(fd, "", 1) == 1) {
732 if(ftruncate(fd, size))
733 error("Cannot truncate file '%s' to size %zu. Will use the larger file.", filename, size);
735 #ifdef MADV_MERGEABLE
736 if(flags & MAP_SHARED || !enable_ksm || !ksm) {
738 mem = mmap(NULL, size, PROT_READ|PROT_WRITE, flags, fd, 0);
739 if(mem != MAP_FAILED) {
740 int advise = MADV_SEQUENTIAL|MADV_DONTFORK;
741 if(flags & MAP_SHARED) advise |= MADV_WILLNEED;
743 if(madvise(mem, size, advise) != 0)
744 error("Cannot advise the kernel about the memory usage of file '%s'.", filename);
746 #ifdef MADV_MERGEABLE
750 // test - load the file into memory
751 mem = calloc(1, size);
753 if(lseek(fd, 0, SEEK_SET) == 0) {
754 if(read(fd, mem, size) != (ssize_t)size)
755 error("Cannot read from file '%s'", filename);
758 error("Cannot seek to beginning of file '%s'.", filename);
761 mem = mmap(NULL, size, PROT_READ|PROT_WRITE, flags|MAP_ANONYMOUS, -1, 0);
762 if(mem != MAP_FAILED) {
763 if(lseek(fd, 0, SEEK_SET) == 0) {
764 if(read(fd, mem, size) != (ssize_t)size)
765 error("Cannot read from file '%s'", filename);
768 error("Cannot seek to beginning of file '%s'.", filename);
770 // don't use MADV_SEQUENTIAL|MADV_DONTFORK, they disable MADV_MERGEABLE
771 if(madvise(mem, size, MADV_SEQUENTIAL|MADV_DONTFORK) != 0)
772 error("Cannot advise the kernel about the memory usage (MADV_SEQUENTIAL|MADV_DONTFORK) of file '%s'.", filename);
774 if(madvise(mem, size, MADV_MERGEABLE) != 0)
775 error("Cannot advise the kernel about the memory usage (MADV_MERGEABLE) of file '%s'.", filename);
778 error("Cannot allocate PRIVATE ANONYMOUS memory for KSM for file '%s'.", filename);
782 else error("Cannot write to file '%s' at position %zu.", filename, size);
784 else error("Cannot seek file '%s' to size %zu.", filename, size);
788 else error("Cannot create/open file '%s'.", filename);
793 int savememory(const char *filename, void *mem, size_t size)
795 char tmpfilename[FILENAME_MAX + 1];
797 snprintfz(tmpfilename, FILENAME_MAX, "%s.%ld.tmp", filename, (long)getpid());
799 int fd = open(tmpfilename, O_RDWR|O_CREAT|O_NOATIME, 0664);
801 error("Cannot create/open file '%s'.", filename);
805 if(write(fd, mem, size) != (ssize_t)size) {
806 error("Cannot write to file '%s' %ld bytes.", filename, (long)size);
813 if(rename(tmpfilename, filename)) {
814 error("Cannot rename '%s' to '%s'", tmpfilename, filename);
821 int fd_is_valid(int fd) {
822 return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
826 ***************************************************************************
827 * Get number of clock ticks per second.
828 ***************************************************************************
836 if ((ticks = sysconf(_SC_CLK_TCK)) == -1) {
840 hz = (unsigned int) ticks;
845 return syscall(SYS_gettid);
848 char *fgets_trim_len(char *buf, size_t buf_size, FILE *fp, size_t *len) {
849 char *s = fgets(buf, buf_size, fp);
854 // find the string end
855 while (*++t != '\0');
857 // trim trailing spaces/newlines/tabs
858 while (--t > s && *t == '\n')
868 char *strncpyz(char *dst, const char *src, size_t n) {
879 int vsnprintfz(char *dst, size_t n, const char *fmt, va_list args) {
882 size = vsnprintf(dst, n, fmt, args);
884 if(unlikely((size_t)size > n)) {
885 // there is bug in vsnprintf() and it returns
886 // a number higher to len, but it does not
887 // overflow the buffer.
895 int snprintfz(char *dst, size_t n, const char *fmt, ...) {
899 int ret = vsnprintfz(dst, n, fmt, args);