Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[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(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   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;
74     linecount++;
75     boost::trim(val);
76     if (val[0] == '#' || val[0] == '\0' || val[0] == '%') // pass comments
77       continue;
78     if (sscanf(val.c_str(), "PERIODICITY %lg\n", &periodicity) == 1)
79       continue;
80     if (sscanf(val.c_str(), "LOOPAFTER %lg\n", &periodicity) == 1)
81       continue;
82
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());
85
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_;
90
91     trace->event_list.push_back(event);
92     last_event = &(trace->event_list.back());
93   }
94   if (last_event) {
95     if (periodicity > 0) {
96       last_event->date_ = periodicity + trace->event_list.at(0).date_;
97     } else {
98       last_event->date_ = -1;
99     }
100   }
101
102   trace_list.insert({xbt_strdup(name), trace});
103
104   return trace;
105 }
106
107 tmgr_trace_t tmgr_trace_new_from_file(const char *filename)
108 {
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);
111
112   std::ifstream* f = surf_ifsopen(filename);
113   xbt_assert(!f->fail(), "Cannot open file '%s' (path=%s)", filename, (boost::join(surf_path, ":")).c_str());
114
115   std::stringstream buffer;
116   buffer << f->rdbuf();
117   delete f;
118
119   return tmgr_trace_new_from_string(filename, buffer.str(), -1);
120 }
121
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)
124 {
125   tmgr_trace_event_t trace_iterator = nullptr;
126
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;
131
132   xbt_assert((trace_iterator->idx < trace->event_list.size()), "Your trace should have at least one event!");
133
134   xbt_heap_push(heap_, trace_iterator, 0. /*start_time*/);
135
136   return trace_iterator;
137 }
138
139 /** @brief returns the date of the next occurring event (pure function) */
140 double simgrid::trace_mgr::future_evt_set::next_date() const
141 {
142   if (xbt_heap_size(heap_))
143     return (xbt_heap_maxkey(heap_));
144   return -1.0;
145 }
146
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)
150 {
151   double event_date = next_date();
152   if (event_date > date)
153     return nullptr;
154
155   tmgr_trace_event_t trace_iterator = (tmgr_trace_event_t)xbt_heap_pop(heap_);
156   if (trace_iterator == nullptr)
157     return nullptr;
158
159   tmgr_trace_t trace = trace_iterator->trace;
160   *resource = trace_iterator->resource;
161
162   tmgr::DatedValue dateVal = trace->event_list.at(trace_iterator->idx);
163
164   *value = dateVal.value_;
165
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;
174   }
175
176   return trace_iterator;
177 }
178
179 void tmgr_finalize()
180 {
181   for (auto kv : trace_list) {
182     xbt_free((char*)kv.first);
183     delete kv.second;
184   }
185 }
186
187 void tmgr_trace_event_unref(tmgr_trace_event_t* trace_event)
188 {
189   if ((*trace_event)->free_me) {
190     xbt_free(*trace_event);
191     *trace_event = nullptr;
192   }
193 }