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 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 xbt_dict_t allContainers = nullptr; /* 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 void PJ_container_alloc ()
28 allContainers = xbt_dict_new_homogeneous(nullptr);
31 void PJ_container_release ()
33 xbt_dict_free (&allContainers);
36 void PJ_container_set_root (container_t root)
41 simgrid::instr::Container::Container(const char* name, simgrid::instr::e_container_types kind, Container* father)
42 : name_(xbt_strdup(name)), father_(father)
44 xbt_assert(name != nullptr, "Container name cannot be nullptr");
46 static long long int container_id = 0;
47 id_ = bprintf("%lld", container_id); // id (or alias) of the container
50 //Search for network_element_t
52 case simgrid::instr::INSTR_HOST:
53 this->netpoint_ = sg_host_by_name(name)->pimpl_netpoint;
54 xbt_assert(this->netpoint_, "Element '%s' not found", name);
56 case simgrid::instr::INSTR_ROUTER:
57 this->netpoint_ = simgrid::s4u::Engine::getInstance()->getNetpointByNameOrNull(name);
58 xbt_assert(this->netpoint_, "Element '%s' not found", name);
60 case simgrid::instr::INSTR_AS:
61 this->netpoint_ = simgrid::s4u::Engine::getInstance()->getNetpointByNameOrNull(name);
62 xbt_assert(this->netpoint_, "Element '%s' not found", name);
65 this->netpoint_ = nullptr;
70 this->level_ = father_->level_ + 1;
71 XBT_DEBUG("new container %s, child of %s", name, father->name_);
74 // type definition (method depends on kind of this new container)
76 if (this->kind_ == simgrid::instr::INSTR_AS) {
77 //if this container is of an AS, its type name depends on its level
78 char as_typename[INSTR_DEFAULT_STR_SIZE];
79 snprintf(as_typename, INSTR_DEFAULT_STR_SIZE, "L%d", this->level_);
81 this->type_ = simgrid::instr::Type::getOrNull(as_typename, this->father_->type_);
82 if (this->type_ == nullptr) {
83 this->type_ = simgrid::instr::Type::containerNew(as_typename, this->father_->type_);
86 this->type_ = simgrid::instr::Type::containerNew("0", nullptr);
89 //otherwise, the name is its kind
90 char typeNameBuff[INSTR_DEFAULT_STR_SIZE];
91 switch (this->kind_) {
92 case simgrid::instr::INSTR_HOST:
93 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "HOST");
95 case simgrid::instr::INSTR_LINK:
96 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "LINK");
98 case simgrid::instr::INSTR_ROUTER:
99 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "ROUTER");
101 case simgrid::instr::INSTR_SMPI:
102 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MPI");
104 case simgrid::instr::INSTR_MSG_PROCESS:
105 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_PROCESS");
107 case simgrid::instr::INSTR_MSG_VM:
108 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_VM");
110 case simgrid::instr::INSTR_MSG_TASK:
111 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_TASK");
114 THROWF (tracing_error, 0, "new container kind is unknown.");
117 simgrid::instr::Type* type = simgrid::instr::Type::getOrNull(typeNameBuff, this->father_->type_);
118 if (type == nullptr){
119 this->type_ = simgrid::instr::Type::containerNew(typeNameBuff, this->father_->type_);
124 this->children_ = xbt_dict_new_homogeneous(nullptr);
126 xbt_dict_set(this->father_->children_, this->name_, this, nullptr);
127 LogContainerCreation(this);
130 //register all kinds by name
131 if (xbt_dict_get_or_null(allContainers, this->name_) != nullptr) {
132 THROWF(tracing_error, 1, "container %s already present in allContainers data structure", this->name_);
135 xbt_dict_set(allContainers, this->name_, this, nullptr);
136 XBT_DEBUG("Add container name '%s'", this->name_);
138 //register NODE types for triva configuration
139 if (this->kind_ == simgrid::instr::INSTR_HOST || this->kind_ == simgrid::instr::INSTR_LINK ||
140 this->kind_ == simgrid::instr::INSTR_ROUTER) {
141 trivaNodeTypes.insert(this->type_->name_);
144 simgrid::instr::Container::~Container()
146 XBT_DEBUG("destroy container %s", name_);
148 // obligation to dump previous events because they might
149 // reference the container that is about to be destroyed
150 TRACE_last_timestamp_to_dump = surf_get_clock();
151 TRACE_paje_dump_buffer(1);
153 // trace my destruction
154 if (not TRACE_disable_destroy() && this != PJ_container_get_root()) {
155 // do not trace the container destruction if user requests
156 // or if the container is root
157 LogContainerDestruction(this);
160 // remove it from allContainers data structure
161 xbt_dict_remove(allContainers, name_);
166 xbt_dict_free(&children_);
169 container_t PJ_container_get (const char *name)
171 container_t ret = PJ_container_get_or_null (name);
173 THROWF(tracing_error, 1, "container with name %s not found", name);
178 container_t PJ_container_get_or_null (const char *name)
180 return static_cast<container_t>(name != nullptr ? xbt_dict_get_or_null(allContainers, name) : nullptr);
183 container_t PJ_container_get_root ()
185 return rootContainer;
188 void PJ_container_remove_from_parent (container_t child)
190 if (child == nullptr){
191 THROWF (tracing_error, 0, "can't remove from parent with a nullptr child");
194 container_t parent = child->father_;
196 XBT_DEBUG("removeChildContainer (%s) FromContainer (%s) ", child->name_, parent->name_);
197 xbt_dict_remove(parent->children_, child->name_);
201 static void recursiveDestroyContainer (container_t container)
203 if (container == nullptr){
204 THROWF (tracing_error, 0, "trying to recursively destroy a nullptr container");
206 XBT_DEBUG("recursiveDestroyContainer %s", container->name_);
207 xbt_dict_cursor_t cursor = nullptr;
210 xbt_dict_foreach (container->children_, cursor, child_name, child) {
211 recursiveDestroyContainer (child);
216 void PJ_container_free_all ()
218 container_t root = PJ_container_get_root();
219 if (root == nullptr){
220 THROWF (tracing_error, 0, "trying to free all containers, but root is nullptr");
222 recursiveDestroyContainer (root);
223 rootContainer = nullptr;
226 if (not xbt_dict_is_empty(allContainers)) {
227 THROWF(tracing_error, 0, "some containers still present even after destroying all of them");