From 25a83061ae2c4418c119fb6e47956793183f9c57 Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Tue, 15 Mar 2016 11:22:38 +0100 Subject: [PATCH] further objectifies the traces - Convert the list of all traces from xbt_dict to std::unordered_map - Refuse "" as a trace filename and fix the calling code --- src/bindings/lua/lua_platf.cpp | 20 ++++++++--- src/surf/sg_platf.cpp | 6 ++-- src/surf/trace_mgr.cpp | 44 +++++++++--------------- src/surf/xml/surfxml_sax_cb.cpp | 14 ++++---- teshsuite/surf/surf_usage/surf_usage.cpp | 6 ++-- 5 files changed, 45 insertions(+), 45 deletions(-) diff --git a/src/bindings/lua/lua_platf.cpp b/src/bindings/lua/lua_platf.cpp index e089aff122..5fb2ccd447 100644 --- a/src/bindings/lua/lua_platf.cpp +++ b/src/bindings/lua/lua_platf.cpp @@ -202,13 +202,17 @@ int console_add_host(lua_State *L) { //get power_trace lua_pushstring(L, "availability_file"); lua_gettable(L, -2); - host.speed_trace = tmgr_trace_new_from_file(lua_tostring(L, -1)); + const char *filename = lua_tostring(L, -1); + if (filename) + host.speed_trace = tmgr_trace_new_from_file(filename); lua_pop(L, 1); //get trace state lua_pushstring(L, "state_file"); lua_gettable(L, -2); - host.state_trace = tmgr_trace_new_from_file(lua_tostring(L, -1)); + filename = lua_tostring(L, -1); + if (filename) + host.state_trace = tmgr_trace_new_from_file(filename); lua_pop(L, 1); sg_platf_new_host(&host); @@ -267,19 +271,25 @@ int console_add_link(lua_State *L) { //get bandwidth_trace value lua_pushstring(L, "bandwidth_file"); lua_gettable(L, -2); - link.bandwidth_trace = tmgr_trace_new_from_file(lua_tostring(L, -1)); + const char *filename = lua_tostring(L, -1); + if (filename) + link.bandwidth_trace = tmgr_trace_new_from_file(filename); lua_pop(L, 1); //get latency_trace value lua_pushstring(L, "latency_file"); lua_gettable(L, -2); - link.latency_trace = tmgr_trace_new_from_file(lua_tostring(L, -1)); + filename = lua_tostring(L, -1); + if (filename) + link.latency_trace = tmgr_trace_new_from_file(filename); lua_pop(L, 1); //get state_trace value lua_pushstring(L, "state_file"); lua_gettable(L, -2); - link.state_trace = tmgr_trace_new_from_file(lua_tostring(L, -1)); + filename = lua_tostring(L, -1); + if (filename) + link.state_trace = tmgr_trace_new_from_file(filename); lua_pop(L, 1); //get policy value diff --git a/src/surf/sg_platf.cpp b/src/surf/sg_platf.cpp index 3adc2e1aeb..11bbd1879e 100644 --- a/src/surf/sg_platf.cpp +++ b/src/surf/sg_platf.cpp @@ -270,7 +270,8 @@ void sg_platf_new_cluster(sg_platf_cluster_cbarg_t cluster) xbt_dict_set(patterns, "radical", bprintf("%d", i), NULL); char *avail_file = xbt_str_varsubst(cluster->availability_trace, patterns); XBT_DEBUG("\tavailability_file=\"%s\"", avail_file); - host.speed_trace = tmgr_trace_new_from_file(avail_file); + if (avail_file && avail_file[0]) + host.speed_trace = tmgr_trace_new_from_file(avail_file); xbt_free(avail_file); } else { XBT_DEBUG("\tavailability_file=\"\""); @@ -279,7 +280,8 @@ void sg_platf_new_cluster(sg_platf_cluster_cbarg_t cluster) if (cluster->state_trace && strcmp(cluster->state_trace, "")) { char *avail_file = xbt_str_varsubst(cluster->state_trace, patterns); XBT_DEBUG("\tstate_file=\"%s\"", avail_file); - host.state_trace = tmgr_trace_new_from_file(avail_file); + if (avail_file && avail_file[0]) + host.state_trace = tmgr_trace_new_from_file(avail_file); xbt_free(avail_file); } else { XBT_DEBUG("\tstate_file=\"\""); diff --git a/src/surf/trace_mgr.cpp b/src/surf/trace_mgr.cpp index 0b9cd01d73..b7412f83df 100644 --- a/src/surf/trace_mgr.cpp +++ b/src/surf/trace_mgr.cpp @@ -12,13 +12,15 @@ #include "surf_private.h" #include "xbt/RngStream.h" #include +#include XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_trace, surf, "Surf trace management"); -static xbt_dict_t trace_list = NULL; +static std::unordered_map trace_list; simgrid::trace_mgr::trace::trace() { + event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL); } simgrid::trace_mgr::trace::~trace() @@ -34,7 +36,7 @@ simgrid::trace_mgr::future_evt_set::~future_evt_set() xbt_heap_free(p_heap); } -tmgr_trace_t tmgr_trace_new_from_string(const char *id, const char *input, double periodicity) +tmgr_trace_t tmgr_trace_new_from_string(const char *name, const char *input, double periodicity) { tmgr_trace_t trace = NULL; int linecount = 0; @@ -44,19 +46,14 @@ tmgr_trace_t tmgr_trace_new_from_string(const char *id, const char *input, doubl unsigned int cpt; char *val; - if (trace_list) { - trace = (tmgr_trace_t)xbt_dict_get_or_null(trace_list, id); - if (trace) { - XBT_WARN("Ignoring redefinition of trace %s", id); - return trace; - } + if (trace_list.find(name) != trace_list.end()) { + XBT_WARN("Ignoring redefinition of trace %s", name); + return trace_list.at(name); } - xbt_assert(periodicity >= 0, - "Invalid periodicity %g (must be positive)", periodicity); + xbt_assert(periodicity >= 0, "Invalid periodicity %g (must be positive)", periodicity); trace = new simgrid::trace_mgr::trace(); - trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL); list = xbt_str_split(input, "\n\r"); @@ -70,13 +67,13 @@ tmgr_trace_t tmgr_trace_new_from_string(const char *id, const char *input, doubl continue; if (sscanf(val, "%lg" " " "%lg" "\n", &event.delta, &event.value) != 2) - xbt_die("%s:%d: Syntax error in trace\n%s", id, linecount, input); + xbt_die("%s:%d: Syntax error in trace\n%s", name, linecount, input); if (last_event) { if (last_event->delta > event.delta) { xbt_die("%s:%d: Invalid trace: Events must be sorted, " "but time %g > time %g.\n%s", - id, linecount, last_event->delta, event.delta, input); + name, linecount, last_event->delta, event.delta, input); } last_event->delta = event.delta - last_event->delta; } else { @@ -93,10 +90,7 @@ tmgr_trace_t tmgr_trace_new_from_string(const char *id, const char *input, doubl if (last_event) last_event->delta = periodicity; - if (!trace_list) - trace_list = xbt_dict_new_homogeneous((void (*)(void *)) tmgr_trace_free); - - xbt_dict_set(trace_list, id, (void *) trace, NULL); + trace_list.insert({xbt_strdup(name), trace}); xbt_dynar_free(&list); return trace; @@ -106,15 +100,11 @@ tmgr_trace_t tmgr_trace_new_from_file(const char *filename) { tmgr_trace_t trace = NULL; - if ((!filename) || (strcmp(filename, "") == 0)) - return NULL; + xbt_assert(filename && filename[0], "Cannot parse a trace from the null or empty filename"); - if (trace_list) { - trace = (tmgr_trace_t)xbt_dict_get_or_null(trace_list, filename); - if (trace) { - XBT_WARN("Ignoring redefinition of trace %s", filename); - return trace; - } + if (trace_list.find(filename) != trace_list.end()) { + XBT_WARN("Ignoring redefinition of trace file %s", filename); + return trace_list.at(filename); } FILE *f = surf_fopen(filename, "r"); @@ -135,7 +125,6 @@ tmgr_trace_t tmgr_empty_trace_new(void) s_tmgr_event_t event; trace = new simgrid::trace_mgr::trace(); - trace->event_list = xbt_dynar_new(sizeof(s_tmgr_event_t), NULL); event.delta = 0.0; event.value = 0.0; @@ -209,7 +198,8 @@ tmgr_trace_iterator_t simgrid::trace_mgr::future_evt_set::pop_leq( void tmgr_finalize(void) { - xbt_dict_free(&trace_list); + for (auto kv : trace_list) + delete kv.second; } void tmgr_trace_event_unref(tmgr_trace_iterator_t *trace_event) diff --git a/src/surf/xml/surfxml_sax_cb.cpp b/src/surf/xml/surfxml_sax_cb.cpp index 4640bf074e..d98e65ebd3 100644 --- a/src/surf/xml/surfxml_sax_cb.cpp +++ b/src/surf/xml/surfxml_sax_cb.cpp @@ -478,8 +478,8 @@ void ETag_surfxml_host(void) { XBT_DEBUG("pstate: %s", A_surfxml_host_pstate); host.core_amount = surf_parse_get_int(A_surfxml_host_core); - host.speed_trace = tmgr_trace_new_from_file(A_surfxml_host_availability___file); - host.state_trace = tmgr_trace_new_from_file(A_surfxml_host_state___file); + host.speed_trace = A_surfxml_host_availability___file[0] ? tmgr_trace_new_from_file(A_surfxml_host_availability___file) : NULL; + host.state_trace = A_surfxml_host_state___file[0] ? tmgr_trace_new_from_file(A_surfxml_host_state___file) : NULL; host.pstate = surf_parse_get_int(A_surfxml_host_pstate); host.coord = A_surfxml_host_coordinates; @@ -616,8 +616,8 @@ void STag_surfxml_peer(void){ peer.bw_out = surf_parse_get_bandwidth(A_surfxml_peer_bw___out, "bw_out of peer", peer.id); peer.lat = surf_parse_get_time(A_surfxml_peer_lat, "lat of peer", peer.id); peer.coord = A_surfxml_peer_coordinates; - peer.availability_trace = tmgr_trace_new_from_file(A_surfxml_peer_availability___file); - peer.state_trace = tmgr_trace_new_from_file(A_surfxml_peer_state___file); + peer.availability_trace = A_surfxml_peer_availability___file[0] ? tmgr_trace_new_from_file(A_surfxml_peer_availability___file) : NULL; + peer.state_trace = A_surfxml_peer_state___file[0] ? tmgr_trace_new_from_file(A_surfxml_peer_state___file) : NULL; sg_platf_new_peer(&peer); } @@ -634,10 +634,10 @@ void ETag_surfxml_link(void){ link.properties = current_property_set; link.id = A_surfxml_link_id; link.bandwidth = surf_parse_get_bandwidth(A_surfxml_link_bandwidth, "bandwidth of link", link.id); - link.bandwidth_trace = tmgr_trace_new_from_file(A_surfxml_link_bandwidth___file); + link.bandwidth_trace = A_surfxml_link_bandwidth___file[0] ? tmgr_trace_new_from_file(A_surfxml_link_bandwidth___file) : NULL; link.latency = surf_parse_get_time(A_surfxml_link_latency, "latency of link", link.id); - link.latency_trace = tmgr_trace_new_from_file(A_surfxml_link_latency___file); - link.state_trace = tmgr_trace_new_from_file(A_surfxml_link_state___file); + link.latency_trace = A_surfxml_link_latency___file[0] ? tmgr_trace_new_from_file(A_surfxml_link_latency___file) : NULL; + link.state_trace = A_surfxml_link_state___file[0] ? tmgr_trace_new_from_file(A_surfxml_link_state___file):NULL; switch (A_surfxml_link_sharing___policy) { case A_surfxml_link_sharing___policy_SHARED: diff --git a/teshsuite/surf/surf_usage/surf_usage.cpp b/teshsuite/surf/surf_usage/surf_usage.cpp index e0ec894599..4b5a9e903f 100644 --- a/teshsuite/surf/surf_usage/surf_usage.cpp +++ b/teshsuite/surf/surf_usage/surf_usage.cpp @@ -14,11 +14,9 @@ #include "src/surf/cpu_interface.hpp" #include "xbt/log.h" -XBT_LOG_NEW_DEFAULT_CATEGORY(surf_test, - "Messages specific for surf example"); +XBT_LOG_NEW_DEFAULT_CATEGORY(surf_test, "Messages specific for surf example"); -const char *string_action(e_surf_action_state_t state); -const char *string_action(e_surf_action_state_t state) +static const char *string_action(e_surf_action_state_t state) { switch (state) { case (SURF_ACTION_READY): -- 2.20.1