- "Beautify" debug printout from Python policy manager

- Added support for resource policies in textual frontend

git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@1064 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
elvez 2006-09-08 14:15:24 +00:00
parent f7b440d53a
commit 0c02a29f8b
7 changed files with 103 additions and 68 deletions

View File

@ -122,7 +122,7 @@ PythonCPUPolicyManager::collect_policies()
if (dot_py.match(*file_it)) if (dot_py.match(*file_it))
{ {
#ifndef NDEBUG #ifndef NDEBUG
std::clog << "\t" << *file_it << " is a valid Python script. Attempting to load... "; std::clog << *file_it << " appears to be a Python script. Attempting to load..." << std::endl;
#endif #endif
//strip extension //strip extension
std::string policy_name = (*file_it).substr(0, (*file_it).size() - 3); std::string policy_name = (*file_it).substr(0, (*file_it).size() - 3);
@ -132,10 +132,6 @@ PythonCPUPolicyManager::collect_policies()
PythonCPUPolicy *pypolicy = new PythonCPUPolicy(policy_name.c_str()); PythonCPUPolicy *pypolicy = new PythonCPUPolicy(policy_name.c_str());
_policies.push_back(pypolicy); _policies.push_back(pypolicy);
#ifndef NDEBUG
std::clog << "OK\n";
#endif
} }
catch (const MalformedPolicyException& e) catch (const MalformedPolicyException& e)
{ {

View File

@ -52,13 +52,16 @@ namespace sgpem
friend class Singleton<CPUPoliciesGatekeeper>; friend class Singleton<CPUPoliciesGatekeeper>;
public: public:
std::vector<CPUPolicyManager*> get_registered() const; typedef CPUPolicyManager Manager;
typedef std::vector<Manager*> Managers;
Managers get_registered() const;
void register_manager(CPUPolicyManager* manager); void register_manager(CPUPolicyManager* manager);
void unregister_manager(CPUPolicyManager* manager); void unregister_manager(CPUPolicyManager* manager);
CPUPolicy* get_current_policy(History* history) throw(std::runtime_error); CPUPolicy* get_current_policy(History* history) throw(std::runtime_error);
/** /**
Associates policy with history. If an exception is thrown, the current associated Associates policy with history. If an exception is thrown, the current associated
@ -74,7 +77,7 @@ namespace sgpem
// Deactivates active policies managed by the specified manager. // Deactivates active policies managed by the specified manager.
void deactivate_policies(CPUPolicyManager* manager); void deactivate_policies(CPUPolicyManager* manager);
std::vector<CPUPolicyManager*> _registered; Managers _registered;
std::map<History*, CPUPolicy*> _active_policies; std::map<History*, CPUPolicy*> _active_policies;
}; };

View File

@ -38,6 +38,8 @@ namespace sgpem
class SG_DLLEXPORT CPUPolicyManager class SG_DLLEXPORT CPUPolicyManager
{ {
public: public:
typedef CPUPolicy Policy;
typedef std::vector<Policy*> Policies;
/** \brief CPUPolicyManager constructor /** \brief CPUPolicyManager constructor
* *
* Saves ``this'' pointer into the _registered attribute, so it can access * Saves ``this'' pointer into the _registered attribute, so it can access
@ -51,7 +53,7 @@ namespace sgpem
virtual ~CPUPolicyManager() = 0; virtual ~CPUPolicyManager() = 0;
virtual const std::vector<CPUPolicy*>& get_avail_policies() = 0; virtual const Policies& get_avail_policies() = 0;
protected: protected:
virtual void collect_policies() = 0; virtual void collect_policies() = 0;

View File

@ -50,7 +50,8 @@ namespace sgpem
friend class Singleton<ResourcePoliciesGatekeeper>; friend class Singleton<ResourcePoliciesGatekeeper>;
public: public:
typedef std::vector<ResourcePolicyManager*> Managers; typedef ResourcePolicyManager Manager;
typedef std::vector<Manager*> Managers;
const Managers& get_registered() const; const Managers& get_registered() const;
@ -69,7 +70,7 @@ namespace sgpem
void deactivate_policies(const ResourcePolicyManager& manager); void deactivate_policies(const ResourcePolicyManager& manager);
std::vector<ResourcePolicyManager*> _registered; Managers _registered;
std::map<History*, ResourcePolicy*> _active_policies; std::map<History*, ResourcePolicy*> _active_policies;
}; };

View File

@ -41,6 +41,8 @@ namespace sgpem
class SG_DLLEXPORT ResourcePolicyManager class SG_DLLEXPORT ResourcePolicyManager
{ {
public: public:
typedef ResourcePolicy Policy;
typedef std::vector<Policy*> Policies;
/** \brief ResourcePolicyManager constructor /** \brief ResourcePolicyManager constructor
* *
* Registers itself to the ResourcePoliciesGatekeeper singleton. * Registers itself to the ResourcePoliciesGatekeeper singleton.
@ -49,7 +51,7 @@ namespace sgpem
virtual ~ResourcePolicyManager(); virtual ~ResourcePolicyManager();
virtual const std::vector<ResourcePolicy*>& get_avail_policies() const = 0; virtual const Policies& get_avail_policies() const = 0;
}; };
} //~ namespace sgpem } //~ namespace sgpem

View File

@ -39,6 +39,8 @@
#include <sgpemv2/sub_request.hh> #include <sgpemv2/sub_request.hh>
#include <sgpemv2/ready_queue.hh> #include <sgpemv2/ready_queue.hh>
#include <sgpemv2/templates/sequences.tcc>
#include "text_simulation.hh" #include "text_simulation.hh"
#include <glibmm/timer.h> #include <glibmm/timer.h>
@ -58,7 +60,6 @@ using Glib::ustring;
namespace sgpem namespace sgpem
{ {
//TODO move this class to another file... (?)
template <typename T> template <typename T>
class CommandParameter class CommandParameter
{ {
@ -652,10 +653,12 @@ TextSimulation::on_help(const Tokens& arguments)
"continue it.\n")); "continue it.\n"));
else if (command == "JUMPTO") else if (command == "JUMPTO")
p_stdout(_("-- JUMPTO COMMAND --\nPauses the simulation and jumps to the specified instant.\n")); p_stdout(_("-- JUMPTO COMMAND --\nPauses the simulation and jumps to the specified instant.\n"));
else if (command == "CONFIGURE-CPU-POLICY") else if (command == "CONFIGURE")
p_stdout(_("-- CONFIGURE-CPU-POLICY COMMAND --\nConfigure parameters exposed by " p_stdout(_("-- CONFIGURE COMMAND --\nConfigures a configurable entity.\n\n"
"the cpu policy.\n\nThis is currently the only way to control the behaviour of " "Syntax: CONFIGURE <entity>\n"
"cpu policies without modifying their source code.\n")); "\twhere <entity> may be cpu-policy or resource-policy.\n"
"This is currently the only way to control the behaviour of "
"policies without modifying their source code.\n"));
else if (command == "HELP") else if (command == "HELP")
p_stdout(_("-- HELP COMMAND --\nThe help you're reading.\n")); p_stdout(_("-- HELP COMMAND --\nThe help you're reading.\n"));
else if (command == "GET") else if (command == "GET")
@ -663,7 +666,7 @@ TextSimulation::on_help(const Tokens& arguments)
"\twhere <attr_name> may be simulation-tick or continuous.\n")); "\twhere <attr_name> may be simulation-tick or continuous.\n"));
else if (command == "SET") else if (command == "SET")
p_stdout(_("-- SET COMMAND --\nSyntax: SET <attr_name> [=] <value>\n" p_stdout(_("-- SET COMMAND --\nSyntax: SET <attr_name> [=] <value>\n"
"\twhere <attr_name> may be simulation-tick, continuous or cpu-policy.\n")); "\twhere <attr_name> may be simulation-tick, continuous, cpu-policy or resource-policy.\n"));
else if (command == "SHOW") else if (command == "SHOW")
p_stderr(_("-- SHOW COMMAND --\nDisplays the name of the entities (if available) " p_stderr(_("-- SHOW COMMAND --\nDisplays the name of the entities (if available) "
"and other informations prefixed by its numeric identifier.\n\n" "and other informations prefixed by its numeric identifier.\n\n"
@ -745,6 +748,36 @@ TextSimulation::on_get(const Tokens& arguments)
p_stderr(_("ERROR: invalid attribute name.\n")); p_stderr(_("ERROR: invalid attribute name.\n"));
} }
template <typename GatekeeperType>
typename GatekeeperType::Manager::Policy&
policy_from_index(const Glib::ustring& value) throw(domain_error)
{
int index = string_to<int>(value) - 1;
if (index < 0)
throw domain_error("");
typedef typename GatekeeperType::Managers ManagerVec;
typedef typename GatekeeperType::Manager::Policies PolicyVec;
typedef typename PolicyVec::iterator CPUPolicyIt;
ManagerVec managers = GatekeeperType::get_instance().get_registered();
for (typename ManagerVec::iterator mit = managers.begin(); mit != managers.end(); ++mit)
{
PolicyVec policies = (*mit)->get_avail_policies();
for (CPUPolicyIt pit = policies.begin(); pit != policies.end(); ++pit)
{
if (index == 0)
return *(*pit);
--index;
}
}
throw domain_error("");
}
void void
TextSimulation::on_set(const Tokens& arguments) TextSimulation::on_set(const Tokens& arguments)
{ {
@ -783,49 +816,20 @@ TextSimulation::on_set(const Tokens& arguments)
} }
else if (attr == "CPU-POLICY") else if (attr == "CPU-POLICY")
{ {
int policy;
try try
{ {
policy = string_to<int>(value) - 1; CPUPolicy& p = policy_from_index<CPUPoliciesGatekeeper>(value);
if (policy < 0) Simulation::get_instance().set_policy(&p);
throw domain_error(""); p_stdout("\n");
p_stdout(p.get_name() + _(" scheduling policy selected.\n"));
typedef vector<CPUPolicyManager*> ManagerVec; //ResourcePolicyManager & rpm = *ResourcePoliciesGatekeeper::get_instance().get_registered().at(0);
typedef vector<CPUPolicy*>::iterator CPUPolicyIt; //Simulation::get_instance().set_resource_policy(rpm.get_avail_policies().at(0));
CPUPoliciesGatekeeper& gatekeeper = CPUPoliciesGatekeeper::get_instance();
ManagerVec managers = gatekeeper.get_registered();
for (ManagerVec::iterator it = managers.begin(); it != managers.end(); ++it)
{
vector<CPUPolicy*> policies = (*it)->get_avail_policies();
for (CPUPolicyIt it = policies.begin(); it != policies.end(); ++it)
{
if (policy == 0)
{
Simulation::get_instance().set_policy(*it);
cout << endl << (*it)->get_name() << " scheduling policy selected." << endl;
// FIXME: dedicate a function to set resource policy
// which includes the following default one
ResourcePolicyManager & rpm = *ResourcePoliciesGatekeeper::get_instance().get_registered().at(0);
Simulation::get_instance().set_resource_policy(rpm.get_avail_policies().at(0));
// end of include
}
--policy;
}
}
if (policy >= 0)
throw domain_error("");
} }
catch (domain_error e) catch (domain_error e)
{ {
p_stderr(_("ERROR: invalid unsigned integer or not a valid policy index\n")); p_stderr(_("ERROR: invalid unsigned integer or not a valid scheduling policy index\n"));
} }
catch (const CPUPolicyException& e) catch (const CPUPolicyException& e)
{ {
@ -835,6 +839,27 @@ TextSimulation::on_set(const Tokens& arguments)
} }
} }
else if (attr == "RESOURCE-POLICY")
{
try
{
ResourcePolicy& p = policy_from_index<ResourcePoliciesGatekeeper>(value);
Simulation::get_instance().set_resource_policy(&p);
p_stdout("\n");
p_stdout(p.get_name() + _(" resource policy selected.\n"));
}
catch (domain_error e)
{
p_stderr(_("ERROR: invalid unsigned integer or not a valid resource policy index\n"));
}
catch (const CPUPolicyException& e)
{
p_stderr(_("ERROR: "));
p_stderr(e.what());
p_stderr("\n");
}
}
else if (attr == "CONTINUOUS") else if (attr == "CONTINUOUS")
{ {
try try
@ -1014,27 +1039,27 @@ TextSimulation::on_show_subrequests(const Tokens& arguments)
show(subrequests); show(subrequests);
} }
template <typename GatekeeperType>
void void
TextSimulation::on_show_cpu_policies(const Tokens& arguments) TextSimulation::show_policies(const Tokens& arguments)
{ {
typedef vector<CPUPolicyManager*> ManagerVec; typedef typename GatekeeperType::Managers ManagerVec;
typedef vector<CPUPolicy*>::iterator CPUPolicyIt; typedef typename GatekeeperType::Manager::Policies PolicyVec;
typedef typename PolicyVec::iterator PolicyIt;
check_arguments_num(arguments, 0); check_arguments_num(arguments, 0);
CPUPoliciesGatekeeper& gatekeeper = CPUPoliciesGatekeeper::get_instance(); ManagerVec managers = GatekeeperType::get_instance().get_registered();
ManagerVec managers = gatekeeper.get_registered();
unsigned int index = 1; unsigned int index = 1;
for (ManagerVec::iterator it = managers.begin(); it != managers.end(); ++it) for (typename ManagerVec::iterator mit = managers.begin(); mit != managers.end(); ++mit)
{ {
vector<CPUPolicy*> policies = (*it)->get_avail_policies(); PolicyVec policies = (*mit)->get_avail_policies();
for (CPUPolicyIt it = policies.begin(); it != policies.end(); ++it) for (PolicyIt pit = policies.begin(); pit != policies.end(); ++pit)
{ {
ostringstream oss; ostringstream oss;
oss << endl << index << ". " << (*it)->get_name() << endl; oss << endl << index << ". " << (*pit)->get_name() << endl;
oss << "\t" << (*it)->get_description() << endl; oss << "\t" << (*pit)->get_description() << endl;
p_stdout(oss.str()); p_stdout(oss.str());
++index; ++index;
@ -1042,12 +1067,16 @@ TextSimulation::on_show_cpu_policies(const Tokens& arguments)
} }
} }
void
TextSimulation::on_show_cpu_policies(const Tokens& arguments)
{
show_policies<CPUPoliciesGatekeeper>(arguments);
}
void void
TextSimulation::on_show_resource_policies(const Tokens& arguments) TextSimulation::on_show_resource_policies(const Tokens& arguments)
{ {
// Waiting for the coder to implementat resource policies show_policies<ResourcePoliciesGatekeeper>(arguments);
// But wait a moment, the coder is me!!!
p_stderr(_("FIXME: Not implemented\n"));
} }
void void

View File

@ -77,6 +77,8 @@ namespace sgpem
void get_parameter(CommandParameter<T>& parameter); void get_parameter(CommandParameter<T>& parameter);
template <typename PolicyType> template <typename PolicyType>
void configure_policy(PolicyType& policy); void configure_policy(PolicyType& policy);
template <typename GatekeeperType>
void show_policies(const Tokens& arguments);
void on_run(const Tokens& arguments); void on_run(const Tokens& arguments);
void on_pause(const Tokens& arguments); void on_pause(const Tokens& arguments);