-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.cpp
306 lines (266 loc) · 8.69 KB
/
task.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author : Richard GAYRAUD - 04 Nov 2003
* Olivier Jacques
* From Hewlett Packard Company.
* Shriram Natarajan
* Peter Higginson
* Eric Miller
* Venkatesh
* Enrico Hartung
* Nasir Khan
* Lee Ballard
* Guillaume Teissier from FTR&D
* Wolfgang Beck
* Venkatesh
* Vlad Troyanker
* Charles P Wright from IBM Research
* Amit On from Followap
* Jan Andres from Freenet
* Ben Evans from Open Cloud
* Marc Van Diest from Belgacom
* Michael Dwyer from Cibation
*/
#include <iterator>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <sys/types.h>
#include <sys/wait.h>
#include <assert.h>
#include "sipp.hpp"
task_list all_tasks;
task_list running_tasks;
timewheel paused_tasks;
/* Get the overall list of running tasks. */
task_list* get_running_tasks()
{
return &running_tasks;
}
void abort_all_tasks()
{
for (task_list::iterator task_it = all_tasks.begin();
task_it != all_tasks.end();
task_it = all_tasks.begin()) {
(*task_it)->abort();
}
}
void dump_tasks()
{
WARNING("---- %zu Active Tasks ----", all_tasks.size());
for (task_list::iterator task_it = all_tasks.begin();
task_it != all_tasks.end();
task_it++) {
(*task_it)->dump();
}
}
int expire_paused_tasks()
{
return paused_tasks.expire_paused_tasks();
}
int paused_tasks_count()
{
return paused_tasks.size();
}
// Methods for the task class
task::task()
{
this->taskit = all_tasks.insert(all_tasks.end(), this);
add_to_runqueue();
}
task::~task()
{
if (running) {
remove_from_runqueue();
} else {
paused_tasks.remove_paused_task(this);
}
all_tasks.erase(taskit);
}
/* Put this task in the run queue. */
void task::add_to_runqueue()
{
this->runit = running_tasks.insert(running_tasks.end(), this);
this->running = true;
}
void task::add_to_paused_tasks(bool increment)
{
paused_tasks.add_paused_task(this, increment);
}
void task::recalculate_wheel()
{
add_to_paused_tasks(false);
}
/* Remove this task from the run queue. */
bool task::remove_from_runqueue()
{
if (!this->running) {
return false;
}
running_tasks.erase(this->runit);
this->running = false;
return true;
}
void task::setRunning()
{
if (!running) {
paused_tasks.remove_paused_task(this);
add_to_runqueue();
}
}
void task::setPaused()
{
if (running) {
if (!remove_from_runqueue()) {
WARNING("Tried to remove a running call that wasn't running!");
assert(0);
}
} else {
paused_tasks.remove_paused_task(this);
}
assert(running == false);
add_to_paused_tasks(true);
}
void task::abort()
{
delete this;
}
// Methods for the timewheel class
// Based on the time a given task should next be woken up, finds the
// correct time wheel for it and returns a list of other tasks
// occurring at that point.
task_list *timewheel::task2list(task *task)
{
unsigned int wake = task->wake();
if (wake == 0) {
return &forever_list;
}
assert(wake >= wheel_base);
if (wheel_base > clock_tick) {
ERROR("wheel_base is %lu, clock_tick is %lu - expected wheel_base to be less than or equal to clock_tick", wheel_base, clock_tick);
assert(wheel_base <= clock_tick);
}
unsigned int slot_in_first_wheel = wake % LEVEL_ONE_SLOTS;
unsigned int slot_in_second_wheel = (wake / LEVEL_ONE_SLOTS) % LEVEL_TWO_SLOTS;
unsigned int slot_in_third_wheel = (wake / (LEVEL_ONE_SLOTS * LEVEL_TWO_SLOTS));
bool fits_in_first_wheel = ((wake / LEVEL_ONE_SLOTS) == (wheel_base / LEVEL_ONE_SLOTS));
bool fits_in_second_wheel = ((wake / (LEVEL_ONE_SLOTS * LEVEL_TWO_SLOTS)) ==
(wheel_base / (LEVEL_ONE_SLOTS * LEVEL_TWO_SLOTS)));
bool fits_in_third_wheel = (slot_in_third_wheel < LEVEL_THREE_SLOTS);
if (fits_in_first_wheel) {
return &wheel_one[slot_in_first_wheel];
} else if (fits_in_second_wheel) {
return &wheel_two[slot_in_second_wheel];
} else if (fits_in_third_wheel) {
return &wheel_three[slot_in_third_wheel];
} else{
ERROR("Attempted to schedule a task too far in the future");
return NULL;
}
}
/* Iterate through our sorted set of paused tasks, removing those that
* should no longer be paused, and adding them to the run queue. */
int timewheel::expire_paused_tasks()
{
int found = 0;
// This while loop counts up from the wheel_base (i.e. the time
// this function last ran) to the current scheduler time (i.e. clock_tick).
while (wheel_base < clock_tick) {
int slot1 = wheel_base % LEVEL_ONE_SLOTS;
/* If slot1 is 0 (i.e. wheel_base is a multiple of 4096ms),
* we need to repopulate the first timer wheel with the
* contents of the first available slot of the second wheel. */
if (slot1 == 0) {
/* slot2 represents the slot in the second timer wheel
* containing the tasks for the next ~4s. So when
* wheel_base is 4096, wheel2[1] will be moved into wheel 1,
* when wheel_base of 8192 wheel2[2] will be moved into
* wheel 1, etc. */
int slot2 = (wheel_base / LEVEL_ONE_SLOTS) % LEVEL_TWO_SLOTS;
/* If slot2 is also zero, we must migrate tasks from slot3 into slot2. */
if (slot2 == 0) {
/* Same logic above, except that each slot of wheel3
contains the next 69 minutes of tasks, enough to
completely fill wheel 2. */
int slot3 = ((wheel_base / LEVEL_ONE_SLOTS) / LEVEL_TWO_SLOTS);
assert(slot3 < LEVEL_THREE_SLOTS);
for (task_list::iterator l3it = wheel_three[slot3].begin();
l3it != wheel_three[slot3].end();
l3it++) {
/* Migrate this task to wheel two. */
(*l3it)->recalculate_wheel();
}
wheel_three[slot3].clear();
}
/* Repopulate wheel 1 from wheel 2 (which will now be full
of the tasks pulled from wheel 3, if that was
necessary) */
for (task_list::iterator l2it = wheel_two[slot2].begin();
l2it != wheel_two[slot2].end();
l2it++) {
/* Migrate this task to wheel one. */
(*l2it)->recalculate_wheel();
}
wheel_two[slot2].clear();
}
/* Move tasks from the current slot of wheel 1 (i.e. the tasks
scheduled to fire in the 1ms interval represented by
wheel_base) onto a run queue. */
found += wheel_one[slot1].size();
for(task_list::iterator it = wheel_one[slot1].begin();
it != wheel_one[slot1].end(); it++) {
(*it)->add_to_runqueue();
// Decrement the total number of tasks in this wheel.
count--;
}
wheel_one[slot1].clear();
wheel_base++; // Move wheel_base to the next 1ms interval
}
return found;
}
// Adds a task to the correct timewheel. When increment is false, does
// not increment the count of tasks owned by this timewheel, and so
// can be used for recalculating the wheel of an existing task.
void timewheel::add_paused_task(task *task, bool increment)
{
task_list::iterator task_it;
if (task->wake() && task->wake() < wheel_base) {
task->add_to_runqueue();
return;
}
task_list *list = task2list(task);
task_it = list->insert(list->end(), task);
task->pauselist = list;
task->pauseit = task_it;
if (increment) {
count++;
}
}
void timewheel::remove_paused_task(task *task)
{
task_list *list = task->pauselist;
list->erase(task->pauseit);
count--;
}
timewheel::timewheel()
{
count = 0;
wheel_base = clock_tick;
}
int timewheel::size()
{
return count;
}