1 /* Copyright (c) 2012-2021. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "simgrid/Exception.hpp"
8 #include "src/instr/instr_private.hpp"
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_paje_types, instr, "Paje tracing event system (types)");
15 long long int new_paje_id()
17 static long long int type_id = 0;
21 Type::Type(PajeEventType event_type, const std::string& name, const std::string& alias, const std::string& color,
23 : name_(name), color_(color), father_(father)
25 if (name_.empty() || alias.empty())
26 throw TracingError(XBT_THROW_POINT, "can't create a new type with no name or alias");
28 if (father != nullptr){
29 father->children_[alias].reset(this);
30 XBT_DEBUG("new type %s, child of %s", get_cname(), father->get_cname());
31 on_creation(*this, event_type);
35 void StateType::set_event(const std::string& value_name)
37 events_.push_back(new StateEvent(get_issuer(), this, PajeEventType::SetState, get_entity_value(value_name), nullptr));
40 void StateType::push_event(const std::string& value_name, TIData* extra)
42 events_.push_back(new StateEvent(get_issuer(), this, PajeEventType::PushState, get_entity_value(value_name), extra));
45 void StateType::push_event(const std::string& value_name)
48 new StateEvent(get_issuer(), this, PajeEventType::PushState, get_entity_value(value_name), nullptr));
51 void StateType::pop_event()
56 void StateType::pop_event(TIData* extra)
58 events_.push_back(new StateEvent(get_issuer(), this, PajeEventType::PopState, nullptr, extra));
61 void VariableType::instr_event(double now, double delta, const char* resource, double value)
63 /* To trace resource utilization, we use AddEvent and SubEvent only. This implies to add a SetEvent first to set the
64 * initial value of all variables for subsequent adds/subs. If we don't do so, the first AddEvent would be added to a
65 * non-determined value, hence causing analysis problems.
68 // to check if variables were previously set to 0, otherwise paje won't simulate them
69 static std::set<std::string, std::less<>> platform_variables;
71 // create a key considering the resource and variable
72 std::string key = std::string(resource) + get_name();
74 // check if key exists: if it doesn't, set the variable to zero and mark this in the global map.
75 if (platform_variables.find(key) == platform_variables.end()) {
77 platform_variables.insert(key);
80 add_event(now, value);
81 sub_event(now + delta, value);
84 void VariableType::set_event(double timestamp, double value)
86 events_.push_back(new VariableEvent(timestamp, get_issuer(), this, PajeEventType::SetVariable, value));
89 void VariableType::add_event(double timestamp, double value)
91 events_.push_back(new VariableEvent(timestamp, get_issuer(), this, PajeEventType::AddVariable, value));
94 void VariableType::sub_event(double timestamp, double value)
96 events_.push_back(new VariableEvent(timestamp, get_issuer(), this, PajeEventType::SubVariable, value));
99 void LinkType::start_event(Container* startContainer, const std::string& value, const std::string& key)
101 start_event(startContainer, value, key, -1);
104 void LinkType::start_event(Container* startContainer, const std::string& value, const std::string& key, int size)
106 new LinkEvent(get_issuer(), this, PajeEventType::StartLink, startContainer, value, key, size);
109 void LinkType::end_event(Container* endContainer, const std::string& value, const std::string& key)
111 new LinkEvent(get_issuer(), this, PajeEventType::EndLink, endContainer, value, key, -1);
114 Type* Type::by_name(const std::string& name)
117 for (auto const& elm : children_) {
118 if (elm.second->name_ == name) {
119 if (ret != nullptr) {
120 throw TracingError(XBT_THROW_POINT, "there are two children types with the same name?");
122 ret = elm.second.get();
127 throw TracingError(XBT_THROW_POINT, xbt::string_printf("type with name (%s) not found in father type (%s)",
128 name.c_str(), get_cname()));
132 void ValueType::add_entity_value(const std::string& name)
134 add_entity_value(name, "");
137 void ValueType::add_entity_value(const std::string& name, const std::string& color)
140 throw TracingError(XBT_THROW_POINT, "can't get a value with no name");
142 auto it = values_.find(name);
143 if (it == values_.end()) {
144 XBT_DEBUG("new value %s, child of %s", name.c_str(), get_cname());
145 values_.emplace(name, EntityValue(name, color, this));
149 EntityValue* ValueType::get_entity_value(const std::string& name)
151 auto ret = values_.find(name);
152 if (ret == values_.end()) {
153 throw TracingError(XBT_THROW_POINT, xbt::string_printf("value with name (%s) not found in father type (%s)",
154 name.c_str(), get_cname()));
159 VariableType* Type::by_name_or_create(const std::string& name, const std::string& color)
161 auto cont = children_.find(name);
162 std::string mycolor = color.empty() ? "1 1 1" : color;
163 return cont == children_.end() ? new VariableType(name, mycolor, this)
164 : static_cast<VariableType*>(cont->second.get());
167 LinkType* Type::by_name_or_create(const std::string& name, const Type* source, const Type* dest)
169 std::string alias = name + "-" + std::to_string(source->id_) + "-" + std::to_string(dest->id_);
170 auto it = children_.find(alias);
171 if (it == children_.end()) {
172 return new LinkType(name, source, dest, alias, this);
174 return static_cast<LinkType*>(it->second.get());
177 } // namespace simgrid