sched: Get rid of (pre)select shortcuts.
[paraslash.git] / sched.c
1 /*
2  * Copyright (C) 2006-2013 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 #include <sys/time.h>
12
13 #include "para.h"
14 #include "ipc.h"
15 #include "fd.h"
16 #include "list.h"
17 #include "sched.h"
18 #include "string.h"
19 #include "time.h"
20 #include "error.h"
21
22 static struct timeval now_struct;
23 struct timeval *now = &now_struct;
24
25 /*
26  * Remove a task from the scheduler.
27  *
28  * \param t The task to remove.
29  *
30  * If the pre_select pointer of \a t is not \p NULL, it is removed from
31  * the pre_select list of the scheduler. Same goes for \a post_select.
32  */
33 static void unregister_task(struct task *t)
34 {
35         assert(t->error < 0);
36         PARA_INFO_LOG("unregistering %s (%s)\n", t->status,
37                 para_strerror(-t->error));
38         if (t->pre_select)
39                 list_del(&t->pre_select_node);
40         if (t->post_select)
41                 list_del(&t->post_select_node);
42 }
43
44 static inline bool timeout_is_zero(struct sched *s)
45 {
46         struct timeval *tv = &s->select_timeout;
47         return tv->tv_sec == 0 && tv->tv_usec == 0;
48 }
49
50 static void sched_preselect(struct sched *s)
51 {
52         struct task *t, *tmp;
53
54         list_for_each_entry_safe(t, tmp, &s->pre_select_list, pre_select_node) {
55                 if (t->notification != 0)
56                         sched_min_delay(s);
57                 if (t->pre_select)
58                         t->pre_select(s, t);
59         }
60 }
61
62 //#define SCHED_DEBUG 1
63 static inline void call_post_select(struct sched *s, struct task *t)
64 {
65 #ifndef SCHED_DEBUG
66         t->post_select(s, t);
67 #else
68         struct timeval t1, t2, diff;
69         unsigned long pst;
70
71         gettimeofday(&t1, NULL);
72         t->post_select(s, t);
73         gettimeofday(&t2, NULL);
74         tv_diff(&t1, &t2, &diff);
75         pst = tv2ms(&diff);
76         if (pst > 50)
77                 PARA_WARNING_LOG("%s: post_select time: %lums\n",
78                         t->status, pst);
79 #endif
80 }
81
82 static void sched_post_select(struct sched *s)
83 {
84         struct task *t, *tmp;
85
86         list_for_each_entry_safe(t, tmp, &s->post_select_list, post_select_node) {
87                 if (t->error >= 0)
88                         call_post_select(s, t);
89 //              PARA_INFO_LOG("%s: %d\n", t->status, t->ret);
90                 t->notification = 0;
91                 if (t->error >= 0)
92                         continue;
93                 unregister_task(t);
94         }
95 }
96
97 /**
98  * The core function for all paraslash programs.
99  *
100  * \param s Pointer to the scheduler struct.
101  *
102  * This function updates the global \a now pointer, calls all registered
103  * pre_select hooks which may set the timeout and add any file descriptors to
104  * the fd sets of \a s.  Next, it calls para_select() and makes the result available
105  * to the registered tasks by calling their post_select hook.
106  *
107  * \return Zero if no more tasks are left in either of the two lists, negative
108  * if para_select returned an error.
109  *
110  * \sa task, now.
111  */
112 int schedule(struct sched *s)
113 {
114         int ret;
115
116         if (!s->select_function)
117                 s->select_function = para_select;
118 again:
119         FD_ZERO(&s->rfds);
120         FD_ZERO(&s->wfds);
121         s->select_timeout = s->default_timeout;
122         s->max_fileno = -1;
123         gettimeofday(now, NULL);
124         sched_preselect(s);
125         ret = s->select_function(s->max_fileno + 1, &s->rfds, &s->wfds,
126                 &s->select_timeout);
127         if (ret < 0)
128                 return ret;
129         if (ret == 0) {
130                 /*
131                  * APUE: Be careful not to check the descriptor sets on return
132                  * unless the return value is greater than zero. The return
133                  * state of the descriptor sets is implementation dependent if
134                  * either a signal is caught or the timer expires.
135                  */
136                 FD_ZERO(&s->rfds);
137                 FD_ZERO(&s->wfds);
138         }
139         gettimeofday(now, NULL);
140         sched_post_select(s);
141         if (list_empty(&s->pre_select_list) && list_empty(&s->post_select_list))
142                 return 0;
143         goto again;
144 }
145
146 /**
147  * Add a task to the scheduler.
148  *
149  * \param t The task to add.
150  * \param s The scheduler instance to add the task to.
151  *
152  * If the pre_select pointer of \a t is not \p NULL, it is added to
153  * the pre_select list of the scheduler. Same goes for post_select.
154  *
155  * \sa task::pre_select, task::post_select
156  */
157 void register_task(struct sched *s, struct task *t)
158 {
159         PARA_INFO_LOG("registering %s (%p)\n", t->status, t);
160         t->notification = 0;
161         if (!s->pre_select_list.next)
162                 INIT_LIST_HEAD(&s->pre_select_list);
163         if (!s->post_select_list.next)
164                 INIT_LIST_HEAD(&s->post_select_list);
165         if (t->pre_select) {
166                 PARA_DEBUG_LOG("pre_select: %p\n", &t->pre_select);
167                 list_add_tail(&t->pre_select_node, &s->pre_select_list);
168         }
169         if (t->post_select) {
170                 PARA_DEBUG_LOG("post_select: %p\n", &t->post_select);
171                 list_add_tail(&t->post_select_node, &s->post_select_list);
172         }
173 }
174
175 /**
176  * Get the list of all registered tasks.
177  *
178  * \param s The scheduler instance to get the task list from.
179  *
180  * \return The task list.
181  *
182  * Each entry of the list contains an identifier which is simply a hex number.
183  * The result is dynamically allocated and must be freed by the caller.
184  */
185 char *get_task_list(struct sched *s)
186 {
187         struct task *t, *tmp;
188         char *msg = NULL;
189
190         list_for_each_entry_safe(t, tmp, &s->pre_select_list, pre_select_node) {
191                 char *tmp_msg;
192                 tmp_msg = make_message("%s%p\tpre\t%s\n", msg? msg : "", t, t->status);
193                 free(msg);
194                 msg = tmp_msg;
195         }
196         list_for_each_entry_safe(t, tmp, &s->post_select_list, post_select_node) {
197                 char *tmp_msg;
198 //              if (t->pre_select)
199 //                      continue;
200                 tmp_msg = make_message("%s%p\tpost\t%s\n", msg? msg : "", t, t->status);
201                 free(msg);
202                 msg = tmp_msg;
203         }
204         //PARA_DEBUG_LOG("task list:\n%s", msg);
205         return msg;
206 }
207
208 /**
209  * Set the notification value of a task.
210  *
211  * \param t The task to notify.
212  * \param err A positive error code.
213  *
214  * Tasks which honor notifications are supposed to call \ref
215  * task_get_notification() in their post_select function and act on the
216  * returned notification value.
217  *
218  * If the scheduler detects during its pre_select loop that at least one task
219  * has been notified, the loop terminates, and the post_select methods of all
220  * taks are immediately called again.
221  *
222  * The notification for a task is reset after the call to its post_select
223  * method.
224  *
225  * \sa \ref task_get_notification().
226  */
227 void task_notify(struct task *t, int err)
228 {
229         assert(err > 0);
230         if (t->notification == -err) /* ignore subsequent notifications */
231                 return;
232         PARA_INFO_LOG("notifying task %s: %s\n", t->status, para_strerror(err));
233         t->notification = -err;
234 }
235
236 /**
237  * Return the notification value of a task.
238  *
239  * \param t The task to get the notification value from.
240  *
241  * \return The notification value. If this is negative, the task has been
242  * notified by another task. Tasks are supposed to check for notifications by
243  * calling this function from their post_select method.
244  *
245  * \sa \ref task_notify().
246  */
247 int task_get_notification(struct task *t)
248 {
249         return t->notification;
250 }
251
252 /**
253  * Set the notification value of all tasks of a scheduler instance.
254  *
255  * \param s The scheduler instance whose tasks should be notified.
256  * \param err A positive error code.
257  *
258  * This simply iterates over all existing tasks of \a s and sets each
259  * task's notification value to \p -err.
260  */
261 void task_notify_all(struct sched *s, int err)
262 {
263         struct task *t;
264
265         list_for_each_entry(t, &s->pre_select_list, pre_select_node)
266                 task_notify(t, err);
267         list_for_each_entry(t, &s->post_select_list, post_select_node)
268                 task_notify(t, err);
269 }
270
271 /**
272  * Set the select timeout to the minimal possible value.
273  *
274  * \param s Pointer to the scheduler struct.
275  *
276  * This causes the next select() call to return immediately.
277  */
278 void sched_min_delay(struct sched *s)
279 {
280         s->select_timeout.tv_sec = s->select_timeout.tv_usec = 0;
281 }
282
283 /**
284  * Impose an upper bound for the timeout of the next select() call.
285  *
286  * \param to Maximal allowed timeout.
287  * \param s Pointer to the scheduler struct.
288  *
289  * If the current scheduler timeout is already smaller than \a to, this
290  * function does nothing. Otherwise the timeout for the next select() call is
291  * set to the given value.
292  *
293  * \sa sched_request_timeout_ms().
294  */
295 void sched_request_timeout(struct timeval *to, struct sched *s)
296 {
297         if (tv_diff(&s->select_timeout, to, NULL) > 0)
298                 s->select_timeout = *to;
299 }
300
301 /**
302  * Force the next select() call to return before the given amount of milliseconds.
303  *
304  * \param ms The maximal allowed timeout in milliseconds.
305  * \param s Pointer to the scheduler struct.
306  *
307  * Like sched_request_timeout() this imposes an upper bound on the timeout
308  * value for the next select() call.
309  */
310 void sched_request_timeout_ms(long unsigned ms, struct sched *s)
311 {
312         struct timeval tv;
313         ms2tv(ms, &tv);
314         sched_request_timeout(&tv, s);
315 }
316
317 /**
318  * Force the next select() call to return before the given future time.
319  *
320  * \param barrier Absolute time before select() should return.
321  * \param s Pointer to the scheduler struct.
322  *
323  * \return If \a barrier is in the past, this function does nothing and returns
324  * zero. Otherwise it returns one.
325  *
326  * \sa sched_request_barrier_or_min_delay().
327  */
328 int sched_request_barrier(struct timeval *barrier, struct sched *s)
329 {
330         struct timeval diff;
331
332         if (tv_diff(now, barrier, &diff) > 0)
333                 return 0;
334         sched_request_timeout(&diff, s);
335         return 1;
336 }
337
338 /**
339  * Force the next select() call to return before the given time.
340  *
341  * \param barrier Absolute time before select() should return.
342  * \param s Pointer to the scheduler struct.
343  *
344  * \return If \a barrier is in the past, this function requests a minimal
345  * timeout and returns zero. Otherwise it returns one.
346  *
347  * \sa sched_min_delay(), sched_request_barrier().
348  */
349 int sched_request_barrier_or_min_delay(struct timeval *barrier, struct sched *s)
350 {
351         struct timeval diff;
352
353         if (tv_diff(now, barrier, &diff) > 0) {
354                 sched_min_delay(s);
355                 return 0;
356         }
357         sched_request_timeout(&diff, s);
358         return 1;
359 }