1 /* Copyright (c) 2004-2020. 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. */
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"
13 #include <boost/algorithm/string.hpp>
17 #include <unordered_map>
20 static std::unordered_map<std::string, simgrid::kernel::profile::Profile*> trace_list;
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);
32 Profile::~Profile() = default;
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)
38 Event* event = new Event();
39 event->profile = this;
41 event->resource = resource;
42 event->free_me = false;
44 xbt_assert((event->idx < event_list.size()), "Your profile should have at least one event!");
47 fes_->add_event(0.0 /* start time */, event);
52 /** @brief Gets the next event from a profile */
53 DatedValue Profile::next(Event* event)
55 double event_date = fes_->next_date();
56 DatedValue dateVal = event_list.at(event->idx);
58 if (event->idx < event_list.size() - 1) {
59 fes_->add_event(event_date + dateVal.date_, event);
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;
71 Profile* Profile::from_string(const std::string& name, const std::string& input, double periodicity)
74 simgrid::kernel::profile::Profile* profile = new simgrid::kernel::profile::Profile();
75 simgrid::kernel::profile::DatedValue* last_event = &(profile->event_list.back());
77 xbt_assert(trace_list.find(name) == trace_list.end(), "Refusing to define trace %s twice", name.c_str());
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;
85 if (val[0] == '#' || val[0] == '\0' || val[0] == '%') // pass comments
87 if (sscanf(val.c_str(), "PERIODICITY %lg\n", &periodicity) == 1)
89 if (sscanf(val.c_str(), "LOOPAFTER %lg\n", &periodicity) == 1)
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());
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_;
100 profile->event_list.push_back(event);
101 last_event = &(profile->event_list.back());
104 if (periodicity > 0) {
105 last_event->date_ = periodicity + profile->event_list.at(0).date_;
107 last_event->date_ = -1;
111 trace_list.insert({name, profile});
115 Profile* Profile::from_file(const std::string& path)
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());
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());
123 std::stringstream buffer;
124 buffer << f->rdbuf();
127 return Profile::from_string(path, buffer.str(), -1);
130 } // namespace profile
131 } // namespace kernel
132 } // namespace simgrid
136 for (auto const& kv : trace_list)
141 void tmgr_trace_event_unref(simgrid::kernel::profile::Event** event)
143 if ((*event)->free_me) {