Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add/update copyright notices.
[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 #ifdef HAVE_TRACING
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_paje_containers, instr, "Paje tracing event system (containers)");
15
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 */
20
21 long long int instr_new_paje_id (void)
22 {
23   static long long int type_id = 0;
24   return type_id++;
25 }
26
27 void PJ_container_alloc (void)
28 {
29   allContainers = xbt_dict_new_homogeneous(NULL);
30   trivaNodeTypes = xbt_dict_new_homogeneous(xbt_free);
31   trivaEdgeTypes = xbt_dict_new_homogeneous(xbt_free);
32 }
33
34 void PJ_container_release (void)
35 {
36   xbt_dict_free (&allContainers);
37   xbt_dict_free (&trivaNodeTypes);
38   xbt_dict_free (&trivaEdgeTypes);
39 }
40
41 void PJ_container_set_root (container_t root)
42 {
43   rootContainer = root;
44 }
45
46 container_t PJ_container_new (const char *name, e_container_types kind, container_t father)
47 {
48   if (name == NULL){
49     THROWF (tracing_error, 0, "can't create a container with a NULL name");
50   }
51
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++);
55
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
59   new->father = father;
60
61   //Search for network_element_t
62   switch (kind){
63     case INSTR_HOST:
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);
66       break;
67     case INSTR_ROUTER:
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);
70       break;
71     case INSTR_AS:
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);
74       break;
75     default:
76       new->net_elm = NULL;
77       break;
78   }
79
80   // level depends on level of father
81   if (new->father){
82     new->level = new->father->level+1;
83     XBT_DEBUG("new container %s, child of %s", name, father->name);
84   }else{
85     new->level = 0;
86   }
87   // type definition (method depends on kind of this new container)
88   new->kind = kind;
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);
93     if (new->father){
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);
97       }
98     }else{
99       new->type = PJ_type_container_new ("0", NULL);
100     }
101   }else{
102     //otherwise, the name is its kind
103     char typename[INSTR_DEFAULT_STR_SIZE];
104     switch (new->kind){
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_VM: snprintf (typename, INSTR_DEFAULT_STR_SIZE, "MSG_VM"); break;
111       case INSTR_MSG_TASK:    snprintf (typename, INSTR_DEFAULT_STR_SIZE, "MSG_TASK");    break;
112       default: THROWF (tracing_error, 0, "new container kind is unknown."); break;
113     }
114     type_t type = PJ_type_get_or_null (typename, new->father->type);
115     if (type == NULL){
116       new->type = PJ_type_container_new (typename, new->father->type);
117     }else{
118       new->type = type;
119     }
120   }
121   new->children = xbt_dict_new_homogeneous(NULL);
122   if (new->father){
123     xbt_dict_set(new->father->children, new->name, new, NULL);
124     new_pajeCreateContainer (new);
125   }
126
127   //register all kinds by name
128   if (xbt_dict_get_or_null(allContainers, new->name) != NULL){
129     THROWF(tracing_error, 1, "container %s already present in allContainers data structure", new->name);
130   }
131
132   xbt_dict_set (allContainers, new->name, new, NULL);
133   XBT_DEBUG("Add container name '%s'",new->name);
134
135   //register NODE types for triva configuration
136   if (new->kind == INSTR_HOST || new->kind == INSTR_LINK || new->kind == INSTR_ROUTER) {
137     xbt_dict_set (trivaNodeTypes, new->type->name, xbt_strdup("1"), NULL);
138   }
139
140   return new;
141 }
142
143 container_t PJ_container_get (const char *name)
144 {
145   container_t ret = PJ_container_get_or_null (name);
146   if (ret == NULL){
147     THROWF(tracing_error, 1, "container with name %s not found", name);
148   }
149   return ret;
150 }
151
152 container_t PJ_container_get_or_null (const char *name)
153 {
154   return name ? xbt_dict_get_or_null(allContainers, name) : NULL;
155 }
156
157 container_t PJ_container_get_root ()
158 {
159   return rootContainer;
160 }
161
162 void PJ_container_remove_from_parent (container_t child)
163 {
164   if (child == NULL){
165     THROWF (tracing_error, 0, "can't remove from parent with a NULL child");
166   }
167
168   container_t parent = child->father;
169   if (parent){
170     XBT_DEBUG("removeChildContainer (%s) FromContainer (%s) ",
171         child->name,
172         parent->name);
173     xbt_dict_remove (parent->children, child->name);
174   }
175 }
176
177 void PJ_container_free (container_t container)
178 {
179   if (container == NULL){
180     THROWF (tracing_error, 0, "trying to free a NULL container");
181   }
182   XBT_DEBUG("destroy container %s", container->name);
183
184   //obligation to dump previous events because they might
185   //reference the container that is about to be destroyed
186   TRACE_last_timestamp_to_dump = surf_get_clock();
187   TRACE_paje_dump_buffer(1);
188
189   //trace my destruction
190   if (!TRACE_disable_destroy() && container != PJ_container_get_root()){
191     //do not trace the container destruction if user requests
192     //or if the container is root
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_is_empty(allContainers)){
233     THROWF(tracing_error, 0, "some containers still present even after destroying all of them");
234   }
235 }
236
237 #endif /* HAVE_TRACING */