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