1 /* Copyright (c) 2010. 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 "instr/instr_private.h"
10 #include "surf/surf_routing.h"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_paje_containers, instr, "Paje tracing event system (containers)");
16 static container_t rootContainer = NULL; /* the root container */
17 static xbt_dict_t allContainers = NULL; /* all created containers indexed by name */
18 xbt_dict_t trivaNodeTypes = NULL; /* all host types defined */
19 xbt_dict_t trivaEdgeTypes = NULL; /* all link types defined */
21 long long int instr_new_paje_id (void)
23 static long long int type_id = 0;
27 void PJ_container_alloc (void)
29 allContainers = xbt_dict_new_homogeneous(NULL);
30 trivaNodeTypes = xbt_dict_new_homogeneous(xbt_free);
31 trivaEdgeTypes = xbt_dict_new_homogeneous(xbt_free);
34 void PJ_container_release (void)
36 xbt_dict_free (&allContainers);
37 xbt_dict_free (&trivaNodeTypes);
38 xbt_dict_free (&trivaEdgeTypes);
41 void PJ_container_set_root (container_t root)
46 container_t PJ_container_new (const char *name, e_container_types kind, container_t father)
49 THROWF (tracing_error, 0, "can't create a container with a NULL name");
52 static long long int container_id = 0;
53 char id_str[INSTR_DEFAULT_STR_SIZE];
54 snprintf (id_str, INSTR_DEFAULT_STR_SIZE, "%lld", container_id++);
56 container_t new = xbt_new0(s_container_t, 1);
57 new->name = xbt_strdup (name); // name of the container
58 new->id = xbt_strdup (id_str); // id (or alias) of the container
61 //Search for network_element_t
64 new->net_elm = xbt_lib_get_or_null(host_lib,name,ROUTING_HOST_LEVEL);
65 if(!new->net_elm) xbt_die("Element '%s' not found",name);
68 new->net_elm = xbt_lib_get_or_null(as_router_lib,name,ROUTING_ASR_LEVEL);
69 if(!new->net_elm) xbt_die("Element '%s' not found",name);
72 new->net_elm = xbt_lib_get_or_null(as_router_lib,name,ROUTING_ASR_LEVEL);
73 if(!new->net_elm) xbt_die("Element '%s' not found",name);
80 // level depends on level of father
82 new->level = new->father->level+1;
83 XBT_DEBUG("new container %s, child of %s", name, father->name);
87 // type definition (method depends on kind of this new container)
89 if (new->kind == INSTR_AS){
90 //if this container is of an AS, its type name depends on its level
91 char as_typename[INSTR_DEFAULT_STR_SIZE];
92 snprintf (as_typename, INSTR_DEFAULT_STR_SIZE, "L%d", new->level);
94 new->type = PJ_type_get_or_null (as_typename, new->father->type);
95 if (new->type == NULL){
96 new->type = PJ_type_container_new (as_typename, new->father->type);
99 new->type = PJ_type_container_new ("0", NULL);
102 //otherwise, the name is its kind
103 char typename[INSTR_DEFAULT_STR_SIZE];
105 case INSTR_HOST: snprintf (typename, INSTR_DEFAULT_STR_SIZE, "HOST"); break;
106 case INSTR_LINK: snprintf (typename, INSTR_DEFAULT_STR_SIZE, "LINK"); break;
107 case INSTR_ROUTER: snprintf (typename, INSTR_DEFAULT_STR_SIZE, "ROUTER"); break;
108 case INSTR_SMPI: snprintf (typename, INSTR_DEFAULT_STR_SIZE, "MPI"); break;
109 case INSTR_MSG_PROCESS: snprintf (typename, INSTR_DEFAULT_STR_SIZE, "MSG_PROCESS"); break;
110 case INSTR_MSG_TASK: snprintf (typename, 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 (typename, new->father->type);
115 new->type = PJ_type_container_new (typename, new->father->type);
120 new->children = xbt_dict_new_homogeneous(NULL);
122 xbt_dict_set(new->father->children, new->name, new, NULL);
123 new_pajeCreateContainer (new);
126 //register all kinds by name
127 if (xbt_dict_get_or_null(allContainers, new->name) != NULL){
128 THROWF(tracing_error, 1, "container %s already present in allContainers data structure", new->name);
131 xbt_dict_set (allContainers, new->name, new, NULL);
132 XBT_DEBUG("Add container name '%s'",new->name);
134 //register NODE types for triva configuration
135 if (new->kind == INSTR_HOST || new->kind == INSTR_LINK || new->kind == INSTR_ROUTER) {
136 xbt_dict_set (trivaNodeTypes, new->type->name, xbt_strdup("1"), NULL);
142 container_t PJ_container_get (const char *name)
144 container_t ret = PJ_container_get_or_null (name);
146 THROWF(tracing_error, 1, "container with name %s not found", name);
151 container_t PJ_container_get_or_null (const char *name)
153 return name ? xbt_dict_get_or_null(allContainers, name) : NULL;
156 container_t PJ_container_get_root ()
158 return rootContainer;
161 void PJ_container_remove_from_parent (container_t child)
164 THROWF (tracing_error, 0, "can't remove from parent with a NULL child");
167 container_t parent = child->father;
169 XBT_DEBUG("removeChildContainer (%s) FromContainer (%s) ",
172 xbt_dict_remove (parent->children, child->name);
176 void PJ_container_free (container_t container)
178 if (container == NULL){
179 THROWF (tracing_error, 0, "trying to free a NULL container");
181 XBT_DEBUG("destroy container %s", container->name);
183 //obligation to dump previous events because they might
184 //reference the container that is about to be destroyed
185 TRACE_last_timestamp_to_dump = surf_get_clock();
186 TRACE_paje_dump_buffer(1);
188 //trace my destruction
189 if (!TRACE_disable_destroy() && container != PJ_container_get_root()){
190 //do not trace the container destruction if user requests
191 //or if the container is root
192 new_pajeDestroyContainer(container);
195 //remove it from allContainers data structure
196 xbt_dict_remove (allContainers, container->name);
199 xbt_free (container->name);
200 xbt_free (container->id);
201 xbt_dict_free (&container->children);
202 xbt_free (container);
206 static void recursiveDestroyContainer (container_t container)
208 if (container == NULL){
209 THROWF (tracing_error, 0, "trying to recursively destroy a NULL container");
211 XBT_DEBUG("recursiveDestroyContainer %s", container->name);
212 xbt_dict_cursor_t cursor = NULL;
215 xbt_dict_foreach(container->children, cursor, child_name, child) {
216 recursiveDestroyContainer (child);
218 PJ_container_free (container);
221 void PJ_container_free_all ()
223 container_t root = PJ_container_get_root();
225 THROWF (tracing_error, 0, "trying to free all containers, but root is NULL");
227 recursiveDestroyContainer (root);
228 rootContainer = NULL;
231 if (xbt_dict_length(allContainers) != 0){
232 THROWF(tracing_error, 0, "some containers still present even after destroying all of them");
236 #endif /* HAVE_TRACING */