35858a0b480b7f683cb9fc6a0a49df086f7c5dec
[senf.git] / Scheduler / FIFORunner.cc
1 // $Id$
2 //
3 // Copyright (C) 2008 
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief FIFORunner non-inline non-template implementation */
25
26 #include "FIFORunner.hh"
27 //#include "FIFORunner.ih"
28
29 // Custom includes
30 #include <signal.h>
31 #include <time.h>
32 #include "../Utils/Exception.hh"
33 #include "../Utils/senfassert.hh"
34
35 //#include "FIFORunner.mpp"
36 #define prefix_
37 ///////////////////////////////cc.p////////////////////////////////////////
38
39 prefix_ senf::scheduler::FIFORunner::FIFORunner()
40     : tasks_ (), next_ (tasks_.end()), watchdogMs_ (1000), watchdogCount_(0), hangCount_ (0)
41 {
42     struct sigevent ev;
43     ::memset(&ev, 0, sizeof(ev));
44     ev.sigev_notify = SIGEV_SIGNAL;
45     ev.sigev_signo = SIGURG;
46     ev.sigev_value.sival_ptr = this;
47     if (timer_create(CLOCK_MONOTONIC, &ev, &watchdogId_) < 0)
48         SENF_THROW_SYSTEM_EXCEPTION("timer_create()");
49
50     struct sigaction sa;
51     ::memset(&sa, 0, sizeof(sa));
52     sa.sa_sigaction = &watchdog;
53     sa.sa_flags = SA_SIGINFO;
54     if (sigaction(SIGURG, &sa, 0) < 0)
55         SENF_THROW_SYSTEM_EXCEPTION("sigaction()");
56
57     sigset_t mask;
58     sigemptyset(&mask);
59     sigaddset(&mask, SIGURG);
60     if (sigprocmask(SIG_UNBLOCK, &mask, 0) < 0)
61         SENF_THROW_SYSTEM_EXCEPTION("sigprocmask()");
62 }
63
64 prefix_ senf::scheduler::FIFORunner::~FIFORunner()
65 {
66     timer_delete(watchdogId_);
67     signal(SIGURG, SIG_DFL);
68 }
69
70 // At the moment, the FIFORunner is not very efficient with many non-runnable tasks since the
71 // complete list of tasks is traversed on each run().
72 //
73 // To optimize this, we woould need a way to find the relative ordering of two tasks in O(1) (at the
74 // moment, this is an O(N) operation by traversing the list).
75 //
76 // One idea is, to give each task an 'order' value. Whenever a task is added at the end, it's order
77 // value is set to the order value of the last task + 1. Whenever the order value such added exceeds
78 // some threshold (e.g. 2^31 -1 or some such), the task list is traversed from beginning to end to
79 // assign new consecutive order values. This O(N) operation is so seldom, that it is amortized over
80 // a very long time.
81 //
82 // With this value at hand, we can do several optimizations: One idea would be the following: The
83 // runnable set always has two types of tasks: There are tasks, which are heavily active and are
84 // signaled constantly and other tasks which lie dormant most of the time. Those dormant tasks will
85 // end up at the beginning of the task queue.
86 //
87 // With the above defined 'ordering' field available, we can manage an iterator pointing to the
88 // first and the last runnable task. This will often help a lot since the group of runnable tasks
89 // will mostly be localized to the end of the queue. only occasionally one of the dormant tasks will
90 // be runnable. This additional traversal time will be amortized over a larger time.
91
92 prefix_ void senf::scheduler::FIFORunner::dequeue(TaskInfo * task)
93 {
94     TaskList::iterator i (TaskList::current(*task));
95     if (next_ == i)
96         ++next_;
97     tasks_.erase(i);
98 }
99
100 namespace {
101     struct NullTask 
102         : public senf::scheduler::FIFORunner::TaskInfo
103     {
104         NullTask() : senf::scheduler::FIFORunner::TaskInfo ("<null>") {}
105         void run() {};
106     };
107 }
108
109 prefix_ void senf::scheduler::FIFORunner::run()
110 {
111     // This algorithm is carefully adjusted to make it work even when arbitrary tasks are removed
112     // from the queue
113     // - Before we begin, we add a NullTask to the queue. The only purpose of this node is, to mark
114     //   the current end of the queue. The iterator to this node becomes the end iterator of the
115     //   range to process
116     // - We update the TaskInfo and move it to the end of the queue before calling the callback so
117     //   we don't access the TaskInfo if it is removed while the callback is running
118     // - We keep the next to-be-processed node in a class variable which is checked and updated
119     //   whenever a node is removed.
120     NullTask null;
121     struct itimerspec timer;
122     timer.it_interval.tv_sec = watchdogMs_ / 1000;
123     timer.it_interval.tv_nsec = (watchdogMs_ % 1000) * 1000000ul;
124     timer.it_value.tv_sec = timer.it_interval.tv_sec;
125     timer.it_value.tv_nsec = timer.it_interval.tv_nsec;
126     tasks_.push_back(null);
127     TaskList::iterator end (TaskList::current(null));
128     next_ = tasks_.begin();
129     try {
130         if (timer_settime(watchdogId_, 0, &timer, 0) < 0)
131             SENF_THROW_SYSTEM_EXCEPTION("timer_settime()");
132         while (next_ != end) {
133             TaskInfo & task (*next_);
134             if (task.runnable_) {
135                 task.runnable_ = false;
136                 runningName_ = task.name_;
137 #           ifdef SENF_DEBUG
138                 runningBacktrace_ = task.backtrace_;
139 #           endif
140                 TaskList::iterator i (next_);
141                 ++ next_;
142                 tasks_.splice(tasks_.end(), tasks_, i);
143                 watchdogCount_ = 1;
144                 task.run();
145             }
146             else
147                 ++ next_;
148         }
149     }
150     catch (...) {
151         watchdogCount_ = 0;
152         timer.it_interval.tv_sec = 0;
153         timer.it_interval.tv_nsec = 0;
154         timer.it_value.tv_sec = 0;
155         timer.it_value.tv_nsec = 0;
156         timer_settime(watchdogId_, 0, &timer, 0);
157         tasks_.erase(end);
158         next_ = tasks_.end();
159         throw;
160     }
161     watchdogCount_ = 0;
162     timer.it_interval.tv_sec = 0;
163     timer.it_interval.tv_nsec = 0;
164     timer.it_value.tv_sec = 0;
165     timer.it_value.tv_nsec = 0;
166     if (timer_settime(watchdogId_, 0, &timer, 0) < 0)
167         SENF_THROW_SYSTEM_EXCEPTION("timer_settime()");
168     tasks_.erase(end);
169     next_ = tasks_.end();
170 }
171
172 prefix_ void senf::scheduler::FIFORunner::watchdog(int, siginfo_t * si, void *)
173 {
174     FIFORunner & runner (*static_cast<FIFORunner *>(si->si_value.sival_ptr));
175     if (runner.watchdogCount_ > 0) {
176         ++ runner.watchdogCount_;
177         if (runner.watchdogCount_ > 2) {
178             ++ runner.hangCount_;
179             write(1, "\n\n*** Scheduler task hanging: ", 30);
180             write(1, runner.runningName_.c_str(), runner.runningName_.size());
181             write(1, "\n", 1);
182 #ifdef SENF_DEBUG
183             write(1, "Task was initialized at\n", 24);
184             write(1, runner.runningBacktrace_.c_str(), runner.runningBacktrace_.size());
185 #endif
186             write(1, "\n", 1);
187         }
188     }
189 }
190
191 ///////////////////////////////cc.e////////////////////////////////////////
192 #undef prefix_
193 //#include "FIFORunner.mpp"
194
195 \f
196 // Local Variables:
197 // mode: c++
198 // fill-column: 100
199 // comment-column: 40
200 // c-file-style: "senf"
201 // indent-tabs-mode: nil
202 // ispell-local-dictionary: "american"
203 // compile-command: "scons -u test"
204 // End: