1 /* Copyright (c) 2004-2023. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "simgrid/kernel/ProfileBuilder.hpp"
7 #include "simgrid/forward.h"
8 #include "src/kernel/resource/profile/Profile.hpp"
9 #include "src/kernel/resource/profile/StochasticDatedValue.hpp"
10 #include "xbt/asserts.h"
11 #include "xbt/file.hpp"
13 #include <boost/algorithm/string.hpp>
14 #include <boost/intrusive/options.hpp>
19 #include <string_view>
21 namespace simgrid::kernel::profile {
23 bool DatedValue::operator==(DatedValue const& e2) const
25 return (fabs(date_ - e2.date_) < 0.0001) && (fabs(value_ - e2.value_) < 0.0001);
28 std::ostream& operator << (std::ostream& out, const DatedValue& dv) {
29 out<<"(+"<<dv.date_<<','<<dv.value_<<')';
33 /** @brief This callback implements the support for legacy profile syntax
35 * These profiles support two orthogonal concepts:
36 * - One-shot/Loop: the given list of {date,value} pairs can be repeated forever or just once.
37 * - Deterministic/Stochastic: dates and/or values can be drawn according to a stochastic law.
39 * A legacy profile is composed of "global" instructions and "local" instructions.
41 * Global instructions set properties that are true for all {date,value} pairs. They include
42 * - STOCHASTIC -> declare that the profile will use stochastic {date,value} pairs and not loop.
43 * - STOCHASTIC_LOOP -> declare that the profile will use stochastic {date,value} pairs and loop.
44 * - PERIODICITY -> declare the period for each iteration of the profile pattern.
45 * - LOOP_AFTER -> declare that the profile pattern will start over after a fixed delay
47 * Note that PERIODICITY and LOOP_AFTER have slightly different meanings.
48 * PERIODICITY represents the delay between two iterations, for instance the time for the first pair of the pattern in
49 * two successive iterations. Hence, PERIODICITY only has meaning for completely deterministic profiles. LOOP_AFTER
50 * represents an additional delay between the last pair of a profile pattern iteration and the first pair of the next
53 * Local instructions define one {date,value} pair, or more exactly one pattern.
54 * In effect, when a profile loops or has stochastic components, the actual {date,value} pairs will be generated
55 * dynamically. Roughly, a local instruction is a line with the following syntax: <date spec> <value spec>
57 * Both date and value specifications may have one of the following formats:
58 * - <num> : in completely deterministic profiles, use this number
59 * - DET <num> : in stochastic profiles, use this number
60 * - NORM <mean> <standard deviation> : draw number from a normal distribution of given parameters
61 * - UNIF <min> <max> : draw number from an uniform distribution of given parameters
62 * - EXP <lambda> : draw number from an exponential distribution of given parameters
64 * Note that in stochastic profiles, the date component is (necessarily) representing the delay between two pairs;
65 * whereas in deterministic profiles, the date components represent the absolute date at which the elements are used in
66 * the first iteration.
69 class LegacyUpdateCb {
70 std::vector<StochasticDatedValue> pattern;
71 bool stochastic = false;
73 double loop_delay = 0.0;
75 static bool is_comment_or_empty_line(std::string_view val)
77 return (val.empty() || val.front() == '#' || val.front() == '%');
80 static bool is_normal_distribution(std::string_view val)
82 return (val == "NORM" || val == "NORMAL" || val == "GAUSS" || val == "GAUSSIAN");
85 static bool is_exponential_distribution(std::string_view val) { return (val == "EXP" || val == "EXPONENTIAL"); }
87 static bool is_uniform_distribution(std::string_view val) { return (val == "UNIF" || val == "UNIFORM"); }
90 LegacyUpdateCb(const std::string& input, double periodicity) : loop(periodicity > 0)
93 std::vector<std::string> list;
95 boost::split(list, input, boost::is_any_of("\n\r"));
96 for (auto val : list) {
97 simgrid::kernel::profile::StochasticDatedValue stochevent;
100 if (is_comment_or_empty_line(val))
102 if (sscanf(val.c_str(), "PERIODICITY %lg\n", &periodicity) == 1) {
106 if (sscanf(val.c_str(), "LOOPAFTER %lg\n", &loop_delay) == 1) {
110 if (val == "STOCHASTIC LOOP") {
115 if (val == "STOCHASTIC") {
122 std::istringstream iss(val);
123 std::vector<std::string> splittedval((std::istream_iterator<std::string>(iss)),
124 std::istream_iterator<std::string>());
126 xbt_assert(not splittedval.empty(), "Invalid profile line");
128 if (splittedval[0] == "DET") {
129 stochevent.date_law = Distribution::DET;
131 } else if (is_normal_distribution(splittedval[0])) {
132 stochevent.date_law = Distribution::NORM;
134 } else if (is_exponential_distribution(splittedval[0])) {
135 stochevent.date_law = Distribution::EXP;
137 } else if (is_uniform_distribution(splittedval[0])) {
138 stochevent.date_law = Distribution::UNIF;
141 xbt_assert(not stochastic);
142 stochevent.date_law = Distribution::DET;
146 xbt_assert(splittedval.size() > i, "Invalid profile line");
147 if (i == 1 || i == 2) {
148 stochevent.date_params = {std::stod(splittedval[i - 1])};
150 stochevent.date_params = {std::stod(splittedval[1]), std::stod(splittedval[2])};
153 if (splittedval[i] == "DET") {
154 stochevent.value_law = Distribution::DET;
156 } else if (is_normal_distribution(splittedval[i])) {
157 stochevent.value_law = Distribution::NORM;
159 } else if (is_exponential_distribution(splittedval[i])) {
160 stochevent.value_law = Distribution::EXP;
162 } else if (is_uniform_distribution(splittedval[i])) {
163 stochevent.value_law = Distribution::UNIF;
166 xbt_assert(not stochastic);
167 stochevent.value_law = Distribution::DET;
171 xbt_assert(splittedval.size() > i + j, "Invalid profile line");
172 if (j == 0 || j == 1) {
173 stochevent.value_params = {std::stod(splittedval[i + j])};
175 stochevent.value_params = {std::stod(splittedval[i + 1]), std::stod(splittedval[i + 2])};
178 if (not stochastic) {
179 // In this mode, dates read from the string are absolute values
180 double new_date = stochevent.date_params[0];
181 xbt_assert(new_date >= 0, "Profile time value is negative, why?");
182 xbt_assert(last_date <= new_date, "%d: Invalid trace: Events must be sorted, but time %g > time %g.\n%s",
183 linecount, last_date, new_date, input.c_str());
184 stochevent.date_params[0] -= last_date;
185 last_date = new_date;
188 pattern.emplace_back(stochevent);
191 xbt_assert(not stochastic || periodicity <= 0, "If you want periodicity with stochastic profiles, use LOOP_AFTER");
192 if (periodicity > 0) {
193 xbt_assert(loop && loop_delay == 0);
194 loop_delay = periodicity - last_date;
197 xbt_assert(loop_delay >= 0, "Profile loop conditions are not realizable!");
200 double get_repeat_delay() const
202 if (not stochastic && loop)
207 void operator()(std::vector<DatedValue>& event_list) const
209 size_t initial_size = event_list.size();
210 if (loop || not initial_size) {
211 for (auto const& dv : pattern)
212 event_list.emplace_back(dv.get_datedvalue());
214 event_list.at(initial_size).date_ += loop_delay;
218 std::vector<StochasticDatedValue> get_pattern() const { return pattern; }
221 Profile* ProfileBuilder::from_string(const std::string& name, const std::string& input, double periodicity)
223 LegacyUpdateCb cb(input, periodicity);
224 return new Profile(name,cb,cb.get_repeat_delay());
227 Profile* ProfileBuilder::from_file(const std::string& filename)
229 xbt_assert(not filename.empty(), "Cannot parse a trace from an empty filename");
230 auto f = std::unique_ptr<std::ifstream>(simgrid::xbt::path_ifsopen(filename));
231 xbt_assert(not f->fail(), "Cannot open file '%s' (path=%s)", filename.c_str(),
232 simgrid::xbt::path_to_string().c_str());
234 std::stringstream buffer;
235 buffer << f->rdbuf();
237 LegacyUpdateCb cb(buffer.str(), -1);
238 return new Profile(filename, cb, cb.get_repeat_delay());
242 Profile* ProfileBuilder::from_void() {
243 static auto* void_profile = new Profile("__void__", nullptr, -1.0);
247 Profile* ProfileBuilder::from_callback(const std::string& name, const std::function<UpdateCb>& cb, double repeat_delay) {
248 return new Profile(name, cb, repeat_delay);
251 } // namespace simgrid::kernel::profile
253 std::vector<simgrid::kernel::profile::StochasticDatedValue> trace2selist( const char* c_str) {
254 std::string str(c_str);
255 simgrid::kernel::profile::LegacyUpdateCb cb(str,0);
256 return cb.get_pattern();