Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3f6c5f467dcf9782a43453b7dd000c3f5dfc7778
[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 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->get_cname());
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)", get_cname(), get_id(), father->get_cname(), father->get_id());
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)", get_cname(), get_id(), father->get_cname(), father->get_id());
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)", get_cname(), get_id(), father->get_cname(), father->get_id());
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(issuer_, this, PAJE_SetState, getEntityValue(value_name), nullptr));
66 }
67
68 void StateType::pushEvent(std::string value_name, TIData* extra)
69 {
70   events_.push_back(new StateEvent(issuer_, this, PAJE_PushState, getEntityValue(value_name), extra));
71 }
72
73 void StateType::pushEvent(std::string value_name)
74 {
75   events_.push_back(new StateEvent(issuer_, this, PAJE_PushState, getEntityValue(value_name), nullptr));
76 }
77
78 void StateType::popEvent()
79 {
80   events_.push_back(new StateEvent(issuer_, this, PAJE_PopState, nullptr, nullptr));
81 }
82
83 void StateType::popEvent(TIData* extra)
84 {
85   events_.push_back(new StateEvent(issuer_, this, PAJE_PopState, nullptr, extra));
86 }
87
88 VariableType::VariableType(std::string name, std::string color, Type* father) : Type(name, name, color, father)
89 {
90   XBT_DEBUG("VariableType %s(%lld), child of %s(%lld)", get_cname(), get_id(), father->get_cname(), father->get_id());
91   logDefinition(PAJE_DefineVariableType);
92 }
93
94 VariableType::~VariableType()
95 {
96   events_.clear();
97 }
98
99 void VariableType::setEvent(double timestamp, double value)
100 {
101   events_.push_back(new VariableEvent(timestamp, issuer_, this, PAJE_SetVariable, value));
102 }
103
104 void VariableType::addEvent(double timestamp, double value)
105 {
106   events_.push_back(new VariableEvent(timestamp, issuer_, this, PAJE_AddVariable, value));
107 }
108
109 void VariableType::subEvent(double timestamp, double value)
110 {
111   events_.push_back(new VariableEvent(timestamp, issuer_, this, PAJE_SubVariable, value));
112 }
113
114 LinkType::LinkType(std::string name, std::string alias, Type* father) : ValueType(name, alias, father)
115 {
116 }
117 void LinkType::startEvent(Container* startContainer, std::string value, std::string key)
118 {
119   startEvent(startContainer, value, key, -1);
120 }
121
122 void LinkType::startEvent(Container* startContainer, std::string value, std::string key, int size)
123 {
124   new LinkEvent(issuer_, this, PAJE_StartLink, startContainer, value, key, size);
125 }
126
127 void LinkType::endEvent(Container* endContainer, std::string value, std::string key)
128 {
129   new LinkEvent(issuer_, this, PAJE_EndLink, endContainer, value, key, -1);
130 }
131
132 void Type::logDefinition(e_event_type event_type)
133 {
134   if (trace_format != simgrid::instr::TraceFormat::Paje)
135     return;
136   std::stringstream stream;
137   XBT_DEBUG("%s: event_type=%u, timestamp=%.*f", __func__, event_type, TRACE_precision(), 0.);
138   stream << std::fixed << std::setprecision(TRACE_precision()) << event_type << " " << get_id();
139   stream << " " << father_->get_id() << " " << get_name();
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 (trace_format != simgrid::instr::TraceFormat::Paje)
150     return;
151   std::stringstream stream;
152   XBT_DEBUG("%s: event_type=%u, timestamp=%.*f", __func__, PAJE_DefineLinkType, TRACE_precision(), 0.);
153   stream << std::fixed << std::setprecision(TRACE_precision()) << PAJE_DefineLinkType << " " << get_id();
154   stream << " " << father_->get_id() << " " << source->get_id() << " " << dest->get_id() << " " << get_name();
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(), get_cname());
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(), get_cname());
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(), get_cname());
201   }
202   return ret->second;
203 }
204
205 ContainerType* Type::getOrCreateContainerType(std::string name)
206 {
207   auto cont = children_.find(name);
208   return cont == children_.end() ? new ContainerType(name, this) : static_cast<ContainerType*>(cont->second);
209 }
210
211 EventType* Type::getOrCreateEventType(std::string name)
212 {
213   auto cont = children_.find(name);
214   return cont == children_.end() ? new EventType(name, this) : static_cast<EventType*>(cont->second);
215 }
216
217 StateType* Type::getOrCreateStateType(std::string name)
218 {
219   auto cont = children_.find(name);
220   return cont == children_.end() ? new StateType(name, this) : static_cast<StateType*>(cont->second);
221 }
222
223 VariableType* Type::getOrCreateVariableType(std::string name, std::string color)
224 {
225   auto cont = children_.find(name);
226   std::string mycolor = color.empty() ? "1 1 1" : color;
227   return cont == children_.end() ? new VariableType(name, mycolor, this) : static_cast<VariableType*>(cont->second);
228 }
229
230 LinkType* Type::getOrCreateLinkType(std::string name, Type* source, Type* dest)
231 {
232   std::string alias = name + "-" + std::to_string(source->id_) + "-" + std::to_string(dest->id_);
233   auto it           = children_.find(alias);
234   if (it == children_.end()) {
235     LinkType* ret = new LinkType(name, alias, this);
236     XBT_DEBUG("LinkType %s(%lld), child of %s(%lld)  %s(%lld)->%s(%lld)", ret->get_cname(), ret->get_id(), get_cname(),
237               get_id(), source->get_cname(), source->get_id(), dest->get_cname(), dest->get_id());
238     ret->logDefinition(source, dest);
239     return ret;
240   } else
241     return static_cast<LinkType*>(it->second);
242 }
243 }
244 }