make life easier to Scheduler - Go on implementing a bit more of Scheduler::step_forward() - Remove "places" from SubRequest git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@778 3ecf2c5c-341e-0410-92b4-d18e462d057c
400 lines
11 KiB
C++
400 lines
11 KiB
C++
// src/backend/concrete_history.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 "config.h"
|
|
|
|
#include "concrete_history.hh"
|
|
|
|
#include "dynamic_process.hh"
|
|
#include "dynamic_thread.hh"
|
|
#include "dynamic_resource.hh"
|
|
#include "dynamic_request.hh"
|
|
#include "dynamic_sub_request.hh"
|
|
|
|
#include "static_process.hh"
|
|
#include "static_thread.hh"
|
|
#include "static_resource.hh"
|
|
#include "static_request.hh"
|
|
#include "static_sub_request.hh"
|
|
|
|
#include "smartp.tcc"
|
|
|
|
#include <algorithm>
|
|
#include <cassert>
|
|
#include <functional>
|
|
|
|
using namespace sgpem;
|
|
using namespace std;
|
|
using memory::smart_ptr;
|
|
|
|
|
|
// ---------------
|
|
// For all your evil-doers on Earth, this is your punishment!
|
|
|
|
template<typename T>
|
|
static bool deep_remove(std::vector<T*>& v, const T& obj)
|
|
{
|
|
typedef typename std::vector<T*> Vector;
|
|
for(typename Vector::iterator it = v.begin(); it != v.end(); it++)
|
|
if(**it == obj)
|
|
{
|
|
delete *it;
|
|
v.erase(it);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
|
|
template<typename T>
|
|
static T* deep_find(const std::vector<T*>& v, const T& obj)
|
|
{
|
|
typedef typename std::vector<T*> Vector;
|
|
for(typename Vector::const_iterator it = v.begin(); it != v.end(); it++)
|
|
if(**it == obj)
|
|
{
|
|
return *it;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
|
|
// -----------------
|
|
|
|
ConcreteHistory::ConcreteHistory()
|
|
: History(), _snapshots()
|
|
{
|
|
_snapshots.push_back(new ConcreteEnvironment());
|
|
}
|
|
|
|
|
|
ConcreteHistory::~ConcreteHistory()
|
|
{
|
|
for_each(_snapshots.begin(), _snapshots.end(), ptr_fun(operator delete));
|
|
}
|
|
|
|
ConcreteHistory::ConcreteHistory(const ConcreteHistory& h) :
|
|
History(h)
|
|
{
|
|
typedef Snapshots::const_iterator SnapIt;
|
|
for(SnapIt it = h._snapshots.begin(); it != h._snapshots.end(); ++it)
|
|
_snapshots.push_back(new ConcreteEnvironment(*(*it)));
|
|
}
|
|
|
|
void
|
|
ConcreteHistory::append_new_environment(ConcreteEnvironment* environment)
|
|
{
|
|
_snapshots.push_back(environment);
|
|
notify_change();
|
|
}
|
|
|
|
|
|
ConcreteHistory::size_t
|
|
ConcreteHistory::get_size()
|
|
{
|
|
return _snapshots.size();
|
|
}
|
|
|
|
|
|
const ConcreteEnvironment&
|
|
ConcreteHistory::get_last_environment() const
|
|
{
|
|
// Should always be true:
|
|
assert(_snapshots.size() > 0);
|
|
return *_snapshots.back();
|
|
}
|
|
|
|
|
|
const ConcreteEnvironment&
|
|
ConcreteHistory::get_environment_at(position index) const
|
|
throw(std::out_of_range)
|
|
{
|
|
return *_snapshots.at(index);
|
|
}
|
|
|
|
|
|
void
|
|
ConcreteHistory::remove(resource_key_t resource_key)
|
|
{
|
|
// Pay attention that initial isn't deleted by reset()
|
|
ConcreteEnvironment& initial = *_snapshots.front();
|
|
ConcreteEnvironment::Resources& resources = initial.get_resources();
|
|
ConcreteEnvironment::Resources::iterator found = resources.find(resource_key);
|
|
if(found == resources.end())
|
|
return;
|
|
|
|
reset(false);
|
|
|
|
delete found->second;
|
|
resources.erase(found);
|
|
|
|
// Listening to "The Thing That Should Not Be"...
|
|
// all hail the cyclomatic complexity!
|
|
ConcreteEnvironment::Processes& processes = initial.get_processes();
|
|
typedef ConcreteEnvironment::Processes::iterator ProcIt;
|
|
for(ProcIt it1 = processes.begin(); it1 != processes.end(); it1++)
|
|
{
|
|
typedef std::vector<DynamicThread*> Threads;
|
|
Threads& threads = dynamic_cast<DynamicProcess&>(**it1).get_dynamic_threads();
|
|
for(Threads::iterator it2 = threads.begin(); it2 != threads.end(); it2++)
|
|
{
|
|
typedef std::vector<DynamicRequest*> Requests;
|
|
Requests& reqs = (*it2)->get_dynamic_requests();
|
|
for(Requests::iterator it3 = reqs.begin(); it3 != reqs.end(); it3++)
|
|
{
|
|
typedef std::vector<DynamicSubRequest*> SubRequests;
|
|
SubRequests& subr = (*it3)->get_dynamic_subrequests();
|
|
for(SubRequests::iterator it4; it4 != subr.end(); it4++)
|
|
{
|
|
if((*it4)->get_resource_key() == resource_key)
|
|
{
|
|
delete *it4;
|
|
subr.erase(it4);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} //~ end monstrous construct, "The Thing That Should Not Be"
|
|
// Chtulhu ftaghn. There are worse things in life. Mother-in-laws,
|
|
// for example. Or hangovers. Or being read poetry by a Vogon.
|
|
// Although the above construct really rates between the first tens.
|
|
|
|
notify_change();
|
|
}
|
|
|
|
|
|
void
|
|
ConcreteHistory::remove(Process& process)
|
|
{
|
|
// Pay attention that initial isn't deleted by reset()
|
|
ConcreteEnvironment& initial = *_snapshots.front();
|
|
ConcreteEnvironment::Processes& processes = initial.get_processes();
|
|
bool found = deep_remove<Process>(processes, process);
|
|
if(found)
|
|
reset(true);
|
|
}
|
|
|
|
|
|
void
|
|
ConcreteHistory::remove(Thread& thread)
|
|
{
|
|
DynamicThread& dyn_thr = dynamic_cast<DynamicThread&>(thread);
|
|
|
|
// Pay attention that initial isn't deleted by reset()
|
|
ConcreteEnvironment& initial = *_snapshots.front();
|
|
ConcreteEnvironment::Processes& processes = initial.get_processes();
|
|
|
|
Process* found = deep_find<Process>(processes, dyn_thr.get_process());
|
|
|
|
if(found == NULL)
|
|
return; // not found, just return.
|
|
|
|
DynamicProcess& dynamic_found = dynamic_cast<DynamicProcess&>(*found);
|
|
bool removed = deep_remove<DynamicThread>(dynamic_found.get_dynamic_threads(), dyn_thr);
|
|
|
|
if(removed)
|
|
reset(true);
|
|
}
|
|
|
|
|
|
void
|
|
ConcreteHistory::remove(Request& request)
|
|
{
|
|
DynamicRequest& dyn_req = dynamic_cast<DynamicRequest&>(request);
|
|
DynamicThread& dyn_thr = dyn_req.get_thread();
|
|
DynamicProcess& dyn_proc = dyn_thr.get_process();
|
|
|
|
// Pay attention that initial isn't deleted by reset()
|
|
ConcreteEnvironment& initial = *_snapshots.front();
|
|
ConcreteEnvironment::Processes& processes = initial.get_processes();
|
|
|
|
Process* proc_ref = deep_find<Process>(processes, dyn_proc);
|
|
DynamicProcess* dyn_proc_ref = dynamic_cast<DynamicProcess*>(proc_ref);
|
|
if(dyn_proc_ref == NULL)
|
|
return; // not found, just return.
|
|
DynamicThread* thr_ref = deep_find<DynamicThread>(dyn_proc_ref->get_dynamic_threads(), dyn_thr);
|
|
if(thr_ref == NULL)
|
|
return; // not found, just return.
|
|
|
|
bool removed = deep_remove<DynamicRequest>(thr_ref->get_dynamic_requests(), dyn_req);
|
|
|
|
if(removed)
|
|
reset(true);
|
|
}
|
|
|
|
|
|
void
|
|
ConcreteHistory::remove(SubRequest& subrequest)
|
|
{
|
|
DynamicSubRequest& dyn_sub = dynamic_cast<DynamicSubRequest&>(subrequest);
|
|
DynamicRequest& dyn_req = dyn_sub.get_request();
|
|
DynamicThread& dyn_thr = dyn_req.get_thread();
|
|
DynamicProcess& dyn_proc = dyn_thr.get_process();
|
|
|
|
// Pay attention that initial isn't deleted by reset()
|
|
ConcreteEnvironment& initial = *_snapshots.front();
|
|
ConcreteEnvironment::Processes& processes = initial.get_processes();
|
|
|
|
Process* proc_ref = deep_find<Process>(processes, dyn_proc);
|
|
DynamicProcess* dyn_proc_ref = dynamic_cast<DynamicProcess*>(proc_ref);
|
|
if(dyn_proc_ref == NULL)
|
|
return; // not found, just return.
|
|
DynamicThread* thr_ref = deep_find<DynamicThread>(dyn_proc_ref->get_dynamic_threads(), dyn_thr);
|
|
if(thr_ref == NULL)
|
|
return; // not found, just return.
|
|
DynamicRequest* req_ref = deep_find<DynamicRequest>(thr_ref->get_dynamic_requests(), dyn_req);
|
|
if(req_ref == NULL)
|
|
return; // not found, just return.
|
|
|
|
bool removed = deep_remove<DynamicSubRequest>(req_ref->get_dynamic_subrequests(), dyn_sub);
|
|
|
|
if(removed)
|
|
reset(true);
|
|
}
|
|
|
|
|
|
ConcreteHistory::ResourcePair
|
|
ConcreteHistory::add_resource(const Glib::ustring& name,
|
|
bool preemptable,
|
|
size_t places,
|
|
size_t availability)
|
|
{
|
|
reset(false);
|
|
|
|
typedef ConcreteEnvironment::Resources Resources;
|
|
|
|
// And preemptable and availability?? FIXME!
|
|
|
|
StaticResource* core = new StaticResource(name, places);
|
|
DynamicResource* resource = new DynamicResource(core);
|
|
|
|
ConcreteEnvironment::Resources& resources = _snapshots.front()->get_resources();
|
|
|
|
// alakazam! Black magic at work... get a unique index for this resource
|
|
resource_key_t index = 0;
|
|
while(resources.find(index) != resources.end())
|
|
index++;
|
|
|
|
// Found a hole in the map, fill it like little Hans,
|
|
// its finger and the spilling dam.
|
|
Resources::iterator temp = resources.insert(pair<resource_key_t,Resource*>(index, resource)).first;
|
|
|
|
notify_change();
|
|
|
|
return *temp;
|
|
}
|
|
|
|
|
|
DynamicProcess&
|
|
ConcreteHistory::add_process(const Glib::ustring& name,
|
|
time_t arrival_time,
|
|
prio_t base_priority)
|
|
{
|
|
reset(false);
|
|
|
|
StaticProcess* core = new StaticProcess(name, arrival_time, base_priority);
|
|
DynamicProcess* proc = new DynamicProcess(core);
|
|
|
|
ConcreteEnvironment::Processes& processes = _snapshots.front()->get_processes();
|
|
processes.push_back(proc);
|
|
|
|
notify_change();
|
|
return *proc;
|
|
}
|
|
|
|
|
|
DynamicThread&
|
|
ConcreteHistory::add_thread(const Glib::ustring& name,
|
|
Process& parent,
|
|
time_t cpu_time,
|
|
time_t arrival_time,
|
|
prio_t base_priority)
|
|
{
|
|
reset(false);
|
|
|
|
// Holy cow! *THIS* is ugly!!!!
|
|
DynamicProcess& parent_process = dynamic_cast<DynamicProcess&>(parent);
|
|
StaticProcess& parent_core = parent_process.get_core();
|
|
StaticThread* core = new StaticThread(name, parent_core, cpu_time, arrival_time, base_priority);
|
|
DynamicThread* thread = new DynamicThread(core, &parent_process);
|
|
|
|
notify_change();
|
|
return *thread;
|
|
}
|
|
|
|
|
|
DynamicRequest&
|
|
ConcreteHistory::add_request(Thread& owner,
|
|
time_t instant)
|
|
{
|
|
reset(false);
|
|
|
|
DynamicThread& dyn_owner = dynamic_cast<DynamicThread&>(owner);
|
|
StaticThread& owner_core = dyn_owner.get_core();
|
|
|
|
StaticRequest* core = new StaticRequest(&owner_core, instant);
|
|
DynamicRequest* req = new DynamicRequest(core, &dyn_owner);
|
|
|
|
dyn_owner.get_requests().push_back(req);
|
|
|
|
notify_change();
|
|
return *req;
|
|
}
|
|
|
|
|
|
DynamicSubRequest&
|
|
ConcreteHistory::add_subrequest(Request& request,
|
|
resource_key_t resource_key,
|
|
time_t duration)
|
|
{
|
|
reset(false);
|
|
|
|
DynamicRequest& dyn_request = dynamic_cast<DynamicRequest&>(request);
|
|
|
|
StaticSubRequest* core = new StaticSubRequest(resource_key, duration);
|
|
DynamicSubRequest* subreq = new DynamicSubRequest(core, &dyn_request);
|
|
|
|
dyn_request.get_subrequests().push_back(subreq);
|
|
|
|
notify_change();
|
|
return *subreq;
|
|
}
|
|
|
|
|
|
void
|
|
ConcreteHistory::reset(bool notify)
|
|
{
|
|
assert(_snapshots.size() > 0);
|
|
Snapshots::iterator it = _snapshots.begin();
|
|
it++; // Skip first environment that we saved
|
|
|
|
for_each(it, _snapshots.end(), ptr_fun(operator delete));
|
|
_snapshots.resize(1); // Truncate to keep only our "model"
|
|
|
|
if(notify)
|
|
notify_change();
|
|
}
|
|
|
|
void
|
|
ConcreteHistory::notify_change()
|
|
{
|
|
// FIXME write code for this method. won't link without this stub
|
|
}
|