diff options
Diffstat (limited to 'libnetdata/libnetdata.c')
-rw-r--r-- | libnetdata/libnetdata.c | 204 |
1 files changed, 68 insertions, 136 deletions
diff --git a/libnetdata/libnetdata.c b/libnetdata/libnetdata.c index 19b861e3..272ba8f1 100644 --- a/libnetdata/libnetdata.c +++ b/libnetdata/libnetdata.c @@ -249,7 +249,7 @@ static avl_tree_lock malloc_trace_index = { .avl_tree = { .root = NULL, .compar = malloc_trace_compare}, - .rwlock = NETDATA_RWLOCK_INITIALIZER + .rwlock = AVL_LOCK_INITIALIZER }; int malloc_trace_walkthrough(int (*callback)(void *item, void *data), void *data) { @@ -370,7 +370,7 @@ static struct malloc_header *malloc_get_header(void *ptr, const char *caller, co struct malloc_header *t = (struct malloc_header *)ret; if(t->signature.magic != 0x0BADCAFE) { - error("pointer %p is not our pointer (called %s() from %zu@%s, %s()).", ptr, caller, line, file, function); + netdata_log_error("pointer %p is not our pointer (called %s() from %zu@%s, %s()).", ptr, caller, line, file, function); return NULL; } @@ -1043,20 +1043,23 @@ void netdata_fix_chart_id(char *s) { } static int memory_file_open(const char *filename, size_t size) { - // info("memory_file_open('%s', %zu", filename, size); + // netdata_log_info("memory_file_open('%s', %zu", filename, size); int fd = open(filename, O_RDWR | O_CREAT | O_NOATIME, 0664); if (fd != -1) { if (lseek(fd, size, SEEK_SET) == (off_t) size) { if (write(fd, "", 1) == 1) { if (ftruncate(fd, size)) - error("Cannot truncate file '%s' to size %zu. Will use the larger file.", filename, size); + netdata_log_error("Cannot truncate file '%s' to size %zu. Will use the larger file.", filename, size); } - else error("Cannot write to file '%s' at position %zu.", filename, size); + else + netdata_log_error("Cannot write to file '%s' at position %zu.", filename, size); } - else error("Cannot seek file '%s' to size %zu.", filename, size); + else + netdata_log_error("Cannot seek file '%s' to size %zu.", filename, size); } - else error("Cannot create/open file '%s'.", filename); + else + netdata_log_error("Cannot create/open file '%s'.", filename); return fd; } @@ -1065,7 +1068,8 @@ inline int madvise_sequential(void *mem, size_t len) { static int logger = 1; int ret = madvise(mem, len, MADV_SEQUENTIAL); - if (ret != 0 && logger-- > 0) error("madvise(MADV_SEQUENTIAL) failed."); + if (ret != 0 && logger-- > 0) + netdata_log_error("madvise(MADV_SEQUENTIAL) failed."); return ret; } @@ -1073,7 +1077,8 @@ inline int madvise_random(void *mem, size_t len) { static int logger = 1; int ret = madvise(mem, len, MADV_RANDOM); - if (ret != 0 && logger-- > 0) error("madvise(MADV_RANDOM) failed."); + if (ret != 0 && logger-- > 0) + netdata_log_error("madvise(MADV_RANDOM) failed."); return ret; } @@ -1081,7 +1086,8 @@ inline int madvise_dontfork(void *mem, size_t len) { static int logger = 1; int ret = madvise(mem, len, MADV_DONTFORK); - if (ret != 0 && logger-- > 0) error("madvise(MADV_DONTFORK) failed."); + if (ret != 0 && logger-- > 0) + netdata_log_error("madvise(MADV_DONTFORK) failed."); return ret; } @@ -1089,7 +1095,8 @@ inline int madvise_willneed(void *mem, size_t len) { static int logger = 1; int ret = madvise(mem, len, MADV_WILLNEED); - if (ret != 0 && logger-- > 0) error("madvise(MADV_WILLNEED) failed."); + if (ret != 0 && logger-- > 0) + netdata_log_error("madvise(MADV_WILLNEED) failed."); return ret; } @@ -1097,7 +1104,8 @@ inline int madvise_dontneed(void *mem, size_t len) { static int logger = 1; int ret = madvise(mem, len, MADV_DONTNEED); - if (ret != 0 && logger-- > 0) error("madvise(MADV_DONTNEED) failed."); + if (ret != 0 && logger-- > 0) + netdata_log_error("madvise(MADV_DONTNEED) failed."); return ret; } @@ -1106,7 +1114,8 @@ inline int madvise_dontdump(void *mem __maybe_unused, size_t len __maybe_unused) static int logger = 1; int ret = madvise(mem, len, MADV_DONTDUMP); - if (ret != 0 && logger-- > 0) error("madvise(MADV_DONTDUMP) failed."); + if (ret != 0 && logger-- > 0) + netdata_log_error("madvise(MADV_DONTDUMP) failed."); return ret; #else return 0; @@ -1118,7 +1127,8 @@ inline int madvise_mergeable(void *mem __maybe_unused, size_t len __maybe_unused static int logger = 1; int ret = madvise(mem, len, MADV_MERGEABLE); - if (ret != 0 && logger-- > 0) error("madvise(MADV_MERGEABLE) failed."); + if (ret != 0 && logger-- > 0) + netdata_log_error("madvise(MADV_MERGEABLE) failed."); return ret; #else return 0; @@ -1127,7 +1137,7 @@ inline int madvise_mergeable(void *mem __maybe_unused, size_t len __maybe_unused void *netdata_mmap(const char *filename, size_t size, int flags, int ksm, bool read_only, int *open_fd) { - // info("netdata_mmap('%s', %zu", filename, size); + // netdata_log_info("netdata_mmap('%s', %zu", filename, size); // MAP_SHARED is used in memory mode map // MAP_PRIVATE is used in memory mode ram and save @@ -1177,9 +1187,9 @@ void *netdata_mmap(const char *filename, size_t size, int flags, int ksm, bool r if(fd != -1 && fd_for_mmap == -1) { if (lseek(fd, 0, SEEK_SET) == 0) { if (read(fd, mem, size) != (ssize_t) size) - info("Cannot read from file '%s'", filename); + netdata_log_info("Cannot read from file '%s'", filename); } - else info("Cannot seek to beginning of file '%s'.", filename); + else netdata_log_info("Cannot seek to beginning of file '%s'.", filename); } // madvise_sequential(mem, size); @@ -1215,12 +1225,12 @@ int memory_file_save(const char *filename, void *mem, size_t size) { int fd = open(tmpfilename, O_RDWR | O_CREAT | O_NOATIME, 0664); if (fd < 0) { - error("Cannot create/open file '%s'.", filename); + netdata_log_error("Cannot create/open file '%s'.", filename); return -1; } if (write(fd, mem, size) != (ssize_t) size) { - error("Cannot write to file '%s' %ld bytes.", filename, (long) size); + netdata_log_error("Cannot write to file '%s' %ld bytes.", filename, (long) size); close(fd); return -1; } @@ -1228,7 +1238,7 @@ int memory_file_save(const char *filename, void *mem, size_t size) { close(fd); if (rename(tmpfilename, filename)) { - error("Cannot rename '%s' to '%s'", tmpfilename, filename); + netdata_log_error("Cannot rename '%s' to '%s'", tmpfilename, filename); return -1; } @@ -1298,7 +1308,7 @@ unsigned long end_tsc(void) { int recursively_delete_dir(const char *path, const char *reason) { DIR *dir = opendir(path); if(!dir) { - error("Cannot read %s directory to be deleted '%s'", reason?reason:"", path); + netdata_log_error("Cannot read %s directory to be deleted '%s'", reason?reason:"", path); return -1; } @@ -1321,16 +1331,16 @@ int recursively_delete_dir(const char *path, const char *reason) { continue; } - info("Deleting %s file '%s'", reason?reason:"", fullpath); + netdata_log_info("Deleting %s file '%s'", reason?reason:"", fullpath); if(unlikely(unlink(fullpath) == -1)) - error("Cannot delete %s file '%s'", reason?reason:"", fullpath); + netdata_log_error("Cannot delete %s file '%s'", reason?reason:"", fullpath); else ret++; } - info("Deleting empty directory '%s'", path); + netdata_log_info("Deleting empty directory '%s'", path); if(unlikely(rmdir(path) == -1)) - error("Cannot delete empty directory '%s'", path); + netdata_log_error("Cannot delete empty directory '%s'", path); else ret++; @@ -1399,12 +1409,12 @@ int verify_netdata_host_prefix() { goto failed; if(netdata_configured_host_prefix && *netdata_configured_host_prefix) - info("Using host prefix directory '%s'", netdata_configured_host_prefix); + netdata_log_info("Using host prefix directory '%s'", netdata_configured_host_prefix); return 0; failed: - error("Ignoring host prefix '%s': path '%s' %s", netdata_configured_host_prefix, path, reason); + netdata_log_error("Ignoring host prefix '%s': path '%s' %s", netdata_configured_host_prefix, path, reason); netdata_configured_host_prefix = ""; return -1; } @@ -1512,18 +1522,18 @@ int path_is_file(const char *path, const char *subpath) { void recursive_config_double_dir_load(const char *user_path, const char *stock_path, const char *subpath, int (*callback)(const char *filename, void *data), void *data, size_t depth) { if(depth > 3) { - error("CONFIG: Max directory depth reached while reading user path '%s', stock path '%s', subpath '%s'", user_path, stock_path, subpath); + netdata_log_error("CONFIG: Max directory depth reached while reading user path '%s', stock path '%s', subpath '%s'", user_path, stock_path, subpath); return; } char *udir = strdupz_path_subpath(user_path, subpath); char *sdir = strdupz_path_subpath(stock_path, subpath); - debug(D_HEALTH, "CONFIG traversing user-config directory '%s', stock config directory '%s'", udir, sdir); + netdata_log_debug(D_HEALTH, "CONFIG traversing user-config directory '%s', stock config directory '%s'", udir, sdir); DIR *dir = opendir(udir); if (!dir) { - error("CONFIG cannot open user-config directory '%s'.", udir); + netdata_log_error("CONFIG cannot open user-config directory '%s'.", udir); } else { struct dirent *de = NULL; @@ -1533,7 +1543,7 @@ void recursive_config_double_dir_load(const char *user_path, const char *stock_p (de->d_name[0] == '.' && de->d_name[1] == '\0') || (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0') ) { - debug(D_HEALTH, "CONFIG ignoring user-config directory '%s/%s'", udir, de->d_name); + netdata_log_debug(D_HEALTH, "CONFIG ignoring user-config directory '%s/%s'", udir, de->d_name); continue; } @@ -1548,24 +1558,24 @@ void recursive_config_double_dir_load(const char *user_path, const char *stock_p if(path_is_file(udir, de->d_name) && len > 5 && !strcmp(&de->d_name[len - 5], ".conf")) { char *filename = strdupz_path_subpath(udir, de->d_name); - debug(D_HEALTH, "CONFIG calling callback for user file '%s'", filename); + netdata_log_debug(D_HEALTH, "CONFIG calling callback for user file '%s'", filename); callback(filename, data); freez(filename); continue; } } - debug(D_HEALTH, "CONFIG ignoring user-config file '%s/%s' of type %d", udir, de->d_name, (int)de->d_type); + netdata_log_debug(D_HEALTH, "CONFIG ignoring user-config file '%s/%s' of type %d", udir, de->d_name, (int)de->d_type); } closedir(dir); } - debug(D_HEALTH, "CONFIG traversing stock config directory '%s', user config directory '%s'", sdir, udir); + netdata_log_debug(D_HEALTH, "CONFIG traversing stock config directory '%s', user config directory '%s'", sdir, udir); dir = opendir(sdir); if (!dir) { - error("CONFIG cannot open stock config directory '%s'.", sdir); + netdata_log_error("CONFIG cannot open stock config directory '%s'.", sdir); } else { if (strcmp(udir, sdir)) { @@ -1576,7 +1586,7 @@ void recursive_config_double_dir_load(const char *user_path, const char *stock_p (de->d_name[0] == '.' && de->d_name[1] == '\0') || (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0') ) { - debug(D_HEALTH, "CONFIG ignoring stock config directory '%s/%s'", sdir, de->d_name); + netdata_log_debug(D_HEALTH, "CONFIG ignoring stock config directory '%s/%s'", sdir, de->d_name); continue; } @@ -1596,7 +1606,7 @@ void recursive_config_double_dir_load(const char *user_path, const char *stock_p if(path_is_file(sdir, de->d_name) && !path_is_file(udir, de->d_name) && len > 5 && !strcmp(&de->d_name[len - 5], ".conf")) { char *filename = strdupz_path_subpath(sdir, de->d_name); - debug(D_HEALTH, "CONFIG calling callback for stock file '%s'", filename); + netdata_log_debug(D_HEALTH, "CONFIG calling callback for stock file '%s'", filename); callback(filename, data); freez(filename); continue; @@ -1604,13 +1614,13 @@ void recursive_config_double_dir_load(const char *user_path, const char *stock_p } - debug(D_HEALTH, "CONFIG ignoring stock-config file '%s/%s' of type %d", udir, de->d_name, (int)de->d_type); + netdata_log_debug(D_HEALTH, "CONFIG ignoring stock-config file '%s/%s' of type %d", udir, de->d_name, (int)de->d_type); } } closedir(dir); } - debug(D_HEALTH, "CONFIG done traversing user-config directory '%s', stock config directory '%s'", udir, sdir); + netdata_log_debug(D_HEALTH, "CONFIG done traversing user-config directory '%s', stock config directory '%s'", udir, sdir); freez(udir); freez(sdir); @@ -1684,7 +1694,7 @@ char *find_and_replace(const char *src, const char *find, const char *replace, c return value; } -inline int pluginsd_space(char c) { +inline int pluginsd_isspace(char c) { switch(c) { case ' ': case '\t': @@ -1698,8 +1708,7 @@ inline int pluginsd_space(char c) { } } -inline int config_isspace(char c) -{ +inline int config_isspace(char c) { switch (c) { case ' ': case '\t': @@ -1713,100 +1722,23 @@ inline int config_isspace(char c) } } -// split a text into words, respecting quotes -inline size_t quoted_strings_splitter(char *str, char **words, size_t max_words, int (*custom_isspace)(char)) -{ - char *s = str, quote = 0; - size_t i = 0; - - // skip all white space - while (unlikely(custom_isspace(*s))) - s++; - - if(unlikely(!*s)) { - words[i] = NULL; - return 0; - } - - // check for quote - if (unlikely(*s == '\'' || *s == '"')) { - quote = *s; // remember the quote - s++; // skip the quote - } - - // store the first word - words[i++] = s; - - // while we have something - while (likely(*s)) { - // if it is an escape - if (unlikely(*s == '\\' && s[1])) { - s += 2; - continue; - } - - // if it is a quote - else if (unlikely(*s == quote)) { - quote = 0; - *s = ' '; - continue; - } - - // if it is a space - else if (unlikely(quote == 0 && custom_isspace(*s))) { - // terminate the word - *s++ = '\0'; - - // skip all white space - while (likely(custom_isspace(*s))) - s++; - - // check for a quote - if (unlikely(*s == '\'' || *s == '"')) { - quote = *s; // remember the quote - s++; // skip the quote - } - - // if we reached the end, stop - if (unlikely(!*s)) - break; - - // store the next word - if (likely(i < max_words)) - words[i++] = s; - else - break; - } - - // anything else - else - s++; - } +inline int group_by_label_isspace(char c) { + if(c == ',' || c == '|') + return 1; - if (i < max_words) - words[i] = NULL; - - return i; -} - -inline size_t pluginsd_split_words(char *str, char **words, size_t max_words) -{ - return quoted_strings_splitter(str, words, max_words, pluginsd_space); + return 0; } -bool bitmap256_get_bit(BITMAP256 *ptr, uint8_t idx) { - if (unlikely(!ptr)) - return false; - return (ptr->data[idx / 64] & (1ULL << (idx % 64))); -} +bool isspace_map_pluginsd[256] = {}; +bool isspace_map_config[256] = {}; +bool isspace_map_group_by_label[256] = {}; -void bitmap256_set_bit(BITMAP256 *ptr, uint8_t idx, bool value) { - if (unlikely(!ptr)) - return; - if (likely(value)) - ptr->data[idx / 64] |= (1ULL << (idx % 64)); - else - ptr->data[idx / 64] &= ~(1ULL << (idx % 64)); +__attribute__((constructor)) void initialize_is_space_arrays(void) { + for(int c = 0; c < 256 ; c++) { + isspace_map_pluginsd[c] = pluginsd_isspace((char) c); + isspace_map_config[c] = config_isspace((char) c); + isspace_map_group_by_label[c] = group_by_label_isspace((char) c); + } } bool run_command_and_copy_output_to_stdout(const char *command, int max_line_length) { @@ -1819,7 +1751,7 @@ bool run_command_and_copy_output_to_stdout(const char *command, int max_line_len fprintf(stdout, "%s", buffer); } else { - error("Failed to execute command '%s'.", command); + netdata_log_error("Failed to execute command '%s'.", command); return false; } @@ -1837,7 +1769,7 @@ void for_each_open_fd(OPEN_FD_ACTION action, OPEN_FD_EXCLUDE excluded_fds){ if(!(excluded_fds & OPEN_FD_EXCLUDE_STDERR)) (void)close(STDERR_FILENO); #if defined(HAVE_CLOSE_RANGE) if(close_range(STDERR_FILENO + 1, ~0U, 0) == 0) return; - error("close_range() failed, will try to close fds one by one"); + netdata_log_error("close_range() failed, will try to close fds one by one"); #endif break; case OPEN_FD_ACTION_FD_CLOEXEC: @@ -1846,7 +1778,7 @@ void for_each_open_fd(OPEN_FD_ACTION action, OPEN_FD_EXCLUDE excluded_fds){ if(!(excluded_fds & OPEN_FD_EXCLUDE_STDERR)) (void)fcntl(STDERR_FILENO, F_SETFD, FD_CLOEXEC); #if defined(HAVE_CLOSE_RANGE) && defined(CLOSE_RANGE_CLOEXEC) // Linux >= 5.11, FreeBSD >= 13.1 if(close_range(STDERR_FILENO + 1, ~0U, CLOSE_RANGE_CLOEXEC) == 0) return; - error("close_range() failed, will try to mark fds for closing one by one"); + netdata_log_error("close_range() failed, will try to mark fds for closing one by one"); #endif break; default: @@ -1999,7 +1931,7 @@ void timing_action(TIMING_ACTION action, TIMING_STEP step) { ); } - info("TIMINGS REPORT:\n%sTIMINGS REPORT: total # %10zu, t %11.2f ms", + netdata_log_info("TIMINGS REPORT:\n%sTIMINGS REPORT: total # %10zu, t %11.2f ms", buffer_tostring(wb), total_reqs, (double)total_usec / USEC_PER_MS); memcpy(timings2, timings3, sizeof(timings2)); |