void cancel_main_threads() { error_log_limit_unlimited(); int i, found = 0, max = 5 * USEC_PER_SEC, step = 100000; for (i = 0; static_threads[i].name != NULL ; i++) { if(static_threads[i].enabled == NETDATA_MAIN_THREAD_RUNNING) { info("EXIT: Stopping master thread: %s", static_threads[i].name); netdata_thread_cancel(*static_threads[i].thread); found++; } } while(found && max > 0) { max -= step; info("Waiting %d threads to finish...", found); sleep_usec(step); found = 0; for (i = 0; static_threads[i].name != NULL ; i++) { if (static_threads[i].enabled != NETDATA_MAIN_THREAD_EXITED) found++; } } if(found) { for (i = 0; static_threads[i].name != NULL ; i++) { if (static_threads[i].enabled != NETDATA_MAIN_THREAD_EXITED) error("Master thread %s takes too long to exit. Giving up...", static_threads[i].name); } } else info("All threads finished."); }
void *cpuidlejitter_main(void *ptr) { struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr; info("IDLEJITTER thread created with task id %d", gettid()); if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0) error("Cannot set pthread cancel type to DEFERRED."); if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0) error("Cannot set pthread cancel state to ENABLE."); int sleep_ms = (int) config_get_number("plugin:idlejitter", "loop time in ms", CPU_IDLEJITTER_SLEEP_TIME_MS); if(sleep_ms <= 0) { config_set_number("plugin:idlejitter", "loop time in ms", CPU_IDLEJITTER_SLEEP_TIME_MS); sleep_ms = CPU_IDLEJITTER_SLEEP_TIME_MS; } RRDSET *st = rrdset_find_localhost("system.idlejitter"); if(!st) { st = rrdset_create_localhost("system", "idlejitter", NULL, "processes", NULL, "CPU Idle Jitter" , "microseconds lost/s", 9999, localhost->rrd_update_every, RRDSET_TYPE_LINE); rrddim_add(st, "jitter", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE); } struct timeval before, after; unsigned long long counter; for(counter = 0; 1 ;counter++) { usec_t usec = 0, susec = 0; if(netdata_exit) break; while(susec < (localhost->rrd_update_every * USEC_PER_SEC)) { now_monotonic_timeval(&before); sleep_usec(sleep_ms * 1000); now_monotonic_timeval(&after); // calculate the time it took for a full loop usec = dt_usec(&after, &before); susec += usec; } usec -= (sleep_ms * 1000); if(counter) rrdset_next(st); rrddim_set(st, "jitter", usec); rrdset_done(st); } info("IDLEJITTER thread exiting"); static_thread->enabled = 0; pthread_exit(NULL); return NULL; }
usec_t heartbeat_next(heartbeat_t *hb, usec_t tick) { heartbeat_t now = now_monotonic_usec(); usec_t next = now - (now % tick) + tick; while(now < next) { sleep_usec(next - now); now = now_monotonic_usec(); } if(likely(*hb != 0ULL)) { usec_t dt = now - *hb; *hb = now; if(unlikely(dt >= tick + tick / 2)) error("heartbeat missed %llu microseconds", dt - tick); return dt; } else { *hb = now; return 0ULL; } }
void *proc_main(void *ptr) { (void)ptr; info("PROC Plugin thread created with task id %d", gettid()); if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0) error("Cannot set pthread cancel type to DEFERRED."); if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0) error("Cannot set pthread cancel state to ENABLE."); // disable (by default) various interface that are not needed config_get_boolean("plugin:proc:/proc/net/dev:lo", "enabled", 0); config_get_boolean("plugin:proc:/proc/net/dev:fireqos_monitor", "enabled", 0); // when ZERO, attempt to do it int vdo_proc_net_dev = !config_get_boolean("plugin:proc", "/proc/net/dev", 1); int vdo_proc_diskstats = !config_get_boolean("plugin:proc", "/proc/diskstats", 1); int vdo_proc_net_snmp = !config_get_boolean("plugin:proc", "/proc/net/snmp", 1); int vdo_proc_net_snmp6 = !config_get_boolean("plugin:proc", "/proc/net/snmp6", 1); int vdo_proc_net_netstat = !config_get_boolean("plugin:proc", "/proc/net/netstat", 1); int vdo_proc_net_stat_conntrack = !config_get_boolean("plugin:proc", "/proc/net/stat/conntrack", 1); int vdo_proc_net_ip_vs_stats = !config_get_boolean("plugin:proc", "/proc/net/ip_vs/stats", 1); int vdo_proc_net_stat_synproxy = !config_get_boolean("plugin:proc", "/proc/net/stat/synproxy", 1); int vdo_proc_stat = !config_get_boolean("plugin:proc", "/proc/stat", 1); int vdo_proc_meminfo = !config_get_boolean("plugin:proc", "/proc/meminfo", 1); int vdo_proc_vmstat = !config_get_boolean("plugin:proc", "/proc/vmstat", 1); int vdo_proc_net_rpc_nfs = !config_get_boolean("plugin:proc", "/proc/net/rpc/nfs", 1); int vdo_proc_net_rpc_nfsd = !config_get_boolean("plugin:proc", "/proc/net/rpc/nfsd", 1); int vdo_proc_sys_kernel_random_entropy_avail = !config_get_boolean("plugin:proc", "/proc/sys/kernel/random/entropy_avail", 1); int vdo_proc_interrupts = !config_get_boolean("plugin:proc", "/proc/interrupts", 1); int vdo_proc_softirqs = !config_get_boolean("plugin:proc", "/proc/softirqs", 1); int vdo_proc_net_softnet_stat = !config_get_boolean("plugin:proc", "/proc/net/softnet_stat", 1); int vdo_proc_loadavg = !config_get_boolean("plugin:proc", "/proc/loadavg", 1); int vdo_ipc = !config_get_boolean("plugin:proc", "ipc", 1); int vdo_sys_kernel_mm_ksm = !config_get_boolean("plugin:proc", "/sys/kernel/mm/ksm", 1); int vdo_cpu_netdata = !config_get_boolean("plugin:proc", "netdata server resources", 1); // keep track of the time each module was called unsigned long long sutime_proc_net_dev = 0ULL; unsigned long long sutime_proc_diskstats = 0ULL; unsigned long long sutime_proc_net_snmp = 0ULL; unsigned long long sutime_proc_net_snmp6 = 0ULL; unsigned long long sutime_proc_net_netstat = 0ULL; unsigned long long sutime_proc_net_stat_conntrack = 0ULL; unsigned long long sutime_proc_net_ip_vs_stats = 0ULL; unsigned long long sutime_proc_net_stat_synproxy = 0ULL; unsigned long long sutime_proc_stat = 0ULL; unsigned long long sutime_proc_meminfo = 0ULL; unsigned long long sutime_proc_vmstat = 0ULL; unsigned long long sutime_proc_net_rpc_nfs = 0ULL; unsigned long long sutime_proc_net_rpc_nfsd = 0ULL; unsigned long long sutime_proc_sys_kernel_random_entropy_avail = 0ULL; unsigned long long sutime_proc_interrupts = 0ULL; unsigned long long sutime_proc_softirqs = 0ULL; unsigned long long sutime_proc_net_softnet_stat = 0ULL; unsigned long long sutime_proc_loadavg = 0ULL; unsigned long long sutime_ipc = 0ULL; unsigned long long sutime_sys_kernel_mm_ksm = 0ULL; unsigned long long step = rrd_update_every * 1000000ULL; for(;;) { unsigned long long now = time_usec(); unsigned long long next = now - (now % step) + step; while(now < next) { sleep_usec(next - now); now = time_usec(); } if(unlikely(netdata_exit)) break; // BEGIN -- the job to be done if(!vdo_sys_kernel_mm_ksm) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling do_sys_kernel_mm_ksm()."); now = time_usec(); vdo_sys_kernel_mm_ksm = do_sys_kernel_mm_ksm(rrd_update_every, (sutime_sys_kernel_mm_ksm > 0)?now - sutime_sys_kernel_mm_ksm:0ULL); sutime_sys_kernel_mm_ksm = now; } if(unlikely(netdata_exit)) break; if(!vdo_proc_loadavg) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling do_proc_loadavg()."); now = time_usec(); vdo_proc_loadavg = do_proc_loadavg(rrd_update_every, (sutime_proc_loadavg > 0)?now - sutime_proc_loadavg:0ULL); sutime_proc_loadavg = now; } if(unlikely(netdata_exit)) break; if(!vdo_ipc) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling do_ipc()."); now = time_usec(); vdo_ipc = do_ipc(rrd_update_every, (sutime_ipc > 0)?now - sutime_ipc:0ULL); sutime_ipc = now; } if(unlikely(netdata_exit)) break; if(!vdo_proc_interrupts) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling do_proc_interrupts()."); now = time_usec(); vdo_proc_interrupts = do_proc_interrupts(rrd_update_every, (sutime_proc_interrupts > 0)?now - sutime_proc_interrupts:0ULL); sutime_proc_interrupts = now; } if(unlikely(netdata_exit)) break; if(!vdo_proc_softirqs) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling do_proc_softirqs()."); now = time_usec(); vdo_proc_softirqs = do_proc_softirqs(rrd_update_every, (sutime_proc_softirqs > 0)?now - sutime_proc_softirqs:0ULL); sutime_proc_softirqs = now; } if(unlikely(netdata_exit)) break; if(!vdo_proc_net_softnet_stat) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling do_proc_net_softnet_stat()."); now = time_usec(); vdo_proc_net_softnet_stat = do_proc_net_softnet_stat(rrd_update_every, (sutime_proc_net_softnet_stat > 0)?now - sutime_proc_net_softnet_stat:0ULL); sutime_proc_net_softnet_stat = now; } if(unlikely(netdata_exit)) break; if(!vdo_proc_sys_kernel_random_entropy_avail) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling do_proc_sys_kernel_random_entropy_avail()."); now = time_usec(); vdo_proc_sys_kernel_random_entropy_avail = do_proc_sys_kernel_random_entropy_avail(rrd_update_every, (sutime_proc_sys_kernel_random_entropy_avail > 0)?now - sutime_proc_sys_kernel_random_entropy_avail:0ULL); sutime_proc_sys_kernel_random_entropy_avail = now; } if(unlikely(netdata_exit)) break; if(!vdo_proc_net_dev) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling do_proc_net_dev()."); now = time_usec(); vdo_proc_net_dev = do_proc_net_dev(rrd_update_every, (sutime_proc_net_dev > 0)?now - sutime_proc_net_dev:0ULL); sutime_proc_net_dev = now; } if(unlikely(netdata_exit)) break; if(!vdo_proc_diskstats) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling do_proc_diskstats()."); now = time_usec(); vdo_proc_diskstats = do_proc_diskstats(rrd_update_every, (sutime_proc_diskstats > 0)?now - sutime_proc_diskstats:0ULL); sutime_proc_diskstats = now; } if(unlikely(netdata_exit)) break; if(!vdo_proc_net_snmp) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling do_proc_net_snmp()."); now = time_usec(); vdo_proc_net_snmp = do_proc_net_snmp(rrd_update_every, (sutime_proc_net_snmp > 0)?now - sutime_proc_net_snmp:0ULL); sutime_proc_net_snmp = now; } if(unlikely(netdata_exit)) break; if(!vdo_proc_net_snmp6) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling do_proc_net_snmp6()."); now = time_usec(); vdo_proc_net_snmp6 = do_proc_net_snmp6(rrd_update_every, (sutime_proc_net_snmp6 > 0)?now - sutime_proc_net_snmp6:0ULL); sutime_proc_net_snmp6 = now; } if(unlikely(netdata_exit)) break; if(!vdo_proc_net_netstat) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling do_proc_net_netstat()."); now = time_usec(); vdo_proc_net_netstat = do_proc_net_netstat(rrd_update_every, (sutime_proc_net_netstat > 0)?now - sutime_proc_net_netstat:0ULL); sutime_proc_net_netstat = now; } if(unlikely(netdata_exit)) break; if(!vdo_proc_net_stat_conntrack) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling do_proc_net_stat_conntrack()."); now = time_usec(); vdo_proc_net_stat_conntrack = do_proc_net_stat_conntrack(rrd_update_every, (sutime_proc_net_stat_conntrack > 0)?now - sutime_proc_net_stat_conntrack:0ULL); sutime_proc_net_stat_conntrack = now; } if(unlikely(netdata_exit)) break; if(!vdo_proc_net_ip_vs_stats) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling vdo_proc_net_ip_vs_stats()."); now = time_usec(); vdo_proc_net_ip_vs_stats = do_proc_net_ip_vs_stats(rrd_update_every, (sutime_proc_net_ip_vs_stats > 0)?now - sutime_proc_net_ip_vs_stats:0ULL); sutime_proc_net_ip_vs_stats = now; } if(unlikely(netdata_exit)) break; if(!vdo_proc_net_stat_synproxy) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling vdo_proc_net_stat_synproxy()."); now = time_usec(); vdo_proc_net_stat_synproxy = do_proc_net_stat_synproxy(rrd_update_every, (sutime_proc_net_stat_synproxy > 0)?now - sutime_proc_net_stat_synproxy:0ULL); sutime_proc_net_stat_synproxy = now; } if(unlikely(netdata_exit)) break; if(!vdo_proc_stat) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling do_proc_stat()."); now = time_usec(); vdo_proc_stat = do_proc_stat(rrd_update_every, (sutime_proc_stat > 0)?now - sutime_proc_stat:0ULL); sutime_proc_stat = now; } if(unlikely(netdata_exit)) break; if(!vdo_proc_meminfo) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling vdo_proc_meminfo()."); now = time_usec(); vdo_proc_meminfo = do_proc_meminfo(rrd_update_every, (sutime_proc_meminfo > 0)?now - sutime_proc_meminfo:0ULL); sutime_proc_meminfo = now; } if(unlikely(netdata_exit)) break; if(!vdo_proc_vmstat) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling vdo_proc_vmstat()."); now = time_usec(); vdo_proc_vmstat = do_proc_vmstat(rrd_update_every, (sutime_proc_vmstat > 0)?now - sutime_proc_vmstat:0ULL); sutime_proc_vmstat = now; } if(unlikely(netdata_exit)) break; if(!vdo_proc_net_rpc_nfsd) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling do_proc_net_rpc_nfsd()."); now = time_usec(); vdo_proc_net_rpc_nfsd = do_proc_net_rpc_nfsd(rrd_update_every, (sutime_proc_net_rpc_nfsd > 0)?now - sutime_proc_net_rpc_nfsd:0ULL); sutime_proc_net_rpc_nfsd = now; } if(unlikely(netdata_exit)) break; if(!vdo_proc_net_rpc_nfs) { debug(D_PROCNETDEV_LOOP, "PROCNETDEV: calling do_proc_net_rpc_nfs()."); now = time_usec(); vdo_proc_net_rpc_nfs = do_proc_net_rpc_nfs(rrd_update_every, (sutime_proc_net_rpc_nfs > 0)?now - sutime_proc_net_rpc_nfs:0ULL); sutime_proc_net_rpc_nfs = now; } if(unlikely(netdata_exit)) break; // END -- the job is done // -------------------------------------------------------------------- if(!vdo_cpu_netdata) { global_statistics_charts(); registry_statistics(); } } info("PROC thread exiting"); pthread_exit(NULL); return NULL; }