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