- 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 "scheduler.hh"
|
||||
#include "cpu_policies_gatekeeper.hh"
|
||||
#include <glibmm/timer.h>
|
||||
|
||||
#include <cassert>
|
||||
|
||||
|
@ -36,34 +35,21 @@
|
|||
using namespace std;
|
||||
using namespace sgpem;
|
||||
using namespace memory;
|
||||
using Glib::usleep;
|
||||
|
||||
ConcreteSimulation::ConcreteSimulation() :
|
||||
_state(state_stopped), _mode(true), _timer_interval(1000), _policy(NULL)
|
||||
_state(state_stopped), _mode(mode_continuous), _policy(NULL)
|
||||
{
|
||||
_notify = false;
|
||||
_front = 0;
|
||||
}
|
||||
|
||||
void
|
||||
ConcreteSimulation::set_timer(unsigned int t)
|
||||
ConcreteSimulation::set_mode(const mode new_mode)
|
||||
{
|
||||
_timer_interval = t;
|
||||
_mode = new_mode;
|
||||
}
|
||||
|
||||
int
|
||||
ConcreteSimulation::get_timer() const
|
||||
{
|
||||
return _timer_interval;
|
||||
}
|
||||
|
||||
void
|
||||
ConcreteSimulation::set_mode(const bool& b)
|
||||
{
|
||||
_mode = b;
|
||||
}
|
||||
|
||||
bool
|
||||
Simulation::mode
|
||||
ConcreteSimulation::get_mode() const
|
||||
{
|
||||
return _mode;
|
||||
|
@ -72,7 +58,6 @@ ConcreteSimulation::get_mode() const
|
|||
void
|
||||
ConcreteSimulation::jump_to(History::position p)
|
||||
{
|
||||
|
||||
switch (_state)
|
||||
{
|
||||
case state_running:
|
||||
|
@ -88,26 +73,36 @@ ConcreteSimulation::jump_to(History::position p)
|
|||
|
||||
pause();
|
||||
|
||||
// Disable updates to registered observers
|
||||
_history.set_notify_enabled(false);
|
||||
|
||||
bool yet_to_finish = true;
|
||||
while (yet_to_finish && p > _front)
|
||||
yet_to_finish = step();
|
||||
|
||||
if (!yet_to_finish)
|
||||
stop();
|
||||
_front = p < _front ? p : _front;
|
||||
notify_change();
|
||||
_front = std::min(p, _front);
|
||||
|
||||
// Reenables updates to registered observers.
|
||||
// Calls _history.notify_change() on reactivation.
|
||||
_history.set_notify_enabled(true);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ConcreteSimulation::pause()
|
||||
{
|
||||
if(_state != state_paused)
|
||||
notify_change();
|
||||
_state = state_paused;
|
||||
}
|
||||
|
||||
void
|
||||
ConcreteSimulation::stop()
|
||||
{
|
||||
if(_state != state_stopped)
|
||||
notify_change();
|
||||
_state = state_stopped;
|
||||
}
|
||||
|
||||
|
@ -128,66 +123,12 @@ ConcreteSimulation::run() throw(UserInterruptException, NullPolicyException, Mal
|
|||
|
||||
_state = state_running;
|
||||
|
||||
if (get_policy() == NULL)
|
||||
{
|
||||
stop();
|
||||
throw NullPolicyException("no policy selected");
|
||||
}
|
||||
|
||||
//******* CONTINUOUS TIME
|
||||
if (_mode)
|
||||
{
|
||||
do
|
||||
{
|
||||
try
|
||||
{
|
||||
//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());
|
||||
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();
|
||||
bool yet_to_finish = step();
|
||||
if (yet_to_finish)
|
||||
pause();
|
||||
else
|
||||
stop();
|
||||
}
|
||||
catch (const CPUPolicyException& e)
|
||||
{
|
||||
stop();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool
|
||||
|
@ -202,7 +143,6 @@ ConcreteSimulation::step()
|
|||
|
||||
try
|
||||
{
|
||||
assert(get_policy() != NULL);
|
||||
//step forward
|
||||
bool yet_to_finish = true;
|
||||
if (_front == get_history().get_size() - 1)
|
||||
|
|
|
@ -50,13 +50,9 @@ namespace sgpem
|
|||
|
||||
bool step() throw(UserInterruptException, NullPolicyException, MalformedPolicyException);
|
||||
|
||||
void set_timer(const unsigned int);
|
||||
void set_mode(mode new_mode);
|
||||
|
||||
int get_timer() const;
|
||||
|
||||
void set_mode(const bool&);
|
||||
|
||||
bool get_mode() const;
|
||||
mode get_mode() const;
|
||||
|
||||
state get_state() const;
|
||||
|
||||
|
@ -74,8 +70,7 @@ namespace sgpem
|
|||
|
||||
private:
|
||||
state _state;
|
||||
bool _mode;
|
||||
int _timer_interval;
|
||||
mode _mode;
|
||||
ConcreteHistory _history;
|
||||
CPUPolicy* _policy;
|
||||
};
|
||||
|
|
|
@ -72,6 +72,12 @@ namespace sgpem
|
|||
state_stopped
|
||||
};
|
||||
|
||||
enum mode
|
||||
{
|
||||
mode_step_by_step = 0,
|
||||
mode_continuous = 1
|
||||
};
|
||||
|
||||
virtual ~Simulation();
|
||||
|
||||
/**
|
||||
|
@ -109,21 +115,6 @@ namespace sgpem
|
|||
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.
|
||||
|
||||
|
@ -133,13 +124,13 @@ namespace sgpem
|
|||
waiting the time defined with set_timer() between each step, until all
|
||||
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
|
||||
continue.
|
||||
*/
|
||||
virtual bool get_mode() const = 0;
|
||||
virtual mode get_mode() 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_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_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);
|
||||
info << "policy activated \n";
|
||||
|
||||
simu.set_mode(false);
|
||||
simu.set_timer(0);
|
||||
simu.set_mode(Simulation::mode_step_by_step);
|
||||
info << "simulation set_mode single step\n";
|
||||
|
||||
info << "START environment dump \n";
|
||||
|
|
|
@ -431,8 +431,7 @@ main(int argc, char** argv)
|
|||
simu.set_policy(pol);
|
||||
info << "policy activated \n";
|
||||
|
||||
simu.set_mode(false);
|
||||
simu.set_timer(0);
|
||||
simu.set_mode(Simulation::mode_step_by_step);
|
||||
info << "simulation set_mode single step\n";
|
||||
|
||||
info << "START environment dump \n";
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
// along with SGPEMv2; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
#include "backend/global_preferences.hh"
|
||||
#include "backend/string_utils.hh"
|
||||
#include "backend/cpu_policies_gatekeeper.hh"
|
||||
#include "backend/cpu_policy_manager.hh"
|
||||
|
@ -26,16 +27,17 @@
|
|||
#include "backend/simulation.hh"
|
||||
#include "backend/serializers_gatekeeper.hh"
|
||||
#include "backend/serializer.hh"
|
||||
#include "backend/static_process.hh"
|
||||
#include "backend/static_resource.hh"
|
||||
#include "backend/static_thread.hh"
|
||||
#include "backend/static_request.hh"
|
||||
#include "backend/static_sub_request.hh"
|
||||
#include "backend/concrete_history.hh"
|
||||
#include "backend/concrete_simulation.hh"
|
||||
#include "backend/process.hh"
|
||||
#include "backend/resource.hh"
|
||||
#include "backend/thread.hh"
|
||||
#include "backend/request.hh"
|
||||
#include "backend/sub_request.hh"
|
||||
#include "backend/ready_queue.hh"
|
||||
|
||||
#include "text_simulation.hh"
|
||||
|
||||
#include <glibmm/timer.h>
|
||||
|
||||
#include <cassert>
|
||||
#include <ios>
|
||||
#include <iomanip>
|
||||
|
@ -349,7 +351,22 @@ TextSimulation::on_run(const Tokens& arguments)
|
|||
|
||||
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)
|
||||
{
|
||||
|
@ -671,7 +688,7 @@ TextSimulation::on_get(const Tokens& arguments)
|
|||
if (attr == "SIMULATION-TICK")
|
||||
{
|
||||
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());
|
||||
}
|
||||
else if (attr == "CONTINUOUS")
|
||||
|
@ -712,7 +729,7 @@ TextSimulation::on_set(const Tokens& arguments)
|
|||
if (timer < 0)
|
||||
throw domain_error("");
|
||||
|
||||
Simulation::get_instance().set_timer(timer);
|
||||
GlobalPreferences::get_instance().set_speed(timer);
|
||||
}
|
||||
catch (domain_error e)
|
||||
{
|
||||
|
@ -769,9 +786,10 @@ TextSimulation::on_set(const Tokens& arguments)
|
|||
{
|
||||
try
|
||||
{
|
||||
bool continuous = string_to<bool>(value);
|
||||
|
||||
Simulation::get_instance().set_mode(continuous);
|
||||
if(string_to<bool>(value) == true)
|
||||
Simulation::get_instance().set_mode(Simulation::mode_continuous);
|
||||
else
|
||||
Simulation::get_instance().set_mode(Simulation::mode_step_by_step);
|
||||
}
|
||||
catch (domain_error e)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue