Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use structured binding declarations (sonar, c++17).
[simgrid.git] / src / instr / instr_paje_containers.cpp
1 /* Copyright (c) 2010-2022. 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/Exception.hpp"
7 #include "simgrid/s4u/Engine.hpp"
8 #include "simgrid/s4u/Host.hpp"
9 #include "src/instr/instr_private.hpp"
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_paje_containers, instr, "Paje tracing event system (containers)");
12
13 namespace simgrid {
14 namespace instr {
15
16 Container* Container::root_container_ = nullptr;              /* the root container */
17 std::map<std::string, Container*, std::less<>> Container::all_containers_; /* all created containers indexed by name */
18
19 NetZoneContainer::NetZoneContainer(const std::string& name, unsigned int level, NetZoneContainer* parent)
20     : Container::Container(name, "", parent)
21 {
22   xbt_assert(s4u::Engine::get_instance()->netpoint_by_name_or_null(get_name()), "Element '%s' not found", get_cname());
23   if (parent_) {
24     std::string type_name = std::string("L") + std::to_string(level);
25     type_                 = parent_->type_->by_name_or_create<ContainerType>(type_name);
26     parent_->children_.try_emplace(get_name(), this);
27     on_creation(*this);
28   } else {
29     type_         = new ContainerType("0");
30     set_root(this);
31   }
32 }
33
34 RouterContainer::RouterContainer(const std::string& name, Container* parent)
35     : Container::Container(name, "ROUTER", parent)
36 {
37   xbt_assert(parent, "Only the Root container has no parent");
38   xbt_assert(s4u::Engine::get_instance()->netpoint_by_name_or_null(get_name()), "Element '%s' not found", get_cname());
39 }
40
41 HostContainer::HostContainer(s4u::Host const& host, NetZoneContainer* parent)
42     : Container::Container(host.get_name(), "HOST", parent)
43 {
44   xbt_assert(parent, "Only the Root container has no parent");
45   xbt_assert(host.get_netpoint(), "Element '%s' not found", host.get_cname());
46 }
47
48 Container::Container(const std::string& name, const std::string& type_name, Container* parent)
49     : name_(name), parent_(parent)
50 {
51   static long long int container_id = 0;
52   id_                               = container_id; // id (or alias) of the container
53   container_id++;
54
55   if (parent_) {
56     XBT_DEBUG("new container %s, child of %s", get_cname(), parent->get_cname());
57
58     if (not type_name.empty()) {
59       type_ = parent_->type_->by_name_or_create<ContainerType>(type_name);
60       parent_->children_.try_emplace(name_, this);
61       on_creation(*this);
62     }
63   }
64
65   //register all kinds by name
66   if (not all_containers_.try_emplace(name_, this).second)
67     throw TracingError(XBT_THROW_POINT,
68                        xbt::string_printf("container %s already present in all_containers_", get_cname()));
69
70   XBT_DEBUG("Add container name '%s'", get_cname());
71 }
72
73 Container::~Container()
74 {
75   XBT_DEBUG("destroy container %s", get_cname());
76   // Begin with destroying my own children
77   for (auto [_, child] : children_)
78     delete child;
79
80   // remove me from the all_containers_ data structure
81   all_containers_.erase(name_);
82
83   // obligation to dump previous events because they might reference the container that is about to be destroyed
84   last_timestamp_to_dump = simgrid_get_clock();
85   dump_buffer(true);
86
87   on_destruction(*this);
88 }
89
90 void Container::create_child(const std::string& name, const std::string& type_name)
91 {
92   new Container(name, type_name, this);
93 }
94
95 Container* Container::by_name_or_null(const std::string& name)
96 {
97   auto cont = all_containers_.find(name);
98   return cont == all_containers_.end() ? nullptr : cont->second;
99 }
100
101 Container* Container::by_name(const std::string& name)
102 {
103   Container* ret = Container::by_name_or_null(name);
104   xbt_assert(ret != nullptr, "container with name %s not found", name.c_str());
105
106   return ret;
107 }
108
109 void Container::remove_from_parent()
110 {
111   if (parent_) {
112     XBT_DEBUG("removeChildContainer (%s) FromContainer (%s) ", get_cname(), parent_->get_cname());
113     parent_->children_.erase(name_);
114   }
115   delete this;
116 }
117
118 StateType* Container::get_state(const std::string& name)
119 {
120   return static_cast<StateType*>(type_->by_name(name)->set_calling_container(this));
121 }
122
123 LinkType* Container::get_link(const std::string& name)
124 {
125   return static_cast<LinkType*>(type_->by_name(name)->set_calling_container(this));
126 }
127
128 VariableType* Container::get_variable(const std::string& name)
129 {
130   return static_cast<VariableType*>(type_->by_name(name)->set_calling_container(this));
131 }
132
133 EntityValue::EntityValue(const std::string& name, const std::string& color, Type* parent)
134     : name_(name), color_(color), parent_(parent)
135 {
136   on_creation(*this);
137 }
138
139 } // namespace instr
140 } // namespace simgrid