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