Logo AND Algorithmique Numérique Distribuée

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