]> git.tuebingen.mpg.de Git - paraslash.git/blob - sched.c
7d91797a4c43139f63bab245d6e904378d80f49a
[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         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 void sched_preselect(struct sched *s)
51 {
52         struct task *t, *tmp;
53         list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
54                 if (t->error >= 0 && t->pre_select)
55                         t->pre_select(s, t);
56 //              PARA_INFO_LOG("%s \n", t->status);
57                 if (t->error >= 0)
58                         continue;
59                 /*
60                  * We have to check whether the list is empty because the call
61                  * to ->pre_select() might have called sched_shutdown(). In
62                  * this case t has been unregistered already, so we must not
63                  * unregister it again.
64                  */
65                 if (list_empty(&pre_select_list))
66                         return;
67                 unregister_task(t);
68         }
69 }
70
71 //#define SCHED_DEBUG 1
72 static inline void call_post_select(struct sched *s, struct task *t)
73 {
74 #ifndef SCHED_DEBUG
75         t->post_select(s, t);
76 #else
77         struct timeval t1, t2, diff;
78         unsigned long pst;
79
80         gettimeofday(&t1, NULL);
81         t->post_select(s, t);
82         gettimeofday(&t2, NULL);
83         tv_diff(&t1, &t2, &diff);
84         pst = tv2ms(&diff);
85         if (pst > 50)
86                 PARA_WARNING_LOG("%s: post_select time: %lums\n",
87                         t->status, pst);
88 #endif
89 }
90
91 static void sched_post_select(struct sched *s)
92 {
93         struct task *t, *tmp;
94
95         list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
96                 if (t->error >= 0)
97                         call_post_select(s, t);
98 //              PARA_INFO_LOG("%s: %d\n", t->status, t->ret);
99                 if (t->error >= 0)
100                         continue;
101                 /* nec., see sched_preselect() */
102                 if (list_empty(&post_select_list))
103                         return;
104                 unregister_task(t);
105         }
106 }
107
108 /**
109  * The core function for all paraslash programs.
110  *
111  * \param s Pointer to the scheduler struct.
112  *
113  * This function updates the global \a now pointer, calls all registered
114  * pre_select hooks which may set the timeout and add any file descriptors to
115  * the fd sets of \a s.  Next, it calls para_select() and makes the result available
116  * to the registered tasks by calling their post_select hook.
117  *
118  * \return Zero if no more tasks are left in either of the two lists, negative
119  * if para_select returned an error.
120  *
121  * \sa task, now.
122  */
123 int schedule(struct sched *s)
124 {
125         int ret;
126
127         if (!initialized)
128                 return -E_NOT_INITIALIZED;
129         if (!s->select_function)
130                 s->select_function = para_select;
131 again:
132         FD_ZERO(&s->rfds);
133         FD_ZERO(&s->wfds);
134         s->select_timeout = s->default_timeout;
135         s->max_fileno = -1;
136         gettimeofday(now, NULL);
137         sched_preselect(s);
138         if (list_empty(&pre_select_list) && list_empty(&post_select_list))
139                 return 0;
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         sched_post_select(s);
156         if (list_empty(&pre_select_list) && list_empty(&post_select_list))
157                 return 0;
158         goto again;
159 }
160
161 /*
162  * Initialize the paraslash scheduler.
163  */
164 static void init_sched(void)
165 {
166         PARA_INFO_LOG("initializing scheduler\n");
167         INIT_LIST_HEAD(&pre_select_list);
168         INIT_LIST_HEAD(&post_select_list);
169         initialized = 1;
170 }
171
172 /**
173  * Add a task to the scheduler.
174  *
175  * \param t the task to add
176  *
177  * If the pre_select pointer of \a t is not \p NULL, it is added to
178  * the pre_select list of the scheduler. Same goes for post_select.
179  *
180  * \sa task::pre_select, task::post_select
181  */
182 void register_task(struct task *t)
183 {
184         if (!initialized)
185                 init_sched();
186         PARA_INFO_LOG("registering %s (%p)\n", t->status, t);
187         if (t->pre_select) {
188                 PARA_DEBUG_LOG("pre_select: %p\n", &t->pre_select);
189                 list_add_tail(&t->pre_select_node, &pre_select_list);
190         }
191         if (t->post_select) {
192                 PARA_DEBUG_LOG("post_select: %p\n", &t->post_select);
193                 list_add_tail(&t->post_select_node, &post_select_list);
194         }
195 }
196
197 /**
198  * Unregister all tasks.
199  *
200  * This will cause \a schedule() to return immediately because both the
201  * \a pre_select_list and the \a post_select_list are empty.
202  */
203 void sched_shutdown(void)
204 {
205         struct task *t, *tmp;
206
207         if (!initialized)
208                 return;
209         list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
210                 t->error = -E_SCHED_SHUTDOWN;
211                 unregister_task(t);
212         }
213         list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
214                 t->error = -E_SCHED_SHUTDOWN;
215                 unregister_task(t);
216         }
217         initialized = 0;
218 }
219
220 /**
221  * Get the list of all registered tasks.
222  *
223  * \return The task list.
224  *
225  * Each entry of the list contains an identifier which is simply a hex number
226  * that may be used in \a kill_task() to terminate the task.
227  * The result is dynamically allocated and must be freed by the caller.
228  */
229 char *get_task_list(void)
230 {
231         struct task *t, *tmp;
232         char *msg = NULL;
233
234         if (!initialized)
235                 return NULL;
236         list_for_each_entry_safe(t, tmp, &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, &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  * Simulate an error for the given task.
256  *
257  * \param id The task identifier.
258  *
259  * Find the task identified by \a id, set the tasks' error value to
260  * \p -E_TASK_KILLED and unregister the task.
261  *
262  * \return Positive on success, negative on errors (e.g. if \a id does not
263  * correspond to a registered task).
264  */
265 int kill_task(char *id)
266 {
267         struct task *t, *tmp;
268         char buf[20];
269
270         if (!initialized)
271                 return -E_NOT_INITIALIZED;
272         list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
273                 sprintf(buf, "%p", t);
274                 if (strcmp(id, buf))
275                         continue;
276                 t->error = -E_TASK_KILLED;
277                 return 1;
278         }
279         list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
280                 sprintf(buf, "%p", t);
281                 if (strcmp(id, buf))
282                         continue;
283                 t->error = -E_TASK_KILLED;
284                 return 1;
285         }
286         return -E_NO_SUCH_TASK;
287 }
288
289 /**
290  * Set the select timeout to the minimal possible value.
291  *
292  * \param s Pointer to the scheduler struct.
293  *
294  * This causes the next select() call to return immediately.
295  */
296 void sched_min_delay(struct sched *s)
297 {
298         s->select_timeout.tv_sec = 0;
299         s->select_timeout.tv_usec = 1;
300 }
301
302 /**
303  * Impose an upper bound for the timeout of the next select() call.
304  *
305  * \param to Maximal allowed timeout.
306  * \param s Pointer to the scheduler struct.
307  *
308  * If the current scheduler timeout is already smaller than \a to, this
309  * function does nothing. Otherwise the timeout for the next select() call is
310  * set to the given value.
311  *
312  * \sa sched_request_timeout_ms().
313  */
314 void sched_request_timeout(struct timeval *to, struct sched *s)
315 {
316         if (tv_diff(&s->select_timeout, to, NULL) > 0)
317                 s->select_timeout = *to;
318 }
319
320 /**
321  * Force the next select() call to return before the given amount of milliseconds.
322  *
323  * \param ms The maximal allowed timeout in milliseconds.
324  * \param s Pointer to the scheduler struct.
325  *
326  * Like sched_request_timeout() this imposes an upper bound on the timeout
327  * value for the next select() call.
328  */
329 void sched_request_timeout_ms(long unsigned ms, struct sched *s)
330 {
331         struct timeval tv;
332         ms2tv(ms, &tv);
333         sched_request_timeout(&tv, s);
334 }
335
336 /**
337  * Force the next select() call to return before the given future time.
338  *
339  * \param barrier Absolute time before select() should return.
340  * \param s Pointer to the scheduler struct.
341  *
342  * If \a barrier is in the past, this function does nothing.
343  *
344  * \sa sched_request_barrier_or_min_delay().
345  */
346 void sched_request_barrier(struct timeval *barrier, struct sched *s)
347 {
348         struct timeval diff;
349
350         if (tv_diff(now, barrier, &diff) > 0)
351                 return;
352         sched_request_timeout(&diff, s);
353 }
354
355 /**
356  * Force the next select() call to return before the given time.
357  *
358  * \param barrier Absolute time before select() should return.
359  * \param s Pointer to the scheduler struct.
360  *
361  * If \a barrier is in the past, this function requests a minimal timeout.
362  *
363  * \sa sched_min_delay(), sched_request_barrier().
364  */
365 void sched_request_barrier_or_min_delay(struct timeval *barrier, struct sched *s)
366 {
367         struct timeval diff;
368
369         if (tv_diff(now, barrier, &diff) > 0)
370                 return sched_min_delay(s);
371         sched_request_timeout(&diff, s);
372 }