@cryptotaxi247 / netdata-1 / commits / 7521dfae3

journal startup (#16443)

* change the main loop of systemd-journal.plugin so that it registers functions before scanning files, pings netdata while scanning files and allows only 1 scan at a time * do not lock the output on startup * properly register functions on startup

Costa Tsaousis committed Nov 21, 2023 at 14:44 UTC 7521dfae3ac436f39bbbd57909c60809c909900a
3 files changed +46 -32
collectors/systemd-journal.plugin/systemd-internals.h
+7
@@ -108,4 +108,11 @@ void netdata_systemd_journal_transform_message_id(FACETS *facets __maybe_unused,
108 void function_systemd_units(const char *transaction, char *function, int timeout, bool *cancelled);
109 #endif
110
111 +static inline void send_newline_and_flush(void) {
112 + netdata_mutex_lock(&stdout_mutex);
113 + fprintf(stdout, "\n");
114 + fflush(stdout);
115 + netdata_mutex_unlock(&stdout_mutex);
116 +}
117 +
118 #endif //NETDATA_COLLECTORS_SYSTEMD_INTERNALS_H
collectors/systemd-journal.plugin/systemd-journal-files.c
+18 -13
@@ -383,6 +383,7 @@ void journal_directory_scan(const char *dirname, int depth, usec_t last_scan_ut)
383 .max_journal_vs_realtime_delta_ut = JOURNAL_VS_REALTIME_DELTA_DEFAULT_UT,
384 };
385 dictionary_set(journal_files_registry, absolute_path, &t, sizeof(t));
386 + send_newline_and_flush();
387 }
388 }
389 }
@@ -391,21 +392,27 @@ void journal_directory_scan(const char *dirname, int depth, usec_t last_scan_ut)
392 }
393
394 void journal_files_registry_update(void) {
394 - usec_t scan_ut = now_monotonic_usec();
395 + static SPINLOCK spinlock = NETDATA_SPINLOCK_INITIALIZER;
396
396 - for(unsigned i = 0; i < MAX_JOURNAL_DIRECTORIES ;i++) {
397 - if(!journal_directories[i].path)
398 - break;
397 + if(spinlock_trylock(&spinlock)) {
398 + usec_t scan_ut = now_monotonic_usec();
399
400 - journal_directory_scan(journal_directories[i].path, 0, scan_ut);
401 - }
400 + for(unsigned i = 0; i < MAX_JOURNAL_DIRECTORIES; i++) {
401 + if(!journal_directories[i].path)
402 + break;
403
403 - struct journal_file *jf;
404 - dfe_start_write(journal_files_registry, jf) {
405 - if(jf->last_scan_ut < scan_ut)
406 - dictionary_del(journal_files_registry, jf_dfe.name);
404 + journal_directory_scan(journal_directories[i].path, 0, scan_ut);
405 + }
406 +
407 + struct journal_file *jf;
408 + dfe_start_write(journal_files_registry, jf){
409 + if(jf->last_scan_ut < scan_ut)
410 + dictionary_del(journal_files_registry, jf_dfe.name);
411 + }
412 + dfe_done(jf);
413 +
414 + spinlock_unlock(&spinlock);
415 }
408 - dfe_done(jf);
416 }
417
418 // ----------------------------------------------------------------------------
@@ -472,6 +479,4 @@ void journal_init_files_and_directories(void) {
479 boot_ids_to_first_ut = dictionary_create_advanced(
480 DICT_OPTION_DONT_OVERWRITE_VALUE | DICT_OPTION_FIXED_SIZE,
481 NULL, sizeof(usec_t));
475 -
476 - journal_files_registry_update();
482 }
collectors/systemd-journal.plugin/systemd-main.c
+21 -19
@@ -68,12 +68,7 @@ int main(int argc __maybe_unused, char **argv __maybe_unused) {
68 #endif
69
70 // ------------------------------------------------------------------------
71 -
72 - time_t started_t = now_monotonic_sec();
73 -
74 - size_t iteration = 0;
75 - usec_t step = 1000 * USEC_PER_MS;
76 - bool tty = isatty(fileno(stderr)) == 1;
71 + // register functions to netdata
72
73 netdata_mutex_lock(&stdout_mutex);
74
@@ -85,26 +80,33 @@ int main(int argc __maybe_unused, char **argv __maybe_unused) {
80 SYSTEMD_UNITS_FUNCTION_NAME, SYSTEMD_UNITS_DEFAULT_TIMEOUT, SYSTEMD_UNITS_FUNCTION_DESCRIPTION);
81 #endif
82
83 + fflush(stdout);
84 + netdata_mutex_unlock(&stdout_mutex);
85 +
86 + // ------------------------------------------------------------------------
87 +
88 + usec_t step_ut = 100 * USEC_PER_MS;
89 + usec_t send_newline_ut = 0;
90 + usec_t since_last_scan_ut = 1000 * USEC_PER_SEC; // something big to trigger scanning at start
91 + bool tty = isatty(fileno(stderr)) == 1;
92 +
93 heartbeat_t hb;
94 heartbeat_init(&hb);
95 while(!plugin_should_exit) {
91 - iteration++;
92 -
93 - netdata_mutex_unlock(&stdout_mutex);
94 - heartbeat_next(&hb, step);
95 - netdata_mutex_lock(&stdout_mutex);
96 -
97 - if(!tty)
98 - fprintf(stdout, "\n");
96
100 - if(iteration % 60 == 0)
97 + if(since_last_scan_ut > 60 * USEC_PER_SEC) {
98 journal_files_registry_update();
99 + since_last_scan_ut = 0;
100 + }
101
103 - fflush(stdout);
102 + usec_t dt_ut = heartbeat_next(&hb, step_ut);
103 + since_last_scan_ut += dt_ut;
104 + send_newline_ut += dt_ut;
105
105 - time_t now = now_monotonic_sec();
106 - if(now - started_t > 86400)
107 - break;
106 + if(!tty && send_newline_ut > USEC_PER_SEC) {
107 + send_newline_and_flush();
108 + send_newline_ut = 0;
109 + }
110 }
111
112 exit(0);