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