1 /* Copyright (c) 2004-2005, 2007, 2009-2014. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "xbt/sysdep.h"
12 #include "src/surf/surf_interface.hpp"
13 #include "src/surf/trace_mgr.hpp"
14 #include "surf_private.h"
15 #include "xbt/RngStream.h"
16 #include <boost/algorithm/string.hpp>
17 #include <boost/algorithm/string/join.hpp>
18 #include <boost/algorithm/string/split.hpp>
22 #include <unordered_map>
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_trace, surf, "Surf trace management");
26 namespace tmgr = simgrid::trace_mgr;
28 static std::unordered_map<const char*, tmgr::trace*> trace_list;
30 static inline bool doubleEq(double d1, double d2)
32 return fabs(d1 - d2) < 0.0001;
37 bool DatedValue::operator==(DatedValue e2)
39 return (doubleEq(date_, e2.date_)) && (doubleEq(value_, e2.value_));
41 std::ostream& operator<<(std::ostream& out, const DatedValue& e)
43 out << e.date_ << " " << e.value_;
47 trace::trace() = default;
48 trace::~trace() = default;
49 future_evt_set::future_evt_set() = default;
50 simgrid::trace_mgr::future_evt_set::~future_evt_set()
52 xbt_heap_free(p_heap);
57 tmgr_trace_t tmgr_trace_new_from_string(const char* name, std::string input, double periodicity)
60 tmgr::DatedValue* last_event;
62 xbt_assert(trace_list.find(name) == trace_list.end(), "Refusing to define trace %s twice", name);
64 tmgr_trace_t trace = new simgrid::trace_mgr::trace();
66 std::vector<std::string> list;
67 boost::split(list, input, boost::is_any_of("\n\r"));
68 for (auto val : list) {
69 tmgr::DatedValue event;
72 if (val[0] == '#' || val[0] == '\0' || val[0] == '%') // pass comments
74 if (sscanf(val.c_str(), "PERIODICITY %lg\n", &periodicity) == 1)
76 if (sscanf(val.c_str(), "WAITFOR %lg\n", &periodicity) == 1)
79 xbt_assert(sscanf(val.c_str(), "%lg %lg\n", &event.date_, &event.value_) == 2, "%s:%d: Syntax error in trace\n%s",
80 name, linecount, input.c_str());
83 xbt_assert(last_event->date_ <= event.date_,
84 "%s:%d: Invalid trace: Events must be sorted, but time %g > time %g.\n%s", name, linecount,
85 last_event->date_, event.date_, input.c_str());
87 last_event->date_ = event.date_ - last_event->date_;
89 /* Add the first fake event storing the time at which the trace begins */
90 tmgr::DatedValue first_event(event.date_, -1.0);
91 trace->event_list.push_back(first_event);
93 trace->event_list.push_back(event);
94 last_event = &(trace->event_list.back());
97 if (periodicity > 0) {
98 last_event->date_ = periodicity + trace->event_list.at(0).date_;
100 last_event->date_ = -1;
104 trace_list.insert({xbt_strdup(name), trace});
109 tmgr_trace_t tmgr_trace_new_from_file(const char *filename)
111 xbt_assert(filename && filename[0], "Cannot parse a trace from the null or empty filename");
112 xbt_assert(trace_list.find(filename) == trace_list.end(), "Refusing to define trace %s twice", filename);
114 std::ifstream* f = surf_ifsopen(filename);
115 xbt_assert(!f->fail(), "Cannot open file '%s' (path=%s)", filename, (boost::join(surf_path, ":")).c_str());
117 std::stringstream buffer;
118 buffer << f->rdbuf();
119 tmgr_trace_t trace = tmgr_trace_new_from_string(filename, buffer.str(), -1);
126 tmgr_trace_t tmgr_empty_trace_new()
128 tmgr_trace_t trace = new simgrid::trace_mgr::trace();
129 tmgr::DatedValue val(0, 0);
130 trace->event_list.push_back(val);
135 void tmgr_trace_free(tmgr_trace_t trace)
140 /** @brief Registers a new trace into the future event set, and get an iterator over the integrated trace */
141 tmgr_trace_event_t simgrid::trace_mgr::future_evt_set::add_trace(tmgr_trace_t trace, surf::Resource* resource)
143 tmgr_trace_event_t trace_iterator = nullptr;
145 trace_iterator = xbt_new0(s_tmgr_trace_event_t, 1);
146 trace_iterator->trace = trace;
147 trace_iterator->idx = 0;
148 trace_iterator->resource = resource;
150 xbt_assert((trace_iterator->idx < trace->event_list.size()), "Your trace should have at least one event!");
152 xbt_heap_push(p_heap, trace_iterator, 0. /*start_time*/);
154 return trace_iterator;
157 /** @brief returns the date of the next occurring event (pure function) */
158 double simgrid::trace_mgr::future_evt_set::next_date() const
160 if (xbt_heap_size(p_heap))
161 return (xbt_heap_maxkey(p_heap));
166 /** @brief Retrieves the next occurring event, or nullptr if none happens before #date */
167 tmgr_trace_event_t simgrid::trace_mgr::future_evt_set::pop_leq(double date, double* value,
168 simgrid::surf::Resource** resource)
170 double event_date = next_date();
171 if (event_date > date)
174 tmgr_trace_event_t trace_iterator = (tmgr_trace_event_t)xbt_heap_pop(p_heap);
175 if (trace_iterator == nullptr)
178 tmgr_trace_t trace = trace_iterator->trace;
179 *resource = trace_iterator->resource;
181 tmgr::DatedValue dateVal = trace->event_list.at(trace_iterator->idx);
183 *value = dateVal.value_;
185 if (trace_iterator->idx < trace->event_list.size() - 1) {
186 xbt_heap_push(p_heap, trace_iterator, event_date + dateVal.date_);
187 trace_iterator->idx++;
188 } else if (dateVal.date_ > 0) { /* Last element. Shall we loop? */
189 xbt_heap_push(p_heap, trace_iterator, event_date + dateVal.date_);
190 trace_iterator->idx = 1; /* idx=0 is a placeholder to store when events really start */
191 } else { /* If we don't loop, we don't need this trace_event anymore */
192 trace_iterator->free_me = 1;
195 return trace_iterator;
200 for (auto kv : trace_list) {
201 xbt_free((char*)kv.first);
206 void tmgr_trace_event_unref(tmgr_trace_event_t* trace_event)
208 if ((*trace_event)->free_me) {
209 xbt_free(*trace_event);
210 *trace_event = nullptr;