1 /* Copyright (c) 2010, 2012-2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "simgrid/s4u/engine.hpp"
8 #include "simgrid/s4u/host.hpp"
14 #include <surf/surf.h>
15 #include <surf/surf_routing.h>
17 #include "src/instr/instr_private.h"
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_paje_containers, instr, "Paje tracing event system (containers)");
21 static container_t rootContainer = nullptr; /* the root container */
22 static xbt_dict_t allContainers = nullptr; /* all created containers indexed by name */
23 xbt_dict_t trivaNodeTypes = nullptr; /* all host types defined */
24 xbt_dict_t trivaEdgeTypes = nullptr; /* all link types defined */
26 long long int instr_new_paje_id ()
28 static long long int type_id = -1;
33 void PJ_container_alloc ()
35 allContainers = xbt_dict_new_homogeneous(nullptr);
36 trivaNodeTypes = xbt_dict_new_homogeneous(xbt_free_f);
37 trivaEdgeTypes = xbt_dict_new_homogeneous(xbt_free_f);
40 void PJ_container_release ()
42 xbt_dict_free (&allContainers);
43 xbt_dict_free (&trivaNodeTypes);
44 xbt_dict_free (&trivaEdgeTypes);
47 void PJ_container_set_root (container_t root)
52 container_t PJ_container_new (const char *name, e_container_types kind, container_t father)
55 THROWF (tracing_error, 0, "can't create a container with a nullptr name");
58 static long long int container_id = 0;
59 char id_str[INSTR_DEFAULT_STR_SIZE];
60 snprintf (id_str, INSTR_DEFAULT_STR_SIZE, "%lld", container_id);
63 container_t newContainer = xbt_new0(s_container_t, 1);
64 newContainer->name = xbt_strdup (name); // name of the container
65 newContainer->id = xbt_strdup (id_str); // id (or alias) of the container
66 newContainer->father = father;
67 sg_host_t sg_host = sg_host_by_name(name);
69 //Search for network_element_t
72 newContainer->netcard = sg_host->pimpl_netcard;
73 xbt_assert(newContainer->netcard, "Element '%s' not found",name);
76 newContainer->netcard = simgrid::s4u::Engine::instance()->netcardByNameOrNull(name);
77 xbt_assert(newContainer->netcard, "Element '%s' not found",name);
80 newContainer->netcard = simgrid::s4u::Engine::instance()->netcardByNameOrNull(name);
81 xbt_assert(newContainer->netcard, "Element '%s' not found",name);
84 newContainer->netcard = nullptr;
88 // level depends on level of father
89 if (newContainer->father){
90 newContainer->level = newContainer->father->level+1;
91 XBT_DEBUG("new container %s, child of %s", name, father->name);
93 newContainer->level = 0;
95 // type definition (method depends on kind of this new container)
96 newContainer->kind = kind;
97 if (newContainer->kind == INSTR_AS){
98 //if this container is of an AS, its type name depends on its level
99 char as_typename[INSTR_DEFAULT_STR_SIZE];
100 snprintf (as_typename, INSTR_DEFAULT_STR_SIZE, "L%d", newContainer->level);
101 if (newContainer->father){
102 newContainer->type = PJ_type_get_or_null (as_typename, newContainer->father->type);
103 if (newContainer->type == nullptr){
104 newContainer->type = PJ_type_container_new (as_typename, newContainer->father->type);
107 newContainer->type = PJ_type_container_new ("0", nullptr);
110 //otherwise, the name is its kind
111 char typeNameBuff[INSTR_DEFAULT_STR_SIZE];
112 switch (newContainer->kind){
114 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "HOST");
117 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "LINK");
120 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "ROUTER");
123 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MPI");
125 case INSTR_MSG_PROCESS:
126 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_PROCESS");
129 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_VM");
132 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_TASK");
135 THROWF (tracing_error, 0, "new container kind is unknown.");
138 type_t type = PJ_type_get_or_null (typeNameBuff, newContainer->father->type);
139 if (type == nullptr){
140 newContainer->type = PJ_type_container_new (typeNameBuff, newContainer->father->type);
142 newContainer->type = type;
145 newContainer->children = xbt_dict_new_homogeneous(nullptr);
146 if (newContainer->father){
147 xbt_dict_set(newContainer->father->children, newContainer->name, newContainer, nullptr);
148 new_pajeCreateContainer (newContainer);
151 //register all kinds by name
152 if (xbt_dict_get_or_null(allContainers, newContainer->name) != nullptr){
153 THROWF(tracing_error, 1, "container %s already present in allContainers data structure", newContainer->name);
156 xbt_dict_set (allContainers, newContainer->name, newContainer, nullptr);
157 XBT_DEBUG("Add container name '%s'",newContainer->name);
159 //register NODE types for triva configuration
160 if (newContainer->kind == INSTR_HOST || newContainer->kind == INSTR_LINK || newContainer->kind == INSTR_ROUTER) {
161 xbt_dict_set (trivaNodeTypes, newContainer->type->name, xbt_strdup("1"), nullptr);
166 container_t PJ_container_get (const char *name)
168 container_t ret = PJ_container_get_or_null (name);
170 THROWF(tracing_error, 1, "container with name %s not found", name);
175 container_t PJ_container_get_or_null (const char *name)
177 return static_cast<container_t>(name != nullptr ? xbt_dict_get_or_null(allContainers, name) : nullptr);
180 container_t PJ_container_get_root ()
182 return rootContainer;
185 void PJ_container_remove_from_parent (container_t child)
187 if (child == nullptr){
188 THROWF (tracing_error, 0, "can't remove from parent with a nullptr child");
191 container_t parent = child->father;
193 XBT_DEBUG("removeChildContainer (%s) FromContainer (%s) ",
196 xbt_dict_remove (parent->children, child->name);
200 void PJ_container_free (container_t container)
202 if (container == nullptr){
203 THROWF (tracing_error, 0, "trying to free a nullptr container");
205 XBT_DEBUG("destroy container %s", container->name);
207 //obligation to dump previous events because they might
208 //reference the container that is about to be destroyed
209 TRACE_last_timestamp_to_dump = surf_get_clock();
210 TRACE_paje_dump_buffer(1);
212 //trace my destruction
213 if (!TRACE_disable_destroy() && container != PJ_container_get_root()){
214 //do not trace the container destruction if user requests
215 //or if the container is root
216 new_pajeDestroyContainer(container);
219 //remove it from allContainers data structure
220 xbt_dict_remove (allContainers, container->name);
223 xbt_free (container->name);
224 xbt_free (container->id);
225 xbt_dict_free (&container->children);
226 xbt_free (container);
230 static void recursiveDestroyContainer (container_t container)
232 if (container == nullptr){
233 THROWF (tracing_error, 0, "trying to recursively destroy a nullptr container");
235 XBT_DEBUG("recursiveDestroyContainer %s", container->name);
236 xbt_dict_cursor_t cursor = nullptr;
239 xbt_dict_foreach(container->children, cursor, child_name, child) {
240 recursiveDestroyContainer (child);
242 PJ_container_free (container);
245 void PJ_container_free_all ()
247 container_t root = PJ_container_get_root();
248 if (root == nullptr){
249 THROWF (tracing_error, 0, "trying to free all containers, but root is nullptr");
251 recursiveDestroyContainer (root);
252 rootContainer = nullptr;
255 if (!xbt_dict_is_empty(allContainers)){
256 THROWF(tracing_error, 0, "some containers still present even after destroying all of them");