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.hpp"
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, container_t> allContainers; /* all created containers indexed by name */
19 std::set<std::string> trivaNodeTypes; /* all host types defined */
20 std::set<std::string> trivaEdgeTypes; /* all link types defined */
22 long long int instr_new_paje_id ()
24 static long long int type_id = 0;
28 void PJ_container_set_root (container_t root)
33 simgrid::instr::Container::Container(std::string name, simgrid::instr::e_container_types kind, Container* father)
34 : name_(name), kind_(kind), father_(father)
36 static long long int container_id = 0;
37 id_ = std::to_string(container_id); // id (or alias) of the container
40 //Search for network_element_t
42 case simgrid::instr::INSTR_HOST:
43 this->netpoint_ = sg_host_by_name(name.c_str())->pimpl_netpoint;
44 xbt_assert(this->netpoint_, "Element '%s' not found", name.c_str());
46 case simgrid::instr::INSTR_ROUTER:
47 this->netpoint_ = simgrid::s4u::Engine::getInstance()->getNetpointByNameOrNull(name);
48 xbt_assert(this->netpoint_, "Element '%s' not found", name.c_str());
50 case simgrid::instr::INSTR_AS:
51 this->netpoint_ = simgrid::s4u::Engine::getInstance()->getNetpointByNameOrNull(name);
52 xbt_assert(this->netpoint_, "Element '%s' not found", name.c_str());
55 this->netpoint_ = nullptr;
60 this->level_ = father_->level_ + 1;
61 XBT_DEBUG("new container %s, child of %s", name.c_str(), father->name_.c_str());
64 // type definition (method depends on kind of this new container)
65 if (this->kind_ == simgrid::instr::INSTR_AS) {
66 //if this container is of an AS, its type name depends on its level
67 char as_typename[INSTR_DEFAULT_STR_SIZE];
68 snprintf(as_typename, INSTR_DEFAULT_STR_SIZE, "L%d", this->level_);
70 this->type_ = this->father_->type_->getChildOrNull(as_typename);
71 if (this->type_ == nullptr) {
72 this->type_ = simgrid::instr::Type::containerNew(as_typename, this->father_->type_);
75 this->type_ = simgrid::instr::Type::containerNew("0", nullptr);
78 //otherwise, the name is its kind
79 char typeNameBuff[INSTR_DEFAULT_STR_SIZE];
80 switch (this->kind_) {
81 case simgrid::instr::INSTR_HOST:
82 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "HOST");
84 case simgrid::instr::INSTR_LINK:
85 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "LINK");
87 case simgrid::instr::INSTR_ROUTER:
88 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "ROUTER");
90 case simgrid::instr::INSTR_SMPI:
91 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MPI");
93 case simgrid::instr::INSTR_MSG_PROCESS:
94 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_PROCESS");
96 case simgrid::instr::INSTR_MSG_VM:
97 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_VM");
99 case simgrid::instr::INSTR_MSG_TASK:
100 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_TASK");
103 THROWF (tracing_error, 0, "new container kind is unknown.");
106 simgrid::instr::Type* type = this->father_->type_->getChildOrNull(typeNameBuff);
107 if (type == nullptr){
108 this->type_ = simgrid::instr::Type::containerNew(typeNameBuff, this->father_->type_);
114 this->father_->children_.insert({this->name_, this});
115 LogContainerCreation(this);
118 //register all kinds by name
119 if (not allContainers.emplace(this->name_, this).second) {
120 THROWF(tracing_error, 1, "container %s already present in allContainers data structure", this->name_.c_str());
123 XBT_DEBUG("Add container name '%s'", this->name_.c_str());
125 //register NODE types for triva configuration
126 if (this->kind_ == simgrid::instr::INSTR_HOST || this->kind_ == simgrid::instr::INSTR_LINK ||
127 this->kind_ == simgrid::instr::INSTR_ROUTER) {
128 trivaNodeTypes.insert(this->type_->name_);
131 simgrid::instr::Container::~Container()
133 XBT_DEBUG("destroy container %s", name_.c_str());
134 // Begin with destroying my own children
135 for (auto child : children_) {
139 // obligation to dump previous events because they might reference the container that is about to be destroyed
140 TRACE_last_timestamp_to_dump = surf_get_clock();
141 TRACE_paje_dump_buffer(1);
143 // trace my destruction
144 if (not TRACE_disable_destroy() && this != PJ_container_get_root()) {
145 // do not trace the container destruction if user requests or if the container is root
146 LogContainerDestruction(this);
149 // remove me from the allContainers data structure
150 allContainers.erase(name_);
153 simgrid::instr::Container* simgrid::instr::Container::byNameOrNull(std::string name)
155 auto cont = allContainers.find(name);
156 return cont == allContainers.end() ? nullptr : cont->second;
159 simgrid::instr::Container* simgrid::instr::Container::byName(std::string name)
161 container_t ret = simgrid::instr::Container::byNameOrNull(name);
163 THROWF(tracing_error, 1, "container with name %s not found", name.c_str());
168 simgrid::instr::Container* PJ_container_get_root()
170 return rootContainer;
173 void PJ_container_remove_from_parent (container_t child)
175 if (child == nullptr){
176 THROWF (tracing_error, 0, "can't remove from parent with a nullptr child");
179 container_t parent = child->father_;
181 XBT_DEBUG("removeChildContainer (%s) FromContainer (%s) ", child->name_.c_str(), parent->name_.c_str());
182 parent->children_.erase(child->name_);