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