Logo AND Algorithmique Numérique Distribuée

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