Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove useless #ifdef __cplusplus from hpp files.
[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, 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   // Begin with destroying my own children
136   for (auto child : children_) {
137     delete child.second;
138   }
139
140   // obligation to dump previous events because they might reference the container that is about to be destroyed
141   TRACE_last_timestamp_to_dump = surf_get_clock();
142   TRACE_paje_dump_buffer(1);
143
144   // trace my destruction
145   if (not TRACE_disable_destroy() && this != PJ_container_get_root()) {
146     // do not trace the container destruction if user requests or if the container is root
147     LogContainerDestruction(this);
148   }
149
150   // remove me from the allContainers data structure
151   allContainers.erase(name_);
152 }
153
154 simgrid::instr::Container* PJ_container_get(const char* name)
155 {
156   container_t ret = PJ_container_get_or_null (name);
157   if (ret == nullptr){
158     THROWF(tracing_error, 1, "container with name %s not found", name);
159   }
160   return ret;
161 }
162
163 simgrid::instr::Container* PJ_container_get_or_null(const char* name)
164 {
165   auto cont = allContainers.find(name);
166   return cont == allContainers.end() ? nullptr : cont->second;
167 }
168
169 simgrid::instr::Container* PJ_container_get_root()
170 {
171   return rootContainer;
172 }
173
174 void PJ_container_remove_from_parent (container_t child)
175 {
176   if (child == nullptr){
177     THROWF (tracing_error, 0, "can't remove from parent with a nullptr child");
178   }
179
180   container_t parent = child->father_;
181   if (parent){
182     XBT_DEBUG("removeChildContainer (%s) FromContainer (%s) ", child->name_.c_str(), parent->name_.c_str());
183     parent->children_.erase(child->name_);
184   }
185 }