Logo AND Algorithmique Numérique Distribuée

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