Logo AND Algorithmique Numérique Distribuée

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