- added src/holt_widget

- added src/testsuite/test-holt_widget
- updated Makefile.am



git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@928 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
paolo 2006-08-22 01:38:30 +00:00
parent 495896597c
commit b3335ed467
7 changed files with 1759 additions and 182 deletions

View File

@ -358,6 +358,7 @@ if COND_TESTS
noinst_PROGRAMS = \
src/testsuite/test-cairo_widget \
src/testsuite/test-history \
src/testsuite/test-holt_widget \
src/testsuite/test-simulation_widget
# disable :
@ -422,6 +423,26 @@ src_testsuite_test_simulation_widget_SOURCES = \
src/simulation_widget.cc \
src/testsuite/test-simulation_widget.cc
src_testsuite_test_holt_widget_CPPFLAGS = \
-I@top_srcdir@/src \
-I@top_srcdir@/src/templates \
$(CAIRO_CFLAGS) \
$(GTKMM_CFLAGS) \
$(GLIBMM_CFLAGS) \
$(GTHREAD_CFLAGS)
src_testsuite_test_holt_widget_LDFLAGS = \
src/backend/libbackend.la \
$(CAIRO_LIBS) \
$(GTKMM_LIBS) \
$(GLIBMM_LIBS) \
$(GTHREAD_LIBS)
src_testsuite_test_holt_widget_SOURCES = \
src/cairo_elements.cc \
src/cairo_widget.cc \
src/simulation_widget.cc \
src/holt_widget.cc \
src/testsuite/test-holt_widget.cc
#src_testsuite_test_parse_command_CPPFLAGS = \
# -I@top_srcdir@/src \
# -I@top_srcdir@/src/templates \

View File

@ -85,12 +85,13 @@ namespace sgpem
typedef std::pair<area_t, method0_t> area_callback_t;
typedef std::vector<area_callback_t> areas_vect_t;
mutable size_t _draw_w;
mutable size_t _draw_h;
private:
// The width and height the widget will assume, must be determined
// before starting drawing by calc_size()
// drawing dimensions required
mutable size_t _draw_w;
mutable size_t _draw_h;
// window client area
mutable size_t _client_w;
mutable size_t _client_h;

683
src/holt_widget.cc Normal file
View File

@ -0,0 +1,683 @@
// src/simulation_widget.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 "cairo_elements.hh"
#include "backend/history.hh"
#include "backend/process.hh"
#include "backend/resource.hh"
#include "backend/simulation.hh"
#include "backend/string_utils.hh"
#include "backend/thread.hh"
#include "holt_widget.hh"
#include <math.h>
#include <iostream>
#include <cassert>
#ifndef NDEBUG
#include <iostream>
#endif
using namespace sgpem;
using namespace std;
HoltNode::HoltNode(Vec2 pt)
: _radius(20)
{
//_x = pt.real(); _y = pt.imag();
_pos = pt;
}
HoltNode::~HoltNode()
{
}
Vec2 HoltNode::set_position(Vec2 pt)
{
Vec2 pt_old = _pos;
_pos = pt;
return pt_old;
}
Vec2 HoltNode::get_position()
{
return _pos;
}
double HoltNode::set_radius(double radius)
{
double old_rad = _radius;
_radius = radius;
return old_rad;
}
double HoltNode::get_radius()
{
return _radius;
}
// double HoltNode::_radius = 20;
HoltResource::HoltResource(const Resource& resource, resource_key_t resource_key, Vec2 pt)
: HoltNode(pt)
{
_resource = &resource;
_resource_key = resource_key;
}
HoltResource::~HoltResource()
{
}
void HoltResource::draw(cairo_t *cr)
{
// draw rectangle
cairo_rectangle(cr,
_pos.real() - _radius, _pos.imag() - _radius,
2*_radius, 2*_radius);
// fill
cairo_set_source_rgb (cr, 1, 0.5, 1);
cairo_fill_preserve (cr);
// outline
cairo_set_source_rgb (cr, 0, 0, 0);
// clip text outside region
cairo_clip_preserve (cr);
// draw text
cairo_text_extents_t extents;
cairo_text_extents(cr, _resource->get_name().c_str(), &extents);
// cairo_move_to(cr, xpos - _radius, ypos - _radius + extents.height);
cairo_move_to(cr, _pos.real() - extents.width/2, _pos.imag() + extents.height/2);
cairo_show_text(cr, _resource->get_name().c_str());
cairo_reset_clip (cr); // reset clip region
// stroke all
cairo_stroke (cr);
}
Vec2 HoltResource::get_intersection_to(Vec2 pt)
{
Vec2 final = _pos;
Vec2 segment = pt - _pos;
double len = std::abs(segment);
if(len>0)
{
// segment to unary modulus vector (versor)
segment /= len;
if(abs(segment.real())>=M_SQRT1_2)
{
// direction East(>0) or West(<0)
segment *= _radius/abs(segment.real());
}
else
{
// direction North(<0) or South(>0)
segment *= _radius/abs(segment.imag());
}
final = _pos + segment;
}
return final;
}
HoltProcess::HoltProcess(const Process& process, Vec2 pt)
: HoltNode(pt)
{
_process = &process;
}
HoltProcess::~HoltProcess()
{
}
void HoltProcess::draw(cairo_t *cr)
{
cairo_save(cr); // save context state
// draw circle
cairo_arc (cr, _pos.real(), _pos.imag(), _radius, 0, 2 * M_PI);
// filling
switch(_process->get_state())
{
case Schedulable::state_running:
cairo_set_source_rgb (cr, 0.5, 1, 0.5);
break;
case Schedulable::state_ready:
cairo_set_source_rgb (cr, 1, 1, 0.5);
break;
case Schedulable::state_blocked:
cairo_set_source_rgb (cr, 1, 0.5, 0.5);
break;
case Schedulable::state_future:
cairo_set_source_rgb (cr, 0.5, 0.5, 1);
break;
case Schedulable::state_terminated:
cairo_set_source_rgb (cr, 0.5, 0.5, 0.5);
break;
}
cairo_fill_preserve (cr);
// outline
cairo_set_source_rgb (cr, 0, 0, 0);
// clip text outside region
cairo_clip_preserve (cr);
// draw text
cairo_text_extents_t extents;
cairo_text_extents(cr, _process->get_name().c_str(), &extents);
// cairo_move_to(cr, xpos - _radius, ypos - _radius + extents.height);
cairo_move_to(cr, _pos.real() - extents.width/2, _pos.imag() + extents.height/2);
cairo_show_text(cr, _process->get_name().c_str());
cairo_reset_clip (cr); // reset clip region
// stroke all
cairo_stroke (cr);
cairo_restore(cr); // restore context state
}
Vec2 HoltProcess::get_intersection_to(Vec2 pt)
{
Vec2 final = _pos;
Vec2 segment = pt - _pos;
double len = std::abs(segment);
if(len>0)
{
segment *= _radius/len;
final = _pos + segment;
}
return final;
}
HoltRequest::HoltRequest(HoltProcess& hp, HoltResource& hr, Request::state state)
: _hp(&hp),
_hr(&hr),
_state(state)
{
}
HoltRequest::~HoltRequest()
{
}
void HoltRequest::draw(cairo_t *cr)
{
Vec2 resource = _hp->get_intersection_to(_hr->get_position());
Vec2 schedulable = _hr->get_intersection_to(_hp->get_position());
cairo_save(cr);
// cairo_set_line_width(cr, 0.5*cairo_get_line_width(cr));
switch(_state)
{
case Request::state_unallocable:
cairo_set_source_rgb(cr, 1, 0, 0);
arrow(cr, schedulable, resource);
break;
case Request::state_allocated:
cairo_set_source_rgb(cr, 0, 1, 0);
arrow(cr, resource, schedulable);
break;
case Request::state_future:
break;
case Request::state_exhausted:
break;
case Request::state_allocable:
cairo_set_source_rgb(cr, 1, 1, 0);
arrow(cr, schedulable, resource);
break;
}
// stroke all
cairo_stroke (cr);
cairo_restore(cr);
}
void HoltRequest::arrow(cairo_t *cr, Vec2 first, Vec2 second)
{
cairo_move_to(cr, second.real(), second.imag());
cairo_line_to(cr, first.real(), first.imag());
Vec2 arrowside = second-first;
arrowside *= 10.0/std::abs(arrowside);
Vec2 deviation(5.0, 1.0); deviation /= std::abs(deviation);// = std::polar(1,M_PI/6.0);
Vec2 side1 = arrowside * deviation;
Vec2 side2 = arrowside * conj(deviation);
cairo_move_to(cr, first.real(), first.imag());
cairo_line_to(cr, first.real()+side1.real(), first.imag()+side1.imag());
cairo_move_to(cr, first.real(), first.imag());
cairo_line_to(cr, first.real()+side2.real(), first.imag()+side2.imag());
}
HoltWidget::HoltWidget(Simulation& simulation)
: Glib::ObjectBase("sgpem_HoltWidget"),
CairoWidget(),
SimulationObserver(),
HistoryObserver(),
_simulation(&simulation),
_n_proc(0),
_n_res(0),
_radius(20),
_arrange_mode(arrange_horizontal)
{
// Register this SimulationObserver:
_simulation->attach(*this);
// Register this HistoryObserver:
_simulation->get_history().attach(*this);
}
HoltWidget::~HoltWidget()
{
// Unregister this HistoryObserver:
_simulation->get_history().detach(*this);
// Unregister this SimulationObserver:
_simulation->detach(*this);
}
double
HoltWidget::get_radius()
{
return _arrange_mode;
}
double
HoltWidget::set_radius(double radius)
{
double old_radius = _radius;
if(radius>0)
_radius = radius;
resize_redraw();
return old_radius;
}
HoltWidget::arrange_mode
HoltWidget::get_arrange_mode()
{
return _arrange_mode;
}
HoltWidget::arrange_mode
HoltWidget::set_arrange_mode(arrange_mode mode)
{
arrange_mode old_mode = _arrange_mode;
_arrange_mode = mode;
arrange();
resize_redraw();
return old_mode;
}
void
HoltWidget::arrange()
{
// cout << "START:" << endl;
// _x_req = 0;
// _y_req = 0;
Vec2 pos, inc, mul, cen;
if(_arrange_mode==arrange_horizontal)
{
pos = Vec2(2*_radius, 2*_radius);
inc = Vec2(3*_radius, 0);
}
else if(_arrange_mode==arrange_vertical)
{
pos = Vec2(2*_radius, 2*_radius);
inc = Vec2(0, 3*_radius);
}
else // _arrange_mode==arrange_circular
{
int sx = _draw_w; // get_width();
int sy = _draw_h; // get_height();
int nelem = _holt_resources.size()+_holt_processes.size();
if(sx<=sy)
inc = Vec2(sx/2-2*_radius, 0);
else
inc = Vec2(sy/2-2*_radius, 0);
if(nelem>0)
mul = Vec2(cos(2*M_PI/(double)nelem), sin(2*M_PI/(double)nelem));
else
mul = Vec2(0,0);
// cen = Vec2(sx/2, sy/2);
cen = Vec2(2*_radius+inc.real(), 2*_radius+inc.real());
pos = inc + cen;
// cout << "A) pos=" << pos << " cen=" << cen << " inc=" << inc << " mul=" << mul << endl;
}
HoltResources::const_iterator riter = _holt_resources.begin();
while(riter!=_holt_resources.end())
{
HoltResource* hr = (*riter).second;
// cout << "r-A) pos=" << pos << " cen=" << cen << " inc=" << inc << " mul=" << mul << endl;
hr->set_position(pos);
hr->set_radius(_radius);
/*
if(pos.real()+_radius>_x_req)
_x_req = pos.real()+_radius;
if(pos.imag()+_radius>_y_req)
_y_req = pos.imag()+_radius;
*/
if(_arrange_mode!=arrange_circular)
{
pos += inc;
}
else // _arrange_mode==arrange_circular
{
inc *= mul;
pos = cen + inc;
}
riter++;
// cout << "r-B) pos=" << pos << " cen=" << cen << " inc=" << inc << " mul=" << mul << endl;
}
if(_arrange_mode==arrange_horizontal)
{
pos = Vec2(2*_radius, 8*_radius);
inc = Vec2(3*_radius, 0);
}
else if(_arrange_mode==arrange_vertical)
{
pos = Vec2(8*_radius, 2*_radius);
inc = Vec2(0, 3*_radius);
}
typedef HoltProcesses::const_iterator holt_proc_iterator;
holt_proc_iterator piter = _holt_processes.begin();
while(piter!=_holt_processes.end())
{
HoltProcess* hp = (*piter);
// cout << "p-A) pos=" << pos << " cen=" << cen << " inc=" << inc << " mul=" << mul << endl;
hp->set_position(pos);
hp->set_radius(_radius);
/*
if(pos.real()+_radius>_x_req)
_x_req = pos.real()+_radius;
if(pos.imag()+_radius>_y_req)
_y_req = pos.imag()+_radius;
*/
if(_arrange_mode!=arrange_circular)
{
pos += inc;
}
else // _arrange_mode==arrange_circular
{
inc *= mul;
pos = cen + inc;
}
// cout << "p-B) pos=" << pos << " cen=" << cen << " inc=" << inc << " mul=" << mul << endl;
piter++;
}
}
void
HoltWidget::update(const Simulation& changed_simulation)
{
// Force redraw
//redraw();
acquire();
resize_redraw();
}
void
HoltWidget::update(const History& changed_history)
{
// Force redraw
//redraw();
//acquire();
//resize_redraw();
}
void
HoltWidget::acquire()
{
_holt_resources.clear();
_holt_processes.clear();
_holt_requests.clear();
_n_res = _n_proc = 0;
const History& hist = _simulation->get_history();
const Environment& env = hist.get_last_environment();
Vec2 pos(2*_radius, 2*_radius);
const Environment::Resources& rvect = env.get_resources();
Environment::Resources::const_iterator riter = rvect.begin();
while(riter!=rvect.end())
{
resource_key_t index = (*riter).first;
Resource* r = (*riter).second;
HoltResource *hr = new HoltResource(*r, index, pos);
HoltResources::iterator temp = _holt_resources.insert(std::pair<resource_key_t,HoltResource*>(index, hr)).first;
_n_res++;
riter++;
pos += Vec2(4*_radius, 0);
}
pos = Vec2(2*_radius, 8*_radius);
// iter trough processes
const Environment::Processes& pvect = env.get_processes();
Environment::Processes::const_iterator proc_iter = pvect.begin();
while(proc_iter!=pvect.end())
{
Process* p = (*proc_iter);
proc_iter++;
Schedulable::state proc_state = p->get_state();
if(proc_state==Schedulable::state_running
|| proc_state==Schedulable::state_ready
|| proc_state==Schedulable::state_blocked )
{
_n_proc++;
HoltProcess *hp = new HoltProcess(*p, pos);
_holt_processes.push_back(hp);
pos += Vec2(4*_radius, 0);
// iter trough threads, requests, subrequests
// FIXME
// typedef std::vector<Thread*> Threads;
// typedef std::vector<Thread*>::const_iterator thr_iterator;
const std::vector<Thread*>& tvect = p->get_threads();
std::vector<Thread*>::const_iterator thr_iter = tvect.begin();
while(thr_iter!=tvect.end())
{
Thread* t = (*thr_iter);
thr_iter++;
/*
os << " thread name: " << t->get_name()
<< " arrival_time: " << t->get_arrival_time()
<< " base_priority: " << t->get_base_priority() << endl;
*/
Schedulable::state thr_state = t->get_state();
if(thr_state==Schedulable::state_running
|| thr_state==Schedulable::state_ready
|| thr_state==Schedulable::state_blocked )
{
// iter trough requests
const std::vector<Request*>& rvect = t->get_requests();
std::vector<Request*>::const_iterator req_iter = rvect.begin();
while(req_iter!=rvect.end())
{
Request* r = (*req_iter);
req_iter++;
// os << " request arrival_time: " << r->get_instant() << endl;
// iter trough subrequests
const std::vector<SubRequest*>& srvect = r->get_subrequests();
std::vector<SubRequest*>::const_iterator subr_iter = srvect.begin();
while(subr_iter!=srvect.end())
{
SubRequest* sr = (*subr_iter);
subr_iter++;
// os << " sub request: " /* << " resource_key: " << sr->get_resource_key() */;
Request::state subr_state = sr->get_state();
if(subr_state==Request::state_unallocable
|| subr_state==Request::state_allocated
|| subr_state==Request::state_allocable )
{
Environment::Resources::const_iterator pos = env.get_resources().find(sr->get_resource_key());
HoltResources::const_iterator hpos = _holt_resources.find(sr->get_resource_key());
if (pos != env.get_resources().end() && hpos!=_holt_resources.end())
{
HoltRequest *hreq = new HoltRequest(*hp, *(hpos->second), sr->get_state());
_holt_requests.push_back(hreq);
// cout << "added HoltRequest\n"
// os << " name: " << pos->second->get_name();
}
} // ~ if(subr_state==Request::state_unallocable ... etc
/*
os << " length: " << sr->get_length();
os << " state: " << sr->get_state() << endl;
*/
} // ~ while(subr_iter!=srvect.end())
} // ~ while(req_iter!=rvect.end())
} // ~ if(thr_state==Schedulable::state_running ...
} // ~ while(thr_iter!=tvect.end())
} // ~ if(proc_state==Schedulable::state_running ...etc
}
// arrange();
}
void
HoltWidget::draw_widget(cairo_t* ctx)
{
arrange();
typedef HoltResources::const_iterator holt_res_iterator;
holt_res_iterator riter = _holt_resources.begin();
while(riter!=_holt_resources.end())
{
HoltResource* hr = (*riter).second;
hr->draw(ctx);
riter++;
}
typedef HoltProcesses::const_iterator holt_proc_iterator;
holt_proc_iterator piter = _holt_processes.begin();
while(piter!=_holt_processes.end())
{
HoltProcess* hp = (*piter);
hp->draw(ctx);
piter++;
}
cairo_set_line_width (ctx, 0.5 * cairo_get_line_width (ctx));
typedef HoltRequests::const_iterator holt_requ_iterator;
holt_requ_iterator reqiter = _holt_requests.begin();
while(reqiter!=_holt_requests.end())
{
HoltRequest* hreq = (*reqiter);
hreq->draw(ctx);
reqiter++;
}
}
void
HoltWidget::calc_drawing_size(cairo_t* ctx, size_t& width, size_t& height)
{
// int pos = _simulation->get_front();
// const History& hist = _simulation->get_history();
int max = _n_proc;
if(_n_res>_n_proc)
max = _n_res;
cairo_text_extents_t extents;
// std::cout << " x_unit: " << std::endl;
Glib::ustring val("Process 999 Resource 999");
cairo_text_extents(ctx, val.c_str(), &extents);
_radius = extents.width/4.0;
switch(_arrange_mode)
{
case arrange_horizontal:
width = max * 4 * _radius;
height = 10 * _radius;
break;
case arrange_vertical:
width = 10 * _radius;
height = max * 4 * _radius;
break;
case arrange_circular:
int tot = (_n_proc + _n_res);
double alpha = 0;
if(tot>0)
alpha = M_PI / tot;
double side = 2 * sin(alpha/2);
double diam = 4 * _radius;
if(side>0)
{
diam = 4 * _radius / side;
}
// double circ = 4 * _radius * (_n_proc + _n_res);
// double diam = circ / M_PI;
if(diam<4 * _radius)
diam = 4 * _radius;
width = height = diam + 4 * _radius;
break;
}
// cout << "Holt widget width=" << width << " height=" << height << endl;
}
void
HoltWidget::calc_widget_size(size_t& width, size_t& height)
{
width = height = 20; // default minimum size
}

256
src/holt_widget.hh Normal file
View File

@ -0,0 +1,256 @@
// src/simulation_widget.hh - 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
#ifndef HOLT_WIDGET_HH
#define HOLT_WIDGET_HH 1
#include "config.h"
#include "cairo_widget.hh"
#include "backend/history_observer.hh"
#include "backend/request.hh"
#include "backend/simulation_observer.hh"
#include <complex>
// *********************************************************************************
// ---------------------------------------------------------------------------------
//
// BEGIN - inserted from old version
//
namespace sgpem
{
class History;
class Process;
class Resource;
}
namespace sgpem
{
typedef SubRequest::resource_key_t resource_key_t;
/**
* \brief Every point or vector is a Vec2.
* This phisical vector class, Vec2 is realized trough
* complex<double> because it's a full operative class with operators.
*/
typedef std::complex<double> Vec2;
class HoltNode
{
public:
// HoltNode(double x=0.0, double y=0.0);
HoltNode(Vec2 pt = Vec2(0.0, 0.0));
virtual ~HoltNode();
virtual void draw(cairo_t *cr) = 0;
// void set_position(double x, double y);
Vec2 set_position(Vec2 pt);
Vec2 get_position();
double set_radius(double radius);
double get_radius();
virtual Vec2 get_intersection_to(Vec2 pt) = 0;
protected:
double _radius;
// double _x, _y;
Vec2 _pos;
private:
};
class HoltResource : public HoltNode
{
public:
// HoltResource(const Resource& resource, resource_key_t resource_key, double x=0.0, double y=0.0);
HoltResource(const Resource& resource, resource_key_t resource_key, Vec2 pt = Vec2(0.0, 0.0));
virtual ~HoltResource();
virtual void draw(cairo_t *cr);
virtual Vec2 get_intersection_to(Vec2 pt);
protected:
private:
const Resource* _resource;
resource_key_t _resource_key;
};
class HoltProcess : public HoltNode
{
public:
//HoltProcess(const Process& process, double x=0.0, double y=0.0);
HoltProcess(const Process& process, Vec2 pt = Vec2(0.0, 0.0));
virtual ~HoltProcess();
virtual void draw(cairo_t *cr);
virtual Vec2 get_intersection_to(Vec2 pt);
protected:
private:
const Process* _process;
};
class HoltRequest
{
public:
HoltRequest(HoltProcess& hp, HoltResource& hr, Request::state state);
virtual ~HoltRequest();
virtual void draw(cairo_t *cr);
virtual void arrow(cairo_t *cr, Vec2 first, Vec2 second);
protected:
private:
HoltProcess* _hp;
HoltResource* _hr;
Request::state _state;
};
/*
class HoltGraph : public Gtk::DrawingArea
{
public:
enum arrange_mode
{
arrange_horizontal = 1 << 0,
arrange_vertical = 1 << 1,
arrange_circular = 1 << 2
};
enum resize_mode
{
resize_none = 0,
resize_radius = 1 << 0,
resize_distance = 1 << 1,
resize_both = resize_radius | resize_distance,
resize_asymetrical = 0,
resize_symetrical = 1 << 2
};
typedef std::map<resource_key_t, HoltResource*> HoltResources;
typedef std::vector<HoltProcess*> HoltProcesses;
typedef std::vector<HoltRequest*> HoltRequests;
HoltGraph();
virtual ~HoltGraph();
void set_history(const sgpem::History& hist);
arrange_mode get_arrange_mode();
arrange_mode set_arrange_mode(arrange_mode mode);
resize_mode get_resize_mode();
resize_mode set_resize_mode(resize_mode mode);
void arrange();
protected:
//Overridden default signal handlers:
virtual void on_realize();
virtual bool on_expose_event(GdkEventExpose* e);
virtual void on_size_request(Gtk::Requisition* requisition);
virtual void on_size_allocate(Gtk::Allocation& allocation);
Glib::RefPtr<Gdk::GC> gc_;
Gdk::Color blue_, red_;
Gdk::Color filler_, back_;
private:
const sgpem::History* _hist;
double _radius;
double _eff_radius;
double _x_dist;
double _y_dist;
double _x_req;
double _y_req;
HoltResources _holt_resources;
HoltProcesses _holt_processes;
HoltRequests _holt_requests;
arrange_mode _arrange_mode;
resize_mode _resize_mode;
};
*/
} // end namespace sgpem
//
// END - inserted from old version
//
// ---------------------------------------------------------------------------------
// *********************************************************************************
namespace sgpem
{
class HoltWidget
: public SimulationObserver,
public HistoryObserver,
public CairoWidget
{
public:
enum arrange_mode
{
arrange_horizontal = 1 << 0,
arrange_vertical = 1 << 1,
arrange_circular = 1 << 2
};
typedef std::map<resource_key_t, HoltResource*> HoltResources;
typedef std::vector<HoltProcess*> HoltProcesses;
typedef std::vector<HoltRequest*> HoltRequests;
HoltWidget(Simulation& simulation);
virtual ~HoltWidget();
void update(const Simulation& changed_simulation);
void update(const History& changed_history);
void acquire();
double get_radius();
double set_radius(double radius);
arrange_mode get_arrange_mode();
arrange_mode set_arrange_mode(arrange_mode mode);
void arrange();
protected:
void draw_widget(cairo_t* ctx);
virtual void calc_drawing_size(cairo_t* ctx, size_t& width, size_t& height);
virtual void calc_widget_size(size_t& width, size_t& height);
private:
// int _x_unit;
//int _y_unit;
Simulation* _simulation;
double _radius;
HoltResources _holt_resources;
HoltProcesses _holt_processes;
HoltRequests _holt_requests;
int _n_proc;
int _n_res;
arrange_mode _arrange_mode;
};
} //~ namespace sgpem
#endif //~ HOLT_WIDGET_HH

View File

@ -60,7 +60,6 @@ SimulationWidget::~SimulationWidget()
_simulation->detach(*this);
}
#pragma argsused
void
SimulationWidget::update(const Simulation& changed_simulation)
{
@ -69,7 +68,6 @@ SimulationWidget::update(const Simulation& changed_simulation)
resize_redraw();
}
#pragma argsused
void
SimulationWidget::update(const History& changed_history)
{
@ -89,6 +87,14 @@ SimulationWidget::draw_widget(cairo_t* ctx)
*/
const History& hist = _simulation->get_history();
const Environment::Processes& processes = hist.get_last_environment().get_processes();
int nproc = processes.size();
if(nproc<1) // nothing to draw
return;
double text_maxw = 0;
bool* terminated = 0;
double top_margin = _y_unit;
double left_margin = _x_unit;
double top_graph_margin = 1.0 * _y_unit; //3.0 * _y_unit;
@ -98,11 +104,6 @@ SimulationWidget::draw_widget(cairo_t* ctx)
double process_bar_height = 1.0 * _y_unit;
double process_height = process_bar_height + 2*process_bar_delta;
Simulation::state sim_state = _simulation->get_state();
const History& hist = _simulation->get_history();
const Environment::Processes& processes = hist.get_last_environment().get_processes();
int nproc = processes.size();
double text_maxw = 0;
bool* terminated = 0;
cairo_text_extents_t extents;
// evaluate minimum x and y extensions

View File

@ -0,0 +1,787 @@
// 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 SimulationWidget class,
* and nothing else. */
#include "config.h"
#include "gettext.h"
#include <glibmm/main.h>
#include <glibmm/module.h>
#include <glibmm/ustring.h>
#include <glibmm/thread.h>
#include <gtkmm/box.h>
#include <gtkmm/button.h>
#include <gtkmm/checkbutton.h>
#include <gtkmm/main.h>
#include <gtkmm/radiobutton.h>
#include <gtkmm/scrolledwindow.h>
#include <gtkmm/window.h>
#include <cassert>
#include <iostream>
#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 "holt_widget.hh"
#include "simulation_widget.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)
{
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);
}
}
}
}
}
/*
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()
{
}
};
// ------------------------------------------------------
//
// MainWindow class
//
// contains and controls the tested widget
//
// ------------------------------------------------------
class MainWindow : public Gtk::Window
{
public:
MainWindow(Simulation& simulation);
virtual ~MainWindow();
protected:
virtual void on_button_start_clicked();
virtual void on_button_stop_clicked();
virtual void on_button_pause_clicked();
virtual void on_button_runmode_clicked();
virtual void on_buttons_radio_clicked();
virtual void on_buttons_radio_disp_clicked();
virtual bool on_timer_timeout();
// TestWidget _test_widget;
SimulationWidget _simulation_widget;
HoltWidget _holt_widget;
Gtk::Button _start_button;
Gtk::Button _stop_button;
Gtk::Button _pause_button;
Gtk::CheckButton _runmode_button;
Gtk::RadioButton _noscale_radio;
Gtk::RadioButton _fit_radio;
Gtk::RadioButton _stretch_radio;
Gtk::RadioButton _horizontal_radio;
Gtk::RadioButton _vertical_radio;
Gtk::RadioButton _circular_radio;
Gtk::HBox _buttons_box;
Gtk::HBox _buttons2_box;
Gtk::VBox _main_box;
Gtk::ScrolledWindow _simulation_scroller;
Gtk::ScrolledWindow _holt_scroller;
Simulation::state _sim_state;
};
MainWindow::MainWindow(Simulation& simulation)
: _start_button("Start"),
_stop_button("Stop"),
_pause_button("Pause"),
_runmode_button("Continue"),
_noscale_radio("no scale"),
_fit_radio("scale all"),
_stretch_radio("stretch"),
_horizontal_radio("dispose horizontal"),
_vertical_radio("dispose vertical"),
_circular_radio("dispose circular"),
_simulation_widget(simulation),
_holt_widget(simulation),
_sim_state(Simulation::state_stopped)
{
// This just sets the title of our new window.
set_title("Holt Graph Widget Test");
add(_main_box);
_buttons_box.pack_start(_start_button);
_start_button.show();
_buttons_box.pack_start(_stop_button);
_stop_button.show();
_buttons_box.pack_start(_pause_button);
_pause_button.show();
_buttons_box.pack_start(_runmode_button);
_runmode_button.set_active(false);
_runmode_button.show();
// scale radio buttons
Gtk::RadioButton::Group group = _noscale_radio.get_group();
_fit_radio.set_group(group);
_stretch_radio.set_group(group);
_buttons_box.pack_start(_noscale_radio);
_noscale_radio.set_active();
_noscale_radio.show();
_buttons_box.pack_start(_fit_radio);
_fit_radio.show();
_buttons_box.pack_start(_stretch_radio);
_stretch_radio.show();
_main_box.pack_start(_buttons_box, Gtk::PACK_SHRINK);
_buttons_box.show();
_simulation_scroller.add(_simulation_widget);
_simulation_widget.show();
_simulation_scroller.show();
_main_box.pack_start(_simulation_scroller);
// dispose radio buttons
Gtk::RadioButton::Group group2 = _horizontal_radio.get_group();
_vertical_radio.set_group(group2);
_circular_radio.set_group(group2);
_buttons2_box.pack_start(_horizontal_radio);
_horizontal_radio.set_active();
_horizontal_radio.show();
_buttons2_box.pack_start(_vertical_radio);
_vertical_radio.show();
_buttons2_box.pack_start(_circular_radio);
_circular_radio.show();
_main_box.pack_start(_buttons2_box, Gtk::PACK_SHRINK);
_buttons2_box.show();
_holt_scroller.add(_holt_widget);
_holt_widget.show();
_holt_scroller.show();
_main_box.pack_start(_holt_scroller);
_main_box.show();
// connect after standard signal handler
_start_button.signal_clicked().connect( sigc::mem_fun(*this, &MainWindow::on_button_start_clicked), true );
_stop_button.signal_clicked().connect( sigc::mem_fun(*this, &MainWindow::on_button_stop_clicked), true );
_pause_button.signal_clicked().connect( sigc::mem_fun(*this, &MainWindow::on_button_pause_clicked), true );
_runmode_button.signal_clicked().connect( sigc::mem_fun(*this, &MainWindow::on_button_runmode_clicked), true );
_noscale_radio.signal_clicked().connect( sigc::mem_fun(*this, &MainWindow::on_buttons_radio_clicked), true );
_fit_radio.signal_clicked().connect( sigc::mem_fun(*this, &MainWindow::on_buttons_radio_clicked), true );
_stretch_radio.signal_clicked().connect( sigc::mem_fun(*this, &MainWindow::on_buttons_radio_clicked), true );
_horizontal_radio.signal_clicked().connect( sigc::mem_fun(*this, &MainWindow::on_buttons_radio_disp_clicked), true );
_vertical_radio.signal_clicked().connect( sigc::mem_fun(*this, &MainWindow::on_buttons_radio_disp_clicked), true );
_circular_radio.signal_clicked().connect( sigc::mem_fun(*this, &MainWindow::on_buttons_radio_disp_clicked), true );
Glib::signal_timeout().connect(sigc::mem_fun(*this, &MainWindow::on_timer_timeout), 1000);
//is equivalent to:
/*
const Glib::RefPtr<Glib::TimeoutSource> timeout_source = Glib::TimeoutSource::create(1000);
timeout_source->connect(sigc::ptr_fun(&timeout_handler));
timeout_source->attach(Glib::MainContext::get_default());
*/
}
MainWindow::~MainWindow()
{
}
void MainWindow::on_button_start_clicked()
{
if(_sim_state == Simulation::state_stopped
|| _sim_state == Simulation::state_paused)
{
if(!_runmode_button.get_active())
{
// one step - drive directly simulation
Simulation& simu = Simulation::get_instance();
simu.run();
_sim_state = simu.get_state();
// _simulation_widget.redraw();
}
else
{
_sim_state = Simulation::state_running;
}
}
}
void MainWindow::on_button_stop_clicked()
{
if(_sim_state == Simulation::state_running
|| _sim_state == Simulation::state_paused)
{
_sim_state = Simulation::state_stopped;
Simulation::get_instance().stop();
// _simulation_widget.redraw();
}
}
void MainWindow::on_button_pause_clicked()
{
if(_sim_state == Simulation::state_running)
{
_sim_state = Simulation::state_paused;
}
else if(_sim_state == Simulation::state_paused)
{
_sim_state = Simulation::state_running;
}
}
void MainWindow::on_button_runmode_clicked()
{
}
void MainWindow::on_buttons_radio_clicked()
{
if(_noscale_radio.get_active())
{
// std::cout << "scaling_none" << endl;
_simulation_widget.set_scaling_mode(CairoWidget::scaling_none);
_holt_widget.set_scaling_mode(CairoWidget::scaling_none);
}
else if(_fit_radio.get_active())
{
// std::cout << "scaling_min" << endl;
_simulation_widget.set_scaling_mode(CairoWidget::scaling_min);
_holt_widget.set_scaling_mode(CairoWidget::scaling_min);
}
else
{
// std::cout << "scaling_all" << endl;
_simulation_widget.set_scaling_mode(CairoWidget::scaling_all);
_holt_widget.set_scaling_mode(CairoWidget::scaling_all);
}
_simulation_widget.resize_redraw();
_holt_widget.resize_redraw();
}
void MainWindow::on_buttons_radio_disp_clicked()
{
if(_horizontal_radio.get_active())
{
// std::cout << "_horizontal_radio" << endl;
_holt_widget.set_arrange_mode(HoltWidget::arrange_horizontal);
}
else if(_vertical_radio.get_active())
{
// std::cout << "_vertical_radio" << endl;
_holt_widget.set_arrange_mode(HoltWidget::arrange_vertical);
}
else
{
// std::cout << "_circular_radio" << endl;
_holt_widget.set_arrange_mode(HoltWidget::arrange_circular);
}
_holt_widget.resize_redraw();
}
bool MainWindow::on_timer_timeout()
{
// std::cout << "Timer sim stat= " << _sim_state << endl;
if(_sim_state == Simulation::state_running)
{
Simulation& simu = Simulation::get_instance();
simu.run();
// _simulation_widget.redraw();
if(simu.get_state()==Simulation::state_stopped)
_sim_state = Simulation::state_stopped;
}
return true;
}
// utility functions...
// 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);
// return the Schedulable::state readable string
Glib::ustring get_schedulable_state_name(Schedulable::state st);
// return the Request::state readable string
Glib::ustring get_request_state_name(Request::state st);
// return the Simulation::state readable string
Glib::ustring get_simulation_state_name(Simulation::state st);
// ------------------------------------------------------
//
// MainWindow class
//
// ------------------------------------------------------
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;
const std::vector<CPUPolicy*>& policies = dummy_manager.get_avail_policies();
CPUPolicy* pol = policies[0];
info << "policy " << pol->get_name() << endl;
fillHistory(hist);
info << "history filled \n";
simu.set_policy(pol);
info << "policy activated \n";
simu.set_mode(false);
simu.set_timer(0);
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;
MainWindow win(simu);
win.set_border_width(10);
win.resize (900, 600);
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, 6);
// add a request - Thread, time
Request& req2 = hist.add_request(p2_t1, 1);
// add a sub request - Request, resource_key, duration, places
SubRequest& req2_sub1 = hist.add_subrequest(req2, respair.first, 4);
// add a request - Thread, time
Request& req3 = hist.add_request(p3_t1, 0);
// add a sub request - Request, resource_key, duration, places
SubRequest& req3_sub1 = hist.add_subrequest(req3, respair.first, 5);
// add a request - Thread, time
Request& req3bis = hist.add_request(p3_t1, 7);
// add a sub request - Request, resource_key, duration, places
SubRequest& req3bis_sub1 = hist.add_subrequest(req3bis, 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;
}

View File

@ -18,7 +18,7 @@
// 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,
/* This executable tests for workingness of the SimulationWidget class,
* and nothing else. */
#include "config.h"
@ -223,178 +223,6 @@ protected:
/*
// ------------------------------------------------------
//
// 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