Logo AND Algorithmique Numérique Distribuée

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