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_;
49 /* Add the first fake event storing the time at which the trace begins */
50 tmgr::DatedValue val(0, -1);
51 event_list.push_back(val);
53 trace::~trace() = default;
54 future_evt_set::future_evt_set() = default;
55 simgrid::trace_mgr::future_evt_set::~future_evt_set()
62 tmgr_trace_t tmgr_trace_new_from_string(const char* name, std::string input, double periodicity)
65 tmgr_trace_t trace = new simgrid::trace_mgr::trace();
66 tmgr::DatedValue* last_event = &(trace->event_list.back());
68 xbt_assert(trace_list.find(name) == trace_list.end(), "Refusing to define trace %s twice", name);
70 std::vector<std::string> list;
71 boost::split(list, input, boost::is_any_of("\n\r"));
72 for (auto val : list) {
73 tmgr::DatedValue event;
76 if (val[0] == '#' || val[0] == '\0' || val[0] == '%') // pass comments
78 if (sscanf(val.c_str(), "PERIODICITY %lg\n", &periodicity) == 1)
80 if (sscanf(val.c_str(), "LOOPAFTER %lg\n", &periodicity) == 1)
83 xbt_assert(sscanf(val.c_str(), "%lg %lg\n", &event.date_, &event.value_) == 2, "%s:%d: Syntax error in trace\n%s",
84 name, linecount, input.c_str());
86 xbt_assert(last_event->date_ <= event.date_,
87 "%s:%d: Invalid trace: Events must be sorted, but time %g > time %g.\n%s", name, linecount,
88 last_event->date_, event.date_, input.c_str());
89 last_event->date_ = event.date_ - last_event->date_;
91 trace->event_list.push_back(event);
92 last_event = &(trace->event_list.back());
95 if (periodicity > 0) {
96 last_event->date_ = periodicity + trace->event_list.at(0).date_;
98 last_event->date_ = -1;
102 trace_list.insert({xbt_strdup(name), trace});
107 tmgr_trace_t tmgr_trace_new_from_file(const char *filename)
109 xbt_assert(filename && filename[0], "Cannot parse a trace from the null or empty filename");
110 xbt_assert(trace_list.find(filename) == trace_list.end(), "Refusing to define trace %s twice", filename);
112 std::ifstream* f = surf_ifsopen(filename);
113 xbt_assert(not f->fail(), "Cannot open file '%s' (path=%s)", filename, (boost::join(surf_path, ":")).c_str());
115 std::stringstream buffer;
116 buffer << f->rdbuf();
119 return tmgr_trace_new_from_string(filename, buffer.str(), -1);
122 /** @brief Registers a new trace into the future event set, and get an iterator over the integrated trace */
123 tmgr_trace_event_t simgrid::trace_mgr::future_evt_set::add_trace(tmgr_trace_t trace, surf::Resource* resource)
125 tmgr_trace_event_t trace_iterator = nullptr;
127 trace_iterator = xbt_new0(s_tmgr_trace_event_t, 1);
128 trace_iterator->trace = trace;
129 trace_iterator->idx = 0;
130 trace_iterator->resource = resource;
132 xbt_assert((trace_iterator->idx < trace->event_list.size()), "Your trace should have at least one event!");
134 xbt_heap_push(heap_, trace_iterator, 0. /*start_time*/);
136 return trace_iterator;
139 /** @brief returns the date of the next occurring event (pure function) */
140 double simgrid::trace_mgr::future_evt_set::next_date() const
142 if (xbt_heap_size(heap_))
143 return (xbt_heap_maxkey(heap_));
147 /** @brief Retrieves the next occurring event, or nullptr if none happens before #date */
148 tmgr_trace_event_t simgrid::trace_mgr::future_evt_set::pop_leq(double date, double* value,
149 simgrid::surf::Resource** resource)
151 double event_date = next_date();
152 if (event_date > date)
155 tmgr_trace_event_t trace_iterator = (tmgr_trace_event_t)xbt_heap_pop(heap_);
156 if (trace_iterator == nullptr)
159 tmgr_trace_t trace = trace_iterator->trace;
160 *resource = trace_iterator->resource;
162 tmgr::DatedValue dateVal = trace->event_list.at(trace_iterator->idx);
164 *value = dateVal.value_;
166 if (trace_iterator->idx < trace->event_list.size() - 1) {
167 xbt_heap_push(heap_, trace_iterator, event_date + dateVal.date_);
168 trace_iterator->idx++;
169 } else if (dateVal.date_ > 0) { /* Last element. Shall we loop? */
170 xbt_heap_push(heap_, trace_iterator, event_date + dateVal.date_);
171 trace_iterator->idx = 1; /* idx=0 is a placeholder to store when events really start */
172 } else { /* If we don't loop, we don't need this trace_event anymore */
173 trace_iterator->free_me = 1;
176 return trace_iterator;
181 for (auto kv : trace_list) {
182 xbt_free((char*)kv.first);
188 void tmgr_trace_event_unref(tmgr_trace_event_t* trace_event)
190 if ((*trace_event)->free_me) {
191 xbt_free(*trace_event);
192 *trace_event = nullptr;