diff --git a/src/backend/history.cc b/src/backend/history.cc index dc8e2ad..271c6a2 100644 --- a/src/backend/history.cc +++ b/src/backend/history.cc @@ -47,41 +47,41 @@ History::get_instance() It can be NULL if time is out of range or if there are no running entities in the associated SchedulableList */ -smart_ptr +smart_ptr History::get_scheduled_at(int time) const { - if (time >= _total_time_elapsed || time < 0) //out of range - return smart_ptr(NULL); + if (time > _total_time_elapsed || time < 0) //out of range + return smart_ptr(NULL); //look for a runing entity - smart_ptr p = get_simulation_status_at(time); + smart_ptr p = get_simulation_status_at(time); for (uint i = 0; i < p->size(); i++) if (p->get_item_at(i)->get_state() == SchedulableStatus::state_running) - return smart_ptr(new SchedulableStatus(*(p->get_item_at(i)))); + return smart_ptr(new SchedulableStatus(*(p->get_item_at(i)))); - return smart_ptr(NULL); + return smart_ptr(NULL); } /** Returns a pointer to a copy of the SimulationStatus object relative to this instant or NULL if time is out of range. */ -smart_ptr +smart_ptr History::get_simulation_status_at(int time) const { if (time > _total_time_elapsed || time < 0) //out of range - return smart_ptr(NULL); + return smart_ptr(NULL); int trascorso = -1; for(vector::const_iterator i=_slices.begin(); i < _slices.end(); i++) if (time <= trascorso + i->get_duration()) //FOUND!! - return smart_ptr(new SchedulableList(*i->get_simulation_status())); + return smart_ptr(new SchedulableList(*i->get_simulation_status())); else //Go on... trascorso += i->get_duration(); //never reached if all slices are CONTIGUOUS (ans THIS shoul be!!)!!! - return smart_ptr(NULL); + return smart_ptr(NULL); } int @@ -100,7 +100,7 @@ History::enqueue_slice(const SchedulableList& status) if(_slices.size() == 0) { _slices.push_back(Slice(-1, 1, status)); - _total_time_elapsed = 1; + _total_time_elapsed++; notify(); return; } diff --git a/src/backend/history.hh b/src/backend/history.hh index a1a0e28..032f19b 100644 --- a/src/backend/history.hh +++ b/src/backend/history.hh @@ -51,8 +51,8 @@ namespace sgpem { public: - memory::smart_ptr get_scheduled_at(int time) const; - memory::smart_ptr get_simulation_status_at(int time) const; + memory::smart_ptr get_scheduled_at(int time) const; + memory::smart_ptr get_simulation_status_at(int time) const; int get_current_time() const; void enqueue_slice(const sgpem::SchedulableList& status); void truncate_at(int instant); diff --git a/src/backend/observed_subject.cc b/src/backend/observed_subject.cc index befbcd5..7eafc72 100644 --- a/src/backend/observed_subject.cc +++ b/src/backend/observed_subject.cc @@ -34,7 +34,8 @@ void ObservedSubject::notify() { for(vector::iterator i = _attached.begin(); i < _attached.end(); i++) - (*i)->update(); + (*i)->update(); + } /** diff --git a/src/backend/policy.cc b/src/backend/policy.cc index f64f8a0..43bc164 100644 --- a/src/backend/policy.cc +++ b/src/backend/policy.cc @@ -34,8 +34,8 @@ Policy::get_id() const } -const PolicyParameters& -Policy::get_parameters() const +PolicyParameters& +Policy::get_parameters() { return _parameters; } diff --git a/src/backend/policy.hh b/src/backend/policy.hh index c40d692..f72da51 100644 --- a/src/backend/policy.hh +++ b/src/backend/policy.hh @@ -22,6 +22,7 @@ #define POLICY_HH 1 #include "config.h" +#include "gettext.h" #include "glibmm/ustring.h" @@ -50,16 +51,15 @@ namespace sgpem virtual int get_time_slice() const = 0; virtual void set_time_slice(const int&) = 0; - const PolicyParameters& get_parameters() const; + PolicyParameters& get_parameters(); - private: + protected: PolicyParameters _parameters; - int _id; + int _id; }; }//~ namespace sgpem - #endif diff --git a/src/backend/schedulable_list.cc b/src/backend/schedulable_list.cc index 526ddb7..e97e2b1 100644 --- a/src/backend/schedulable_list.cc +++ b/src/backend/schedulable_list.cc @@ -134,7 +134,7 @@ SchedulableList::remove(const uint& position) } /** - Switches two elements in the queue. Returns FALSE if one of the indexes is out of range. + */ bool SchedulableList::insert_at(const uint& which, const uint& where) diff --git a/src/backend/scheduler.cc b/src/backend/scheduler.cc index f57d03d..ea42d79 100644 --- a/src/backend/scheduler.cc +++ b/src/backend/scheduler.cc @@ -75,29 +75,46 @@ Scheduler::get_policy() void Scheduler::step_forward() { + History& h = History::get_instance(); //****************** //check for arrivals and prepare the queue //****************** - smart_ptr initial = h.get_simulation_status_at(h.get_current_time()); + smart_ptr initial = h.get_simulation_status_at(h.get_current_time()); + if (!initial) + { + cout << _("\nNo initial state inserted!!\n"); + return; + } _ready_queue.clear(); + //adds running schedulable - smart_ptr running_ptr = h.get_scheduled_at(h.get_current_time()); + smart_ptr running_ptr = h.get_scheduled_at(h.get_current_time()); if (running_ptr) _ready_queue.add_at_top(*running_ptr); + + //adds the READY ones + for(uint rea=0; rea < initial->size(); rea++) + if (initial->get_item_at(rea)->get_state() == SchedulableStatus::state_ready) + _ready_queue.add_at_bottom(*initial->get_item_at(rea)); //adds each new ready schedulable and sorts the queue for(uint i=0; i < initial->size(); i++) - if (initial->get_item_at(i)->get_state() == SchedulableStatus::state_ready + if (initial->get_item_at(i)->get_state() == SchedulableStatus::state_future && (int)initial->get_item_at(i)->get_schedulable()->get_arrival_time() == h.get_current_time()) { - _ready_queue.add_at_bottom(*initial->get_item_at(i)); - - //pops the running schedulable only if the policy is not preemptive + //cout << "\nnuovo running: " << initial->get_item_at(i)->get_schedulable()->get_name(); + + //restore the old running schedulable if (_policy->is_pre_emptive() == false && running_ptr) _ready_queue.remove(0); - + + //adds the NEW one + _ready_queue.add_at_bottom(*initial->get_item_at(i)); + _ready_queue.get_item_at(_ready_queue.size()-1)->set_state(SchedulableStatus::state_ready); + initial->get_item_at(i)->set_state(SchedulableStatus::state_ready); + // Sort the queue _policy->sort_queue(event_schedulable_arrival); @@ -106,22 +123,27 @@ Scheduler::step_forward() _ready_queue.add_at_top(*running_ptr); } + //**************** // Check for termination //**************** if (running_ptr && running_ptr->get_cpu_time_left() == 0) { - //there was a running schedulable and it's terminated. Remove it! + //there is a running schedulable and it's terminated. Append at the bottom with the state TERMINATED for(uint i=0; i < _ready_queue.size(); i++) if (*_ready_queue.get_item_at(i) == *running_ptr) { + _ready_queue.add_at_bottom(*_ready_queue.get_item_at(i)); _ready_queue.remove(i); + _ready_queue.get_item_at(_ready_queue.size()-1)->set_state(SchedulableStatus::state_terminated); break; } + //cout << "\nTERMINATO!!"; + running_ptr = NULL; - //IF _ready_queue.size() == 0 sort_queue(...) is called but has no effect!! - _policy->sort_queue(event_schedulable_termination); + //IF _ready_queue.size() == 0 sort_queue(...) is called but has no effect!! + _policy->sort_queue(event_schedulable_termination); } //***************** @@ -134,30 +156,31 @@ Scheduler::step_forward() // Create the final list of schedulable //****************** - SchedulableList final = _ready_queue; - - if (final.size() != 0) - //the firs element IS the running one (if != *running_ptr then there is a CONTEXT SWICH) - final.get_item_at(0)->set_state(SchedulableStatus::state_running); + + if (_ready_queue.size() != 0 && (_ready_queue.get_item_at(0)->get_state() == SchedulableStatus::state_ready + || _ready_queue.get_item_at(0)->get_state() == SchedulableStatus::state_running)) + { + //the first ready element IS the running one (if != *running_ptr then there is a CONTEXT SWICH) + _ready_queue.get_item_at(0)->set_state(SchedulableStatus::state_running); + _ready_queue.get_item_at(0)->give_cpu_time(1); + } //all the others are ready - for (uint i = 1; i < final.size(); i++) - if (final.get_item_at(i)->get_state() == SchedulableStatus::state_running) - final.get_item_at(i)->set_state(SchedulableStatus::state_ready); + for (uint i = 1; i < _ready_queue.size(); i++) + if (_ready_queue.get_item_at(i)->get_state() == SchedulableStatus::state_running) + _ready_queue.get_item_at(i)->set_state(SchedulableStatus::state_ready); - //append blocked, future, terminated and the old running schedulables + //append blocked, future, and terminated schedulables for (uint i = 0; i < initial->size(); i++) if(initial->get_item_at(i)->get_state() == SchedulableStatus::state_blocked || initial->get_item_at(i)->get_state() == SchedulableStatus::state_future - || initial->get_item_at(i)->get_state() == SchedulableStatus::state_terminated - || (initial->get_item_at(i)->get_state() == SchedulableStatus::state_ready - && (int)initial->get_item_at(i)->get_schedulable()->get_arrival_time() != h.get_current_time()) ) - final.add_at_bottom(*initial->get_item_at(i)); - - h.enqueue_slice(final); + || initial->get_item_at(i)->get_state() == SchedulableStatus::state_terminated) + _ready_queue.add_at_bottom(*initial->get_item_at(i)); + + cout << "\n"; +/* for (uint i = 0; i < _ready_queue.size(); i++) + cout << " " << _ready_queue.get_item_at(i)->get_schedulable()->get_name() + <<"_" << _ready_queue.get_item_at(i)->get_state(); + */ + h.enqueue_slice(_ready_queue); } - - - - - diff --git a/src/backend/scheduler.hh b/src/backend/scheduler.hh index c0b895a..be6c770 100644 --- a/src/backend/scheduler.hh +++ b/src/backend/scheduler.hh @@ -28,7 +28,7 @@ namespace sgpem #include "config.h" #include - +#include #include "observed_subject.hh" #include "history.hh" diff --git a/src/graphical_simulation.hh b/src/graphical_simulation.hh index 8cc7a69..766b0f4 100644 --- a/src/graphical_simulation.hh +++ b/src/graphical_simulation.hh @@ -60,4 +60,3 @@ namespace sgpem { } #endif - diff --git a/src/io_manager.hh b/src/io_manager.hh index f396f00..510b22e 100644 --- a/src/io_manager.hh +++ b/src/io_manager.hh @@ -27,7 +27,6 @@ #include namespace sgpem { - class IOManager; @@ -39,23 +38,21 @@ namespace sgpem { public: virtual ~IOManager() {} - /**Writes a string into an output (the console, a text widget, ...) - \returns the number of charaters written - */ - virtual unsigned int write_buffer(const Glib::ustring& buffer) = 0; + /**Writes a string into an output (the console, a text widget, ...) + \returns the number of charaters written + */ + virtual uint write_buffer(const Glib::ustring& buffer) = 0; - /**Reads a command from an interactive input (the console, a text widget, ...) - \returns a trimmed string (without blank spaces, tabs... at the extremities) - */ - virtual Glib::ustring read_command() = 0; + /**Reads a command from an interactive input (the console, a text widget, ...) + \returns a trimmed string (without blank spaces, tabs... at the extremities) + */ + virtual Glib::ustring read_command() = 0; - /**Specify whether this IOManger permits to write and read at the same time - */ - virtual bool is_full_duplex() = 0; + /**Specify whether this IOManger permits to write and read at the same time + */ + virtual bool is_full_duplex() = 0; }; } #endif - - diff --git a/src/main.cc b/src/main.cc index 30959bc..1277bf7 100644 --- a/src/main.cc +++ b/src/main.cc @@ -34,9 +34,13 @@ #include "backend/process.hh" #include "backend/policy.hh" #include "backend/policy_parameters.hh" +#include "backend/python_policy.hh" +#include "backend/python_policy_manager.hh" #include "standard_io.hh" #include "text_simulation.hh" +#include "backend/dummy_policy.hh" + #include #include @@ -72,32 +76,55 @@ main(int argc, char* argv[]) filenames.insert(filenames.begin(), a_ptr, a_ptr+a_count); } */ - + + // Set the unique POLICY + DummyPolicy pol; + pol.configure(); + pol.get_parameters().set_int("var2", 33); + pol.get_parameters().set_float("multiplier", 100); + pol.get_parameters().set_string("che_ne_so", "ciao"); + Scheduler::get_instance().set_policy(&pol); TextSimulation text_sim; + History::get_instance().attach(&text_sim); + //textual IO smart_ptr io(new StandardIO()); text_sim.add_io_device(io); - //grafical IO - start_gui(argc, argv); - - //SMOKE-TEST for backend classes - /* cout << "\n\n********************************"; - Process p1("P1", 0,10,1); - Process p2("P2", 0,30,2); - Process p3("P3", 5,15,3); - Process p4("P4", 6,5,3); - Process p5("P5", 1,10,3); + + // 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); ss1.set_state(SchedulableStatus::state_running); + SchedulableStatus ss1(p1); SchedulableStatus ss2(p2); SchedulableStatus ss3(p3); - SchedulableStatus ss4(p4); ss4.set_state(SchedulableStatus::state_running); + SchedulableStatus ss4(p4); SchedulableStatus ss5(p5); SchedulableStatus ss6(p6); -// SimulationStatus sim1; sim1.set_running(p1); + 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); + + + + //grafical IO + start_gui(argc, argv); + + //SMOKE-TEST for backend classes + /* cout << "\n\n********************************"; + + //************** TEST HISTORY @@ -170,4 +197,3 @@ main(int argc, char* argv[]) */ return 0; } - diff --git a/src/simulation.cc b/src/simulation.cc index ab72322..7aac98e 100644 --- a/src/simulation.cc +++ b/src/simulation.cc @@ -25,7 +25,7 @@ using namespace sgpem; using namespace memory; using Glib::usleep; -Simulation::Simulation(): _state(state_paused), _mode(false), _timer_interval(1000) +Simulation::Simulation(): _state(state_paused), _mode(true), _timer_interval(1000) { } @@ -83,16 +83,17 @@ Simulation::run() _state = state_running; //******* CONTINUOUS TIME + if (_mode) { - loop: +loop: // chech for termination bool all_term = true; - smart_ptr left = h.get_simulation_status_at(h.get_current_time()); + smart_ptr left = h.get_simulation_status_at(h.get_current_time()); for(uint i = 0; i < left->size(); i++) if (left->get_item_at(i)->get_state() != SchedulableStatus::state_terminated) { - all_term = true; + all_term = false; break; } @@ -119,14 +120,14 @@ Simulation::run() //******* STEP by STEP else - { + { // chech for termination bool all_term = true; - smart_ptr left = h.get_simulation_status_at(h.get_current_time()); + smart_ptr left = h.get_simulation_status_at(h.get_current_time()); for(uint i = 0; i < left->size(); i++) if (left->get_item_at(i)->get_state() != SchedulableStatus::state_terminated) { - all_term = true; + all_term = false; break; } @@ -178,4 +179,3 @@ Simulation::get_avaiable_policies() v.push_back(Scheduler::get_instance().get_policy()); return v; } - diff --git a/src/simulation.hh b/src/simulation.hh index 565745c..c25f39e 100644 --- a/src/simulation.hh +++ b/src/simulation.hh @@ -76,4 +76,3 @@ namespace sgpem #endif - diff --git a/src/standard_io.cc b/src/standard_io.cc index e8b5b07..bac42cf 100644 --- a/src/standard_io.cc +++ b/src/standard_io.cc @@ -61,4 +61,3 @@ StandardIO::is_full_duplex() { return false; } - diff --git a/src/standard_io.hh b/src/standard_io.hh index 1dbc840..3a7320e 100644 --- a/src/standard_io.hh +++ b/src/standard_io.hh @@ -49,4 +49,3 @@ namespace sgpem { } #endif - diff --git a/src/text_simulation.cc b/src/text_simulation.cc index af82245..3b65240 100644 --- a/src/text_simulation.cc +++ b/src/text_simulation.cc @@ -95,7 +95,7 @@ check: " depending the mode configured with SetMode (default=continuous)")); return; } - obj->_devices[quale]->write_buffer("\n\tRUN!!"); + obj->run(); } else if (arguments[param] == "PAUSE") { @@ -105,7 +105,7 @@ check: "\n-- PAUSE COMMAND --\nPauses the simulation. The next call to RUN will restart it.")); return; } - obj->_devices[quale]->write_buffer("\n\tPAUSE!!"); + obj->pause(); } else if (arguments[param] == "STOP") { @@ -116,7 +116,7 @@ check: "bring the simulation to the FIRST instant and start it.")); return; } - obj->_devices[quale]->write_buffer("\n\tSTOP!!"); + obj->stop(); } else if (arguments[param] == "RESET") { @@ -126,8 +126,7 @@ check: "\n-- RESET COMMAND --\nResets the simulation jumping back to the first instant.")); return; } - obj->_devices[quale]->write_buffer("\n\tRESET!!"); - + obj->reset(); } else if (arguments[param] == "QUIT") { @@ -145,7 +144,8 @@ check: if (show_help) { obj->_devices[quale]->write_buffer(_( - "\n-- YOU ARE JOKING ME --\nYou're really too dummy!!!\n")); + "\n-- Do you really want me to explain what HELP means? --" + "\n ************** YOU ARE JOKING ME !!! ************\n\n")); exit(1); } if (arguments.size() == 1) @@ -293,13 +293,31 @@ check: int_to_string(i_i->second.get_value(), temp); obj->_devices[quale]->write_buffer("\tvalue=" + temp); int_to_string(i_i->second.get_lower_bound(), temp); - obj->_devices[quale]->write_buffer(" lower=" + temp); + obj->_devices[quale]->write_buffer("\t\tlower=" + temp); int_to_string(i_i->second.get_upper_bound(), temp); - obj->_devices[quale]->write_buffer(" upper=" + temp); + obj->_devices[quale]->write_buffer("\t\tupper=" + temp); if (i_i->second.is_required()) - obj->_devices[quale]->write_buffer(" required=true"); + obj->_devices[quale]->write_buffer("\t\trequired=true"); else - obj->_devices[quale]->write_buffer(" required=false"); + obj->_devices[quale]->write_buffer("\t\trequired=false"); + } + + map > map_f = param.get_registered_float_parameters(); + map >::iterator i_f = map_f.begin(); + + for(; i_f != map_f.end(); i_f++) + { + obj->_devices[quale]->write_buffer("\nfloat\t" + i_f->second.get_name()); + float_to_string(i_f->second.get_value(), temp); + obj->_devices[quale]->write_buffer("\tvalue=" + temp); + float_to_string(i_f->second.get_lower_bound(), temp); + obj->_devices[quale]->write_buffer("\t\tlower=" + temp); + float_to_string(i_f->second.get_upper_bound(), temp); + obj->_devices[quale]->write_buffer("\t\tupper=" + temp); + if (i_f->second.is_required()) + obj->_devices[quale]->write_buffer("\t\trequired=true"); + else + obj->_devices[quale]->write_buffer("\t\trequired=false"); } map > map_s = param.get_registered_string_parameters(); @@ -314,11 +332,13 @@ check: else obj->_devices[quale]->write_buffer(" required=false"); } + } else { - obj->_devices[quale]->write_buffer("\nCommand not recognized: "); + obj->_devices[quale]->write_buffer(_("\nCommand not recognized: ")); obj->_devices[quale]->write_buffer(arguments[param]); + obj->_devices[quale]->write_buffer(_("\nTyper HELP for a list of avaiable commands.")); return; } } @@ -327,7 +347,69 @@ check: void TextSimulation::update() { + History& h = History::get_instance(); + int when, pri; + ustring temp; + + when = h.get_current_time(); + smart_ptr ll = h.get_simulation_status_at(when); + + int_to_string(when, temp); + if (when<10) + _devices[0]->write_buffer("\n "); + else + _devices[0]->write_buffer("\n"); + _devices[0]->write_buffer(temp + ") [RUNS]"); + + //insert the RUNNING ONE + smart_ptr running = h.get_scheduled_at(when); + if (running) + { + pri = running->get_schedulable()->get_priority(); + int_to_string(pri, temp); + _devices[0]->write_buffer(" " + running->get_schedulable()->get_name() + "_(" + temp + ")"); + } + + _devices[0]->write_buffer(" --[READY]"); + //insert the READY ones + for (uint i = 0; i < ll->size(); i++) + if (ll->get_item_at(i)->get_state() == SchedulableStatus::state_ready) + { + pri = ll->get_item_at(i)->get_schedulable()->get_priority(); + int_to_string(pri, temp); + _devices[0]->write_buffer(" " + ll->get_item_at(i)->get_schedulable()->get_name() + "_(" + temp + ")"); + } + _devices[0]->write_buffer(" --[BLOCKED]"); + //insert the BLOCKED ones + for (uint i = 0; i < ll->size(); i++) + if (ll->get_item_at(i)->get_state() == SchedulableStatus::state_blocked) + { + pri = ll->get_item_at(i)->get_schedulable()->get_priority(); + int_to_string(pri, temp); + _devices[0]->write_buffer(" " + ll->get_item_at(i)->get_schedulable()->get_name() + "_(" + temp + ")"); + } + + _devices[0]->write_buffer(" --[FUTURE]"); + //insert the FUTURE ones + for (uint i = 0; i < ll->size(); i++) + if (ll->get_item_at(i)->get_state() == SchedulableStatus::state_future) + { + pri = ll->get_item_at(i)->get_schedulable()->get_priority(); + int_to_string(pri, temp); + _devices[0]->write_buffer(" " + ll->get_item_at(i)->get_schedulable()->get_name() + "_(" + temp + ")"); + } + + _devices[0]->write_buffer(" --[TERM]"); + //insert the TERMINATED ones + for (uint i = 0; i < ll->size(); i++) + if (ll->get_item_at(i)->get_state() == SchedulableStatus::state_terminated) + { + pri = ll->get_item_at(i)->get_schedulable()->get_priority(); + int_to_string(pri, temp); + _devices[0]->write_buffer(" " + ll->get_item_at(i)->get_schedulable()->get_name() + "_(" + temp + ")"); + } + } void @@ -351,4 +433,3 @@ TextSimulation::_io_loop(pair pun) pun.first->parse_command(p); } } - diff --git a/src/text_simulation.hh b/src/text_simulation.hh index a5743e8..cdc24f3 100644 --- a/src/text_simulation.hh +++ b/src/text_simulation.hh @@ -60,4 +60,4 @@ namespace sgpem } -#endif +#endif \ No newline at end of file