Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
752649eeb3eac46d1f8f5e348f410c08c56c24e7
[simgrid.git] / src / instr / instr_paje_containers.c
1 /* Copyright (c) 2010, 2012-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "instr/instr_private.h"
8 #include "xbt/lib.h"
9 #include "surf/surf.h"
10 #include "surf/surf_routing.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_paje_containers, instr, "Paje tracing event system (containers)");
13
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 */
18
19 long long int instr_new_paje_id (void)
20 {
21   static long long int type_id = 0;
22   return type_id++;
23 }
24
25 void PJ_container_alloc (void)
26 {
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);
30 }
31
32 void PJ_container_release (void)
33 {
34   xbt_dict_free (&allContainers);
35   xbt_dict_free (&trivaNodeTypes);
36   xbt_dict_free (&trivaEdgeTypes);
37 }
38
39 void PJ_container_set_root (container_t root)
40 {
41   rootContainer = root;
42 }
43
44 container_t PJ_container_new (const char *name, e_container_types kind, container_t father)
45 {
46   if (name == NULL){
47     THROWF (tracing_error, 0, "can't create a container with a NULL name");
48   }
49
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++);
53
54   container_t new = xbt_new0(s_container_t, 1);
55   new->name = xbt_strdup (name); // name of the container
56   new->id = xbt_strdup (id_str); // id (or alias) of the container
57   new->father = father;
58
59   //Search for network_element_t
60   switch (kind){
61     case INSTR_HOST:
62       new->net_elm = xbt_lib_get_or_null(host_lib,name,ROUTING_HOST_LEVEL);
63       if(!new->net_elm) xbt_die("Element '%s' not found",name);
64       break;
65     case INSTR_ROUTER:
66       new->net_elm = xbt_lib_get_or_null(as_router_lib,name,ROUTING_ASR_LEVEL);
67       if(!new->net_elm) xbt_die("Element '%s' not found",name);
68       break;
69     case INSTR_AS:
70       new->net_elm = xbt_lib_get_or_null(as_router_lib,name,ROUTING_ASR_LEVEL);
71       if(!new->net_elm) xbt_die("Element '%s' not found",name);
72       break;
73     default:
74       new->net_elm = NULL;
75       break;
76   }
77
78   // level depends on level of father
79   if (new->father){
80     new->level = new->father->level+1;
81     XBT_DEBUG("new container %s, child of %s", name, father->name);
82   }else{
83     new->level = 0;
84   }
85   // type definition (method depends on kind of this new container)
86   new->kind = kind;
87   if (new->kind == INSTR_AS){
88     //if this container is of an AS, its type name depends on its level
89     char as_typename[INSTR_DEFAULT_STR_SIZE];
90     snprintf (as_typename, INSTR_DEFAULT_STR_SIZE, "L%d", new->level);
91     if (new->father){
92       new->type = PJ_type_get_or_null (as_typename, new->father->type);
93       if (new->type == NULL){
94         new->type = PJ_type_container_new (as_typename, new->father->type);
95       }
96     }else{
97       new->type = PJ_type_container_new ("0", NULL);
98     }
99   }else{
100     //otherwise, the name is its kind
101     char typename[INSTR_DEFAULT_STR_SIZE];
102     switch (new->kind){
103       case INSTR_HOST:        snprintf (typename, INSTR_DEFAULT_STR_SIZE, "HOST");        break;
104       case INSTR_LINK:        snprintf (typename, INSTR_DEFAULT_STR_SIZE, "LINK");        break;
105       case INSTR_ROUTER:      snprintf (typename, INSTR_DEFAULT_STR_SIZE, "ROUTER");      break;
106       case INSTR_SMPI:        snprintf (typename, INSTR_DEFAULT_STR_SIZE, "MPI");         break;
107       case INSTR_MSG_PROCESS: snprintf (typename, INSTR_DEFAULT_STR_SIZE, "MSG_PROCESS"); break;
108       case INSTR_MSG_VM: snprintf (typename, INSTR_DEFAULT_STR_SIZE, "MSG_VM"); break;
109       case INSTR_MSG_TASK:    snprintf (typename, INSTR_DEFAULT_STR_SIZE, "MSG_TASK");    break;
110       default: THROWF (tracing_error, 0, "new container kind is unknown."); break;
111     }
112     type_t type = PJ_type_get_or_null (typename, new->father->type);
113     if (type == NULL){
114       new->type = PJ_type_container_new (typename, new->father->type);
115     }else{
116       new->type = type;
117     }
118   }
119   new->children = xbt_dict_new_homogeneous(NULL);
120   if (new->father){
121     xbt_dict_set(new->father->children, new->name, new, NULL);
122     new_pajeCreateContainer (new);
123   }
124
125   //register all kinds by name
126   if (xbt_dict_get_or_null(allContainers, new->name) != NULL){
127     THROWF(tracing_error, 1, "container %s already present in allContainers data structure", new->name);
128   }
129
130   xbt_dict_set (allContainers, new->name, new, NULL);
131   XBT_DEBUG("Add container name '%s'",new->name);
132
133   //register NODE types for triva configuration
134   if (new->kind == INSTR_HOST || new->kind == INSTR_LINK || new->kind == INSTR_ROUTER) {
135     xbt_dict_set (trivaNodeTypes, new->type->name, xbt_strdup("1"), NULL);
136   }
137
138   return new;
139 }
140
141 container_t PJ_container_get (const char *name)
142 {
143   container_t ret = PJ_container_get_or_null (name);
144   if (ret == NULL){
145     THROWF(tracing_error, 1, "container with name %s not found", name);
146   }
147   return ret;
148 }
149
150 container_t PJ_container_get_or_null (const char *name)
151 {
152   return name ? xbt_dict_get_or_null(allContainers, name) : NULL;
153 }
154
155 container_t PJ_container_get_root ()
156 {
157   return rootContainer;
158 }
159
160 void PJ_container_remove_from_parent (container_t child)
161 {
162   if (child == NULL){
163     THROWF (tracing_error, 0, "can't remove from parent with a NULL child");
164   }
165
166   container_t parent = child->father;
167   if (parent){
168     XBT_DEBUG("removeChildContainer (%s) FromContainer (%s) ",
169         child->name,
170         parent->name);
171     xbt_dict_remove (parent->children, child->name);
172   }
173 }
174
175 void PJ_container_free (container_t container)
176 {
177   if (container == NULL){
178     THROWF (tracing_error, 0, "trying to free a NULL container");
179   }
180   XBT_DEBUG("destroy container %s", container->name);
181
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);
186
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);
192   }
193
194   //remove it from allContainers data structure
195   xbt_dict_remove (allContainers, container->name);
196
197   //free
198   xbt_free (container->name);
199   xbt_free (container->id);
200   xbt_dict_free (&container->children);
201   xbt_free (container);
202   container = NULL;
203 }
204
205 static void recursiveDestroyContainer (container_t container)
206 {
207   if (container == NULL){
208     THROWF (tracing_error, 0, "trying to recursively destroy a NULL container");
209   }
210   XBT_DEBUG("recursiveDestroyContainer %s", container->name);
211   xbt_dict_cursor_t cursor = NULL;
212   container_t child;
213   char *child_name;
214   xbt_dict_foreach(container->children, cursor, child_name, child) {
215     recursiveDestroyContainer (child);
216   }
217   PJ_container_free (container);
218 }
219
220 void PJ_container_free_all ()
221 {
222   container_t root = PJ_container_get_root();
223   if (root == NULL){
224     THROWF (tracing_error, 0, "trying to free all containers, but root is NULL");
225   }
226   recursiveDestroyContainer (root);
227   rootContainer = NULL;
228
229   //checks
230   if (!xbt_dict_is_empty(allContainers)){
231     THROWF(tracing_error, 0, "some containers still present even after destroying all of them");
232   }
233 }