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