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