]> git.tuebingen.mpg.de Git - paraslash.git/blob - sched.c
9976a11d2aa315f78ff88072c0a27d84ed35e58d
[paraslash.git] / sched.c
1 /*
2  * Copyright (C) 2006-2008 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 <dirent.h> /* readdir() */
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 "error.h"
20
21 static struct list_head pre_select_list, post_select_list;
22 static int initialized;
23
24 static struct timeval now_struct;
25 struct timeval *now = &now_struct;
26
27 /**
28  * Remove a task from the scheduler.
29  *
30  * \param t the task to remove
31  *
32  * If the pre_select pointer of \a t is not \p NULL, it is removed from
33  * the pre_select list of the scheduler. Same goes for \a post_select.
34  */
35 void unregister_task(struct task *t)
36 {
37         if (!initialized)
38                 return;
39         PARA_INFO_LOG("unregistering %s (%p)\n", t->status, t);
40         if (t->pre_select)
41                 list_del(&t->pre_select_node);
42         if (t->post_select)
43                 list_del(&t->post_select_node);
44 };
45
46
47 static void sched_preselect(struct sched *s)
48 {
49         struct task *t, *tmp;
50         list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
51                 t->pre_select(s, t);
52 //              PARA_INFO_LOG("%s \n", t->status);
53                 if (t->error >= 0)
54                         continue;
55                 unregister_task(t);
56         }
57 }
58
59 static void sched_post_select(struct sched *s)
60 {
61         struct task *t, *tmp;
62
63         list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
64                 t->post_select(s, t);
65 //              PARA_INFO_LOG("%s: %d\n", t->status, t->ret);
66                 if (t->error >= 0)
67                         continue;
68                 unregister_task(t);
69         }
70 }
71
72 /**
73  * the core function for all paraslash programs
74  *
75  * \param s pointer to the scheduler struct
76  *
77  * This function updates the global \a now pointer, calls all registered
78  * pre_select hooks which may set the timeout and add any file descriptors to
79  * the fd sets of \a s.  Next, it calls para_select() and makes the result available
80  * to the registered tasks by calling their post_select hook.
81  *
82  * \return Zero if no more tasks are left in either of the two lists, negative
83  * if para_select returned an error.
84  *
85  * \sa task, now.
86  */
87 int schedule(struct sched *s)
88 {
89         int ret;
90
91         if (!initialized)
92                 return -E_NOT_INITIALIZED;
93         gettimeofday(now, NULL);
94 again:
95         FD_ZERO(&s->rfds);
96         FD_ZERO(&s->wfds);
97         s->timeout = s->default_timeout;
98         s->max_fileno = -1;
99         sched_preselect(s);
100         ret = para_select(s->max_fileno + 1, &s->rfds, &s->wfds, &s->timeout);
101         if (ret < 0)
102                 return ret;
103         gettimeofday(now, NULL);
104         sched_post_select(s);
105         if (list_empty(&pre_select_list) && list_empty(&post_select_list))
106                 return 0;
107         goto again;
108 }
109
110 /**
111  * initialize the paraslash scheduler
112  */
113 static void init_sched(void)
114 {
115         PARA_INFO_LOG("initializing scheduler\n");
116         INIT_LIST_HEAD(&pre_select_list);
117         INIT_LIST_HEAD(&post_select_list);
118         initialized = 1;
119 };
120
121 /**
122  * add a task to the scheduler
123  *
124  * \param t the task to add
125  *
126  * If the pre_select pointer of \a t is not \p NULL, it is added to
127  * the pre_select list of the scheduler. Same goes for post_select.
128  *
129  * \sa task::pre_select, task::post_select
130  */
131 void register_task(struct task *t)
132 {
133         if (!initialized)
134                 init_sched();
135         PARA_INFO_LOG("registering %s (%p)\n", t->status, t);
136         if (t->pre_select) {
137                 PARA_DEBUG_LOG("pre_select: %p\n", &t->pre_select);
138                 list_add_tail(&t->pre_select_node, &pre_select_list);
139         }
140         if (t->post_select) {
141                 PARA_DEBUG_LOG("post_select: %p\n", &t->pre_select);
142                 list_add_tail(&t->post_select_node, &post_select_list);
143         }
144 }
145
146 /**
147  * unregister all tasks
148  *
149  * This will cause \a schedule() to return immediately because both the
150  * \a pre_select_list and the \a post_select_list are empty.
151  */
152 void sched_shutdown(void)
153 {
154         struct task *t, *tmp;
155
156         if (!initialized)
157                 return;
158         list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node)
159                 unregister_task(t);
160         /* remove tasks which do not have a pre_select hook */
161         list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node)
162                 unregister_task(t);
163         initialized = 0;
164 };
165
166 /**
167  * get the list of all registered tasks.
168  *
169  * \return the task list
170  *
171  * Each entry of the list contains an identifier which is simply a hex number
172  * that may be used in \a kill_task() to terminate the task.
173  * The result ist dynamically allocated and must be freed by the caller.
174  */
175 char *get_task_list(void)
176 {
177         struct task *t, *tmp;
178         char *msg = NULL;
179
180         if (!initialized)
181                 return NULL;
182         list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
183                 char *tmp_msg;
184                 tmp_msg = make_message("%s%p\tpre\t%s\n", msg? msg : "", t, t->status);
185                 free(msg);
186                 msg = tmp_msg;
187         }
188         list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
189                 char *tmp_msg;
190 //              if (t->pre_select)
191 //                      continue;
192                 tmp_msg = make_message("%s%p\tpost\t%s\n", msg? msg : "", t, t->status);
193                 free(msg);
194                 msg = tmp_msg;
195         }
196         //PARA_DEBUG_LOG("task list:\n%s", msg);
197         return msg;
198 }
199
200 /**
201  * simulate an error for the given task
202  *
203  * \param id the task identifier
204  *
205  * Find the task identified by \a id, set the tasks' error value to
206  * \p -E_TASK_KILLED and unregister the task.
207  *
208  * \return Positive on success, negative on errors (e.g. if \a id does not
209  * correspond to a registered task).
210  */
211 int kill_task(char *id)
212 {
213         struct task *t, *tmp;
214         char buf[20];
215
216         if (!initialized)
217                 return -E_NOT_INITIALIZED;
218         list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
219                 sprintf(buf, "%p", t);
220                 if (strcmp(id, buf))
221                         continue;
222                 t->error = -E_TASK_KILLED;
223                 unregister_task(t);
224                 return 1;
225         }
226         list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
227                 sprintf(buf, "%p", t);
228                 if (strcmp(id, buf))
229                         continue;
230                 t->error = -E_TASK_KILLED;
231                 unregister_task(t);
232                 return 1;
233         }
234         return -E_NO_SUCH_TASK;
235 }