Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
rename last PJ_ functions
[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
30 namespace simgrid {
31 namespace instr {
32
33 container_t Container::getRootContainer()
34 {
35   return rootContainer;
36 }
37
38 NetZoneContainer::NetZoneContainer(std::string name, unsigned int level, NetZoneContainer* father)
39     : Container::Container(name, "", father)
40 {
41   netpoint_ = simgrid::s4u::Engine::getInstance()->getNetpointByNameOrNull(name);
42   xbt_assert(netpoint_, "Element '%s' not found", name.c_str());
43   if (father_) {
44     type_ = father_->type_->getOrCreateContainerType(std::string("L") + std::to_string(level));
45     father_->children_.insert({getName(), this});
46     logCreation();
47   } else {
48     type_ = Type::createRootType();
49     rootContainer = this;
50   }
51 }
52
53 RouterContainer::RouterContainer(std::string name, Container* father) : Container::Container(name, "ROUTER", father)
54 {
55   xbt_assert(father, "Only the Root container has no father");
56
57   netpoint_ = simgrid::s4u::Engine::getInstance()->getNetpointByNameOrNull(name);
58   xbt_assert(netpoint_, "Element '%s' not found", name.c_str());
59
60   trivaNodeTypes.insert(type_->getName());
61 }
62
63 HostContainer::HostContainer(simgrid::s4u::Host& host, NetZoneContainer* father)
64     : Container::Container(host.getCname(), "HOST", father)
65 {
66   xbt_assert(father, "Only the Root container has no father");
67
68   netpoint_ = host.pimpl_netpoint;
69   xbt_assert(netpoint_, "Element '%s' not found", host.getCname());
70
71   trivaNodeTypes.insert(type_->getName());
72 }
73
74 Container::Container(std::string name, std::string type_name, Container* father) : name_(name), father_(father)
75 {
76   static long long int container_id = 0;
77   id_                               = container_id; // id (or alias) of the container
78   container_id++;
79
80   if (father_) {
81     XBT_DEBUG("new container %s, child of %s", name.c_str(), father->name_.c_str());
82
83     if (not type_name.empty()) {
84       type_ = father_->type_->getOrCreateContainerType(type_name);
85       father_->children_.insert({name_, this});
86       logCreation();
87     }
88   }
89
90   //register all kinds by name
91   if (not allContainers.emplace(name_, this).second) {
92     THROWF(tracing_error, 1, "container %s already present in allContainers data structure", name_.c_str());
93   }
94
95   XBT_DEBUG("Add container name '%s'", name_.c_str());
96
97   //register NODE types for triva configuration
98   if (type_name == "LINK")
99     trivaNodeTypes.insert(type_->getName());
100 }
101
102 Container::~Container()
103 {
104   XBT_DEBUG("destroy container %s", name_.c_str());
105   // Begin with destroying my own children
106   for (auto child : children_) {
107     delete child.second;
108   }
109
110   // obligation to dump previous events because they might reference the container that is about to be destroyed
111   TRACE_last_timestamp_to_dump = surf_get_clock();
112   TRACE_paje_dump_buffer(true);
113
114   // trace my destruction
115   if (not TRACE_disable_destroy() && this != Container::getRootContainer()) {
116     // do not trace the container destruction if user requests or if the container is root
117     logDestruction();
118   }
119
120   // remove me from the allContainers data structure
121   allContainers.erase(name_);
122 }
123
124 Container* Container::byNameOrNull(std::string name)
125 {
126   auto cont = allContainers.find(name);
127   return cont == allContainers.end() ? nullptr : cont->second;
128 }
129
130 Container* Container::byName(std::string name)
131 {
132   Container* ret = Container::byNameOrNull(name);
133   if (ret == nullptr)
134     THROWF(tracing_error, 1, "container with name %s not found", name.c_str());
135
136   return ret;
137 }
138
139 void Container::removeFromParent()
140 {
141   if (father_) {
142     XBT_DEBUG("removeChildContainer (%s) FromContainer (%s) ", name_.c_str(), father_->name_.c_str());
143     father_->children_.erase(name_);
144   }
145 }
146
147 void Container::logCreation()
148 {
149   double timestamp = SIMIX_get_clock();
150   std::stringstream stream;
151
152   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, simgrid::instr::PAJE_CreateContainer, timestamp);
153
154   if (instr_fmt_type == instr_fmt_paje) {
155     stream << std::fixed << std::setprecision(TRACE_precision());
156     stream << simgrid::instr::PAJE_CreateContainer;
157     stream << " ";
158     /* prevent 0.0000 in the trace - this was the behavior before the transition to c++ */
159     if (timestamp < 1e-12)
160       stream << 0;
161     else
162       stream << timestamp;
163     stream << " " << id_ << " " << type_->getId() << " " << father_->id_ << " \"" << name_ << "\"" << std::endl;
164     fprintf(tracing_file, "%s", stream.str().c_str());
165     XBT_DEBUG("Dump %s", stream.str().c_str());
166     stream.str("");
167     stream.clear();
168   } else if (instr_fmt_type == instr_fmt_TI) {
169     // if we are in the mode with only one file
170     static FILE* ti_unique_file = nullptr;
171
172     if (tracing_files.empty()) {
173       // generate unique run id with time
174       prefix = xbt_os_time();
175     }
176
177     if (not xbt_cfg_get_boolean("tracing/smpi/format/ti-one-file") || ti_unique_file == nullptr) {
178       char* folder_name = bprintf("%s_files", TRACE_get_filename());
179       char* filename    = bprintf("%s/%f_%s.txt", folder_name, prefix, name_.c_str());
180 #ifdef WIN32
181       _mkdir(folder_name);
182 #else
183       mkdir(folder_name, S_IRWXU | S_IRWXG | S_IRWXO);
184 #endif
185       ti_unique_file = fopen(filename, "w");
186       xbt_assert(ti_unique_file, "Tracefile %s could not be opened for writing: %s", filename, strerror(errno));
187       fprintf(tracing_file, "%s\n", filename);
188
189       xbt_free(folder_name);
190       xbt_free(filename);
191     }
192
193     tracing_files.insert({this, ti_unique_file});
194   } else {
195     THROW_IMPOSSIBLE;
196   }
197 }
198
199 void Container::logDestruction()
200 {
201   std::stringstream stream;
202   double timestamp = SIMIX_get_clock();
203
204   XBT_DEBUG("%s: event_type=%d, timestamp=%f", __FUNCTION__, simgrid::instr::PAJE_DestroyContainer, timestamp);
205
206   if (instr_fmt_type == instr_fmt_paje) {
207     stream << std::fixed << std::setprecision(TRACE_precision());
208     stream << simgrid::instr::PAJE_DestroyContainer;
209     stream << " ";
210     /* prevent 0.0000 in the trace - this was the behavior before the transition to c++ */
211     if (timestamp < 1e-12)
212       stream << 0;
213     else
214       stream << timestamp;
215     stream << " " << type_->getId() << " " << id_ << std::endl;
216     fprintf(tracing_file, "%s", stream.str().c_str());
217     XBT_DEBUG("Dump %s", stream.str().c_str());
218     stream.str("");
219     stream.clear();
220   } else if (instr_fmt_type == instr_fmt_TI) {
221     if (not xbt_cfg_get_boolean("tracing/smpi/format/ti-one-file") || tracing_files.size() == 1) {
222       FILE* f = tracing_files.at(this);
223       fclose(f);
224     }
225     tracing_files.erase(this);
226   } else {
227     THROW_IMPOSSIBLE;
228   }
229 }
230 }
231 }