Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
91ab9374ce66195741353eb326603ab6b7de4e73
[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 VariableType::VariableType(std::string name, std::string color, Type* father) : Type(name, name, color, father)
60 {
61   XBT_DEBUG("VariableType %s(%lld), child of %s(%lld)", getCname(), getId(), father->getCname(), father->getId());
62   logDefinition(PAJE_DefineVariableType);
63 }
64
65 LinkType::LinkType(std::string name, std::string alias, Type* father) : ValueType(name, alias, father)
66 {
67 }
68
69 void Type::logDefinition(e_event_type event_type)
70 {
71   if (instr_fmt_type != instr_fmt_paje)
72     return;
73   std::stringstream stream;
74   XBT_DEBUG("%s: event_type=%u, timestamp=%.*f", __FUNCTION__, event_type, TRACE_precision(), 0.);
75   stream << std::fixed << std::setprecision(TRACE_precision()) << event_type << " " << getId();
76   stream << " " << father_->getId() << " " << getName();
77   if (isColored())
78     stream << " \"" << color_ << "\"";
79   XBT_DEBUG("Dump %s", stream.str().c_str());
80   stream << std::endl;
81   fprintf(tracing_file, "%s", stream.str().c_str());
82 }
83
84 void Type::logDefinition(simgrid::instr::Type* source, simgrid::instr::Type* dest)
85 {
86   if (instr_fmt_type != instr_fmt_paje)
87     return;
88   std::stringstream stream;
89   XBT_DEBUG("%s: event_type=%u, timestamp=%.*f", __FUNCTION__, PAJE_DefineLinkType, TRACE_precision(), 0.);
90   stream << std::fixed << std::setprecision(TRACE_precision()) << PAJE_DefineLinkType << " " << getId();
91   stream << " " << father_->getId() << " " << source->getId() << " " << dest->getId() << " " << getName();
92   XBT_DEBUG("Dump %s", stream.str().c_str());
93   stream << std::endl;
94   fprintf(tracing_file, "%s", stream.str().c_str());
95 }
96
97 Type* Type::byName(std::string name)
98 {
99   Type* ret = nullptr;
100   for (auto elm : children_) {
101     if (elm.second->name_ == name) {
102       if (ret != nullptr) {
103         THROWF (tracing_error, 0, "there are two children types with the same name?");
104       } else {
105         ret = elm.second;
106       }
107     }
108   }
109   if (ret == nullptr)
110     THROWF(tracing_error, 2, "type with name (%s) not found in father type (%s)", name.c_str(), getCname());
111   return ret;
112 }
113
114 void ValueType::addEntityValue(std::string name)
115 {
116   addEntityValue(name, "");
117 }
118
119 void ValueType::addEntityValue(std::string name, std::string color)
120 {
121   if (name.empty())
122     THROWF(tracing_error, 0, "can't get a value with no name");
123
124   auto it = values_.find(name);
125   if (it == values_.end()) {
126     EntityValue* new_val = new EntityValue(name, color, this);
127     values_.insert({name, new_val});
128     XBT_DEBUG("new value %s, child of %s", name.c_str(), getCname());
129     new_val->print();
130   }
131 }
132
133 EntityValue* ValueType::getEntityValue(std::string name)
134 {
135   auto ret = values_.find(name);
136   if (ret == values_.end()) {
137     THROWF(tracing_error, 2, "value with name (%s) not found in father type (%s)", name.c_str(), getCname());
138   }
139   return ret->second;
140 }
141
142 ContainerType* Type::createRootType()
143 {
144   rootType = static_cast<ContainerType*>(new simgrid::instr::Type("0", "0", "", nullptr));
145   return rootType;
146 }
147
148 ContainerType* Type::getRootType()
149 {
150   return rootType;
151 }
152
153 ContainerType* Type::getOrCreateContainerType(std::string name)
154 {
155   auto cont = children_.find(name);
156   return cont == children_.end() ? new ContainerType(name, this) : static_cast<ContainerType*>(cont->second);
157 }
158
159 EventType* Type::getOrCreateEventType(std::string name)
160 {
161   auto cont = children_.find(name);
162   return cont == children_.end() ? new EventType(name, this) : static_cast<EventType*>(cont->second);
163 }
164
165 StateType* Type::getOrCreateStateType(std::string name)
166 {
167   auto cont = children_.find(name);
168   return cont == children_.end() ? new StateType(name, this) : static_cast<StateType*>(cont->second);
169 }
170
171 VariableType* Type::getOrCreateVariableType(std::string name, std::string color)
172 {
173   auto cont = children_.find(name);
174   std::string mycolor = color.empty() ? "1 1 1" : color;
175   return cont == children_.end() ? new VariableType(name, mycolor, this) : static_cast<VariableType*>(cont->second);
176 }
177
178 LinkType* Type::getOrCreateLinkType(std::string name, Type* source, Type* dest)
179 {
180   std::string alias = name + "-" + std::to_string(source->id_) + "-" + std::to_string(dest->id_);
181   auto it           = children_.find(alias);
182   if (it == children_.end()) {
183     LinkType* ret = new LinkType(name, alias, this);
184     XBT_DEBUG("LinkType %s(%lld), child of %s(%lld)  %s(%lld)->%s(%lld)", ret->getCname(), ret->getId(), getCname(),
185               getId(), source->getCname(), source->getId(), dest->getCname(), dest->getId());
186     ret->logDefinition(source, dest);
187     return ret;
188   } else
189     return static_cast<LinkType*>(it->second);
190 }
191 }
192 }