Logo AND Algorithmique Numérique Distribuée

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