1 /* Copyright (c) 2012, 2014-2017. 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->getCname());
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)", getCname(), getId(), father->getCname(), father->getId());
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)", getCname(), getId(), father->getCname(), father->getId());
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)", getCname(), getId(), father->getCname(), father->getId());
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)));
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)));
78 void StateType::popEvent()
80 events_.push_back(new StateEvent(issuer_, this, PAJE_PopState, nullptr));
83 VariableType::VariableType(std::string name, std::string color, Type* father) : Type(name, name, color, father)
85 XBT_DEBUG("VariableType %s(%lld), child of %s(%lld)", getCname(), getId(), father->getCname(), father->getId());
86 logDefinition(PAJE_DefineVariableType);
89 VariableType::~VariableType()
94 void VariableType::setEvent(double timestamp, double value)
96 events_.push_back(new VariableEvent(timestamp, issuer_, this, PAJE_SetVariable, value));
99 void VariableType::addEvent(double timestamp, double value)
101 events_.push_back(new VariableEvent(timestamp, issuer_, this, PAJE_AddVariable, value));
104 void VariableType::subEvent(double timestamp, double value)
106 events_.push_back(new VariableEvent(timestamp, issuer_, this, PAJE_SubVariable, value));
109 LinkType::LinkType(std::string name, std::string alias, Type* father) : ValueType(name, alias, father)
112 void LinkType::startEvent(container_t startContainer, std::string value, std::string key)
114 startEvent(startContainer, value, key, -1);
117 void LinkType::startEvent(container_t startContainer, std::string value, std::string key, int size)
119 new LinkEvent(issuer_, this, PAJE_StartLink, startContainer, value, key, size);
122 void LinkType::endEvent(container_t endContainer, std::string value, std::string key)
124 new LinkEvent(issuer_, this, PAJE_EndLink, endContainer, value, key);
127 void Type::logDefinition(e_event_type event_type)
129 if (instr_fmt_type != instr_fmt_paje)
131 std::stringstream stream;
132 XBT_DEBUG("%s: event_type=%u, timestamp=%.*f", __FUNCTION__, event_type, TRACE_precision(), 0.);
133 stream << std::fixed << std::setprecision(TRACE_precision()) << event_type << " " << getId();
134 stream << " " << father_->getId() << " " << getName();
136 stream << " \"" << color_ << "\"";
137 XBT_DEBUG("Dump %s", stream.str().c_str());
139 fprintf(tracing_file, "%s", stream.str().c_str());
142 void Type::logDefinition(simgrid::instr::Type* source, simgrid::instr::Type* dest)
144 if (instr_fmt_type != instr_fmt_paje)
146 std::stringstream stream;
147 XBT_DEBUG("%s: event_type=%u, timestamp=%.*f", __FUNCTION__, PAJE_DefineLinkType, TRACE_precision(), 0.);
148 stream << std::fixed << std::setprecision(TRACE_precision()) << PAJE_DefineLinkType << " " << getId();
149 stream << " " << father_->getId() << " " << source->getId() << " " << dest->getId() << " " << getName();
150 XBT_DEBUG("Dump %s", stream.str().c_str());
152 fprintf(tracing_file, "%s", stream.str().c_str());
155 Type* Type::byName(std::string name)
158 for (auto elm : children_) {
159 if (elm.second->name_ == name) {
160 if (ret != nullptr) {
161 THROWF (tracing_error, 0, "there are two children types with the same name?");
168 THROWF(tracing_error, 2, "type with name (%s) not found in father type (%s)", name.c_str(), getCname());
172 void ValueType::addEntityValue(std::string name)
174 addEntityValue(name, "");
177 void ValueType::addEntityValue(std::string name, std::string color)
180 THROWF(tracing_error, 0, "can't get a value with no name");
182 auto it = values_.find(name);
183 if (it == values_.end()) {
184 EntityValue* new_val = new EntityValue(name, color, this);
185 values_.insert({name, new_val});
186 XBT_DEBUG("new value %s, child of %s", name.c_str(), getCname());
191 EntityValue* ValueType::getEntityValue(std::string name)
193 auto ret = values_.find(name);
194 if (ret == values_.end()) {
195 THROWF(tracing_error, 2, "value with name (%s) not found in father type (%s)", name.c_str(), getCname());
200 ContainerType* Type::getOrCreateContainerType(std::string name)
202 auto cont = children_.find(name);
203 return cont == children_.end() ? new ContainerType(name, this) : static_cast<ContainerType*>(cont->second);
206 EventType* Type::getOrCreateEventType(std::string name)
208 auto cont = children_.find(name);
209 return cont == children_.end() ? new EventType(name, this) : static_cast<EventType*>(cont->second);
212 StateType* Type::getOrCreateStateType(std::string name)
214 auto cont = children_.find(name);
215 return cont == children_.end() ? new StateType(name, this) : static_cast<StateType*>(cont->second);
218 VariableType* Type::getOrCreateVariableType(std::string name, std::string color)
220 auto cont = children_.find(name);
221 std::string mycolor = color.empty() ? "1 1 1" : color;
222 return cont == children_.end() ? new VariableType(name, mycolor, this) : static_cast<VariableType*>(cont->second);
225 LinkType* Type::getOrCreateLinkType(std::string name, Type* source, Type* dest)
227 std::string alias = name + "-" + std::to_string(source->id_) + "-" + std::to_string(dest->id_);
228 auto it = children_.find(alias);
229 if (it == children_.end()) {
230 LinkType* ret = new LinkType(name, alias, this);
231 XBT_DEBUG("LinkType %s(%lld), child of %s(%lld) %s(%lld)->%s(%lld)", ret->getCname(), ret->getId(), getCname(),
232 getId(), source->getCname(), source->getId(), dest->getCname(), dest->getId());
233 ret->logDefinition(source, dest);
236 return static_cast<LinkType*>(it->second);