1280e07e1040d475281d554ee8911c7afe8de613
[paraslash.git] / sched.c
1 /*
2  * Copyright (C) 2006-2014 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file sched.c Paraslash's scheduling functions. */
8
9 #include <regex.h>
10 #include <assert.h>
11
12 #include "para.h"
13 #include "ipc.h"
14 #include "fd.h"
15 #include "list.h"
16 #include "sched.h"
17 #include "string.h"
18 #include "time.h"
19 #include "error.h"
20
21 struct task {
22         /** A copy of the task name supplied when the task was registered. */
23         char *name;
24         /** Copied during task_register(). */
25         struct task_info info;
26         /** Whether this task is active (>=0) or in error state (<0). */
27         int status;
28         /** Position of the task in the task list of the scheduler. */
29         struct list_head node;
30         /** If less than zero, the task was notified by another task. */
31         int notification;
32         /** True if task is in error state and exit status has been queried. */
33         bool dead;
34 };
35
36 static struct timeval now_struct;
37 struct timeval *now = &now_struct;
38
39 static inline bool timeout_is_zero(struct sched *s)
40 {
41         struct timeval *tv = &s->select_timeout;
42         return tv->tv_sec == 0 && tv->tv_usec == 0;
43 }
44
45 static void sched_preselect(struct sched *s)
46 {
47         struct task *t, *tmp;
48
49         list_for_each_entry_safe(t, tmp, &s->task_list, node) {
50                 if (t->status < 0)
51                         continue;
52                 if (t->notification != 0)
53                         sched_min_delay(s);
54                 if (t->info.pre_select)
55                         t->info.pre_select(s, t);
56         }
57 }
58
59 static void unlink_and_free_task(struct task *t)
60 {
61         PARA_INFO_LOG("freeing task %s\n", t->name);
62         list_del(&t->node);
63         free(t->name);
64         free(t);
65 }
66
67 //#define SCHED_DEBUG 1
68 static inline void call_post_select(struct sched *s, struct task *t)
69 {
70 #ifndef SCHED_DEBUG
71         t->status = t->info.post_select(s, t);
72 #else
73         struct timeval t1, t2, diff;
74         unsigned long pst;
75
76         clock_get_realtime(&t1);
77         t->status = t->info.post_select(s, t);
78         clock_get_realtime(&t2);
79         tv_diff(&t1, &t2, &diff);
80         pst = tv2ms(&diff);
81         if (pst > 50)
82                 PARA_WARNING_LOG("%s: post_select time: %lums\n",
83                         t->name, pst);
84 #endif
85 }
86
87 static unsigned sched_post_select(struct sched *s)
88 {
89         struct task *t, *tmp;
90         unsigned num_running_tasks = 0;
91
92         list_for_each_entry_safe(t, tmp, &s->task_list, node) {
93                 if (t->status < 0) {
94                         if (t->dead) /* task has been reaped */
95                                 unlink_and_free_task(t);
96                         continue;
97                 }
98                 call_post_select(s, t);
99                 t->notification = 0;
100                 if (t->status >= 0)
101                         num_running_tasks++;
102         }
103         return num_running_tasks;
104 }
105
106 /**
107  * The core function of all paraslash programs.
108  *
109  * \param s Pointer to the scheduler struct.
110  *
111  * This function updates the global \a now pointer, calls all registered
112  * pre_select hooks which may set the timeout and add any file descriptors to
113  * the fd sets of \a s.  Next, it calls para_select() and makes the result available
114  * to the registered tasks by calling their post_select hook.
115  *
116  * \return Zero if no more tasks are left in the task list, negative if the
117  * select function returned an error.
118  *
119  * \sa \ref task, \ref now.
120  */
121 int schedule(struct sched *s)
122 {
123         int ret;
124         unsigned num_running_tasks;
125
126         if (!s->select_function)
127                 s->select_function = para_select;
128 again:
129         FD_ZERO(&s->rfds);
130         FD_ZERO(&s->wfds);
131         s->select_timeout = s->default_timeout;
132         s->max_fileno = -1;
133         clock_get_realtime(now);
134         sched_preselect(s);
135         ret = s->select_function(s->max_fileno + 1, &s->rfds, &s->wfds,
136                 &s->select_timeout);
137         if (ret < 0)
138                 return ret;
139         if (ret == 0) {
140                 /*
141                  * APUE: Be careful not to check the descriptor sets on return
142                  * unless the return value is greater than zero. The return
143                  * state of the descriptor sets is implementation dependent if
144                  * either a signal is caught or the timer expires.
145                  */
146                 FD_ZERO(&s->rfds);
147                 FD_ZERO(&s->wfds);
148         }
149         clock_get_realtime(now);
150         num_running_tasks = sched_post_select(s);
151         if (num_running_tasks == 0)
152                 return 0;
153         goto again;
154 }
155
156 /**
157  * Obtain the error status of a task and deallocate its resources.
158  *
159  * \param tptr Identifies the task to reap.
160  *
161  * This function is similar to wait(2) in that it returns information about a
162  * terminated task and allows to release the resources associated with the
163  * task. Until this function is called, the terminated task remains in a zombie
164  * state.
165  *
166  * \return If \a tptr is \p NULL, or \a *tptr is \p NULL, the function does
167  * nothing and returns zero. Otherwise, it is checked whether the task
168  * identified by \a tptr is still running. If it is, the function returns zero
169  * and again, no action is taken. Otherwise the (negative) error code of the
170  * terminated task is returned and \a *tptr is set to \p NULL. The task will
171  * then be removed removed from the scheduler task list.
172  *
173  * \sa \ref sched_shutdown(), wait(2).
174  */
175 int task_reap(struct task **tptr)
176 {
177         struct task *t;
178
179         if (!tptr)
180                 return 0;
181         t = *tptr;
182         if (!t)
183                 return 0;
184         if (t->status >= 0)
185                 return 0;
186         if (t->dead) /* will be freed in sched_post_select() */
187                 return 0;
188         /*
189          * With list_for_each_entry_safe() it is only safe to remove the
190          * _current_ list item. Since we are being called from the loop in
191          * schedule() via some task's ->post_select() function, freeing the
192          * given task here would result in use-after-free bugs in schedule().
193          * So we only set t->dead which tells schedule() to free the task in
194          * the next iteration of its loop.
195          */
196         t->dead = true;
197         *tptr = NULL;
198         return t->status;
199 }
200
201 /**
202  * Deallocate all resources of all tasks of a scheduler instance.
203  *
204  * \param s The scheduler instance.
205  *
206  * This should only be called after \ref schedule() has returned.
207  */
208 void sched_shutdown(struct sched *s)
209 {
210         struct task *t, *tmp;
211
212         list_for_each_entry_safe(t, tmp, &s->task_list, node) {
213                 if (t->status >= 0)
214                         /* The task list should contain only terminated tasks. */
215                         PARA_WARNING_LOG("shutting down running task %s\n",
216                                 t->name);
217                 unlink_and_free_task(t);
218         }
219 }
220
221 /**
222  * Add a task to the scheduler task list.
223  *
224  * \param info Task information supplied by the caller.
225  * \param s The scheduler instance.
226  *
227  * \return A pointer to a newly allocated task structure. It will be
228  * freed by sched_shutdown().
229  */
230 struct task *task_register(struct task_info *info, struct sched *s)
231 {
232         struct task *t = para_malloc(sizeof(*t));
233
234         assert(info->post_select);
235
236         if (!s->task_list.next)
237                 INIT_LIST_HEAD(&s->task_list);
238
239         t->info = *info;
240         t->name = para_strdup(info->name);
241         t->notification = 0;
242         t->status = 0;
243         t->dead = false;
244         list_add_tail(&t->node, &s->task_list);
245         return t;
246 }
247
248 /**
249  * Obtain the context pointer of a task.
250  *
251  * \param t Return this task's context pointer.
252  *
253  * \return A pointer to the memory location specified previously as \a
254  * task_info->context when the task was registered with \ref task_register().
255  */
256 void *task_context(struct task *t)
257 {
258         return t->info.context;
259 }
260
261 /**
262  * Get the list of all registered tasks.
263  *
264  * \param s The scheduler instance to get the task list from.
265  *
266  * \return The task list.
267  *
268  * Each entry of the list contains an identifier which is simply a hex number.
269  * The result is dynamically allocated and must be freed by the caller.
270  */
271 char *get_task_list(struct sched *s)
272 {
273         struct task *t, *tmp;
274         char *msg = NULL;
275
276         list_for_each_entry_safe(t, tmp, &s->task_list, node) {
277                 char *tmp_msg;
278                 tmp_msg = make_message("%s%p\t%s\t%s\n", msg? msg : "", t,
279                         t->status < 0? (t->dead? "dead" : "zombie") : "running",
280                         t->name);
281                 free(msg);
282                 msg = tmp_msg;
283         }
284         return msg;
285 }
286
287 /**
288  * Set the notification value of a task.
289  *
290  * \param t The task to notify.
291  * \param err A positive error code.
292  *
293  * Tasks which honor notifications are supposed to call \ref
294  * task_get_notification() in their post_select function and act on the
295  * returned notification value.
296  *
297  * If the scheduler detects during its pre_select loop that at least one task
298  * has been notified, the loop terminates, and the post_select methods of all
299  * taks are immediately called again.
300  *
301  * The notification for a task is reset after the call to its post_select
302  * method.
303  *
304  * \sa \ref task_get_notification().
305  */
306 void task_notify(struct task *t, int err)
307 {
308         assert(err > 0);
309         if (t->notification == -err) /* ignore subsequent notifications */
310                 return;
311         PARA_INFO_LOG("notifying task %s: %s\n", t->name, para_strerror(err));
312         t->notification = -err;
313 }
314
315 /**
316  * Return the notification value of a task.
317  *
318  * \param t The task to get the notification value from.
319  *
320  * \return The notification value. If this is negative, the task has been
321  * notified by another task. Tasks are supposed to check for notifications by
322  * calling this function from their post_select method.
323  *
324  * \sa \ref task_notify().
325  */
326 int task_get_notification(const struct task *t)
327 {
328         return t->notification;
329 }
330
331 /**
332  * Return the status value of a task.
333  *
334  * \param t The task to get the status value from.
335  *
336  * \return Zero if task does not exist, one if task is running, negative error
337  * code if task has terminated.
338  */
339 int task_status(const struct task *t)
340 {
341         if (!t)
342                 return 0;
343         if (t->dead)
344                 return 0;
345         if (t->status >= 0)
346                 return 1;
347         return t->status;
348 }
349
350 /**
351  * Set the notification value of all tasks of a scheduler instance.
352  *
353  * \param s The scheduler instance whose tasks should be notified.
354  * \param err A positive error code.
355  *
356  * This simply iterates over all existing tasks of \a s and sets each
357  * task's notification value to \p -err.
358  */
359 void task_notify_all(struct sched *s, int err)
360 {
361         struct task *t;
362
363         list_for_each_entry(t, &s->task_list, node)
364                 task_notify(t, err);
365 }
366
367 /**
368  * Set the select timeout to the minimal possible value.
369  *
370  * \param s Pointer to the scheduler struct.
371  *
372  * This causes the next select() call to return immediately.
373  */
374 void sched_min_delay(struct sched *s)
375 {
376         s->select_timeout.tv_sec = s->select_timeout.tv_usec = 0;
377 }
378
379 /**
380  * Impose an upper bound for the timeout of the next select() call.
381  *
382  * \param to Maximal allowed timeout.
383  * \param s Pointer to the scheduler struct.
384  *
385  * If the current scheduler timeout is already smaller than \a to, this
386  * function does nothing. Otherwise the timeout for the next select() call is
387  * set to the given value.
388  *
389  * \sa sched_request_timeout_ms().
390  */
391 void sched_request_timeout(struct timeval *to, struct sched *s)
392 {
393         if (tv_diff(&s->select_timeout, to, NULL) > 0)
394                 s->select_timeout = *to;
395 }
396
397 /**
398  * Force the next select() call to return before the given amount of milliseconds.
399  *
400  * \param ms The maximal allowed timeout in milliseconds.
401  * \param s Pointer to the scheduler struct.
402  *
403  * Like sched_request_timeout() this imposes an upper bound on the timeout
404  * value for the next select() call.
405  */
406 void sched_request_timeout_ms(long unsigned ms, struct sched *s)
407 {
408         struct timeval tv;
409         ms2tv(ms, &tv);
410         sched_request_timeout(&tv, s);
411 }
412
413 /**
414  * Force the next select() call to return before the given future time.
415  *
416  * \param barrier Absolute time before select() should return.
417  * \param s Pointer to the scheduler struct.
418  *
419  * \return If \a barrier is in the past, this function does nothing and returns
420  * zero. Otherwise it returns one.
421  *
422  * \sa sched_request_barrier_or_min_delay().
423  */
424 int sched_request_barrier(struct timeval *barrier, struct sched *s)
425 {
426         struct timeval diff;
427
428         if (tv_diff(now, barrier, &diff) > 0)
429                 return 0;
430         sched_request_timeout(&diff, s);
431         return 1;
432 }
433
434 /**
435  * Force the next select() call to return before the given time.
436  *
437  * \param barrier Absolute time before select() should return.
438  * \param s Pointer to the scheduler struct.
439  *
440  * \return If \a barrier is in the past, this function requests a minimal
441  * timeout and returns zero. Otherwise it returns one.
442  *
443  * \sa sched_min_delay(), sched_request_barrier().
444  */
445 int sched_request_barrier_or_min_delay(struct timeval *barrier, struct sched *s)
446 {
447         struct timeval diff;
448
449         if (tv_diff(now, barrier, &diff) > 0) {
450                 sched_min_delay(s);
451                 return 0;
452         }
453         sched_request_timeout(&diff, s);
454         return 1;
455 }