Logo AND Algorithmique Numérique Distribuée

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