Logo AND Algorithmique Numérique Distribuée

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