1 /* Copyright (c) 2010-2017. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "simgrid/s4u/Engine.hpp"
7 #include "simgrid/s4u/Host.hpp"
9 #include "src/instr/instr_private.hpp"
11 #include <unordered_map>
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_paje_containers, instr, "Paje tracing event system (containers)");
15 static container_t rootContainer = nullptr; /* the root container */
16 static std::unordered_map<std::string, container_t> allContainers; /* all created containers indexed by name */
17 std::set<std::string> trivaNodeTypes; /* all host types defined */
18 std::set<std::string> trivaEdgeTypes; /* all link types defined */
20 long long int instr_new_paje_id ()
22 static long long int type_id = 0;
26 container_t PJ_container_get_root()
31 void PJ_container_set_root (container_t root)
39 Container::Container(std::string name, e_container_types kind, Container* father)
40 : name_(name), kind_(kind), father_(father)
42 static long long int container_id = 0;
43 id_ = std::to_string(container_id); // id (or alias) of the container
46 //Search for network_element_t
49 this->netpoint_ = sg_host_by_name(name.c_str())->pimpl_netpoint;
50 xbt_assert(this->netpoint_, "Element '%s' not found", name.c_str());
53 this->netpoint_ = simgrid::s4u::Engine::getInstance()->getNetpointByNameOrNull(name);
54 xbt_assert(this->netpoint_, "Element '%s' not found", name.c_str());
57 this->netpoint_ = simgrid::s4u::Engine::getInstance()->getNetpointByNameOrNull(name);
58 xbt_assert(this->netpoint_, "Element '%s' not found", name.c_str());
61 this->netpoint_ = nullptr;
66 this->level_ = father_->level_ + 1;
67 XBT_DEBUG("new container %s, child of %s", name.c_str(), father->name_.c_str());
70 // type definition (method depends on kind of this new container)
71 if (this->kind_ == INSTR_AS) {
72 //if this container is of an AS, its type name depends on its level
73 char as_typename[INSTR_DEFAULT_STR_SIZE];
74 snprintf(as_typename, INSTR_DEFAULT_STR_SIZE, "L%d", this->level_);
76 this->type_ = this->father_->type_->getChildOrNull(as_typename);
77 if (this->type_ == nullptr) {
78 this->type_ = Type::containerNew(as_typename, this->father_->type_);
81 this->type_ = Type::containerNew("0", nullptr);
84 //otherwise, the name is its kind
85 char typeNameBuff[INSTR_DEFAULT_STR_SIZE];
86 switch (this->kind_) {
88 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "HOST");
91 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "LINK");
94 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "ROUTER");
97 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MPI");
99 case INSTR_MSG_PROCESS:
100 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_PROCESS");
103 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_VM");
106 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_TASK");
109 THROWF (tracing_error, 0, "new container kind is unknown.");
112 Type* type = this->father_->type_->getChildOrNull(typeNameBuff);
113 if (type == nullptr){
114 this->type_ = Type::containerNew(typeNameBuff, this->father_->type_);
120 this->father_->children_.insert({this->name_, this});
121 LogContainerCreation(this);
124 //register all kinds by name
125 if (not allContainers.emplace(this->name_, this).second) {
126 THROWF(tracing_error, 1, "container %s already present in allContainers data structure", this->name_.c_str());
129 XBT_DEBUG("Add container name '%s'", this->name_.c_str());
131 //register NODE types for triva configuration
132 if (this->kind_ == INSTR_HOST || this->kind_ == INSTR_LINK || this->kind_ == INSTR_ROUTER) {
133 trivaNodeTypes.insert(this->type_->getName());
137 Container::~Container()
139 XBT_DEBUG("destroy container %s", name_.c_str());
140 // Begin with destroying my own children
141 for (auto child : children_) {
145 // obligation to dump previous events because they might reference the container that is about to be destroyed
146 TRACE_last_timestamp_to_dump = surf_get_clock();
147 TRACE_paje_dump_buffer(1);
149 // trace my destruction
150 if (not TRACE_disable_destroy() && this != PJ_container_get_root()) {
151 // do not trace the container destruction if user requests or if the container is root
152 LogContainerDestruction(this);
155 // remove me from the allContainers data structure
156 allContainers.erase(name_);
159 Container* Container::byNameOrNull(std::string name)
161 auto cont = allContainers.find(name);
162 return cont == allContainers.end() ? nullptr : cont->second;
165 Container* Container::byName(std::string name)
167 Container* ret = Container::byNameOrNull(name);
169 THROWF(tracing_error, 1, "container with name %s not found", name.c_str());
176 void PJ_container_remove_from_parent (container_t child)
178 if (child == nullptr){
179 THROWF (tracing_error, 0, "can't remove from parent with a nullptr child");
182 container_t parent = child->father_;
184 XBT_DEBUG("removeChildContainer (%s) FromContainer (%s) ", child->name_.c_str(), parent->name_.c_str());
185 parent->children_.erase(child->name_);