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 container_t PJ_container_new (const char *name, e_container_types kind, container_t father)
44 THROWF (tracing_error, 0, "can't create a container with a nullptr name");
47 static long long int container_id = 0;
48 char id_str[INSTR_DEFAULT_STR_SIZE];
49 snprintf (id_str, INSTR_DEFAULT_STR_SIZE, "%lld", container_id);
52 container_t newContainer = xbt_new0(s_container, 1);
53 newContainer->name = xbt_strdup (name); // name of the container
54 newContainer->id = xbt_strdup (id_str); // id (or alias) of the container
55 newContainer->father = father;
56 sg_host_t sg_host = sg_host_by_name(name);
58 //Search for network_element_t
61 newContainer->netpoint = sg_host->pimpl_netpoint;
62 xbt_assert(newContainer->netpoint, "Element '%s' not found", name);
65 newContainer->netpoint = simgrid::s4u::Engine::getInstance()->getNetpointByNameOrNull(name);
66 xbt_assert(newContainer->netpoint, "Element '%s' not found", name);
69 newContainer->netpoint = simgrid::s4u::Engine::getInstance()->getNetpointByNameOrNull(name);
70 xbt_assert(newContainer->netpoint, "Element '%s' not found", name);
73 newContainer->netpoint = nullptr;
77 // level depends on level of father
78 if (newContainer->father){
79 newContainer->level = newContainer->father->level+1;
80 XBT_DEBUG("new container %s, child of %s", name, father->name);
82 newContainer->level = 0;
84 // type definition (method depends on kind of this new container)
85 newContainer->kind = kind;
86 if (newContainer->kind == INSTR_AS){
87 //if this container is of an AS, its type name depends on its level
88 char as_typename[INSTR_DEFAULT_STR_SIZE];
89 snprintf (as_typename, INSTR_DEFAULT_STR_SIZE, "L%d", newContainer->level);
90 if (newContainer->father){
91 newContainer->type = PJ_type_get_or_null (as_typename, newContainer->father->type);
92 if (newContainer->type == nullptr){
93 newContainer->type = PJ_type_container_new (as_typename, newContainer->father->type);
96 newContainer->type = PJ_type_container_new ("0", nullptr);
99 //otherwise, the name is its kind
100 char typeNameBuff[INSTR_DEFAULT_STR_SIZE];
101 switch (newContainer->kind){
103 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "HOST");
106 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "LINK");
109 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "ROUTER");
112 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MPI");
114 case INSTR_MSG_PROCESS:
115 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_PROCESS");
118 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_VM");
121 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_TASK");
124 THROWF (tracing_error, 0, "new container kind is unknown.");
127 type_t type = PJ_type_get_or_null (typeNameBuff, newContainer->father->type);
128 if (type == nullptr){
129 newContainer->type = PJ_type_container_new (typeNameBuff, newContainer->father->type);
131 newContainer->type = type;
134 newContainer->children = xbt_dict_new_homogeneous(nullptr);
135 if (newContainer->father){
136 xbt_dict_set(newContainer->father->children, newContainer->name, newContainer, nullptr);
137 LogContainerCreation(newContainer);
140 //register all kinds by name
141 if (xbt_dict_get_or_null(allContainers, newContainer->name) != nullptr){
142 THROWF(tracing_error, 1, "container %s already present in allContainers data structure", newContainer->name);
145 xbt_dict_set (allContainers, newContainer->name, newContainer, nullptr);
146 XBT_DEBUG("Add container name '%s'",newContainer->name);
148 //register NODE types for triva configuration
149 if (newContainer->kind == INSTR_HOST || newContainer->kind == INSTR_LINK || newContainer->kind == INSTR_ROUTER) {
150 trivaNodeTypes.insert(newContainer->type->name);
155 container_t PJ_container_get (const char *name)
157 container_t ret = PJ_container_get_or_null (name);
159 THROWF(tracing_error, 1, "container with name %s not found", name);
164 container_t PJ_container_get_or_null (const char *name)
166 return static_cast<container_t>(name != nullptr ? xbt_dict_get_or_null(allContainers, name) : nullptr);
169 container_t 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) ",
185 xbt_dict_remove (parent->children, child->name);
189 void PJ_container_free (container_t container)
191 if (container == nullptr){
192 THROWF (tracing_error, 0, "trying to free a nullptr container");
194 XBT_DEBUG("destroy container %s", container->name);
196 //obligation to dump previous events because they might
197 //reference the container that is about to be destroyed
198 TRACE_last_timestamp_to_dump = surf_get_clock();
199 TRACE_paje_dump_buffer(1);
201 //trace my destruction
202 if (not TRACE_disable_destroy() && container != PJ_container_get_root()) {
203 //do not trace the container destruction if user requests
204 //or if the container is root
205 LogContainerDestruction(container);
208 //remove it from allContainers data structure
209 xbt_dict_remove (allContainers, container->name);
212 xbt_free (container->name);
213 xbt_free (container->id);
214 xbt_dict_free (&container->children);
215 xbt_free (container);
219 static void recursiveDestroyContainer (container_t container)
221 if (container == nullptr){
222 THROWF (tracing_error, 0, "trying to recursively destroy a nullptr container");
224 XBT_DEBUG("recursiveDestroyContainer %s", container->name);
225 xbt_dict_cursor_t cursor = nullptr;
228 xbt_dict_foreach(container->children, cursor, child_name, child) {
229 recursiveDestroyContainer (child);
231 PJ_container_free (container);
234 void PJ_container_free_all ()
236 container_t root = PJ_container_get_root();
237 if (root == nullptr){
238 THROWF (tracing_error, 0, "trying to free all containers, but root is nullptr");
240 recursiveDestroyContainer (root);
241 rootContainer = nullptr;
244 if (not xbt_dict_is_empty(allContainers)) {
245 THROWF(tracing_error, 0, "some containers still present even after destroying all of them");