Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'dev-add_comm_fault_scenario' into 'master'
[simgrid.git] / src / kernel / resource / profile / ProfileBuilder.cpp
1 /* Copyright (c) 2004-2022. The SimGrid Team. All rights reserved.          */
2
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. */
5
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
12 #include <boost/algorithm/string.hpp>
13 #include <boost/intrusive/options.hpp>
14 #include <cstddef>
15 #include <fstream>
16 #include <sstream>
17 #include <string_view>
18
19 namespace simgrid::kernel::profile {
20
21 bool DatedValue::operator==(DatedValue const& e2) const
22 {
23   return (fabs(date_ - e2.date_) < 0.0001) && (fabs(value_ - e2.value_) < 0.0001);
24 }
25
26 std::ostream& operator << (std::ostream& out, const DatedValue& dv) {
27   out<<"(+"<<dv.date_<<','<<dv.value_<<')';
28   return out;
29 }
30
31 /** @brief This callback implements the support for legacy profile syntax
32  *
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.
36  *
37  * A legacy profile is composed of "global" instructions and "local" instructions.
38  *
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
44  *
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
49  * iteration.
50  *
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>
54  *
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
61  *
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.
65  */
66
67 class LegacyUpdateCb {
68   std::vector<StochasticDatedValue> pattern;
69   bool stochastic = false;
70   bool loop;
71   double loop_delay = 0.0;
72
73   static bool is_comment_or_empty_line(std::string_view val)
74   {
75     return (val.empty() || val.front() == '#' || val.front() == '%');
76   }
77
78   static bool is_normal_distribution(std::string_view val)
79   {
80     return (val == "NORM" || val == "NORMAL" || val == "GAUSS" || val == "GAUSSIAN");
81   }
82
83   static bool is_exponential_distribution(std::string_view val) { return (val == "EXP" || val == "EXPONENTIAL"); }
84
85   static bool is_uniform_distribution(std::string_view val) { return (val == "UNIF" || val == "UNIFORM"); }
86
87 public:
88   LegacyUpdateCb(const std::string& input, double periodicity) : loop(periodicity > 0)
89   {
90     int linecount = 0;
91     std::vector<std::string> list;
92     double last_date = 0;
93     boost::split(list, input, boost::is_any_of("\n\r"));
94     for (auto val : list) {
95       simgrid::kernel::profile::StochasticDatedValue stochevent;
96       linecount++;
97       boost::trim(val);
98       if (is_comment_or_empty_line(val))
99         continue;
100       if (sscanf(val.c_str(), "PERIODICITY %lg\n", &periodicity) == 1) {
101         loop = true;
102         continue;
103       }
104       if (sscanf(val.c_str(), "LOOPAFTER %lg\n", &loop_delay) == 1) {
105         loop = true;
106         continue;
107       }
108       if (val == "STOCHASTIC LOOP") {
109         stochastic = true;
110         loop       = true;
111         continue;
112       }
113       if (val == "STOCHASTIC") {
114         stochastic = true;
115         continue;
116       }
117
118       unsigned int i;
119       unsigned int j;
120       std::istringstream iss(val);
121       std::vector<std::string> splittedval((std::istream_iterator<std::string>(iss)),
122                                            std::istream_iterator<std::string>());
123
124       xbt_assert(not splittedval.empty(), "Invalid profile line");
125
126       if (splittedval[0] == "DET") {
127         stochevent.date_law = Distribution::DET;
128         i                   = 2;
129       } else if (is_normal_distribution(splittedval[0])) {
130         stochevent.date_law = Distribution::NORM;
131         i                   = 3;
132       } else if (is_exponential_distribution(splittedval[0])) {
133         stochevent.date_law = Distribution::EXP;
134         i                   = 2;
135       } else if (is_uniform_distribution(splittedval[0])) {
136         stochevent.date_law = Distribution::UNIF;
137         i                   = 3;
138       } else {
139         xbt_assert(not stochastic);
140         stochevent.date_law = Distribution::DET;
141         i                   = 1;
142       }
143
144       xbt_assert(splittedval.size() > i, "Invalid profile line");
145       if (i == 1 || i == 2) {
146         stochevent.date_params = {std::stod(splittedval[i - 1])};
147       } else if (i == 3) {
148         stochevent.date_params = {std::stod(splittedval[1]), std::stod(splittedval[2])};
149       }
150
151       if (splittedval[i] == "DET") {
152         stochevent.value_law = Distribution::DET;
153         j                    = 1;
154       } else if (is_normal_distribution(splittedval[i])) {
155         stochevent.value_law = Distribution::NORM;
156         j                    = 2;
157       } else if (is_exponential_distribution(splittedval[i])) {
158         stochevent.value_law = Distribution::EXP;
159         j                    = 1;
160       } else if (is_uniform_distribution(splittedval[i])) {
161         stochevent.value_law = Distribution::UNIF;
162         j                    = 2;
163       } else {
164         xbt_assert(not stochastic);
165         stochevent.value_law = Distribution::DET;
166         j                    = 0;
167       }
168
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])};
172       } else if (j == 2) {
173         stochevent.value_params = {std::stod(splittedval[i + 1]), std::stod(splittedval[i + 2])};
174       }
175
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;
184       }
185
186       pattern.emplace_back(stochevent);
187     }
188
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;
193     }
194
195     xbt_assert(loop_delay >= 0, "Profile loop conditions are not realizable!");
196   }
197
198   double get_repeat_delay() const
199   {
200     if (not stochastic && loop)
201       return loop_delay;
202     return -1.0;
203   }
204
205   void operator()(std::vector<DatedValue>& event_list) const
206   {
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());
211       if (initial_size)
212         event_list.at(initial_size).date_ += loop_delay;
213     }
214   }
215
216   std::vector<StochasticDatedValue> get_pattern() const { return pattern; }
217 };
218
219 Profile* ProfileBuilder::from_string(const std::string& name, const std::string& input, double periodicity)
220 {
221   LegacyUpdateCb cb(input, periodicity);
222   return new Profile(name,cb,cb.get_repeat_delay());
223 }
224
225 Profile* ProfileBuilder::from_file(const std::string& path)
226 {
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());
230
231   std::stringstream buffer;
232   buffer << f->rdbuf();
233
234   LegacyUpdateCb cb(buffer.str(), -1);
235   return new Profile(path,cb,cb.get_repeat_delay());
236 }
237
238
239 Profile* ProfileBuilder::from_void() {
240   static Profile void_profile("__void__", nullptr, -1.0);
241   return &void_profile;
242 }
243
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);
246 }
247
248 } // namespace simgrid::kernel::profile
249
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();
254 }
255
256