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