1 /* Copyright (c) 2010-2018. 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 <xbt/config.hpp>
8 #include "simgrid/s4u/Engine.hpp"
9 #include "simgrid/s4u/Host.hpp"
10 #include "src/instr/instr_private.hpp"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_paje_containers, instr, "Paje tracing event system (containers)");
14 extern FILE* tracing_file;
15 extern std::map<container_t, FILE*> tracing_files; // TI specific
16 double prefix = 0.0; // TI specific
18 static container_t rootContainer = nullptr; /* the root container */
19 static std::map<std::string, container_t> allContainers; /* all created containers indexed by name */
20 std::set<std::string> trivaNodeTypes; /* all host types defined */
21 std::set<std::string> trivaEdgeTypes; /* all link types defined */
23 long long int instr_new_paje_id ()
25 static long long int type_id = 0;
32 container_t Container::getRoot()
37 NetZoneContainer::NetZoneContainer(std::string name, unsigned int level, NetZoneContainer* father)
38 : Container::Container(name, "", father)
40 netpoint_ = simgrid::s4u::Engine::getInstance()->getNetpointByNameOrNull(name);
41 xbt_assert(netpoint_, "Element '%s' not found", name.c_str());
43 type_ = father_->type_->getOrCreateContainerType(std::string("L") + std::to_string(level));
44 father_->children_.insert({get_name(), this});
47 type_ = new ContainerType("0");
52 RouterContainer::RouterContainer(std::string name, Container* father) : Container::Container(name, "ROUTER", father)
54 xbt_assert(father, "Only the Root container has no father");
56 netpoint_ = simgrid::s4u::Engine::getInstance()->getNetpointByNameOrNull(name);
57 xbt_assert(netpoint_, "Element '%s' not found", name.c_str());
59 trivaNodeTypes.insert(type_->get_name());
62 HostContainer::HostContainer(simgrid::s4u::Host& host, NetZoneContainer* father)
63 : Container::Container(host.get_cname(), "HOST", father)
65 xbt_assert(father, "Only the Root container has no father");
67 netpoint_ = host.pimpl_netpoint;
68 xbt_assert(netpoint_, "Element '%s' not found", host.get_cname());
70 trivaNodeTypes.insert(type_->get_name());
73 Container::Container(std::string name, std::string type_name, Container* father) : name_(name), father_(father)
75 static long long int container_id = 0;
76 id_ = container_id; // id (or alias) of the container
80 XBT_DEBUG("new container %s, child of %s", name.c_str(), father->name_.c_str());
82 if (not type_name.empty()) {
83 type_ = father_->type_->getOrCreateContainerType(type_name);
84 father_->children_.insert({name_, this});
89 //register all kinds by name
90 if (not allContainers.emplace(name_, this).second)
91 THROWF(tracing_error, 1, "container %s already present in allContainers data structure", name_.c_str());
93 XBT_DEBUG("Add container name '%s'", name_.c_str());
95 //register NODE types for triva configuration
96 if (type_name == "LINK")
97 trivaNodeTypes.insert(type_->get_name());
100 Container::~Container()
102 XBT_DEBUG("destroy container %s", name_.c_str());
103 // Begin with destroying my own children
104 for (auto child : children_)
107 // obligation to dump previous events because they might reference the container that is about to be destroyed
108 TRACE_last_timestamp_to_dump = SIMIX_get_clock();
109 TRACE_paje_dump_buffer(true);
111 // trace my destruction, but not if user requests so or if the container is root
112 if (not TRACE_disable_destroy() && this != Container::getRoot())
115 // remove me from the allContainers data structure
116 allContainers.erase(name_);
119 void Container::createChild(std::string name, std::string type_name)
121 new Container(name, type_name, this);
124 Container* Container::byNameOrNull(std::string name)
126 auto cont = allContainers.find(name);
127 return cont == allContainers.end() ? nullptr : cont->second;
130 Container* Container::byName(std::string name)
132 Container* ret = Container::byNameOrNull(name);
134 THROWF(tracing_error, 1, "container with name %s not found", name.c_str());
139 void Container::removeFromParent()
142 XBT_DEBUG("removeChildContainer (%s) FromContainer (%s) ", get_cname(), father_->get_cname());
143 father_->children_.erase(name_);
148 void Container::logCreation()
150 double timestamp = SIMIX_get_clock();
151 std::stringstream stream;
153 XBT_DEBUG("%s: event_type=%u, timestamp=%f", __func__, PAJE_CreateContainer, timestamp);
155 if (trace_format == simgrid::instr::TraceFormat::Paje) {
156 stream << std::fixed << std::setprecision(TRACE_precision()) << PAJE_CreateContainer << " ";
157 stream << timestamp << " " << id_ << " " << type_->get_id() << " " << father_->id_ << " \"" << name_ << "\"";
158 XBT_DEBUG("Dump %s", stream.str().c_str());
159 fprintf(tracing_file, "%s\n", stream.str().c_str());
160 } else if (trace_format == simgrid::instr::TraceFormat::Ti) {
161 // if we are in the mode with only one file
162 static FILE* ti_unique_file = nullptr;
164 if (tracing_files.empty()) {
165 // generate unique run id with time
166 prefix = xbt_os_time();
169 if (not simgrid::config::get_value<bool>("tracing/smpi/format/ti-one-file") || ti_unique_file == nullptr) {
170 std::string folder_name = TRACE_get_filename() + "_files";
171 std::string filename = folder_name + "/" + std::to_string(prefix) + "_" + name_ + ".txt";
173 _mkdir(folder_name.c_str());
175 mkdir(folder_name.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
177 ti_unique_file = fopen(filename.c_str(), "w");
178 xbt_assert(ti_unique_file, "Tracefile %s could not be opened for writing: %s", filename.c_str(), strerror(errno));
179 fprintf(tracing_file, "%s\n", filename.c_str());
181 tracing_files.insert({this, ti_unique_file});
187 void Container::logDestruction()
189 std::stringstream stream;
190 double timestamp = SIMIX_get_clock();
192 XBT_DEBUG("%s: event_type=%u, timestamp=%f", __func__, PAJE_DestroyContainer, timestamp);
194 if (trace_format == simgrid::instr::TraceFormat::Paje) {
195 stream << std::fixed << std::setprecision(TRACE_precision()) << PAJE_DestroyContainer << " ";
196 stream << timestamp << " " << type_->get_id() << " " << id_;
197 XBT_DEBUG("Dump %s", stream.str().c_str());
198 fprintf(tracing_file, "%s\n", stream.str().c_str());
199 } else if (trace_format == simgrid::instr::TraceFormat::Ti) {
200 if (not simgrid::config::get_value<bool>("tracing/smpi/format/ti-one-file") || tracing_files.size() == 1) {
201 fclose(tracing_files.at(this));
203 tracing_files.erase(this);
209 StateType* Container::getState(std::string name)
211 StateType* ret = dynamic_cast<StateType*>(type_->byName(name));
212 ret->setCallingContainer(this);
216 LinkType* Container::getLink(std::string name)
218 LinkType* ret = dynamic_cast<LinkType*>(type_->byName(name));
219 ret->setCallingContainer(this);
223 VariableType* Container::getVariable(std::string name)
225 VariableType* ret = dynamic_cast<VariableType*>(type_->byName(name));
226 ret->setCallingContainer(this);