1 /* Copyright (c) 2004-2022. 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 "src/surf/surf_interface.hpp"
12 #include <boost/algorithm/string.hpp>
13 #include <boost/intrusive/options.hpp>
17 #include <string_view>
19 namespace simgrid::kernel::profile {
21 bool DatedValue::operator==(DatedValue const& e2) const
23 return (fabs(date_ - e2.date_) < 0.0001) && (fabs(value_ - e2.value_) < 0.0001);
26 std::ostream& operator << (std::ostream& out, const DatedValue& dv) {
27 out<<"(+"<<dv.date_<<','<<dv.value_<<')';
31 /** @brief This callback implements the support for legacy profile syntax
33 * These profiles support two orthogonal concepts:
34 * - One-shot/Loop: the given list of {date,value} pairs can be repeated forever or just once.
35 * - Deterministic/Stochastic: dates and/or values can be drawn according to a stochastic law.
37 * A legacy profile is composed of "global" instructions and "local" instructions.
39 * Global instructions set properties that are true for all {date,value} pairs. They include
40 * - STOCHASTIC -> declare that the profile will use stochastic {date,value} pairs and not loop.
41 * - STOCHASTIC_LOOP -> declare that the profile will use stochastic {date,value} pairs and loop.
42 * - PERIODICITY -> declare the period for each iteration of the profile pattern.
43 * - LOOP_AFTER -> declare that the profile pattern will start over after a fixed delay
45 * Note that PERIODICITY and LOOP_AFTER have slightly different meanings.
46 * PERIODICITY represents the delay between two iterations, for instance the time for the first pair of the pattern in
47 * two successive iterations. Hence, PERIODICITY only has meaning for completely deterministic profiles. LOOP_AFTER
48 * represents an additional delay between the last pair of a profile pattern iteration and the first pair of the next
51 * Local instructions define one {date,value} pair, or more exactly one pattern.
52 * In effect, when a profile loops or has stochastic components, the actual {date,value} pairs will be generated
53 * dynamically. Roughly, a local instruction is a line with the following syntax: <date spec> <value spec>
55 * Both date and value specifications may have one of the following formats:
56 * - <num> : in completely deterministic profiles, use this number
57 * - DET <num> : in stochastic profiles, use this number
58 * - NORM <mean> <standard deviation> : draw number from a normal distribution of given parameters
59 * - UNIF <min> <max> : draw number from an uniform distribution of given parameters
60 * - EXP <lambda> : draw number from an exponential distribution of given parameters
62 * Note that in stochastic profiles, the date component is (necessarily) representing the delay between two pairs;
63 * whereas in deterministic profiles, the date components represent the absolute date at which the elements are used in
64 * the first iteration.
67 class LegacyUpdateCb {
68 std::vector<StochasticDatedValue> pattern;
69 bool stochastic = false;
71 double loop_delay = 0.0;
73 static bool is_comment_or_empty_line(std::string_view val)
75 return (val.empty() || val.front() == '#' || val.front() == '%');
78 static bool is_normal_distribution(std::string_view val)
80 return (val == "NORM" || val == "NORMAL" || val == "GAUSS" || val == "GAUSSIAN");
83 static bool is_exponential_distribution(std::string_view val) { return (val == "EXP" || val == "EXPONENTIAL"); }
85 static bool is_uniform_distribution(std::string_view val) { return (val == "UNIF" || val == "UNIFORM"); }
88 LegacyUpdateCb(const std::string& input, double periodicity) : loop(periodicity > 0)
91 std::vector<std::string> list;
93 boost::split(list, input, boost::is_any_of("\n\r"));
94 for (auto val : list) {
95 simgrid::kernel::profile::StochasticDatedValue stochevent;
98 if (is_comment_or_empty_line(val))
100 if (sscanf(val.c_str(), "PERIODICITY %lg\n", &periodicity) == 1) {
104 if (sscanf(val.c_str(), "LOOPAFTER %lg\n", &loop_delay) == 1) {
108 if (val == "STOCHASTIC LOOP") {
113 if (val == "STOCHASTIC") {
120 std::istringstream iss(val);
121 std::vector<std::string> splittedval((std::istream_iterator<std::string>(iss)),
122 std::istream_iterator<std::string>());
124 xbt_assert(not splittedval.empty(), "Invalid profile line");
126 if (splittedval[0] == "DET") {
127 stochevent.date_law = Distribution::DET;
129 } else if (is_normal_distribution(splittedval[0])) {
130 stochevent.date_law = Distribution::NORM;
132 } else if (is_exponential_distribution(splittedval[0])) {
133 stochevent.date_law = Distribution::EXP;
135 } else if (is_uniform_distribution(splittedval[0])) {
136 stochevent.date_law = Distribution::UNIF;
139 xbt_assert(not stochastic);
140 stochevent.date_law = Distribution::DET;
144 xbt_assert(splittedval.size() > i, "Invalid profile line");
145 if (i == 1 || i == 2) {
146 stochevent.date_params = {std::stod(splittedval[i - 1])};
148 stochevent.date_params = {std::stod(splittedval[1]), std::stod(splittedval[2])};
151 if (splittedval[i] == "DET") {
152 stochevent.value_law = Distribution::DET;
154 } else if (is_normal_distribution(splittedval[i])) {
155 stochevent.value_law = Distribution::NORM;
157 } else if (is_exponential_distribution(splittedval[i])) {
158 stochevent.value_law = Distribution::EXP;
160 } else if (is_uniform_distribution(splittedval[i])) {
161 stochevent.value_law = Distribution::UNIF;
164 xbt_assert(not stochastic);
165 stochevent.value_law = Distribution::DET;
169 xbt_assert(splittedval.size() > i + j, "Invalid profile line");
170 if (j == 0 || j == 1) {
171 stochevent.value_params = {std::stod(splittedval[i + j])};
173 stochevent.value_params = {std::stod(splittedval[i + 1]), std::stod(splittedval[i + 2])};
176 if (not stochastic) {
177 // In this mode, dates read from the string are absolute values
178 double new_date = stochevent.date_params[0];
179 xbt_assert(new_date >= 0, "Profile time value is negative, why?");
180 xbt_assert(last_date <= new_date, "%d: Invalid trace: Events must be sorted, but time %g > time %g.\n%s",
181 linecount, last_date, new_date, input.c_str());
182 stochevent.date_params[0] -= last_date;
183 last_date = new_date;
186 pattern.emplace_back(stochevent);
189 xbt_assert(not stochastic || periodicity <= 0, "If you want periodicity with stochastic profiles, use LOOP_AFTER");
190 if (periodicity > 0) {
191 xbt_assert(loop && loop_delay == 0);
192 loop_delay = periodicity - last_date;
195 xbt_assert(loop_delay >= 0, "Profile loop conditions are not realizable!");
198 double get_repeat_delay() const
200 if (not stochastic && loop)
205 void operator()(std::vector<DatedValue>& event_list) const
207 size_t initial_size = event_list.size();
208 if (loop || not initial_size) {
209 for (auto const& dv : pattern)
210 event_list.emplace_back(dv.get_datedvalue());
212 event_list.at(initial_size).date_ += loop_delay;
216 std::vector<StochasticDatedValue> get_pattern() const { return pattern; }
219 Profile* ProfileBuilder::from_string(const std::string& name, const std::string& input, double periodicity)
221 LegacyUpdateCb cb(input, periodicity);
222 return new Profile(name,cb,cb.get_repeat_delay());
225 Profile* ProfileBuilder::from_file(const std::string& path)
227 xbt_assert(not path.empty(), "Cannot parse a trace from an empty filename");
228 auto f = std::unique_ptr<std::ifstream>(surf_ifsopen(path));
229 xbt_assert(not f->fail(), "Cannot open file '%s' (path=%s)", path.c_str(), (boost::join(surf_path, ":")).c_str());
231 std::stringstream buffer;
232 buffer << f->rdbuf();
234 LegacyUpdateCb cb(buffer.str(), -1);
235 return new Profile(path,cb,cb.get_repeat_delay());
239 Profile* ProfileBuilder::from_void() {
240 static Profile void_profile("__void__", nullptr, -1.0);
241 return &void_profile;
244 Profile* ProfileBuilder::from_callback(const std::string& name, const std::function<UpdateCb>& cb, double repeat_delay) {
245 return new Profile(name, cb, repeat_delay);
248 } // namespace simgrid::kernel::profile
250 std::vector<simgrid::kernel::profile::StochasticDatedValue> trace2selist( const char* c_str) {
251 std::string str(c_str);
252 simgrid::kernel::profile::LegacyUpdateCb cb(str,0);
253 return cb.get_pattern();