procfile_adaptive_initial_allocation = 1;
time_t started_t = time(NULL);
- time_t current_t;
get_system_HZ();
get_system_pid_max();
get_system_cpus();
#ifndef PROFILING_MODE
unsigned long long sunext = (time(NULL) - (time(NULL) % update_every) + update_every) * 1000000ULL;
- unsigned long long sunow;
#endif /* PROFILING_MODE */
global_iterations_counter = 1;
for(;1; global_iterations_counter++) {
#ifndef PROFILING_MODE
+ unsigned long long sunow;
// delay until it is our time to run
while((sunow = time_usec()) < sunext)
sleep_usec(sunext - sunow);
if(unlikely(debug))
fprintf(stderr, "apps.plugin: done Loop No %llu\n", global_iterations_counter);
- current_t = time(NULL);
+ time_t current_t = time(NULL);
#ifndef PROFILING_MODE
// restart check (14400 seconds)
*/
void strreverse(char *begin, char *end) {
- char aux;
-
while (end > begin) {
// clearer code.
- aux = *end;
+ char aux = *end;
*end-- = *begin;
*begin++ = aux;
}
#ifdef MADV_MERGEABLE
static int log_madvise_2 = 1, log_madvise_3 = 1;
#endif
- int fd;
void *mem = NULL;
errno = 0;
- fd = open(filename, O_RDWR | O_CREAT | O_NOATIME, 0664);
+ 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) {
int processors = 1;
long get_system_cpus(void) {
- procfile *ff = NULL;
-
processors = 1;
char filename[FILENAME_MAX + 1];
snprintfz(filename, FILENAME_MAX, "%s/proc/stat", global_host_prefix);
- ff = procfile_open(filename, NULL, PROCFILE_FLAG_DEFAULT);
+ procfile *ff = procfile_open(filename, NULL, PROCFILE_FLAG_DEFAULT);
if(!ff) {
error("Cannot open file '%s'. Assuming system has %d processors.", filename, processors);
return processors;
pid_t pid_max = 32768;
pid_t get_system_pid_max(void) {
- procfile *ff = NULL;
-
char filename[FILENAME_MAX + 1];
snprintfz(filename, FILENAME_MAX, "%s/proc/sys/kernel/pid_max", global_host_prefix);
- ff = procfile_open(filename, NULL, PROCFILE_FLAG_DEFAULT);
+ procfile *ff = procfile_open(filename, NULL, PROCFILE_FLAG_DEFAULT);
if(!ff) {
error("Cannot open file '%s'. Assuming system supports %d pids.", filename, pid_max);
return pid_max;
error("Cannot set supplementary groups for user '%s'", username);
freez(supplementary_groups);
- supplementary_groups = NULL;
ngroups = 0;
}
create_needed_dir(VARLIB_DIR, getuid(), getgid());
}
- if(pidfd != -1) {
+ if(pidfd != -1)
close(pidfd);
- pidfd = -1;
- }
return(0);
}
// high level function to parse an expression or a sub-expression
static inline EVAL_NODE *parse_full_expression(const char **string, int *error) {
- EVAL_NODE *op1 = NULL;
-
- op1 = parse_one_full_operand(string, error);
+ EVAL_NODE *op1 = parse_one_full_operand(string, error);
if(!op1) {
*error = EVAL_ERROR_MISSING_OPERAND;
return NULL;
EVAL_EXPRESSION *expression_parse(const char *string, const char **failed_at, int *error) {
const char *s = string;
int err = EVAL_ERROR_OK;
- unsigned long pos = 0;
EVAL_NODE *op = parse_full_expression(&s, &err);
if (error) *error = err;
if(!op) {
- pos = s - string + 1;
+ unsigned long pos = s - string + 1;
error("failed to parse expression '%s': %s at character %lu (i.e.: '%s').", string, expression_strerror(err), pos, s);
return NULL;
}
static inline ssize_t health_alarm_log_read(RRDHOST *host, FILE *fp, const char *filename) {
static uint32_t max_unique_id = 0, max_alarm_id = 0;
- ssize_t loaded = -1, updated = -1, errored = -1, duplicate = -1;
errno = 0;
char *s, *buf = mallocz(65536 + 1);
size_t line = 0, len = 0;
- loaded = updated = errored = duplicate = 0;
+ ssize_t loaded = 0, updated = 0, errored = 0, duplicate = 0;
pthread_rwlock_rdlock(&host->health_log.alarm_log_rwlock);
*/
static inline int ipc_sem_get_status(struct ipc_status *st) {
- static int error_shown = 0;
struct seminfo seminfo;
union semun arg;
if(unlikely(semctl (0, 0, SEM_INFO, arg) < 0)) {
/* kernel not configured for semaphores */
+ static int error_shown = 0;
if(unlikely(!error_shown)) {
error("IPC: kernel is not configured for semaphores");
error_shown = 1;
}
int open_log_file(int fd, FILE **fp, const char *filename, int *enabled_syslog) {
- int f, t;
+ int f;
if(!filename || !*filename || !strcmp(filename, "none"))
filename = "/dev/null";
if(fd != f && fd != -1) {
// it automatically closes
- t = dup2(f, fd);
+ int t = dup2(f, fd);
if (t == -1) {
error("Cannot dup2() new fd %d to old fd %d for '%s'", f, fd, filename);
close(f);
while(susec < (rrd_update_every * 1000000ULL)) {
gettimeofday(&before, NULL);
- usleep(sleep_ms * 1000);
+ sleep_usec(sleep_ms * 1000);
gettimeofday(&after, NULL);
// calculate the time it took for a full loop
// the next time we will run - aligned properly
unsigned long long sunext = (time(NULL) - (time(NULL) % rrd_update_every) + rrd_update_every) * 1000000ULL;
- unsigned long long sunow;
for(;;) {
+ unsigned long long sunow;
if(unlikely(netdata_exit)) break;
// delay until it is our time to run
info("PLUGINSD: '%s' running on pid %d", cd->fullfilename, cd->pid);
RRDSET *st = NULL;
- char *s;
uint32_t hash;
while(likely(fgets(line, PLUGINSD_LINE_MAX, fp) != NULL)) {
// debug(D_PLUGINSD, "PLUGINSD: %s: %s", cd->filename, line);
int w = pluginsd_split_words(line, words, MAX_WORDS);
- s = words[0];
+ char *s = words[0];
if(unlikely(!s || !*s || !w)) {
// debug(D_PLUGINSD, "PLUGINSD: empty line");
continue;
if(unlikely(!ff)) return 0; // we return 0, so that we will retry to open it next time
uint32_t lines = procfile_lines(ff), l;
- uint32_t words;
// this is smart enough not to reload it every time
mountinfo_reload(0);
last_writes = 0, last_writesectors = 0, last_writems = 0,
last_busy_ms = 0;
- words = procfile_linewords(ff, l);
+ uint32_t words = procfile_linewords(ff, l);
if(unlikely(words < 14)) continue;
major = strtoul(procfile_lineword(ff, l, 0), NULL, 10);
return 0; // we return 0, so that we will retry to open it next time
uint32_t lines = procfile_lines(ff), l;
- uint32_t words = procfile_linewords(ff, 0), w;
+ uint32_t words = procfile_linewords(ff, 0);
if(unlikely(!lines)) {
error("Cannot read /proc/interrupts, zero lines reported.");
// find how many CPUs are there
if(unlikely(cpus == -1)) {
+ uint32_t w;
cpus = 0;
for(w = 0; w < words ; w++) {
if(unlikely(strncmp(procfile_lineword(ff, 0, w), "CPU", 3) == 0))
return 0; // we return 0, so that we will retry to open it next time
uint32_t lines = procfile_lines(ff), l;
- uint32_t words;
int hwcorrupted = 0;
HardwareCorrupted = 0;
for(l = 0; l < lines ;l++) {
- words = procfile_linewords(ff, l);
+ uint32_t words = procfile_linewords(ff, l);
if(unlikely(words < 2)) continue;
char *name = procfile_lineword(ff, l, 0);
if(do_events == -1) do_events = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "frames, collisions, carrier counters for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
uint32_t lines = procfile_lines(ff), l;
- uint32_t words;
char *iface;
unsigned long long rbytes, rpackets, rerrors, rdrops, rfifo, rframe, rcompressed, rmulticast;
unsigned long long tbytes, tpackets, terrors, tdrops, tfifo, tcollisions, tcarrier, tcompressed;
for(l = 2; l < lines ;l++) {
- words = procfile_linewords(ff, l);
+ uint32_t words = procfile_linewords(ff, l);
if(words < 17) continue;
iface = procfile_lineword(ff, l, 0);
if(do_proc4) do_proc4 = 1;
uint32_t lines = procfile_lines(ff), l;
- uint32_t words;
char *type;
unsigned long long net_count = 0, net_udp_count = 0, net_tcp_count = 0, net_tcp_connections = 0;
unsigned long long rpc_calls = 0, rpc_retransmits = 0, rpc_auth_refresh = 0;
for(l = 0; l < lines ;l++) {
- words = procfile_linewords(ff, l);
+ uint32_t words = procfile_linewords(ff, l);
if(!words) continue;
type = procfile_lineword(ff, l, 0);
if(do_proc4ops) do_proc4ops = 1;
uint32_t lines = procfile_lines(ff), l;
- uint32_t words;
char *type;
unsigned long long rc_hits = 0, rc_misses = 0, rc_nocache = 0;
unsigned long long rpc_calls = 0, rpc_bad_format = 0, rpc_bad_auth = 0, rpc_bad_client = 0;
for(l = 0; l < lines ;l++) {
- words = procfile_linewords(ff, l);
+ uint32_t words = procfile_linewords(ff, l);
if(!words) continue;
type = procfile_lineword(ff, l, 0);
return 0; // we return 0, so that we will retry to open it next time
uint32_t lines = procfile_lines(ff), l;
- uint32_t words;
unsigned long long Ip6InReceives = 0ULL;
unsigned long long Ip6InHdrErrors = 0ULL;
unsigned long long UdpLite6InCsumErrors = 0ULL;
for(l = 0; l < lines ;l++) {
- words = procfile_linewords(ff, l);
+ uint32_t words = procfile_linewords(ff, l);
if(unlikely(words < 2)) {
if(unlikely(words)) error("Cannot read /proc/net/snmp6 line %u. Expected 2 params, read %u.", l, words);
continue;
return 0; // we return 0, so that we will retry to open it next time
uint32_t lines = procfile_lines(ff), l;
- uint32_t words;
unsigned long long aentries = 0, asearched = 0, afound = 0, anew = 0, ainvalid = 0, aignore = 0, adelete = 0, adelete_list = 0,
ainsert = 0, ainsert_failed = 0, adrop = 0, aearly_drop = 0, aicmp_error = 0, aexpect_new = 0, aexpect_create = 0, aexpect_delete = 0, asearch_restart = 0;
for(l = 1; l < lines ;l++) {
- words = procfile_linewords(ff, l);
+ uint32_t words = procfile_linewords(ff, l);
if(unlikely(words < 17)) {
if(unlikely(words)) error("Cannot read /proc/net/stat/nf_conntrack line. Expected 17 params, read %u.", words);
continue;
// read the whole mountinfo into a linked list
struct mountinfo *mountinfo_read() {
- procfile *ff = NULL;
-
char filename[FILENAME_MAX + 1];
snprintfz(filename, FILENAME_MAX, "%s/proc/self/mountinfo", global_host_prefix);
- ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
+ procfile *ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
if(unlikely(!ff)) {
snprintfz(filename, FILENAME_MAX, "%s/proc/1/mountinfo", global_host_prefix);
ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
if(unlikely(!ff)) return 0; // we return 0, so that we will retry to open it next time
uint32_t lines = procfile_lines(ff), l;
- uint32_t words = procfile_linewords(ff, 0), w;
+ uint32_t words = procfile_linewords(ff, 0);
if(unlikely(!lines)) {
error("Cannot read /proc/softirqs, zero lines reported.");
// find how many CPUs are there
if(unlikely(cpus == -1)) {
+ uint32_t w;
cpus = 0;
for(w = 0; w < words ; w++) {
if(unlikely(strncmp(procfile_lineword(ff, 0, w), "CPU", 3) == 0))
if(unlikely(!ff)) return 0; // we return 0, so that we will retry to open it next time
uint32_t lines = procfile_lines(ff), l;
- uint32_t words;
// unsigned long long allocstall = 0ULL;
// unsigned long long compact_blocks_moved = 0ULL;
// unsigned long long unevictable_pgs_stranded = 0ULL;
for(l = 0; l < lines ;l++) {
- words = procfile_linewords(ff, l);
+ uint32_t words = procfile_linewords(ff, l);
if(unlikely(words < 2)) {
if(unlikely(words)) error("Cannot read /proc/vmstat line %u. Expected 2 params, read %u.", l, words);
continue;
procfile *procfile_readall(procfile *ff) {
debug(D_PROCFILE, PF_PREFIX ": Reading file '%s'.", ff->filename);
- ssize_t s, r = 1, x;
+ ssize_t r = 1;
ff->len = 0;
while(likely(r > 0)) {
- s = ff->len;
- x = ff->size - s;
+ ssize_t s = ff->len;
+ ssize_t x = ff->size - s;
if(unlikely(!x)) {
debug(D_PROCFILE, PF_PREFIX ": Expanding data buffer for file '%s'.", ff->filename);
static void procfile_set_separators(procfile *ff, const char *separators) {
static char def[256] = { [0 ... 255] = 0 };
- int i;
if(unlikely(!def[255])) {
// this is thread safe
// we check that the last byte is non-zero
// if it is zero, multiple threads may be executing this at the same time
// setting in def[] the exact same values
+ int i;
for(i = 0; likely(i < 256) ;i++) {
if(unlikely(i == '\n' || i == '\r')) def[i] = PF_CHAR_IS_NEWLINE;
else if(unlikely(isspace(i) || !isprint(i))) def[i] = PF_CHAR_IS_SEPARATOR;
void procfile_print(procfile *ff) {
uint32_t lines = procfile_lines(ff), l;
- uint32_t words, w;
char *s;
debug(D_PROCFILE, "File '%s' with %u lines and %u words", ff->filename, ff->lines->len, ff->words->len);
for(l = 0; likely(l < lines) ;l++) {
- words = procfile_linewords(ff, l);
+ uint32_t words = procfile_linewords(ff, l);
debug(D_PROCFILE, " line %u starts at word %u and has %u words", l, ff->lines->lines[l].first, ff->lines->lines[l].words);
+ uint32_t w;
for(w = 0; likely(w < words) ;w++) {
s = procfile_lineword(ff, l, w);
debug(D_PROCFILE, " [%u.%u] '%s'", l, w, s);
char fullid[RRD_ID_LENGTH_MAX + 1];
char fullfilename[FILENAME_MAX + 1];
- RRDSET *st = NULL;
snprintfz(fullid, RRD_ID_LENGTH_MAX, "%s.%s", type, id);
- st = rrdset_find(fullid);
+ RRDSET *st = rrdset_find(fullid);
if(st) {
error("Cannot create rrd stats for '%s', it already exists.", fullid);
return st;
}
unsigned long i = procfile_linewords(ff, 0);
- if(i <= 0) return;
+ if(i == 0) return;
// we may have 1 more CPU reported
while(i > 0) {
// the next time we will run - aligned properly
unsigned long long sunext = (time(NULL) - (time(NULL) % rrd_update_every) + rrd_update_every) * 1000000ULL;
- unsigned long long sunow;
RRDSET *stcpu_thread = NULL;
for(;;) {
+ unsigned long long sunow;
if(unlikely(netdata_exit)) break;
// delay until it is our time to run