Logo AND Algorithmique Numérique Distribuée

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