Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
e9bd24f3785cfaa2bda0b5553128242526d80b5d
[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(std::string value_name)
65 {
66   events_.push_back(new StateEvent(SIMIX_get_clock(), issuer_, this, PAJE_SetState, getEntityValue(value_name)));
67 }
68
69 void StateType::pushEvent(std::string value_name, void* extra)
70 {
71   events_.push_back(
72       new StateEvent(SIMIX_get_clock(), issuer_, this, PAJE_PushState, getEntityValue(value_name), extra));
73 }
74
75 void StateType::pushEvent(std::string value_name)
76 {
77   events_.push_back(new StateEvent(SIMIX_get_clock(), issuer_, this, PAJE_PushState, getEntityValue(value_name)));
78 }
79
80 void StateType::popEvent()
81 {
82   events_.push_back(new StateEvent(SIMIX_get_clock(), issuer_, this, PAJE_PopState, nullptr));
83 }
84
85 VariableType::VariableType(std::string name, std::string color, Type* father) : Type(name, name, color, father)
86 {
87   XBT_DEBUG("VariableType %s(%lld), child of %s(%lld)", getCname(), getId(), father->getCname(), father->getId());
88   logDefinition(PAJE_DefineVariableType);
89 }
90
91 VariableType::~VariableType()
92 {
93   events_.clear();
94 }
95
96 void VariableType::setEvent(double timestamp, Container* container, double value)
97 {
98   events_.push_back(new VariableEvent(timestamp, container, this, PAJE_SetVariable, value));
99 }
100
101 void VariableType::addEvent(double timestamp, Container* container, double value)
102 {
103   events_.push_back(new VariableEvent(timestamp, container, this, PAJE_AddVariable, value));
104 }
105
106 void VariableType::subEvent(double timestamp, Container* container, double value)
107 {
108   events_.push_back(new VariableEvent(timestamp, container, this, PAJE_SubVariable, value));
109 }
110
111 LinkType::LinkType(std::string name, std::string alias, Type* father) : ValueType(name, alias, father)
112 {
113 }
114 void LinkType::startEvent(double timestamp, Container* container, container_t endpoint, std::string value,
115                           std::string key)
116 {
117   startEvent(timestamp, container, endpoint, value, key, -1);
118 }
119
120 void LinkType::startEvent(double timestamp, Container* container, container_t endpoint, std::string value,
121                           std::string key, int size)
122 {
123   new LinkEvent(timestamp, container, this, PAJE_StartLink, endpoint, value, key, size);
124 }
125
126 void LinkType::endEvent(double timestamp, Container* container, container_t endpoint, std::string value,
127                         std::string key)
128 {
129   new LinkEvent(timestamp, container, this, PAJE_EndLink, endpoint, value, key);
130 }
131
132 void Type::logDefinition(e_event_type event_type)
133 {
134   if (instr_fmt_type != instr_fmt_paje)
135     return;
136   std::stringstream stream;
137   XBT_DEBUG("%s: event_type=%u, timestamp=%.*f", __FUNCTION__, event_type, TRACE_precision(), 0.);
138   stream << std::fixed << std::setprecision(TRACE_precision()) << event_type << " " << getId();
139   stream << " " << father_->getId() << " " << getName();
140   if (isColored())
141     stream << " \"" << color_ << "\"";
142   XBT_DEBUG("Dump %s", stream.str().c_str());
143   stream << std::endl;
144   fprintf(tracing_file, "%s", stream.str().c_str());
145 }
146
147 void Type::logDefinition(simgrid::instr::Type* source, simgrid::instr::Type* dest)
148 {
149   if (instr_fmt_type != instr_fmt_paje)
150     return;
151   std::stringstream stream;
152   XBT_DEBUG("%s: event_type=%u, timestamp=%.*f", __FUNCTION__, PAJE_DefineLinkType, TRACE_precision(), 0.);
153   stream << std::fixed << std::setprecision(TRACE_precision()) << PAJE_DefineLinkType << " " << getId();
154   stream << " " << father_->getId() << " " << source->getId() << " " << dest->getId() << " " << getName();
155   XBT_DEBUG("Dump %s", stream.str().c_str());
156   stream << std::endl;
157   fprintf(tracing_file, "%s", stream.str().c_str());
158 }
159
160 Type* Type::byName(std::string name)
161 {
162   Type* ret = nullptr;
163   for (auto elm : children_) {
164     if (elm.second->name_ == name) {
165       if (ret != nullptr) {
166         THROWF (tracing_error, 0, "there are two children types with the same name?");
167       } else {
168         ret = elm.second;
169       }
170     }
171   }
172   if (ret == nullptr)
173     THROWF(tracing_error, 2, "type with name (%s) not found in father type (%s)", name.c_str(), getCname());
174   return ret;
175 }
176
177 void ValueType::addEntityValue(std::string name)
178 {
179   addEntityValue(name, "");
180 }
181
182 void ValueType::addEntityValue(std::string name, std::string color)
183 {
184   if (name.empty())
185     THROWF(tracing_error, 0, "can't get a value with no name");
186
187   auto it = values_.find(name);
188   if (it == values_.end()) {
189     EntityValue* new_val = new EntityValue(name, color, this);
190     values_.insert({name, new_val});
191     XBT_DEBUG("new value %s, child of %s", name.c_str(), getCname());
192     new_val->print();
193   }
194 }
195
196 EntityValue* ValueType::getEntityValue(std::string name)
197 {
198   auto ret = values_.find(name);
199   if (ret == values_.end()) {
200     THROWF(tracing_error, 2, "value with name (%s) not found in father type (%s)", name.c_str(), getCname());
201   }
202   return ret->second;
203 }
204
205 ContainerType* Type::createRootType()
206 {
207   rootType = new ContainerType("0");
208   return rootType;
209 }
210
211 ContainerType* Type::getRootType()
212 {
213   return rootType;
214 }
215
216 ContainerType* Type::getOrCreateContainerType(std::string name)
217 {
218   auto cont = children_.find(name);
219   return cont == children_.end() ? new ContainerType(name, this) : static_cast<ContainerType*>(cont->second);
220 }
221
222 EventType* Type::getOrCreateEventType(std::string name)
223 {
224   auto cont = children_.find(name);
225   return cont == children_.end() ? new EventType(name, this) : static_cast<EventType*>(cont->second);
226 }
227
228 StateType* Type::getOrCreateStateType(std::string name)
229 {
230   auto cont = children_.find(name);
231   return cont == children_.end() ? new StateType(name, this) : static_cast<StateType*>(cont->second);
232 }
233
234 VariableType* Type::getOrCreateVariableType(std::string name, std::string color)
235 {
236   auto cont = children_.find(name);
237   std::string mycolor = color.empty() ? "1 1 1" : color;
238   return cont == children_.end() ? new VariableType(name, mycolor, this) : static_cast<VariableType*>(cont->second);
239 }
240
241 LinkType* Type::getOrCreateLinkType(std::string name, Type* source, Type* dest)
242 {
243   std::string alias = name + "-" + std::to_string(source->id_) + "-" + std::to_string(dest->id_);
244   auto it           = children_.find(alias);
245   if (it == children_.end()) {
246     LinkType* ret = new LinkType(name, alias, this);
247     XBT_DEBUG("LinkType %s(%lld), child of %s(%lld)  %s(%lld)->%s(%lld)", ret->getCname(), ret->getId(), getCname(),
248               getId(), source->getCname(), source->getId(), dest->getCname(), dest->getId());
249     ret->logDefinition(source, dest);
250     return ret;
251   } else
252     return static_cast<LinkType*>(it->second);
253 }
254 }
255 }