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