1 /* Copyright (c) 2004-2019. The SimGrid Team. All rights reserved. */
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. */
7 #include "xbt/sysdep.h"
9 #include "src/kernel/resource/profile/Profile.hpp"
10 #include <boost/algorithm/string.hpp>
11 #include "src/kernel/resource/profile/FutureEvtSet.cpp"
12 #include "src/kernel/resource/profile/DatedValue.cpp"
13 #include "simgrid/forward.h"
16 static std::unordered_map<std::string, simgrid::kernel::profile::Profile*> trace_list;
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);
28 Profile::~Profile() = default;
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)
34 Event* event = new Event();
35 event->profile = this;
37 event->resource = resource;
38 event->free_me = false;
40 xbt_assert((event->idx < event_list.size()), "Your profile should have at least one event!");
43 fes_->add_event(0.0 /* start time */, event);
48 /** @brief Gets the next event from a profile */
49 DatedValue Profile::next(Event* event)
51 double event_date = fes_->next_date();
52 DatedValue dateVal = event_list.at(event->idx);
54 if (event->idx < event_list.size() - 1) {
55 fes_->add_event(event_date + dateVal.date_, event);
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;
67 Profile* Profile::from_string(const std::string& name, const std::string& input, double periodicity)
70 simgrid::kernel::profile::Profile* profile = new simgrid::kernel::profile::Profile();
71 simgrid::kernel::profile::DatedValue* last_event = &(profile->event_list.back());
73 xbt_assert(trace_list.find(name) == trace_list.end(), "Refusing to define trace %s twice", name.c_str());
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;
81 if (val[0] == '#' || val[0] == '\0' || val[0] == '%') // pass comments
83 if (sscanf(val.c_str(), "PERIODICITY %lg\n", &periodicity) == 1)
85 if (sscanf(val.c_str(), "LOOPAFTER %lg\n", &periodicity) == 1)
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());
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_;
96 profile->event_list.push_back(event);
97 last_event = &(profile->event_list.back());
100 if (periodicity > 0) {
101 last_event->date_ = periodicity + profile->event_list.at(0).date_;
103 last_event->date_ = -1;
107 trace_list.insert({name, profile});
111 Profile* Profile::from_file(const std::string& path)
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());
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());
119 std::stringstream buffer;
120 buffer << f->rdbuf();
123 return Profile::from_string(path, buffer.str(), -1);
126 } // namespace profile
127 } // namespace kernel
128 } // namespace simgrid