Logo AND Algorithmique Numérique Distribuée

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