Logo AND Algorithmique Numérique Distribuée

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