Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2d7a70f14ea812406d49a9316e4959eb3f397a85
[simgrid.git] / src / instr / instr_paje_types.cpp
1 /* Copyright (c) 2012-2022. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "simgrid/Exception.hpp"
8 #include "src/instr/instr_private.hpp"
9
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_paje_types, instr, "Paje tracing event system (types)");
11
12 namespace simgrid {
13 namespace instr {
14
15 long long int new_paje_id()
16 {
17   static long long int type_id = 0;
18   return type_id++;
19 }
20
21 Type::Type(PajeEventType event_type, const std::string& name, const std::string& alias, const std::string& color,
22            Type* parent)
23     : name_(name), color_(color), parent_(parent)
24 {
25   if (name_.empty() || alias.empty())
26     throw TracingError(XBT_THROW_POINT, "can't create a new type with no name or alias");
27
28   if (parent != nullptr) {
29     parent->children_[alias].reset(this);
30     XBT_DEBUG("new type %s, child of %s", get_cname(), parent->get_cname());
31     on_creation(*this, event_type);
32   }
33 }
34
35 void StateType::set_event(const std::string& value_name)
36 {
37   events_.push_back(new StateEvent(get_issuer(), this, PajeEventType::SetState, get_entity_value(value_name), nullptr));
38 }
39
40 void StateType::push_event(const std::string& value_name, TIData* extra)
41 {
42   events_.push_back(new StateEvent(get_issuer(), this, PajeEventType::PushState, get_entity_value(value_name), extra));
43 }
44
45 void StateType::push_event(const std::string& value_name)
46 {
47   events_.push_back(
48       new StateEvent(get_issuer(), this, PajeEventType::PushState, get_entity_value(value_name), nullptr));
49 }
50
51 void StateType::pop_event()
52 {
53   pop_event(nullptr);
54 }
55
56 void StateType::pop_event(TIData* extra)
57 {
58   events_.push_back(new StateEvent(get_issuer(), this, PajeEventType::PopState, nullptr, extra));
59 }
60
61 void VariableType::instr_event(double now, double delta, const char* resource, double value)
62 {
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.
66    */
67
68   // to check if variables were previously set to 0, otherwise paje won't simulate them
69   // create a key considering the resource and variable, and check if key exists in the global map:
70   // if it doesn't, set the variable to zero.
71   if (static std::set<std::string, std::less<>> platform_variables;
72       platform_variables.emplace(std::string(resource) + get_name()).second)
73     set_event(now, 0);
74
75   add_event(now, value);
76   sub_event(now + delta, value);
77 }
78
79 void VariableType::set_event(double timestamp, double value)
80 {
81   events_.push_back(new VariableEvent(timestamp, get_issuer(), this, PajeEventType::SetVariable, value));
82 }
83
84 void VariableType::add_event(double timestamp, double value)
85 {
86   events_.push_back(new VariableEvent(timestamp, get_issuer(), this, PajeEventType::AddVariable, value));
87 }
88
89 void VariableType::sub_event(double timestamp, double value)
90 {
91   events_.push_back(new VariableEvent(timestamp, get_issuer(), this, PajeEventType::SubVariable, value));
92 }
93
94 void LinkType::start_event(Container* startContainer, const std::string& value, const std::string& key, size_t size)
95 {
96   new LinkEvent(get_issuer(), this, PajeEventType::StartLink, startContainer, value, key, size);
97 }
98
99 void LinkType::end_event(Container* endContainer, const std::string& value, const std::string& key)
100 {
101   new LinkEvent(get_issuer(), this, PajeEventType::EndLink, endContainer, value, key);
102 }
103
104 Type* Type::by_name(const std::string& name)
105 {
106   Type* ret = nullptr;
107   for (auto const& [_, child] : children_) {
108     if (child->name_ == name) {
109       if (ret != nullptr) {
110         throw TracingError(XBT_THROW_POINT, "there are two children types with the same name?");
111       } else {
112         ret = child.get();
113       }
114     }
115   }
116   if (ret == nullptr)
117     throw TracingError(XBT_THROW_POINT, xbt::string_printf("type with name (%s) not found in parent type (%s)",
118                                                            name.c_str(), get_cname()));
119   return ret;
120 }
121
122 void ValueType::add_entity_value(const std::string& name)
123 {
124   add_entity_value(name, "");
125 }
126
127 void ValueType::add_entity_value(const std::string& name, const std::string& color)
128 {
129   if (name.empty())
130     throw TracingError(XBT_THROW_POINT, "can't get a value with no name");
131
132   auto it = values_.find(name);
133   if (it == values_.end()) {
134     XBT_DEBUG("new value %s, child of %s", name.c_str(), get_cname());
135     values_.try_emplace(name, name, color, this);
136   }
137 }
138
139 EntityValue* ValueType::get_entity_value(const std::string& name)
140 {
141   auto ret = values_.find(name);
142   if (ret == values_.end()) {
143     throw TracingError(XBT_THROW_POINT, xbt::string_printf("value with name (%s) not found in parent type (%s)",
144                                                            name.c_str(), get_cname()));
145   }
146   return &ret->second;
147 }
148
149 VariableType* Type::by_name_or_create(const std::string& name, const std::string& color)
150 {
151   auto cont = children_.find(name);
152   std::string mycolor = color.empty() ? "1 1 1" : color;
153   return cont == children_.end() ? new VariableType(name, mycolor, this)
154                                  : static_cast<VariableType*>(cont->second.get());
155 }
156
157 LinkType* Type::by_name_or_create(const std::string& name, const Type* source, const Type* dest)
158 {
159   std::string alias = name + "-" + std::to_string(source->id_) + "-" + std::to_string(dest->id_);
160   auto it           = children_.find(alias);
161   if (it == children_.end()) {
162     return new LinkType(name, source, dest, alias, this);
163   } else
164     return static_cast<LinkType*>(it->second.get());
165 }
166 } // namespace instr
167 } // namespace simgrid