1 /* Copyright (c) 2010-2020. 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 static container_t rootContainer = nullptr; /* the root container */
14 static std::map<std::string, container_t> allContainers; /* all created containers indexed by name */
16 long long int instr_new_paje_id ()
18 static long long int type_id = 0;
25 Container* Container::get_root()
30 NetZoneContainer::NetZoneContainer(const std::string& name, unsigned int level, NetZoneContainer* father)
31 : Container::Container(name, "", father)
33 netpoint_ = s4u::Engine::get_instance()->netpoint_by_name_or_null(get_name());
34 xbt_assert(netpoint_, "Element '%s' not found", get_cname());
36 std::string type_name = std::string("L") + std::to_string(level);
37 type_ = father_->type_->by_name_or_create<ContainerType>(type_name);
38 father_->children_.insert({get_name(), this});
41 type_ = new ContainerType("0");
46 RouterContainer::RouterContainer(const std::string& name, Container* father)
47 : Container::Container(name, "ROUTER", father)
49 xbt_assert(father, "Only the Root container has no father");
51 netpoint_ = s4u::Engine::get_instance()->netpoint_by_name_or_null(get_name());
52 xbt_assert(netpoint_, "Element '%s' not found", get_cname());
55 HostContainer::HostContainer(s4u::Host const& host, NetZoneContainer* father)
56 : Container::Container(host.get_name(), "HOST", father)
58 xbt_assert(father, "Only the Root container has no father");
60 netpoint_ = host.get_netpoint();
61 xbt_assert(netpoint_, "Element '%s' not found", host.get_cname());
64 Container::Container(const std::string& name, const std::string& type_name, Container* father)
65 : name_(name), father_(father)
67 static long long int container_id = 0;
68 id_ = container_id; // id (or alias) of the container
72 XBT_DEBUG("new container %s, child of %s", get_cname(), father->get_cname());
74 if (not type_name.empty()) {
75 type_ = father_->type_->by_name_or_create<ContainerType>(type_name);
76 father_->children_.insert({name_, this});
81 //register all kinds by name
82 if (not allContainers.emplace(name_, this).second)
83 throw TracingError(XBT_THROW_POINT,
84 xbt::string_printf("container %s already present in allContainers data structure", get_cname()));
86 XBT_DEBUG("Add container name '%s'", get_cname());
89 Container::~Container()
91 XBT_DEBUG("destroy container %s", get_cname());
92 // Begin with destroying my own children
93 for (auto child : children_)
96 // remove me from the allContainers data structure
97 allContainers.erase(name_);
99 on_destruction(*this);
102 void Container::create_child(const std::string& name, const std::string& type_name)
104 new Container(name, type_name, this);
107 Container* Container::by_name_or_null(const std::string& name)
109 auto cont = allContainers.find(name);
110 return cont == allContainers.end() ? nullptr : cont->second;
113 Container* Container::by_name(const std::string& name)
115 Container* ret = Container::by_name_or_null(name);
116 xbt_assert(ret != nullptr, "container with name %s not found", name.c_str());
121 void Container::remove_from_parent()
124 XBT_DEBUG("removeChildContainer (%s) FromContainer (%s) ", get_cname(), father_->get_cname());
125 father_->children_.erase(name_);
130 StateType* Container::get_state(const std::string& name)
132 return static_cast<StateType*>(type_->by_name(name)->set_calling_container(this));
135 LinkType* Container::get_link(const std::string& name)
137 return static_cast<LinkType*>(type_->by_name(name)->set_calling_container(this));
140 VariableType* Container::get_variable(const std::string& name)
142 return static_cast<VariableType*>(type_->by_name(name)->set_calling_container(this));
145 } // namespace simgrid