Logo AND Algorithmique Numérique Distribuée

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