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 "src/instr/instr_private.h"
10 #include "surf/surf_routing.h"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_paje_containers, instr, "Paje tracing event system (containers)");
14 static container_t rootContainer = NULL; /* the root container */
15 static xbt_dict_t allContainers = NULL; /* all created containers indexed by name */
16 xbt_dict_t trivaNodeTypes = NULL; /* all host types defined */
17 xbt_dict_t trivaEdgeTypes = NULL; /* all link types defined */
19 long long int instr_new_paje_id (void)
21 static long long int type_id = 0;
25 void PJ_container_alloc (void)
27 allContainers = xbt_dict_new_homogeneous(NULL);
28 trivaNodeTypes = xbt_dict_new_homogeneous(xbt_free_f);
29 trivaEdgeTypes = xbt_dict_new_homogeneous(xbt_free_f);
32 void PJ_container_release (void)
34 xbt_dict_free (&allContainers);
35 xbt_dict_free (&trivaNodeTypes);
36 xbt_dict_free (&trivaEdgeTypes);
39 void PJ_container_set_root (container_t root)
44 container_t PJ_container_new (const char *name, e_container_types kind, container_t father)
47 THROWF (tracing_error, 0, "can't create a container with a NULL name");
50 static long long int container_id = 0;
51 char id_str[INSTR_DEFAULT_STR_SIZE];
52 snprintf (id_str, INSTR_DEFAULT_STR_SIZE, "%lld", container_id++);
54 container_t newContainer = xbt_new0(s_container_t, 1);
55 newContainer->name = xbt_strdup (name); // name of the container
56 newContainer->id = xbt_strdup (id_str); // id (or alias) of the container
57 newContainer->father = father;
58 sg_host_t sg_host = sg_host_by_name(name);
60 //Search for network_element_t
63 newContainer->netcard = sg_host->pimpl_netcard;
64 if(!newContainer->netcard) xbt_die("Element '%s' not found",name);
67 newContainer->netcard = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib,name,ROUTING_ASR_LEVEL);
68 if(!newContainer->netcard) xbt_die("Element '%s' not found",name);
71 newContainer->netcard = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib,name,ROUTING_ASR_LEVEL);
72 if(!newContainer->netcard) xbt_die("Element '%s' not found",name);
75 newContainer->netcard = NULL;
79 // level depends on level of father
80 if (newContainer->father){
81 newContainer->level = newContainer->father->level+1;
82 XBT_DEBUG("new container %s, child of %s", name, father->name);
84 newContainer->level = 0;
86 // type definition (method depends on kind of this new container)
87 newContainer->kind = kind;
88 if (newContainer->kind == INSTR_AS){
89 //if this container is of an AS, its type name depends on its level
90 char as_typename[INSTR_DEFAULT_STR_SIZE];
91 snprintf (as_typename, INSTR_DEFAULT_STR_SIZE, "L%d", newContainer->level);
92 if (newContainer->father){
93 newContainer->type = PJ_type_get_or_null (as_typename, newContainer->father->type);
94 if (newContainer->type == NULL){
95 newContainer->type = PJ_type_container_new (as_typename, newContainer->father->type);
98 newContainer->type = PJ_type_container_new ("0", NULL);
101 //otherwise, the name is its kind
102 char typeNameBuff[INSTR_DEFAULT_STR_SIZE];
103 switch (newContainer->kind){
104 case INSTR_HOST: snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "HOST"); break;
105 case INSTR_LINK: snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "LINK"); break;
106 case INSTR_ROUTER: snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "ROUTER"); break;
107 case INSTR_SMPI: snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MPI"); break;
108 case INSTR_MSG_PROCESS: snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_PROCESS"); break;
109 case INSTR_MSG_VM: snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_VM"); break;
110 case INSTR_MSG_TASK: snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_TASK"); break;
111 default: THROWF (tracing_error, 0, "new container kind is unknown."); break;
113 type_t type = PJ_type_get_or_null (typeNameBuff, newContainer->father->type);
115 newContainer->type = PJ_type_container_new (typeNameBuff, newContainer->father->type);
117 newContainer->type = type;
120 newContainer->children = xbt_dict_new_homogeneous(NULL);
121 if (newContainer->father){
122 xbt_dict_set(newContainer->father->children, newContainer->name, newContainer, NULL);
123 new_pajeCreateContainer (newContainer);
126 //register all kinds by name
127 if (xbt_dict_get_or_null(allContainers, newContainer->name) != NULL){
128 THROWF(tracing_error, 1, "container %s already present in allContainers data structure", newContainer->name);
131 xbt_dict_set (allContainers, newContainer->name, newContainer, NULL);
132 XBT_DEBUG("Add container name '%s'",newContainer->name);
134 //register NODE types for triva configuration
135 if (newContainer->kind == INSTR_HOST || newContainer->kind == INSTR_LINK || newContainer->kind == INSTR_ROUTER) {
136 xbt_dict_set (trivaNodeTypes, newContainer->type->name, xbt_strdup("1"), NULL);
141 container_t PJ_container_get (const char *name)
143 container_t ret = PJ_container_get_or_null (name);
145 THROWF(tracing_error, 1, "container with name %s not found", name);
150 container_t PJ_container_get_or_null (const char *name)
152 return (container_t)(name ? xbt_dict_get_or_null(allContainers, name) : NULL);
155 container_t PJ_container_get_root ()
157 return rootContainer;
160 void PJ_container_remove_from_parent (container_t child)
163 THROWF (tracing_error, 0, "can't remove from parent with a NULL child");
166 container_t parent = child->father;
168 XBT_DEBUG("removeChildContainer (%s) FromContainer (%s) ",
171 xbt_dict_remove (parent->children, child->name);
175 void PJ_container_free (container_t container)
177 if (container == NULL){
178 THROWF (tracing_error, 0, "trying to free a NULL container");
180 XBT_DEBUG("destroy container %s", container->name);
182 //obligation to dump previous events because they might
183 //reference the container that is about to be destroyed
184 TRACE_last_timestamp_to_dump = surf_get_clock();
185 TRACE_paje_dump_buffer(1);
187 //trace my destruction
188 if (!TRACE_disable_destroy() && container != PJ_container_get_root()){
189 //do not trace the container destruction if user requests
190 //or if the container is root
191 new_pajeDestroyContainer(container);
194 //remove it from allContainers data structure
195 xbt_dict_remove (allContainers, container->name);
198 xbt_free (container->name);
199 xbt_free (container->id);
200 xbt_dict_free (&container->children);
201 xbt_free (container);
205 static void recursiveDestroyContainer (container_t container)
207 if (container == NULL){
208 THROWF (tracing_error, 0, "trying to recursively destroy a NULL container");
210 XBT_DEBUG("recursiveDestroyContainer %s", container->name);
211 xbt_dict_cursor_t cursor = NULL;
214 xbt_dict_foreach(container->children, cursor, child_name, child) {
215 recursiveDestroyContainer (child);
217 PJ_container_free (container);
220 void PJ_container_free_all ()
222 container_t root = PJ_container_get_root();
224 THROWF (tracing_error, 0, "trying to free all containers, but root is NULL");
226 recursiveDestroyContainer (root);
227 rootContainer = NULL;
230 if (!xbt_dict_is_empty(allContainers)){
231 THROWF(tracing_error, 0, "some containers still present even after destroying all of them");