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/host.hpp>
13 #include <surf/surf.h>
14 #include <surf/surf_routing.h>
16 #include "src/instr/instr_private.h"
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_paje_containers, instr, "Paje tracing event system (containers)");
20 static container_t rootContainer = nullptr; /* the root container */
21 static xbt_dict_t allContainers = nullptr; /* all created containers indexed by name */
22 xbt_dict_t trivaNodeTypes = nullptr; /* all host types defined */
23 xbt_dict_t trivaEdgeTypes = nullptr; /* all link types defined */
25 long long int instr_new_paje_id ()
27 static long long int type_id = 0;
31 void PJ_container_alloc ()
33 allContainers = xbt_dict_new_homogeneous(nullptr);
34 trivaNodeTypes = xbt_dict_new_homogeneous(xbt_free_f);
35 trivaEdgeTypes = xbt_dict_new_homogeneous(xbt_free_f);
38 void PJ_container_release ()
40 xbt_dict_free (&allContainers);
41 xbt_dict_free (&trivaNodeTypes);
42 xbt_dict_free (&trivaEdgeTypes);
45 void PJ_container_set_root (container_t root)
50 container_t PJ_container_new (const char *name, e_container_types kind, container_t father)
53 THROWF (tracing_error, 0, "can't create a container with a nullptr name");
56 static long long int container_id = 0;
57 char id_str[INSTR_DEFAULT_STR_SIZE];
58 snprintf (id_str, INSTR_DEFAULT_STR_SIZE, "%lld", container_id);
61 container_t newContainer = xbt_new0(s_container_t, 1);
62 newContainer->name = xbt_strdup (name); // name of the container
63 newContainer->id = xbt_strdup (id_str); // id (or alias) of the container
64 newContainer->father = father;
65 sg_host_t sg_host = sg_host_by_name(name);
67 //Search for network_element_t
70 newContainer->netcard = sg_host->pimpl_netcard;
71 xbt_assert(newContainer->netcard, "Element '%s' not found",name);
74 newContainer->netcard = static_cast<sg_netcard_t>(xbt_lib_get_or_null(as_router_lib,name,ROUTING_ASR_LEVEL));
75 xbt_assert(newContainer->netcard, "Element '%s' not found",name);
78 newContainer->netcard = static_cast<sg_netcard_t>(xbt_lib_get_or_null(as_router_lib,name,ROUTING_ASR_LEVEL));
79 xbt_assert(newContainer->netcard, "Element '%s' not found",name);
82 newContainer->netcard = nullptr;
86 // level depends on level of father
87 if (newContainer->father){
88 newContainer->level = newContainer->father->level+1;
89 XBT_DEBUG("new container %s, child of %s", name, father->name);
91 newContainer->level = 0;
93 // type definition (method depends on kind of this new container)
94 newContainer->kind = kind;
95 if (newContainer->kind == INSTR_AS){
96 //if this container is of an AS, its type name depends on its level
97 char as_typename[INSTR_DEFAULT_STR_SIZE];
98 snprintf (as_typename, INSTR_DEFAULT_STR_SIZE, "L%d", newContainer->level);
99 if (newContainer->father){
100 newContainer->type = PJ_type_get_or_null (as_typename, newContainer->father->type);
101 if (newContainer->type == nullptr){
102 newContainer->type = PJ_type_container_new (as_typename, newContainer->father->type);
105 newContainer->type = PJ_type_container_new ("0", nullptr);
108 //otherwise, the name is its kind
109 char typeNameBuff[INSTR_DEFAULT_STR_SIZE];
110 switch (newContainer->kind){
112 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "HOST");
115 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "LINK");
118 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "ROUTER");
121 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MPI");
123 case INSTR_MSG_PROCESS:
124 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_PROCESS");
127 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_VM");
130 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_TASK");
133 THROWF (tracing_error, 0, "new container kind is unknown.");
136 type_t type = PJ_type_get_or_null (typeNameBuff, newContainer->father->type);
137 if (type == nullptr){
138 newContainer->type = PJ_type_container_new (typeNameBuff, newContainer->father->type);
140 newContainer->type = type;
143 newContainer->children = xbt_dict_new_homogeneous(nullptr);
144 if (newContainer->father){
145 xbt_dict_set(newContainer->father->children, newContainer->name, newContainer, nullptr);
146 new_pajeCreateContainer (newContainer);
149 //register all kinds by name
150 if (xbt_dict_get_or_null(allContainers, newContainer->name) != nullptr){
151 THROWF(tracing_error, 1, "container %s already present in allContainers data structure", newContainer->name);
154 xbt_dict_set (allContainers, newContainer->name, newContainer, nullptr);
155 XBT_DEBUG("Add container name '%s'",newContainer->name);
157 //register NODE types for triva configuration
158 if (newContainer->kind == INSTR_HOST || newContainer->kind == INSTR_LINK || newContainer->kind == INSTR_ROUTER) {
159 xbt_dict_set (trivaNodeTypes, newContainer->type->name, xbt_strdup("1"), nullptr);
164 container_t PJ_container_get (const char *name)
166 container_t ret = PJ_container_get_or_null (name);
168 THROWF(tracing_error, 1, "container with name %s not found", name);
173 container_t PJ_container_get_or_null (const char *name)
175 return static_cast<container_t>(name != nullptr ? xbt_dict_get_or_null(allContainers, name) : nullptr);
178 container_t 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) ",
194 xbt_dict_remove (parent->children, child->name);
198 void PJ_container_free (container_t container)
200 if (container == nullptr){
201 THROWF (tracing_error, 0, "trying to free a nullptr container");
203 XBT_DEBUG("destroy container %s", container->name);
205 //obligation to dump previous events because they might
206 //reference the container that is about to be destroyed
207 TRACE_last_timestamp_to_dump = surf_get_clock();
208 TRACE_paje_dump_buffer(1);
210 //trace my destruction
211 if (!TRACE_disable_destroy() && container != PJ_container_get_root()){
212 //do not trace the container destruction if user requests
213 //or if the container is root
214 new_pajeDestroyContainer(container);
217 //remove it from allContainers data structure
218 xbt_dict_remove (allContainers, container->name);
221 xbt_free (container->name);
222 xbt_free (container->id);
223 xbt_dict_free (&container->children);
224 xbt_free (container);
228 static void recursiveDestroyContainer (container_t container)
230 if (container == nullptr){
231 THROWF (tracing_error, 0, "trying to recursively destroy a nullptr container");
233 XBT_DEBUG("recursiveDestroyContainer %s", container->name);
234 xbt_dict_cursor_t cursor = nullptr;
237 xbt_dict_foreach(container->children, cursor, child_name, child) {
238 recursiveDestroyContainer (child);
240 PJ_container_free (container);
243 void PJ_container_free_all ()
245 container_t root = PJ_container_get_root();
246 if (root == nullptr){
247 THROWF (tracing_error, 0, "trying to free all containers, but root is nullptr");
249 recursiveDestroyContainer (root);
250 rootContainer = nullptr;
253 if (!xbt_dict_is_empty(allContainers)){
254 THROWF(tracing_error, 0, "some containers still present even after destroying all of them");