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