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"
11 #include "src/instr/instr_private.h"
13 #include <unordered_map>
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_paje_containers, instr, "Paje tracing event system (containers)");
17 static container_t rootContainer = nullptr; /* the root container */
18 static std::unordered_map<std::string, simgrid::instr::Container*>
19 allContainers; /* all created containers indexed by name */
20 std::set<std::string> trivaNodeTypes; /* all host types defined */
21 std::set<std::string> trivaEdgeTypes; /* all link types defined */
23 long long int instr_new_paje_id ()
25 static long long int type_id = 0;
29 void PJ_container_set_root (container_t root)
34 simgrid::instr::Container::Container(std::string name, simgrid::instr::e_container_types kind, Container* father)
35 : name_(name), kind_(kind), father_(father)
37 static long long int container_id = 0;
38 id_ = std::to_string(container_id); // id (or alias) of the container
41 //Search for network_element_t
43 case simgrid::instr::INSTR_HOST:
44 this->netpoint_ = sg_host_by_name(name.c_str())->pimpl_netpoint;
45 xbt_assert(this->netpoint_, "Element '%s' not found", name.c_str());
47 case simgrid::instr::INSTR_ROUTER:
48 this->netpoint_ = simgrid::s4u::Engine::getInstance()->getNetpointByNameOrNull(name);
49 xbt_assert(this->netpoint_, "Element '%s' not found", name.c_str());
51 case simgrid::instr::INSTR_AS:
52 this->netpoint_ = simgrid::s4u::Engine::getInstance()->getNetpointByNameOrNull(name);
53 xbt_assert(this->netpoint_, "Element '%s' not found", name.c_str());
56 this->netpoint_ = nullptr;
61 this->level_ = father_->level_ + 1;
62 XBT_DEBUG("new container %s, child of %s", name.c_str(), father->name_.c_str());
65 // type definition (method depends on kind of this new container)
66 if (this->kind_ == simgrid::instr::INSTR_AS) {
67 //if this container is of an AS, its type name depends on its level
68 char as_typename[INSTR_DEFAULT_STR_SIZE];
69 snprintf(as_typename, INSTR_DEFAULT_STR_SIZE, "L%d", this->level_);
71 this->type_ = this->father_->type_->getChildOrNull(as_typename);
72 if (this->type_ == nullptr) {
73 this->type_ = simgrid::instr::Type::containerNew(as_typename, this->father_->type_);
76 this->type_ = simgrid::instr::Type::containerNew("0", nullptr);
79 //otherwise, the name is its kind
80 char typeNameBuff[INSTR_DEFAULT_STR_SIZE];
81 switch (this->kind_) {
82 case simgrid::instr::INSTR_HOST:
83 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "HOST");
85 case simgrid::instr::INSTR_LINK:
86 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "LINK");
88 case simgrid::instr::INSTR_ROUTER:
89 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "ROUTER");
91 case simgrid::instr::INSTR_SMPI:
92 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MPI");
94 case simgrid::instr::INSTR_MSG_PROCESS:
95 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_PROCESS");
97 case simgrid::instr::INSTR_MSG_VM:
98 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_VM");
100 case simgrid::instr::INSTR_MSG_TASK:
101 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_TASK");
104 THROWF (tracing_error, 0, "new container kind is unknown.");
107 simgrid::instr::Type* type = this->father_->type_->getChildOrNull(typeNameBuff);
108 if (type == nullptr){
109 this->type_ = simgrid::instr::Type::containerNew(typeNameBuff, this->father_->type_);
115 this->father_->children_.insert({this->name_, this});
116 LogContainerCreation(this);
119 //register all kinds by name
120 if (not allContainers.emplace(this->name_, this).second) {
121 THROWF(tracing_error, 1, "container %s already present in allContainers data structure", this->name_.c_str());
124 XBT_DEBUG("Add container name '%s'", this->name_.c_str());
126 //register NODE types for triva configuration
127 if (this->kind_ == simgrid::instr::INSTR_HOST || this->kind_ == simgrid::instr::INSTR_LINK ||
128 this->kind_ == simgrid::instr::INSTR_ROUTER) {
129 trivaNodeTypes.insert(this->type_->name_);
132 simgrid::instr::Container::~Container()
134 XBT_DEBUG("destroy container %s", name_.c_str());
135 // Begin with destroying my own children
136 for (auto child : children_) {
140 // obligation to dump previous events because they might reference the container that is about to be destroyed
141 TRACE_last_timestamp_to_dump = surf_get_clock();
142 TRACE_paje_dump_buffer(1);
144 // trace my destruction
145 if (not TRACE_disable_destroy() && this != PJ_container_get_root()) {
146 // do not trace the container destruction if user requests or if the container is root
147 LogContainerDestruction(this);
150 // remove me from the allContainers data structure
151 allContainers.erase(name_);
154 simgrid::instr::Container* PJ_container_get(const char* name)
156 container_t ret = PJ_container_get_or_null (name);
158 THROWF(tracing_error, 1, "container with name %s not found", name);
163 simgrid::instr::Container* PJ_container_get_or_null(const char* name)
165 auto cont = allContainers.find(name);
166 return cont == allContainers.end() ? nullptr : cont->second;
169 simgrid::instr::Container* PJ_container_get_root()
171 return rootContainer;
174 void PJ_container_remove_from_parent (container_t child)
176 if (child == nullptr){
177 THROWF (tracing_error, 0, "can't remove from parent with a nullptr child");
180 container_t parent = child->father_;
182 XBT_DEBUG("removeChildContainer (%s) FromContainer (%s) ", child->name_.c_str(), parent->name_.c_str());
183 parent->children_.erase(child->name_);