- Moved Simulation and ConcreteSimulation to the backend

> - Completed ConcreteSimulation, i think it is all we need at the moment...

git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@773 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
elvez 2006-07-16 13:35:39 +00:00
parent cab84a0e3d
commit a092f3dc7b
7 changed files with 196 additions and 225 deletions

View file

@ -0,0 +1,188 @@
// src/backend/concrete_simulation.cc - Copyright 2005, 2006, University
// of Padova, dept. of Pure and Applied
// Mathematics
//
// This file is part of SGPEMv2.
//
// This is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// SGPEMv2 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SGPEMv2; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include "concrete_simulation.hh"
#include "scheduler.hh"
#include "policies_gatekeeper.hh"
#include <glibmm/timer.h>
#include "smartp.tcc"
using namespace std;
using namespace sgpem;
using namespace memory;
using Glib::usleep;
ConcreteSimulation::ConcreteSimulation() :
_state(state_paused), _mode(true), _timer_interval(1000), _policy(NULL)
{}
void
ConcreteSimulation::set_timer(const int& t)
{
_timer_interval = t;
}
int
ConcreteSimulation::get_timer() const
{
return _timer_interval;
}
void
ConcreteSimulation::set_mode(const bool& b)
{
_mode = b;
}
bool
ConcreteSimulation::get_mode() const
{
return _mode;
}
void
ConcreteSimulation::pause()
{
_state = state_paused;
}
void
ConcreteSimulation::stop()
{
_state = state_stopped;
}
void
ConcreteSimulation::run() throw(UserInterruptException)
{
switch(_state)
{
case state_running:
return;
case state_stopped:
_history.reset(true);
break;
}
_state = state_running;
// chech for termination
const Environment &env = _history.get_last_environment();
const Environment::Processes& processes = env.get_processes();
typedef Environment::Processes::const_iterator ProcessesIt;
//******* CONTINUOUS TIME
if (_mode)
{
do
{
bool all_term = true;
for(ProcessesIt it = processes.begin(); it != processes.end(); ++it)
if((*it)->get_state() != Schedulable::state_terminated)
{
all_term = false;
break;
}
//if there are no processes left the termination message has already been notified
//by the last execution of upadate()
if (all_term)
{
_state = state_stopped;
return; // Exit from loop
}
try
{
assert(get_policy() != NULL);
//step forward
Scheduler::get_instance().step_forward(_history, *get_policy());
//sleep
Glib::usleep(_timer_interval*1000);
}
catch(UserInterruptException e)
{
stop();
throw;
}
//check the state
if (_state == state_stopped || _state == state_paused)
return;
}
while(true);
}
//******* STEP by STEP
else
{
bool all_term = true;
for(ProcessesIt it = processes.begin(); it != processes.end(); ++it)
if((*it)->get_state() != Schedulable::state_terminated)
{
all_term = false;
break;
}
if (all_term)
//if there are no processes left the termination message has already been notified
//by the last execution of upadate()
_state = state_paused;
else
{
try
{
assert(get_policy() != NULL);
//step forward
Scheduler::get_instance().step_forward(_history, *get_policy());
}
catch(UserInterruptException e)
{
throw;
}
}
}
}
void
ConcreteSimulation::set_policy(Policy* p)
{
_policy = p;
if(p != NULL)
PoliciesGatekeeper::get_instance().activate_policy(&_history, p);
}
Policy*
ConcreteSimulation::get_policy()
{
return _policy;
}

View file

@ -0,0 +1,66 @@
// src/frontend/concrete_simulation.hh - Copyright 2005, 2006, University
// of Padova, dept. of Pure and Applied
// Mathematics
//
// This file is part of SGPEMv2.
//
// This is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// SGPEMv2 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SGPEMv2; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#ifndef CONCRETE_SIMULATION_HH
#define CONCRETE_SIMULATION_HH 1
#include "simulation.hh"
#include "concrete_history.hh"
namespace sgpem
{
class ConcreteSimulation;
class ConcreteSimulation : public Simulation
{
public:
ConcreteSimulation();
void run() throw(UserInterruptException);
void pause();
void stop();
void set_timer(const int&);
int get_timer() const;
void set_mode(const bool&);
bool get_mode() const;
void set_policy(Policy*);
Policy* get_policy();
private:
state _state;
bool _mode;
int _timer_interval;
ConcreteHistory _history;
Policy* _policy;
};
}
#endif

40
src/backend/simulation.cc Normal file
View file

@ -0,0 +1,40 @@
// src/backend/simulation.cc - Copyright 2005, 2006, University
// of Padova, dept. of Pure and Applied
// Mathematics
//
// This file is part of SGPEMv2.
//
// This is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// SGPEMv2 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SGPEMv2; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include "simulation.hh"
#include "concrete_simulation.hh"
// Do not include in header file:
#include "singleton.tcc"
using namespace sgpem;
// Explicit template instantiation to allow to export symbols from the DSO.
template class SG_DLLEXPORT Singleton<ConcreteSimulation>;
Simulation::~Simulation()
{
}
Simulation&
Simulation::get_instance()
{
return Singleton<ConcreteSimulation>::get_instance();
}

151
src/backend/simulation.hh Normal file
View file

@ -0,0 +1,151 @@
// src/frontend/simulation.hh - Copyright 2005, 2006, University
// of Padova, dept. of Pure and Applied
// Mathematics
//
// This file is part of SGPEMv2.
//
// This is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// SGPEMv2 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SGPEMv2; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#ifndef SIMULATION_HH
#define SIMULATION_HH 1
namespace sgpem
{
class ConcreteSimulation;
class Policy;
}
#include "config.h"
#include "singleton.hh"
#include "user_interrupt_exception.hh"
#include <vector>
namespace sgpem
{
class Simulation;
/**
\brief Manages a single simulation instance.
Starting from a base state, simulates the progress of a scheduling system.
Given the discrete nature of the implementation, once a scheduling algorithm
and its parameters are fixed, the state of the system is a function of time.
This doesn't mean the behaviour of the policy is assumed to be deterministic,
since this will restrict the range of available policies (we won't be able
to uselottery scheduling, for example).
It provides methods to edit the state of the simulation, to check the
characteristics of the simulation (advancement speed, advancement mode) and to
check which schedulng algorithm is currently in use.
\remarks Implements the Observer pattern: observes classes \ref History, \ref Scheduler,
\ref Policy.
\remarks Implements the Controller pattern: ensures Low Coupling between the Frontend and
the Backend layers.
*/
class SG_DLLEXPORT Simulation : public Singleton<ConcreteSimulation>
{
public:
enum state
{
state_running,
state_paused,
state_stopped
};
virtual ~Simulation();
/**
\brief Runs the simulation.
Advances the simulation by one or more steps, depending on the
actual state and on the value set with set_mode().
*/
virtual void run() throw(UserInterruptException) = 0;
/**
\brief Pauses a running simulation.
It is obviously useful only when the advancement mode is continue.
Calling again run() will cause the simulation to start from the current
simulation step.
*/
virtual void pause() = 0;
/**
\brief Stops the simulation.
Behaves in the same way as pause(), except that the next call to run()
will cause the simulation to start from the beginning.
*/
virtual void stop() = 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(const int&) = 0;
/**
\see set_timer()
*/
virtual int get_timer() const = 0;
/**
\brief This methods allows to change the way the simulation progresses.
If the input value is 0 (false), the simulation will advance a single time
step for each call to run().
If the input value is 1 (true), the simulation will advance contiuosly,
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;
/**
\return The simulation advancement mode: 0 if step-to-step, 1 if
continue.
*/
virtual bool get_mode() const = 0;
/**
\brief Setup the policy to be used by the system.
The input pointer must be one of those returned by get_avaiable_policies().
*/
virtual void set_policy(Policy*) = 0;
/**
\return The policy currently in use.
*/
virtual Policy* get_policy() = 0;
/**
* Small kludge to avoid the need for declaration of ConcreteSimulation
* by the calling code of Simulation::get_instance()
*/
static Simulation& get_instance();
};
}
#endif