1 /* Copyright (c) 2010-2022. The SimGrid Team. All rights reserved. */
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. */
6 #include "simgrid/Exception.hpp"
7 #include "simgrid/s4u/Engine.hpp"
8 #include "simgrid/s4u/Host.hpp"
9 #include "src/instr/instr_private.hpp"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_paje_containers, instr, "Paje tracing event system (containers)");
13 namespace simgrid::instr {
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 */
18 NetZoneContainer::NetZoneContainer(const std::string& name, unsigned int level, NetZoneContainer* parent)
19 : Container::Container(name, "", parent)
21 xbt_assert(s4u::Engine::get_instance()->netpoint_by_name_or_null(get_name()), "Element '%s' not found", get_cname());
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);
28 type_ = new ContainerType("0");
33 RouterContainer::RouterContainer(const std::string& name, Container* parent)
34 : Container::Container(name, "ROUTER", parent)
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());
40 HostContainer::HostContainer(s4u::Host const& host, NetZoneContainer* parent)
41 : Container::Container(host.get_name(), "HOST", parent)
43 xbt_assert(parent, "Only the Root container has no parent");
44 xbt_assert(host.get_netpoint(), "Element '%s' not found", host.get_cname());
47 Container::Container(const std::string& name, const std::string& type_name, Container* parent)
48 : name_(name), parent_(parent)
50 static long long int container_id = 0;
51 id_ = container_id; // id (or alias) of the container
55 XBT_DEBUG("new container %s, child of %s", get_cname(), parent->get_cname());
57 if (not type_name.empty()) {
58 type_ = parent_->type_->by_name_or_create<ContainerType>(type_name);
59 parent_->children_.try_emplace(name_, this);
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()));
69 XBT_DEBUG("Add container name '%s'", get_cname());
72 Container::~Container()
74 XBT_DEBUG("destroy container %s", get_cname());
75 // Begin with destroying my own children
76 for (auto const& [_, child] : children_)
79 // remove me from the all_containers_ data structure
80 all_containers_.erase(name_);
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();
86 on_destruction(*this);
89 void Container::create_child(const std::string& name, const std::string& type_name)
91 new Container(name, type_name, this);
94 Container* Container::by_name_or_null(const std::string& name)
96 auto cont = all_containers_.find(name);
97 return cont == all_containers_.end() ? nullptr : cont->second;
100 Container* Container::by_name(const std::string& name)
102 Container* ret = Container::by_name_or_null(name);
103 xbt_assert(ret != nullptr, "container with name %s not found", name.c_str());
108 void Container::remove_from_parent()
111 XBT_DEBUG("removeChildContainer (%s) FromContainer (%s) ", get_cname(), parent_->get_cname());
112 parent_->children_.erase(name_);
117 StateType* Container::get_state(const std::string& name)
119 return static_cast<StateType*>(type_->by_name(name)->set_calling_container(this));
122 LinkType* Container::get_link(const std::string& name)
124 return static_cast<LinkType*>(type_->by_name(name)->set_calling_container(this));
127 VariableType* Container::get_variable(const std::string& name)
129 return static_cast<VariableType*>(type_->by_name(name)->set_calling_container(this));
132 EntityValue::EntityValue(const std::string& name, const std::string& color, Type* parent)
133 : name_(name), color_(color), parent_(parent)
138 } // namespace simgrid::instr