Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines with new year.
[simgrid.git] / src / kernel / resource / profile / Profile.cpp
1 /* Copyright (c) 2004-2020. 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 "src/kernel/resource/profile/Profile.hpp"
7 #include "simgrid/forward.h"
8 #include "src/kernel/resource/profile/DatedValue.hpp"
9 #include "src/kernel/resource/profile/Event.hpp"
10 #include "src/kernel/resource/profile/FutureEvtSet.hpp"
11 #include "src/surf/surf_interface.hpp"
12
13 #include <boost/algorithm/string.hpp>
14 #include <fstream>
15 #include <ostream>
16 #include <sstream>
17 #include <unordered_map>
18 #include <vector>
19
20 static std::unordered_map<std::string, simgrid::kernel::profile::Profile*> trace_list;
21
22 namespace simgrid {
23 namespace kernel {
24 namespace profile {
25
26 Profile::Profile()
27 {
28   /* Add the first fake event storing the time at which the trace begins */
29   DatedValue val(0, -1);
30   event_list.push_back(val);
31 }
32 Profile::~Profile() = default;
33
34 /** @brief Register this profile for that resource onto that FES,
35  * and get an iterator over the integrated trace  */
36 Event* Profile::schedule(FutureEvtSet* fes, resource::Resource* resource)
37 {
38   Event* event    = new Event();
39   event->profile  = this;
40   event->idx      = 0;
41   event->resource = resource;
42   event->free_me  = false;
43
44   xbt_assert((event->idx < event_list.size()), "Your profile should have at least one event!");
45
46   fes_ = fes;
47   fes_->add_event(0.0 /* start time */, event);
48
49   return event;
50 }
51
52 /** @brief Gets the next event from a profile */
53 DatedValue Profile::next(Event* event)
54 {
55   double event_date  = fes_->next_date();
56   DatedValue dateVal = event_list.at(event->idx);
57
58   if (event->idx < event_list.size() - 1) {
59     fes_->add_event(event_date + dateVal.date_, event);
60     event->idx++;
61   } else if (dateVal.date_ > 0) { /* Last element. Shall we loop? */
62     fes_->add_event(event_date + dateVal.date_, event);
63     event->idx = 1; /* idx=0 is a placeholder to store when events really start */
64   } else {          /* If we don't loop, we don't need this event anymore */
65     event->free_me = true;
66   }
67
68   return dateVal;
69 }
70
71 Profile* Profile::from_string(const std::string& name, const std::string& input, double periodicity)
72 {
73   int linecount                                    = 0;
74   simgrid::kernel::profile::Profile* profile       = new simgrid::kernel::profile::Profile();
75   simgrid::kernel::profile::DatedValue* last_event = &(profile->event_list.back());
76
77   xbt_assert(trace_list.find(name) == trace_list.end(), "Refusing to define trace %s twice", name.c_str());
78
79   std::vector<std::string> list;
80   boost::split(list, input, boost::is_any_of("\n\r"));
81   for (auto val : list) {
82     simgrid::kernel::profile::DatedValue event;
83     linecount++;
84     boost::trim(val);
85     if (val[0] == '#' || val[0] == '\0' || val[0] == '%') // pass comments
86       continue;
87     if (sscanf(val.c_str(), "PERIODICITY %lg\n", &periodicity) == 1)
88       continue;
89     if (sscanf(val.c_str(), "LOOPAFTER %lg\n", &periodicity) == 1)
90       continue;
91
92     XBT_ATTRIB_UNUSED int res = sscanf(val.c_str(), "%lg  %lg\n", &event.date_, &event.value_);
93     xbt_assert(res == 2, "%s:%d: Syntax error in trace\n%s", name.c_str(), linecount, input.c_str());
94
95     xbt_assert(last_event->date_ <= event.date_,
96                "%s:%d: Invalid trace: Events must be sorted, but time %g > time %g.\n%s", name.c_str(), linecount,
97                last_event->date_, event.date_, input.c_str());
98     last_event->date_ = event.date_ - last_event->date_;
99
100     profile->event_list.push_back(event);
101     last_event = &(profile->event_list.back());
102   }
103   if (last_event) {
104     if (periodicity > 0) {
105       last_event->date_ = periodicity + profile->event_list.at(0).date_;
106     } else {
107       last_event->date_ = -1;
108     }
109   }
110
111   trace_list.insert({name, profile});
112
113   return profile;
114 }
115 Profile* Profile::from_file(const std::string& path)
116 {
117   xbt_assert(not path.empty(), "Cannot parse a trace from an empty filename");
118   xbt_assert(trace_list.find(path) == trace_list.end(), "Refusing to define trace %s twice", path.c_str());
119
120   const std::ifstream* f = surf_ifsopen(path);
121   xbt_assert(not f->fail(), "Cannot open file '%s' (path=%s)", path.c_str(), (boost::join(surf_path, ":")).c_str());
122
123   std::stringstream buffer;
124   buffer << f->rdbuf();
125   delete f;
126
127   return Profile::from_string(path, buffer.str(), -1);
128 }
129
130 } // namespace profile
131 } // namespace kernel
132 } // namespace simgrid
133
134 void tmgr_finalize()
135 {
136   for (auto const& kv : trace_list)
137     delete kv.second;
138   trace_list.clear();
139 }
140
141 void tmgr_trace_event_unref(simgrid::kernel::profile::Event** event)
142 {
143   if ((*event)->free_me) {
144     delete *event;
145     *event = nullptr;
146   }
147 }