Logo AND Algorithmique Numérique Distribuée

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