Logo AND Algorithmique Numérique Distribuée

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