Logo AND Algorithmique Numérique Distribuée

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