1 /* Copyright (c) 2012-2018. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "src/instr/instr_private.hpp"
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_paje_types, instr, "Paje tracing event system (types)");
11 extern FILE* tracing_file;
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)
19 if (name.empty() || alias.empty())
20 THROWF(tracing_error, 0, "can't create a new type with no name or alias");
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());
30 for (auto elm : children_)
34 ValueType::~ValueType()
36 for (auto elm : values_)
40 ContainerType::ContainerType(std::string name, Type* father) : Type(name, name, "", father)
42 XBT_DEBUG("ContainerType %s(%lld), child of %s(%lld)", get_cname(), get_id(), father->get_cname(), father->get_id());
43 logDefinition(PAJE_DefineContainerType);
46 EventType::EventType(std::string name, Type* father) : ValueType(name, father)
48 XBT_DEBUG("EventType %s(%lld), child of %s(%lld)", get_cname(), get_id(), father->get_cname(), father->get_id());
49 logDefinition(PAJE_DefineEventType);
52 StateType::StateType(std::string name, Type* father) : ValueType(name, father)
54 XBT_DEBUG("StateType %s(%lld), child of %s(%lld)", get_cname(), get_id(), father->get_cname(), father->get_id());
55 logDefinition(PAJE_DefineStateType);
58 StateType::~StateType()
63 void StateType::setEvent(std::string value_name)
65 events_.push_back(new StateEvent(issuer_, this, PAJE_SetState, getEntityValue(value_name), nullptr));
68 void StateType::pushEvent(std::string value_name, TIData* extra)
70 events_.push_back(new StateEvent(issuer_, this, PAJE_PushState, getEntityValue(value_name), extra));
73 void StateType::pushEvent(std::string value_name)
75 events_.push_back(new StateEvent(issuer_, this, PAJE_PushState, getEntityValue(value_name), nullptr));
78 void StateType::popEvent()
80 events_.push_back(new StateEvent(issuer_, this, PAJE_PopState, nullptr, nullptr));
83 void StateType::popEvent(TIData* extra)
85 events_.push_back(new StateEvent(issuer_, this, PAJE_PopState, nullptr, extra));
88 VariableType::VariableType(std::string name, std::string color, Type* father) : Type(name, name, color, father)
90 XBT_DEBUG("VariableType %s(%lld), child of %s(%lld)", get_cname(), get_id(), father->get_cname(), father->get_id());
91 logDefinition(PAJE_DefineVariableType);
94 VariableType::~VariableType()
99 void VariableType::setEvent(double timestamp, double value)
101 events_.push_back(new VariableEvent(timestamp, issuer_, this, PAJE_SetVariable, value));
104 void VariableType::addEvent(double timestamp, double value)
106 events_.push_back(new VariableEvent(timestamp, issuer_, this, PAJE_AddVariable, value));
109 void VariableType::subEvent(double timestamp, double value)
111 events_.push_back(new VariableEvent(timestamp, issuer_, this, PAJE_SubVariable, value));
114 LinkType::LinkType(std::string name, std::string alias, Type* father) : ValueType(name, alias, father)
117 void LinkType::startEvent(Container* startContainer, std::string value, std::string key)
119 startEvent(startContainer, value, key, -1);
122 void LinkType::startEvent(Container* startContainer, std::string value, std::string key, int size)
124 new LinkEvent(issuer_, this, PAJE_StartLink, startContainer, value, key, size);
127 void LinkType::endEvent(Container* endContainer, std::string value, std::string key)
129 new LinkEvent(issuer_, this, PAJE_EndLink, endContainer, value, key, -1);
132 void Type::logDefinition(e_event_type event_type)
134 if (trace_format != simgrid::instr::TraceFormat::Paje)
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();
141 stream << " \"" << color_ << "\"";
142 XBT_DEBUG("Dump %s", stream.str().c_str());
144 fprintf(tracing_file, "%s", stream.str().c_str());
147 void Type::logDefinition(simgrid::instr::Type* source, simgrid::instr::Type* dest)
149 if (trace_format != simgrid::instr::TraceFormat::Paje)
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());
157 fprintf(tracing_file, "%s", stream.str().c_str());
160 Type* Type::byName(std::string name)
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?");
173 THROWF(tracing_error, 2, "type with name (%s) not found in father type (%s)", name.c_str(), get_cname());
177 void ValueType::addEntityValue(std::string name)
179 addEntityValue(name, "");
182 void ValueType::addEntityValue(std::string name, std::string color)
185 THROWF(tracing_error, 0, "can't get a value with no name");
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());
196 EntityValue* ValueType::getEntityValue(std::string name)
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());
205 ContainerType* Type::getOrCreateContainerType(std::string name)
207 auto cont = children_.find(name);
208 return cont == children_.end() ? new ContainerType(name, this) : static_cast<ContainerType*>(cont->second);
211 EventType* Type::getOrCreateEventType(std::string name)
213 auto cont = children_.find(name);
214 return cont == children_.end() ? new EventType(name, this) : static_cast<EventType*>(cont->second);
217 StateType* Type::getOrCreateStateType(std::string name)
219 auto cont = children_.find(name);
220 return cont == children_.end() ? new StateType(name, this) : static_cast<StateType*>(cont->second);
223 VariableType* Type::getOrCreateVariableType(std::string name, std::string color)
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);
230 LinkType* Type::getOrCreateLinkType(std::string name, Type* source, Type* dest)
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);
241 return static_cast<LinkType*>(it->second);