Logo AND Algorithmique Numérique Distribuée

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