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. */
7 #include "xbt/sysdep.h"
9 #include "src/kernel/resource/profile/trace_mgr.hpp"
10 #include "src/surf/surf_interface.hpp"
11 #include <boost/algorithm/string.hpp>
12 #include <boost/algorithm/string/join.hpp>
13 #include <boost/algorithm/string/split.hpp>
17 #include <unordered_map>
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(profile, resource, "Surf profile management");
21 static std::unordered_map<std::string, simgrid::kernel::profile::Profile*> trace_list;
27 bool DatedValue::operator==(DatedValue const& e2) const
29 return (fabs(date_ - e2.date_) < 0.0001) && (fabs(value_ - e2.value_) < 0.0001);
31 std::ostream& operator<<(std::ostream& out, const DatedValue& e)
33 out << e.date_ << " " << e.value_;
39 /* Add the first fake event storing the time at which the trace begins */
40 DatedValue val(0, -1);
41 event_list.push_back(val);
43 Profile::~Profile() = default;
44 Profile* Profile::from_string(std::string name, std::string input, double periodicity)
47 simgrid::kernel::profile::Profile* profile = new simgrid::kernel::profile::Profile();
48 simgrid::kernel::profile::DatedValue* last_event = &(profile->event_list.back());
50 xbt_assert(trace_list.find(name) == trace_list.end(), "Refusing to define trace %s twice", name.c_str());
52 std::vector<std::string> list;
53 boost::split(list, input, boost::is_any_of("\n\r"));
54 for (auto val : list) {
55 simgrid::kernel::profile::DatedValue event;
58 if (val[0] == '#' || val[0] == '\0' || val[0] == '%') // pass comments
60 if (sscanf(val.c_str(), "PERIODICITY %lg\n", &periodicity) == 1)
62 if (sscanf(val.c_str(), "LOOPAFTER %lg\n", &periodicity) == 1)
65 xbt_assert(sscanf(val.c_str(), "%lg %lg\n", &event.date_, &event.value_) == 2, "%s:%d: Syntax error in trace\n%s",
66 name.c_str(), linecount, input.c_str());
68 xbt_assert(last_event->date_ <= event.date_,
69 "%s:%d: Invalid trace: Events must be sorted, but time %g > time %g.\n%s", name.c_str(), linecount,
70 last_event->date_, event.date_, input.c_str());
71 last_event->date_ = event.date_ - last_event->date_;
73 profile->event_list.push_back(event);
74 last_event = &(profile->event_list.back());
77 if (periodicity > 0) {
78 last_event->date_ = periodicity + profile->event_list.at(0).date_;
80 last_event->date_ = -1;
84 trace_list.insert({name, profile});
88 Profile* Profile::from_file(std::string path)
90 xbt_assert(not path.empty(), "Cannot parse a trace from an empty filename");
91 xbt_assert(trace_list.find(path) == trace_list.end(), "Refusing to define trace %s twice", path.c_str());
93 std::ifstream* f = surf_ifsopen(path);
94 xbt_assert(not f->fail(), "Cannot open file '%s' (path=%s)", path.c_str(), (boost::join(surf_path, ":")).c_str());
96 std::stringstream buffer;
100 return Profile::from_string(path, buffer.str(), -1);
102 FutureEvtSet::FutureEvtSet() = default;
103 FutureEvtSet::~FutureEvtSet()
105 while (not heap_.empty()) {
106 delete heap_.top().second;
111 /** @brief Registers a new trace into the future event set, and get an iterator over the integrated trace */
112 Event* FutureEvtSet::add_trace(Profile* profile, resource::Resource* resource)
114 Event* event = new Event();
115 event->profile = profile;
117 event->resource = resource;
118 event->free_me = false;
120 xbt_assert((event->idx < profile->event_list.size()), "Your profile should have at least one event!");
122 heap_.emplace(0.0 /* start time */, event);
127 /** @brief returns the date of the next occurring event */
128 double FutureEvtSet::next_date() const
130 return heap_.empty() ? -1.0 : heap_.top().first;
133 /** @brief Retrieves the next occurring event, or nullptr if none happens before date */
134 Event* FutureEvtSet::pop_leq(double date, double* value, resource::Resource** resource)
136 double event_date = next_date();
137 if (event_date > date)
142 Event* event = heap_.top().second;
145 Profile* profile = event->profile;
146 *resource = event->resource;
148 DatedValue dateVal = profile->event_list.at(event->idx);
150 *value = dateVal.value_;
152 if (event->idx < profile->event_list.size() - 1) {
153 heap_.emplace(event_date + dateVal.date_, event);
155 } else if (dateVal.date_ > 0) { /* Last element. Shall we loop? */
156 heap_.emplace(event_date + dateVal.date_, event);
157 event->idx = 1; /* idx=0 is a placeholder to store when events really start */
158 } else { /* If we don't loop, we don't need this event anymore */
159 event->free_me = true;
164 } // namespace profile
165 } // namespace kernel
166 } // namespace simgrid
169 for (auto const& kv : trace_list)
174 void tmgr_trace_event_unref(simgrid::kernel::profile::Event** event)
176 if ((*event)->free_me) {