- Rename Policy to CPUPolicy where appropriate

git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@811 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
tchernobog 2006-08-02 21:57:36 +00:00
parent 5b577c5979
commit 43b817aaed
31 changed files with 222 additions and 225 deletions

View file

@ -92,16 +92,16 @@ libpyloader_la_LDFLAGS = \
# Please keep this in sorted order:
libpyloader_la_SOURCES = \
src/plugin.cc \
src/python_policy.cc \
src/python_policy_manager.cc
src/cpu_python_policy.cc \
src/cpu_python_policy_manager.cc
noinst_HEADERS += \
src/python_policy.hh \
src/python_policy_manager.hh
src/cpu_python_policy.hh \
src/cpu_python_policy_manager.hh
share_PYTHON = \
src/Abstract.py \
src/Policy.py \
src/CPUPolicy.py \
src/ScriptAdapter.py
# ############################################################
@ -165,7 +165,7 @@ noinst_PROGRAMS = \
src_testsuite_test_pyloader_CPPFLAGS = \
-I@top_srcdir@ \
-DSHAREDIR="\"$(sharedir)\"" \
-DSHAREDIR="\"$(sharedir)\"" \
$(PYTHON_CPPFLAGS) \
$(GLIBMM_CFLAGS) \
$(GTHREAD_CFLAGS) \
@ -181,8 +181,8 @@ src_testsuite_test_pyloader_LDFLAGS = \
$(PYTHON_EXTRA_LDFLAGS)
src_testsuite_test_pyloader_SOURCES = \
src/testsuite/test-python_loader.cc \
src/python_policy.cc \
src/python_policy_manager.cc
src/cpu_python_policy.cc \
src/cpu_python_policy_manager.cc
noinst_PYTHON += src/testsuite/python_loader_configure.py \
src/testsuite/python_loader_sort_queue.py \

View file

@ -7,7 +7,7 @@ import sgpem
# This class also exposes the method sort(), which can be
# used to easily sort the queue of ready process with a
# user-defined given compare function.
class Policy:
class CPUPolicy:
## @var Avoid instantiation of an abstract class.
# @see Abstract.Metaclass
__metaclass__ = Metaclass

View file

@ -19,11 +19,11 @@
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
from Policy import Policy
from CPUPolicy import CPUPolicy
import sys
#from sgpem import policy_sorts_processes
class fcfs(Policy) :
class fcfs(CPUPolicy) :
def __init__(self):
pass;

View file

@ -19,10 +19,10 @@
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
from Policy import Policy
from CPUPolicy import CPUPolicy
import sys
class rr_priority(Policy) :
class rr_priority(CPUPolicy) :
def __init__(self):
pass;

View file

@ -19,11 +19,11 @@
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
from Policy import Policy
from CPUPolicy import CPUPolicy
import sys
#from sgpem import policy_sorts_processes
class sjf(Policy) :
class sjf(CPUPolicy) :
def __init__(self):
pass;

View file

@ -18,10 +18,12 @@
// along with SGPEMv2; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include "python_policy.hh"
#include "cpu_python_policy.hh"
#include <limits>
#include <unistd.h>
#include <iostream>
using namespace sgpem;
using namespace std;
@ -30,14 +32,14 @@ using namespace std;
// WARNING : this class needs extensive and above all
// *strong* exception checking / handling!
PythonPolicy::PythonPolicy(const char* name)
PythonCPUPolicy::PythonCPUPolicy(const char* name)
: _upolicy_dict(NULL), _adapter(NULL), _name(name)
{
PyObject* pLoadmeStr = PyString_FromString(name);
PyObject* pUserPolicyModule = PyImport_Import(pLoadmeStr);
PyObject* pUserCPUPolicyModule = PyImport_Import(pLoadmeStr);
Py_DECREF(pLoadmeStr);
if( !pUserPolicyModule )
if( !pUserCPUPolicyModule )
{
PyErr_Print(); // Error in import
// FIXME : don't exit abruptly, but fall back gracefully
@ -45,7 +47,7 @@ PythonPolicy::PythonPolicy(const char* name)
}
// Dictionary with defined ``symbols'' for .pyc file
_upolicy_dict = PyModule_GetDict(pUserPolicyModule);
_upolicy_dict = PyModule_GetDict(pUserCPUPolicyModule);
assert(_upolicy_dict);
// Loads ScriptAdapter module and get its dictionary
@ -56,16 +58,16 @@ PythonPolicy::PythonPolicy(const char* name)
PyObject* pAdapterDict = PyModule_GetDict(pScriptAdapterModule);
assert(pAdapterDict);
// Now takes the user-defined policy class from pUserPolicyDict
PyObject* pPolicyClass = PyDict_GetItemString(_upolicy_dict, name);
assert(pPolicyClass); // FIXME needs stricter checking and exception throwing
// Now takes the user-defined policy class from pUserCPUPolicyDict
PyObject* pCPUPolicyClass = PyDict_GetItemString(_upolicy_dict, name);
assert(pCPUPolicyClass); // FIXME needs stricter checking and exception throwing
// Creates a new object of type ScriptAdapter :
// takes init function from ScriptAdapter class
PyObject* pAdapterClass = PyDict_GetItemString(pAdapterDict, "ScriptAdapter");
PyObject* pAdapterCtorParam = PyTuple_New(1);
Py_INCREF(pPolicyClass); // PyTuple_SetItem steals a reference
PyTuple_SetItem(pAdapterCtorParam, 0, pPolicyClass);
Py_INCREF(pCPUPolicyClass); // PyTuple_SetItem steals a reference
PyTuple_SetItem(pAdapterCtorParam, 0, pCPUPolicyClass);
_adapter = PyInstance_New(pAdapterClass, pAdapterCtorParam, NULL);
Py_DECREF(pAdapterCtorParam);
assert(_adapter);
@ -76,7 +78,7 @@ PythonPolicy::PythonPolicy(const char* name)
}
PythonPolicy::~PythonPolicy()
PythonCPUPolicy::~PythonCPUPolicy()
{
if(_adapter) Py_DECREF(_adapter);
@ -88,7 +90,7 @@ PythonPolicy::~PythonPolicy()
void
PythonPolicy::configure() throw(UserInterruptException)
PythonCPUPolicy::configure() throw(UserInterruptException)
{
PyObject* retval = PyObject_CallMethod(_adapter, "async_configure", NULL);
Py_DECREF(retval);
@ -98,7 +100,7 @@ PythonPolicy::configure() throw(UserInterruptException)
void
PythonPolicy::sort_queue() const throw(UserInterruptException)
PythonCPUPolicy::sort_queue() const throw(UserInterruptException)
{
PyObject* retval = PyObject_CallMethod(_adapter, "async_sort_queue", NULL);
@ -111,20 +113,20 @@ PythonPolicy::sort_queue() const throw(UserInterruptException)
Glib::ustring
PythonPolicy::get_description() const
PythonCPUPolicy::get_description() const
{
return _name;
}
Glib::ustring
PythonPolicy::get_name() const
PythonCPUPolicy::get_name() const
{
return _name;
}
bool
PythonPolicy::is_pre_emptive() const throw(UserInterruptException)
PythonCPUPolicy::is_pre_emptive() const throw(UserInterruptException)
{
PyObject* retval = PyObject_CallMethod(_adapter, "async_is_preemptive", NULL);
Py_DECREF(retval);
@ -141,7 +143,7 @@ PythonPolicy::is_pre_emptive() const throw(UserInterruptException)
int
PythonPolicy::get_time_slice() const throw(UserInterruptException)
PythonCPUPolicy::get_time_slice() const throw(UserInterruptException)
{
PyObject* retval = PyObject_CallMethod(_adapter, "async_get_time_slice", NULL);
@ -162,7 +164,7 @@ PythonPolicy::get_time_slice() const throw(UserInterruptException)
void
PythonPolicy::wait_unlock() const throw(UserInterruptException)
PythonCPUPolicy::wait_unlock() const throw(UserInterruptException)
{
PyThreadState* _save;
int i = 0; // We give the sort_queue() three seconds max time, then...

View file

@ -1,4 +1,4 @@
// src/backend/pyloader/python_policy.hh - Copyright 2005, 2006, University
// src/cpu_python_policy.hh - Copyright 2005, 2006, University
// of Padova, dept. of Pure and Applied
// Mathematics
//
@ -18,8 +18,8 @@
// along with SGPEMv2; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#ifndef PYTHON_POLICY_HH
#define PYTHON_POLICY_HH 1
#ifndef CPU_PYTHON_POLICY_HH
#define CPU_PYTHON_POLICY_HH 1
#include "config.h"
@ -28,13 +28,13 @@
#include <iostream>
#include "policy.hh"
#include "cpu_policy.hh"
#include "user_interrupt_exception.hh"
namespace sgpem
{
class PythonPolicy;
class PythonPolicyManager;
class PythonCPUPolicy;
class PythonCPUPolicyManager;
class UserInterruptException;
/** \brief A specialization of abstract class Policy
@ -42,11 +42,11 @@ namespace sgpem
This class represents a policy written in Python. Its methods interact with Python interpreter.
See the documentation of class Policy for more detailed informations.
*/
class SG_DLLEXPORT PythonPolicy : public Policy
class SG_DLLEXPORT PythonCPUPolicy : public CPUPolicy
{
public:
PythonPolicy(const char* name);
virtual ~PythonPolicy();
PythonCPUPolicy(const char* name);
virtual ~PythonCPUPolicy();
/**
Calls the method \c async_configure
@ -87,8 +87,8 @@ namespace sgpem
}
private:
PythonPolicy(const PythonPolicy&);
PythonPolicy& operator=(const PythonPolicy&);
PythonCPUPolicy(const PythonCPUPolicy&);
PythonCPUPolicy& operator=(const PythonCPUPolicy&);
void wait_unlock() const throw(UserInterruptException);

View file

@ -1,4 +1,4 @@
// src/python_policy_manager.cc - Copyright 2005, 2006, University
// src/cpu_python_policy_manager.cc - Copyright 2005, 2006, University
// of Padova, dept. of Pure and Applied
// Mathematics
//
@ -18,9 +18,9 @@
// along with SGPEMv2; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include "python_policy_manager.hh"
#include "cpu_python_policy_manager.hh"
#include "global_preferences.hh"
#include "policies_gatekeeper.hh"
#include "cpu_policies_gatekeeper.hh"
#include <Python.h>
#include <glibmm/ustring.h>
@ -35,6 +35,7 @@
#include <string>
#include <iostream>
#include <unistd.h>
using namespace sgpem;
using std::vector;
using std::cout;
@ -57,21 +58,21 @@ private:
};
PythonPolicyManager::PythonPolicyManager()
PythonCPUPolicyManager::PythonCPUPolicyManager()
: _initialized(false)
{
PyEval_InitThreads();
}
PythonPolicyManager::~PythonPolicyManager()
PythonCPUPolicyManager::~PythonCPUPolicyManager()
{
for_each(_policies.begin(), _policies.end(), ptr_fun(operator delete));
}
void
PythonPolicyManager::init()
PythonCPUPolicyManager::init()
{
if(_initialized)
// No-op
@ -100,14 +101,14 @@ PythonPolicyManager::init()
collect_policies();
}
const vector<Policy*>&
PythonPolicyManager::get_avail_policies()
const vector<CPUPolicy*>&
PythonCPUPolicyManager::get_avail_policies()
{
return _policies;
}
void
PythonPolicyManager::collect_policies()
PythonCPUPolicyManager::collect_policies()
{
GlobalPreferences& prefs = GlobalPreferences::get_instance();
GlobalPreferences::dir_iterator dir_it = prefs.policies_dir_begin();
@ -132,7 +133,7 @@ PythonPolicyManager::collect_policies()
//strip extension
std::string policy_name = (*file_it).substr(0, (*file_it).size() - 3);
PythonPolicy *pypolicy = new PythonPolicy(policy_name.c_str());
PythonCPUPolicy *pypolicy = new PythonCPUPolicy(policy_name.c_str());
_policies.push_back(pypolicy);
}

View file

@ -1,4 +1,4 @@
// src/python_policy_manager.hh - Copyright 2005, 2006, University
// src/cpu_python_policy_manager.hh - Copyright 2005, 2006, University
// of Padova, dept. of Pure and Applied
// Mathematics
//
@ -18,31 +18,31 @@
// along with SGPEMv2; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#ifndef PYTHON_POLICY_MANAGER_HH
#define PYTHON_POLICY_MANAGER_HH 1
#ifndef CPU_PYTHON_POLICY_MANAGER_HH
#define CPU_PYTHON_POLICY_MANAGER_HH 1
#include "config.h"
#include <Python.h>
#include "policy_manager.hh"
#include "python_policy.hh"
#include "cpu_policy_manager.hh"
#include "cpu_python_policy.hh"
namespace sgpem
{
//class PolicyManager;
class PythonPolicyManager;
//class CPUPolicyManager;
class PythonCPUPolicyManager;
/** \brief Manages Python user-implemented policies
*
* This singleton manages the creation and destruction
* of a Python policy.
*/
class SG_DLLEXPORT PythonPolicyManager : public PolicyManager
class SG_DLLEXPORT PythonCPUPolicyManager : public CPUPolicyManager
{
public:
PythonPolicyManager();
~PythonPolicyManager();
PythonCPUPolicyManager();
~PythonCPUPolicyManager();
/** \brief Initialize the Python interpreter.
*
@ -51,15 +51,15 @@ namespace sgpem
*/
void init();
const std::vector<Policy*>& get_avail_policies();
const std::vector<CPUPolicy*>& get_avail_policies();
protected:
/** The selected and active PyhonPolicy object. */
/** The selected and active PyhonCPUPolicy object. */
void collect_policies();
private:
PythonPolicyManager(const PythonPolicyManager&);
PythonPolicyManager& operator=(const PythonPolicyManager&);
PythonCPUPolicyManager(const PythonCPUPolicyManager&);
PythonCPUPolicyManager& operator=(const PythonCPUPolicyManager&);
bool _initialized;
};

View file

@ -21,18 +21,18 @@
#include "config.h"
#include "plugin.hh"
#include "python_policy_manager.hh"
#include "cpu_python_policy_manager.hh"
using namespace sgpem;
// Is this OK? If not, we must use a function with a local static variable...
PythonPolicyManager* _policy_manager = NULL;
PythonCPUPolicyManager* _policy_manager = NULL;
void
sgpem__Plugin__on_init()
{
if(_policy_manager == NULL)
_policy_manager = new sgpem::PythonPolicyManager();
_policy_manager = new sgpem::PythonCPUPolicyManager();
}
void

View file

@ -1,6 +1,6 @@
%module sgpem
%{
#include "policy.hh"
#include "cpu_policy.hh"
#include "policy_parameters.hh"
#include "process.hh"
#include "ready_queue.hh"
@ -59,9 +59,9 @@ namespace std
namespace sgpem {
class Policy {
class CPUPolicy {
public:
virtual ~Policy() = 0;
virtual ~CPUPolicy() = 0;
sgpem::PolicyParameters& get_parameters();
};
@ -218,7 +218,7 @@ namespace sgpem {
// ---------------------------------------------
class Scheduler {
public:
sgpem::Policy* get_policy();
sgpem::CPUPolicy* get_policy();
static sgpem::Scheduler& get_instance();
sgpem::ReadyQueue* get_ready_queue();
private:

View file

@ -1,7 +1,7 @@
from Policy import Policy
from CPUPolicy import CPUPolicy
import sys
class python_loader_configure(Policy) :
class python_loader_configure(CPUPolicy) :
def __init__(self):
pass;

View file

@ -1,7 +1,7 @@
from Policy import Policy
from CPUPolicy import CPUPolicy
import sys
class python_loader_get_time_slice(Policy) :
class python_loader_get_time_slice(CPUPolicy) :
def __init__(self):
pass

View file

@ -1,7 +1,7 @@
from Policy import Policy
from CPUPolicy import CPUPolicy
import sys
class python_loader_is_preemptive(Policy) :
class python_loader_is_preemptive(CPUPolicy) :
def __init__(self):
pass

View file

@ -1,7 +1,7 @@
from Policy import Policy
from CPUPolicy import CPUPolicy
import sys
class python_loader_sort_queue(Policy) :
class python_loader_sort_queue(CPUPolicy) :
def __init__(self):
pass

View file

@ -22,12 +22,12 @@
* class and its closely related cousins. More documentation to be written
* here, thanks very much. */
#include "../python_policy_manager.hh"
#include "../python_policy.hh"
#include "../cpu_python_policy_manager.hh"
#include "../cpu_python_policy.hh"
#include "simulation.hh"
#include "global_preferences.hh"
#include "policies_gatekeeper.hh"
#include "cpu_policies_gatekeeper.hh"
#include "simulation.hh"
#include "scheduler.hh"
#include "user_interrupt_exception.hh"
@ -42,10 +42,10 @@
using namespace sgpem;
using namespace std;
static Policy*
find_pol_by_name(const vector<Policy*>& pols, const Glib::ustring& name)
static CPUPolicy*
find_pol_by_name(const vector<CPUPolicy*>& pols, const Glib::ustring& name)
{
vector<Policy*>::const_iterator it = pols.begin();
vector<CPUPolicy*>::const_iterator it = pols.begin();
for( ; it != pols.end(); it++)
if((*it)->get_name() == name)
return *it;
@ -69,24 +69,24 @@ main(int argc, char** argv)
sgpem::GlobalPreferences::get_instance().add_policies_dir(argv[1]);
// Self-register itself to PoliciesGatekeeper, however we don't care about it
PythonPolicyManager polman;
PythonCPUPolicyManager polman;
polman.init();
Simulation& sim = Simulation::get_instance();
History& his = sim.get_history();
PoliciesGatekeeper& pgk = PoliciesGatekeeper::get_instance();
CPUPoliciesGatekeeper& pgk = CPUPoliciesGatekeeper::get_instance();
const std::vector<Policy*>& policies = polman.get_avail_policies();
const std::vector<CPUPolicy*>& policies = polman.get_avail_policies();
// Print out avail policies
cout << "These are the policies I found:" << endl;
vector<Policy*>::const_iterator it = policies.begin();
vector<CPUPolicy*>::const_iterator it = policies.begin();
for(; it != policies.end(); it++)
cout << "\t * " << (*it)->get_name() << endl;
try
{
Policy* pol = find_pol_by_name(policies, "python_loader_configure");
CPUPolicy* pol = find_pol_by_name(policies, "python_loader_configure");
assert(pol != NULL);
// FIXME : Maybe activating a policy only to configure it is an overkill?
@ -103,7 +103,7 @@ main(int argc, char** argv)
try
{
Policy* pol = find_pol_by_name(policies, "python_loader_is_preemptive");
CPUPolicy* pol = find_pol_by_name(policies, "python_loader_is_preemptive");
assert(pol != NULL);
pgk.activate_policy(&his, pol);
pol->is_pre_emptive();
@ -117,7 +117,7 @@ main(int argc, char** argv)
try
{
Policy* pol = find_pol_by_name(policies, "python_loader_get_time_slice");
CPUPolicy* pol = find_pol_by_name(policies, "python_loader_get_time_slice");
assert(pol != NULL);
pgk.activate_policy(&his, pol);
pol->get_time_slice();
@ -132,7 +132,7 @@ main(int argc, char** argv)
try
{
Policy* pol = find_pol_by_name(policies, "python_loader_get_time_slice");
CPUPolicy* pol = find_pol_by_name(policies, "python_loader_get_time_slice");
assert(pol != NULL);
pgk.activate_policy(&his, pol);
pol->sort_queue();