Logo AND Algorithmique Numérique Distribuée

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