@cryptotaxi247 / netdata-1 / commits / 4b25987ed

add randomness per thread to heartbeat (#18929)

* add randomness per thread to heartbeat * tune randomness * use the thread tag name in the randomness hash * use os_gettid() * fix xenstat * fix all occurences of heartbeat_init and heartbeat_next * initialize clocks with a constructor; on windows enable high resolution timer globally * better distribution of heartbeats; prevent heartbeats to be aligned with system HZ * move randomness away from scheduler ticks * make sure randomness is not the same across processes * make randomness spread even across processes

Costa Tsaousis committed Nov 2, 2024 at 18:11 UTC 4b25987ed4d92ddb45fea56395ddcc74682291d0
54 files changed +219 -242
src/cli/cli.c
-1
@@ -155,7 +155,6 @@ static void connect_cb(uv_connect_t* req, int status)
155
156 int main(int argc, char **argv)
157 {
158 - clocks_init();
158 nd_log_initialize_for_external_plugins("netdatacli");
159
160 int ret, i;
src/collectors/apps.plugin/apps_plugin.c
+2 -5
@@ -665,7 +665,6 @@ netdata_mutex_t apps_and_stdout_mutex = NETDATA_MUTEX_INITIALIZER;
665 static bool apps_plugin_exit = false;
666
667 int main(int argc, char **argv) {
668 - clocks_init();
668 nd_log_initialize_for_external_plugins("apps.plugin");
669
670 pagesize = (size_t)sysconf(_SC_PAGESIZE);
@@ -709,7 +708,6 @@ int main(int argc, char **argv) {
708 #endif /* NETDATA_INTERNAL_CHECKS */
709
710 procfile_set_adaptive_allocation(true, 0, 0, 0);
712 - os_get_system_HZ();
711 os_get_system_cpus_uncached();
712 apps_managers_and_aggregators_init(); // before parsing args!
713 parse_args(argc, argv);
@@ -763,10 +761,9 @@ int main(int argc, char **argv) {
761 netdata_mutex_lock(&apps_and_stdout_mutex);
762 APPS_PLUGIN_GLOBAL_FUNCTIONS();
763
766 - usec_t step = update_every * USEC_PER_SEC;
764 global_iterations_counter = 1;
765 heartbeat_t hb;
769 - heartbeat_init(&hb);
766 + heartbeat_init(&hb, update_every * USEC_PER_SEC);
767 for(; !apps_plugin_exit ; global_iterations_counter++) {
768 netdata_mutex_unlock(&apps_and_stdout_mutex);
769
@@ -778,7 +775,7 @@ int main(int argc, char **argv) {
775 dt = update_every * USEC_PER_SEC;
776 }
777 else
781 - dt = heartbeat_next(&hb, step);
778 + dt = heartbeat_next(&hb);
779
780 netdata_mutex_lock(&apps_and_stdout_mutex);
781
src/collectors/cgroups.plugin/cgroup-network.c
-2
@@ -717,8 +717,6 @@ void usage(void) {
717 int main(int argc, const char **argv) {
718 pid_t pid = 0;
719
720 - clocks_init();
721 -
720 if (setresuid(0, 0, 0) == -1)
721 collector_error("setresuid(0, 0, 0) failed.");
722
src/collectors/cgroups.plugin/sys_fs_cgroup.c
+2 -3
@@ -1417,14 +1417,13 @@ void *cgroups_main(void *ptr) {
1417 cgroup_function_systemd_top);
1418
1419 heartbeat_t hb;
1420 - heartbeat_init(&hb);
1421 - usec_t step = cgroup_update_every * USEC_PER_SEC;
1420 + heartbeat_init(&hb, cgroup_update_every * USEC_PER_SEC);
1421 usec_t find_every = cgroup_check_for_new_every * USEC_PER_SEC, find_dt = 0;
1422
1423 while(service_running(SERVICE_COLLECTORS)) {
1424 worker_is_idle();
1425
1427 - usec_t hb_dt = heartbeat_next(&hb, step);
1426 + usec_t hb_dt = heartbeat_next(&hb);
1427
1428 if (unlikely(!service_running(SERVICE_COLLECTORS)))
1429 break;
src/collectors/cups.plugin/cups_plugin.c
+2 -4
@@ -226,7 +226,6 @@ void reset_metrics() {
226 }
227
228 int main(int argc, char **argv) {
229 - clocks_init();
229 nd_log_initialize_for_external_plugins("cups.plugin");
230
231 parse_command_line(argc, argv);
@@ -243,12 +242,11 @@ int main(int argc, char **argv) {
242
243 time_t started_t = now_monotonic_sec();
244 size_t iteration = 0;
246 - usec_t step = netdata_update_every * USEC_PER_SEC;
245
246 heartbeat_t hb;
249 - heartbeat_init(&hb);
247 + heartbeat_init(&hb, netdata_update_every * USEC_PER_SEC);
248 for (iteration = 0; 1; iteration++) {
251 - heartbeat_next(&hb, step);
249 + heartbeat_next(&hb);
250
251 if (unlikely(netdata_exit))
252 break;
src/collectors/debugfs.plugin/debugfs_plugin.c
+2 -4
@@ -159,7 +159,6 @@ static void debugfs_parse_args(int argc, char **argv)
159
160 int main(int argc, char **argv)
161 {
162 - clocks_init();
162 nd_log_initialize_for_external_plugins("debugfs.plugin");
163
164 netdata_configured_host_prefix = getenv("NETDATA_HOST_PREFIX");
@@ -214,12 +213,11 @@ int main(int argc, char **argv)
213 debugfs_parse_args(argc, argv);
214
215 size_t iteration;
217 - usec_t step = update_every * USEC_PER_SEC;
216 heartbeat_t hb;
219 - heartbeat_init(&hb);
217 + heartbeat_init(&hb, update_every * USEC_PER_SEC);
218
219 for (iteration = 0; iteration < 86400; iteration++) {
222 - heartbeat_next(&hb, step);
220 + heartbeat_next(&hb);
221 int enabled = 0;
222
223 for (int i = 0; debugfs_modules[i].name; i++) {
src/collectors/diskspace.plugin/plugin_diskspace.c
+4 -5
@@ -544,11 +544,11 @@ void *diskspace_slow_worker(void *ptr)
544 usec_t step = slow_update_every * USEC_PER_SEC;
545 usec_t real_step = USEC_PER_SEC;
546 heartbeat_t hb;
547 - heartbeat_init(&hb);
547 + heartbeat_init(&hb, USEC_PER_SEC);
548
549 while(service_running(SERVICE_COLLECTORS)) {
550 worker_is_idle();
551 - heartbeat_next(&hb, USEC_PER_SEC);
551 + heartbeat_next(&hb);
552
553 if (real_step < step) {
554 real_step += USEC_PER_SEC;
@@ -876,12 +876,11 @@ void *diskspace_main(void *ptr) {
876 diskspace_slow_worker,
877 &slow_worker_data);
878
879 - usec_t step = update_every * USEC_PER_SEC;
879 heartbeat_t hb;
881 - heartbeat_init(&hb);
880 + heartbeat_init(&hb, update_every * USEC_PER_SEC);
881 while(service_running(SERVICE_COLLECTORS)) {
882 worker_is_idle();
884 - /* usec_t hb_dt = */ heartbeat_next(&hb, step);
883 + /* usec_t hb_dt = */ heartbeat_next(&hb);
884
885 if(unlikely(!service_running(SERVICE_COLLECTORS))) break;
886
src/collectors/ebpf.plugin/ebpf.c
+2 -4
@@ -4005,7 +4005,6 @@ static void ebpf_manage_pid(pid_t pid)
4005 */
4006 int main(int argc, char **argv)
4007 {
4008 - clocks_init();
4008 nd_log_initialize_for_external_plugins(NETDATA_EBPF_PLUGIN_NAME);
4009
4010 ebpf_set_global_variables();
@@ -4076,15 +4075,14 @@ int main(int argc, char **argv)
4075 }
4076 }
4077
4079 - usec_t step = USEC_PER_SEC;
4078 heartbeat_t hb;
4081 - heartbeat_init(&hb);
4079 + heartbeat_init(&hb, USEC_PER_SEC);
4080 int update_apps_every = (int) EBPF_CFG_UPDATE_APPS_EVERY_DEFAULT;
4081 int update_apps_list = update_apps_every - 1;
4082 int process_maps_per_core = ebpf_modules[EBPF_MODULE_PROCESS_IDX].maps_per_core;
4083 //Plugin will be killed when it receives a signal
4084 for ( ; !ebpf_plugin_stop(); global_iterations_counter++) {
4087 - (void)heartbeat_next(&hb, step);
4085 + (void)heartbeat_next(&hb);
4086
4087 if (global_iterations_counter % EBPF_DEFAULT_UPDATE_EVERY == 0) {
4088 pthread_mutex_lock(&lock);
src/collectors/ebpf.plugin/ebpf_cachestat.c
+5 -7
@@ -837,9 +837,6 @@ void ebpf_resume_apps_data()
837 */
838 void *ebpf_read_cachestat_thread(void *ptr)
839 {
840 - heartbeat_t hb;
841 - heartbeat_init(&hb);
842 -
840 ebpf_module_t *em = (ebpf_module_t *)ptr;
841
842 int maps_per_core = em->maps_per_core;
@@ -849,10 +846,11 @@ void *ebpf_read_cachestat_thread(void *ptr)
846
847 uint32_t lifetime = em->lifetime;
848 uint32_t running_time = 0;
852 - usec_t period = update_every * USEC_PER_SEC;
849 pids_fd[EBPF_PIDS_CACHESTAT_IDX] = cachestat_maps[NETDATA_CACHESTAT_PID_STATS].map_fd;
850 + heartbeat_t hb;
851 + heartbeat_init(&hb, update_every * USEC_PER_SEC);
852 while (!ebpf_plugin_stop() && running_time < lifetime) {
855 - (void)heartbeat_next(&hb, period);
853 + (void)heartbeat_next(&hb);
854 if (ebpf_plugin_stop() || ++counter != update_every)
855 continue;
856
@@ -1401,7 +1399,7 @@ static void cachestat_collector(ebpf_module_t *em)
1399 int update_every = em->update_every;
1400 int maps_per_core = em->maps_per_core;
1401 heartbeat_t hb;
1404 - heartbeat_init(&hb);
1402 + heartbeat_init(&hb, USEC_PER_SEC);
1403 int counter = update_every - 1;
1404 //This will be cancelled by its parent
1405 uint32_t running_time = 0;
@@ -1409,7 +1407,7 @@ static void cachestat_collector(ebpf_module_t *em)
1407 netdata_idx_t *stats = em->hash_table_stats;
1408 memset(stats, 0, sizeof(em->hash_table_stats));
1409 while (!ebpf_plugin_stop() && running_time < lifetime) {
1412 - (void)heartbeat_next(&hb, USEC_PER_SEC);
1410 + (void)heartbeat_next(&hb);
1411
1412 if (ebpf_plugin_stop() || ++counter != update_every)
1413 continue;
src/collectors/ebpf.plugin/ebpf_cgroup.c
+2 -3
@@ -373,13 +373,12 @@ void ebpf_create_charts_on_systemd(ebpf_systemd_args_t *chart)
373 */
374 void *ebpf_cgroup_integration(void *ptr __maybe_unused)
375 {
376 - usec_t step = USEC_PER_SEC;
376 int counter = NETDATA_EBPF_CGROUP_UPDATE - 1;
377 heartbeat_t hb;
379 - heartbeat_init(&hb);
378 + heartbeat_init(&hb, USEC_PER_SEC);
379 //Plugin will be killed when it receives a signal
380 while (!ebpf_plugin_stop()) {
382 - (void)heartbeat_next(&hb, step);
381 + heartbeat_next(&hb);
382
383 // We are using a small heartbeat time to wake up thread,
384 // but we should not update so frequently the shared memory data
src/collectors/ebpf.plugin/ebpf_dcstat.c
+5 -7
@@ -639,9 +639,6 @@ void ebpf_dc_resume_apps_data()
639 */
640 void *ebpf_read_dcstat_thread(void *ptr)
641 {
642 - heartbeat_t hb;
643 - heartbeat_init(&hb);
644 -
642 ebpf_module_t *em = (ebpf_module_t *)ptr;
643
644 int maps_per_core = em->maps_per_core;
@@ -654,10 +651,11 @@ void *ebpf_read_dcstat_thread(void *ptr)
651
652 uint32_t lifetime = em->lifetime;
653 uint32_t running_time = 0;
657 - usec_t period = update_every * USEC_PER_SEC;
654 pids_fd[EBPF_PIDS_DCSTAT_IDX] = dcstat_maps[NETDATA_DCSTAT_PID_STATS].map_fd;
655 + heartbeat_t hb;
656 + heartbeat_init(&hb, update_every * USEC_PER_SEC);
657 while (!ebpf_plugin_stop() && running_time < lifetime) {
660 - (void)heartbeat_next(&hb, period);
658 + (void)heartbeat_next(&hb);
659 if (ebpf_plugin_stop() || ++counter != update_every)
660 continue;
661
@@ -1265,7 +1263,7 @@ static void dcstat_collector(ebpf_module_t *em)
1263 int cgroups = em->cgroup_charts;
1264 int update_every = em->update_every;
1265 heartbeat_t hb;
1268 - heartbeat_init(&hb);
1266 + heartbeat_init(&hb, USEC_PER_SEC);
1267 int counter = update_every - 1;
1268 int maps_per_core = em->maps_per_core;
1269 uint32_t running_time = 0;
@@ -1273,7 +1271,7 @@ static void dcstat_collector(ebpf_module_t *em)
1271 netdata_idx_t *stats = em->hash_table_stats;
1272 memset(stats, 0, sizeof(em->hash_table_stats));
1273 while (!ebpf_plugin_stop() && running_time < lifetime) {
1276 - (void)heartbeat_next(&hb, USEC_PER_SEC);
1274 + heartbeat_next(&hb);
1275
1276 if (ebpf_plugin_stop() || ++counter != update_every)
1277 continue;
src/collectors/ebpf.plugin/ebpf_disk.c
+2 -2
@@ -771,13 +771,13 @@ static void disk_collector(ebpf_module_t *em)
771
772 int update_every = em->update_every;
773 heartbeat_t hb;
774 - heartbeat_init(&hb);
774 + heartbeat_init(&hb, USEC_PER_SEC);
775 int counter = update_every - 1;
776 int maps_per_core = em->maps_per_core;
777 uint32_t running_time = 0;
778 uint32_t lifetime = em->lifetime;
779 while (!ebpf_plugin_stop() && running_time < lifetime) {
780 - (void)heartbeat_next(&hb, USEC_PER_SEC);
780 + heartbeat_next(&hb);
781
782 if (ebpf_plugin_stop() || ++counter != update_every)
783 continue;
src/collectors/ebpf.plugin/ebpf_fd.c
+7 -8
@@ -780,9 +780,6 @@ void ebpf_fd_resume_apps_data()
780 */
781 void *ebpf_read_fd_thread(void *ptr)
782 {
783 - heartbeat_t hb;
784 - heartbeat_init(&hb);
785 -
783 ebpf_module_t *em = (ebpf_module_t *)ptr;
784
785 int maps_per_core = em->maps_per_core;
@@ -795,10 +792,12 @@ void *ebpf_read_fd_thread(void *ptr)
792
793 uint32_t lifetime = em->lifetime;
794 uint32_t running_time = 0;
798 - int period = USEC_PER_SEC;
795 pids_fd[EBPF_PIDS_FD_IDX] = fd_maps[NETDATA_FD_PID_STATS].map_fd;
796 +
797 + heartbeat_t hb;
798 + heartbeat_init(&hb, USEC_PER_SEC);
799 while (!ebpf_plugin_stop() && running_time < lifetime) {
801 - (void)heartbeat_next(&hb, period);
800 + heartbeat_next(&hb);
801 if (ebpf_plugin_stop() || ++counter != update_every)
802 continue;
803
@@ -1213,8 +1212,6 @@ static void ebpf_fd_send_cgroup_data(ebpf_module_t *em)
1212 static void fd_collector(ebpf_module_t *em)
1213 {
1214 int cgroups = em->cgroup_charts;
1216 - heartbeat_t hb;
1217 - heartbeat_init(&hb);
1215 int update_every = em->update_every;
1216 int counter = update_every - 1;
1217 int maps_per_core = em->maps_per_core;
@@ -1222,8 +1219,10 @@ static void fd_collector(ebpf_module_t *em)
1219 uint32_t lifetime = em->lifetime;
1220 netdata_idx_t *stats = em->hash_table_stats;
1221 memset(stats, 0, sizeof(em->hash_table_stats));
1222 + heartbeat_t hb;
1223 + heartbeat_init(&hb, USEC_PER_SEC);
1224 while (!ebpf_plugin_stop() && running_time < lifetime) {
1226 - (void)heartbeat_next(&hb, USEC_PER_SEC);
1225 + heartbeat_next(&hb);
1226
1227 if (ebpf_plugin_stop() || ++counter != update_every)
1228 continue;
src/collectors/ebpf.plugin/ebpf_filesystem.c
+3 -3
@@ -980,13 +980,13 @@ static void ebpf_histogram_send_data()
980 static void filesystem_collector(ebpf_module_t *em)
981 {
982 int update_every = em->update_every;
983 - heartbeat_t hb;
984 - heartbeat_init(&hb);
983 int counter = update_every - 1;
984 uint32_t running_time = 0;
985 uint32_t lifetime = em->lifetime;
986 + heartbeat_t hb;
987 + heartbeat_init(&hb, USEC_PER_SEC);
988 while (!ebpf_plugin_stop() && running_time < lifetime) {
989 - (void)heartbeat_next(&hb, USEC_PER_SEC);
989 + heartbeat_next(&hb);
990
991 if (ebpf_plugin_stop() || ++counter != update_every)
992 continue;
src/collectors/ebpf.plugin/ebpf_functions.c
+2 -2
@@ -712,9 +712,9 @@ void *ebpf_function_thread(void *ptr)
712 pthread_mutex_unlock(&lock);
713
714 heartbeat_t hb;
715 - heartbeat_init(&hb);
715 + heartbeat_init(&hb, USEC_PER_SEC);
716 while(!ebpf_plugin_stop()) {
717 - (void)heartbeat_next(&hb, USEC_PER_SEC);
717 + heartbeat_next(&hb);
718
719 if (ebpf_plugin_stop()) {
720 break;
src/collectors/ebpf.plugin/ebpf_hardirq.c
+3 -3
@@ -571,15 +571,15 @@ static void hardirq_collector(ebpf_module_t *em)
571 pthread_mutex_unlock(&lock);
572
573 // loop and read from published data until ebpf plugin is closed.
574 - heartbeat_t hb;
575 - heartbeat_init(&hb);
574 int update_every = em->update_every;
575 int counter = update_every - 1;
576 //This will be cancelled by its parent
577 uint32_t running_time = 0;
578 uint32_t lifetime = em->lifetime;
579 + heartbeat_t hb;
580 + heartbeat_init(&hb, USEC_PER_SEC);
581 while (!ebpf_plugin_stop() && running_time < lifetime) {
582 - (void)heartbeat_next(&hb, USEC_PER_SEC);
582 + heartbeat_next(&hb);
583
584 if (ebpf_plugin_stop() || ++counter != update_every)
585 continue;
src/collectors/ebpf.plugin/ebpf_mdflush.c
+3 -3
@@ -337,14 +337,14 @@ static void mdflush_collector(ebpf_module_t *em)
337 pthread_mutex_unlock(&lock);
338
339 // loop and read from published data until ebpf plugin is closed.
340 - heartbeat_t hb;
341 - heartbeat_init(&hb);
340 int counter = update_every - 1;
341 int maps_per_core = em->maps_per_core;
342 uint32_t running_time = 0;
343 uint32_t lifetime = em->lifetime;
344 + heartbeat_t hb;
345 + heartbeat_init(&hb, USEC_PER_SEC);
346 while (!ebpf_plugin_stop() && running_time < lifetime) {
347 - (void)heartbeat_next(&hb, USEC_PER_SEC);
347 + heartbeat_next(&hb);
348
349 if (ebpf_plugin_stop() || ++counter != update_every)
350 continue;
src/collectors/ebpf.plugin/ebpf_mount.c
+3 -3
@@ -361,15 +361,15 @@ static void mount_collector(ebpf_module_t *em)
361 {
362 memset(mount_hash_values, 0, sizeof(mount_hash_values));
363
364 - heartbeat_t hb;
365 - heartbeat_init(&hb);
364 int update_every = em->update_every;
365 int counter = update_every - 1;
366 int maps_per_core = em->maps_per_core;
367 uint32_t running_time = 0;
368 uint32_t lifetime = em->lifetime;
369 + heartbeat_t hb;
370 + heartbeat_init(&hb, USEC_PER_SEC);
371 while (!ebpf_plugin_stop() && running_time < lifetime) {
372 - (void)heartbeat_next(&hb, USEC_PER_SEC);
372 + heartbeat_next(&hb);
373 if (ebpf_plugin_stop() || ++counter != update_every)
374 continue;
375
src/collectors/ebpf.plugin/ebpf_oomkill.c
+3 -3
@@ -459,14 +459,14 @@ static void oomkill_collector(ebpf_module_t *em)
459 memset(keys, 0, sizeof(keys));
460
461 // loop and read until ebpf plugin is closed.
462 - heartbeat_t hb;
463 - heartbeat_init(&hb);
462 int counter = update_every - 1;
463 uint32_t running_time = 0;
464 uint32_t lifetime = em->lifetime;
465 netdata_idx_t *stats = em->hash_table_stats;
466 + heartbeat_t hb;
467 + heartbeat_init(&hb, USEC_PER_SEC);
468 while (!ebpf_plugin_stop() && running_time < lifetime) {
469 - (void)heartbeat_next(&hb, USEC_PER_SEC);
469 + (void)heartbeat_next(&hb);
470 if (ebpf_plugin_stop() || ++counter != update_every)
471 continue;
472
src/collectors/ebpf.plugin/ebpf_process.c
+4 -4
@@ -1120,8 +1120,6 @@ void ebpf_process_update_cgroup_algorithm()
1120 */
1121 static void process_collector(ebpf_module_t *em)
1122 {
1123 - heartbeat_t hb;
1124 - heartbeat_init(&hb);
1123 int publish_global = em->global_charts;
1124 int cgroups = em->cgroup_charts;
1125 pthread_mutex_lock(&ebpf_exit_cleanup);
@@ -1137,9 +1135,11 @@ static void process_collector(ebpf_module_t *em)
1135 uint32_t lifetime = em->lifetime;
1136 netdata_idx_t *stats = em->hash_table_stats;
1137 memset(stats, 0, sizeof(em->hash_table_stats));
1138 + heartbeat_t hb;
1139 + heartbeat_init(&hb, USEC_PER_SEC);
1140 while (!ebpf_plugin_stop() && running_time < lifetime) {
1141 - usec_t dt = heartbeat_next(&hb, USEC_PER_SEC);
1142 - (void)dt;
1141 + heartbeat_next(&hb);
1142 +
1143 if (ebpf_plugin_stop())
1144 break;
1145
src/collectors/ebpf.plugin/ebpf_shm.c
+7 -8
@@ -1058,9 +1058,6 @@ void ebpf_shm_resume_apps_data() {
1058 */
1059 void *ebpf_read_shm_thread(void *ptr)
1060 {
1061 - heartbeat_t hb;
1062 - heartbeat_init(&hb);
1063 -
1061 ebpf_module_t *em = (ebpf_module_t *)ptr;
1062
1063 int maps_per_core = em->maps_per_core;
@@ -1073,10 +1070,11 @@ void *ebpf_read_shm_thread(void *ptr)
1070
1071 uint32_t lifetime = em->lifetime;
1072 uint32_t running_time = 0;
1076 - usec_t period = update_every * USEC_PER_SEC;
1073 pids_fd[EBPF_PIDS_SHM_IDX] = shm_maps[NETDATA_PID_SHM_TABLE].map_fd;
1074 + heartbeat_t hb;
1075 + heartbeat_init(&hb, update_every * USEC_PER_SEC);
1076 while (!ebpf_plugin_stop() && running_time < lifetime) {
1079 - (void)heartbeat_next(&hb, period);
1077 + (void)heartbeat_next(&hb);
1078 if (ebpf_plugin_stop() || ++counter != update_every)
1079 continue;
1080
@@ -1107,16 +1105,17 @@ static void shm_collector(ebpf_module_t *em)
1105 {
1106 int cgroups = em->cgroup_charts;
1107 int update_every = em->update_every;
1110 - heartbeat_t hb;
1111 - heartbeat_init(&hb);
1108 int counter = update_every - 1;
1109 int maps_per_core = em->maps_per_core;
1110 uint32_t running_time = 0;
1111 uint32_t lifetime = em->lifetime;
1112 netdata_idx_t *stats = em->hash_table_stats;
1113 memset(stats, 0, sizeof(em->hash_table_stats));
1114 + heartbeat_t hb;
1115 + heartbeat_init(&hb, USEC_PER_SEC);
1116 while (!ebpf_plugin_stop() && running_time < lifetime) {
1119 - (void)heartbeat_next(&hb, USEC_PER_SEC);
1117 + heartbeat_next(&hb);
1118 +
1119 if (ebpf_plugin_stop() || ++counter != update_every)
1120 continue;
1121
src/collectors/ebpf.plugin/ebpf_socket.c
+6 -9
@@ -1811,9 +1811,6 @@ void ebpf_socket_resume_apps_data()
1811 */
1812 void *ebpf_read_socket_thread(void *ptr)
1813 {
1814 - heartbeat_t hb;
1815 - heartbeat_init(&hb);
1816 -
1814 ebpf_module_t *em = (ebpf_module_t *)ptr;
1815
1816 ebpf_update_array_vectors(em);
@@ -1826,9 +1823,10 @@ void *ebpf_read_socket_thread(void *ptr)
1823
1824 uint32_t running_time = 0;
1825 uint32_t lifetime = em->lifetime;
1829 - usec_t period = update_every * USEC_PER_SEC;
1826 + heartbeat_t hb;
1827 + heartbeat_init(&hb, update_every * USEC_PER_SEC);
1828 while (!ebpf_plugin_stop() && running_time < lifetime) {
1831 - (void)heartbeat_next(&hb, period);
1829 + heartbeat_next(&hb);
1830 if (ebpf_plugin_stop() || ++counter != update_every)
1831 continue;
1832
@@ -2608,9 +2606,6 @@ static void ebpf_socket_send_cgroup_data(int update_every)
2606 */
2607 static void socket_collector(ebpf_module_t *em)
2608 {
2611 - heartbeat_t hb;
2612 - heartbeat_init(&hb);
2613 -
2609 int cgroups = em->cgroup_charts;
2610 if (cgroups)
2611 ebpf_socket_update_cgroup_algorithm();
@@ -2623,8 +2618,10 @@ static void socket_collector(ebpf_module_t *em)
2618 uint32_t lifetime = em->lifetime;
2619 netdata_idx_t *stats = em->hash_table_stats;
2620 memset(stats, 0, sizeof(em->hash_table_stats));
2621 + heartbeat_t hb;
2622 + heartbeat_init(&hb, USEC_PER_SEC);
2623 while (!ebpf_plugin_stop() && running_time < lifetime) {
2627 - (void)heartbeat_next(&hb, USEC_PER_SEC);
2624 + heartbeat_next(&hb);
2625 if (ebpf_plugin_stop() || ++counter != update_every)
2626 continue;
2627
src/collectors/ebpf.plugin/ebpf_softirq.c
+2 -2
@@ -209,7 +209,7 @@ static void softirq_collector(ebpf_module_t *em)
209
210 // loop and read from published data until ebpf plugin is closed.
211 heartbeat_t hb;
212 - heartbeat_init(&hb);
212 + heartbeat_init(&hb, USEC_PER_SEC);
213 int update_every = em->update_every;
214 int counter = update_every - 1;
215 int maps_per_core = em->maps_per_core;
@@ -217,7 +217,7 @@ static void softirq_collector(ebpf_module_t *em)
217 uint32_t running_time = 0;
218 uint32_t lifetime = em->lifetime;
219 while (!ebpf_plugin_stop() && running_time < lifetime) {
220 - (void)heartbeat_next(&hb, USEC_PER_SEC);
220 + heartbeat_next(&hb);
221 if (ebpf_plugin_stop() || ++counter != update_every)
222 continue;
223
src/collectors/ebpf.plugin/ebpf_swap.c
+7 -8
@@ -592,9 +592,6 @@ end_swap_loop:
592 */
593 void *ebpf_read_swap_thread(void *ptr)
594 {
595 - heartbeat_t hb;
596 - heartbeat_init(&hb);
597 -
595 ebpf_module_t *em = (ebpf_module_t *)ptr;
596
597 int maps_per_core = em->maps_per_core;
@@ -607,11 +604,12 @@ void *ebpf_read_swap_thread(void *ptr)
604
605 uint32_t lifetime = em->lifetime;
606 uint32_t running_time = 0;
610 - usec_t period = update_every * USEC_PER_SEC;
607 pids_fd[EBPF_PIDS_SWAP_IDX] = swap_maps[NETDATA_PID_SWAP_TABLE].map_fd;
608
609 + heartbeat_t hb;
610 + heartbeat_init(&hb, update_every * USEC_PER_SEC);
611 while (!ebpf_plugin_stop() && running_time < lifetime) {
614 - (void)heartbeat_next(&hb, period);
612 + heartbeat_next(&hb);
613 if (ebpf_plugin_stop() || ++counter != update_every)
614 continue;
615
@@ -924,16 +922,17 @@ static void swap_collector(ebpf_module_t *em)
922 {
923 int cgroup = em->cgroup_charts;
924 int update_every = em->update_every;
927 - heartbeat_t hb;
928 - heartbeat_init(&hb);
925 int counter = update_every - 1;
926 int maps_per_core = em->maps_per_core;
927 uint32_t running_time = 0;
928 uint32_t lifetime = em->lifetime;
929 netdata_idx_t *stats = em->hash_table_stats;
930 memset(stats, 0, sizeof(em->hash_table_stats));
931 +
932 + heartbeat_t hb;
933 + heartbeat_init(&hb, USEC_PER_SEC);
934 while (!ebpf_plugin_stop() && running_time < lifetime) {
936 - (void)heartbeat_next(&hb, USEC_PER_SEC);
935 + (void)heartbeat_next(&hb);
936 if (ebpf_plugin_stop() || ++counter != update_every)
937 continue;
938
src/collectors/ebpf.plugin/ebpf_sync.c
+3 -3
@@ -554,15 +554,15 @@ static void sync_send_data()
554 */
555 static void sync_collector(ebpf_module_t *em)
556 {
557 - heartbeat_t hb;
558 - heartbeat_init(&hb);
557 int update_every = em->update_every;
558 int counter = update_every - 1;
559 int maps_per_core = em->maps_per_core;
560 uint32_t running_time = 0;
561 uint32_t lifetime = em->lifetime;
562 + heartbeat_t hb;
563 + heartbeat_init(&hb, USEC_PER_SEC);
564 while (!ebpf_plugin_stop() && running_time < lifetime) {
565 - (void)heartbeat_next(&hb, USEC_PER_SEC);
565 + heartbeat_next(&hb);
566 if (ebpf_plugin_stop() || ++counter != update_every)
567 continue;
568
src/collectors/ebpf.plugin/ebpf_vfs.c
+6 -8
@@ -2060,9 +2060,6 @@ void ebpf_vfs_resume_apps_data() {
2060 */
2061 void *ebpf_read_vfs_thread(void *ptr)
2062 {
2063 - heartbeat_t hb;
2064 - heartbeat_init(&hb);
2065 -
2063 ebpf_module_t *em = (ebpf_module_t *)ptr;
2064
2065 int maps_per_core = em->maps_per_core;
@@ -2075,11 +2072,12 @@ void *ebpf_read_vfs_thread(void *ptr)
2072
2073 uint32_t lifetime = em->lifetime;
2074 uint32_t running_time = 0;
2078 - usec_t period = update_every * USEC_PER_SEC;
2075 uint32_t max_period = EBPF_CLEANUP_FACTOR;
2076 pids_fd[EBPF_PIDS_VFS_IDX] = vfs_maps[NETDATA_VFS_PID].map_fd;
2077 + heartbeat_t hb;
2078 + heartbeat_init(&hb, update_every * USEC_PER_SEC);
2079 while (!ebpf_plugin_stop() && running_time < lifetime) {
2082 - (void)heartbeat_next(&hb, period);
2080 + heartbeat_next(&hb);
2081 if (ebpf_plugin_stop() || ++counter != update_every)
2082 continue;
2083
@@ -2112,8 +2110,6 @@ void *ebpf_read_vfs_thread(void *ptr)
2110 static void vfs_collector(ebpf_module_t *em)
2111 {
2112 int cgroups = em->cgroup_charts;
2115 - heartbeat_t hb;
2116 - heartbeat_init(&hb);
2113 int update_every = em->update_every;
2114 int counter = update_every - 1;
2115 int maps_per_core = em->maps_per_core;
@@ -2121,8 +2117,10 @@ static void vfs_collector(ebpf_module_t *em)
2117 uint32_t lifetime = em->lifetime;
2118 netdata_idx_t *stats = em->hash_table_stats;
2119 memset(stats, 0, sizeof(em->hash_table_stats));
2120 + heartbeat_t hb;
2121 + heartbeat_init(&hb, USEC_PER_SEC);
2122 while (!ebpf_plugin_stop() && running_time < lifetime) {
2125 - (void)heartbeat_next(&hb, USEC_PER_SEC);
2123 + heartbeat_next(&hb);
2124 if (ebpf_plugin_stop() || ++counter != update_every)
2125 continue;
2126
src/collectors/freebsd.plugin/plugin_freebsd.c
+2 -3
@@ -105,14 +105,13 @@ void *freebsd_main(void *ptr)
105 worker_register_job_name(i, freebsd_modules[i].dim);
106 }
107
108 - usec_t step = localhost->rrd_update_every * USEC_PER_SEC;
108 heartbeat_t hb;
110 - heartbeat_init(&hb);
109 + heartbeat_init(&hb, localhost->rrd_update_every * USEC_PER_SEC);
110
111 while(service_running(SERVICE_COLLECTORS)) {
112 worker_is_idle();
113
115 - usec_t hb_dt = heartbeat_next(&hb, step);
114 + usec_t hb_dt = heartbeat_next(&hb);
115
116 if (!service_running(SERVICE_COLLECTORS))
117 break;
src/collectors/freeipmi.plugin/freeipmi_plugin.c
+4 -8
@@ -1240,9 +1240,9 @@ void *netdata_ipmi_collection_thread(void *ptr) {
1240 usec_t step = t->freq_s * USEC_PER_SEC;
1241
1242 heartbeat_t hb;
1243 - heartbeat_init(&hb);
1243 + heartbeat_init(&hb, step);
1244 while(++iteration) {
1245 - heartbeat_next(&hb, step);
1245 + heartbeat_next(&hb);
1246
1247 if(t->debug)
1248 fprintf(stderr, "%s: calling netdata_ipmi_collect_data() for %s\n",
@@ -1647,7 +1647,6 @@ static void plugin_exit(int code) {
1647 }
1648
1649 int main (int argc, char **argv) {
1650 - clocks_init();
1650 nd_log_initialize_for_external_plugins("freeipmi.plugin");
1651 netdata_threads_init_for_external_plugins(0); // set the default threads stack size here
1652
@@ -2000,15 +1999,13 @@ int main (int argc, char **argv) {
1999 time_t started_t = now_monotonic_sec();
2000
2001 size_t iteration = 0;
2003 - usec_t step = 100 * USEC_PER_MS;
2002 bool global_chart_created = false;
2003 bool tty = isatty(fileno(stdout)) == 1;
2004
2005 heartbeat_t hb;
2008 - heartbeat_init(&hb);
2009 -
2006 + heartbeat_init(&hb, update_every * USEC_PER_SEC);
2007 for(iteration = 0; 1 ; iteration++) {
2011 - usec_t dt = heartbeat_next(&hb, step);
2008 + usec_t dt = heartbeat_next(&hb);
2009
2010 if (!tty) {
2011 netdata_mutex_lock(&stdout_mutex);
@@ -2027,7 +2024,6 @@ int main (int argc, char **argv) {
2024
2025 switch(state.sensors.status) {
2026 case ICS_RUNNING:
2030 - step = update_every * USEC_PER_SEC;
2027 if(state.sensors.last_iteration_ut < now_monotonic_usec() - IPMI_RESTART_IF_SENSORS_DONT_ITERATE_EVERY_SECONDS * USEC_PER_SEC) {
2028 collector_error("%s(): sensors have not be collected for %zu seconds. Exiting to restart.",
2029 __FUNCTION__, (size_t)((now_monotonic_usec() - state.sensors.last_iteration_ut) / USEC_PER_SEC));
src/collectors/macos.plugin/plugin_macos.c
+2 -3
@@ -54,13 +54,12 @@ void *macos_main(void *ptr)
54 worker_register_job_name(i, macos_modules[i].dim);
55 }
56
57 - usec_t step = localhost->rrd_update_every * USEC_PER_SEC;
57 heartbeat_t hb;
59 - heartbeat_init(&hb);
58 + heartbeat_init(&hb, localhost->rrd_update_every * USEC_PER_SEC);
59
60 while(service_running(SERVICE_COLLECTORS)) {
61 worker_is_idle();
63 - usec_t hb_dt = heartbeat_next(&hb, step);
62 + usec_t hb_dt = heartbeat_next(&hb);
63
64 if (!service_running(SERVICE_COLLECTORS))
65 break;
src/collectors/network-viewer.plugin/network-viewer.c
+2 -4
@@ -958,7 +958,6 @@ close_and_send:
958 // main
959
960 int main(int argc __maybe_unused, char **argv __maybe_unused) {
961 - clocks_init();
961 nd_thread_tag_set("NETWORK-VIEWER");
962 nd_log_initialize_for_external_plugins("network-viewer.plugin");
963
@@ -1016,15 +1015,14 @@ int main(int argc __maybe_unused, char **argv __maybe_unused) {
1015
1016 // ----------------------------------------------------------------------------------------------------------------
1017
1019 - usec_t step_ut = 100 * USEC_PER_MS;
1018 usec_t send_newline_ut = 0;
1019 bool tty = isatty(fileno(stdout)) == 1;
1020
1021 heartbeat_t hb;
1024 - heartbeat_init(&hb);
1022 + heartbeat_init(&hb, USEC_PER_SEC);
1023 while(!plugin_should_exit) {
1024
1027 - usec_t dt_ut = heartbeat_next(&hb, step_ut);
1025 + usec_t dt_ut = heartbeat_next(&hb);
1026 send_newline_ut += dt_ut;
1027
1028 if(!tty && send_newline_ut > USEC_PER_SEC) {
src/collectors/nfacct.plugin/plugin_nfacct.c
+2 -4
@@ -747,7 +747,6 @@ void nfacct_signals()
747 }
748
749 int main(int argc, char **argv) {
750 - clocks_init();
750 nd_log_initialize_for_external_plugins("nfacct.plugin");
751
752 // ------------------------------------------------------------------------
@@ -832,12 +831,11 @@ int main(int argc, char **argv) {
831 time_t started_t = now_monotonic_sec();
832
833 size_t iteration;
835 - usec_t step = netdata_update_every * USEC_PER_SEC;
834
835 heartbeat_t hb;
838 - heartbeat_init(&hb);
836 + heartbeat_init(&hb, netdata_update_every * USEC_PER_SEC);
837 for(iteration = 0; 1; iteration++) {
840 - usec_t dt = heartbeat_next(&hb, step);
838 + usec_t dt = heartbeat_next(&hb);
839
840 if(unlikely(netdata_exit)) break;
841
src/collectors/perf.plugin/perf_plugin.c
+2 -4
@@ -1287,7 +1287,6 @@ void parse_command_line(int argc, char **argv) {
1287 }
1288
1289 int main(int argc, char **argv) {
1290 - clocks_init();
1290 nd_log_initialize_for_external_plugins("perf.plugin");
1291
1292 parse_command_line(argc, argv);
@@ -1318,14 +1317,13 @@ int main(int argc, char **argv) {
1317 time_t started_t = now_monotonic_sec();
1318
1319 size_t iteration;
1321 - usec_t step = update_every * USEC_PER_SEC;
1320
1321 int perf = 1;
1322
1323 heartbeat_t hb;
1326 - heartbeat_init(&hb);
1324 + heartbeat_init(&hb, update_every * USEC_PER_SEC);
1325 for(iteration = 0; 1; iteration++) {
1328 - usec_t dt = heartbeat_next(&hb, step);
1326 + usec_t dt = heartbeat_next(&hb);
1327
1328 if (unlikely(netdata_exit))
1329 break;
src/collectors/proc.plugin/plugin_proc.c
+2 -3
@@ -226,9 +226,8 @@ void *proc_main(void *ptr)
226 worker_register_job_name(i, proc_modules[i].dim);
227 }
228
229 - usec_t step = localhost->rrd_update_every * USEC_PER_SEC;
229 heartbeat_t hb;
231 - heartbeat_init(&hb);
230 + heartbeat_init(&hb, localhost->rrd_update_every * USEC_PER_SEC);
231
232 inside_lxc_container = is_lxcfs_proc_mounted();
233 is_mem_swap_enabled = is_swap_enabled();
@@ -245,7 +244,7 @@ void *proc_main(void *ptr)
244
245 while(service_running(SERVICE_COLLECTORS)) {
246 worker_is_idle();
248 - usec_t hb_dt = heartbeat_next(&hb, step);
247 + usec_t hb_dt = heartbeat_next(&hb);
248
249 if(unlikely(!service_running(SERVICE_COLLECTORS)))
250 break;
src/collectors/proc.plugin/proc_net_dev.c
+2 -3
@@ -1706,13 +1706,12 @@ void *netdev_main(void *ptr_is_null __maybe_unused)
1706 "top", HTTP_ACCESS_ANONYMOUS_DATA,
1707 netdev_function_net_interfaces);
1708
1709 - usec_t step = localhost->rrd_update_every * USEC_PER_SEC;
1709 heartbeat_t hb;
1711 - heartbeat_init(&hb);
1710 + heartbeat_init(&hb, localhost->rrd_update_every * USEC_PER_SEC);
1711
1712 while (service_running(SERVICE_COLLECTORS)) {
1713 worker_is_idle();
1715 - usec_t hb_dt = heartbeat_next(&hb, step);
1714 + usec_t hb_dt = heartbeat_next(&hb);
1715
1716 if (unlikely(!service_running(SERVICE_COLLECTORS)))
1717 break;
src/collectors/profile.plugin/plugin_profile.cc
+2 -2
@@ -117,7 +117,7 @@ public:
117 worker_register_job_custom_metric(WORKER_JOB_METRIC_POINTS_BACKFILLED, "points backfilled", "points", WORKER_METRIC_ABSOLUTE);
118
119 heartbeat_t HB;
120 - heartbeat_init(&HB);
120 + heartbeat_init(&HB, UpdateEvery * USEC_PER_SEC);
121
122 worker_is_busy(WORKER_JOB_CREATE_CHARTS);
123 create();
@@ -157,7 +157,7 @@ public:
157
158 if (CollectionTV.tv_sec >= NowTV.tv_sec) {
159 worker_is_idle();
160 - heartbeat_next(&HB, UpdateEvery * USEC_PER_SEC);
160 + heartbeat_next(&HB);
161 }
162 }
163 }
src/collectors/slabinfo.plugin/slabinfo.c
-1
@@ -345,7 +345,6 @@ void usage(void) {
345 }
346
347 int main(int argc, char **argv) {
348 - clocks_init();
348 nd_log_initialize_for_external_plugins("slabinfo.plugin");
349
350 program_name = argv[0];
src/collectors/statsd.plugin/statsd.c
+2 -3
@@ -2818,12 +2818,11 @@ void *statsd_main(void *ptr) {
2818 // ----------------------------------------------------------------------------------------------------------------
2819 // statsd thread to turn metrics into charts
2820
2821 - usec_t step = statsd.update_every * USEC_PER_SEC;
2821 heartbeat_t hb;
2823 - heartbeat_init(&hb);
2822 + heartbeat_init(&hb, statsd.update_every * USEC_PER_SEC);
2823 while(service_running(SERVICE_COLLECTORS)) {
2824 worker_is_idle();
2826 - heartbeat_next(&hb, step);
2825 + heartbeat_next(&hb);
2826
2827 worker_is_busy(WORKER_STATSD_FLUSH_GAUGES);
2828 statsd_flush_index_metrics(&statsd.gauges, statsd_flush_gauge);
src/collectors/systemd-journal.plugin/systemd-main.c
+2 -4
@@ -18,7 +18,6 @@ static bool journal_data_directories_exist() {
18 }
19
20 int main(int argc __maybe_unused, char **argv __maybe_unused) {
21 - clocks_init();
21 nd_thread_tag_set("sd-jrnl.plugin");
22 nd_log_initialize_for_external_plugins("systemd-journal.plugin");
23
@@ -114,13 +113,12 @@ int main(int argc __maybe_unused, char **argv __maybe_unused) {
113
114 // ------------------------------------------------------------------------
115
117 - const usec_t step_ut = 100 * USEC_PER_MS;
116 usec_t send_newline_ut = 0;
117 usec_t since_last_scan_ut = SYSTEMD_JOURNAL_ALL_FILES_SCAN_EVERY_USEC * 2; // something big to trigger scanning at start
118 const bool tty = isatty(fileno(stdout)) == 1;
119
120 heartbeat_t hb;
123 - heartbeat_init(&hb);
121 + heartbeat_init(&hb, USEC_PER_SEC);
122 while(!plugin_should_exit) {
123
124 if(since_last_scan_ut > SYSTEMD_JOURNAL_ALL_FILES_SCAN_EVERY_USEC) {
@@ -128,7 +126,7 @@ int main(int argc __maybe_unused, char **argv __maybe_unused) {
126 since_last_scan_ut = 0;
127 }
128
131 - usec_t dt_ut = heartbeat_next(&hb, step_ut);
129 + usec_t dt_ut = heartbeat_next(&hb);
130 since_last_scan_ut += dt_ut;
131 send_newline_ut += dt_ut;
132
src/collectors/timex.plugin/plugin_timex.c
+2 -2
@@ -67,10 +67,10 @@ void *timex_main(void *ptr)
67 usec_t step = update_every * USEC_PER_SEC;
68 usec_t real_step = USEC_PER_SEC;
69 heartbeat_t hb;
70 - heartbeat_init(&hb);
70 + heartbeat_init(&hb, USEC_PER_SEC);
71 while (service_running(SERVICE_COLLECTORS)) {
72 worker_is_idle();
73 - heartbeat_next(&hb, USEC_PER_SEC);
73 + heartbeat_next(&hb);
74
75 if (real_step < step) {
76 real_step += USEC_PER_SEC;
src/collectors/windows-events.plugin/windows-events.c
+2 -4
@@ -1290,7 +1290,6 @@ void function_windows_events(const char *transaction, char *function, usec_t *st
1290 }
1291
1292 int main(int argc __maybe_unused, char **argv __maybe_unused) {
1293 - clocks_init();
1293 nd_thread_tag_set("wevt.plugin");
1294 nd_log_initialize_for_external_plugins("windows-events.plugin");
1295
@@ -1369,14 +1368,13 @@ int main(int argc __maybe_unused, char **argv __maybe_unused) {
1368
1369 // ------------------------------------------------------------------------
1370
1372 - const usec_t step_ut = 100 * USEC_PER_MS;
1371 usec_t send_newline_ut = 0;
1372 usec_t since_last_scan_ut = WINDOWS_EVENTS_SCAN_EVERY_USEC * 2; // something big to trigger scanning at start
1373 usec_t since_last_providers_release_ut = 0;
1374 const bool tty = isatty(fileno(stdout)) == 1;
1375
1376 heartbeat_t hb;
1379 - heartbeat_init(&hb);
1377 + heartbeat_init(&hb, USEC_PER_SEC);
1378 while(!plugin_should_exit) {
1379
1380 if(since_last_scan_ut > WINDOWS_EVENTS_SCAN_EVERY_USEC) {
@@ -1389,7 +1387,7 @@ int main(int argc __maybe_unused, char **argv __maybe_unused) {
1387 since_last_providers_release_ut = 0;
1388 }
1389
1392 - usec_t dt_ut = heartbeat_next(&hb, step_ut);
1390 + usec_t dt_ut = heartbeat_next(&hb);
1391 since_last_providers_release_ut += dt_ut;
1392 since_last_scan_ut += dt_ut;
1393 send_newline_ut += dt_ut;
src/collectors/windows.plugin/windows_plugin.c
+2 -3
@@ -79,9 +79,8 @@ void *win_plugin_main(void *ptr) {
79 worker_register_job_name(i, win_modules[i].dim);
80 }
81
82 - usec_t step = localhost->rrd_update_every * USEC_PER_SEC;
82 heartbeat_t hb;
84 - heartbeat_init(&hb);
83 + heartbeat_init(&hb, localhost->rrd_update_every * USEC_PER_SEC);
84
85 #define LGS_MODULE_ID 0
86
@@ -93,7 +92,7 @@ void *win_plugin_main(void *ptr) {
92
93 while(service_running(SERVICE_COLLECTORS)) {
94 worker_is_idle();
96 - usec_t hb_dt = heartbeat_next(&hb, step);
95 + usec_t hb_dt = heartbeat_next(&hb);
96
97 if(unlikely(!service_running(SERVICE_COLLECTORS)))
98 break;
src/collectors/xenstat.plugin/xenstat_plugin.c
+2 -5
@@ -920,8 +920,6 @@ static void xenstat_send_domain_metrics() {
920 }
921
922 int main(int argc, char **argv) {
923 - clocks_init();
924 -
923 // ------------------------------------------------------------------------
924 // initialization of netdata plugin
925
@@ -1022,12 +1020,11 @@ int main(int argc, char **argv) {
1020 time_t started_t = now_monotonic_sec();
1021
1022 size_t iteration;
1025 - usec_t step = netdata_update_every * USEC_PER_SEC;
1023
1024 heartbeat_t hb;
1028 - heartbeat_init(&hb);
1025 + heartbeat_init(&hb, netdata_update_every * USEC_PER_SEC);
1026 for(iteration = 0; 1; iteration++) {
1030 - usec_t dt = heartbeat_next(&hb, step);
1027 + usec_t dt = heartbeat_next(&hb);
1028
1029 if(unlikely(netdata_exit)) break;
1030
src/daemon/analytics.c
+5 -6
@@ -569,14 +569,13 @@ void *analytics_main(void *ptr)
569 CLEANUP_FUNCTION_REGISTER(analytics_main_cleanup) cleanup_ptr = ptr;
570 unsigned int sec = 0;
571 heartbeat_t hb;
572 - heartbeat_init(&hb);
573 - usec_t step_ut = USEC_PER_SEC;
572 + heartbeat_init(&hb, USEC_PER_SEC);
573
574 netdata_log_debug(D_ANALYTICS, "Analytics thread starts");
575
577 - //first delay after agent start
576 + // first delay after agent start
577 while (service_running(SERVICE_ANALYTICS) && likely(sec <= ANALYTICS_INIT_SLEEP_SEC)) {
579 - heartbeat_next(&hb, step_ut);
578 + heartbeat_next(&hb);
579 sec++;
580 }
581
@@ -592,8 +591,8 @@ void *analytics_main(void *ptr)
591
592 sec = 0;
593 while (1) {
595 - heartbeat_next(&hb, step_ut * 2);
596 - sec += 2;
594 + heartbeat_next(&hb);
595 + sec++;
596
597 if (unlikely(!service_running(SERVICE_ANALYTICS)))
598 break;
src/daemon/global_statistics.c
+4 -4
@@ -4229,7 +4229,7 @@ void *global_statistics_main(void *ptr)
4229
4230 usec_t step = update_every * USEC_PER_SEC;
4231 heartbeat_t hb;
4232 - heartbeat_init(&hb);
4232 + heartbeat_init(&hb, USEC_PER_SEC);
4233 usec_t real_step = USEC_PER_SEC;
4234
4235 // keep the randomness at zero
@@ -4238,7 +4238,7 @@ void *global_statistics_main(void *ptr)
4238
4239 while (service_running(SERVICE_COLLECTORS)) {
4240 worker_is_idle();
4241 - heartbeat_next(&hb, USEC_PER_SEC);
4241 + heartbeat_next(&hb);
4242 if (real_step < step) {
4243 real_step += USEC_PER_SEC;
4244 continue;
@@ -4287,12 +4287,12 @@ void *global_statistics_extended_main(void *ptr)
4287
4288 usec_t step = update_every * USEC_PER_SEC;
4289 heartbeat_t hb;
4290 - heartbeat_init(&hb);
4290 + heartbeat_init(&hb, USEC_PER_SEC);
4291 usec_t real_step = USEC_PER_SEC;
4292
4293 while (service_running(SERVICE_COLLECTORS)) {
4294 worker_is_idle();
4295 - heartbeat_next(&hb, USEC_PER_SEC);
4295 + heartbeat_next(&hb);
4296 if (real_step < step) {
4297 real_step += USEC_PER_SEC;
4298 continue;
src/daemon/main.c
-2
@@ -1357,7 +1357,6 @@ static void get_netdata_configured_variables()
1357 // --------------------------------------------------------------------
1358 // get various system parameters
1359
1360 - os_get_system_HZ();
1360 os_get_system_cpus_uncached();
1361 os_get_system_pid_max();
1362
@@ -1510,7 +1509,6 @@ int unittest_prepare_rrd(const char **user) {
1509 }
1510
1511 int netdata_main(int argc, char **argv) {
1513 - clocks_init();
1512 string_init();
1513 analytics_init();
1514
src/daemon/service.c
+2 -2
@@ -297,7 +297,7 @@ void *service_main(void *ptr)
297 CLEANUP_FUNCTION_REGISTER(service_main_cleanup) cleanup_ptr = ptr;
298
299 heartbeat_t hb;
300 - heartbeat_init(&hb);
300 + heartbeat_init(&hb, USEC_PER_SEC);
301 usec_t step = USEC_PER_SEC * SERVICE_HEARTBEAT;
302 usec_t real_step = USEC_PER_SEC;
303
@@ -305,7 +305,7 @@ void *service_main(void *ptr)
305
306 while (service_running(SERVICE_MAINTENANCE)) {
307 worker_is_idle();
308 - heartbeat_next(&hb, USEC_PER_SEC);
308 + heartbeat_next(&hb);
309 if (real_step < step) {
310 real_step += USEC_PER_SEC;
311 continue;
src/database/contexts/worker.c
+2 -3
@@ -1099,12 +1099,11 @@ void *rrdcontext_main(void *ptr) {
1099 worker_register_job_custom_metric(WORKER_JOB_PP_QUEUE_SIZE, "post processing queue size", "contexts", WORKER_METRIC_ABSOLUTE);
1100
1101 heartbeat_t hb;
1102 - heartbeat_init(&hb);
1103 - usec_t step = RRDCONTEXT_WORKER_THREAD_HEARTBEAT_USEC;
1102 + heartbeat_init(&hb, RRDCONTEXT_WORKER_THREAD_HEARTBEAT_USEC);
1103
1104 while (service_running(SERVICE_CONTEXT)) {
1105 worker_is_idle();
1107 - heartbeat_next(&hb, step);
1106 + heartbeat_next(&hb);
1107
1108 if(unlikely(!service_running(SERVICE_CONTEXT))) break;
1109
src/database/engine/cache.c
+6 -6
@@ -2366,7 +2366,7 @@ void *unittest_stress_test_collector(void *ptr) {
2366 time_t start_time_t = pgc_uts.first_time_t + 1;
2367
2368 heartbeat_t hb;
2369 - heartbeat_init(&hb);
2369 + heartbeat_init(&hb, pgc_uts.time_per_collection_ut);
2370
2371 while(!__atomic_load_n(&pgc_uts.stop, __ATOMIC_RELAXED)) {
2372 // netdata_log_info("COLLECTOR %zu: collecting metrics %zu to %zu, from %ld to %lu", id, metric_start, metric_end, start_time_t, start_time_t + pgc_uts.points_per_page);
@@ -2393,7 +2393,7 @@ void *unittest_stress_test_collector(void *ptr) {
2393
2394 time_t end_time_t = start_time_t + (time_t)pgc_uts.points_per_page;
2395 while(++start_time_t <= end_time_t && !__atomic_load_n(&pgc_uts.stop, __ATOMIC_RELAXED)) {
2396 - heartbeat_next(&hb, pgc_uts.time_per_collection_ut);
2396 + heartbeat_next(&hb);
2397
2398 for (size_t i = metric_start; i < metric_end; i++) {
2399 if(pgc_uts.metrics[i])
@@ -2480,9 +2480,9 @@ void *unittest_stress_test_queries(void *ptr) {
2480
2481 void *unittest_stress_test_service(void *ptr) {
2482 heartbeat_t hb;
2483 - heartbeat_init(&hb);
2483 + heartbeat_init(&hb, USEC_PER_SEC);
2484 while(!__atomic_load_n(&pgc_uts.stop, __ATOMIC_RELAXED)) {
2485 - heartbeat_next(&hb, 1 * USEC_PER_SEC);
2485 + heartbeat_next(&hb);
2486
2487 pgc_flush_pages(pgc_uts.cache, 1000);
2488 pgc_evict_pages(pgc_uts.cache, 0, 0);
@@ -2545,7 +2545,7 @@ void unittest_stress_test(void) {
2545 }
2546
2547 heartbeat_t hb;
2548 - heartbeat_init(&hb);
2548 + heartbeat_init(&hb, USEC_PER_SEC);
2549
2550 struct {
2551 size_t entries;
@@ -2578,7 +2578,7 @@ void unittest_stress_test(void) {
2578 } stats = {}, old_stats = {};
2579
2580 for(int i = 0; i < 86400 ;i++) {
2581 - heartbeat_next(&hb, 1 * USEC_PER_SEC);
2581 + heartbeat_next(&hb);
2582
2583 old_stats = stats;
2584 stats.entries = __atomic_load_n(&pgc_uts.cache->stats.entries, __ATOMIC_RELAXED);
src/exporting/exporting_engine.c
+2 -3
@@ -195,12 +195,11 @@ void *exporting_main(void *ptr)
195 RRDDIM *rd_main_system = NULL;
196 create_main_rusage_chart(&st_main_rusage, &rd_main_user, &rd_main_system);
197
198 - usec_t step_ut = localhost->rrd_update_every * USEC_PER_SEC;
198 heartbeat_t hb;
200 - heartbeat_init(&hb);
199 + heartbeat_init(&hb, localhost->rrd_update_every * USEC_PER_SEC);
200
201 while (service_running(SERVICE_EXPORTERS)) {
203 - heartbeat_next(&hb, step_ut);
202 + heartbeat_next(&hb);
203 engine->now = now_realtime_sec();
204
205 if (mark_scheduled_instances(engine))
src/libnetdata/clocks/clocks.c
+72 -38
@@ -78,7 +78,9 @@ static usec_t get_clock_resolution(clockid_t clock) {
78
79 // perform any initializations required for clocks
80
81 -void clocks_init(void) {
81 +static __attribute__((constructor)) void clocks_init(void) {
82 + os_get_system_HZ();
83 +
84 // monotonic raw has to be tested before boottime
85 test_clock_monotonic_raw();
86
@@ -87,6 +89,18 @@ void clocks_init(void) {
89
90 clock_monotonic_resolution = get_clock_resolution(clock_monotonic_to_use);
91 clock_realtime_resolution = get_clock_resolution(CLOCK_REALTIME);
92 +
93 +#if defined(OS_WINDOWS)
94 + timeBeginPeriod(1);
95 + clock_monotonic_resolution = 1 * USEC_PER_MS;
96 + clock_realtime_resolution = 1 * USEC_PER_MS;
97 +#endif
98 +}
99 +
100 +static __attribute__((destructor)) void clocks_fin(void) {
101 +#if defined(OS_WINDOWS)
102 + timeEndPeriod(1);
103 +#endif
104 }
105
106 inline time_t now_sec(clockid_t clk_id) {
@@ -246,13 +260,15 @@ void sleep_to_absolute_time(usec_t usec) {
260 }
261 #endif
262
249 -#define HEARTBEAT_ALIGNMENT_STATISTICS_SIZE 10
250 -netdata_mutex_t heartbeat_alignment_mutex = NETDATA_MUTEX_INITIALIZER;
263 +#define HEARTBEAT_ALIGNMENT_STATISTICS_SIZE 20
264 +static SPINLOCK heartbeat_alignment_spinlock = NETDATA_SPINLOCK_INITIALIZER;
265 static size_t heartbeat_alignment_id = 0;
266
267 struct heartbeat_thread_statistics {
268 + pid_t tid;
269 size_t sequence;
270 usec_t dt;
271 + usec_t randomness;
272 };
273 static struct heartbeat_thread_statistics heartbeat_alignment_values[HEARTBEAT_ALIGNMENT_STATISTICS_SIZE] = { 0 };
274
@@ -290,19 +306,52 @@ void heartbeat_statistics(usec_t *min_ptr, usec_t *max_ptr, usec_t *average_ptr,
306 memcpy(old, current, sizeof(struct heartbeat_thread_statistics) * HEARTBEAT_ALIGNMENT_STATISTICS_SIZE);
307 }
308
293 -inline void heartbeat_init(heartbeat_t *hb) {
294 - hb->realtime = 0ULL;
295 - hb->randomness = (usec_t)250 * USEC_PER_MS + ((usec_t)(now_realtime_usec() * clock_realtime_resolution) % (250 * USEC_PER_MS));
296 - hb->randomness -= (hb->randomness % clock_realtime_resolution);
309 +static usec_t heartbeat_randomness(usec_t step __maybe_unused, size_t statistics_id) {
310 + struct {
311 + pid_t pid;
312 + pid_t tid;
313 + usec_t now_ut;
314 + size_t statistics_id;
315 + char tag[ND_THREAD_TAG_MAX + 1];
316 + } key = {
317 + .pid = getpid(),
318 + .tid = os_gettid(),
319 + .now_ut = now_realtime_usec(),
320 + .statistics_id = statistics_id,
321 + };
322 + strncpyz(key.tag, nd_thread_tag(), sizeof(key.tag) - 1);
323 + XXH64_hash_t hash = XXH3_64bits(&key, sizeof(key));
324 + usec_t offset_ut = (100 * USEC_PER_MS) + (hash % (400 * USEC_PER_MS));
325 +
326 + // Calculate the scheduler tick interval in microseconds
327 + usec_t scheduler_step_ut = USEC_PER_SEC / (usec_t)system_hz;
328 + if(scheduler_step_ut > 10 * USEC_PER_MS)
329 + scheduler_step_ut = 10 * USEC_PER_MS;
330 +
331 + // if the offset is close to the scheduler tick, move it away from it
332 + if(offset_ut % scheduler_step_ut < scheduler_step_ut / 4)
333 + offset_ut += scheduler_step_ut / 4;
334
298 - netdata_mutex_lock(&heartbeat_alignment_mutex);
335 + return offset_ut;
336 +}
337 +
338 +inline void heartbeat_init(heartbeat_t *hb, usec_t step) {
339 + if(!step) step = USEC_PER_SEC;
340 +
341 + spinlock_lock(&heartbeat_alignment_spinlock);
342 hb->statistics_id = heartbeat_alignment_id;
343 heartbeat_alignment_id++;
301 - netdata_mutex_unlock(&heartbeat_alignment_mutex);
344 + spinlock_unlock(&heartbeat_alignment_spinlock);
345 +
346 + hb->step = step;
347 + hb->realtime = 0ULL;
348 + hb->randomness = heartbeat_randomness(hb->step, hb->statistics_id);
349
350 if(hb->statistics_id < HEARTBEAT_ALIGNMENT_STATISTICS_SIZE) {
351 heartbeat_alignment_values[hb->statistics_id].dt = 0;
352 heartbeat_alignment_values[hb->statistics_id].sequence = 0;
353 + heartbeat_alignment_values[hb->statistics_id].randomness = hb->randomness;
354 + heartbeat_alignment_values[hb->statistics_id].tid = os_gettid();
355 }
356 }
357
@@ -310,17 +359,8 @@ inline void heartbeat_init(heartbeat_t *hb) {
359 // it waits using the monotonic clock
360 // it returns the dt using the realtime clock
361
313 -usec_t heartbeat_next(heartbeat_t *hb, usec_t tick) {
314 - if(unlikely(hb->randomness > tick / 2)) {
315 - // TODO: The heartbeat tick should be specified at the heartbeat_init() function
316 - usec_t tmp = (now_realtime_usec() * clock_realtime_resolution) % (tick / 2);
317 -
318 - nd_log_limit_static_global_var(erl, 10, 0);
319 - nd_log_limit(&erl, NDLS_DAEMON, NDLP_NOTICE,
320 - "heartbeat randomness of %"PRIu64" is too big for a tick of %"PRIu64" - setting it to %"PRIu64"",
321 - hb->randomness, tick, tmp);
322 - hb->randomness = tmp;
323 - }
362 +usec_t heartbeat_next(heartbeat_t *hb) {
363 + usec_t tick = hb->step;
364
365 usec_t dt;
366 usec_t now = now_realtime_usec();
@@ -331,10 +371,13 @@ usec_t heartbeat_next(heartbeat_t *hb, usec_t tick) {
371 next = next - (next % clock_realtime_resolution) + clock_realtime_resolution;
372
373 // sleep_usec() has a loop to guarantee we will sleep for at least the requested time.
334 - // According the specs, when we sleep for a relative time, clock adjustments should not affect the duration
335 - // we sleep.
374 + // According to the specs, when we sleep for a relative time, clock adjustments should
375 + // not affect the duration we sleep.
376 sleep_usec_with_now(next - now, now);
377 + spinlock_lock(&heartbeat_alignment_spinlock);
378 now = now_realtime_usec();
379 + spinlock_unlock(&heartbeat_alignment_spinlock);
380 +
381 dt = now - hb->realtime;
382
383 if(hb->statistics_id < HEARTBEAT_ALIGNMENT_STATISTICS_SIZE) {
@@ -368,22 +411,15 @@ usec_t heartbeat_next(heartbeat_t *hb, usec_t tick) {
411 return dt;
412 }
413
371 -#ifdef OS_WINDOWS
372 -
373 -#include "windows.h"
374 -
375 -void sleep_usec_with_now(usec_t usec, usec_t started_ut)
376 -{
414 +#if defined(OS_WINDOWS)
415 +void sleep_usec_with_now(usec_t usec, usec_t started_ut) {
416 if (!started_ut)
417 started_ut = now_realtime_usec();
418
419 usec_t end_ut = started_ut + usec;
420 usec_t remaining_ut = usec;
421
383 - timeBeginPeriod(1);
384 -
385 - while (remaining_ut >= 1000)
386 - {
422 + while (remaining_ut >= clock_realtime_resolution) {
423 DWORD sleep_ms = (DWORD) (remaining_ut / USEC_PER_MS);
424 Sleep(sleep_ms);
425
@@ -393,8 +429,6 @@ void sleep_usec_with_now(usec_t usec, usec_t started_ut)
429
430 remaining_ut = end_ut - now_ut;
431 }
396 -
397 - timeEndPeriod(1);
432 }
433 #else
434 void sleep_usec_with_now(usec_t usec, usec_t started_ut) {
@@ -406,7 +440,7 @@ void sleep_usec_with_now(usec_t usec, usec_t started_ut) {
440 };
441
442 // make sure errno is not EINTR
409 - errno = 0;
443 + errno_clear();
444
445 if(!started_ut)
446 started_ut = now_realtime_usec();
@@ -419,7 +453,7 @@ void sleep_usec_with_now(usec_t usec, usec_t started_ut) {
453 rem = (struct timespec){ 0, 0 };
454
455 // break an infinite loop
422 - errno = 0;
456 + errno_clear();
457
458 usec_t now_ut = now_realtime_usec();
459 if(now_ut >= end_ut)
@@ -429,8 +463,8 @@ void sleep_usec_with_now(usec_t usec, usec_t started_ut) {
463 usec_t check_ut = now_ut - started_ut;
464 if(remaining_ut > check_ut) {
465 req = (struct timespec){
432 - .tv_sec = (time_t) ( check_ut / USEC_PER_SEC),
433 - .tv_nsec = (suseconds_t) ((check_ut % USEC_PER_SEC) * NSEC_PER_USEC)
466 + .tv_sec = (time_t) ( check_ut / USEC_PER_SEC),
467 + .tv_nsec = (suseconds_t) ((check_ut % USEC_PER_SEC) * NSEC_PER_USEC)
468 };
469 }
470 }
src/libnetdata/clocks/clocks.h
+3 -4
@@ -26,6 +26,7 @@ typedef int64_t smsec_t;
26 typedef int64_t stime_t;
27
28 typedef struct heartbeat {
29 + usec_t step;
30 usec_t realtime;
31 usec_t randomness;
32 size_t statistics_id;
@@ -139,20 +140,18 @@ msec_t timeval_msec(struct timeval *tv);
140 usec_t dt_usec(struct timeval *now, struct timeval *old);
141 susec_t dt_usec_signed(struct timeval *now, struct timeval *old);
142
142 -void heartbeat_init(heartbeat_t *hb);
143 +void heartbeat_init(heartbeat_t *hb, usec_t step);
144
145 /* Sleeps until next multiple of tick using monotonic clock.
146 * Returns elapsed time in microseconds since previous heartbeat
147 */
147 -usec_t heartbeat_next(heartbeat_t *hb, usec_t tick);
148 +usec_t heartbeat_next(heartbeat_t *hb);
149
150 void heartbeat_statistics(usec_t *min_ptr, usec_t *max_ptr, usec_t *average_ptr, size_t *count_ptr);
151
152 void sleep_usec_with_now(usec_t usec, usec_t started_ut);
153 #define sleep_usec(usec) sleep_usec_with_now(usec, 0)
154
154 -void clocks_init(void);
155 -
155 // lower level functions - avoid using directly
156 time_t now_sec(clockid_t clk_id);
157 usec_t now_usec(clockid_t clk_id);
src/libnetdata/log/systemd-cat-native.c
-1
@@ -737,7 +737,6 @@ cleanup:
737 }
738
739 int main(int argc, char *argv[]) {
740 - clocks_init();
740 nd_log_initialize_for_external_plugins(argv[0]);
741
742 int timeout_ms = 0; // wait forever
src/libnetdata/os/os.c
+2 -1
@@ -6,12 +6,13 @@
6 // system functions
7 // to retrieve settings of the system
8
9 -unsigned int system_hz;
9 +unsigned int system_hz = 100;
10 void os_get_system_HZ(void) {
11 long ticks;
12
13 if ((ticks = sysconf(_SC_CLK_TCK)) == -1) {
14 netdata_log_error("Cannot get system clock ticks");
15 + ticks = 100;
16 }
17
18 system_hz = (unsigned int) ticks;
src/ml/ml.cc
+2 -2
@@ -1218,11 +1218,11 @@ ml_detect_main(void *arg)
1218 worker_register_job_name(WORKER_JOB_DETECTION_STATS, "training stats");
1219
1220 heartbeat_t hb;
1221 - heartbeat_init(&hb);
1221 + heartbeat_init(&hb, USEC_PER_SEC);
1222
1223 while (!Cfg.detection_stop && service_running(SERVICE_COLLECTORS)) {
1224 worker_is_idle();
1225 - heartbeat_next(&hb, USEC_PER_SEC);
1225 + heartbeat_next(&hb);
1226
1227 RRDHOST *rh;
1228 rrd_rdlock();