Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
sloppy replacement of dict by map and char* by string
[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 #include <unordered_map>
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_paje_containers, instr, "Paje tracing event system (containers)");
16
17 static container_t rootContainer = nullptr;    /* the root container */
18 static std::unordered_map<std::string, simgrid::instr::Container*>
19     allContainers;                              /* all created containers indexed by name */
20 std::set<std::string> trivaNodeTypes;           /* all host types defined */
21 std::set<std::string> trivaEdgeTypes;           /* all link types defined */
22
23 long long int instr_new_paje_id ()
24 {
25   static long long int type_id = 0;
26   return type_id++;
27 }
28
29 void PJ_container_set_root (container_t root)
30 {
31   rootContainer = root;
32 }
33
34 simgrid::instr::Container::Container(std::string name, simgrid::instr::e_container_types kind, Container* father)
35     : name_(name), kind_(kind), father_(father)
36 {
37   static long long int container_id = 0;
38   id_                               = std::to_string(container_id); // id (or alias) of the container
39   container_id++;
40
41   //Search for network_element_t
42   switch (kind){
43     case simgrid::instr::INSTR_HOST:
44       this->netpoint_ = sg_host_by_name(name.c_str())->pimpl_netpoint;
45       xbt_assert(this->netpoint_, "Element '%s' not found", name.c_str());
46       break;
47     case simgrid::instr::INSTR_ROUTER:
48       this->netpoint_ = simgrid::s4u::Engine::getInstance()->getNetpointByNameOrNull(name);
49       xbt_assert(this->netpoint_, "Element '%s' not found", name.c_str());
50       break;
51     case simgrid::instr::INSTR_AS:
52       this->netpoint_ = simgrid::s4u::Engine::getInstance()->getNetpointByNameOrNull(name);
53       xbt_assert(this->netpoint_, "Element '%s' not found", name.c_str());
54       break;
55     default:
56       this->netpoint_ = nullptr;
57       break;
58   }
59
60   if (father_) {
61     this->level_ = father_->level_ + 1;
62     XBT_DEBUG("new container %s, child of %s", name.c_str(), father->name_.c_str());
63   }
64
65   // type definition (method depends on kind of this new container)
66   if (this->kind_ == simgrid::instr::INSTR_AS) {
67     //if this container is of an AS, its type name depends on its level
68     char as_typename[INSTR_DEFAULT_STR_SIZE];
69     snprintf(as_typename, INSTR_DEFAULT_STR_SIZE, "L%d", this->level_);
70     if (this->father_) {
71       this->type_ = this->father_->type_->getChildOrNull(as_typename);
72       if (this->type_ == nullptr) {
73         this->type_ = simgrid::instr::Type::containerNew(as_typename, this->father_->type_);
74       }
75     }else{
76       this->type_ = simgrid::instr::Type::containerNew("0", nullptr);
77     }
78   } else {
79     //otherwise, the name is its kind
80     char typeNameBuff[INSTR_DEFAULT_STR_SIZE];
81     switch (this->kind_) {
82       case simgrid::instr::INSTR_HOST:
83         snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "HOST");
84         break;
85       case simgrid::instr::INSTR_LINK:
86         snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "LINK");
87         break;
88       case simgrid::instr::INSTR_ROUTER:
89         snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "ROUTER");
90         break;
91       case simgrid::instr::INSTR_SMPI:
92         snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MPI");
93         break;
94       case simgrid::instr::INSTR_MSG_PROCESS:
95         snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_PROCESS");
96         break;
97       case simgrid::instr::INSTR_MSG_VM:
98         snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_VM");
99         break;
100       case simgrid::instr::INSTR_MSG_TASK:
101         snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_TASK");
102         break;
103       default:
104         THROWF (tracing_error, 0, "new container kind is unknown.");
105         break;
106     }
107     simgrid::instr::Type* type = this->father_->type_->getChildOrNull(typeNameBuff);
108     if (type == nullptr){
109       this->type_ = simgrid::instr::Type::containerNew(typeNameBuff, this->father_->type_);
110     }else{
111       this->type_ = type;
112     }
113   }
114   if (this->father_) {
115     this->father_->children_.insert({this->name_, this});
116     LogContainerCreation(this);
117   }
118
119   //register all kinds by name
120   if (not allContainers.emplace(this->name_, this).second) {
121     THROWF(tracing_error, 1, "container %s already present in allContainers data structure", this->name_.c_str());
122   }
123
124   XBT_DEBUG("Add container name '%s'", this->name_.c_str());
125
126   //register NODE types for triva configuration
127   if (this->kind_ == simgrid::instr::INSTR_HOST || this->kind_ == simgrid::instr::INSTR_LINK ||
128       this->kind_ == simgrid::instr::INSTR_ROUTER) {
129     trivaNodeTypes.insert(this->type_->name_);
130   }
131 }
132 simgrid::instr::Container::~Container()
133 {
134   XBT_DEBUG("destroy container %s", name_.c_str());
135
136   // obligation to dump previous events because they might
137   // reference the container that is about to be destroyed
138   TRACE_last_timestamp_to_dump = surf_get_clock();
139   TRACE_paje_dump_buffer(1);
140
141   // trace my destruction
142   if (not TRACE_disable_destroy() && this != PJ_container_get_root()) {
143     // do not trace the container destruction if user requests or if the container is root
144     LogContainerDestruction(this);
145   }
146
147   // remove it from allContainers data structure
148   allContainers.erase(name_);
149
150   // free
151 }
152
153 simgrid::instr::Container* PJ_container_get(const char* name)
154 {
155   container_t ret = PJ_container_get_or_null (name);
156   if (ret == nullptr){
157     THROWF(tracing_error, 1, "container with name %s not found", name);
158   }
159   return ret;
160 }
161
162 simgrid::instr::Container* PJ_container_get_or_null(const char* name)
163 {
164   auto cont = allContainers.find(name);
165   return cont == allContainers.end() ? nullptr : cont->second;
166 }
167
168 simgrid::instr::Container* PJ_container_get_root()
169 {
170   return rootContainer;
171 }
172
173 void PJ_container_remove_from_parent (container_t child)
174 {
175   if (child == nullptr){
176     THROWF (tracing_error, 0, "can't remove from parent with a nullptr child");
177   }
178
179   container_t parent = child->father_;
180   if (parent){
181     XBT_DEBUG("removeChildContainer (%s) FromContainer (%s) ", child->name_.c_str(), parent->name_.c_str());
182     parent->children_.erase(child->name_);
183   }
184 }
185
186 static void recursiveDestroyContainer (container_t container)
187 {
188   if (container == nullptr){
189     THROWF (tracing_error, 0, "trying to recursively destroy a nullptr container");
190   }
191   XBT_DEBUG("recursiveDestroyContainer %s", container->name_.c_str());
192   for (auto child : container->children_) {
193     recursiveDestroyContainer(child.second);
194   }
195   delete container;
196 }
197
198 void PJ_container_free_all ()
199 {
200   container_t root = PJ_container_get_root();
201   if (root == nullptr){
202     THROWF (tracing_error, 0, "trying to free all containers, but root is nullptr");
203   }
204   recursiveDestroyContainer (root);
205   rootContainer = nullptr;
206
207   //checks
208   if (not allContainers.empty()) {
209     THROWF(tracing_error, 0, "some containers still present even after destroying all of them");
210   }
211 }