Logo AND Algorithmique Numérique Distribuée

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