Logo AND Algorithmique Numérique Distribuée

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