Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'Adrien.Gougeon/simgrid-master'
[simgrid.git] / src / kernel / resource / profile / Profile.cpp
1 /* Copyright (c) 2004-2020. 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 "src/kernel/resource/profile/Profile.hpp"
7 #include "simgrid/forward.h"
8 #include "src/kernel/resource/profile/DatedValue.hpp"
9 #include "src/kernel/resource/profile/Event.hpp"
10 #include "src/kernel/resource/profile/FutureEvtSet.hpp"
11 #include "src/kernel/resource/profile/StochasticDatedValue.hpp"
12 #include "src/surf/surf_interface.hpp"
13
14 #include <boost/algorithm/string.hpp>
15 #include <fstream>
16 #include <memory>
17 #include <ostream>
18 #include <sstream>
19 #include <unordered_map>
20 #include <vector>
21
22 static std::unordered_map<std::string, simgrid::kernel::profile::Profile*> trace_list;
23
24 namespace simgrid {
25 namespace kernel {
26 namespace profile {
27
28 Profile::Profile()
29 {
30   /* Add the first fake event storing the time at which the trace begins */
31   DatedValue val(0, -1);
32   StochasticDatedValue stoval(0, -1);
33   event_list.emplace_back(val);
34   stochastic_event_list.emplace_back(stoval);
35 }
36 Profile::~Profile() = default;
37
38 /** @brief Register this profile for that resource onto that FES,
39  * and get an iterator over the integrated trace  */
40 Event* Profile::schedule(FutureEvtSet* fes, resource::Resource* resource)
41 {
42   auto* event     = new Event();
43   event->profile  = this;
44   event->idx      = 0;
45   event->resource = resource;
46   event->free_me  = false;
47
48   xbt_assert((event->idx < event_list.size()), "Your profile should have at least one event!");
49
50   fes_ = fes;
51   fes_->add_event(0.0 /* start time */, event);
52   if (stochastic) {
53     xbt_assert(event->idx < stochastic_event_list.size(), "Your profile should have at least one stochastic event!");
54     futureDV = stochastic_event_list.at(event->idx).get_datedvalue();
55   }
56
57   return event;
58 }
59
60 /** @brief Gets the next event from a profile */
61 DatedValue Profile::next(Event* event)
62 {
63   double event_date  = fes_->next_date();
64
65   if (not stochastic) {
66     DatedValue dateVal = event_list.at(event->idx);
67
68     if (event->idx < event_list.size() - 1) {
69       fes_->add_event(event_date + dateVal.date_, event);
70       event->idx++;
71     } else if (dateVal.date_ > 0) { /* Last element. Shall we loop? */
72       fes_->add_event(event_date + dateVal.date_, event);
73       event->idx = 1; /* idx=0 is a placeholder to store when events really start */
74     } else {          /* If we don't loop, we don't need this event anymore */
75       event->free_me = true;
76     }
77     return dateVal;
78   } else {
79     DatedValue dateVal = futureDV;
80     if (event->idx < stochastic_event_list.size() - 1) {
81       event->idx++;
82     } else if (stochasticloop) { /* We have reached the last element and we have to loop. */
83       event->idx = 1;
84     } else {
85       event->free_me = true; /* We have reached the last element, but we don't need to loop. */
86     }
87
88     if (not event->free_me) { // In the case there is an element, we draw the next event
89       futureDV = stochastic_event_list.at(event->idx).get_datedvalue();
90       fes_->add_event(event_date + futureDV.date_, event);
91     }
92     return dateVal;
93   }
94 }
95
96 Profile* Profile::from_string(const std::string& name, const std::string& input, double periodicity)
97 {
98   int linecount                                    = 0;
99   auto* profile                                    = new simgrid::kernel::profile::Profile();
100   simgrid::kernel::profile::DatedValue* last_event = &(profile->event_list.back());
101
102   xbt_assert(trace_list.find(name) == trace_list.end(), "Refusing to define trace %s twice", name.c_str());
103
104   std::vector<std::string> list;
105   boost::split(list, input, boost::is_any_of("\n\r"));
106   for (auto val : list) {
107     simgrid::kernel::profile::DatedValue event;
108     simgrid::kernel::profile::StochasticDatedValue stochevent;
109     linecount++;
110     boost::trim(val);
111     if (val[0] == '#' || val[0] == '\0' || val[0] == '%') // pass comments
112       continue;
113     if (sscanf(val.c_str(), "PERIODICITY %lg\n", &periodicity) == 1)
114       continue;
115     if (sscanf(val.c_str(), "LOOPAFTER %lg\n", &periodicity) == 1)
116       continue;
117     if (val == "STOCHASTIC LOOP") {
118       profile->stochastic     = true;
119       profile->stochasticloop = true;
120       continue;
121     }
122     if (val == "STOCHASTIC") {
123       profile->stochastic = true;
124       continue;
125     }
126
127     if (profile->stochastic) {
128       unsigned int i;
129       unsigned int j;
130       std::istringstream iss(val);
131       std::vector<std::string> splittedval((std::istream_iterator<std::string>(iss)),
132                                            std::istream_iterator<std::string>());
133
134       xbt_assert(splittedval.size() > 0, "Invalid profile line");
135
136       if (splittedval[0] == "DET") {
137         stochevent.date_law = Distribution::DET;
138         i                   = 2;
139       } else if (splittedval[0] == "NORM" || splittedval[0] == "NORMAL" || splittedval[0] == "GAUSS" ||
140                  splittedval[0] == "GAUSSIAN") {
141         stochevent.date_law = Distribution::NORM;
142         i                   = 3;
143       } else if (splittedval[0] == "EXP" || splittedval[0] == "EXPONENTIAL") {
144         stochevent.date_law = Distribution::EXP;
145         i                   = 2;
146       } else if (splittedval[0] == "UNIF" || splittedval[0] == "UNIFORM") {
147         stochevent.date_law = Distribution::UNIF;
148         i                   = 3;
149       } else {
150         xbt_die("Unknown law %s", splittedval[0].c_str());
151       }
152
153       xbt_assert(splittedval.size() > i, "Invalid profile line");
154       if (i == 2) {
155         stochevent.date_params = {std::stod(splittedval[1])};
156       } else if (i == 3) {
157         stochevent.date_params = {std::stod(splittedval[1]), std::stod(splittedval[2])};
158       }
159
160       if (splittedval[i] == "DET") {
161         stochevent.value_law = Distribution::DET;
162         j                    = 1;
163       } else if (splittedval[i] == "NORM" || splittedval[i] == "NORMAL" || splittedval[i] == "GAUSS" ||
164                  splittedval[i] == "GAUSSIAN") {
165         stochevent.value_law = Distribution::NORM;
166         j                    = 2;
167       } else if (splittedval[i] == "EXP" || splittedval[i] == "EXPONENTIAL") {
168         stochevent.value_law = Distribution::EXP;
169         j                    = 1;
170       } else if (splittedval[i] == "UNIF" || splittedval[i] == "UNIFORM") {
171         stochevent.value_law = Distribution::UNIF;
172         j                    = 2;
173       } else {
174         xbt_die("Unknown law %s", splittedval[i].c_str());
175       }
176
177       xbt_assert(splittedval.size() > i + j, "Invalid profile line");
178       if (j == 1) {
179         stochevent.value_params = {std::stod(splittedval[i + 1])};
180       } else if (j == 2) {
181         stochevent.value_params = {std::stod(splittedval[i + 1]), std::stod(splittedval[i + 2])};
182       }
183
184       profile->stochastic_event_list.emplace_back(stochevent);
185     } else {
186       XBT_ATTRIB_UNUSED int res = sscanf(val.c_str(), "%lg  %lg\n", &event.date_, &event.value_);
187       xbt_assert(res == 2, "%s:%d: Syntax error in trace\n%s", name.c_str(), linecount, input.c_str());
188
189       xbt_assert(last_event->date_ <= event.date_,
190                  "%s:%d: Invalid trace: Events must be sorted, but time %g > time %g.\n%s", name.c_str(), linecount,
191                  last_event->date_, event.date_, input.c_str());
192       last_event->date_ = event.date_ - last_event->date_;
193
194       profile->event_list.emplace_back(event);
195       last_event = &(profile->event_list.back());
196     }
197   }
198   if (last_event) {
199     if (periodicity > 0) {
200       last_event->date_ = periodicity + profile->event_list.at(0).date_;
201     } else {
202       last_event->date_ = -1;
203     }
204   }
205
206   trace_list.insert({name, profile});
207
208   return profile;
209 }
210 Profile* Profile::from_file(const std::string& path)
211 {
212   xbt_assert(not path.empty(), "Cannot parse a trace from an empty filename");
213   xbt_assert(trace_list.find(path) == trace_list.end(), "Refusing to define trace %s twice", path.c_str());
214
215   auto f = std::unique_ptr<std::ifstream>(surf_ifsopen(path));
216   xbt_assert(not f->fail(), "Cannot open file '%s' (path=%s)", path.c_str(), (boost::join(surf_path, ":")).c_str());
217
218   std::stringstream buffer;
219   buffer << f->rdbuf();
220
221   return Profile::from_string(path, buffer.str(), -1);
222 }
223
224 } // namespace profile
225 } // namespace kernel
226 } // namespace simgrid
227
228 void tmgr_finalize()
229 {
230   for (auto const& kv : trace_list)
231     delete kv.second;
232   trace_list.clear();
233 }
234
235 void tmgr_trace_event_unref(simgrid::kernel::profile::Event** event)
236 {
237   if ((*event)->free_me) {
238     delete *event;
239     *event = nullptr;
240   }
241 }