Add some missing documentation.
[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->pre_select)
59 continue;
60 t->pre_select(s, t);
61 if (timeout_is_zero(s))
62 break;
63 }
64 }
65
66 //#define SCHED_DEBUG 1
67 static inline void call_post_select(struct sched *s, struct task *t)
68 {
69 #ifndef SCHED_DEBUG
70 t->post_select(s, t);
71 #else
72 struct timeval t1, t2, diff;
73 unsigned long pst;
74
75 gettimeofday(&t1, NULL);
76 t->post_select(s, t);
77 gettimeofday(&t2, NULL);
78 tv_diff(&t1, &t2, &diff);
79 pst = tv2ms(&diff);
80 if (pst > 50)
81 PARA_WARNING_LOG("%s: post_select time: %lums\n",
82 t->status, pst);
83 #endif
84 }
85
86 static void sched_post_select(struct sched *s)
87 {
88 struct task *t, *tmp;
89
90 list_for_each_entry_safe(t, tmp, &s->post_select_list, post_select_node) {
91 if (t->error >= 0)
92 call_post_select(s, t);
93 // PARA_INFO_LOG("%s: %d\n", t->status, t->ret);
94 if (t->error >= 0)
95 continue;
96 /*
97 * We have to check whether the list is empty because the call
98 * to ->post_select() might have called sched_shutdown(). In
99 * this case t has been unregistered already, so we must not
100 * unregister it again.
101 */
102 if (list_empty(&s->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 (!s->select_function)
128 s->select_function = para_select;
129 again:
130 FD_ZERO(&s->rfds);
131 FD_ZERO(&s->wfds);
132 s->select_timeout = s->default_timeout;
133 s->max_fileno = -1;
134 gettimeofday(now, NULL);
135 sched_preselect(s);
136 if (list_empty(&s->pre_select_list) && list_empty(&s->post_select_list))
137 return 0;
138 if (!timeout_is_zero(s)) {
139 ret = s->select_function(s->max_fileno + 1, &s->rfds, &s->wfds,
140 &s->select_timeout);
141 if (ret < 0)
142 return ret;
143 if (ret == 0) {
144 /*
145 * APUE: Be careful not to check the descriptor sets on return
146 * unless the return value is greater than zero. The return
147 * state of the descriptor sets is implementation dependent if
148 * either a signal is caught or the timer expires.
149 */
150 FD_ZERO(&s->rfds);
151 FD_ZERO(&s->wfds);
152 }
153 gettimeofday(now, NULL);
154 } else {
155 FD_ZERO(&s->rfds);
156 FD_ZERO(&s->wfds);
157 }
158 sched_post_select(s);
159 if (list_empty(&s->pre_select_list) && list_empty(&s->post_select_list))
160 return 0;
161 goto again;
162 }
163
164 /**
165 * Add a task to the scheduler.
166 *
167 * \param t The task to add.
168 * \param s The scheduler instance to add the task to.
169 *
170 * If the pre_select pointer of \a t is not \p NULL, it is added to
171 * the pre_select list of the scheduler. Same goes for post_select.
172 *
173 * \sa task::pre_select, task::post_select
174 */
175 void register_task(struct sched *s, struct task *t)
176 {
177 PARA_INFO_LOG("registering %s (%p)\n", t->status, t);
178 if (!s->pre_select_list.next)
179 INIT_LIST_HEAD(&s->pre_select_list);
180 if (!s->post_select_list.next)
181 INIT_LIST_HEAD(&s->post_select_list);
182 if (t->pre_select) {
183 PARA_DEBUG_LOG("pre_select: %p\n", &t->pre_select);
184 list_add_tail(&t->pre_select_node, &s->pre_select_list);
185 }
186 if (t->post_select) {
187 PARA_DEBUG_LOG("post_select: %p\n", &t->post_select);
188 list_add_tail(&t->post_select_node, &s->post_select_list);
189 }
190 }
191
192 /**
193 * Unregister all tasks.
194 *
195 * \param s The scheduler instance to shut down.
196 *
197 * This will cause \a schedule() to return immediately because both the
198 * \a pre_select_list and the \a post_select_list are empty. This function
199 * must be called from the post_select (rather than the pre_select) method.
200 */
201 void sched_shutdown(struct sched *s)
202 {
203 struct task *t, *tmp;
204
205 list_for_each_entry_safe(t, tmp, &s->pre_select_list, pre_select_node) {
206 t->error = -E_SCHED_SHUTDOWN;
207 unregister_task(t);
208 }
209 list_for_each_entry_safe(t, tmp, &s->post_select_list, post_select_node) {
210 t->error = -E_SCHED_SHUTDOWN;
211 unregister_task(t);
212 }
213 }
214
215 /**
216 * Get the list of all registered tasks.
217 *
218 * \param s The scheduler instance to get the task list from.
219 *
220 * \return The task list.
221 *
222 * Each entry of the list contains an identifier which is simply a hex number.
223 * The result is dynamically allocated and must be freed by the caller.
224 */
225 char *get_task_list(struct sched *s)
226 {
227 struct task *t, *tmp;
228 char *msg = NULL;
229
230 list_for_each_entry_safe(t, tmp, &s->pre_select_list, pre_select_node) {
231 char *tmp_msg;
232 tmp_msg = make_message("%s%p\tpre\t%s\n", msg? msg : "", t, t->status);
233 free(msg);
234 msg = tmp_msg;
235 }
236 list_for_each_entry_safe(t, tmp, &s->post_select_list, post_select_node) {
237 char *tmp_msg;
238 // if (t->pre_select)
239 // continue;
240 tmp_msg = make_message("%s%p\tpost\t%s\n", msg? msg : "", t, t->status);
241 free(msg);
242 msg = tmp_msg;
243 }
244 //PARA_DEBUG_LOG("task list:\n%s", msg);
245 return msg;
246 }
247
248 /**
249 * Set the select timeout to the minimal possible value.
250 *
251 * \param s Pointer to the scheduler struct.
252 *
253 * This causes the next select() call to return immediately.
254 */
255 void sched_min_delay(struct sched *s)
256 {
257 s->select_timeout.tv_sec = s->select_timeout.tv_usec = 0;
258 }
259
260 /**
261 * Impose an upper bound for the timeout of the next select() call.
262 *
263 * \param to Maximal allowed timeout.
264 * \param s Pointer to the scheduler struct.
265 *
266 * If the current scheduler timeout is already smaller than \a to, this
267 * function does nothing. Otherwise the timeout for the next select() call is
268 * set to the given value.
269 *
270 * \sa sched_request_timeout_ms().
271 */
272 void sched_request_timeout(struct timeval *to, struct sched *s)
273 {
274 if (tv_diff(&s->select_timeout, to, NULL) > 0)
275 s->select_timeout = *to;
276 }
277
278 /**
279 * Force the next select() call to return before the given amount of milliseconds.
280 *
281 * \param ms The maximal allowed timeout in milliseconds.
282 * \param s Pointer to the scheduler struct.
283 *
284 * Like sched_request_timeout() this imposes an upper bound on the timeout
285 * value for the next select() call.
286 */
287 void sched_request_timeout_ms(long unsigned ms, struct sched *s)
288 {
289 struct timeval tv;
290 ms2tv(ms, &tv);
291 sched_request_timeout(&tv, s);
292 }
293
294 /**
295 * Force the next select() call to return before the given future time.
296 *
297 * \param barrier Absolute time before select() should return.
298 * \param s Pointer to the scheduler struct.
299 *
300 * \return If \a barrier is in the past, this function does nothing and returns
301 * zero. Otherwise it returns one.
302 *
303 * \sa sched_request_barrier_or_min_delay().
304 */
305 int sched_request_barrier(struct timeval *barrier, struct sched *s)
306 {
307 struct timeval diff;
308
309 if (tv_diff(now, barrier, &diff) > 0)
310 return 0;
311 sched_request_timeout(&diff, s);
312 return 1;
313 }
314
315 /**
316 * Force the next select() call to return before the given time.
317 *
318 * \param barrier Absolute time before select() should return.
319 * \param s Pointer to the scheduler struct.
320 *
321 * \return If \a barrier is in the past, this function requests a minimal
322 * timeout and returns zero. Otherwise it returns one.
323 *
324 * \sa sched_min_delay(), sched_request_barrier().
325 */
326 int sched_request_barrier_or_min_delay(struct timeval *barrier, struct sched *s)
327 {
328 struct timeval diff;
329
330 if (tv_diff(now, barrier, &diff) > 0) {
331 sched_min_delay(s);
332 return 0;
333 }
334 sched_request_timeout(&diff, s);
335 return 1;
336 }