Merge branch 'maint'
[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 <dirent.h> /* readdir() */
11 #include <assert.h>
12 #include <sys/time.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 PARA_INFO_LOG("unregistering %s (%s)\n", t->status,
42 t->error <0? para_strerror(-t->error) : "shutdown");
43 if (t->pre_select)
44 list_del(&t->pre_select_node);
45 if (t->post_select)
46 list_del(&t->post_select_node);
47 t->error = -E_TASK_UNREGISTERED;
48 }
49
50
51 static void sched_preselect(struct sched *s)
52 {
53 struct task *t, *tmp;
54 list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
55 if (t->error >= 0 && t->pre_select)
56 t->pre_select(s, t);
57 // PARA_INFO_LOG("%s \n", t->status);
58 if (t->error >= 0)
59 continue;
60 /*
61 * We have to check whether the list is empty because the call
62 * to ->pre_select() might have called sched_shutdown(). In
63 * this case t has been unregistered already, so we must not
64 * unregister it again.
65 */
66 if (list_empty(&pre_select_list))
67 return;
68 unregister_task(t);
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 /* nec., see sched_preselect() */
103 if (list_empty(&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 (!initialized)
129 return -E_NOT_INITIALIZED;
130 if (!s->select_function)
131 s->select_function = para_select;
132 again:
133 FD_ZERO(&s->rfds);
134 FD_ZERO(&s->wfds);
135 s->select_timeout = s->default_timeout;
136 s->max_fileno = -1;
137 gettimeofday(now, NULL);
138 sched_preselect(s);
139 if (list_empty(&pre_select_list) && list_empty(&post_select_list))
140 return 0;
141 ret = s->select_function(s->max_fileno + 1, &s->rfds, &s->wfds,
142 &s->select_timeout);
143 if (ret < 0)
144 return ret;
145 if (ret == 0) {
146 /*
147 * APUE: Be careful not to check the descriptor sets on return
148 * unless the return value is greater than zero. The return
149 * state of the descriptor sets is implementation dependent if
150 * either a signal is caught or the timer expires.
151 */
152 FD_ZERO(&s->rfds);
153 FD_ZERO(&s->wfds);
154 }
155 gettimeofday(now, NULL);
156 sched_post_select(s);
157 if (list_empty(&pre_select_list) && list_empty(&post_select_list))
158 return 0;
159 goto again;
160 }
161
162 /*
163 * Initialize the paraslash scheduler.
164 */
165 static void init_sched(void)
166 {
167 PARA_INFO_LOG("initializing scheduler\n");
168 INIT_LIST_HEAD(&pre_select_list);
169 INIT_LIST_HEAD(&post_select_list);
170 initialized = 1;
171 }
172
173 /**
174 * Add a task to the scheduler.
175 *
176 * \param t the task to add
177 *
178 * If the pre_select pointer of \a t is not \p NULL, it is added to
179 * the pre_select list of the scheduler. Same goes for post_select.
180 *
181 * \sa task::pre_select, task::post_select
182 */
183 void register_task(struct task *t)
184 {
185 if (!initialized)
186 init_sched();
187 PARA_INFO_LOG("registering %s (%p)\n", t->status, t);
188 if (t->pre_select) {
189 PARA_DEBUG_LOG("pre_select: %p\n", &t->pre_select);
190 list_add_tail(&t->pre_select_node, &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, &post_select_list);
195 }
196 }
197
198 /**
199 * Unregister all tasks.
200 *
201 * This will cause \a schedule() to return immediately because both the
202 * \a pre_select_list and the \a post_select_list are empty.
203 */
204 void sched_shutdown(void)
205 {
206 struct task *t, *tmp;
207
208 if (!initialized)
209 return;
210 list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node)
211 unregister_task(t);
212 list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node)
213 unregister_task(t);
214 initialized = 0;
215 }
216
217 /**
218 * Get the list of all registered tasks.
219 *
220 * \return The task list.
221 *
222 * Each entry of the list contains an identifier which is simply a hex number
223 * that may be used in \a kill_task() to terminate the task.
224 * The result is dynamically allocated and must be freed by the caller.
225 */
226 char *get_task_list(void)
227 {
228 struct task *t, *tmp;
229 char *msg = NULL;
230
231 if (!initialized)
232 return NULL;
233 list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
234 char *tmp_msg;
235 tmp_msg = make_message("%s%p\tpre\t%s\n", msg? msg : "", t, t->status);
236 free(msg);
237 msg = tmp_msg;
238 }
239 list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
240 char *tmp_msg;
241 // if (t->pre_select)
242 // continue;
243 tmp_msg = make_message("%s%p\tpost\t%s\n", msg? msg : "", t, t->status);
244 free(msg);
245 msg = tmp_msg;
246 }
247 //PARA_DEBUG_LOG("task list:\n%s", msg);
248 return msg;
249 }
250
251 /**
252 * Simulate an error for the given task.
253 *
254 * \param id The task identifier.
255 *
256 * Find the task identified by \a id, set the tasks' error value to
257 * \p -E_TASK_KILLED and unregister the task.
258 *
259 * \return Positive on success, negative on errors (e.g. if \a id does not
260 * correspond to a registered task).
261 */
262 int kill_task(char *id)
263 {
264 struct task *t, *tmp;
265 char buf[20];
266
267 if (!initialized)
268 return -E_NOT_INITIALIZED;
269 list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
270 sprintf(buf, "%p", t);
271 if (strcmp(id, buf))
272 continue;
273 t->error = -E_TASK_KILLED;
274 return 1;
275 }
276 list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
277 sprintf(buf, "%p", t);
278 if (strcmp(id, buf))
279 continue;
280 t->error = -E_TASK_KILLED;
281 return 1;
282 }
283 return -E_NO_SUCH_TASK;
284 }
285
286 /**
287 * Set the select timeout to the minimal possible value.
288 *
289 * \param s Pointer to the scheduler struct.
290 *
291 * This causes the next select() call to return immediately.
292 */
293 void sched_min_delay(struct sched *s)
294 {
295 s->select_timeout.tv_sec = 0;
296 s->select_timeout.tv_usec = 1;
297 }
298
299 /**
300 * Impose an upper bound for the timeout of the next select() call.
301 *
302 * \param to Maximal allowed timeout.
303 * \param s Pointer to the scheduler struct.
304 *
305 * If the current scheduler timeout is already smaller than \a to, this
306 * function does nothing. Otherwise the timeout for the next select() call is
307 * set to the given value.
308 *
309 * \sa sched_request_timeout_ms().
310 */
311 void sched_request_timeout(struct timeval *to, struct sched *s)
312 {
313 if (tv_diff(&s->select_timeout, to, NULL) > 0)
314 s->select_timeout = *to;
315 }
316
317 /**
318 * Force the next select() call to return before the given amount of milliseconds.
319 *
320 * \param ms The maximal allowed timeout in milliseconds.
321 * \param s Pointer to the scheduler struct.
322 *
323 * Like sched_request_timeout() this imposes an upper bound on the timeout
324 * value for the next select() call.
325 */
326 void sched_request_timeout_ms(long unsigned ms, struct sched *s)
327 {
328 struct timeval tv;
329 ms2tv(ms, &tv);
330 sched_request_timeout(&tv, s);
331 }
332
333 /**
334 * Force the next select() call to return before the given future time.
335 *
336 * \param barrier Absolute time before select() should return.
337 * \param s Pointer to the scheduler struct.
338 *
339 * If \a barrier is in the past, this function does nothing.
340 *
341 * \sa sched_request_barrier_or_min_delay().
342 */
343 void sched_request_barrier(struct timeval *barrier, struct sched *s)
344 {
345 struct timeval diff;
346
347 if (tv_diff(now, barrier, &diff) > 0)
348 return;
349 sched_request_timeout(&diff, s);
350 }
351
352 /**
353 * Force the next select() call to return before the given time.
354 *
355 * \param barrier Absolute time before select() should return.
356 * \param s Pointer to the scheduler struct.
357 *
358 * If \a barrier is in the past, this function requests a minimal timeout.
359 *
360 * \sa sched_min_delay(), sched_request_barrier().
361 */
362 void sched_request_barrier_or_min_delay(struct timeval *barrier, struct sched *s)
363 {
364 struct timeval diff;
365
366 if (tv_diff(now, barrier, &diff) > 0)
367 return sched_min_delay(s);
368 sched_request_timeout(&diff, s);
369 }