X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/b4a423f5085cf79b6c539eae25549bb66e8e3033..8c6bbb14f44b8daed5ea43d80880c6bf2155118b:/src/kernel/resource/profile/ProfileBuilder.cpp diff --git a/src/kernel/resource/profile/ProfileBuilder.cpp b/src/kernel/resource/profile/ProfileBuilder.cpp index b92928ed5f..83c587d5c3 100644 --- a/src/kernel/resource/profile/ProfileBuilder.cpp +++ b/src/kernel/resource/profile/ProfileBuilder.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2004-2022. The SimGrid Team. All rights reserved. */ +/* Copyright (c) 2004-2023. 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. */ @@ -7,18 +7,18 @@ #include "simgrid/forward.h" #include "src/kernel/resource/profile/Profile.hpp" #include "src/kernel/resource/profile/StochasticDatedValue.hpp" -#include "src/surf/surf_interface.hpp" +#include "xbt/asserts.h" +#include "xbt/file.hpp" #include #include #include #include +#include #include +#include - -namespace simgrid { -namespace kernel { -namespace profile { +namespace simgrid::kernel::profile { bool DatedValue::operator==(DatedValue const& e2) const { @@ -68,29 +68,26 @@ std::ostream& operator << (std::ostream& out, const DatedValue& dv) { class LegacyUpdateCb { std::vector pattern; - - bool stochastic; - + bool stochastic = false; bool loop; + double loop_delay = 0.0; - double loop_delay; - - static bool is_comment_or_empty_line(const std::string& val) + static bool is_comment_or_empty_line(std::string_view val) { - return (val[0] == '#' || val[0] == '\0' || val[0] == '%'); + return (val.empty() || val.front() == '#' || val.front() == '%'); } - static bool is_normal_distribution(const std::string& val) + static bool is_normal_distribution(std::string_view val) { return (val == "NORM" || val == "NORMAL" || val == "GAUSS" || val == "GAUSSIAN"); } - static bool is_exponential_distribution(const std::string& val) { return (val == "EXP" || val == "EXPONENTIAL"); } + static bool is_exponential_distribution(std::string_view val) { return (val == "EXP" || val == "EXPONENTIAL"); } - static bool is_uniform_distribution(const std::string& val) { return (val == "UNIF" || val == "UNIFORM"); } + static bool is_uniform_distribution(std::string_view val) { return (val == "UNIF" || val == "UNIFORM"); } public: - LegacyUpdateCb(const std::string& input, double periodicity) : stochastic(false), loop(periodicity > 0), loop_delay(0) + LegacyUpdateCb(const std::string& input, double periodicity) : loop(periodicity > 0) { int linecount = 0; std::vector list; @@ -147,7 +144,7 @@ public: } xbt_assert(splittedval.size() > i, "Invalid profile line"); - if (i == 1 or i == 2) { + if (i == 1 || i == 2) { stochevent.date_params = {std::stod(splittedval[i - 1])}; } else if (i == 3) { stochevent.date_params = {std::stod(splittedval[1]), std::stod(splittedval[2])}; @@ -172,7 +169,7 @@ public: } xbt_assert(splittedval.size() > i + j, "Invalid profile line"); - if (j == 0 or j == 1) { + if (j == 0 || j == 1) { stochevent.value_params = {std::stod(splittedval[i + j])}; } else if (j == 2) { stochevent.value_params = {std::stod(splittedval[i + 1]), std::stod(splittedval[i + 2])}; @@ -191,9 +188,9 @@ public: pattern.emplace_back(stochevent); } - xbt_assert(not stochastic or periodicity <= 0, "If you want periodicity with stochastic profiles, use LOOP_AFTER"); + xbt_assert(not stochastic || periodicity <= 0, "If you want periodicity with stochastic profiles, use LOOP_AFTER"); if (periodicity > 0) { - xbt_assert(loop and loop_delay == 0); + xbt_assert(loop && loop_delay == 0); loop_delay = periodicity - last_date; } @@ -202,7 +199,7 @@ public: double get_repeat_delay() const { - if (not stochastic and loop) + if (not stochastic && loop) return loop_delay; return -1.0; } @@ -210,13 +207,15 @@ public: void operator()(std::vector& event_list) const { size_t initial_size = event_list.size(); - if (loop or not initial_size) { + if (loop || not initial_size) { for (auto const& dv : pattern) event_list.emplace_back(dv.get_datedvalue()); if (initial_size) event_list.at(initial_size).date_ += loop_delay; } } + + std::vector get_pattern() const { return pattern; } }; Profile* ProfileBuilder::from_string(const std::string& name, const std::string& input, double periodicity) @@ -225,30 +224,36 @@ Profile* ProfileBuilder::from_string(const std::string& name, const std::string& return new Profile(name,cb,cb.get_repeat_delay()); } -Profile* ProfileBuilder::from_file(const std::string& path) +Profile* ProfileBuilder::from_file(const std::string& filename) { - xbt_assert(not path.empty(), "Cannot parse a trace from an empty filename"); - auto f = std::unique_ptr(surf_ifsopen(path)); - xbt_assert(not f->fail(), "Cannot open file '%s' (path=%s)", path.c_str(), (boost::join(surf_path, ":")).c_str()); + xbt_assert(not filename.empty(), "Cannot parse a trace from an empty filename"); + auto f = std::unique_ptr(simgrid::xbt::path_ifsopen(filename)); + xbt_assert(not f->fail(), "Cannot open file '%s' (path=%s)", filename.c_str(), + simgrid::xbt::path_to_string().c_str()); std::stringstream buffer; buffer << f->rdbuf(); LegacyUpdateCb cb(buffer.str(), -1); - return new Profile(path,cb,cb.get_repeat_delay()); + return new Profile(filename, cb, cb.get_repeat_delay()); } Profile* ProfileBuilder::from_void() { - static Profile void_profile("__void__",[](std::vector&){},-1.0); - return &void_profile; + static auto* void_profile = new Profile("__void__", nullptr, -1.0); + return void_profile; } Profile* ProfileBuilder::from_callback(const std::string& name, const std::function& cb, double repeat_delay) { return new Profile(name, cb, repeat_delay); } +} // namespace simgrid::kernel::profile + +std::vector trace2selist( const char* c_str) { + std::string str(c_str); + simgrid::kernel::profile::LegacyUpdateCb cb(str,0); + return cb.get_pattern(); +} + -} // namespace profile -} // namespace kernel -} // namespace simgrid