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