Logo AND Algorithmique Numérique Distribuée

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