Logo AND Algorithmique Numérique Distribuée

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