From 73a3e72118befbe6961391806ccd5e67d21ae5e8 Mon Sep 17 00:00:00 2001 From: tchernobog Date: Wed, 22 Feb 2006 22:57:32 +0000 Subject: [PATCH] - Implement first draft of quicksort in Python - Extend test to have a set of processes (now lacks significative output) - Extend FCFS policy to do something useful - FIXME : segfaults on sortQueue() - FIXME : needs implementation for SchedulableQueue.swap() git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@389 3ecf2c5c-341e-0410-92b4-d18e462d057c --- src/backend/policy.hh | 2 +- src/backend/pyloader/Policy.py | 78 +++++++++++++++---- src/backend/pyloader/python_policy_manager.cc | 6 +- src/backend/sgpem.i | 4 - src/builtin-policies/fcfs.py | 20 +---- src/testsuite/test-python_loader.cc | 35 ++++++++- 6 files changed, 102 insertions(+), 43 deletions(-) diff --git a/src/backend/policy.hh b/src/backend/policy.hh index f81764a..fee04ac 100644 --- a/src/backend/policy.hh +++ b/src/backend/policy.hh @@ -44,7 +44,7 @@ namespace sgpem virtual ~Policy(); virtual void configure() = 0; - virtual void sort_queue(sgpem::Scheduler::event) const = 0; + virtual void sort_queue(Scheduler::event) const = 0; int get_id() const; virtual Glib::ustring get_description() const = 0; virtual bool is_pre_emptive() const = 0; diff --git a/src/backend/pyloader/Policy.py b/src/backend/pyloader/Policy.py index bb7aa34..c1431cf 100644 --- a/src/backend/pyloader/Policy.py +++ b/src/backend/pyloader/Policy.py @@ -1,22 +1,11 @@ from Abstract import * - -## @brief this function implements a quicksort in place -# using the SchedulableQueue methods + +## @brief This is the abstract class a user-defined policy +# should inherit from # -# The compare parameter should be a user defined function -# name returning either True or False, defined like: -# @code -# def compare(SchedulableA,SchedulableB): -# return SchedulableA.someProperty() < SchedulableB.someProperty() -# @endcode -# -# @param queue The SchedulableQueue to be sorted in place -# @param cmp The binary function to use to compare elements -# @returns None -def sort(queue, cmp): - pass - - +# This class also exposes the method sort, which can be +# used to easily sort the queue of ready process with a +# given compare function class Policy: ## @var Avoid instantiation of an abstract class __metaclass__ = Metaclass @@ -26,3 +15,58 @@ class Policy: is_preemptive = AbstractMethod('is_preemptive') get_time_slice = AbstractMethod('get_time_slice') + ## @brief this function implements a quicksort in place + # using the SchedulableQueue methods + # + # The compare parameter should be a user defined function + # name returning either True or False, defined like: + # @code + # def compare(SchedulableA,SchedulableB): + # return SchedulableA.someProperty() < SchedulableB.someProperty() + # @endcode + # + # @param queue The SchedulableQueue to be sorted in place + # @param cmpf The binary function to use to compare elements + # @returns None + def sort(queue, cmpf): + __recursive_qsort_(queue, 0, queue.size()-1, cmpf) + + ## @brief Recursive (private) call to perform quicksort on a + # queue + # + # @param queue The queue to sort + # @param a The initial element position of the slice + # @param b The final element position of the slice + # @param cmpf The user-defined compare function to employ + # @returns None + def __recursive_qsort_(queue, a, b, cmpf): + if(b>a): + pivot = __partition_(queue, a, b, cmpf) + __recursive_qsort_(queue, a, pivot-1, cmpf) + __recursive_qsort_(queue, pivot+1, b, cmpf) + + ## @brief Recursive (private) call to partition a slice of the queue + # + # This private function (the name mangling should work) + # naively sorts a partition of queue in place using just + # its methods. + # + # Feel the love. + # + # @param queue The SchedulableQueue to sort + # @param a The partition starting element position in the queue + # @param b The partition ending element position in the queue + # @param cmpf The binary function to use for comparing two elements + # @return The new pivot index + def __partition_(queue, a, b, cmpf): + # takes pivot element: + right = queue.get_item_at(b) + i = a + for j in range(a,b-1): + if cmpf(queue.get_item_at(j), right): + # the C++ code should do nothing if i == j: + queue.swap(i,j) + i = i+1 + # puts pivot in place + queue.swap(i,b) + return i diff --git a/src/backend/pyloader/python_policy_manager.cc b/src/backend/pyloader/python_policy_manager.cc index 38503f2..27936cc 100644 --- a/src/backend/pyloader/python_policy_manager.cc +++ b/src/backend/pyloader/python_policy_manager.cc @@ -50,7 +50,7 @@ Policy& PythonPolicyManager::get_policy() { // FIXME : assumes that _python_policy is always != NULL! - return *_python_policy; + return *_python_policy; } void @@ -76,7 +76,5 @@ PythonPolicyManager::init() // FIXME : Hardcoded policy name char* policy_name = "fcfs"; - PythonPolicy policy(policy_name); - policy.configure(); - std::cout << policy.get_time_slice() << std::endl; + _python_policy = std::auto_ptr(new PythonPolicy(policy_name)); } diff --git a/src/backend/sgpem.i b/src/backend/sgpem.i index 246cee2..71227d5 100644 --- a/src/backend/sgpem.i +++ b/src/backend/sgpem.i @@ -126,11 +126,7 @@ namespace sgpem { class SchedulableList { public: - sgpem::SchedulableStatus* top(); - sgpem::SchedulableStatus* bottom(); - bool insert_at(const uint&, const uint&); uint size() const; - SchedulableStatus* get_item_at(const uint&); const SchedulableStatus* get_item_at(const uint&) const; void swap(unsigned int positionA, unsigned int positionB) throw(); diff --git a/src/builtin-policies/fcfs.py b/src/builtin-policies/fcfs.py index 8821025..7fbbcfe 100644 --- a/src/builtin-policies/fcfs.py +++ b/src/builtin-policies/fcfs.py @@ -1,18 +1,5 @@ from Policy import Policy -##### Typical session : ####### -############################### -# cd [...]/share/sgpemv2/policies -# python -# >>> import sys -# >>> sys.path[:0] = [ '../modules' ] -# >>> import fcfs -# >>> p = fcfs.fcfs_policy() -# >>> par = p.get_parameters() -# Segmentation fault -# (this makes sense...) -############################### - class fcfs(Policy) : def __init__(self): pass; @@ -27,6 +14,7 @@ class fcfs(Policy) : return -1 def sort_queue(self, event, queue): - # How am I supposed to sort that mess?? - # FIXME - return queue + cmpf = lambda a, b: \ + a.get_schedulable().get_arrival_time() < \ + b.get_schedulable().get_arrival_time() + self.sort(queue,cmpf) diff --git a/src/testsuite/test-python_loader.cc b/src/testsuite/test-python_loader.cc index ca13390..cea07cb 100644 --- a/src/testsuite/test-python_loader.cc +++ b/src/testsuite/test-python_loader.cc @@ -23,11 +23,44 @@ * here, thanks very much. */ #include "backend/pyloader/python_policy_manager.hh" +#include "backend/process.hh" +#include "backend/schedulable_status.hh" +#include "backend/schedulable_list.hh" +#include "backend/scheduler.hh" +#include int main(int, char** ) { using namespace sgpem; - + + // Create an INITIAL STATE + Process p1("P1", 0,5,1); + Process p2("P2", 0,5,2); + Process p3("P3", 5,3,3); + Process p4("P4", 6,2,3); + Process p5("P5", 1,2,3); + Process p6("P6", 10,2,1); + + SchedulableStatus ss1(p1); + SchedulableStatus ss2(p2); + SchedulableStatus ss3(p3); + SchedulableStatus ss4(p4); + SchedulableStatus ss5(p5); + SchedulableStatus ss6(p6); + + SchedulableList initial; + initial.add_at_bottom(ss1); + initial.add_at_bottom(ss2); + initial.add_at_bottom(ss3); + initial.add_at_bottom(ss4); + initial.add_at_bottom(ss5); + initial.add_at_bottom(ss6); + History::get_instance().enqueue_slice(initial); + PythonPolicyManager* ppm = PythonPolicyManager::get_instance(); ppm->init(); + Policy& pol = ppm->get_policy(); + pol.configure(); + std::cout << pol.get_time_slice() << std::endl; + pol.sort_queue(Scheduler::event_schedulable_arrival); }