Logo AND Algorithmique Numérique Distribuée

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