From c4efc3e47819a857ecac6c2a3a39ba351001df33 Mon Sep 17 00:00:00 2001 From: Timo Kokkonen Date: Sun, 11 Oct 2020 11:59:12 +0300 Subject: [PATCH] process: Introduce notify_job_request() This function can be used to kick the main thread out of poll_job_request() function in case someone wants to re-schedule rrd processing. As a starter, a notify call is added at the end of worker processing when the last worker thread exits. The only purpose of this feature is to make the main thread to print out the time of the next scheduled job to be processed, making it just easier to follow the daemon as it operates. Signed-off-by: Timo Kokkonen --- process.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++- process.h | 1 + 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/process.c b/process.c index 8a4059b..e2126a3 100644 --- a/process.c +++ b/process.c @@ -53,6 +53,8 @@ struct mutex work_stats_mutex = { static int workers_active; static int worker_count; +static int job_notify_fd[2]; + static int run_work_on_queue(struct work_queue *queue) { struct work_struct *work; @@ -132,9 +134,16 @@ out_unlock: * ensures next time we start spawning worker threads * the first thread will have number zero on its name. */ - if (!workers_active) + if (!workers_active) { worker_count = 0; + /* + * Kick the job poller, just to print the time of next + * update on the logs + */ + notify_job_request(); + } + mutex_unlock(&work_stats_mutex); return NULL; @@ -196,6 +205,18 @@ int queue_work(unsigned int priority, char *name, return 0; } +static int job_notify_handler(struct event_handler *h) +{ + int ret; + char buf[64]; + + ret = read(job_notify_fd[0], buf, sizeof(buf)); + if (ret < 0) + pr_err("Failed to read: %m\n"); + + return 0; +} + /* * Initialize the jobcontrol. * @@ -205,6 +226,7 @@ int queue_work(unsigned int priority, char *name, */ int init_jobcontrol(int max_jobs_requested) { + static struct event_handler ev; FILE *file; int ret; char buf[256]; @@ -245,6 +267,18 @@ int init_jobcontrol(int max_jobs_requested) max_jobs++; } + ret = pipe(job_notify_fd); + if (ret) { + pr_err("pipe() failed: %m\n"); + return ret; + } + + ev.fd = job_notify_fd[0]; + ev.events = EPOLLIN; + ev.handle_event = job_notify_handler; + ev.name = "job_notify"; + register_event_handler(&ev, EPOLL_CTL_ADD); + open_fail: read_fail: fclose(file); @@ -304,6 +338,18 @@ out: return ret; } +int notify_job_request(void) +{ + int ret; + char byte = 0; + + ret = write(job_notify_fd[1], &byte, sizeof(byte)); + if (ret < 0) + pr_err("Failed to write: %m\n"); + + return 0; +} + int do_fork(void) { int child; diff --git a/process.h b/process.h index 1b92e5b..705ef88 100644 --- a/process.h +++ b/process.h @@ -27,6 +27,7 @@ int register_event_handler(struct event_handler *handler, int op); int init_jobcontrol(int max_jobs_requested); int poll_job_requests(int timeout); +int notify_job_request(void); int do_fork(void); int run(const char *p, char *const argv[]); int clear_zombie(int pid); -- 2.44.0