]> git.tuebingen.mpg.de Git - paraslash.git/blob - sched.c
13993b13be128e261e989cb3963cfc2e73a693b4
[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 /**
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 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);
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);
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);
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 task, \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);
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);
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  * Obtain the context pointer of a task.
267  *
268  * \param t Return this task's context pointer.
269  *
270  * \return A pointer to the memory location specified previously as \a
271  * task_info->context when the task was registered with \ref task_register().
272  */
273 void *task_context(struct task *t)
274 {
275         return t->info.context;
276 }
277
278 /**
279  * Get the list of all registered tasks.
280  *
281  * \param s The scheduler instance to get the task list from.
282  *
283  * \return The task list.
284  *
285  * Each entry of the list contains an identifier which is simply a hex number.
286  * The result is dynamically allocated and must be freed by the caller.
287  */
288 char *get_task_list(struct sched *s)
289 {
290         struct task *t, *tmp;
291         char *msg = NULL;
292
293         list_for_each_entry_safe(t, tmp, &s->task_list, node) {
294                 char *tmp_msg;
295                 tmp_msg = make_message("%s%p\t%s\t%s\n", msg? msg : "", t,
296                         t->status == TS_DEAD? "dead" :
297                                 (t->status == TS_RUNNING? "running" : "zombie"),
298                         t->name);
299                 free(msg);
300                 msg = tmp_msg;
301         }
302         return msg;
303 }
304
305 /**
306  * Set the notification value of a task.
307  *
308  * \param t The task to notify.
309  * \param err A positive error code.
310  *
311  * Tasks which honor notifications are supposed to call \ref
312  * task_get_notification() in their post_select function and act on the
313  * returned notification value.
314  *
315  * If the scheduler detects during its pre_select loop that at least one task
316  * has been notified, the loop terminates, and the post_select methods of all
317  * taks are immediately called again.
318  *
319  * The notification for a task is reset after the call to its post_select
320  * method.
321  *
322  * \sa \ref task_get_notification().
323  */
324 void task_notify(struct task *t, int err)
325 {
326         assert(err > 0);
327         if (t->notification == -err) /* ignore subsequent notifications */
328                 return;
329         PARA_INFO_LOG("notifying task %s: %s\n", t->name, para_strerror(err));
330         t->notification = -err;
331 }
332
333 /**
334  * Return the notification value of a task.
335  *
336  * \param t The task to get the notification value from.
337  *
338  * \return The notification value. If this is negative, the task has been
339  * notified by another task. Tasks are supposed to check for notifications by
340  * calling this function from their post_select method.
341  *
342  * \sa \ref task_notify().
343  */
344 int task_get_notification(const struct task *t)
345 {
346         return t->notification;
347 }
348
349 /**
350  * Return the status value of a task.
351  *
352  * \param t The task to get the status value from.
353  *
354  * \return Zero if task does not exist, one if task is running, negative error
355  * code if task has terminated.
356  */
357 int task_status(const struct task *t)
358 {
359         if (!t)
360                 return 0;
361         if (t->status == TS_DEAD) /* pretend dead tasks don't exist */
362                 return 0;
363         if (t->status == TS_RUNNING)
364                 return 1;
365         return t->status;
366 }
367
368 /**
369  * Set the notification value of all tasks of a scheduler instance.
370  *
371  * \param s The scheduler instance whose tasks should be notified.
372  * \param err A positive error code.
373  *
374  * This simply iterates over all existing tasks of \a s and sets each
375  * task's notification value to \p -err.
376  */
377 void task_notify_all(struct sched *s, int err)
378 {
379         struct task *t;
380
381         list_for_each_entry(t, &s->task_list, node)
382                 task_notify(t, err);
383 }
384
385 /**
386  * Set the select timeout to the minimal possible value.
387  *
388  * \param s Pointer to the scheduler struct.
389  *
390  * This causes the next select() call to return immediately.
391  */
392 void sched_min_delay(struct sched *s)
393 {
394         s->select_timeout.tv_sec = s->select_timeout.tv_usec = 0;
395 }
396
397 /**
398  * Impose an upper bound for the timeout of the next select() call.
399  *
400  * \param to Maximal allowed timeout.
401  * \param s Pointer to the scheduler struct.
402  *
403  * If the current scheduler timeout is already smaller than \a to, this
404  * function does nothing. Otherwise the timeout for the next select() call is
405  * set to the given value.
406  *
407  * \sa sched_request_timeout_ms().
408  */
409 void sched_request_timeout(struct timeval *to, struct sched *s)
410 {
411         if (tv_diff(&s->select_timeout, to, NULL) > 0)
412                 s->select_timeout = *to;
413 }
414
415 /**
416  * Force the next select() call to return before the given amount of milliseconds.
417  *
418  * \param ms The maximal allowed timeout in milliseconds.
419  * \param s Pointer to the scheduler struct.
420  *
421  * Like sched_request_timeout() this imposes an upper bound on the timeout
422  * value for the next select() call.
423  */
424 void sched_request_timeout_ms(long unsigned ms, struct sched *s)
425 {
426         struct timeval tv;
427         ms2tv(ms, &tv);
428         sched_request_timeout(&tv, s);
429 }
430
431 /**
432  * Force the next select() call to return before the given future 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 does nothing and returns
438  * zero. Otherwise it returns one.
439  *
440  * \sa sched_request_barrier_or_min_delay().
441  */
442 int sched_request_barrier(struct timeval *barrier, struct sched *s)
443 {
444         struct timeval diff;
445
446         if (tv_diff(now, barrier, &diff) > 0)
447                 return 0;
448         sched_request_timeout(&diff, s);
449         return 1;
450 }
451
452 /**
453  * Force the next select() call to return before the given time.
454  *
455  * \param barrier Absolute time before select() should return.
456  * \param s Pointer to the scheduler struct.
457  *
458  * \return If \a barrier is in the past, this function requests a minimal
459  * timeout and returns zero. Otherwise it returns one.
460  *
461  * \sa sched_min_delay(), sched_request_barrier().
462  */
463 int sched_request_barrier_or_min_delay(struct timeval *barrier, struct sched *s)
464 {
465         struct timeval diff;
466
467         if (tv_diff(now, barrier, &diff) > 0) {
468                 sched_min_delay(s);
469                 return 0;
470         }
471         sched_request_timeout(&diff, s);
472         return 1;
473 }