1 /* Copyright (c) 2004-2020. The SimGrid Team. All rights reserved. */
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. */
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"
14 #include <boost/algorithm/string.hpp>
19 #include <unordered_map>
22 static std::unordered_map<std::string, simgrid::kernel::profile::Profile*> trace_list;
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);
34 Profile::~Profile() = default;
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)
40 auto* event = new Event();
41 event->profile = this;
43 event->resource = resource;
44 event->free_me = false;
46 xbt_assert((event->idx < event_list.size()), "Your profile should have at least one event!");
49 fes_->add_event(0.0 /* start time */, event);
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();
58 /** @brief Gets the next event from a profile */
59 DatedValue Profile::next(Event* event)
61 double event_date = fes_->next_date();
64 DatedValue dateVal = event_list.at(event->idx);
66 if (event->idx < event_list.size() - 1) {
67 fes_->add_event(event_date + dateVal.date_, event);
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;
77 DatedValue dateVal = futureDV;
78 if (event->idx < stochastic_event_list.size() - 1) {
80 } else if (stochasticloop) { /* We have reached the last element and we have to loop. */
83 event->free_me = true; /* We have reached the last element, but we don't need to loop. */
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);
94 Profile* Profile::from_string(const std::string& name, const std::string& input, double periodicity)
97 auto* profile = new simgrid::kernel::profile::Profile();
98 simgrid::kernel::profile::DatedValue* last_event = &(profile->event_list.back());
100 xbt_assert(trace_list.find(name) == trace_list.end(), "Refusing to define trace %s twice", name.c_str());
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;
109 if (val[0] == '#' || val[0] == '\0' || val[0] == '%') // pass comments
111 if (sscanf(val.c_str(), "PERIODICITY %lg\n", &periodicity) == 1)
113 if (sscanf(val.c_str(), "LOOPAFTER %lg\n", &periodicity) == 1)
115 if (val == "STOCHASTIC LOOP") {
116 profile->stochastic = true;
117 profile->stochasticloop = true;
120 if (val == "STOCHASTIC") {
121 profile->stochastic = true;
125 if (profile->stochastic) {
128 std::istringstream iss(val);
129 std::vector<std::string> splittedval((std::istream_iterator<std::string>(iss)),
130 std::istream_iterator<std::string>());
132 xbt_assert(splittedval.size() > 0, "Invalid profile line");
134 if (splittedval[0] == "DET") {
135 stochevent.date_law = Distribution::DET;
137 } else if (splittedval[0] == "NORM" || splittedval[0] == "NORMAL" || splittedval[0] == "GAUSS" ||
138 splittedval[0] == "GAUSSIAN") {
139 stochevent.date_law = Distribution::NORM;
141 } else if (splittedval[0] == "EXP" || splittedval[0] == "EXPONENTIAL") {
142 stochevent.date_law = Distribution::EXP;
144 } else if (splittedval[0] == "UNIF" || splittedval[0] == "UNIFORM") {
145 stochevent.date_law = Distribution::UNIF;
148 xbt_die("Unknown law %s", splittedval[0].c_str());
151 xbt_assert(splittedval.size() > i, "Invalid profile line");
153 stochevent.date_params = {std::stod(splittedval[1])};
155 stochevent.date_params = {std::stod(splittedval[1]), std::stod(splittedval[2])};
158 if (splittedval[i] == "DET") {
159 stochevent.value_law = Distribution::DET;
161 } else if (splittedval[i] == "NORM" || splittedval[i] == "NORMAL" || splittedval[i] == "GAUSS" ||
162 splittedval[i] == "GAUSSIAN") {
163 stochevent.value_law = Distribution::NORM;
165 } else if (splittedval[i] == "EXP" || splittedval[i] == "EXPONENTIAL") {
166 stochevent.value_law = Distribution::EXP;
168 } else if (splittedval[i] == "UNIF" || splittedval[i] == "UNIFORM") {
169 stochevent.value_law = Distribution::UNIF;
172 xbt_die("Unknown law %s", splittedval[i].c_str());
175 xbt_assert(splittedval.size() > i + j, "Invalid profile line");
177 stochevent.value_params = {std::stod(splittedval[i + 1])};
179 stochevent.value_params = {std::stod(splittedval[i + 1]), std::stod(splittedval[i + 2])};
182 profile->stochastic_event_list.emplace_back(stochevent);
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());
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_;
192 profile->event_list.emplace_back(event);
193 last_event = &(profile->event_list.back());
197 if (periodicity > 0) {
198 last_event->date_ = periodicity + profile->event_list.at(0).date_;
200 last_event->date_ = -1;
204 trace_list.insert({name, profile});
208 Profile* Profile::from_file(const std::string& path)
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());
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());
216 std::stringstream buffer;
217 buffer << f->rdbuf();
219 return Profile::from_string(path, buffer.str(), -1);
222 } // namespace profile
223 } // namespace kernel
224 } // namespace simgrid
228 for (auto const& kv : trace_list)
233 void tmgr_trace_event_unref(simgrid::kernel::profile::Event** event)
235 if ((*event)->free_me) {