X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/a92d7b716f51a53dea7f59db8524d4add713b910..dac676c1180a5ea95897072e55f698433a857d90:/src/surf/trace_mgr.cpp diff --git a/src/surf/trace_mgr.cpp b/src/surf/trace_mgr.cpp index 7771715793..07c5ba6f3e 100644 --- a/src/surf/trace_mgr.cpp +++ b/src/surf/trace_mgr.cpp @@ -1,23 +1,21 @@ -/* Copyright (c) 2004-2005, 2007, 2009-2014. The SimGrid Team. +/* Copyright (c) 2004-2005, 2007, 2009-2014, 2016-2017. The SimGrid Team. * All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ #include "xbt/sysdep.h" -#include "xbt/dict.h" #include "xbt/log.h" -#include "xbt/str.h" #include "src/surf/surf_interface.hpp" #include "src/surf/trace_mgr.hpp" -#include "surf_private.h" +#include "surf_private.hpp" #include "xbt/RngStream.h" #include #include #include +#include #include -#include #include #include @@ -25,7 +23,7 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_trace, surf, "Surf trace management"); namespace tmgr = simgrid::trace_mgr; -static std::unordered_map trace_list; +static std::unordered_map trace_list; static inline bool doubleEq(double d1, double d2) { @@ -52,20 +50,23 @@ trace::trace() } trace::~trace() = default; future_evt_set::future_evt_set() = default; -simgrid::trace_mgr::future_evt_set::~future_evt_set() +future_evt_set::~future_evt_set() { - xbt_heap_free(heap_); + while (not heap_.empty()) { + delete heap_.top().second; + heap_.pop(); + } } } } -tmgr_trace_t tmgr_trace_new_from_string(const char* name, std::string input, double periodicity) +tmgr_trace_t tmgr_trace_new_from_string(std::string name, std::string input, double periodicity) { int linecount = 0; tmgr_trace_t trace = new simgrid::trace_mgr::trace(); tmgr::DatedValue* last_event = &(trace->event_list.back()); - xbt_assert(trace_list.find(name) == trace_list.end(), "Refusing to define trace %s twice", name); + xbt_assert(trace_list.find(name) == trace_list.end(), "Refusing to define trace %s twice", name.c_str()); std::vector list; boost::split(list, input, boost::is_any_of("\n\r")); @@ -81,10 +82,10 @@ tmgr_trace_t tmgr_trace_new_from_string(const char* name, std::string input, dou continue; xbt_assert(sscanf(val.c_str(), "%lg %lg\n", &event.date_, &event.value_) == 2, "%s:%d: Syntax error in trace\n%s", - name, linecount, input.c_str()); + name.c_str(), linecount, input.c_str()); xbt_assert(last_event->date_ <= event.date_, - "%s:%d: Invalid trace: Events must be sorted, but time %g > time %g.\n%s", name, linecount, + "%s:%d: Invalid trace: Events must be sorted, but time %g > time %g.\n%s", name.c_str(), linecount, last_event->date_, event.date_, input.c_str()); last_event->date_ = event.date_ - last_event->date_; @@ -99,18 +100,18 @@ tmgr_trace_t tmgr_trace_new_from_string(const char* name, std::string input, dou } } - trace_list.insert({xbt_strdup(name), trace}); + trace_list.insert({name, trace}); return trace; } -tmgr_trace_t tmgr_trace_new_from_file(const char *filename) +tmgr_trace_t tmgr_trace_new_from_file(std::string filename) { - xbt_assert(filename && filename[0], "Cannot parse a trace from the null or empty filename"); - xbt_assert(trace_list.find(filename) == trace_list.end(), "Refusing to define trace %s twice", filename); + xbt_assert(not filename.empty(), "Cannot parse a trace from an empty filename"); + xbt_assert(trace_list.find(filename) == trace_list.end(), "Refusing to define trace %s twice", filename.c_str()); std::ifstream* f = surf_ifsopen(filename); - xbt_assert(not f->fail(), "Cannot open file '%s' (path=%s)", filename, (boost::join(surf_path, ":")).c_str()); + xbt_assert(not f->fail(), "Cannot open file '%s' (path=%s)", filename.c_str(), (boost::join(surf_path, ":")).c_str()); std::stringstream buffer; buffer << f->rdbuf(); @@ -120,18 +121,20 @@ tmgr_trace_t tmgr_trace_new_from_file(const char *filename) } /** @brief Registers a new trace into the future event set, and get an iterator over the integrated trace */ -tmgr_trace_event_t simgrid::trace_mgr::future_evt_set::add_trace(tmgr_trace_t trace, surf::Resource* resource) +tmgr_trace_event_t simgrid::trace_mgr::future_evt_set::add_trace(tmgr_trace_t trace, + kernel::resource::Resource* resource) { tmgr_trace_event_t trace_iterator = nullptr; - trace_iterator = xbt_new0(s_tmgr_trace_event_t, 1); - trace_iterator->trace = trace; - trace_iterator->idx = 0; + trace_iterator = new s_tmgr_trace_event_t; + trace_iterator->trace = trace; + trace_iterator->idx = 0; trace_iterator->resource = resource; + trace_iterator->free_me = false; xbt_assert((trace_iterator->idx < trace->event_list.size()), "Your trace should have at least one event!"); - xbt_heap_push(heap_, trace_iterator, 0. /*start_time*/); + heap_.emplace(0.0 /* start time */, trace_iterator); return trace_iterator; } @@ -139,22 +142,21 @@ tmgr_trace_event_t simgrid::trace_mgr::future_evt_set::add_trace(tmgr_trace_t tr /** @brief returns the date of the next occurring event (pure function) */ double simgrid::trace_mgr::future_evt_set::next_date() const { - if (xbt_heap_size(heap_)) - return (xbt_heap_maxkey(heap_)); - return -1.0; + return heap_.empty() ? -1.0 : heap_.top().first; } /** @brief Retrieves the next occurring event, or nullptr if none happens before #date */ tmgr_trace_event_t simgrid::trace_mgr::future_evt_set::pop_leq(double date, double* value, - simgrid::surf::Resource** resource) + kernel::resource::Resource** resource) { double event_date = next_date(); if (event_date > date) return nullptr; - tmgr_trace_event_t trace_iterator = (tmgr_trace_event_t)xbt_heap_pop(heap_); - if (trace_iterator == nullptr) + if (heap_.empty()) return nullptr; + tmgr_trace_event_t trace_iterator = heap_.top().second; + heap_.pop(); tmgr_trace_t trace = trace_iterator->trace; *resource = trace_iterator->resource; @@ -164,13 +166,13 @@ tmgr_trace_event_t simgrid::trace_mgr::future_evt_set::pop_leq(double date, doub *value = dateVal.value_; if (trace_iterator->idx < trace->event_list.size() - 1) { - xbt_heap_push(heap_, trace_iterator, event_date + dateVal.date_); + heap_.emplace(event_date + dateVal.date_, trace_iterator); trace_iterator->idx++; } else if (dateVal.date_ > 0) { /* Last element. Shall we loop? */ - xbt_heap_push(heap_, trace_iterator, event_date + dateVal.date_); + heap_.emplace(event_date + dateVal.date_, trace_iterator); trace_iterator->idx = 1; /* idx=0 is a placeholder to store when events really start */ } else { /* If we don't loop, we don't need this trace_event anymore */ - trace_iterator->free_me = 1; + trace_iterator->free_me = true; } return trace_iterator; @@ -178,17 +180,15 @@ tmgr_trace_event_t simgrid::trace_mgr::future_evt_set::pop_leq(double date, doub void tmgr_finalize() { - for (auto kv : trace_list) { - xbt_free((char*)kv.first); + for (auto const& kv : trace_list) delete kv.second; - } trace_list.clear(); } void tmgr_trace_event_unref(tmgr_trace_event_t* trace_event) { if ((*trace_event)->free_me) { - xbt_free(*trace_event); + delete *trace_event; *trace_event = nullptr; } }