Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
e648aeb3dc5490e64cbccd925e2cf8bd6269dd11
[simgrid.git] / src / instr / instr_paje_containers.cpp
1 /* Copyright (c) 2010-2019. 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/s4u/Engine.hpp"
7 #include "simgrid/s4u/Host.hpp"
8 #include "src/instr/instr_private.hpp"
9
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_paje_containers, instr, "Paje tracing event system (containers)");
11
12 extern std::ofstream tracing_file;
13 std::map<container_t, std::ofstream*> tracing_files; // TI specific
14 double prefix = 0.0;                               // TI specific
15
16 static container_t rootContainer = nullptr;    /* the root container */
17 static std::map<std::string, container_t> allContainers; /* all created containers indexed by name */
18 std::set<std::string> trivaNodeTypes;           /* all host types defined */
19 std::set<std::string> trivaEdgeTypes;           /* all link types defined */
20
21 long long int instr_new_paje_id ()
22 {
23   static long long int type_id = 0;
24   return type_id++;
25 }
26
27 namespace simgrid {
28 namespace instr {
29
30 container_t Container::get_root()
31 {
32   return rootContainer;
33 }
34
35 NetZoneContainer::NetZoneContainer(std::string name, unsigned int level, NetZoneContainer* father)
36     : Container::Container(std::move(name), "", father)
37 {
38   netpoint_ = simgrid::s4u::Engine::get_instance()->netpoint_by_name_or_null(get_name());
39   xbt_assert(netpoint_, "Element '%s' not found", get_cname());
40   if (father_) {
41     std::string type_name = std::string("L") + std::to_string(level);
42     type_                 = father_->type_->by_name_or_create<ContainerType>(type_name);
43     father_->children_.insert({get_name(), this});
44     log_creation();
45   } else {
46     type_         = new ContainerType("0");
47     rootContainer = this;
48   }
49 }
50
51 RouterContainer::RouterContainer(std::string name, Container* father)
52     : Container::Container(std::move(name), "ROUTER", father)
53 {
54   xbt_assert(father, "Only the Root container has no father");
55
56   netpoint_ = simgrid::s4u::Engine::get_instance()->netpoint_by_name_or_null(get_name());
57   xbt_assert(netpoint_, "Element '%s' not found", get_cname());
58
59   trivaNodeTypes.insert(type_->get_name());
60 }
61
62 HostContainer::HostContainer(simgrid::s4u::Host& host, NetZoneContainer* father)
63     : Container::Container(host.get_name(), "HOST", father)
64 {
65   xbt_assert(father, "Only the Root container has no father");
66
67   netpoint_ = host.pimpl_netpoint;
68   xbt_assert(netpoint_, "Element '%s' not found", host.get_cname());
69
70   trivaNodeTypes.insert(type_->get_name());
71 }
72
73 Container::Container(std::string name, const std::string& type_name, Container* father)
74     : name_(std::move(name)), father_(father)
75 {
76   static long long int container_id = 0;
77   id_                               = container_id; // id (or alias) of the container
78   container_id++;
79
80   if (father_) {
81     XBT_DEBUG("new container %s, child of %s", get_cname(), father->get_cname());
82
83     if (not type_name.empty()) {
84       type_ = father_->type_->by_name_or_create<ContainerType>(type_name);
85       father_->children_.insert({name_, this});
86       log_creation();
87     }
88   }
89
90   //register all kinds by name
91   if (not allContainers.emplace(name_, this).second)
92     THROWF(tracing_error, 1, "container %s already present in allContainers data structure", get_cname());
93
94   XBT_DEBUG("Add container name '%s'", get_cname());
95
96   //register NODE types for triva configuration
97   if (type_name == "LINK")
98     trivaNodeTypes.insert(type_->get_name());
99 }
100
101 Container::~Container()
102 {
103   XBT_DEBUG("destroy container %s", get_cname());
104   // Begin with destroying my own children
105   for (auto child : children_)
106     delete child.second;
107
108   // obligation to dump previous events because they might reference the container that is about to be destroyed
109   TRACE_last_timestamp_to_dump = SIMIX_get_clock();
110   TRACE_paje_dump_buffer(true);
111
112   // trace my destruction, but not if user requests so or if the container is root
113   if (not TRACE_disable_destroy() && this != Container::get_root())
114     log_destruction();
115
116   // remove me from the allContainers data structure
117   allContainers.erase(name_);
118 }
119
120 void Container::create_child(std::string name, const std::string& type_name)
121 {
122   new Container(std::move(name), type_name, this);
123 }
124
125 Container* Container::by_name_or_null(const std::string& name)
126 {
127   auto cont = allContainers.find(name);
128   return cont == allContainers.end() ? nullptr : cont->second;
129 }
130
131 Container* Container::by_name(const std::string& name)
132 {
133   Container* ret = Container::by_name_or_null(name);
134   xbt_assert(ret != nullptr, "container with name %s not found", name.c_str());
135
136   return ret;
137 }
138
139 void Container::remove_from_parent()
140 {
141   if (father_) {
142     XBT_DEBUG("removeChildContainer (%s) FromContainer (%s) ", get_cname(), father_->get_cname());
143     father_->children_.erase(name_);
144   }
145   delete this;
146 }
147
148 void Container::log_creation()
149 {
150   double timestamp = SIMIX_get_clock();
151   std::stringstream stream;
152
153   XBT_DEBUG("%s: event_type=%u, timestamp=%f", __func__, PAJE_CreateContainer, timestamp);
154
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_ << " \"";
158     if (name_.find("rank-") != 0)
159       stream << name_ << "\"";
160     else
161       /* Subtract -1 because this is the process id and we transform it to the rank id */
162       stream << "rank-" << stoi(name_.substr(5)) - 1 << "\"";
163
164     XBT_DEBUG("Dump %s", stream.str().c_str());
165     tracing_file << stream.str() << std::endl;
166   } else if (trace_format == simgrid::instr::TraceFormat::Ti) {
167     // if we are in the mode with only one file
168     static std::ofstream* ti_unique_file = nullptr;
169
170     if (tracing_files.empty()) {
171       // generate unique run id with time
172       prefix = xbt_os_time();
173     }
174
175     if (not simgrid::config::get_value<bool>("tracing/smpi/format/ti-one-file") || ti_unique_file == nullptr) {
176       std::string folder_name = TRACE_get_filename() + "_files";
177       std::string filename    = folder_name + "/" + std::to_string(prefix) + "_" + name_ + ".txt";
178 #ifdef WIN32
179       _mkdir(folder_name.c_str());
180 #else
181       mkdir(folder_name.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
182 #endif
183       ti_unique_file = new std::ofstream(filename.c_str(), std::ofstream::out);
184       xbt_assert(not ti_unique_file->fail(), "Tracefile %s could not be opened for writing", filename.c_str());
185       tracing_file << filename << std::endl;
186     }
187     tracing_files.insert({this, ti_unique_file});
188   } else {
189     THROW_IMPOSSIBLE;
190   }
191 }
192
193 void Container::log_destruction()
194 {
195   std::stringstream stream;
196   double timestamp = SIMIX_get_clock();
197
198   XBT_DEBUG("%s: event_type=%u, timestamp=%f", __func__, PAJE_DestroyContainer, timestamp);
199
200   if (trace_format == simgrid::instr::TraceFormat::Paje) {
201     stream << std::fixed << std::setprecision(TRACE_precision()) << PAJE_DestroyContainer << " ";
202     stream << timestamp << " " << type_->get_id() << " " << id_;
203     XBT_DEBUG("Dump %s", stream.str().c_str());
204     tracing_file << stream.str() << std::endl;
205   } else if (trace_format == simgrid::instr::TraceFormat::Ti) {
206     if (not simgrid::config::get_value<bool>("tracing/smpi/format/ti-one-file") || tracing_files.size() == 1) {
207       tracing_files.at(this)->close();
208       delete tracing_files.at(this);
209     }
210     tracing_files.erase(this);
211   } else {
212     THROW_IMPOSSIBLE;
213   }
214 }
215
216 StateType* Container::get_state(const std::string& name)
217 {
218   StateType* ret = dynamic_cast<StateType*>(type_->by_name(name));
219   ret->set_calling_container(this);
220   return ret;
221 }
222
223 LinkType* Container::get_link(const std::string& name)
224 {
225   LinkType* ret = dynamic_cast<LinkType*>(type_->by_name(name));
226   ret->set_calling_container(this);
227   return ret;
228 }
229
230 VariableType* Container::get_variable(const std::string& name)
231 {
232   VariableType* ret = dynamic_cast<VariableType*>(type_->by_name(name));
233   ret->set_calling_container(this);
234   return ret;
235 }
236 }
237 }