From 8894e31222b4b6bea154fdb05cf7ef47a48dd170 Mon Sep 17 00:00:00 2001 From: paolo Date: Wed, 12 Jul 2006 22:31:47 +0000 Subject: [PATCH] - partial update of serializer git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@756 3ecf2c5c-341e-0410-92b4-d18e462d057c --- .../xmlsave/src/testsuite/test-xml_visitor.cc | 60 ++++++- plugins/xmlsave/src/xml_serializer.cc | 157 +++++++++++++++--- plugins/xmlsave/src/xml_serializer.hh | 4 + plugins/xmlsave/src/xml_serializer_factory.cc | 110 +++++++++++- plugins/xmlsave/src/xml_serializer_factory.hh | 27 ++- plugins/xmlsave/src/xml_visitor.cc | 2 +- 6 files changed, 316 insertions(+), 44 deletions(-) diff --git a/plugins/xmlsave/src/testsuite/test-xml_visitor.cc b/plugins/xmlsave/src/testsuite/test-xml_visitor.cc index 4a6cc9a..2b72c5e 100644 --- a/plugins/xmlsave/src/testsuite/test-xml_visitor.cc +++ b/plugins/xmlsave/src/testsuite/test-xml_visitor.cc @@ -47,7 +47,7 @@ void fillHistory(History &hist) cout << "resource name: " << respair.second->get_name() << " key: " << respair.first << " places: " << respair.second->get_places() << endl; // add a process - name, arrival time, priority - Process& p1 = hist.add_process(Glib::ustring("Process 1"), 5, 2); // name, arrival time, priority + Process& p1 = hist.add_process(Glib::ustring("Process 1"), 0, 2); // name, arrival time, priority // and print his values cout << "process name: " << p1.get_name() << " arrival_time: " << p1.get_arrival_time() << " base_priority: " << p1.get_base_priority() << endl; @@ -62,12 +62,17 @@ void fillHistory(History &hist) cout << "process name: " << p3.get_name() << " arrival_time: " << p3.get_arrival_time() << " base_priority: " << p3.get_base_priority() << endl; // add a thread - name, parent, cpu time, arrival time, priority - Thread& p1_t1 = hist.add_thread(Glib::ustring("Process 1 - Thread 1"), p1, 3, 2, 6); + Thread& p1_t1 = hist.add_thread(Glib::ustring("Process 1 - Thread 1"), p1, 8, 2, 6); // and print his values cout << "thread name: " << p1_t1.get_name() << " total_cpu_time: " << p1_t1.get_total_cpu_time() << " arrival_time: " << p1_t1.get_arrival_time() << " base_priority: " << p1_t1.get_base_priority() << endl; + // add a thread - name, parent, cpu time, arrival time, priority + Thread& p1_t2 = hist.add_thread(Glib::ustring("Process 1 - Thread 2"), p1, 3, 3, 5); + // and print his values + cout << "thread name: " << p1_t2.get_name() << " total_cpu_time: " << p1_t2.get_total_cpu_time() << " arrival_time: " << p1_t2.get_arrival_time() << " base_priority: " << p1_t2.get_base_priority() << endl; + // add a request - Thread, time - Request& req1 = hist.add_request(p1_t1, 7); + Request& req1 = hist.add_request(p1_t1, 3); // and print his values cout << "request arrival_time: " << req1.get_instant() << endl; @@ -79,8 +84,14 @@ void fillHistory(History &hist) } +void showHistory(const History &hist) +{ + +} + void dumpEnvironment(const Environment& env) { + cout << "dump environment start " <::const_iterator proc_iterator; @@ -89,7 +100,7 @@ void dumpEnvironment(const Environment& env) while(iter!=end) { Process* p = (*iter); - cout << "process name: " << p->get_name() << " arrival_time: " << p->get_arrival_time() << " base_priority: " << p->get_base_priority() << endl; + cout << " process name: " << p->get_name() << " arrival_time: " << p->get_arrival_time() << " base_priority: " << p->get_base_priority() << endl; iter++; @@ -101,13 +112,43 @@ void dumpEnvironment(const Environment& env) while(iter1!=end1) { - Thread* p = (*iter1); - cout << "Thread name: " << p->get_name() << " arrival_time: " << p->get_arrival_time() << " base_priority: " << p->get_base_priority() << endl; + Thread* t = (*iter1); + cout << " thread name: " << t->get_name() << " arrival_time: " << t->get_arrival_time() << " base_priority: " << t->get_base_priority() << endl; + + typedef std::vector Requests; + typedef std::vector::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); + cout << " request arrival_time: " << r->get_instant() << endl; + + typedef std::vector SubRequests; + typedef std::vector::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); + cout << " sub request resource_key: " << sr->get_resource_key() << " places: " << sr->get_places() << " length: " << sr->get_length() << endl; + iter3++; + } + + + iter2++; + } + iter1++; } } + cout << "dump environment end " << endl << endl; } @@ -118,14 +159,21 @@ main(int argc, char** argv) ConcreteHistory hist; + cout << "fill history " << endl; fillHistory(hist); + cout << "history " << endl; const Environment& env = hist.get_last_environment(); dumpEnvironment(env); XMLSerializer xmlser; xmlser.save_snapshot(Glib::ustring("xml-visit.xml"), hist); + ConcreteHistory hist2; + xmlser.restore_snapshot(Glib::ustring("xml-visit.xml"), hist2); + + cout << "history2 - copy of history " << endl; + dumpEnvironment(hist2.get_last_environment()); // typedef std::vector Processes; return 0; diff --git a/plugins/xmlsave/src/xml_serializer.cc b/plugins/xmlsave/src/xml_serializer.cc index 13869cd..e543d51 100644 --- a/plugins/xmlsave/src/xml_serializer.cc +++ b/plugins/xmlsave/src/xml_serializer.cc @@ -198,6 +198,25 @@ void XMLSerializer::fill_schedulables(xmlNodePtr schedulables_node, const Histor } } +void XMLSerializer::clear_history(History& hist) +{ +/* + const Environment& env = hist.get_last_environment(); + + const Environment::Processes& pvect = env.get_processes(); + typedef std::vector::const_iterator proc_iterator; + + proc_iterator iter = pvect.begin(); + proc_iterator end = pvect.end(); + while(iter!=end) + { + xvisit.from_process(*(*iter)); + pvect. + iter = pvect.begin(); + } +*/ +} + void XMLSerializer::read_doc(xmlDocPtr doc, XMLSerializerFactory& fact) { /* @@ -211,13 +230,13 @@ void XMLSerializer::read_doc(xmlDocPtr doc, XMLSerializerFactory& fact) throw SerializerError("Reading Error: xml doc is empty."); } - cout << "ROOT: " << root->name << endl; + // cout << "ROOT: " << root->name << endl; xmlNodePtr cur; cur = root->children; while(cur!=NULL) { - cout << "NODE: " << cur->name << endl; + // cout << "NODE: " << cur->name << endl; Glib::ustring name((const char *)cur->name); if(name=="resources") @@ -239,25 +258,25 @@ XMLSerializerFactory::Parameters* read_properties(xmlAttrPtr prop) XMLSerializerFactory::Parameters* par=new XMLSerializerFactory::Parameters(); while (prop != NULL) { - // cout << "PROP: " << prop->name; + // // cout << "PROP: " << prop->name; if(prop->children && xmlNodeIsText(prop->children)){ xmlChar *key = xmlNodeGetContent (prop->children); // xmlChar *key = xmlNodeListGetString(doc, prop->children, 1); if(key!=NULL) { - // cout << " VALUE: " << key; + // // cout << " VALUE: " << key; std::pair key_value(Glib::ustring((const char *)prop->name), Glib::ustring((const char *)key)); par->insert(key_value); // c.insert(typename Cont::value_type(new_key, pos->second)); - cout << " pair PROP: " << key_value.first << " VALUE: " << key_value.second << endl; + // cout << " pair PROP: " << key_value.first << " VALUE: " << key_value.second << endl; xmlFree(key); } - else - cout << " !VALUE IS NULL! "; + // else + // cout << " !VALUE IS NULL! "; } - cout << endl; + // cout << endl; prop = prop->next; } return par; @@ -272,7 +291,7 @@ void XMLSerializer::read_resources(xmlNodePtr resources_node, XMLSerializerFacto Glib::ustring node_name((const char *)cur->name); if(node_name=="resource") { - cout << "NODE: " << cur->name << endl; + // cout << "read_resources NODE: " << cur->name << endl; xmlAttrPtr prop = cur->properties; XMLSerializerFactory::Parameters* par=read_properties(prop); @@ -288,34 +307,118 @@ void XMLSerializer::read_resources(xmlNodePtr resources_node, XMLSerializerFacto } void XMLSerializer::read_schedulables(xmlNodePtr schedulables_node, XMLSerializerFactory& fact) { + if(schedulables_node==NULL) + return; + xmlNodePtr cur; cur = schedulables_node->children; while(cur!=NULL) { - cout << "NODE: " << cur->name << endl; - - xmlAttrPtr prop = cur->properties; - while (prop != NULL) { - cout << "PROP: " << prop->name; - - if(prop->children && xmlNodeIsText(prop->children)){ - xmlChar *key = xmlNodeGetContent (prop->children); - // xmlChar *key = xmlNodeListGetString(doc, prop->children, 1); - if(key!=NULL) - { - cout << " VALUE: " << key; - xmlFree(key); - } - else - cout << " !VALUE IS NULL! "; + Glib::ustring node_name((const char *)cur->name); + if(node_name=="process") + { + // cout << "read_schedulables NODE: " << cur->name << endl; + + xmlAttrPtr prop = cur->properties; + XMLSerializerFactory::Parameters* par=read_properties(prop); + if(par!=NULL) + { + fact.factory_method(Glib::ustring("Process"), *par); } - cout << endl; - prop = prop->next; + // pass the "threads node" + read_threads(cur->children, fact); } cur = cur->next; } } +void XMLSerializer::read_threads(xmlNodePtr threads_node, XMLSerializerFactory& fact) +{ + if(threads_node==NULL) + return; + + xmlNodePtr cur; + cur = threads_node->children; + while(cur!=NULL) + { + Glib::ustring node_name((const char *)cur->name); + if(node_name=="thread") + { + // cout << "read_threads NODE: " << cur->name << endl; + + xmlAttrPtr prop = cur->properties; + XMLSerializerFactory::Parameters* par=read_properties(prop); + if(par!=NULL) + { + fact.factory_method(Glib::ustring("Thread"), *par); + } + // pass the "requests node" + read_requests(cur->children, fact); + } + + cur = cur->next; + } +} + +void XMLSerializer::read_requests(xmlNodePtr requests_node, XMLSerializerFactory& fact) +{ + if(requests_node==NULL) + { + // cout << "read_requests NULL" << endl; + return; + } + + xmlNodePtr cur; + cur = requests_node->children; + while(cur!=NULL) + { + Glib::ustring node_name((const char *)cur->name); + if(node_name=="request") + { + // cout << "read_requests NODE: " << cur->name << endl; + + xmlAttrPtr prop = cur->properties; + XMLSerializerFactory::Parameters* par=read_properties(prop); + if(par!=NULL) + { + fact.factory_method(Glib::ustring("Request"), *par); + } + // pass the "subrequest nodes" + read_subrequests(cur->children, fact); + } + + cur = cur->next; + } +} + +void XMLSerializer::read_subrequests(xmlNodePtr subrequest_node, XMLSerializerFactory& fact) +{ + if(subrequest_node==NULL) + { + // cout << "read_subrequest NULL" << endl; + return; + } + + xmlNodePtr cur; + cur = subrequest_node; + while(cur!=NULL) + { + Glib::ustring node_name((const char *)cur->name); + if(node_name=="subrequest") + { + // cout << "read_subrequest NODE: " << cur->name << endl; + + xmlAttrPtr prop = cur->properties; + XMLSerializerFactory::Parameters* par=read_properties(prop); + if(par!=NULL) + { + fact.factory_method(Glib::ustring("SubRequest"), *par); + } + } + + cur = cur->next; + } +} /* comment */ diff --git a/plugins/xmlsave/src/xml_serializer.hh b/plugins/xmlsave/src/xml_serializer.hh index 93bbb3f..79743ac 100644 --- a/plugins/xmlsave/src/xml_serializer.hh +++ b/plugins/xmlsave/src/xml_serializer.hh @@ -52,9 +52,13 @@ namespace sgpem void fill_resources(xmlNodePtr resources_node, const History& hist); void fill_schedulables(xmlNodePtr schedulables_node, const History& hist); + void clear_history(History& hist); void read_doc(xmlDocPtr doc, XMLSerializerFactory& fact); void read_resources(xmlNodePtr resources_node, XMLSerializerFactory& fact); void read_schedulables(xmlNodePtr schedulables_node, XMLSerializerFactory& fact); + void read_threads(xmlNodePtr schedulables_node, XMLSerializerFactory& fact); + void read_requests(xmlNodePtr schedulables_node, XMLSerializerFactory& fact); + void read_subrequests(xmlNodePtr schedulables_node, XMLSerializerFactory& fact); }; } diff --git a/plugins/xmlsave/src/xml_serializer_factory.cc b/plugins/xmlsave/src/xml_serializer_factory.cc index 676d047..b076372 100644 --- a/plugins/xmlsave/src/xml_serializer_factory.cc +++ b/plugins/xmlsave/src/xml_serializer_factory.cc @@ -58,19 +58,27 @@ XMLSerializerFactory::factory_method(const Glib::ustring& class_name, Parameters } else if(class_name == "Process") { + create_process(parameters); } else if(class_name == "Thread") { + create_thread(parameters); } else if(class_name == "Request") { + create_request(parameters); } else if(class_name == "SubRequest") { + create_subrequest(parameters); + } + else + { + throw SerializerError("Factory for Unknown class: " + class_name + " doesn't exists."); } } -XMLSerializerFactory::ResourcePair +History::ResourcePair XMLSerializerFactory::create_resource(Parameters& parameters) { // if(_hist!=NULL) @@ -81,6 +89,7 @@ XMLSerializerFactory::create_resource(Parameters& parameters) int arrival_time=0; int how_many=1; bool preemptable=false; + int old_key; Parameters::iterator pos; // read "name" property @@ -93,6 +102,7 @@ XMLSerializerFactory::create_resource(Parameters& parameters) pos = parameters.find(Glib::ustring("key")); if (pos != parameters.end()) { key = pos->second; + string_to_int(key, old_key); } // read "preemptable" property @@ -115,6 +125,7 @@ XMLSerializerFactory::create_resource(Parameters& parameters) History::ResourcePair respair = _hist->add_resource(name, preemptable, how_many, arrival_time); + _temp_map.insert(TempMap::value_type((resource_key_t)old_key, respair.first)); return respair; } @@ -150,24 +161,115 @@ XMLSerializerFactory::create_process(Parameters& parameters) string_to_int(pos->second, priority); } - - return _hist->add_process(Glib::ustring(name, arrival_time, priority);; + _last_process = &_hist->add_process(name, arrival_time, priority); + return *_last_process; } } + + Thread& XMLSerializerFactory::create_thread(Parameters& parameters) { + // if(_hist!=NULL) + { + + Glib::ustring name; + int arrival_time=0; + int lasts_for=0; + int priority=1; + Parameters::iterator pos; + + // read "name" property + pos = parameters.find(Glib::ustring("name")); + if (pos != parameters.end()) { + name = pos->second; + } + + // read "arrival-time" property + pos = parameters.find(Glib::ustring("arrival-time")); + if (pos != parameters.end()) { + string_to_int(pos->second, arrival_time); + } + + // read "priority" property + pos = parameters.find(Glib::ustring("priority")); + if (pos != parameters.end()) { + string_to_int(pos->second, priority); + } + + // read "priority" property + pos = parameters.find(Glib::ustring("lasts-for")); + if (pos != parameters.end()) { + string_to_int(pos->second, lasts_for); + } + + _last_thread = &_hist->add_thread(name, *_last_process, lasts_for, arrival_time, priority); + return *_last_thread; + } } Request& XMLSerializerFactory::create_request(Parameters& parameters) { + // if(_hist!=NULL) + { + + int arrival_time=0; + Parameters::iterator pos; + + // read "arrival-time" property + pos = parameters.find(Glib::ustring("arrival-time")); + if (pos != parameters.end()) { + string_to_int(pos->second, arrival_time); + } + + _last_request = &_hist->add_request(*_last_thread, arrival_time); + return *_last_request; + } } -Subrequest& +SubRequest& XMLSerializerFactory::create_subrequest(Parameters& parameters) { + // if(_hist!=NULL) + { + + int old_key=0; + int lasts_for=0; + int places=1; + Parameters::iterator pos; + resource_key_t new_key = 0; + + + // read "arrival-time" property + pos = parameters.find(Glib::ustring("resource")); + if (pos != parameters.end()) { + string_to_int(pos->second, old_key); + + TempMap::iterator temp_pos; + temp_pos = _temp_map.find((resource_key_t)old_key); + if (temp_pos != _temp_map.end()) { + //take key of resource in _hist + new_key = temp_pos->second; + } + } + + // read "priority" property + pos = parameters.find(Glib::ustring("how-many")); + if (pos != parameters.end()) { + string_to_int(pos->second, places); + } + + // read "priority" property + pos = parameters.find(Glib::ustring("lasts-for")); + if (pos != parameters.end()) { + string_to_int(pos->second, lasts_for); + } + + return _hist->add_subrequest(*_last_request, new_key, (History::time_t)lasts_for, (History::size_t)places); + } + // add a sub request - Request, resource_key, duration, places } diff --git a/plugins/xmlsave/src/xml_serializer_factory.hh b/plugins/xmlsave/src/xml_serializer_factory.hh index c99f40f..5928b61 100644 --- a/plugins/xmlsave/src/xml_serializer_factory.hh +++ b/plugins/xmlsave/src/xml_serializer_factory.hh @@ -22,7 +22,7 @@ #define XML_SERIALIZER_FACTORY_HH 1 #include "config.h" -// #include "history.hh" +#include "history.hh" #include "environment.hh" #include @@ -35,7 +35,7 @@ namespace sgpem class Process; class Thread; class Request; - class Subrequest; + class SubRequest; } namespace sgpem @@ -51,20 +51,35 @@ namespace sgpem History* get_history(); typedef Environment::resource_key_t resource_key_t; - typedef const std::pair ResourcePair; - //typedef const std::map Parameters; + // typedef const std::pair ResourcePair; + //NOUSE typedef const std::map Parameters; + // associate old keys with new ones typedef std::map Parameters; + // associate old keys with new ones + //typedef pair TempPair; + typedef std::map TempMap; + void factory_method(const Glib::ustring& class_name, Parameters& parameters); protected: private: - ResourcePair create_resource(Parameters& parameters); + History::ResourcePair create_resource(Parameters& parameters); Process& create_process(Parameters& parameters); Thread& create_thread(Parameters& parameters); Request& create_request(Parameters& parameters); - Subrequest& create_subrequest(Parameters& parameters); + SubRequest& create_subrequest(Parameters& parameters); + // history object to add to resources processes etc... History* _hist; + + // memory of last objects to link to + Process* _last_process; + Thread* _last_thread; + Request* _last_request; + + // take association with old keys an new keys + // to reslove sub requests + TempMap _temp_map; }; } diff --git a/plugins/xmlsave/src/xml_visitor.cc b/plugins/xmlsave/src/xml_visitor.cc index 755c6b2..4883047 100644 --- a/plugins/xmlsave/src/xml_visitor.cc +++ b/plugins/xmlsave/src/xml_visitor.cc @@ -220,7 +220,7 @@ void XMLVisitor::from_request(xmlNodePtr parent, const Request& obj) while(iter!=end) { const SubRequest* sr = *iter; - cout << "SUB REQUEST: " << sr->get_resource_key() << " places: " << sr->get_places() << " length: " << sr->get_length() << endl; + // cout << "SUB REQUEST: " << sr->get_resource_key() << " places: " << sr->get_places() << " length: " << sr->get_length() << endl; from_subrequest(request_node, *(*iter)); iter++; }