From 0c02a29f8b139b3b9920d1ad7035e83f82e5d963 Mon Sep 17 00:00:00 2001 From: elvez Date: Fri, 8 Sep 2006 14:15:24 +0000 Subject: [PATCH] - "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 --- .../pyloader/src/python_cpu_policy_manager.cc | 6 +- .../sgpemv2/cpu_policies_gatekeeper.hh | 9 +- src/backend/sgpemv2/cpu_policy_manager.hh | 4 +- .../sgpemv2/resource_policies_gatekeeper.hh | 5 +- .../sgpemv2/resource_policy_manager.hh | 4 +- src/text_simulation.cc | 141 +++++++++++------- src/text_simulation.hh | 2 + 7 files changed, 103 insertions(+), 68 deletions(-) diff --git a/plugins/pyloader/src/python_cpu_policy_manager.cc b/plugins/pyloader/src/python_cpu_policy_manager.cc index ef06b2d..752436b 100644 --- a/plugins/pyloader/src/python_cpu_policy_manager.cc +++ b/plugins/pyloader/src/python_cpu_policy_manager.cc @@ -122,7 +122,7 @@ PythonCPUPolicyManager::collect_policies() if (dot_py.match(*file_it)) { #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 //strip extension 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()); _policies.push_back(pypolicy); - -#ifndef NDEBUG - std::clog << "OK\n"; -#endif } catch (const MalformedPolicyException& e) { diff --git a/src/backend/sgpemv2/cpu_policies_gatekeeper.hh b/src/backend/sgpemv2/cpu_policies_gatekeeper.hh index 4ed17f1..569784b 100644 --- a/src/backend/sgpemv2/cpu_policies_gatekeeper.hh +++ b/src/backend/sgpemv2/cpu_policies_gatekeeper.hh @@ -52,13 +52,16 @@ namespace sgpem friend class Singleton; public: - std::vector get_registered() const; + typedef CPUPolicyManager Manager; + typedef std::vector Managers; + + Managers get_registered() const; void register_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 @@ -74,7 +77,7 @@ namespace sgpem // Deactivates active policies managed by the specified manager. void deactivate_policies(CPUPolicyManager* manager); - std::vector _registered; + Managers _registered; std::map _active_policies; }; diff --git a/src/backend/sgpemv2/cpu_policy_manager.hh b/src/backend/sgpemv2/cpu_policy_manager.hh index 0f81d50..57a60bf 100644 --- a/src/backend/sgpemv2/cpu_policy_manager.hh +++ b/src/backend/sgpemv2/cpu_policy_manager.hh @@ -38,6 +38,8 @@ namespace sgpem class SG_DLLEXPORT CPUPolicyManager { public: + typedef CPUPolicy Policy; + typedef std::vector Policies; /** \brief CPUPolicyManager constructor * * Saves ``this'' pointer into the _registered attribute, so it can access @@ -51,7 +53,7 @@ namespace sgpem virtual ~CPUPolicyManager() = 0; - virtual const std::vector& get_avail_policies() = 0; + virtual const Policies& get_avail_policies() = 0; protected: virtual void collect_policies() = 0; diff --git a/src/backend/sgpemv2/resource_policies_gatekeeper.hh b/src/backend/sgpemv2/resource_policies_gatekeeper.hh index 8f61f39..07637f5 100644 --- a/src/backend/sgpemv2/resource_policies_gatekeeper.hh +++ b/src/backend/sgpemv2/resource_policies_gatekeeper.hh @@ -50,7 +50,8 @@ namespace sgpem friend class Singleton; public: - typedef std::vector Managers; + typedef ResourcePolicyManager Manager; + typedef std::vector Managers; const Managers& get_registered() const; @@ -69,7 +70,7 @@ namespace sgpem void deactivate_policies(const ResourcePolicyManager& manager); - std::vector _registered; + Managers _registered; std::map _active_policies; }; diff --git a/src/backend/sgpemv2/resource_policy_manager.hh b/src/backend/sgpemv2/resource_policy_manager.hh index a1b1c9a..c8b7a62 100644 --- a/src/backend/sgpemv2/resource_policy_manager.hh +++ b/src/backend/sgpemv2/resource_policy_manager.hh @@ -41,6 +41,8 @@ namespace sgpem class SG_DLLEXPORT ResourcePolicyManager { public: + typedef ResourcePolicy Policy; + typedef std::vector Policies; /** \brief ResourcePolicyManager constructor * * Registers itself to the ResourcePoliciesGatekeeper singleton. @@ -49,7 +51,7 @@ namespace sgpem virtual ~ResourcePolicyManager(); - virtual const std::vector& get_avail_policies() const = 0; + virtual const Policies& get_avail_policies() const = 0; }; } //~ namespace sgpem diff --git a/src/text_simulation.cc b/src/text_simulation.cc index 270a319..d1ccd2f 100644 --- a/src/text_simulation.cc +++ b/src/text_simulation.cc @@ -39,6 +39,8 @@ #include #include +#include + #include "text_simulation.hh" #include @@ -58,7 +60,6 @@ using Glib::ustring; namespace sgpem { - //TODO move this class to another file... (?) template class CommandParameter { @@ -652,10 +653,12 @@ TextSimulation::on_help(const Tokens& arguments) "continue it.\n")); else if (command == "JUMPTO") p_stdout(_("-- JUMPTO COMMAND --\nPauses the simulation and jumps to the specified instant.\n")); - else if (command == "CONFIGURE-CPU-POLICY") - p_stdout(_("-- CONFIGURE-CPU-POLICY COMMAND --\nConfigure parameters exposed by " - "the cpu policy.\n\nThis is currently the only way to control the behaviour of " - "cpu policies without modifying their source code.\n")); + else if (command == "CONFIGURE") + p_stdout(_("-- CONFIGURE COMMAND --\nConfigures a configurable entity.\n\n" + "Syntax: CONFIGURE \n" + "\twhere 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") p_stdout(_("-- HELP COMMAND --\nThe help you're reading.\n")); else if (command == "GET") @@ -663,7 +666,7 @@ TextSimulation::on_help(const Tokens& arguments) "\twhere may be simulation-tick or continuous.\n")); else if (command == "SET") p_stdout(_("-- SET COMMAND --\nSyntax: SET [=] \n" - "\twhere may be simulation-tick, continuous or cpu-policy.\n")); + "\twhere may be simulation-tick, continuous, cpu-policy or resource-policy.\n")); else if (command == "SHOW") p_stderr(_("-- SHOW COMMAND --\nDisplays the name of the entities (if available) " "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")); } +template +typename GatekeeperType::Manager::Policy& +policy_from_index(const Glib::ustring& value) throw(domain_error) +{ + int index = string_to(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 TextSimulation::on_set(const Tokens& arguments) { @@ -783,49 +816,20 @@ TextSimulation::on_set(const Tokens& arguments) } else if (attr == "CPU-POLICY") { - int policy; - try { - policy = string_to(value) - 1; + CPUPolicy& p = policy_from_index(value); - if (policy < 0) - throw domain_error(""); + Simulation::get_instance().set_policy(&p); + p_stdout("\n"); + p_stdout(p.get_name() + _(" scheduling policy selected.\n")); - typedef vector ManagerVec; - typedef vector::iterator CPUPolicyIt; - - CPUPoliciesGatekeeper& gatekeeper = CPUPoliciesGatekeeper::get_instance(); - - ManagerVec managers = gatekeeper.get_registered(); - - for (ManagerVec::iterator it = managers.begin(); it != managers.end(); ++it) - { - vector 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(""); + //ResourcePolicyManager & rpm = *ResourcePoliciesGatekeeper::get_instance().get_registered().at(0); + //Simulation::get_instance().set_resource_policy(rpm.get_avail_policies().at(0)); } 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) { @@ -835,6 +839,27 @@ TextSimulation::on_set(const Tokens& arguments) } } + else if (attr == "RESOURCE-POLICY") + { + try + { + ResourcePolicy& p = policy_from_index(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") { try @@ -1014,27 +1039,27 @@ TextSimulation::on_show_subrequests(const Tokens& arguments) show(subrequests); } +template void -TextSimulation::on_show_cpu_policies(const Tokens& arguments) +TextSimulation::show_policies(const Tokens& arguments) { - typedef vector ManagerVec; - typedef vector::iterator CPUPolicyIt; + typedef typename GatekeeperType::Managers ManagerVec; + typedef typename GatekeeperType::Manager::Policies PolicyVec; + typedef typename PolicyVec::iterator PolicyIt; check_arguments_num(arguments, 0); - CPUPoliciesGatekeeper& gatekeeper = CPUPoliciesGatekeeper::get_instance(); - - ManagerVec managers = gatekeeper.get_registered(); + ManagerVec managers = GatekeeperType::get_instance().get_registered(); 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 policies = (*it)->get_avail_policies(); - for (CPUPolicyIt it = policies.begin(); it != policies.end(); ++it) + PolicyVec policies = (*mit)->get_avail_policies(); + for (PolicyIt pit = policies.begin(); pit != policies.end(); ++pit) { ostringstream oss; - oss << endl << index << ". " << (*it)->get_name() << endl; - oss << "\t" << (*it)->get_description() << endl; + oss << endl << index << ". " << (*pit)->get_name() << endl; + oss << "\t" << (*pit)->get_description() << endl; p_stdout(oss.str()); ++index; @@ -1042,12 +1067,16 @@ TextSimulation::on_show_cpu_policies(const Tokens& arguments) } } +void +TextSimulation::on_show_cpu_policies(const Tokens& arguments) +{ + show_policies(arguments); +} + void TextSimulation::on_show_resource_policies(const Tokens& arguments) { - // Waiting for the coder to implementat resource policies - // But wait a moment, the coder is me!!! - p_stderr(_("FIXME: Not implemented\n")); + show_policies(arguments); } void diff --git a/src/text_simulation.hh b/src/text_simulation.hh index 8041abc..ad93460 100644 --- a/src/text_simulation.hh +++ b/src/text_simulation.hh @@ -77,6 +77,8 @@ namespace sgpem void get_parameter(CommandParameter& parameter); template void configure_policy(PolicyType& policy); + template + void show_policies(const Tokens& arguments); void on_run(const Tokens& arguments); void on_pause(const Tokens& arguments);