Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill another dict in instr
[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.hpp"
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, container_t> allContainers; /* all created containers indexed by name */
19 std::set<std::string> trivaNodeTypes;           /* all host types defined */
20 std::set<std::string> trivaEdgeTypes;           /* all link types defined */
21
22 long long int instr_new_paje_id ()
23 {
24   static long long int type_id = 0;
25   return type_id++;
26 }
27
28 void PJ_container_set_root (container_t root)
29 {
30   rootContainer = root;
31 }
32
33 simgrid::instr::Container::Container(std::string name, simgrid::instr::e_container_types kind, Container* father)
34     : name_(name), kind_(kind), father_(father)
35 {
36   static long long int container_id = 0;
37   id_                               = std::to_string(container_id); // id (or alias) of the container
38   container_id++;
39
40   //Search for network_element_t
41   switch (kind){
42     case simgrid::instr::INSTR_HOST:
43       this->netpoint_ = sg_host_by_name(name.c_str())->pimpl_netpoint;
44       xbt_assert(this->netpoint_, "Element '%s' not found", name.c_str());
45       break;
46     case simgrid::instr::INSTR_ROUTER:
47       this->netpoint_ = simgrid::s4u::Engine::getInstance()->getNetpointByNameOrNull(name);
48       xbt_assert(this->netpoint_, "Element '%s' not found", name.c_str());
49       break;
50     case simgrid::instr::INSTR_AS:
51       this->netpoint_ = simgrid::s4u::Engine::getInstance()->getNetpointByNameOrNull(name);
52       xbt_assert(this->netpoint_, "Element '%s' not found", name.c_str());
53       break;
54     default:
55       this->netpoint_ = nullptr;
56       break;
57   }
58
59   if (father_) {
60     this->level_ = father_->level_ + 1;
61     XBT_DEBUG("new container %s, child of %s", name.c_str(), father->name_.c_str());
62   }
63
64   // type definition (method depends on kind of this new container)
65   if (this->kind_ == simgrid::instr::INSTR_AS) {
66     //if this container is of an AS, its type name depends on its level
67     char as_typename[INSTR_DEFAULT_STR_SIZE];
68     snprintf(as_typename, INSTR_DEFAULT_STR_SIZE, "L%d", this->level_);
69     if (this->father_) {
70       this->type_ = this->father_->type_->getChildOrNull(as_typename);
71       if (this->type_ == nullptr) {
72         this->type_ = simgrid::instr::Type::containerNew(as_typename, this->father_->type_);
73       }
74     }else{
75       this->type_ = simgrid::instr::Type::containerNew("0", nullptr);
76     }
77   } else {
78     //otherwise, the name is its kind
79     char typeNameBuff[INSTR_DEFAULT_STR_SIZE];
80     switch (this->kind_) {
81       case simgrid::instr::INSTR_HOST:
82         snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "HOST");
83         break;
84       case simgrid::instr::INSTR_LINK:
85         snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "LINK");
86         break;
87       case simgrid::instr::INSTR_ROUTER:
88         snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "ROUTER");
89         break;
90       case simgrid::instr::INSTR_SMPI:
91         snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MPI");
92         break;
93       case simgrid::instr::INSTR_MSG_PROCESS:
94         snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_PROCESS");
95         break;
96       case simgrid::instr::INSTR_MSG_VM:
97         snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_VM");
98         break;
99       case simgrid::instr::INSTR_MSG_TASK:
100         snprintf (typeNameBuff, INSTR_DEFAULT_STR_SIZE, "MSG_TASK");
101         break;
102       default:
103         THROWF (tracing_error, 0, "new container kind is unknown.");
104         break;
105     }
106     simgrid::instr::Type* type = this->father_->type_->getChildOrNull(typeNameBuff);
107     if (type == nullptr){
108       this->type_ = simgrid::instr::Type::containerNew(typeNameBuff, this->father_->type_);
109     }else{
110       this->type_ = type;
111     }
112   }
113   if (this->father_) {
114     this->father_->children_.insert({this->name_, this});
115     LogContainerCreation(this);
116   }
117
118   //register all kinds by name
119   if (not allContainers.emplace(this->name_, this).second) {
120     THROWF(tracing_error, 1, "container %s already present in allContainers data structure", this->name_.c_str());
121   }
122
123   XBT_DEBUG("Add container name '%s'", this->name_.c_str());
124
125   //register NODE types for triva configuration
126   if (this->kind_ == simgrid::instr::INSTR_HOST || this->kind_ == simgrid::instr::INSTR_LINK ||
127       this->kind_ == simgrid::instr::INSTR_ROUTER) {
128     trivaNodeTypes.insert(this->type_->name_);
129   }
130 }
131 simgrid::instr::Container::~Container()
132 {
133   XBT_DEBUG("destroy container %s", name_.c_str());
134   // Begin with destroying my own children
135   for (auto child : children_) {
136     delete child.second;
137   }
138
139   // obligation to dump previous events because they might reference the container that is about to be destroyed
140   TRACE_last_timestamp_to_dump = surf_get_clock();
141   TRACE_paje_dump_buffer(1);
142
143   // trace my destruction
144   if (not TRACE_disable_destroy() && this != PJ_container_get_root()) {
145     // do not trace the container destruction if user requests or if the container is root
146     LogContainerDestruction(this);
147   }
148
149   // remove me from the allContainers data structure
150   allContainers.erase(name_);
151 }
152
153 simgrid::instr::Container* simgrid::instr::Container::byNameOrNull(std::string name)
154 {
155   auto cont = allContainers.find(name);
156   return cont == allContainers.end() ? nullptr : cont->second;
157 }
158
159 simgrid::instr::Container* simgrid::instr::Container::byName(std::string name)
160 {
161   container_t ret = simgrid::instr::Container::byNameOrNull(name);
162   if (ret == nullptr)
163     THROWF(tracing_error, 1, "container with name %s not found", name.c_str());
164
165   return ret;
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 }