Logo AND Algorithmique Numérique Distribuée

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