Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4ae22bb97bec6e9ebd35a2de02531fdc2b727b0f
[simgrid.git] / src / instr / instr_paje_types.cpp
1 /* Copyright (c) 2012, 2014-2017. 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 "src/instr/instr_private.hpp"
8
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_paje_types, instr, "Paje tracing event system (types)");
10
11 static simgrid::instr::ContainerType* rootType = nullptr; /* the root type */
12 extern FILE* tracing_file;
13
14 namespace simgrid {
15 namespace instr {
16
17 Type::Type(std::string name, std::string alias, std::string color, Type* father)
18     : id_(instr_new_paje_id()), name_(name), color_(color), father_(father)
19 {
20   if (name.empty() || alias.empty())
21     THROWF(tracing_error, 0, "can't create a new type with no name or alias");
22
23   if (father != nullptr){
24     father->children_.insert({alias, this});
25     XBT_DEBUG("new type %s, child of %s", name_.c_str(), father->getCname());
26   }
27 }
28
29 Type::~Type()
30 {
31   for (auto elm : children_)
32     delete elm.second;
33 }
34
35 ValueType::~ValueType()
36 {
37   for (auto elm : values_)
38     delete elm.second;
39 }
40
41 ContainerType::ContainerType(std::string name, Type* father) : Type(name, name, "", father)
42 {
43   XBT_DEBUG("ContainerType %s(%lld), child of %s(%lld)", getCname(), getId(), father->getCname(), father->getId());
44   logDefinition(PAJE_DefineContainerType);
45 }
46
47 EventType::EventType(std::string name, Type* father) : ValueType(name, father)
48 {
49   XBT_DEBUG("EventType %s(%lld), child of %s(%lld)", getCname(), getId(), father->getCname(), father->getId());
50   logDefinition(PAJE_DefineEventType);
51 }
52
53 StateType::StateType(std::string name, Type* father) : ValueType(name, father)
54 {
55   XBT_DEBUG("StateType %s(%lld), child of %s(%lld)", getCname(), getId(), father->getCname(), father->getId());
56   logDefinition(PAJE_DefineStateType);
57 }
58
59 StateType::~StateType()
60 {
61   events_.clear();
62 }
63
64 void StateType::setEvent(double timestamp, Container* container, std::string value_name)
65 {
66   events_.push_back(new StateEvent(timestamp, container, this, PAJE_SetState, getEntityValue(value_name)));
67 }
68
69 void StateType::pushEvent(double timestamp, Container* container, std::string value_name, void* extra)
70 {
71   events_.push_back(new StateEvent(timestamp, container, this, PAJE_PushState, getEntityValue(value_name), extra));
72 }
73
74 void StateType::pushEvent(double timestamp, Container* container, std::string value_name)
75 {
76   events_.push_back(new StateEvent(timestamp, container, this, PAJE_PushState, getEntityValue(value_name)));
77 }
78
79 void StateType::popEvent(double timestamp, Container* container)
80 {
81   events_.push_back(new StateEvent(timestamp, container, this, PAJE_PopState, nullptr));
82 }
83
84 VariableType::VariableType(std::string name, std::string color, Type* father) : Type(name, name, color, father)
85 {
86   XBT_DEBUG("VariableType %s(%lld), child of %s(%lld)", getCname(), getId(), father->getCname(), father->getId());
87   logDefinition(PAJE_DefineVariableType);
88 }
89
90 VariableType::~VariableType()
91 {
92   events_.clear();
93 }
94
95 void VariableType::setEvent(double timestamp, Container* container, double value)
96 {
97   events_.push_back(new VariableEvent(timestamp, container, this, PAJE_SetVariable, value));
98 }
99
100 void VariableType::addEvent(double timestamp, Container* container, double value)
101 {
102   events_.push_back(new VariableEvent(timestamp, container, this, PAJE_AddVariable, value));
103 }
104
105 void VariableType::subEvent(double timestamp, Container* container, double value)
106 {
107   events_.push_back(new VariableEvent(timestamp, container, this, PAJE_SubVariable, value));
108 }
109
110 LinkType::LinkType(std::string name, std::string alias, Type* father) : ValueType(name, alias, father)
111 {
112 }
113
114 void Type::logDefinition(e_event_type event_type)
115 {
116   if (instr_fmt_type != instr_fmt_paje)
117     return;
118   std::stringstream stream;
119   XBT_DEBUG("%s: event_type=%u, timestamp=%.*f", __FUNCTION__, event_type, TRACE_precision(), 0.);
120   stream << std::fixed << std::setprecision(TRACE_precision()) << event_type << " " << getId();
121   stream << " " << father_->getId() << " " << getName();
122   if (isColored())
123     stream << " \"" << color_ << "\"";
124   XBT_DEBUG("Dump %s", stream.str().c_str());
125   stream << std::endl;
126   fprintf(tracing_file, "%s", stream.str().c_str());
127 }
128
129 void Type::logDefinition(simgrid::instr::Type* source, simgrid::instr::Type* dest)
130 {
131   if (instr_fmt_type != instr_fmt_paje)
132     return;
133   std::stringstream stream;
134   XBT_DEBUG("%s: event_type=%u, timestamp=%.*f", __FUNCTION__, PAJE_DefineLinkType, TRACE_precision(), 0.);
135   stream << std::fixed << std::setprecision(TRACE_precision()) << PAJE_DefineLinkType << " " << getId();
136   stream << " " << father_->getId() << " " << source->getId() << " " << dest->getId() << " " << getName();
137   XBT_DEBUG("Dump %s", stream.str().c_str());
138   stream << std::endl;
139   fprintf(tracing_file, "%s", stream.str().c_str());
140 }
141
142 Type* Type::byName(std::string name)
143 {
144   Type* ret = nullptr;
145   for (auto elm : children_) {
146     if (elm.second->name_ == name) {
147       if (ret != nullptr) {
148         THROWF (tracing_error, 0, "there are two children types with the same name?");
149       } else {
150         ret = elm.second;
151       }
152     }
153   }
154   if (ret == nullptr)
155     THROWF(tracing_error, 2, "type with name (%s) not found in father type (%s)", name.c_str(), getCname());
156   return ret;
157 }
158
159 void ValueType::addEntityValue(std::string name)
160 {
161   addEntityValue(name, "");
162 }
163
164 void ValueType::addEntityValue(std::string name, std::string color)
165 {
166   if (name.empty())
167     THROWF(tracing_error, 0, "can't get a value with no name");
168
169   auto it = values_.find(name);
170   if (it == values_.end()) {
171     EntityValue* new_val = new EntityValue(name, color, this);
172     values_.insert({name, new_val});
173     XBT_DEBUG("new value %s, child of %s", name.c_str(), getCname());
174     new_val->print();
175   }
176 }
177
178 EntityValue* ValueType::getEntityValue(std::string name)
179 {
180   auto ret = values_.find(name);
181   if (ret == values_.end()) {
182     THROWF(tracing_error, 2, "value with name (%s) not found in father type (%s)", name.c_str(), getCname());
183   }
184   return ret->second;
185 }
186
187 ContainerType* Type::createRootType()
188 {
189   rootType = new ContainerType("0");
190   return rootType;
191 }
192
193 ContainerType* Type::getRootType()
194 {
195   return rootType;
196 }
197
198 ContainerType* Type::getOrCreateContainerType(std::string name)
199 {
200   auto cont = children_.find(name);
201   return cont == children_.end() ? new ContainerType(name, this) : static_cast<ContainerType*>(cont->second);
202 }
203
204 EventType* Type::getOrCreateEventType(std::string name)
205 {
206   auto cont = children_.find(name);
207   return cont == children_.end() ? new EventType(name, this) : static_cast<EventType*>(cont->second);
208 }
209
210 StateType* Type::getOrCreateStateType(std::string name)
211 {
212   auto cont = children_.find(name);
213   return cont == children_.end() ? new StateType(name, this) : static_cast<StateType*>(cont->second);
214 }
215
216 VariableType* Type::getOrCreateVariableType(std::string name, std::string color)
217 {
218   auto cont = children_.find(name);
219   std::string mycolor = color.empty() ? "1 1 1" : color;
220   return cont == children_.end() ? new VariableType(name, mycolor, this) : static_cast<VariableType*>(cont->second);
221 }
222
223 LinkType* Type::getOrCreateLinkType(std::string name, Type* source, Type* dest)
224 {
225   std::string alias = name + "-" + std::to_string(source->id_) + "-" + std::to_string(dest->id_);
226   auto it           = children_.find(alias);
227   if (it == children_.end()) {
228     LinkType* ret = new LinkType(name, alias, this);
229     XBT_DEBUG("LinkType %s(%lld), child of %s(%lld)  %s(%lld)->%s(%lld)", ret->getCname(), ret->getId(), getCname(),
230               getId(), source->getCname(), source->getId(), dest->getCname(), dest->getId());
231     ret->logDefinition(source, dest);
232     return ret;
233   } else
234     return static_cast<LinkType*>(it->second);
235 }
236 }
237 }