- Move code to manage step_by_step or continuous simulation from
ConcreteSimulation to TextSimulation git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@946 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
parent
1087d45628
commit
645156e62c
|
@ -22,7 +22,6 @@
|
||||||
#include "simulation_observer.hh"
|
#include "simulation_observer.hh"
|
||||||
#include "scheduler.hh"
|
#include "scheduler.hh"
|
||||||
#include "cpu_policies_gatekeeper.hh"
|
#include "cpu_policies_gatekeeper.hh"
|
||||||
#include <glibmm/timer.h>
|
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
|
@ -36,34 +35,21 @@
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace sgpem;
|
using namespace sgpem;
|
||||||
using namespace memory;
|
using namespace memory;
|
||||||
using Glib::usleep;
|
|
||||||
|
|
||||||
ConcreteSimulation::ConcreteSimulation() :
|
ConcreteSimulation::ConcreteSimulation() :
|
||||||
_state(state_stopped), _mode(true), _timer_interval(1000), _policy(NULL)
|
_state(state_stopped), _mode(mode_continuous), _policy(NULL)
|
||||||
{
|
{
|
||||||
_notify = false;
|
_notify = false;
|
||||||
_front = 0;
|
_front = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
ConcreteSimulation::set_timer(unsigned int t)
|
ConcreteSimulation::set_mode(const mode new_mode)
|
||||||
{
|
{
|
||||||
_timer_interval = t;
|
_mode = new_mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
Simulation::mode
|
||||||
ConcreteSimulation::get_timer() const
|
|
||||||
{
|
|
||||||
return _timer_interval;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
ConcreteSimulation::set_mode(const bool& b)
|
|
||||||
{
|
|
||||||
_mode = b;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool
|
|
||||||
ConcreteSimulation::get_mode() const
|
ConcreteSimulation::get_mode() const
|
||||||
{
|
{
|
||||||
return _mode;
|
return _mode;
|
||||||
|
@ -72,7 +58,6 @@ ConcreteSimulation::get_mode() const
|
||||||
void
|
void
|
||||||
ConcreteSimulation::jump_to(History::position p)
|
ConcreteSimulation::jump_to(History::position p)
|
||||||
{
|
{
|
||||||
|
|
||||||
switch (_state)
|
switch (_state)
|
||||||
{
|
{
|
||||||
case state_running:
|
case state_running:
|
||||||
|
@ -88,26 +73,36 @@ ConcreteSimulation::jump_to(History::position p)
|
||||||
|
|
||||||
pause();
|
pause();
|
||||||
|
|
||||||
|
// Disable updates to registered observers
|
||||||
|
_history.set_notify_enabled(false);
|
||||||
|
|
||||||
bool yet_to_finish = true;
|
bool yet_to_finish = true;
|
||||||
while (yet_to_finish && p > _front)
|
while (yet_to_finish && p > _front)
|
||||||
yet_to_finish = step();
|
yet_to_finish = step();
|
||||||
|
|
||||||
if (!yet_to_finish)
|
if (!yet_to_finish)
|
||||||
stop();
|
stop();
|
||||||
_front = p < _front ? p : _front;
|
_front = std::min(p, _front);
|
||||||
notify_change();
|
|
||||||
|
// Reenables updates to registered observers.
|
||||||
|
// Calls _history.notify_change() on reactivation.
|
||||||
|
_history.set_notify_enabled(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
ConcreteSimulation::pause()
|
ConcreteSimulation::pause()
|
||||||
{
|
{
|
||||||
|
if(_state != state_paused)
|
||||||
|
notify_change();
|
||||||
_state = state_paused;
|
_state = state_paused;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
ConcreteSimulation::stop()
|
ConcreteSimulation::stop()
|
||||||
{
|
{
|
||||||
|
if(_state != state_stopped)
|
||||||
|
notify_change();
|
||||||
_state = state_stopped;
|
_state = state_stopped;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -128,66 +123,12 @@ ConcreteSimulation::run() throw(UserInterruptException, NullPolicyException, Mal
|
||||||
|
|
||||||
_state = state_running;
|
_state = state_running;
|
||||||
|
|
||||||
if (get_policy() == NULL)
|
|
||||||
{
|
|
||||||
stop();
|
|
||||||
throw NullPolicyException("no policy selected");
|
|
||||||
}
|
|
||||||
|
|
||||||
//******* CONTINUOUS TIME
|
|
||||||
if (_mode)
|
|
||||||
{
|
|
||||||
do
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
//step forward
|
//step forward
|
||||||
bool yet_to_finish = true;
|
bool yet_to_finish = step();
|
||||||
if (_front == get_history().get_size() - 1)
|
|
||||||
yet_to_finish = Scheduler::get_instance().step_forward(_history, *get_policy());
|
|
||||||
if (!yet_to_finish) stop();
|
|
||||||
_front++;
|
|
||||||
notify_change();
|
|
||||||
|
|
||||||
//sleep
|
|
||||||
Glib::usleep(_timer_interval*1000);
|
|
||||||
}
|
|
||||||
catch (const CPUPolicyException& e)
|
|
||||||
{
|
|
||||||
stop();
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
|
|
||||||
//check the state
|
|
||||||
if (_state == state_stopped || _state == state_paused)
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
while (true);
|
|
||||||
}
|
|
||||||
|
|
||||||
//******* STEP by STEP
|
|
||||||
else
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
assert(get_policy() != NULL);
|
|
||||||
//step forward
|
|
||||||
bool yet_to_finish = true;
|
|
||||||
if (_front == get_history().get_size() - 1)
|
|
||||||
yet_to_finish = Scheduler::get_instance().step_forward(_history, *get_policy());
|
|
||||||
_front++;
|
|
||||||
notify_change();
|
|
||||||
if (yet_to_finish)
|
if (yet_to_finish)
|
||||||
pause();
|
pause();
|
||||||
else
|
else
|
||||||
stop();
|
stop();
|
||||||
}
|
|
||||||
catch (const CPUPolicyException& e)
|
|
||||||
{
|
|
||||||
stop();
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
|
@ -202,7 +143,6 @@ ConcreteSimulation::step()
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
assert(get_policy() != NULL);
|
|
||||||
//step forward
|
//step forward
|
||||||
bool yet_to_finish = true;
|
bool yet_to_finish = true;
|
||||||
if (_front == get_history().get_size() - 1)
|
if (_front == get_history().get_size() - 1)
|
||||||
|
|
|
@ -50,13 +50,9 @@ namespace sgpem
|
||||||
|
|
||||||
bool step() throw(UserInterruptException, NullPolicyException, MalformedPolicyException);
|
bool step() throw(UserInterruptException, NullPolicyException, MalformedPolicyException);
|
||||||
|
|
||||||
void set_timer(const unsigned int);
|
void set_mode(mode new_mode);
|
||||||
|
|
||||||
int get_timer() const;
|
mode get_mode() const;
|
||||||
|
|
||||||
void set_mode(const bool&);
|
|
||||||
|
|
||||||
bool get_mode() const;
|
|
||||||
|
|
||||||
state get_state() const;
|
state get_state() const;
|
||||||
|
|
||||||
|
@ -74,8 +70,7 @@ namespace sgpem
|
||||||
|
|
||||||
private:
|
private:
|
||||||
state _state;
|
state _state;
|
||||||
bool _mode;
|
mode _mode;
|
||||||
int _timer_interval;
|
|
||||||
ConcreteHistory _history;
|
ConcreteHistory _history;
|
||||||
CPUPolicy* _policy;
|
CPUPolicy* _policy;
|
||||||
};
|
};
|
||||||
|
|
|
@ -72,6 +72,12 @@ namespace sgpem
|
||||||
state_stopped
|
state_stopped
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum mode
|
||||||
|
{
|
||||||
|
mode_step_by_step = 0,
|
||||||
|
mode_continuous = 1
|
||||||
|
};
|
||||||
|
|
||||||
virtual ~Simulation();
|
virtual ~Simulation();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -109,21 +115,6 @@ namespace sgpem
|
||||||
virtual void jump_to(History::position p) = 0;
|
virtual void jump_to(History::position p) = 0;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
\brief Setter for the attribute timer_interval.
|
|
||||||
|
|
||||||
This method is used to define how a single time unit is to be
|
|
||||||
interpreted when the simulation advancement mode is continue.
|
|
||||||
The input value is in milliseconds, and it must be in range [0, 10000].
|
|
||||||
*/
|
|
||||||
virtual void set_timer(unsigned int) = 0;
|
|
||||||
|
|
||||||
/**
|
|
||||||
\see set_timer()
|
|
||||||
*/
|
|
||||||
virtual int get_timer() const = 0;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
\brief This methods allows to change the way the simulation progresses.
|
\brief This methods allows to change the way the simulation progresses.
|
||||||
|
|
||||||
|
@ -133,13 +124,13 @@ namespace sgpem
|
||||||
waiting the time defined with set_timer() between each step, until all
|
waiting the time defined with set_timer() between each step, until all
|
||||||
processes have terminated, or some error happens.
|
processes have terminated, or some error happens.
|
||||||
*/
|
*/
|
||||||
virtual void set_mode(const bool&) = 0;
|
virtual void set_mode(const mode) = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
\return The simulation advancement mode: 0 if step-to-step, 1 if
|
\return The simulation advancement mode: 0 if step-to-step, 1 if
|
||||||
continue.
|
continue.
|
||||||
*/
|
*/
|
||||||
virtual bool get_mode() const = 0;
|
virtual mode get_mode() const = 0;
|
||||||
|
|
||||||
virtual state get_state() const = 0;
|
virtual state get_state() const = 0;
|
||||||
|
|
||||||
|
|
|
@ -234,6 +234,7 @@ GuiBuilder::GuiBuilder(const std::string& gladefile)
|
||||||
toolbt_start->signal_clicked().connect(sigc::bind(sigc::mem_fun(*toolbt_pause, &ToolButton::set_sensitive), true));
|
toolbt_start->signal_clicked().connect(sigc::bind(sigc::mem_fun(*toolbt_pause, &ToolButton::set_sensitive), true));
|
||||||
toolbt_start->signal_clicked().connect(sigc::bind(sigc::mem_fun(*toolbt_start, &ToolButton::set_sensitive), false));
|
toolbt_start->signal_clicked().connect(sigc::bind(sigc::mem_fun(*toolbt_start, &ToolButton::set_sensitive), false));
|
||||||
|
|
||||||
|
toolbt_pause->signal_clicked().connect(sigc::bind(sigc::mem_fun(*toolbt_start, &ToolButton::set_sensitive), true));
|
||||||
toolbt_pause->signal_clicked().connect(sigc::bind(sigc::mem_fun(*toolbt_pause, &ToolButton::set_sensitive), false));
|
toolbt_pause->signal_clicked().connect(sigc::bind(sigc::mem_fun(*toolbt_pause, &ToolButton::set_sensitive), false));
|
||||||
|
|
||||||
toolbt_stop->signal_clicked().connect(sigc::bind(sigc::mem_fun(*toolbt_stop, &ToolButton::set_sensitive), false));
|
toolbt_stop->signal_clicked().connect(sigc::bind(sigc::mem_fun(*toolbt_stop, &ToolButton::set_sensitive), false));
|
||||||
|
|
|
@ -563,8 +563,7 @@ main(int argc, char** argv)
|
||||||
simu.set_policy(pol);
|
simu.set_policy(pol);
|
||||||
info << "policy activated \n";
|
info << "policy activated \n";
|
||||||
|
|
||||||
simu.set_mode(false);
|
simu.set_mode(Simulation::mode_step_by_step);
|
||||||
simu.set_timer(0);
|
|
||||||
info << "simulation set_mode single step\n";
|
info << "simulation set_mode single step\n";
|
||||||
|
|
||||||
info << "START environment dump \n";
|
info << "START environment dump \n";
|
||||||
|
|
|
@ -431,8 +431,7 @@ main(int argc, char** argv)
|
||||||
simu.set_policy(pol);
|
simu.set_policy(pol);
|
||||||
info << "policy activated \n";
|
info << "policy activated \n";
|
||||||
|
|
||||||
simu.set_mode(false);
|
simu.set_mode(Simulation::mode_step_by_step);
|
||||||
simu.set_timer(0);
|
|
||||||
info << "simulation set_mode single step\n";
|
info << "simulation set_mode single step\n";
|
||||||
|
|
||||||
info << "START environment dump \n";
|
info << "START environment dump \n";
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
// along with SGPEMv2; if not, write to the Free Software
|
// along with SGPEMv2; if not, write to the Free Software
|
||||||
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
|
||||||
|
#include "backend/global_preferences.hh"
|
||||||
#include "backend/string_utils.hh"
|
#include "backend/string_utils.hh"
|
||||||
#include "backend/cpu_policies_gatekeeper.hh"
|
#include "backend/cpu_policies_gatekeeper.hh"
|
||||||
#include "backend/cpu_policy_manager.hh"
|
#include "backend/cpu_policy_manager.hh"
|
||||||
|
@ -26,16 +27,17 @@
|
||||||
#include "backend/simulation.hh"
|
#include "backend/simulation.hh"
|
||||||
#include "backend/serializers_gatekeeper.hh"
|
#include "backend/serializers_gatekeeper.hh"
|
||||||
#include "backend/serializer.hh"
|
#include "backend/serializer.hh"
|
||||||
#include "backend/static_process.hh"
|
#include "backend/process.hh"
|
||||||
#include "backend/static_resource.hh"
|
#include "backend/resource.hh"
|
||||||
#include "backend/static_thread.hh"
|
#include "backend/thread.hh"
|
||||||
#include "backend/static_request.hh"
|
#include "backend/request.hh"
|
||||||
#include "backend/static_sub_request.hh"
|
#include "backend/sub_request.hh"
|
||||||
#include "backend/concrete_history.hh"
|
#include "backend/ready_queue.hh"
|
||||||
#include "backend/concrete_simulation.hh"
|
|
||||||
|
|
||||||
#include "text_simulation.hh"
|
#include "text_simulation.hh"
|
||||||
|
|
||||||
|
#include <glibmm/timer.h>
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <ios>
|
#include <ios>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
|
@ -349,7 +351,22 @@ TextSimulation::on_run(const Tokens& arguments)
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Simulation::get_instance().run();
|
Simulation& sim = Simulation::get_instance();
|
||||||
|
switch(sim.get_mode())
|
||||||
|
{
|
||||||
|
case Simulation::mode_step_by_step:
|
||||||
|
sim.run();
|
||||||
|
break;
|
||||||
|
case Simulation::mode_continuous:
|
||||||
|
int interval = GlobalPreferences::get_instance().get_speed() * 1000;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
sim.run();
|
||||||
|
Glib::usleep(interval);
|
||||||
|
}
|
||||||
|
while(sim.get_state() != Simulation::state_stopped);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (const UserInterruptException& e)
|
catch (const UserInterruptException& e)
|
||||||
{
|
{
|
||||||
|
@ -671,7 +688,7 @@ TextSimulation::on_get(const Tokens& arguments)
|
||||||
if (attr == "SIMULATION-TICK")
|
if (attr == "SIMULATION-TICK")
|
||||||
{
|
{
|
||||||
ostringstream oss;
|
ostringstream oss;
|
||||||
oss << "simulation-tick = " << Simulation::get_instance().get_timer() << "ms" << endl;
|
oss << "simulation-tick = " << GlobalPreferences::get_instance().get_speed() << "ms" << endl;
|
||||||
p_stdout(oss.str());
|
p_stdout(oss.str());
|
||||||
}
|
}
|
||||||
else if (attr == "CONTINUOUS")
|
else if (attr == "CONTINUOUS")
|
||||||
|
@ -712,7 +729,7 @@ TextSimulation::on_set(const Tokens& arguments)
|
||||||
if (timer < 0)
|
if (timer < 0)
|
||||||
throw domain_error("");
|
throw domain_error("");
|
||||||
|
|
||||||
Simulation::get_instance().set_timer(timer);
|
GlobalPreferences::get_instance().set_speed(timer);
|
||||||
}
|
}
|
||||||
catch (domain_error e)
|
catch (domain_error e)
|
||||||
{
|
{
|
||||||
|
@ -769,9 +786,10 @@ TextSimulation::on_set(const Tokens& arguments)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
bool continuous = string_to<bool>(value);
|
if(string_to<bool>(value) == true)
|
||||||
|
Simulation::get_instance().set_mode(Simulation::mode_continuous);
|
||||||
Simulation::get_instance().set_mode(continuous);
|
else
|
||||||
|
Simulation::get_instance().set_mode(Simulation::mode_step_by_step);
|
||||||
}
|
}
|
||||||
catch (domain_error e)
|
catch (domain_error e)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue