Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
modern simcall for set_category
[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 Profile* Profile::from_string(std::string name, std::string input, double periodicity)
45 {
46   int linecount                                    = 0;
47   simgrid::kernel::profile::Profile* profile       = new simgrid::kernel::profile::Profile();
48   simgrid::kernel::profile::DatedValue* last_event = &(profile->event_list.back());
49
50   xbt_assert(trace_list.find(name) == trace_list.end(), "Refusing to define trace %s twice", name.c_str());
51
52   std::vector<std::string> list;
53   boost::split(list, input, boost::is_any_of("\n\r"));
54   for (auto val : list) {
55     simgrid::kernel::profile::DatedValue event;
56     linecount++;
57     boost::trim(val);
58     if (val[0] == '#' || val[0] == '\0' || val[0] == '%') // pass comments
59       continue;
60     if (sscanf(val.c_str(), "PERIODICITY %lg\n", &periodicity) == 1)
61       continue;
62     if (sscanf(val.c_str(), "LOOPAFTER %lg\n", &periodicity) == 1)
63       continue;
64
65     xbt_assert(sscanf(val.c_str(), "%lg  %lg\n", &event.date_, &event.value_) == 2, "%s:%d: Syntax error in trace\n%s",
66                name.c_str(), linecount, input.c_str());
67
68     xbt_assert(last_event->date_ <= event.date_,
69                "%s:%d: Invalid trace: Events must be sorted, but time %g > time %g.\n%s", name.c_str(), linecount,
70                last_event->date_, event.date_, input.c_str());
71     last_event->date_ = event.date_ - last_event->date_;
72
73     profile->event_list.push_back(event);
74     last_event = &(profile->event_list.back());
75   }
76   if (last_event) {
77     if (periodicity > 0) {
78       last_event->date_ = periodicity + profile->event_list.at(0).date_;
79     } else {
80       last_event->date_ = -1;
81     }
82   }
83
84   trace_list.insert({name, profile});
85
86   return profile;
87 }
88 Profile* Profile::from_file(std::string path)
89 {
90   xbt_assert(not path.empty(), "Cannot parse a trace from an empty filename");
91   xbt_assert(trace_list.find(path) == trace_list.end(), "Refusing to define trace %s twice", path.c_str());
92
93   std::ifstream* f = surf_ifsopen(path);
94   xbt_assert(not f->fail(), "Cannot open file '%s' (path=%s)", path.c_str(), (boost::join(surf_path, ":")).c_str());
95
96   std::stringstream buffer;
97   buffer << f->rdbuf();
98   delete f;
99
100   return Profile::from_string(path, buffer.str(), -1);
101 }
102 FutureEvtSet::FutureEvtSet() = default;
103 FutureEvtSet::~FutureEvtSet()
104 {
105   while (not heap_.empty()) {
106     delete heap_.top().second;
107     heap_.pop();
108   }
109 }
110
111 /** @brief Registers a new trace into the future event set, and get an iterator over the integrated trace  */
112 Event* FutureEvtSet::add_trace(Profile* profile, resource::Resource* resource)
113 {
114   Event* event    = new Event();
115   event->profile  = profile;
116   event->idx      = 0;
117   event->resource = resource;
118   event->free_me  = false;
119
120   xbt_assert((event->idx < profile->event_list.size()), "Your profile should have at least one event!");
121
122   heap_.emplace(0.0 /* start time */, event);
123
124   return event;
125 }
126
127 /** @brief returns the date of the next occurring event */
128 double FutureEvtSet::next_date() const
129 {
130   return heap_.empty() ? -1.0 : heap_.top().first;
131 }
132
133 /** @brief Retrieves the next occurring event, or nullptr if none happens before date */
134 Event* FutureEvtSet::pop_leq(double date, double* value, resource::Resource** resource)
135 {
136   double event_date = next_date();
137   if (event_date > date)
138     return nullptr;
139
140   if (heap_.empty())
141     return nullptr;
142   Event* event = heap_.top().second;
143   heap_.pop();
144
145   Profile* profile = event->profile;
146   *resource        = event->resource;
147
148   DatedValue dateVal = profile->event_list.at(event->idx);
149
150   *value = dateVal.value_;
151
152   if (event->idx < profile->event_list.size() - 1) {
153     heap_.emplace(event_date + dateVal.date_, event);
154     event->idx++;
155   } else if (dateVal.date_ > 0) { /* Last element. Shall we loop? */
156     heap_.emplace(event_date + dateVal.date_, event);
157     event->idx = 1; /* idx=0 is a placeholder to store when events really start */
158   } else {          /* If we don't loop, we don't need this event anymore */
159     event->free_me = true;
160   }
161
162   return event;
163 }
164 } // namespace profile
165 } // namespace kernel
166 } // namespace simgrid
167 void tmgr_finalize()
168 {
169   for (auto const& kv : trace_list)
170     delete kv.second;
171   trace_list.clear();
172 }
173
174 void tmgr_trace_event_unref(simgrid::kernel::profile::Event** event)
175 {
176   if ((*event)->free_me) {
177     delete *event;
178     *event = nullptr;
179   }
180 }