Logo AND Algorithmique Numérique Distribuée

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