Logo AND Algorithmique Numérique Distribuée

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