- modified simulation_widget (pre release)

- added test-simulation_widget - modified Makefile.am



git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@910 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
paolo 2006-08-20 03:49:17 +00:00
parent dd40bac86c
commit 894784463f
5 changed files with 882 additions and 66 deletions

View File

@ -351,8 +351,9 @@ if COND_TESTS
# DEJATOOL = src/testsuite/example-test.exp # DEJATOOL = src/testsuite/example-test.exp
noinst_PROGRAMS = \ noinst_PROGRAMS = \
src/testsuite/test-cairo_widget \
src/testsuite/test-history \ src/testsuite/test-history \
src/testsuite/test-cairo_widget src/testsuite/test-simulation_widget
# disable : # disable :
# src/testsuite/test-parse_command # src/testsuite/test-parse_command
@ -397,6 +398,24 @@ src_testsuite_test_cairo_widget_SOURCES = \
src/cairo_widget.cc \ src/cairo_widget.cc \
src/testsuite/test-cairo_widget.cc src/testsuite/test-cairo_widget.cc
src_testsuite_test_simulation_widget_CPPFLAGS = \
-I@top_srcdir@/src \
-I@top_srcdir@/src/templates \
$(CAIRO_CFLAGS) \
$(GTKMM_CFLAGS) \
$(GLIBMM_CFLAGS) \
$(GTHREAD_CFLAGS)
src_testsuite_test_simulation_widget_LDFLAGS = \
src/backend/libbackend.la \
$(CAIRO_LIBS) \
$(GTKMM_LIBS) \
$(GLIBMM_LIBS) \
$(GTHREAD_LIBS)
src_testsuite_test_simulation_widget_SOURCES = \
src/cairo_elements.cc \
src/cairo_widget.cc \
src/simulation_widget.cc \
src/testsuite/test-simulation_widget.cc
#src_testsuite_test_parse_command_CPPFLAGS = \ #src_testsuite_test_parse_command_CPPFLAGS = \
# -I@top_srcdir@/src \ # -I@top_srcdir@/src \

View File

@ -48,6 +48,7 @@ namespace sgpem
scaling_mode set_scaling_mode(scaling_mode scaling_mode); scaling_mode set_scaling_mode(scaling_mode scaling_mode);
scaling_mode get_scaling_mode(); scaling_mode get_scaling_mode();
void change_scaling_mode(); void change_scaling_mode();
void redraw();
protected: protected:
typedef unsigned int size_t; typedef unsigned int size_t;
@ -67,8 +68,6 @@ namespace sgpem
// with this method CairoWidget tells the needed widgwt dimensions // with this method CairoWidget tells the needed widgwt dimensions
virtual void calc_widget_size(size_t& width, size_t& height) const; virtual void calc_widget_size(size_t& width, size_t& height) const;
void redraw();
typedef Gdk::Rectangle area_t; typedef Gdk::Rectangle area_t;
typedef sigc::mem_functor0<void, CairoWidget> method0_t; typedef sigc::mem_functor0<void, CairoWidget> method0_t;
typedef std::pair<area_t, method0_t> area_callback_t; typedef std::pair<area_t, method0_t> area_callback_t;

View File

@ -36,23 +36,25 @@ using namespace sgpem;
SimulationWidget::SimulationWidget() SimulationWidget::SimulationWidget()
: Glib::ObjectBase("sgpem_SimulationWidget"), CairoWidget(), : Glib::ObjectBase("sgpem_SimulationWidget"), CairoWidget(),
_n_update(0) SimulationObserver(), _simulation(0),
_x_unit(10), _y_unit(10)
{ {
// Register this observer: // Register this observer:
Simulation::get_instance().get_history().attach(*this); Simulation::get_instance().attach(*this);
} }
SimulationWidget::~SimulationWidget() SimulationWidget::~SimulationWidget()
{ {
Simulation::get_instance().get_history().detach(*this); Simulation::get_instance().detach(*this);
} }
void void
SimulationWidget::update(const History& history) SimulationWidget::update(const Simulation& changed_simulation)
{ {
_n_update++; _simulation = &changed_simulation;
std::cout << " update: " << _n_update << std::endl;
// Force redraw // Force redraw
redraw(); redraw();
@ -62,69 +64,94 @@ SimulationWidget::update(const History& history)
void void
SimulationWidget::draw_widget(cairo_t* ctx) SimulationWidget::draw_widget(cairo_t* ctx)
{ {
cairo_set_source_rgb(ctx, 0, 0, 0); /*
cairo_move_to(ctx, 0, 0); std::cout << " draw_widget start " << std::endl;
cairo_line_to(ctx, 300, 350); if(!_simulation)
cairo_stroke(ctx); return;
std::cout << " draw_widget continue " << std::endl;
cairo_set_source_rgb(ctx, 0, 0, 0); */
cairo_move_to(ctx, 0, 0); const History& hist = Simulation::get_instance().get_history();
cairo_line_to(ctx, 300, 350); const Environment::Processes& processes = hist.get_last_environment().get_processes();
// NOTE: just to try int nproc = processes.size();
CairoElements ce(ctx); double text_maxw = 0;
bool* terminated = 0;
Rectangle area = { 30, 30, 100, 100 }; if(nproc>0)
ce.draw_container(area); terminated = new bool[nproc];
Color red = { 1, 0, 0 };
Point center = { 25, 25 };
ce.draw_3dsphere(center, 20, red);
Glib::ustring msg("Number of updates: "), number;
int_to_string(_n_update, number);
msg = msg + number;
cairo_move_to(ctx, 10, 105);
cairo_show_text(ctx,msg.c_str());
switch(get_scaling_mode()) for(int i=0; i<nproc; i++)
{ {
case CairoWidget::scaling_none: cairo_text_extents_t extents;
msg = "scaling_none = without any scaling"; cairo_text_extents(ctx, processes[i]->get_name().c_str(), &extents);
break; if(text_maxw<extents.width)
case CairoWidget::scaling_to_w: text_maxw=extents.width;
msg = "scaling_to_w = uses width to calculate scale factor"; cairo_move_to(ctx, _x_unit, extents.height + (3*i+1)*_y_unit);
break; cairo_show_text(ctx,processes[i]->get_name().c_str());
case CairoWidget::scaling_to_h: terminated[i] = false;
msg = "scaling_to_h = uses height to calculate scale factor";
break;
case CairoWidget::scaling_min:
msg = "scaling_min = uses minimum scale factor from width, height";
break;
case CairoWidget::scaling_max:
msg = "scaling_max = uses maximum scale factor from width, height";
break;
case CairoWidget::scaling_all:
msg = "scaling_all = scale to stretch into client area";
break;
} }
cairo_move_to(ctx, 10, 115);
cairo_show_text(ctx,msg.c_str());
msg = "clic to change mode...";
cairo_move_to(ctx, 10, 125);
cairo_show_text(ctx,msg.c_str());
if(_simulation && hist.get_size()>0
/* && _simulation->get_state()!=Simulation::state_stopped */ )
{
// std::cout << " draw_widget not_stop " << std::endl;
unsigned int pos = _simulation->get_front();
for(int t=1; t<=pos; t++)
{
const Environment::Processes& processes = hist.get_environment_at(t).get_processes();
double xpos = text_maxw + (1 + t)*_x_unit;
for(int i=0; i<nproc; i++)
{
double ypos = (3*i+1)*_y_unit;
Schedulable::state st = processes[i]->get_state();
switch(st)
{
case Schedulable::state_running:
cairo_set_source_rgb(ctx, 0, 1, 0);
cairo_rectangle(ctx, xpos, ypos, _x_unit, _y_unit);
cairo_fill(ctx);
break;
case Schedulable::state_ready:
cairo_set_source_rgb(ctx, 1, 1, 0);
cairo_rectangle(ctx, xpos, ypos, _x_unit, _y_unit);
cairo_fill(ctx);
break;
case Schedulable::state_blocked:
cairo_set_source_rgb(ctx, 1, 0, 0);
cairo_rectangle(ctx, xpos, ypos, _x_unit, _y_unit);
cairo_fill(ctx);
break;
case Schedulable::state_future:
break;
case Schedulable::state_terminated:
if(!terminated[i])
{
cairo_set_source_rgb(ctx, 0, 0, 0);
cairo_rectangle(ctx, xpos, ypos, _x_unit, _y_unit);
cairo_fill(ctx);
}
terminated[i] = true;
break;
}
}
}
}
delete[] terminated;
} }
void void
SimulationWidget::calc_drawing_size(size_t& width, size_t& height) const SimulationWidget::calc_drawing_size(size_t& width, size_t& height) const
{ {
// FIXME: write me if(!_simulation)
// some magic here! return;
width = 150; const History& hist = _simulation->get_history();
height = 150; const Environment::Processes& processes = hist.get_last_environment().get_processes();
width = (10 + 1 + hist.get_size()) * _x_unit;
height = 3 * processes.size() * _y_unit;
} }
/*
bool bool
SimulationWidget::on_button_press_event(GdkEventButton* event) SimulationWidget::on_button_press_event(GdkEventButton* event)
{ {
@ -133,8 +160,9 @@ SimulationWidget::on_button_press_event(GdkEventButton* event)
// Not here. Yet. // Not here. Yet.
return true; return true;
} }
*/
/*
void void
SimulationWidget::change_scaling_mode() SimulationWidget::change_scaling_mode()
{ {
@ -158,3 +186,4 @@ SimulationWidget::change_scaling_mode()
} }
redraw(); redraw();
} }
*/

View File

@ -24,27 +24,29 @@
#include "config.h" #include "config.h"
#include "cairo_widget.hh" #include "cairo_widget.hh"
#include "backend/history_observer.hh" #include "backend/simulation_observer.hh"
namespace sgpem namespace sgpem
{ {
class SimulationWidget : public CairoWidget, public HistoryObserver class SimulationWidget : public SimulationObserver, public CairoWidget
{ {
public: public:
SimulationWidget(); SimulationWidget();
virtual ~SimulationWidget(); virtual ~SimulationWidget();
void update(const History& history); void update(const Simulation& changed_simulation);
protected: protected:
virtual bool on_button_press_event(GdkEventButton* event); //virtual bool on_button_press_event(GdkEventButton* event);
void change_scaling_mode(); // void change_scaling_mode();
void draw_widget(cairo_t* ctx); void draw_widget(cairo_t* ctx);
virtual void calc_drawing_size(size_t& width, size_t& height) const; virtual void calc_drawing_size(size_t& width, size_t& height) const;
// void calc_size(const History& history, size_t& width, size_t& height) const; // void calc_size(const History& history, size_t& width, size_t& height) const;
private: private:
int _n_update; int _x_unit;
int _y_unit;
const Simulation* _simulation;
}; };
} //~ namespace sgpem } //~ namespace sgpem

View File

@ -0,0 +1,767 @@
// src/testsuite/test-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
/* This executable tests for workingness of the whole History services,
* and nothing else. */
#include "config.h"
#include "gettext.h"
#include <glibmm/module.h>
#include <glibmm/ustring.h>
#include <glibmm/thread.h>
#include <gtkmm/box.h>
#include <gtkmm/button.h>
#include <gtkmm/main.h>
#include <gtkmm/window.h>
#include <cassert>
#include <iostream>
/*
#include "backend/concrete_environment.hh"
#include "backend/concrete_history.hh"
*/
#include "backend/cpu_policies_gatekeeper.hh"
#include "backend/cpu_policy.hh"
#include "backend/cpu_policy_manager.hh"
#include "backend/history_observer.hh"
#include "backend/scheduler.hh"
#include "backend/simulation.hh"
#include "backend/simulation_observer.hh"
#include "backend/process.hh"
#include "backend/resource.hh"
#include "backend/thread.hh"
#include "cairo_elements.hh"
#include "cairo_widget.hh"
#include "simulation_widget.hh"
// #include "backend/concrete_history.hh"
using namespace sgpem;
using namespace std;
using Glib::ustring;
// ------------------------------------------------------
//
// PRRPolicy class
//
// ------------------------------------------------------
/** An hard-coded Priority Round Robin policy
* it adds a new constructor taking the quantum size (time slice)*/
class PRRPolicy : public CPUPolicy
{
public:
PRRPolicy()
{
_instance = this;
}
PRRPolicy(int quantum) : _quantum(quantum)
{
_instance = this;
}
static PRRPolicy& get_instance()
{
if (!_instance) _instance = new PRRPolicy(3); // quantum size
return *_instance;
}
virtual ~PRRPolicy()
{}
virtual void configure() throw(UserInterruptException, MalformedPolicyException)
{}
virtual void sort_queue() const throw(UserInterruptException)
{ // here a lot of fun, exactly O(n^2) fun!
// ReadyQueue sl = History.get_instance().get_simulation_status_at(get_current_time());
ReadyQueue* sl = Scheduler::get_instance().get_ready_queue();
if(sl && sl->size()>=2)
{
/*
cout << " sort_queue time: " << Simulation::get_instance().get_front() << endl;
for (int i = 0; i < sl->size(); i++)
{
cout << " i: " << i << " name: " << sl->get_item_at(i).get_name()
<< " pr: " << sl->get_item_at(i).get_current_priority()
<< " last: " << sl->get_item_at(i).get_last_acquisition()
<< endl;
}
*/
for (int i = 0; i < sl->size(); i++)
{
for (int j = 0; j < sl->size() - 1; j++)
{
if ( (sl->get_item_at(j).get_current_priority() < sl->get_item_at(j + 1).get_current_priority())
|| ((sl->get_item_at(j).get_current_priority() == sl->get_item_at(j + 1).get_current_priority())
&& (sl->get_item_at(j).get_last_acquisition() > sl->get_item_at(j + 1).get_last_acquisition())) )
{
sl->swap(j, j + 1);
}
}
}
/*
cout << "after" << endl;
for (int i = 0; i < sl->size(); i++)
{
cout << " i: " << i << " name: " << sl->get_item_at(i).get_name()
<< " pr: " << sl->get_item_at(i).get_current_priority()
<< " last: " << sl->get_item_at(i).get_last_acquisition()
<< endl;
}
cout << endl;
*/
}
}
int get_id() const
{
return 42;
}
virtual Glib::ustring get_description() const
{
return "42";
}
virtual Glib::ustring get_name() const
{
return "FourtyTwo";
}
virtual bool is_pre_emptive() const throw(UserInterruptException, MalformedPolicyException)
{
return true;
}
virtual int get_time_slice() const throw(UserInterruptException, MalformedPolicyException)
{
return _quantum;
}
PolicyParameters& get_parameters()
{
return _parameters;
}
virtual void activate() throw(UserInterruptException, MalformedPolicyException)
{
}
virtual void deactivate()
{
}
protected:
PolicyParameters _parameters;
int _id;
int _quantum;
private:
static PRRPolicy* _instance;
};
PRRPolicy* PRRPolicy::_instance=0;
// ------------------------------------------------------
//
// DummyPolicyManager class
//
// ------------------------------------------------------
class DummyPolicyManager : public CPUPolicyManager
{
public:
/** \brief CPUPolicyManager constructor
*
* Saves ``this'' pointer into the _registered attribute, so it can access
* it when requested. This is done so that concrete subclasses can be defined
* even if they are found in external dynamic modules not known at compile time.
*
* For the moment, just an instance of CPUPolicyManager can be saved. This will
* be expanded in next milestones.
*/
DummyPolicyManager()
{
_policies.push_back(new PRRPolicy);
}
virtual ~DummyPolicyManager()
{
}
/**
Gets THE policy (the only today) used.
Next versions will implement some other kind.
\return A reference to the policy.
FIXME deprecated
*/
//virtual CPUPolicy& get_policy() = 0;
/**
Init (or reset if yet initialized) the manager.
FIXME deprecated
*/
virtual void init()
{
}
virtual const std::vector<CPUPolicy*>& get_avail_policies()
{
return _policies;
}
protected:
virtual void collect_policies()
{
}
};
// insert some resources, processes, threads, requests, subrequests
void fillHistory(History &hist);
// print entire environment into the passed ostream
void dumpEnvironment(const Environment& env, ostream &os);
Glib::ustring get_schedulable_state_name(Schedulable::state st);
Glib::ustring get_request_state_name(Request::state st);
Glib::ustring get_simulation_state_name(Simulation::state st);
// ------------------------------------------------------
//
// TestWidget class
//
// ------------------------------------------------------
namespace sgpem
{
class TestWidget : public SimulationObserver, public CairoWidget
{
public:
TestWidget();
virtual ~TestWidget();
protected:
virtual void update(const Simulation& changed_simulation);
virtual bool on_button_press_event(GdkEventButton* event);
void draw_widget(cairo_t* ctx);
void change_scaling_mode();
virtual void calc_drawing_size(size_t& width, size_t& height) const;
// void calc_size(const History& history, size_t& width, size_t& height) const;
private:
int _x_unit;
int _y_unit;
const Simulation* _simulation;
};
}
TestWidget::TestWidget()
: Glib::ObjectBase("sgpem_TestWidget"), CairoWidget(),
_x_unit(10), _y_unit(10), _simulation(0)
{
}
TestWidget::~TestWidget()
{
}
bool
TestWidget::on_button_press_event(GdkEventButton* event)
{
std::cout << " on_button_press_event " << std::endl;
// change_scaling_mode();
// Not here. Yet.
Simulation::get_instance().run();
return true;
}
void TestWidget::update(const Simulation& changed_simulation)
{
_simulation = &changed_simulation;
redraw();
}
void
TestWidget::draw_widget(cairo_t* ctx)
{
const History& hist = Simulation::get_instance().get_history();
const Environment::Processes& processes = hist.get_last_environment().get_processes();
int nproc = processes.size();
double text_maxw = 0;
bool* terminated = 0;
if(nproc>0)
terminated = new bool[nproc];
for(int i=0; i<nproc; i++)
{
cairo_text_extents_t extents;
cairo_text_extents(ctx, processes[i]->get_name().c_str(), &extents);
if(text_maxw<extents.width)
text_maxw=extents.width;
cairo_move_to(ctx, 0, extents.height + (3*i+1)*_y_unit);
cairo_show_text(ctx,processes[i]->get_name().c_str());
terminated[i] = false;
}
if(!_simulation)
{
cairo_move_to(ctx, text_maxw + _x_unit, (3.0*nproc)/2*_y_unit);
cairo_show_text(ctx,"click to start");
}
if(_simulation && _simulation->get_state()!=Simulation::state_stopped && hist.get_size()>0)
{
unsigned int pos = _simulation->get_front();
cout << "front " << pos << endl;
for(int t=1; t<=pos; t++)
{
const Environment::Processes& processes = hist.get_environment_at(t).get_processes();
double xpos = text_maxw + (1 + t)*_x_unit;
for(int i=0; i<nproc; i++)
{
double ypos = (3*i+1)*_y_unit;
Schedulable::state st = processes[i]->get_state();
switch(st)
{
case Schedulable::state_running:
cairo_set_source_rgb(ctx, 0, 1, 0);
cairo_rectangle(ctx, xpos, ypos, _x_unit, _y_unit);
cairo_fill(ctx);
break;
case Schedulable::state_ready:
cairo_set_source_rgb(ctx, 1, 1, 0);
cairo_rectangle(ctx, xpos, ypos, _x_unit, _y_unit);
cairo_fill(ctx);
break;
case Schedulable::state_blocked:
cairo_set_source_rgb(ctx, 1, 0, 0);
cairo_rectangle(ctx, xpos, ypos, _x_unit, _y_unit);
cairo_fill(ctx);
break;
case Schedulable::state_future:
break;
case Schedulable::state_terminated:
if(!terminated[i])
{
cairo_set_source_rgb(ctx, 0, 0, 0);
cairo_rectangle(ctx, xpos, ypos, _x_unit, _y_unit);
cairo_fill(ctx);
}
terminated[i] = true;
break;
}
}
}
}
delete[] terminated;
}
void
TestWidget::calc_drawing_size(size_t& width, size_t& height) const
{
if(!_simulation)
return;
const History& hist = _simulation->get_history();
const Environment::Processes& processes = hist.get_last_environment().get_processes();
width = (10 + 1 + hist.get_size()) * _x_unit;
height = 3 * processes.size() * _y_unit;
}
void
TestWidget::change_scaling_mode()
{
std::cout << " change_scaling_mode " << std::endl;
scaling_mode scaling = get_scaling_mode();
switch(scaling)
{
case scaling_none:
set_scaling_mode(scaling_to_w);
break;
case scaling_to_w:
case scaling_to_h:
case scaling_min:
case scaling_max:
set_scaling_mode((scaling_mode)(scaling << 1));
break;
case scaling_all:
set_scaling_mode(scaling_none);
break;
}
redraw();
}
// ------------------------------------------------------
//
// MainWindow class
//
// ------------------------------------------------------
class MainWindow : public Gtk::Window
{
public:
MainWindow();
virtual ~MainWindow();
protected:
virtual void on_button_start_clicked();
virtual void on_button_stop_clicked();
TestWidget _test_widget;
SimulationWidget _simulation_widget;
Gtk::Button _start_button;
Gtk::Button _stop_button;
Gtk::HBox _buttons_box;
Gtk::VBox _main_box;
};
MainWindow::MainWindow() : _start_button("Start"), _stop_button("Stop")
{
// This just sets the title of our new window.
set_title("Simulation Widget Test");
add(_main_box);
_buttons_box.pack_start(_start_button);
_start_button.show();
_buttons_box.pack_start(_stop_button);
_stop_button.show();
_main_box.pack_start(_buttons_box, Gtk::PACK_SHRINK);
_buttons_box.show();
// _main_box.pack_start(_test_widget);
// _test_widget.show();
_main_box.pack_start(_simulation_widget);
_simulation_widget.show();
_main_box.show();
// Simulation::get_instance().attach(_simulation_widget);
_start_button.signal_clicked().connect( sigc::mem_fun(*this, &MainWindow::on_button_start_clicked) );
_stop_button.signal_clicked().connect( sigc::mem_fun(*this, &MainWindow::on_button_stop_clicked) );
}
MainWindow::~MainWindow()
{
}
void MainWindow::on_button_start_clicked()
{
Simulation::get_instance().run();
}
void MainWindow::on_button_stop_clicked()
{
Simulation::get_instance().stop();
_simulation_widget.redraw();
}
int
main(int argc, char** argv)
{
ostream& info = cout;
ostream& test = cerr;
Gtk::Main kit(argc, argv);
// Set up Glib thread support
Glib::thread_init();
info << "test-holt - start \n";
Simulation& simu = Simulation::get_instance();
info << "gets simulation \n";
History& hist = Simulation::get_instance().get_history();
info << "gets history \n";
DummyPolicyManager dummy_manager;
/*
DummyPolicy dummy_policy;
PRRPolicy PRR_Policy;
*/
const std::vector<CPUPolicy*>& policies = dummy_manager.get_avail_policies();
CPUPolicy* pol = policies[0];
info << "policy " << pol->get_name() << endl;
// CPUPoliciesGatekeeper::get_instance().register_manager(&dummy_manager);
fillHistory(hist);
info << "history filled \n";
// CPUPoliciesGatekeeper::get_instance().activate_policy(&hist, pol);
simu.set_policy(pol);
info << "policy activated \n";
simu.set_mode(false);
simu.set_timer(100);
info << "simulation set_mode single step\n";
info << "START environment dump \n";
dumpEnvironment(hist.get_last_environment(), info);
info << "END environment dump \n";
info << "simulation state: " << get_simulation_state_name(simu.get_state()) << endl;
// Gtk::Window win;
MainWindow win;
win.set_border_width(10);
win.resize (700, 200);
Gtk::Main::run(win);
return 0;
}
// insert some resources, processes, threads, requests, subrequests
void fillHistory(History &hist)
{
// insert resources to delete soon
// this create a numbering of keys starting from 2...
// serves to prove correct save/restore independent from key numbering
// add a resource - name, preemptable, places, availability
History::ResourcePair respair0 = hist.add_resource(Glib::ustring("Resource 0"), false, 1, 2);
// add a resource - name, preemptable, places, availability
History::ResourcePair respair00 = hist.add_resource(Glib::ustring("Resource 00"), false, 1, 2);
// add a resource - name, preemptable, places, availability
History::ResourcePair respair = hist.add_resource(Glib::ustring("Resource 1"), false, 1, 2);
// add a resource - name, preemptable, places, availability
History::ResourcePair respair2 = hist.add_resource(Glib::ustring("Invalid? Resource <n> 1"), false, 1, 2);
// delete resources to kreate a key numbering hole
hist.remove(respair0.first);
hist.remove(respair00.first);
// add a process - name, arrival time, priority
Process& p1 = hist.add_process(Glib::ustring("Process 1"), 0, 2); // name, arrival time, priority
// add a process - name, arrival time, priority
Process& p2 = hist.add_process(Glib::ustring("Process 2"), 7, 3); // name, arrival time, priority
// add a process - name, arrival time, priority
Process& p3 = hist.add_process(Glib::ustring("Process 3"), 5, 5); // name, arrival time, priority
// add a process - name, arrival time, priority
Process& p4 = hist.add_process(Glib::ustring("Invalid? <process/> &3 or\\4"), 9, 1); // name, arrival time, priority
// add a thread - name, parent, cpu time, arrival time, priority
Thread& p1_t1 = hist.add_thread(Glib::ustring("P1 - Thread 1"), p1, 14, 0, 2);
// add a thread - name, parent, cpu time, arrival time, priority
// Thread& p1_t2 = hist.add_thread(Glib::ustring("P1 - Thread 2"), p1, 3, 0, 5);
// add a thread - name, parent, cpu time, arrival time, priority
Thread& p2_t1 = hist.add_thread(Glib::ustring("P2 - Thread 1"), p2, 20, 0, 2);
// add a thread - name, parent, cpu time, arrival time, priority
Thread& p3_t1 = hist.add_thread(Glib::ustring("P3 - Thread 2"), p3, 12, 0, 2);
// add a thread - name, parent, cpu time, arrival time, priority
Thread& p4_t1 = hist.add_thread(Glib::ustring("P4 - Thread 1"), p4, 3, 0, 2);
// add a request - Thread, time
Request& req1 = hist.add_request(p1_t1, 3);
// add a sub request - Request, resource_key, duration, places
SubRequest& req1_sub1 = hist.add_subrequest(req1, respair.first, 5);
}
Glib::ustring get_schedulable_state_name(Schedulable::state st)
{
switch(st)
{
case Schedulable::state_running:
return Glib::ustring("Schedulable::state_running");
break;
case Schedulable::state_ready:
return Glib::ustring("Schedulable::state_ready");
break;
case Schedulable::state_blocked:
return Glib::ustring("Schedulable::state_blocked");
break;
case Schedulable::state_future:
return Glib::ustring("Schedulable::state_future");
break;
case Schedulable::state_terminated:
return Glib::ustring("Schedulable::state_terminated");
break;
}
}
Glib::ustring get_request_state_name(Request::state st)
{
switch(st)
{
case Request::state_unallocable:
return Glib::ustring("state_unallocable");
break;
case Request::state_allocated:
return Glib::ustring("state_allocated");
break;
case Request::state_future:
return Glib::ustring("state_future");
break;
case Request::state_exhausted:
return Glib::ustring("state_exhausted");
break;
case Request::state_allocable:
return Glib::ustring("state_allocable");
break;
}
}
Glib::ustring get_simulation_state_name(Simulation::state st)
{
switch(st)
{
case Simulation::state_running:
return Glib::ustring("Simulation::state_running");
break;
case Simulation::state_paused:
return Glib::ustring("Simulation::state_paused");
break;
case Simulation::state_stopped:
return Glib::ustring("Simulation::state_stopped");
break;
}
}
// print entire environment into the passed ostream
void dumpEnvironment(const Environment& env, ostream &os)
{
os << "dump environment start " << endl;
const Environment::Resources& rvect = env.get_resources();
typedef Environment::Resources::const_iterator res_iterator;
res_iterator riter = rvect.begin();
while (riter != rvect.end())
{
Resource* r = (*riter).second;
os << " resource name: " << r->get_name()
/* << " key: " << (*riter).first */
<< " places: " << r->get_places() << endl;
riter++;
}
const Environment::Processes& pvect = env.get_processes();
typedef std::vector<Process*>::const_iterator proc_iterator;
proc_iterator iter = pvect.begin();
proc_iterator end = pvect.end();
while (iter != end)
{
Process* p = (*iter);
os << " process name: " << p->get_name()
<< " arrival_time: " << p->get_arrival_time()
<< " base_priority: " << p->get_base_priority()
<< " " << get_schedulable_state_name(p->get_state())
<< endl;
iter++;
typedef std::vector<Thread*> Threads;
typedef std::vector<Thread*>::const_iterator thr_iterator;
const Threads& tvect = p->get_threads();
thr_iterator iter1 = tvect.begin();
thr_iterator end1 = tvect.end();
while (iter1 != end1)
{
Thread* t = (*iter1);
os << " thread name: " << t->get_name()
<< " arrival_time: " << t->get_arrival_time()
<< " base_priority: " << t->get_base_priority()
<< " " << get_schedulable_state_name(t->get_state())
<< endl;
typedef std::vector<Request*> Requests;
typedef std::vector<Request*>::const_iterator req_iterator;
const Requests& rvect = t->get_requests();
req_iterator iter2 = rvect.begin();
req_iterator end2 = rvect.end();
while (iter2 != end2)
{
Request* r = (*iter2);
os << " request arrival_time: " << r->get_instant() << endl;
typedef std::vector<SubRequest*> SubRequests;
typedef std::vector<SubRequest*>::const_iterator subreq_iterator;
const SubRequests& srvect = r->get_subrequests();
subreq_iterator iter3 = srvect.begin();
subreq_iterator end3 = srvect.end();
while (iter3 != end3)
{
SubRequest* sr = (*iter3);
os << " sub request: " /* << " resource_key: " << sr->get_resource_key() */;
Environment::Resources::const_iterator pos = env.get_resources().find(sr->get_resource_key());
if (pos != env.get_resources().end())
{
os << " name: " << pos->second->get_name();
}
os << " length: " << sr->get_length()
<< " " << get_request_state_name(sr->get_state())
<< endl;
iter3++;
}
iter2++;
}
iter1++;
}
}
os << "dump environment end " << endl << endl;
}