System.cpp

Go to the documentation of this file.
00001 /** @file System.cpp
00002 * @author Gabor Madl
00003 * @date Created 05/2005
00004 * @brief Distributed Real-time Embedded system model of computation.
00005 *
00006 *
00007 * =================================================================
00008 * DREAM License v2.0
00009 * 
00010 * DREAM - Distributed Real-time Embedded Analysis Method
00011 * http://dre.sourceforge.net.
00012 * Copyright (c) 2005-2007 Gabor Madl, All Rights Reserved.
00013 * 
00014 * This file is part of DREAM.
00015 * 
00016 * DREAM is free software; you can redistribute it and/or modify it
00017 * under the terms of the GNU General Public License version 2 as
00018 * published by the Free Software Foundation. No future versions of
00019 * the GPL license may be automatically applied to DREAM. It is in
00020 * the sole discretion of the copyright holder to determine whether
00021 * DREAM may be released under a different license or terms. There
00022 * are no restrictions on the use of DREAM for any purpose.
00023 * 
00024 * DREAM is distributed in the hope that it will be useful,
00025 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00026 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00027 * GNU General Public License for more details.
00028 * 
00029 * You should have received a copy of the GNU General Public License
00030 * along with this program; if not, write to the Free Software
00031 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00032 * MA 02110-1301, USA.
00033 * 
00034 * By submitting comments, suggestions, code, code snippets,
00035 * techniques (including that of usage), and algorithms, submitters
00036 * acknowledge that they have the right to do so, that any such
00037 * submissions are given freely and unreservedly, and that they
00038 * waive any claims to copyright or ownership. In addition,
00039 * submitters acknowledge that any such submission might become
00040 * part of the copyright maintained on the overall body of code,
00041 * which comprises DREAM. By making a submission, submitter agrees
00042 * to these terms. Furthermore, submitters acknowledge that the
00043 * incorporation or modification of such submissions is entirely
00044 * at the discretion of the moderators of the DREAM project.
00045 * 
00046 * DREAM links to the Libxml2 third party library. Please see 
00047 * COPYING-libxml for the copyright information of Libxml2.
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         // Reduce number of calls on verbose...
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 

Generated on Fri Jul 27 18:30:03 2007 for DREAM by  doxygen 1.5.1