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(const char* name, simgrid::instr::e_container_types kind, Container* father)
35 : name_(xbt_strdup(name)), father_(father)
37 xbt_assert(name != nullptr, "Container name cannot be nullptr");
39 static long long int container_id = 0;
40 id_ = bprintf("%lld", container_id); // id (or alias) of the container
43 //Search for network_element_t
45 case simgrid::instr::INSTR_HOST:
46 this->netpoint_ = sg_host_by_name(name)->pimpl_netpoint;
47 xbt_assert(this->netpoint_, "Element '%s' not found", name);
49 case simgrid::instr::INSTR_ROUTER:
50 this->netpoint_ = simgrid::s4u::Engine::getInstance()->getNetpointByNameOrNull(name);
51 xbt_assert(this->netpoint_, "Element '%s' not found", name);
53 case simgrid::instr::INSTR_AS:
54 this->netpoint_ = simgrid::s4u::Engine::getInstance()->getNetpointByNameOrNull(name);
55 xbt_assert(this->netpoint_, "Element '%s' not found", name);
58 this->netpoint_ = nullptr;
63 this->level_ = father_->level_ + 1;
64 XBT_DEBUG("new container %s, child of %s", name, father->name_);
67 // type definition (method depends on kind of this new container)
69 if (this->kind_ == simgrid::instr::INSTR_AS) {
70 //if this container is of an AS, its type name depends on its level
71 char as_typename[INSTR_DEFAULT_STR_SIZE];
72 snprintf(as_typename, INSTR_DEFAULT_STR_SIZE, "L%d", this->level_);
74 this->type_ = simgrid::instr::Type::getOrNull(as_typename, this->father_->type_);
75 if (this->type_ == nullptr) {
76 this->type_ = simgrid::instr::Type::containerNew(as_typename, this->father_->type_);
79 this->type_ = simgrid::instr::Type::containerNew("0", nullptr);
82 //otherwise, the name is its kind
83 char typeNameBuff[INSTR_DEFAULT_STR_SIZE];
84 switch (this->kind_) {
85 case simgrid::instr::INSTR_HOST:
86 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "HOST");
88 case simgrid::instr::INSTR_LINK:
89 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "LINK");
91 case simgrid::instr::INSTR_ROUTER:
92 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "ROUTER");
94 case simgrid::instr::INSTR_SMPI:
95 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MPI");
97 case simgrid::instr::INSTR_MSG_PROCESS:
98 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_PROCESS");
100 case simgrid::instr::INSTR_MSG_VM:
101 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_VM");
103 case simgrid::instr::INSTR_MSG_TASK:
104 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_TASK");
107 THROWF (tracing_error, 0, "new container kind is unknown.");
110 simgrid::instr::Type* type = simgrid::instr::Type::getOrNull(typeNameBuff, this->father_->type_);
111 if (type == nullptr){
112 this->type_ = simgrid::instr::Type::containerNew(typeNameBuff, this->father_->type_);
117 this->children_ = xbt_dict_new_homogeneous(nullptr);
119 xbt_dict_set(this->father_->children_, this->name_, this, nullptr);
120 LogContainerCreation(this);
123 //register all kinds by name
124 if (allContainers.find(this->name_) != allContainers.end()) {
125 THROWF(tracing_error, 1, "container %s already present in allContainers data structure", this->name_);
128 allContainers.emplace(this->name_, this);
129 XBT_DEBUG("Add container name '%s'", this->name_);
131 //register NODE types for triva configuration
132 if (this->kind_ == simgrid::instr::INSTR_HOST || this->kind_ == simgrid::instr::INSTR_LINK ||
133 this->kind_ == simgrid::instr::INSTR_ROUTER) {
134 trivaNodeTypes.insert(this->type_->name_);
137 simgrid::instr::Container::~Container()
139 XBT_DEBUG("destroy container %s", name_);
141 // obligation to dump previous events because they might
142 // reference the container that is about to be destroyed
143 TRACE_last_timestamp_to_dump = surf_get_clock();
144 TRACE_paje_dump_buffer(1);
146 // trace my destruction
147 if (not TRACE_disable_destroy() && this != PJ_container_get_root()) {
148 // do not trace the container destruction if user requests
149 // or if the container is root
150 LogContainerDestruction(this);
153 // remove it from allContainers data structure
154 allContainers.erase(name_);
159 xbt_dict_free(&children_);
162 simgrid::instr::Container* PJ_container_get(const char* name)
164 container_t ret = PJ_container_get_or_null (name);
166 THROWF(tracing_error, 1, "container with name %s not found", name);
171 simgrid::instr::Container* PJ_container_get_or_null(const char* name)
173 if (allContainers.find(name) == allContainers.end())
175 return allContainers.at(name);
178 simgrid::instr::Container* PJ_container_get_root()
180 return rootContainer;
183 void PJ_container_remove_from_parent (container_t child)
185 if (child == nullptr){
186 THROWF (tracing_error, 0, "can't remove from parent with a nullptr child");
189 container_t parent = child->father_;
191 XBT_DEBUG("removeChildContainer (%s) FromContainer (%s) ", child->name_, parent->name_);
192 xbt_dict_remove(parent->children_, child->name_);
196 static void recursiveDestroyContainer (container_t container)
198 if (container == nullptr){
199 THROWF (tracing_error, 0, "trying to recursively destroy a nullptr container");
201 XBT_DEBUG("recursiveDestroyContainer %s", container->name_);
202 xbt_dict_cursor_t cursor = nullptr;
205 xbt_dict_foreach (container->children_, cursor, child_name, child) {
206 recursiveDestroyContainer (child);
211 void PJ_container_free_all ()
213 container_t root = PJ_container_get_root();
214 if (root == nullptr){
215 THROWF (tracing_error, 0, "trying to free all containers, but root is nullptr");
217 recursiveDestroyContainer (root);
218 rootContainer = nullptr;
221 if (not allContainers.empty()) {
222 THROWF(tracing_error, 0, "some containers still present even after destroying all of them");