// src/schedulables_tree_widget.cc - Copyright 2005-2007, 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 3 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, see http://www.gnu.org/licenses/. #include "gettext.h" #include "sgpemv2/config.h" #include "schedulables_tree_widget.hh" #include #include #include #include #include #include #include "add_request_dialog.hh" #include #include #include #include #include #include #include #include #include using namespace sgpem; using namespace Gtk; using namespace Glib; PropertyProxy_Base SchedulablesTreeWidget::CellRendererTextMarkup::_property_renderable() { return Glib::PropertyProxy_Base(this, "markup"); } SchedulablesTreeWidget::SchedulablesTreeWidget() { _columns.add(_main_column); _columns.add(_types_column); _columns.add(_handles_column); _model = TreeStore::create(_columns); set_model(_model); int idx = append_column(_("Arrival"), _cell_renderer) - 1; Gtk::TreeViewColumn* tvc = get_column(idx); tvc->set_cell_data_func(_cell_renderer, sigc::mem_fun(*this, &SchedulablesTreeWidget::_on_arrival_column_data)); idx = append_column(_("Entity"), _cell_renderer) - 1; tvc = get_column(idx); tvc->set_cell_data_func(_cell_renderer, sigc::mem_fun(*this, &SchedulablesTreeWidget::_on_entity_column_data)); idx = append_column(_("State"), _cell_renderer) - 1; tvc = get_column(idx); tvc->set_cell_data_func(_cell_renderer, sigc::mem_fun(*this, &SchedulablesTreeWidget::_on_state_column_data)); set_headers_visible(true); Simulation::get_instance().get_history().attach(*this); } SchedulablesTreeWidget::~SchedulablesTreeWidget() { Simulation::get_instance().get_history().detach(*this); } template bool SchedulablesTreeWidget::check_type(SchedulablesTreeWidget::HandleType type) { return false; } namespace sgpem { template <> bool SchedulablesTreeWidget::check_type(HandleType type) { return type == htype_process; } template <> bool SchedulablesTreeWidget::check_type(HandleType type) { return type == htype_thread; } template <> bool SchedulablesTreeWidget::check_type(HandleType type) { return type == htype_request; } // template <> // bool // SchedulablesTreeWidget::check_type(HandleType type) // { // return type == htype_subrequest; // } } template T* SchedulablesTreeWidget::get_selected() { TreeModel::iterator sel = get_selection()->get_selected(); return get_row_handle_as(sel); } template T* SchedulablesTreeWidget::get_row_handle_as(const Gtk::TreeModel::iterator& row) { if(!row) return nullptr; const void* p_handle = (*row)[_handles_column]; HandleType type = (*row)[_types_column]; if(!check_type(type)) return nullptr; return reinterpret_cast(const_cast(p_handle)); } SchedulablesTreeWidget::HandleType SchedulablesTreeWidget::get_selection_type() { TreeModel::iterator sel = get_selection()->get_selected(); return get_row_type(sel); } SchedulablesTreeWidget::HandleType SchedulablesTreeWidget::get_row_type(const TreeModel::iterator& row) { if(!row) return htype_undefined; else return (*row)[_types_column]; } bool SchedulablesTreeWidget::on_button_press_event(GdkEventButton* event) { TreeView::on_button_press_event(event); if((Simulation::get_instance().get_state() == Simulation::state_stopped) && (event->type == GDK_BUTTON_PRESS) && (event->button == 3) ) { RefPtr action_group = Gtk::ActionGroup::create(); action_group->add( Gtk::Action::create("AddProcess", _("Add Process")), sigc::mem_fun(*this, &SchedulablesTreeWidget::_on_add_process) ); action_group->add( Gtk::Action::create("AddThread", _("Add Thread")), sigc::mem_fun(*this, &SchedulablesTreeWidget::_on_add_thread) ); action_group->add( Gtk::Action::create("AddRequest", _("Add Request")), sigc::mem_fun(*this, &SchedulablesTreeWidget::_on_add_request) ); action_group->add( Gtk::Action::create("EditProcess", _("Edit Process")), sigc::mem_fun(*this, &SchedulablesTreeWidget::_on_edit_process) ); action_group->add( Gtk::Action::create("EditThread", _("Edit Thread")), sigc::mem_fun(*this, &SchedulablesTreeWidget::_on_edit_thread) ); action_group->add( Gtk::Action::create("EditRequest", _("Edit Request")), sigc::mem_fun(*this, &SchedulablesTreeWidget::_on_edit_request) ); action_group->add( Gtk::Action::create("RemoveProcess", _("Remove Process")), sigc::mem_fun(*this, &SchedulablesTreeWidget::_on_remove_process) ); action_group->add( Gtk::Action::create("RemoveThread", _("Remove Thread")), sigc::mem_fun(*this, &SchedulablesTreeWidget::_on_remove_thread) ); action_group->add( Gtk::Action::create("RemoveRequest", _("Remove Request")), sigc::mem_fun(*this, &SchedulablesTreeWidget::_on_remove_request) ); // action_group->add( Gtk::Action::create("RemoveSubrequest", _("Remove Subrequest")), // sigc::mem_fun(*this, &SchedulablesTreeWidget::_on_remove_subrequest) ); RefPtr UIManager = Gtk::UIManager::create(); UIManager->insert_action_group(action_group); const HandleType selection_type = get_selection_type(); Glib::ustring adds; Glib::ustring edits; Glib::ustring removes; Glib::ustring separator; if(selection_type != htype_undefined) separator = ""; switch(selection_type) { case htype_process: adds = ""; edits = ""; removes = ""; break; case htype_thread: adds = ""; edits = ""; removes = ""; break; case htype_request: edits = ""; removes = ""; break; // case htype_subrequest: // removes = ""; default: break; } adds += ""; Glib::ustring ui_info = "" " "; ui_info += adds + separator + edits + ((edits.size() == 0) ? ustring() : separator) + removes; ui_info += " " ""; UIManager->add_ui_from_string(ui_info); Gtk::Menu* menu = dynamic_cast(UIManager->get_widget("/PopupMenu")); menu->popup(event->button, event->time); return true; //It has been handled. } else return false; } void SchedulablesTreeWidget::_update_expanded_vector(Gtk::TreeView*, const Gtk::TreeModel::Path& path) { _expanded_rows.push_back(path.to_string()); } void SchedulablesTreeWidget::update(const History&) { using std::vector; typedef Environment::Processes::const_iterator ProcessIt; const Environment::Processes& processes = Simulation::get_instance().get_history().get_last_environment().get_processes(); // Save expanded rows (FIXME: find a better way?) map_expanded_rows(sigc::mem_fun(*this, &SchedulablesTreeWidget::_update_expanded_vector)); _model->clear(); for(Iseq pit = iseq(processes); pit; ++pit) { Process& p = *(*pit); TreeModel::Row prow = *(_model->append()); // prow[_main_column] = p.get_name(); prow[_types_column] = htype_process; prow[_handles_column] = &p; vector threads = p.get_threads(); for(Iseq::iterator> tit = iseq(threads); tit; ++tit) { Thread& t = *(*tit); TreeModel::Row trow = *(_model->append(prow.children())); // trow[_main_column] = t.get_name(); trow[_types_column] = htype_thread; trow[_handles_column] = &t; vector requests = t.get_requests(); for(unsigned int ri = 0; ri < requests.size(); ++ri) { Request& r = *requests[ri]; TreeModel::Row rrow = *(_model->append(trow.children())); // std::ostringstream oss; // oss << _("request ") << ri + 1; // rrow[_main_column] = oss.str(); rrow[_types_column] = htype_request; rrow[_handles_column] = &r; } } } // Restore expanded rows const vector& const_expanded_rows = _expanded_rows; for(Iseq::const_iterator> it = iseq(const_expanded_rows); it; ++it) expand_row(Gtk::TreeModel::Path(*it), false); // We can clear it now, since until next update we won't need it. _expanded_rows.clear(); } void SchedulablesTreeWidget::_on_add_process() { add_edit_process(true); } void SchedulablesTreeWidget::_on_edit_process() { add_edit_process(false); } void SchedulablesTreeWidget::add_edit_process(bool adding) { /** This is ugly, I know, we should be using derived widgets, but I also believe we * have little time, and I'm not going to waste too much of it on the frontend */ RefPtr ui(Builder::create_from_file(UIDIR "/add-process-dialog.ui")); Dialog* add_process_dialog; ui->get_widget("AddProcessDialog", add_process_dialog); Entry* name_entry; SpinButton* arrival_time_spin; SpinButton* duration_spin; SpinButton* base_priority_spin; ui->get_widget("Name.Entry", name_entry); ui->get_widget("ArrivalTime.Spin", arrival_time_spin); ui->get_widget("Duration.Spin", duration_spin); ui->get_widget("BasePriority.Spin", base_priority_spin); Process* selection = nullptr; if(!adding) { selection = get_selected(); name_entry->set_text(selection->get_name()); arrival_time_spin->set_value(static_cast(selection->get_arrival_time())); duration_spin->set_value(static_cast(selection->get_total_cpu_time())); duration_spin->set_sensitive(false); base_priority_spin->set_value(static_cast(selection->get_base_priority())); } if(add_process_dialog->run() == RESPONSE_OK) { if(adding) { History& h = Simulation::get_instance().get_history(); History::LockNotify lock(h); Process& np = h.add_process(name_entry->get_text(), arrival_time_spin->get_value_as_int(), base_priority_spin->get_value_as_int()); h.add_thread(_("Main"), np, duration_spin->get_value_as_int(), 0, base_priority_spin->get_value_as_int()); } else { History& h = Simulation::get_instance().get_history(); History::LockNotify lock(h); h.edit_process(*selection, name_entry->get_text(), arrival_time_spin->get_value_as_int(), base_priority_spin->get_value_as_int()); } } add_process_dialog->hide(); } void SchedulablesTreeWidget::_on_add_thread() { add_edit_thread(true); } void SchedulablesTreeWidget::_on_edit_thread() { add_edit_thread(false); } void SchedulablesTreeWidget::add_edit_thread(bool adding) { /** This is ugly, I know, we should be using derived widgets, but I also believe we * have little time, and I'm not going to waste too much of it on the frontend */ RefPtr ui(Builder::create_from_file(UIDIR "/add-thread-dialog.ui")); Dialog* add_thread_dialog; ui->get_widget("AddThreadDialog", add_thread_dialog); Entry* name_entry; SpinButton* cpu_time_spin; SpinButton* arrival_time_spin; SpinButton* base_priority_spin; ui->get_widget("Name.Entry", name_entry); ui->get_widget("CpuTime.Spin", cpu_time_spin); ui->get_widget("ArrivalTime.Spin", arrival_time_spin); ui->get_widget("BasePriority.Spin", base_priority_spin); Thread* t = nullptr; if(!adding) { t = get_selected(); name_entry->set_text(t->get_name()); cpu_time_spin->set_value(static_cast(t->get_total_cpu_time())); arrival_time_spin->set_value(static_cast(t->get_arrival_time())); base_priority_spin->set_value(static_cast(t->get_base_priority())); } if(add_thread_dialog->run() == RESPONSE_OK) { if(adding) { Process* p = get_selected(); assert(p != nullptr); Simulation::get_instance().get_history().add_thread(name_entry->get_text(), *p, cpu_time_spin->get_value_as_int(), arrival_time_spin->get_value_as_int(), base_priority_spin->get_value_as_int()); } else { Simulation::get_instance().get_history().edit_thread(*t, name_entry->get_text(), cpu_time_spin->get_value_as_int(), arrival_time_spin->get_value_as_int(), base_priority_spin->get_value_as_int()); } } add_thread_dialog->hide(); } void SchedulablesTreeWidget::_on_add_request() { RefPtr ui(Builder::create_from_file(UIDIR "/add-request-dialog.ui")); AddRequestDialog* add_request_dialog; // NOTE This is *not* reflective programming! AddRequestDialog is the name of // the base widget in the xml ui file. ui->get_widget_derived("AddRequestDialog", add_request_dialog); Thread* t = get_selected(); assert(t != nullptr); add_request_dialog->run_add(*t); } void SchedulablesTreeWidget::_on_edit_request() { using std::vector; RefPtr ui(Builder::create_from_file(UIDIR "/add-request-dialog.ui")); AddRequestDialog* add_request_dialog; // NOTE This is *not* reflective programming! AddRequestDialog is the name of // the base widget in the xml ui file. ui->get_widget_derived("AddRequestDialog", add_request_dialog); Request* r = get_selected(); assert(r != nullptr); // FIXME: write more polite code for doing this, and probably using the same logic // for all other editing operations may be useful for making the app more robust const Environment::Processes& processes = Simulation::get_instance().get_history().get_environment_at(0).get_processes(); for(Iseq pit = iseq(processes); pit; ++pit) { vector threads = (*pit)->get_threads(); for(Iseq::iterator> tit = iseq(threads); tit; ++tit) { vector requests = (*tit)->get_requests(); for(Iseq::iterator> rit = iseq(requests); rit; ++rit) { if(*(*rit) == *r) r = *rit; } } } add_request_dialog->run_edit(*r); } void SchedulablesTreeWidget::_on_remove_process() { Process* p = get_selected(); assert(p != nullptr); Simulation::get_instance().get_history().remove(*p); } void SchedulablesTreeWidget::_on_remove_thread() { Thread* t = get_selected(); assert(t != nullptr); Simulation::get_instance().get_history().remove(*t); } void SchedulablesTreeWidget::_on_remove_request() { Request* r = get_selected(); assert(r != nullptr); Simulation::get_instance().get_history().remove(*r); } void SchedulablesTreeWidget::_on_arrival_column_data(Gtk::CellRenderer* cr, const Gtk::TreeModel::iterator& it) { CellRendererTextMarkup& crtm = static_cast(*cr); std::ostringstream oss; oss << ""; switch(get_row_type(it)) { case htype_process: oss << get_row_handle_as(it)->get_arrival_time(); break; case htype_thread: oss << get_row_handle_as(it)->get_arrival_time(); break; case htype_request: oss << get_row_handle_as(it)->get_instant(); break; default: oss << _("ERROR"); } oss << ""; crtm.property_markup() = oss.str(); } void SchedulablesTreeWidget::_on_entity_column_data(Gtk::CellRenderer* cr, const Gtk::TreeModel::iterator& it) { CellRendererTextMarkup& crtm = static_cast(*cr); ustring marked_up; switch(get_row_type(it)) { case htype_process: marked_up = markup_schedulable(*get_row_handle_as(it)); break; case htype_thread: marked_up = markup_schedulable(*get_row_handle_as(it)); break; case htype_request: marked_up = markup_request(*get_row_handle_as(it)); break; default: marked_up = _("ERROR"); } crtm.property_markup() = marked_up; } ustring SchedulablesTreeWidget::markup_schedulable(const Schedulable& s) { using std::endl; std::ostringstream oss; // TODO : add note for the translator to use HTML-escaped text // See the gettext manual. oss << "" << Markup::escape_text(s.get_name()) << "" << Glib::ustring::compose(_(" (%1/%2), current_priority: %3"), s.get_elapsed_time(), s.get_total_cpu_time(), s.get_current_priority()) << ""; return oss.str(); } ustring SchedulablesTreeWidget::markup_request(Request& r) { using std::endl; std::ostringstream oss; oss << "" << _("request:") << ""; const Environment& env = Simulation::get_instance().get_history().get_last_environment(); const Environment::Resources& resources = env.get_resources(); std::vector subrequests = r.get_subrequests(); for(Iseq::iterator> it = iseq(subrequests); it; ++it) { SubRequest& sr = *(*it); Resource& res = *(resources.find(sr.get_resource_key())->second); oss << " -> " << res.get_name() << " (" << (sr.get_length() - sr.get_remaining_time()) << "/" << sr.get_length() << ")"; } oss << ""; return oss.str(); } void SchedulablesTreeWidget::_on_state_column_data(Gtk::CellRenderer* cr, const Gtk::TreeModel::iterator& it) { CellRendererTextMarkup& crtm = static_cast(*cr); ustring marked_up = ""; switch(get_row_type(it)) { case htype_process: marked_up += state_text(get_row_handle_as(it)->get_state()); break; case htype_thread: marked_up += state_text(get_row_handle_as(it)->get_state()); break; case htype_request: marked_up += state_text(get_row_handle_as(it)->get_state()); break; default: marked_up += "NO STATE"; } crtm.property_markup() = marked_up + ""; } ustring SchedulablesTreeWidget::state_text(Schedulable::state state) { ustring text; switch (state) { case Schedulable::state_running: text = _("RUNNING"); break; case Schedulable::state_ready: text = _("READY"); break; case Schedulable::state_blocked: text = _("BLOCKED"); break; case Schedulable::state_future: text = _("FUTURE"); break; case Schedulable::state_terminated: text = _("TERMINATED"); break; default: text = _("@@@ ERROR @@@"); } return Markup::escape_text(text); } ustring SchedulablesTreeWidget::state_text(Request::state state) { ustring text; switch (state) { case Request::state_allocated: text = _("ALLOCATED"); break; case Request::state_allocable: text = _("ALLOCABLE"); break; case Request::state_unallocable: text = _("UNALLOCABLE"); break; case Request::state_future: text = _("FUTURE"); break; case Request::state_exhausted: text = _("EXHAUSTED"); break; default: text = _("@@@ ERROR @@@"); } return Markup::escape_text(text); }