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 std::ofstream 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());
26 if (trace_format == simgrid::instr::TraceFormat::Paje) {
27 stream_ << std::fixed << std::setprecision(TRACE_precision());
33 for (auto elm : children_)
37 ValueType::~ValueType()
39 for (auto elm : values_)
43 ContainerType::ContainerType(std::string name, Type* father) : Type(name, name, "", father)
45 XBT_DEBUG("ContainerType %s(%lld), child of %s(%lld)", get_cname(), get_id(), father->get_cname(), father->get_id());
46 logDefinition(PAJE_DefineContainerType);
49 EventType::EventType(std::string name, Type* father) : ValueType(name, father)
51 XBT_DEBUG("EventType %s(%lld), child of %s(%lld)", get_cname(), get_id(), father->get_cname(), father->get_id());
52 logDefinition(PAJE_DefineEventType);
55 StateType::StateType(std::string name, Type* father) : ValueType(name, father)
57 XBT_DEBUG("StateType %s(%lld), child of %s(%lld)", get_cname(), get_id(), father->get_cname(), father->get_id());
58 logDefinition(PAJE_DefineStateType);
61 StateType::~StateType()
66 void StateType::setEvent(std::string value_name)
68 events_.push_back(new StateEvent(issuer_, this, PAJE_SetState, getEntityValue(value_name), nullptr));
71 void StateType::pushEvent(std::string value_name, TIData* extra)
73 events_.push_back(new StateEvent(issuer_, this, PAJE_PushState, getEntityValue(value_name), extra));
76 void StateType::pushEvent(std::string value_name)
78 events_.push_back(new StateEvent(issuer_, this, PAJE_PushState, getEntityValue(value_name), nullptr));
81 void StateType::popEvent()
83 events_.push_back(new StateEvent(issuer_, this, PAJE_PopState, nullptr, nullptr));
86 void StateType::popEvent(TIData* extra)
88 events_.push_back(new StateEvent(issuer_, this, PAJE_PopState, nullptr, extra));
91 VariableType::VariableType(std::string name, std::string color, Type* father) : Type(name, name, color, father)
93 XBT_DEBUG("VariableType %s(%lld), child of %s(%lld)", get_cname(), get_id(), father->get_cname(), father->get_id());
94 logDefinition(PAJE_DefineVariableType);
97 VariableType::~VariableType()
102 void VariableType::setEvent(double timestamp, double value)
104 events_.push_back(new VariableEvent(timestamp, issuer_, this, PAJE_SetVariable, value));
107 void VariableType::addEvent(double timestamp, double value)
109 events_.push_back(new VariableEvent(timestamp, issuer_, this, PAJE_AddVariable, value));
112 void VariableType::subEvent(double timestamp, double value)
114 events_.push_back(new VariableEvent(timestamp, issuer_, this, PAJE_SubVariable, value));
117 LinkType::LinkType(std::string name, std::string alias, Type* father) : ValueType(name, alias, father)
120 void LinkType::startEvent(Container* startContainer, std::string value, std::string key)
122 startEvent(startContainer, value, key, -1);
125 void LinkType::startEvent(Container* startContainer, std::string value, std::string key, int size)
127 new LinkEvent(issuer_, this, PAJE_StartLink, startContainer, value, key, size);
130 void LinkType::endEvent(Container* endContainer, std::string value, std::string key)
132 new LinkEvent(issuer_, this, PAJE_EndLink, endContainer, value, key, -1);
135 void Type::logDefinition(e_event_type event_type)
137 if (trace_format != simgrid::instr::TraceFormat::Paje)
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();
142 stream_ << " \"" << color_ << "\"";
143 XBT_DEBUG("Dump %s", stream_.str().c_str());
144 tracing_file << stream_.str() << std::endl;
147 void Type::logDefinition(simgrid::instr::Type* source, simgrid::instr::Type* dest)
149 if (trace_format != simgrid::instr::TraceFormat::Paje)
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;
158 Type* Type::byName(std::string name)
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?");
171 THROWF(tracing_error, 2, "type with name (%s) not found in father type (%s)", name.c_str(), get_cname());
175 void ValueType::addEntityValue(std::string name)
177 addEntityValue(name, "");
180 void ValueType::addEntityValue(std::string name, std::string color)
183 THROWF(tracing_error, 0, "can't get a value with no name");
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());
194 EntityValue* ValueType::getEntityValue(std::string name)
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());
203 ContainerType* Type::getOrCreateContainerType(std::string name)
205 auto cont = children_.find(name);
206 return cont == children_.end() ? new ContainerType(name, this) : static_cast<ContainerType*>(cont->second);
209 EventType* Type::getOrCreateEventType(std::string name)
211 auto cont = children_.find(name);
212 return cont == children_.end() ? new EventType(name, this) : static_cast<EventType*>(cont->second);
215 StateType* Type::getOrCreateStateType(std::string name)
217 auto cont = children_.find(name);
218 return cont == children_.end() ? new StateType(name, this) : static_cast<StateType*>(cont->second);
221 VariableType* Type::getOrCreateVariableType(std::string name, std::string color)
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);
228 LinkType* Type::getOrCreateLinkType(std::string name, Type* source, Type* dest)
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);
239 return static_cast<LinkType*>(it->second);