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(SIMIX_get_clock(), issuer_, this, PAJE_SetState, getEntityValue(value_name)));
68 void StateType::pushEvent(std::string value_name, void* extra)
71 new StateEvent(SIMIX_get_clock(), issuer_, this, PAJE_PushState, getEntityValue(value_name), extra));
74 void StateType::pushEvent(std::string value_name)
76 events_.push_back(new StateEvent(SIMIX_get_clock(), issuer_, this, PAJE_PushState, getEntityValue(value_name)));
79 void StateType::popEvent()
81 events_.push_back(new StateEvent(SIMIX_get_clock(), issuer_, this, PAJE_PopState, nullptr));
84 VariableType::VariableType(std::string name, std::string color, Type* father) : Type(name, name, color, father)
86 XBT_DEBUG("VariableType %s(%lld), child of %s(%lld)", getCname(), getId(), father->getCname(), father->getId());
87 logDefinition(PAJE_DefineVariableType);
90 VariableType::~VariableType()
95 void VariableType::setEvent(double timestamp, double value)
97 events_.push_back(new VariableEvent(timestamp, issuer_, this, PAJE_SetVariable, value));
100 void VariableType::addEvent(double timestamp, double value)
102 events_.push_back(new VariableEvent(timestamp, issuer_, this, PAJE_AddVariable, value));
105 void VariableType::subEvent(double timestamp, double value)
107 events_.push_back(new VariableEvent(timestamp, issuer_, this, PAJE_SubVariable, value));
110 LinkType::LinkType(std::string name, std::string alias, Type* father) : ValueType(name, alias, father)
113 void LinkType::startEvent(container_t startContainer, std::string value, std::string key)
115 startEvent(startContainer, value, key, -1);
118 void LinkType::startEvent(container_t startContainer, std::string value, std::string key, int size)
120 new LinkEvent(SIMIX_get_clock(), issuer_, this, PAJE_StartLink, startContainer, value, key, size);
123 void LinkType::endEvent(container_t endContainer, std::string value, std::string key)
125 new LinkEvent(SIMIX_get_clock(), issuer_, this, PAJE_EndLink, endContainer, value, key);
128 void Type::logDefinition(e_event_type event_type)
130 if (instr_fmt_type != instr_fmt_paje)
132 std::stringstream stream;
133 XBT_DEBUG("%s: event_type=%u, timestamp=%.*f", __FUNCTION__, event_type, TRACE_precision(), 0.);
134 stream << std::fixed << std::setprecision(TRACE_precision()) << event_type << " " << getId();
135 stream << " " << father_->getId() << " " << getName();
137 stream << " \"" << color_ << "\"";
138 XBT_DEBUG("Dump %s", stream.str().c_str());
140 fprintf(tracing_file, "%s", stream.str().c_str());
143 void Type::logDefinition(simgrid::instr::Type* source, simgrid::instr::Type* dest)
145 if (instr_fmt_type != instr_fmt_paje)
147 std::stringstream stream;
148 XBT_DEBUG("%s: event_type=%u, timestamp=%.*f", __FUNCTION__, PAJE_DefineLinkType, TRACE_precision(), 0.);
149 stream << std::fixed << std::setprecision(TRACE_precision()) << PAJE_DefineLinkType << " " << getId();
150 stream << " " << father_->getId() << " " << source->getId() << " " << dest->getId() << " " << getName();
151 XBT_DEBUG("Dump %s", stream.str().c_str());
153 fprintf(tracing_file, "%s", stream.str().c_str());
156 Type* Type::byName(std::string name)
159 for (auto elm : children_) {
160 if (elm.second->name_ == name) {
161 if (ret != nullptr) {
162 THROWF (tracing_error, 0, "there are two children types with the same name?");
169 THROWF(tracing_error, 2, "type with name (%s) not found in father type (%s)", name.c_str(), getCname());
173 void ValueType::addEntityValue(std::string name)
175 addEntityValue(name, "");
178 void ValueType::addEntityValue(std::string name, std::string color)
181 THROWF(tracing_error, 0, "can't get a value with no name");
183 auto it = values_.find(name);
184 if (it == values_.end()) {
185 EntityValue* new_val = new EntityValue(name, color, this);
186 values_.insert({name, new_val});
187 XBT_DEBUG("new value %s, child of %s", name.c_str(), getCname());
192 EntityValue* ValueType::getEntityValue(std::string name)
194 auto ret = values_.find(name);
195 if (ret == values_.end()) {
196 THROWF(tracing_error, 2, "value with name (%s) not found in father type (%s)", name.c_str(), getCname());
201 ContainerType* Type::getOrCreateContainerType(std::string name)
203 auto cont = children_.find(name);
204 return cont == children_.end() ? new ContainerType(name, this) : static_cast<ContainerType*>(cont->second);
207 EventType* Type::getOrCreateEventType(std::string name)
209 auto cont = children_.find(name);
210 return cont == children_.end() ? new EventType(name, this) : static_cast<EventType*>(cont->second);
213 StateType* Type::getOrCreateStateType(std::string name)
215 auto cont = children_.find(name);
216 return cont == children_.end() ? new StateType(name, this) : static_cast<StateType*>(cont->second);
219 VariableType* Type::getOrCreateVariableType(std::string name, std::string color)
221 auto cont = children_.find(name);
222 std::string mycolor = color.empty() ? "1 1 1" : color;
223 return cont == children_.end() ? new VariableType(name, mycolor, this) : static_cast<VariableType*>(cont->second);
226 LinkType* Type::getOrCreateLinkType(std::string name, Type* source, Type* dest)
228 std::string alias = name + "-" + std::to_string(source->id_) + "-" + std::to_string(dest->id_);
229 auto it = children_.find(alias);
230 if (it == children_.end()) {
231 LinkType* ret = new LinkType(name, alias, this);
232 XBT_DEBUG("LinkType %s(%lld), child of %s(%lld) %s(%lld)->%s(%lld)", ret->getCname(), ret->getId(), getCname(),
233 getId(), source->getCname(), source->getId(), dest->getCname(), dest->getId());
234 ret->logDefinition(source, dest);
237 return static_cast<LinkType*>(it->second);