Logo AND Algorithmique Numérique Distribuée

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