Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Rename C++ only header files from .h to .hpp.
[simgrid.git] / src / surf / trace_mgr.cpp
1 /* Copyright (c) 2004-2005, 2007, 2009-2014, 2016-2017. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "xbt/sysdep.h"
8 #include "xbt/log.h"
9
10 #include "src/surf/surf_interface.hpp"
11 #include "src/surf/trace_mgr.hpp"
12 #include "surf_private.hpp"
13 #include "xbt/RngStream.h"
14 #include <boost/algorithm/string.hpp>
15 #include <boost/algorithm/string/join.hpp>
16 #include <boost/algorithm/string/split.hpp>
17 #include <cmath>
18 #include <fstream>
19 #include <sstream>
20 #include <unordered_map>
21
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_trace, surf, "Surf trace management");
23
24 namespace tmgr = simgrid::trace_mgr;
25
26 static std::unordered_map<std::string, tmgr::trace*> trace_list;
27
28 static inline bool doubleEq(double d1, double d2)
29 {
30   return fabs(d1 - d2) < 0.0001;
31 }
32 namespace simgrid {
33 namespace trace_mgr {
34
35 bool DatedValue::operator==(DatedValue e2)
36 {
37   return (doubleEq(date_, e2.date_)) && (doubleEq(value_, e2.value_));
38 }
39 std::ostream& operator<<(std::ostream& out, const DatedValue& e)
40 {
41   out << e.date_ << " " << e.value_;
42   return out;
43 }
44
45 trace::trace()
46 {
47   /* Add the first fake event storing the time at which the trace begins */
48   tmgr::DatedValue val(0, -1);
49   event_list.push_back(val);
50 }
51 trace::~trace()                  = default;
52 future_evt_set::future_evt_set() = default;
53 simgrid::trace_mgr::future_evt_set::~future_evt_set()
54 {
55   xbt_heap_free(heap_);
56 }
57 }
58 }
59
60 tmgr_trace_t tmgr_trace_new_from_string(std::string name, std::string input, double periodicity)
61 {
62   int linecount = 0;
63   tmgr_trace_t trace           = new simgrid::trace_mgr::trace();
64   tmgr::DatedValue* last_event = &(trace->event_list.back());
65
66   xbt_assert(trace_list.find(name) == trace_list.end(), "Refusing to define trace %s twice", name.c_str());
67
68   std::vector<std::string> list;
69   boost::split(list, input, boost::is_any_of("\n\r"));
70   for (auto val : list) {
71     tmgr::DatedValue event;
72     linecount++;
73     boost::trim(val);
74     if (val[0] == '#' || val[0] == '\0' || val[0] == '%') // pass comments
75       continue;
76     if (sscanf(val.c_str(), "PERIODICITY %lg\n", &periodicity) == 1)
77       continue;
78     if (sscanf(val.c_str(), "LOOPAFTER %lg\n", &periodicity) == 1)
79       continue;
80
81     xbt_assert(sscanf(val.c_str(), "%lg  %lg\n", &event.date_, &event.value_) == 2, "%s:%d: Syntax error in trace\n%s",
82                name.c_str(), linecount, input.c_str());
83
84     xbt_assert(last_event->date_ <= event.date_,
85                "%s:%d: Invalid trace: Events must be sorted, but time %g > time %g.\n%s", name.c_str(), linecount,
86                last_event->date_, event.date_, input.c_str());
87     last_event->date_ = event.date_ - last_event->date_;
88
89     trace->event_list.push_back(event);
90     last_event = &(trace->event_list.back());
91   }
92   if (last_event) {
93     if (periodicity > 0) {
94       last_event->date_ = periodicity + trace->event_list.at(0).date_;
95     } else {
96       last_event->date_ = -1;
97     }
98   }
99
100   trace_list.insert({name, trace});
101
102   return trace;
103 }
104
105 tmgr_trace_t tmgr_trace_new_from_file(std::string filename)
106 {
107   xbt_assert(not filename.empty(), "Cannot parse a trace from an empty filename");
108   xbt_assert(trace_list.find(filename) == trace_list.end(), "Refusing to define trace %s twice", filename.c_str());
109
110   std::ifstream* f = surf_ifsopen(filename);
111   xbt_assert(not f->fail(), "Cannot open file '%s' (path=%s)", filename.c_str(), (boost::join(surf_path, ":")).c_str());
112
113   std::stringstream buffer;
114   buffer << f->rdbuf();
115   delete f;
116
117   return tmgr_trace_new_from_string(filename, buffer.str(), -1);
118 }
119
120 /** @brief Registers a new trace into the future event set, and get an iterator over the integrated trace  */
121 tmgr_trace_event_t simgrid::trace_mgr::future_evt_set::add_trace(tmgr_trace_t trace, surf::Resource* resource)
122 {
123   tmgr_trace_event_t trace_iterator = nullptr;
124
125   trace_iterator = xbt_new0(s_tmgr_trace_event_t, 1);
126   trace_iterator->trace = trace;
127   trace_iterator->idx = 0;
128   trace_iterator->resource = resource;
129
130   xbt_assert((trace_iterator->idx < trace->event_list.size()), "Your trace should have at least one event!");
131
132   xbt_heap_push(heap_, trace_iterator, 0. /*start_time*/);
133
134   return trace_iterator;
135 }
136
137 /** @brief returns the date of the next occurring event (pure function) */
138 double simgrid::trace_mgr::future_evt_set::next_date() const
139 {
140   if (xbt_heap_size(heap_))
141     return (xbt_heap_maxkey(heap_));
142   return -1.0;
143 }
144
145 /** @brief Retrieves the next occurring event, or nullptr if none happens before #date */
146 tmgr_trace_event_t simgrid::trace_mgr::future_evt_set::pop_leq(double date, double* value,
147                                                                simgrid::surf::Resource** resource)
148 {
149   double event_date = next_date();
150   if (event_date > date)
151     return nullptr;
152
153   tmgr_trace_event_t trace_iterator = (tmgr_trace_event_t)xbt_heap_pop(heap_);
154   if (trace_iterator == nullptr)
155     return nullptr;
156
157   tmgr_trace_t trace = trace_iterator->trace;
158   *resource = trace_iterator->resource;
159
160   tmgr::DatedValue dateVal = trace->event_list.at(trace_iterator->idx);
161
162   *value = dateVal.value_;
163
164   if (trace_iterator->idx < trace->event_list.size() - 1) {
165     xbt_heap_push(heap_, trace_iterator, event_date + dateVal.date_);
166     trace_iterator->idx++;
167   } else if (dateVal.date_ > 0) { /* Last element. Shall we loop? */
168     xbt_heap_push(heap_, trace_iterator, event_date + dateVal.date_);
169     trace_iterator->idx = 1; /* idx=0 is a placeholder to store when events really start */
170   } else {                   /* If we don't loop, we don't need this trace_event anymore */
171     trace_iterator->free_me = 1;
172   }
173
174   return trace_iterator;
175 }
176
177 void tmgr_finalize()
178 {
179   for (auto const& kv : trace_list)
180     delete kv.second;
181   trace_list.clear();
182 }
183
184 void tmgr_trace_event_unref(tmgr_trace_event_t* trace_event)
185 {
186   if ((*trace_event)->free_me) {
187     xbt_free(*trace_event);
188     *trace_event = nullptr;
189   }
190 }