From aaa2d952c3f91042dc283f8dd4de9dd41b62e3ba Mon Sep 17 00:00:00 2001 From: Gabriel Corona Date: Tue, 5 Apr 2016 14:48:33 +0200 Subject: [PATCH] [mc] Use std::string in simgrid::mc::request_to_string --- include/xbt/string.hpp | 21 ++++++------ src/mc/CommunicationDeterminismChecker.cpp | 18 ++++------ src/mc/LivenessChecker.cpp | 25 ++++++-------- src/mc/SafetyChecker.cpp | 32 ++++++++---------- src/mc/mc_global.cpp | 9 +++-- src/mc/mc_request.cpp | 10 +++--- src/mc/mc_request.h | 4 ++- src/xbt/string.cpp | 39 ++++++++++++++++++++++ 8 files changed, 93 insertions(+), 65 deletions(-) diff --git a/include/xbt/string.hpp b/include/xbt/string.hpp index 9840855a42..be542c0914 100644 --- a/include/xbt/string.hpp +++ b/include/xbt/string.hpp @@ -9,20 +9,26 @@ #include +#include +#include + #if HAVE_MC #include #include #include #include -#include #include #include +#endif + namespace simgrid { namespace xbt { +#if HAVE_MC + /** POD structure representation of a string */ struct string_data { @@ -281,21 +287,16 @@ bool operator>=(std::string const& a, string const& b) return b <= a; } -} -} - #else -#include +typedef std::string string; -namespace simgrid { -namespace xbt { +#endif -typedef std::string string; +std::string string_vprintf(const char *fmt, va_list ap); +std::string string_printf(const char *fmt, ...); } } #endif - -#endif diff --git a/src/mc/CommunicationDeterminismChecker.cpp b/src/mc/CommunicationDeterminismChecker.cpp index 4c51b1b2a6..15f19f05f8 100644 --- a/src/mc/CommunicationDeterminismChecker.cpp +++ b/src/mc/CommunicationDeterminismChecker.cpp @@ -323,12 +323,9 @@ std::vector CommunicationDeterminismChecker::getTextualTrace() // o for (auto const& state : stack_) { int value; smx_simcall_t req = MC_state_get_executed_request(state.get(), &value); - if (req) { - char* req_str = simgrid::mc::request_to_string( - req, value, simgrid::mc::RequestType::executed); - trace.push_back(req_str); - xbt_free(req_str); - } + if (req) + trace.push_back(simgrid::mc::request_to_string( + req, value, simgrid::mc::RequestType::executed)); } return trace; } @@ -387,8 +384,6 @@ bool all_communications_are_finished() int CommunicationDeterminismChecker::main(void) { - - char *req_str = nullptr; int value; std::unique_ptr visited_state = nullptr; smx_simcall_t req = nullptr; @@ -410,10 +405,11 @@ int CommunicationDeterminismChecker::main(void) && (req = MC_state_get_request(state, &value)) && (visited_state == nullptr)) { - req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix); - XBT_DEBUG("Execute: %s", req_str); - xbt_free(req_str); + XBT_DEBUG("Execute: %s", + simgrid::mc::request_to_string( + req, value, simgrid::mc::RequestType::simix).c_str()); + char* req_str = nullptr; if (dot_output != nullptr) req_str = simgrid::mc::request_get_dot_output(req, value); diff --git a/src/mc/LivenessChecker.cpp b/src/mc/LivenessChecker.cpp index 086d12252a..5ded308d47 100644 --- a/src/mc/LivenessChecker.cpp +++ b/src/mc/LivenessChecker.cpp @@ -211,12 +211,11 @@ void LivenessChecker::replay() req = &issuer->simcall; /* Debug information */ - if (XBT_LOG_ISENABLED(mc_liveness, xbt_log_priority_debug)) { - char* req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix); - XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state.get()); - xbt_free(req_str); - } - + XBT_DEBUG("Replay (depth = %d) : %s (%p)", + depth, + simgrid::mc::request_to_string( + req, value, simgrid::mc::RequestType::simix).c_str(), + state.get()); } simgrid::mc::handle_simcall(req, value); @@ -328,11 +327,9 @@ std::vector LivenessChecker::getTextualTrace() // override for (std::shared_ptr const& pair : explorationStack_) { int value; smx_simcall_t req = MC_state_get_executed_request(pair->graph_state.get(), &value); - if (req && req->call != SIMCALL_NONE) { - char* req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::executed); - trace.push_back(std::string(req_str)); - xbt_free(req_str); - } + if (req && req->call != SIMCALL_NONE) + trace.push_back(simgrid::mc::request_to_string( + req, value, simgrid::mc::RequestType::executed)); } return trace; } @@ -393,9 +390,9 @@ int LivenessChecker::main(void) fflush(dot_output); } - char* req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix); - XBT_DEBUG("Execute: %s", req_str); - xbt_free(req_str); + XBT_DEBUG("Execute: %s", + simgrid::mc::request_to_string( + req, value, simgrid::mc::RequestType::simix).c_str()); /* Set request as executed */ MC_state_set_executed_request(current_pair->graph_state.get(), req, value); diff --git a/src/mc/SafetyChecker.cpp b/src/mc/SafetyChecker.cpp index 0f20188764..4b8c59f900 100644 --- a/src/mc/SafetyChecker.cpp +++ b/src/mc/SafetyChecker.cpp @@ -81,12 +81,9 @@ std::vector SafetyChecker::getTextualTrace() // override for (auto const& state : stack_) { int value; smx_simcall_t req = MC_state_get_executed_request(state.get(), &value); - if (req) { - char* req_str = simgrid::mc::request_to_string( - req, value, simgrid::mc::RequestType::executed); - trace.push_back(req_str); - xbt_free(req_str); - } + if (req) + trace.push_back(simgrid::mc::request_to_string( + req, value, simgrid::mc::RequestType::executed)); } return trace; } @@ -125,12 +122,9 @@ int SafetyChecker::run() // If there are processes to interleave and the maximum depth has not been // reached then perform one step of the exploration algorithm. - - if (XBT_LOG_ISENABLED(mc_safety, xbt_log_priority_debug)) { - char* req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix); - XBT_DEBUG("Execute: %s", req_str); - xbt_free(req_str); - } + XBT_DEBUG("Execute: %s", + simgrid::mc::request_to_string( + req, value, simgrid::mc::RequestType::simix).c_str()); char* req_str = nullptr; if (dot_output != nullptr) @@ -232,13 +226,15 @@ int SafetyChecker::backtrack() XBT_DEBUG("Dependent Transitions:"); int value; smx_simcall_t prev_req = MC_state_get_executed_request(prev_state, &value); - char* req_str = simgrid::mc::request_to_string(prev_req, value, simgrid::mc::RequestType::internal); - XBT_DEBUG("%s (state=%d)", req_str, prev_state->num); - xbt_free(req_str); + XBT_DEBUG("%s (state=%d)", + simgrid::mc::request_to_string( + prev_req, value, simgrid::mc::RequestType::internal).c_str(), + prev_state->num); prev_req = MC_state_get_executed_request(state.get(), &value); - req_str = simgrid::mc::request_to_string(prev_req, value, simgrid::mc::RequestType::executed); - XBT_DEBUG("%s (state=%d)", req_str, state->num); - xbt_free(req_str); + XBT_DEBUG("%s (state=%d)", + simgrid::mc::request_to_string( + prev_req, value, simgrid::mc::RequestType::executed).c_str(), + state->num); } if (!prev_state->processStates[issuer->pid].done()) diff --git a/src/mc/mc_global.cpp b/src/mc/mc_global.cpp index ad03c3ff74..079c2aa563 100644 --- a/src/mc/mc_global.cpp +++ b/src/mc/mc_global.cpp @@ -167,11 +167,10 @@ void replay(std::list> const& stack) smx_simcall_t req = &issuer->simcall; /* Debug information */ - if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) { - char* req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix); - XBT_DEBUG("Replay: %s (%p)", req_str, state.get()); - xbt_free(req_str); - } + XBT_DEBUG("Replay: %s (%p)", + simgrid::mc::request_to_string( + req, value, simgrid::mc::RequestType::simix).c_str(), + state.get()); /* TODO : handle test and testany simcalls */ e_mc_call_type_t call = MC_CALL_TYPE_NONE; diff --git a/src/mc/mc_request.cpp b/src/mc/mc_request.cpp index 1404c21e6f..556e3358aa 100644 --- a/src/mc/mc_request.cpp +++ b/src/mc/mc_request.cpp @@ -200,7 +200,7 @@ static char *buff_size_to_string(size_t buff_size) } -char *simgrid::mc::request_to_string(smx_simcall_t req, int value, simgrid::mc::RequestType request_type) +std::string simgrid::mc::request_to_string(smx_simcall_t req, int value, simgrid::mc::RequestType request_type) { bool use_remote_comm = true; switch(request_type) { @@ -401,16 +401,14 @@ char *simgrid::mc::request_to_string(smx_simcall_t req, int value, simgrid::mc:: THROW_UNIMPLEMENTED; } - char* str; + std::string str; if (args != nullptr) - str = - bprintf("[(%lu)%s (%s)] %s(%s)", issuer->pid, + str = simgrid::xbt::string_printf("[(%lu)%s (%s)] %s(%s)", issuer->pid, MC_smx_process_get_host_name(issuer), MC_smx_process_get_name(issuer), type, args); else - str = - bprintf("[(%lu)%s (%s)] %s ", issuer->pid, + str = simgrid::xbt::string_printf("[(%lu)%s (%s)] %s ", issuer->pid, MC_smx_process_get_host_name(issuer), MC_smx_process_get_name(issuer), type); diff --git a/src/mc/mc_request.h b/src/mc/mc_request.h index 2fa08961aa..97c153ac79 100644 --- a/src/mc/mc_request.h +++ b/src/mc/mc_request.h @@ -7,6 +7,8 @@ #ifndef SIMGRID_MC_REQUEST_H #define SIMGRID_MC_REQUEST_H +#include + #include #include "src/simix/smx_private.h" @@ -22,7 +24,7 @@ enum class RequestType { XBT_PRIVATE bool request_depend(smx_simcall_t req1, smx_simcall_t req2); -XBT_PRIVATE char* request_to_string(smx_simcall_t req, int value, simgrid::mc::RequestType type); +XBT_PRIVATE std::string request_to_string(smx_simcall_t req, int value, simgrid::mc::RequestType type); XBT_PRIVATE bool request_is_enabled_by_idx(smx_simcall_t req, unsigned int idx); diff --git a/src/xbt/string.cpp b/src/xbt/string.cpp index f378cec98a..f23e4295ff 100644 --- a/src/xbt/string.cpp +++ b/src/xbt/string.cpp @@ -4,8 +4,13 @@ /* 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 +#include + #include + #include +#include namespace simgrid { namespace xbt { @@ -16,5 +21,39 @@ const char string::NUL = '\0'; #endif +std::string string_vprintf(const char *fmt, va_list ap) +{ + // Get the size: + va_list ap2; + va_copy(ap2, ap); + int size = std::vsnprintf(nullptr, 0, fmt, ap2); + va_end(ap2); + if (size < 0) + xbt_die("string_vprintf error"); + + // Allocate the string and format: + std::string res; + res.resize(size); + if (size != 0 && std::vsnprintf(&res[0], size + 1, fmt, ap) != size) + xbt_die("string_vprintf error"); + return res; +} + +std::string string_printf(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + std::string res; + try { + res = string_vprintf(fmt, ap); + } + catch(...) { + va_end(ap); + throw; + } + va_end(ap); + return res; +} + } } -- 2.20.1