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