1 /* Copyright (c) 2004-2019. 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>
18 #include <unordered_map>
21 static std::unordered_map<std::string, simgrid::kernel::profile::Profile*> trace_list;
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.push_back(val);
33 stochastic_event_list.push_back(stoval);
35 Profile::~Profile() = default;
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)
41 Event* event = new Event();
42 event->profile = this;
44 event->resource = resource;
45 event->free_me = false;
47 xbt_assert((event->idx < event_list.size()), "Your profile should have at least one event!");
50 fes_->add_event(0.0 /* start time */, event);
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();
59 /** @brief Gets the next event from a profile */
60 DatedValue Profile::next(Event* event)
62 double event_date = fes_->next_date();
65 DatedValue dateVal = event_list.at(event->idx);
67 if (event->idx < event_list.size() - 1) {
68 fes_->add_event(event_date + dateVal.date_, event);
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;
78 DatedValue dateVal = futureDV;
79 if (event->idx < stochastic_event_list.size() - 1) {
81 } else if (stochasticloop > 0) { /* We have reached the last element and we have to loop. */
84 event->free_me = true; /* We have reached the last element, but we don't need to loop. */
87 if (event->free_me == false) { // In the case there is an element, we draw the next event
88 StochasticDatedValue stodateVal = stochastic_event_list.at(event->idx);
89 futureDV = stochastic_event_list.at(event->idx).get_datedvalue();
90 fes_->add_event(event_date + futureDV.date_, event);
96 Profile* Profile::from_string(const std::string& name, const std::string& input, double periodicity)
99 simgrid::kernel::profile::Profile* profile = new simgrid::kernel::profile::Profile();
100 simgrid::kernel::profile::DatedValue* last_event = &(profile->event_list.back());
102 xbt_assert(trace_list.find(name) == trace_list.end(), "Refusing to define trace %s twice", name.c_str());
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;
111 if (val[0] == '#' || val[0] == '\0' || val[0] == '%') // pass comments
113 if (sscanf(val.c_str(), "PERIODICITY %lg\n", &periodicity) == 1)
115 if (sscanf(val.c_str(), "LOOPAFTER %lg\n", &periodicity) == 1)
117 if (val == "STOCHASTIC LOOP") {
118 profile->stochastic = true;
119 profile->stochasticloop = true;
122 if (val == "STOCHASTIC") {
123 profile->stochastic = true;
127 if (profile->stochastic) {
130 std::istringstream iss(val);
131 std::vector<std::string> splittedval((std::istream_iterator<std::string>(iss)),
132 std::istream_iterator<std::string>());
134 xbt_assert(splittedval.size() > 0, "Invalid profile line");
136 if (splittedval[0] == "DET") {
137 stochevent.date_law = Dist_Det;
139 } else if (splittedval[0] == "NORM" || splittedval[0] == "NORMAL" || splittedval[0] == "GAUSS" ||
140 splittedval[0] == "GAUSSIAN") {
141 stochevent.date_law = Dist_Norm;
143 } else if (splittedval[0] == "EXP" || splittedval[0] == "EXPONENTIAL") {
144 stochevent.date_law = Dist_Exp;
146 } else if (splittedval[0] == "UNIF" || splittedval[0] == "UNIFORM") {
147 stochevent.date_law = Dist_Unif;
150 xbt_assert(false, "Unknown law %s", splittedval[0].c_str());
154 xbt_assert(splittedval.size() > i, "Invalid profile line");
156 stochevent.date_params = {std::atof(splittedval[1].c_str())};
158 stochevent.date_params = {std::atof(splittedval[1].c_str()), std::atof(splittedval[2].c_str())};
161 if (splittedval[i] == "DET") {
162 stochevent.value_law = Dist_Det;
164 } else if (splittedval[i] == "NORM" || splittedval[i] == "NORMAL" || splittedval[i] == "GAUSS" ||
165 splittedval[i] == "GAUSSIAN") {
166 stochevent.value_law = Dist_Norm;
168 } else if (splittedval[i] == "EXP" || splittedval[i] == "EXPONENTIAL") {
169 stochevent.value_law = Dist_Exp;
171 } else if (splittedval[i] == "UNIF" || splittedval[i] == "UNIFORM") {
172 stochevent.value_law = Dist_Unif;
175 xbt_assert(false, "Unknown law %s", splittedval[i].c_str());
179 xbt_assert(splittedval.size() > i + j, "Invalid profile line");
181 stochevent.value_params = {std::atof(splittedval[i + 1].c_str())};
183 stochevent.value_params = {std::atof(splittedval[i + 1].c_str()), std::atof(splittedval[i + 2].c_str())};
186 profile->stochastic_event_list.push_back(stochevent);
188 XBT_ATTRIB_UNUSED int res = sscanf(val.c_str(), "%lg %lg\n", &event.date_, &event.value_);
189 xbt_assert(res == 2, "%s:%d: Syntax error in trace\n%s", name.c_str(), linecount, input.c_str());
191 xbt_assert(last_event->date_ <= event.date_,
192 "%s:%d: Invalid trace: Events must be sorted, but time %g > time %g.\n%s", name.c_str(), linecount,
193 last_event->date_, event.date_, input.c_str());
194 last_event->date_ = event.date_ - last_event->date_;
196 profile->event_list.push_back(event);
197 last_event = &(profile->event_list.back());
201 if (periodicity > 0) {
202 last_event->date_ = periodicity + profile->event_list.at(0).date_;
204 last_event->date_ = -1;
208 trace_list.insert({name, profile});
212 Profile* Profile::from_file(const std::string& path)
214 xbt_assert(not path.empty(), "Cannot parse a trace from an empty filename");
215 xbt_assert(trace_list.find(path) == trace_list.end(), "Refusing to define trace %s twice", path.c_str());
217 std::ifstream* f = surf_ifsopen(path);
218 xbt_assert(not f->fail(), "Cannot open file '%s' (path=%s)", path.c_str(), (boost::join(surf_path, ":")).c_str());
220 std::stringstream buffer;
221 buffer << f->rdbuf();
224 return Profile::from_string(path, buffer.str(), -1);
227 } // namespace profile
228 } // namespace kernel
229 } // namespace simgrid
233 for (auto const& kv : trace_list)
238 void tmgr_trace_event_unref(simgrid::kernel::profile::Event** event)
240 if ((*event)->free_me) {