blob: 9b0d0338c1514a98ccae2da1266394955bd0bde9 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001Adjust the reaction to a polling interval timestamp that references
2to a past time.
3
4Past timestamps can happen when ntpd adjusts router's time after network
5connectivity is obtained after boot. Collectd shows warnings for each plugin
6as it tries to enter new values with the same timestamp as the previous one.
7
8This patch adjusts the next polling time to be now+2 seconds for the main
9loop and for the plugin-specific read loops. That avoids the warnings, but
10does not overreact in case there are shorter polling intervals or the time
11gets adjusted for other reasons.
12
13Additionally some debug statements are added, but they are visible only
14when --enable-debug configure option is used in Makefile.
15
16
17--- a/src/daemon/collectd.c
18+++ b/src/daemon/collectd.c
19@@ -274,20 +274,23 @@ static int do_loop(void) {
20 update_kstat();
21 #endif
22
23+ DEBUG("do_loop before plugin_read_all: now = %.3f", CDTIME_T_TO_DOUBLE(cdtime()));
24 /* Issue all plugins */
25 plugin_read_all();
26
27 cdtime_t now = cdtime();
28+ DEBUG("do_loop after plugin_read_all: now = %.3f, wait_until= %.3f", CDTIME_T_TO_DOUBLE(now), CDTIME_T_TO_DOUBLE(wait_until));
29 if (now >= wait_until) {
30- WARNING("Not sleeping because the next interval is "
31+ WARNING("Sleeping only 2s because the next interval is "
32 "%.3f seconds in the past!",
33 CDTIME_T_TO_DOUBLE(now - wait_until));
34- wait_until = now + interval;
35- continue;
36+ wait_until = now + DOUBLE_TO_CDTIME_T(2);
37+ DEBUG("do_loop: wait_until adjusted to now+2 = %.3f", CDTIME_T_TO_DOUBLE(wait_until));
38 }
39
40 struct timespec ts_wait = CDTIME_T_TO_TIMESPEC(wait_until - now);
41 wait_until = wait_until + interval;
42+ DEBUG("do_loop ends: wait_until set to %.3f", CDTIME_T_TO_DOUBLE(wait_until));
43
44 while ((loop == 0) && (nanosleep(&ts_wait, &ts_wait) != 0)) {
45 if (errno != EINTR) {
46--- a/src/daemon/plugin.c
47+++ b/src/daemon/plugin.c
48@@ -585,10 +585,11 @@ static void *plugin_read_thread(void __a
49
50 /* Check, if `rf_next_read' is in the past. */
51 if (rf->rf_next_read < now) {
52- /* `rf_next_read' is in the past. Insert `now'
53+ /* `rf_next_read' is in the past. Insert `now'+2s
54 * so this value doesn't trail off into the
55 * past too much. */
56- rf->rf_next_read = now;
57+ rf->rf_next_read = now + DOUBLE_TO_CDTIME_T(2);
58+ DEBUG("plugin_read_thread: Next read is in the past. Adjusted to now+2s");
59 }
60
61 DEBUG("plugin_read_thread: Next read of the `%s' plugin at %.3f.",