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 xbt_dict_t trivaNodeTypes = nullptr; /* all host types defined */
18 xbt_dict_t trivaEdgeTypes = nullptr; /* 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);
29 trivaNodeTypes = xbt_dict_new_homogeneous(xbt_free_f);
30 trivaEdgeTypes = xbt_dict_new_homogeneous(xbt_free_f);
33 void PJ_container_release ()
35 xbt_dict_free (&allContainers);
36 xbt_dict_free (&trivaNodeTypes);
37 xbt_dict_free (&trivaEdgeTypes);
40 void PJ_container_set_root (container_t root)
45 container_t PJ_container_new (const char *name, e_container_types kind, container_t father)
48 THROWF (tracing_error, 0, "can't create a container with a nullptr name");
51 static long long int container_id = 0;
52 char id_str[INSTR_DEFAULT_STR_SIZE];
53 snprintf (id_str, INSTR_DEFAULT_STR_SIZE, "%lld", container_id);
56 container_t newContainer = xbt_new0(s_container_t, 1);
57 newContainer->name = xbt_strdup (name); // name of the container
58 newContainer->id = xbt_strdup (id_str); // id (or alias) of the container
59 newContainer->father = father;
60 sg_host_t sg_host = sg_host_by_name(name);
62 //Search for network_element_t
65 newContainer->netpoint = sg_host->pimpl_netpoint;
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 = simgrid::s4u::Engine::getInstance()->getNetpointByNameOrNull(name);
74 xbt_assert(newContainer->netpoint, "Element '%s' not found", name);
77 newContainer->netpoint = nullptr;
81 // level depends on level of father
82 if (newContainer->father){
83 newContainer->level = newContainer->father->level+1;
84 XBT_DEBUG("new container %s, child of %s", name, father->name);
86 newContainer->level = 0;
88 // type definition (method depends on kind of this new container)
89 newContainer->kind = kind;
90 if (newContainer->kind == INSTR_AS){
91 //if this container is of an AS, its type name depends on its level
92 char as_typename[INSTR_DEFAULT_STR_SIZE];
93 snprintf (as_typename, INSTR_DEFAULT_STR_SIZE, "L%d", newContainer->level);
94 if (newContainer->father){
95 newContainer->type = PJ_type_get_or_null (as_typename, newContainer->father->type);
96 if (newContainer->type == nullptr){
97 newContainer->type = PJ_type_container_new (as_typename, newContainer->father->type);
100 newContainer->type = PJ_type_container_new ("0", nullptr);
103 //otherwise, the name is its kind
104 char typeNameBuff[INSTR_DEFAULT_STR_SIZE];
105 switch (newContainer->kind){
107 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "HOST");
110 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "LINK");
113 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "ROUTER");
116 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MPI");
118 case INSTR_MSG_PROCESS:
119 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_PROCESS");
122 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_VM");
125 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_TASK");
128 THROWF (tracing_error, 0, "new container kind is unknown.");
131 type_t type = PJ_type_get_or_null (typeNameBuff, newContainer->father->type);
132 if (type == nullptr){
133 newContainer->type = PJ_type_container_new (typeNameBuff, newContainer->father->type);
135 newContainer->type = type;
138 newContainer->children = xbt_dict_new_homogeneous(nullptr);
139 if (newContainer->father){
140 xbt_dict_set(newContainer->father->children, newContainer->name, newContainer, nullptr);
141 LogContainerCreation(newContainer);
144 //register all kinds by name
145 if (xbt_dict_get_or_null(allContainers, newContainer->name) != nullptr){
146 THROWF(tracing_error, 1, "container %s already present in allContainers data structure", newContainer->name);
149 xbt_dict_set (allContainers, newContainer->name, newContainer, nullptr);
150 XBT_DEBUG("Add container name '%s'",newContainer->name);
152 //register NODE types for triva configuration
153 if (newContainer->kind == INSTR_HOST || newContainer->kind == INSTR_LINK || newContainer->kind == INSTR_ROUTER) {
154 xbt_dict_set (trivaNodeTypes, newContainer->type->name, xbt_strdup("1"), nullptr);
159 container_t PJ_container_get (const char *name)
161 container_t ret = PJ_container_get_or_null (name);
163 THROWF(tracing_error, 1, "container with name %s not found", name);
168 container_t PJ_container_get_or_null (const char *name)
170 return static_cast<container_t>(name != nullptr ? xbt_dict_get_or_null(allContainers, name) : nullptr);
173 container_t PJ_container_get_root ()
175 return rootContainer;
178 void PJ_container_remove_from_parent (container_t child)
180 if (child == nullptr){
181 THROWF (tracing_error, 0, "can't remove from parent with a nullptr child");
184 container_t parent = child->father;
186 XBT_DEBUG("removeChildContainer (%s) FromContainer (%s) ",
189 xbt_dict_remove (parent->children, child->name);
193 void PJ_container_free (container_t container)
195 if (container == nullptr){
196 THROWF (tracing_error, 0, "trying to free a nullptr container");
198 XBT_DEBUG("destroy container %s", container->name);
200 //obligation to dump previous events because they might
201 //reference the container that is about to be destroyed
202 TRACE_last_timestamp_to_dump = surf_get_clock();
203 TRACE_paje_dump_buffer(1);
205 //trace my destruction
206 if (not TRACE_disable_destroy() && container != PJ_container_get_root()) {
207 //do not trace the container destruction if user requests
208 //or if the container is root
209 LogContainerDestruction(container);
212 //remove it from allContainers data structure
213 xbt_dict_remove (allContainers, container->name);
216 xbt_free (container->name);
217 xbt_free (container->id);
218 xbt_dict_free (&container->children);
219 xbt_free (container);
223 static void recursiveDestroyContainer (container_t container)
225 if (container == nullptr){
226 THROWF (tracing_error, 0, "trying to recursively destroy a nullptr container");
228 XBT_DEBUG("recursiveDestroyContainer %s", container->name);
229 xbt_dict_cursor_t cursor = nullptr;
232 xbt_dict_foreach(container->children, cursor, child_name, child) {
233 recursiveDestroyContainer (child);
235 PJ_container_free (container);
238 void PJ_container_free_all ()
240 container_t root = PJ_container_get_root();
241 if (root == nullptr){
242 THROWF (tracing_error, 0, "trying to free all containers, but root is nullptr");
244 recursiveDestroyContainer (root);
245 rootContainer = nullptr;
248 if (not xbt_dict_is_empty(allContainers)) {
249 THROWF(tracing_error, 0, "some containers still present even after destroying all of them");