Logo AND Algorithmique Numérique Distribuée

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