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 = 0;
32 void PJ_container_alloc ()
34 allContainers = xbt_dict_new_homogeneous(nullptr);
35 trivaNodeTypes = xbt_dict_new_homogeneous(xbt_free_f);
36 trivaEdgeTypes = xbt_dict_new_homogeneous(xbt_free_f);
39 void PJ_container_release ()
41 xbt_dict_free (&allContainers);
42 xbt_dict_free (&trivaNodeTypes);
43 xbt_dict_free (&trivaEdgeTypes);
46 void PJ_container_set_root (container_t root)
51 container_t PJ_container_new (const char *name, e_container_types kind, container_t father)
54 THROWF (tracing_error, 0, "can't create a container with a nullptr name");
57 static long long int container_id = 0;
58 char id_str[INSTR_DEFAULT_STR_SIZE];
59 snprintf (id_str, INSTR_DEFAULT_STR_SIZE, "%lld", container_id);
62 container_t newContainer = xbt_new0(s_container_t, 1);
63 newContainer->name = xbt_strdup (name); // name of the container
64 newContainer->id = xbt_strdup (id_str); // id (or alias) of the container
65 newContainer->father = father;
66 sg_host_t sg_host = sg_host_by_name(name);
68 //Search for network_element_t
71 newContainer->netpoint = sg_host->pimpl_netpoint;
72 xbt_assert(newContainer->netpoint, "Element '%s' not found", name);
75 newContainer->netpoint = simgrid::s4u::Engine::instance()->netpointByNameOrNull(name);
76 xbt_assert(newContainer->netpoint, "Element '%s' not found", name);
79 newContainer->netpoint = simgrid::s4u::Engine::instance()->netpointByNameOrNull(name);
80 xbt_assert(newContainer->netpoint, "Element '%s' not found", name);
83 newContainer->netpoint = nullptr;
87 // level depends on level of father
88 if (newContainer->father){
89 newContainer->level = newContainer->father->level+1;
90 XBT_DEBUG("new container %s, child of %s", name, father->name);
92 newContainer->level = 0;
94 // type definition (method depends on kind of this new container)
95 newContainer->kind = kind;
96 if (newContainer->kind == INSTR_AS){
97 //if this container is of an AS, its type name depends on its level
98 char as_typename[INSTR_DEFAULT_STR_SIZE];
99 snprintf (as_typename, INSTR_DEFAULT_STR_SIZE, "L%d", newContainer->level);
100 if (newContainer->father){
101 newContainer->type = PJ_type_get_or_null (as_typename, newContainer->father->type);
102 if (newContainer->type == nullptr){
103 newContainer->type = PJ_type_container_new (as_typename, newContainer->father->type);
106 newContainer->type = PJ_type_container_new ("0", nullptr);
109 //otherwise, the name is its kind
110 char typeNameBuff[INSTR_DEFAULT_STR_SIZE];
111 switch (newContainer->kind){
113 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "HOST");
116 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "LINK");
119 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "ROUTER");
122 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MPI");
124 case INSTR_MSG_PROCESS:
125 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_PROCESS");
128 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_VM");
131 snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_TASK");
134 THROWF (tracing_error, 0, "new container kind is unknown.");
137 type_t type = PJ_type_get_or_null (typeNameBuff, newContainer->father->type);
138 if (type == nullptr){
139 newContainer->type = PJ_type_container_new (typeNameBuff, newContainer->father->type);
141 newContainer->type = type;
144 newContainer->children = xbt_dict_new_homogeneous(nullptr);
145 if (newContainer->father){
146 xbt_dict_set(newContainer->father->children, newContainer->name, newContainer, nullptr);
147 new_pajeCreateContainer (newContainer);
150 //register all kinds by name
151 if (xbt_dict_get_or_null(allContainers, newContainer->name) != nullptr){
152 THROWF(tracing_error, 1, "container %s already present in allContainers data structure", newContainer->name);
155 xbt_dict_set (allContainers, newContainer->name, newContainer, nullptr);
156 XBT_DEBUG("Add container name '%s'",newContainer->name);
158 //register NODE types for triva configuration
159 if (newContainer->kind == INSTR_HOST || newContainer->kind == INSTR_LINK || newContainer->kind == INSTR_ROUTER) {
160 xbt_dict_set (trivaNodeTypes, newContainer->type->name, xbt_strdup("1"), nullptr);
165 container_t PJ_container_get (const char *name)
167 container_t ret = PJ_container_get_or_null (name);
169 THROWF(tracing_error, 1, "container with name %s not found", name);
174 container_t PJ_container_get_or_null (const char *name)
176 return static_cast<container_t>(name != nullptr ? xbt_dict_get_or_null(allContainers, name) : nullptr);
179 container_t PJ_container_get_root ()
181 return rootContainer;
184 void PJ_container_remove_from_parent (container_t child)
186 if (child == nullptr){
187 THROWF (tracing_error, 0, "can't remove from parent with a nullptr child");
190 container_t parent = child->father;
192 XBT_DEBUG("removeChildContainer (%s) FromContainer (%s) ",
195 xbt_dict_remove (parent->children, child->name);
199 void PJ_container_free (container_t container)
201 if (container == nullptr){
202 THROWF (tracing_error, 0, "trying to free a nullptr container");
204 XBT_DEBUG("destroy container %s", container->name);
206 //obligation to dump previous events because they might
207 //reference the container that is about to be destroyed
208 TRACE_last_timestamp_to_dump = surf_get_clock();
209 TRACE_paje_dump_buffer(1);
211 //trace my destruction
212 if (!TRACE_disable_destroy() && container != PJ_container_get_root()){
213 //do not trace the container destruction if user requests
214 //or if the container is root
215 new_pajeDestroyContainer(container);
218 //remove it from allContainers data structure
219 xbt_dict_remove (allContainers, container->name);
222 xbt_free (container->name);
223 xbt_free (container->id);
224 xbt_dict_free (&container->children);
225 xbt_free (container);
229 static void recursiveDestroyContainer (container_t container)
231 if (container == nullptr){
232 THROWF (tracing_error, 0, "trying to recursively destroy a nullptr container");
234 XBT_DEBUG("recursiveDestroyContainer %s", container->name);
235 xbt_dict_cursor_t cursor = nullptr;
238 xbt_dict_foreach(container->children, cursor, child_name, child) {
239 recursiveDestroyContainer (child);
241 PJ_container_free (container);
244 void PJ_container_free_all ()
246 container_t root = PJ_container_get_root();
247 if (root == nullptr){
248 THROWF (tracing_error, 0, "trying to free all containers, but root is nullptr");
250 recursiveDestroyContainer (root);
251 rootContainer = nullptr;
254 if (!xbt_dict_is_empty(allContainers)){
255 THROWF(tracing_error, 0, "some containers still present even after destroying all of them");