Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
More objectification of instr::Type (wip)
[simgrid.git] / src / instr / instr_paje_containers.cpp
1 /* Copyright (c) 2010-2017. 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 "surf/surf.h"
10 #include <unordered_map>
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_paje_containers, instr, "Paje tracing event system (containers)");
13
14 extern FILE* tracing_file;
15 extern std::map<container_t, FILE*> tracing_files; // TI specific
16 double prefix = 0.0;                               // TI specific
17
18 static container_t rootContainer = nullptr;    /* the root container */
19 static std::unordered_map<std::string, container_t> allContainers; /* all created containers indexed by name */
20 std::set<std::string> trivaNodeTypes;           /* all host types defined */
21 std::set<std::string> trivaEdgeTypes;           /* all link types defined */
22
23 long long int instr_new_paje_id ()
24 {
25   static long long int type_id = 0;
26   return type_id++;
27 }
28
29 container_t PJ_container_get_root()
30 {
31   return rootContainer;
32 }
33
34 void PJ_container_set_root (container_t root)
35 {
36   rootContainer = root;
37 }
38
39 namespace simgrid {
40 namespace instr {
41
42 Container::Container(std::string name, e_container_types kind, Container* father)
43     : kind_(kind), name_(name), father_(father)
44 {
45   static long long int container_id = 0;
46   id_                               = std::to_string(container_id); // id (or alias) of the container
47   container_id++;
48
49   if (father_) {
50     level_ = father_->level_ + 1;
51     XBT_DEBUG("new container %s, child of %s", name.c_str(), father->name_.c_str());
52   }
53
54   // Search for network_element_t for AS, ROUTER and HOST
55   // Name the kind of container. For AS otherwise, the name depends on its level
56   std::string typeNameBuff;
57   switch (kind_) {
58     case INSTR_AS:
59       netpoint_ = simgrid::s4u::Engine::getInstance()->getNetpointByNameOrNull(name);
60       xbt_assert(netpoint_, "Element '%s' not found", name.c_str());
61       typeNameBuff = std::string("L") + std::to_string(level_);
62       break;
63     case INSTR_ROUTER:
64       netpoint_ = simgrid::s4u::Engine::getInstance()->getNetpointByNameOrNull(name);
65       xbt_assert(netpoint_, "Element '%s' not found", name.c_str());
66       typeNameBuff = std::string("ROUTER");
67       break;
68     case INSTR_HOST:
69       netpoint_ = sg_host_by_name(name.c_str())->pimpl_netpoint;
70       xbt_assert(netpoint_, "Element '%s' not found", name.c_str());
71       typeNameBuff = std::string("HOST");
72       break;
73     case INSTR_LINK:
74       typeNameBuff = std::string("LINK");
75       break;
76     case INSTR_SMPI:
77       typeNameBuff = std::string("MPI");
78       break;
79     case INSTR_MSG_PROCESS:
80       typeNameBuff = std::string("MSG_PROCESS");
81       break;
82     case INSTR_MSG_VM:
83       typeNameBuff = std::string("MSG_VM");
84       break;
85     case INSTR_MSG_TASK:
86       typeNameBuff = std::string("MSG_TASK");
87       break;
88     default:
89       THROWF(tracing_error, 0, "new container kind is unknown.");
90       break;
91   }
92
93   if (father_) {
94     type_ = father_->type_->getChildOrNull(typeNameBuff);
95     if (type_ == nullptr) {
96       type_ = Type::containerNew(typeNameBuff.c_str(), father_->type_);
97     }
98     father_->children_.insert({name_, this});
99     logCreation();
100   } else if (kind_ == INSTR_AS) {
101     type_ = Type::containerNew("0", nullptr);
102   }
103
104   //register all kinds by name
105   if (not allContainers.emplace(name_, this).second) {
106     THROWF(tracing_error, 1, "container %s already present in allContainers data structure", name_.c_str());
107   }
108
109   XBT_DEBUG("Add container name '%s'", name_.c_str());
110
111   //register NODE types for triva configuration
112   if (kind_ == INSTR_HOST || kind_ == INSTR_LINK || kind_ == INSTR_ROUTER)
113     trivaNodeTypes.insert(type_->getName());
114 }
115
116 Container::~Container()
117 {
118   XBT_DEBUG("destroy container %s", name_.c_str());
119   // Begin with destroying my own children
120   for (auto child : children_) {
121     delete child.second;
122   }
123
124   // obligation to dump previous events because they might reference the container that is about to be destroyed
125   TRACE_last_timestamp_to_dump = surf_get_clock();
126   TRACE_paje_dump_buffer(1);
127
128   // trace my destruction
129   if (not TRACE_disable_destroy() && this != PJ_container_get_root()) {
130     // do not trace the container destruction if user requests or if the container is root
131     logDestruction();
132   }
133
134   // remove me from the allContainers data structure
135   allContainers.erase(name_);
136 }
137
138 Container* Container::byNameOrNull(std::string name)
139 {
140   auto cont = allContainers.find(name);
141   return cont == allContainers.end() ? nullptr : cont->second;
142 }
143
144 Container* Container::byName(std::string name)
145 {
146   Container* ret = Container::byNameOrNull(name);
147   if (ret == nullptr)
148     THROWF(tracing_error, 1, "container with name %s not found", name.c_str());
149
150   return ret;
151 }
152
153 void Container::removeFromParent()
154 {
155   if (father_) {
156     XBT_DEBUG("removeChildContainer (%s) FromContainer (%s) ", name_.c_str(), father_->name_.c_str());
157     father_->children_.erase(name_);
158   }
159 }
160
161 void Container::logCreation()
162 {
163   double timestamp = SIMIX_get_clock();
164   std::stringstream stream;
165
166   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, simgrid::instr::PAJE_CreateContainer, timestamp);
167
168   if (instr_fmt_type == instr_fmt_paje) {
169     stream << std::fixed << std::setprecision(TRACE_precision());
170     stream << simgrid::instr::PAJE_CreateContainer;
171     stream << " ";
172     /* prevent 0.0000 in the trace - this was the behavior before the transition to c++ */
173     if (timestamp < 1e-12)
174       stream << 0;
175     else
176       stream << timestamp;
177     stream << " " << id_ << " " << type_->getId() << " " << father_->id_ << " \"" << name_ << "\"" << std::endl;
178     fprintf(tracing_file, "%s", stream.str().c_str());
179     XBT_DEBUG("Dump %s", stream.str().c_str());
180     stream.str("");
181     stream.clear();
182   } else if (instr_fmt_type == instr_fmt_TI) {
183     // if we are in the mode with only one file
184     static FILE* ti_unique_file = nullptr;
185
186     if (tracing_files.empty()) {
187       // generate unique run id with time
188       prefix = xbt_os_time();
189     }
190
191     if (not xbt_cfg_get_boolean("tracing/smpi/format/ti-one-file") || ti_unique_file == nullptr) {
192       char* folder_name = bprintf("%s_files", TRACE_get_filename());
193       char* filename    = bprintf("%s/%f_%s.txt", folder_name, prefix, name_.c_str());
194 #ifdef WIN32
195       _mkdir(folder_name);
196 #else
197       mkdir(folder_name, S_IRWXU | S_IRWXG | S_IRWXO);
198 #endif
199       ti_unique_file = fopen(filename, "w");
200       xbt_assert(ti_unique_file, "Tracefile %s could not be opened for writing: %s", filename, strerror(errno));
201       fprintf(tracing_file, "%s\n", filename);
202
203       xbt_free(folder_name);
204       xbt_free(filename);
205     }
206
207     tracing_files.insert({this, ti_unique_file});
208   } else {
209     THROW_IMPOSSIBLE;
210   }
211 }
212
213 void Container::logDestruction()
214 {
215   std::stringstream stream;
216   double timestamp = SIMIX_get_clock();
217
218   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, simgrid::instr::PAJE_DestroyContainer, timestamp);
219
220   if (instr_fmt_type == instr_fmt_paje) {
221     stream << std::fixed << std::setprecision(TRACE_precision());
222     stream << simgrid::instr::PAJE_DestroyContainer;
223     stream << " ";
224     /* prevent 0.0000 in the trace - this was the behavior before the transition to c++ */
225     if (timestamp < 1e-12)
226       stream << 0;
227     else
228       stream << timestamp;
229     stream << " " << type_->getId() << " " << id_ << std::endl;
230     fprintf(tracing_file, "%s", stream.str().c_str());
231     XBT_DEBUG("Dump %s", stream.str().c_str());
232     stream.str("");
233     stream.clear();
234   } else if (instr_fmt_type == instr_fmt_TI) {
235     if (not xbt_cfg_get_boolean("tracing/smpi/format/ti-one-file") || tracing_files.size() == 1) {
236       FILE* f = tracing_files.at(this);
237       fclose(f);
238     }
239     tracing_files.erase(this);
240   } else {
241     THROW_IMPOSSIBLE;
242   }
243 }
244 }
245 }