00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 #include "System.h"
00052
00053 namespace DREAM
00054 {
00055
00056 System::System ()
00057 : active_ (true),
00058 time_ (0),
00059 relevant_event_ (false)
00060 {
00061 #ifdef DREAM_RACE_CONDITION
00062 priority_list_ptr_ = new DREAM::PriorityInversionList (this);
00063 #endif
00064 }
00065
00066 System::~System ()
00067 {
00068 std::for_each (scheduler_map_.begin (), scheduler_map_.end (), delete_pair ());
00069
00070 #ifdef DREAM_RACE_CONDITION
00071 delete priority_list_ptr_;
00072 #endif
00073 }
00074
00075 void System::add_scheduler (DREAM::Scheduler* scheduler_ptr)
00076 {
00077 scheduler_map_.insert (DREAM::SCHEDULER_PAIR (scheduler_ptr->id (), scheduler_ptr));
00078 }
00079
00080 #ifdef DREAM_RACE_CONDITION
00081 void System::destroy_priority_list ()
00082 {
00083 priority_list_ptr_->destroy ();
00084 }
00085 #endif
00086
00087 uint System::CPUs () const
00088 {
00089 DREAM::SCHEDULER_MAP::const_iterator scheduler_iter;
00090 uint CPUs = 0;
00091
00092 for (scheduler_iter = scheduler_map_.begin (); scheduler_iter != scheduler_map_.end (); scheduler_iter++)
00093 {
00094 CPUs += scheduler_iter->second->CPUs ();
00095 }
00096
00097 return CPUs;
00098 }
00099
00100 #ifdef DREAM_RACE_CONDITION
00101 DREAM::Task* System::find_current (const DREAM::TASK_AVLTREE* task_avltree_ptr, DREAM::Task* task_ptr) const
00102 #ifdef DREAM_ENHANCED_EXCEPTION_CHECKING
00103 throw (DREAM::Exception)
00104 #endif
00105 {
00106 return (priority_list_ptr_->find_current (task_avltree_ptr, task_ptr));
00107 }
00108
00109 bool System::inversion ()
00110 {
00111 return priority_list_ptr_->inversion ();
00112 }
00113 #endif
00114
00115 double System::next_event (bool deterministic) const
00116 #ifdef DREAM_ENHANCED_EXCEPTION_CHECKING
00117 throw (DREAM::Exception)
00118 #endif
00119 {
00120 DREAM::SCHEDULER_MAP::const_iterator scheduler_iter;
00121 double step, temp;
00122
00123 step = 0.0;
00124
00125 for (scheduler_iter = scheduler_map_.begin (); scheduler_iter != scheduler_map_.end (); scheduler_iter++)
00126 {
00127 temp = scheduler_iter->second->next_event (deterministic);
00128
00129 if ((step == 0.0) || ((temp < step) && (0.0 < temp)))
00130 step = temp;
00131 }
00132
00133 return step;
00134 }
00135
00136 inline bool System::relevant_event () const
00137 {
00138 return relevant_event_;
00139 }
00140
00141 inline void System::relevant_event (bool value)
00142 {
00143 relevant_event_ = value;
00144 }
00145
00146 void System::reset ()
00147 {
00148 std::for_each (scheduler_map_.begin (), scheduler_map_.end (), reset_pair ());
00149 time_ = 0;
00150 }
00151
00152 inline const DREAM::SCHEDULER_MAP* System::scheduler_map ()
00153 #ifdef DREAM_ENHANCED_EXCEPTION_CHECKING
00154 throw (DREAM::Exception)
00155 #endif
00156 {
00157 return &scheduler_map_;
00158 }
00159
00160 void System::simulate (bool verbose, bool deterministic, double* endtoend)
00161 {
00162 if (verbose)
00163 if (deterministic)
00164 std::cout << std::endl << "Deterministic distributed discrete event simulation." << std::endl << std::endl;
00165 else
00166 std::cout << std::endl << "Non-deterministic distributed discrete event simulation." << std::endl << std::endl;
00167
00168 DREAM::SCHEDULER_MAP::iterator scheduler_iter;
00169
00170 for (scheduler_iter = scheduler_map_.begin (); scheduler_iter != scheduler_map_.end (); scheduler_iter++)
00171 scheduler_iter->second->verbose (verbose);
00172
00173 active_ = true;
00174 double step, temp, exec_time = 0.0;
00175
00176 while (active_)
00177 {
00178 step = 0.0;
00179
00180 for (scheduler_iter = scheduler_map_.begin (); scheduler_iter != scheduler_map_.end (); scheduler_iter++)
00181 {
00182 temp = scheduler_iter->second->next_event (deterministic);
00183
00184 if ((step == 0.0) || ((0.0 < temp) && (temp < step)))
00185 step = temp;
00186 }
00187
00188 time_ += step;
00189
00190 if (time_ < Option::simulation_time_)
00191 exec_time += step;
00192
00193 for (scheduler_iter = scheduler_map_.begin (); scheduler_iter != scheduler_map_.end (); scheduler_iter++)
00194 scheduler_iter->second->time_step (step);
00195
00196 relevant_event_ = false;
00197
00198 std::for_each (scheduler_map_.begin (), scheduler_map_.end (), transition_pair ());
00199
00200 for (scheduler_iter = scheduler_map_.begin (); scheduler_iter != scheduler_map_.end (); scheduler_iter++)
00201 scheduler_iter->second->schedule (deterministic);
00202
00203 if ((time_ >= Option::simulation_time_) || (step == 0.0))
00204 {
00205 active_ = false;
00206
00207 std::for_each (scheduler_map_.begin (), scheduler_map_.end (), stop_pair ());
00208
00209 if (endtoend != NULL)
00210 *endtoend = exec_time;
00211
00212 if (verbose)
00213 std::cout << "Simulation ended. End-to-end execution time: " << exec_time << std::endl;
00214 }
00215 }
00216 }
00217
00218 void System::visitor_error_avltree (DREAM::TASK_AVLTREE* task_avltree)
00219 #ifdef DREAM_ENHANCED_EXCEPTION_CHECKING
00220 throw (DREAM::Exception)
00221 #endif
00222 {
00223 DREAM::SCHEDULER_MAP::iterator scheduler_iter;
00224
00225 for (scheduler_iter = scheduler_map_.begin (); scheduler_iter != scheduler_map_.end (); scheduler_iter++)
00226 {
00227 scheduler_iter->second->visitor_error_avltree (task_avltree);
00228 }
00229 }
00230
00231 void System::visitor_if (DREAM::NODE_MAP* task_map, DREAM::NODE_MAP* channel_map, DREAM::NODE_MAP* timer_map, std::ofstream& f_stream)
00232 #ifdef DREAM_ENHANCED_EXCEPTION_CHECKING
00233 throw (DREAM::Exception)
00234 #endif
00235 {
00236 DREAM::SCHEDULER_MAP::iterator scheduler_iter;
00237
00238 for (scheduler_iter = scheduler_map_.begin (); scheduler_iter != scheduler_map_.end (); scheduler_iter++)
00239 {
00240 scheduler_iter->second->visitor_if (task_map, channel_map, timer_map, f_stream);
00241 }
00242 }
00243
00244 void System::visitor_map (DREAM::NODE_MAP* task_map, DREAM::NODE_MAP* channel_map, DREAM::NODE_MAP* timer_map)
00245 #ifdef DREAM_ENHANCED_EXCEPTION_CHECKING
00246 throw (DREAM::Exception)
00247 #endif
00248 {
00249 DREAM::SCHEDULER_MAP::iterator scheduler_iter;
00250
00251 for (scheduler_iter = scheduler_map_.begin (); scheduler_iter != scheduler_map_.end (); scheduler_iter++)
00252 {
00253 scheduler_iter->second->visitor_map (task_map, channel_map, timer_map);
00254 }
00255 }
00256
00257 void System::visitor_task_avltree (DREAM::TASK_AVLTREE* task_avltree)
00258 #ifdef DREAM_ENHANCED_EXCEPTION_CHECKING
00259 throw (DREAM::Exception)
00260 #endif
00261 {
00262 DREAM::SCHEDULER_MAP::iterator scheduler_iter;
00263
00264 for (scheduler_iter = scheduler_map_.begin (); scheduler_iter != scheduler_map_.end (); scheduler_iter++)
00265 {
00266 scheduler_iter->second->visitor_task_avltree (task_avltree);
00267 }
00268 }
00269
00270 void System::visitor_update_task_avltree (DREAM::TASK_AVLTREE* task_avltree)
00271 #ifdef DREAM_ENHANCED_EXCEPTION_CHECKING
00272 throw (DREAM::Exception)
00273 #endif
00274 {
00275 DREAM::SCHEDULER_MAP::iterator scheduler_iter;
00276
00277 for (scheduler_iter = scheduler_map_.begin (); scheduler_iter != scheduler_map_.end (); scheduler_iter++)
00278 {
00279 scheduler_iter->second->visitor_update_task_avltree (task_avltree);
00280 }
00281 }
00282
00283 void System::visitor_uppaal (DREAM::NODE_MAP* task_map, DREAM::NODE_MAP* channel_map, DREAM::NODE_MAP* timer_map, std::ofstream& f_stream)
00284 #ifdef DREAM_ENHANCED_EXCEPTION_CHECKING
00285 throw (DREAM::Exception)
00286 #endif
00287 {
00288 DREAM::SCHEDULER_MAP::iterator scheduler_iter;
00289
00290 for (scheduler_iter = scheduler_map_.begin (); scheduler_iter != scheduler_map_.end (); scheduler_iter++)
00291 {
00292 scheduler_iter->second->visitor_uppaal (task_map, channel_map, timer_map, f_stream);
00293 }
00294 }
00295
00296 }
00297