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