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 "src/surf/surf_interface.hpp"
11 #include "xbt/file.hpp"
13 #include <boost/algorithm/string.hpp>
14 #include <boost/intrusive/options.hpp>
18 #include <string_view>
20 namespace simgrid::kernel::profile {
22 bool DatedValue::operator==(DatedValue const& e2) const
24 return (fabs(date_ - e2.date_) < 0.0001) && (fabs(value_ - e2.value_) < 0.0001);
27 std::ostream& operator << (std::ostream& out, const DatedValue& dv) {
28 out<<"(+"<<dv.date_<<','<<dv.value_<<')';
32 /** @brief This callback implements the support for legacy profile syntax
34 * These profiles support two orthogonal concepts:
35 * - One-shot/Loop: the given list of {date,value} pairs can be repeated forever or just once.
36 * - Deterministic/Stochastic: dates and/or values can be drawn according to a stochastic law.
38 * A legacy profile is composed of "global" instructions and "local" instructions.
40 * Global instructions set properties that are true for all {date,value} pairs. They include
41 * - STOCHASTIC -> declare that the profile will use stochastic {date,value} pairs and not loop.
42 * - STOCHASTIC_LOOP -> declare that the profile will use stochastic {date,value} pairs and loop.
43 * - PERIODICITY -> declare the period for each iteration of the profile pattern.
44 * - LOOP_AFTER -> declare that the profile pattern will start over after a fixed delay
46 * Note that PERIODICITY and LOOP_AFTER have slightly different meanings.
47 * PERIODICITY represents the delay between two iterations, for instance the time for the first pair of the pattern in
48 * two successive iterations. Hence, PERIODICITY only has meaning for completely deterministic profiles. LOOP_AFTER
49 * represents an additional delay between the last pair of a profile pattern iteration and the first pair of the next
52 * Local instructions define one {date,value} pair, or more exactly one pattern.
53 * In effect, when a profile loops or has stochastic components, the actual {date,value} pairs will be generated
54 * dynamically. Roughly, a local instruction is a line with the following syntax: <date spec> <value spec>
56 * Both date and value specifications may have one of the following formats:
57 * - <num> : in completely deterministic profiles, use this number
58 * - DET <num> : in stochastic profiles, use this number
59 * - NORM <mean> <standard deviation> : draw number from a normal distribution of given parameters
60 * - UNIF <min> <max> : draw number from an uniform distribution of given parameters
61 * - EXP <lambda> : draw number from an exponential distribution of given parameters
63 * Note that in stochastic profiles, the date component is (necessarily) representing the delay between two pairs;
64 * whereas in deterministic profiles, the date components represent the absolute date at which the elements are used in
65 * the first iteration.
68 class LegacyUpdateCb {
69 std::vector<StochasticDatedValue> pattern;
70 bool stochastic = false;
72 double loop_delay = 0.0;
74 static bool is_comment_or_empty_line(std::string_view val)
76 return (val.empty() || val.front() == '#' || val.front() == '%');
79 static bool is_normal_distribution(std::string_view val)
81 return (val == "NORM" || val == "NORMAL" || val == "GAUSS" || val == "GAUSSIAN");
84 static bool is_exponential_distribution(std::string_view val) { return (val == "EXP" || val == "EXPONENTIAL"); }
86 static bool is_uniform_distribution(std::string_view val) { return (val == "UNIF" || val == "UNIFORM"); }
89 LegacyUpdateCb(const std::string& input, double periodicity) : loop(periodicity > 0)
92 std::vector<std::string> list;
94 boost::split(list, input, boost::is_any_of("\n\r"));
95 for (auto val : list) {
96 simgrid::kernel::profile::StochasticDatedValue stochevent;
99 if (is_comment_or_empty_line(val))
101 if (sscanf(val.c_str(), "PERIODICITY %lg\n", &periodicity) == 1) {
105 if (sscanf(val.c_str(), "LOOPAFTER %lg\n", &loop_delay) == 1) {
109 if (val == "STOCHASTIC LOOP") {
114 if (val == "STOCHASTIC") {
121 std::istringstream iss(val);
122 std::vector<std::string> splittedval((std::istream_iterator<std::string>(iss)),
123 std::istream_iterator<std::string>());
125 xbt_assert(not splittedval.empty(), "Invalid profile line");
127 if (splittedval[0] == "DET") {
128 stochevent.date_law = Distribution::DET;
130 } else if (is_normal_distribution(splittedval[0])) {
131 stochevent.date_law = Distribution::NORM;
133 } else if (is_exponential_distribution(splittedval[0])) {
134 stochevent.date_law = Distribution::EXP;
136 } else if (is_uniform_distribution(splittedval[0])) {
137 stochevent.date_law = Distribution::UNIF;
140 xbt_assert(not stochastic);
141 stochevent.date_law = Distribution::DET;
145 xbt_assert(splittedval.size() > i, "Invalid profile line");
146 if (i == 1 || i == 2) {
147 stochevent.date_params = {std::stod(splittedval[i - 1])};
149 stochevent.date_params = {std::stod(splittedval[1]), std::stod(splittedval[2])};
152 if (splittedval[i] == "DET") {
153 stochevent.value_law = Distribution::DET;
155 } else if (is_normal_distribution(splittedval[i])) {
156 stochevent.value_law = Distribution::NORM;
158 } else if (is_exponential_distribution(splittedval[i])) {
159 stochevent.value_law = Distribution::EXP;
161 } else if (is_uniform_distribution(splittedval[i])) {
162 stochevent.value_law = Distribution::UNIF;
165 xbt_assert(not stochastic);
166 stochevent.value_law = Distribution::DET;
170 xbt_assert(splittedval.size() > i + j, "Invalid profile line");
171 if (j == 0 || j == 1) {
172 stochevent.value_params = {std::stod(splittedval[i + j])};
174 stochevent.value_params = {std::stod(splittedval[i + 1]), std::stod(splittedval[i + 2])};
177 if (not stochastic) {
178 // In this mode, dates read from the string are absolute values
179 double new_date = stochevent.date_params[0];
180 xbt_assert(new_date >= 0, "Profile time value is negative, why?");
181 xbt_assert(last_date <= new_date, "%d: Invalid trace: Events must be sorted, but time %g > time %g.\n%s",
182 linecount, last_date, new_date, input.c_str());
183 stochevent.date_params[0] -= last_date;
184 last_date = new_date;
187 pattern.emplace_back(stochevent);
190 xbt_assert(not stochastic || periodicity <= 0, "If you want periodicity with stochastic profiles, use LOOP_AFTER");
191 if (periodicity > 0) {
192 xbt_assert(loop && loop_delay == 0);
193 loop_delay = periodicity - last_date;
196 xbt_assert(loop_delay >= 0, "Profile loop conditions are not realizable!");
199 double get_repeat_delay() const
201 if (not stochastic && loop)
206 void operator()(std::vector<DatedValue>& event_list) const
208 size_t initial_size = event_list.size();
209 if (loop || not initial_size) {
210 for (auto const& dv : pattern)
211 event_list.emplace_back(dv.get_datedvalue());
213 event_list.at(initial_size).date_ += loop_delay;
217 std::vector<StochasticDatedValue> get_pattern() const { return pattern; }
220 Profile* ProfileBuilder::from_string(const std::string& name, const std::string& input, double periodicity)
222 LegacyUpdateCb cb(input, periodicity);
223 return new Profile(name,cb,cb.get_repeat_delay());
226 Profile* ProfileBuilder::from_file(const std::string& filename)
228 xbt_assert(not filename.empty(), "Cannot parse a trace from an empty filename");
229 auto f = std::unique_ptr<std::ifstream>(simgrid::xbt::path_ifsopen(filename));
230 xbt_assert(not f->fail(), "Cannot open file '%s' (path=%s)", filename.c_str(),
231 simgrid::xbt::path_to_string().c_str());
233 std::stringstream buffer;
234 buffer << f->rdbuf();
236 LegacyUpdateCb cb(buffer.str(), -1);
237 return new Profile(filename, cb, cb.get_repeat_delay());
241 Profile* ProfileBuilder::from_void() {
242 static auto* void_profile = new Profile("__void__", nullptr, -1.0);
246 Profile* ProfileBuilder::from_callback(const std::string& name, const std::function<UpdateCb>& cb, double repeat_delay) {
247 return new Profile(name, cb, repeat_delay);
250 } // namespace simgrid::kernel::profile
252 std::vector<simgrid::kernel::profile::StochasticDatedValue> trace2selist( const char* c_str) {
253 std::string str(c_str);
254 simgrid::kernel::profile::LegacyUpdateCb cb(str,0);
255 return cb.get_pattern();