1 /* Copyright (c) 2010-2018. The SimGrid Team. All rights reserved. */
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. */
6 #include "src/instr/instr_private.hpp"
8 #include "simgrid/kernel/routing/NetPoint.hpp"
9 #include "simgrid/kernel/routing/NetZoneImpl.hpp"
10 #include "simgrid/s4u/Engine.hpp"
11 #include "simgrid/s4u/Host.hpp"
12 #include "src/surf/network_interface.hpp"
13 #include "src/surf/xml/platf_private.hpp"
14 #include "surf/surf.hpp"
15 #include "xbt/graph.h"
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(instr_routing, instr, "Tracing platform hierarchy");
19 static std::vector<simgrid::instr::NetZoneContainer*> currentContainer; /* push and pop, used only in creation */
21 static const char* instr_node_name(xbt_node_t node)
23 return static_cast<char*>(xbt_graph_node_get_data(node));
26 static container_t lowestCommonAncestor(container_t a1, container_t a2)
28 // this is only an optimization (since most of a1 and a2 share the same parent)
29 if (a1->father_ == a2->father_)
32 // create an array with all ancestors of a1
33 std::vector<container_t> ancestors_a1;
34 container_t p = a1->father_;
36 ancestors_a1.push_back(p);
40 // create an array with all ancestors of a2
41 std::vector<container_t> ancestors_a2;
44 ancestors_a2.push_back(p);
48 // find the lowest ancestor
50 int i = ancestors_a1.size() - 1;
51 int j = ancestors_a2.size() - 1;
52 while (i >= 0 && j >= 0) {
53 container_t a1p = ancestors_a1.at(i);
54 container_t a2p = ancestors_a2.at(j);
66 static void linkContainers(container_t src, container_t dst, std::set<std::string>* filter)
69 if (src->get_name() == "__loopback__" || dst->get_name() == "__loopback__") {
70 XBT_DEBUG(" linkContainers: ignoring loopback link");
75 container_t father = lowestCommonAncestor(src, dst);
77 xbt_die("common father unknown, this is a tracing problem");
80 // check if we already register this pair (we only need one direction)
81 std::string aux1 = src->get_name() + dst->get_name();
82 std::string aux2 = dst->get_name() + src->get_name();
83 if (filter->find(aux1) != filter->end()) {
84 XBT_DEBUG(" linkContainers: already registered %s <-> %s (1)", src->get_cname(), dst->get_cname());
87 if (filter->find(aux2) != filter->end()) {
88 XBT_DEBUG(" linkContainers: already registered %s <-> %s (2)", dst->get_cname(), src->get_cname());
92 // ok, not found, register it
97 std::string link_typename = father->type_->get_name() + "-" + src->type_->get_name() +
98 std::to_string(src->type_->get_id()) + "-" + dst->type_->get_name() +
99 std::to_string(dst->type_->get_id());
100 simgrid::instr::LinkType* link = father->type_->getOrCreateLinkType(link_typename, src->type_, dst->type_);
101 link->setCallingContainer(father);
103 // register EDGE types for triva configuration
104 trivaEdgeTypes.insert(link->get_name());
107 static long long counter = 0;
109 std::string key = std::to_string(counter);
112 link->startEvent(src, "topology", key);
113 link->endEvent(dst, "topology", key);
115 XBT_DEBUG(" linkContainers %s <-> %s", src->get_cname(), dst->get_cname());
118 static void recursiveGraphExtraction(simgrid::s4u::NetZone* netzone, container_t container,
119 std::set<std::string>* filter)
121 if (not TRACE_platform_topology()) {
122 XBT_DEBUG("Graph extraction disabled by user.");
125 XBT_DEBUG("Graph extraction for NetZone = %s", netzone->get_cname());
126 if (not netzone->getChildren()->empty()) {
127 // bottom-up recursion
128 for (auto const& nz_son : *netzone->getChildren()) {
129 container_t child_container = container->children_.at(nz_son->get_cname());
130 recursiveGraphExtraction(nz_son, child_container, filter);
134 xbt_graph_t graph = xbt_graph_new_graph(0, nullptr);
135 std::map<std::string, xbt_node_t>* nodes = new std::map<std::string, xbt_node_t>;
136 std::map<std::string, xbt_edge_t>* edges = new std::map<std::string, xbt_edge_t>;
138 static_cast<simgrid::kernel::routing::NetZoneImpl*>(netzone)->get_graph(graph, nodes, edges);
139 for (auto elm : *edges) {
140 xbt_edge_t edge = elm.second;
141 linkContainers(simgrid::instr::Container::byName(static_cast<const char*>(edge->src->data)),
142 simgrid::instr::Container::byName(static_cast<const char*>(edge->dst->data)), filter);
146 xbt_graph_free_graph(graph, xbt_free_f, xbt_free_f, nullptr);
152 static void instr_netzone_on_creation(simgrid::s4u::NetZone& netzone)
154 std::string id = netzone.get_name();
155 if (simgrid::instr::Container::getRoot() == nullptr) {
156 simgrid::instr::NetZoneContainer* root = new simgrid::instr::NetZoneContainer(id, 0, nullptr);
158 if (TRACE_smpi_is_enabled()) {
159 simgrid::instr::Type* mpi = root->type_->getOrCreateContainerType("MPI");
160 if (not TRACE_smpi_is_grouped())
161 mpi->getOrCreateStateType("MPI_STATE");
162 root->type_->getOrCreateLinkType("MPI_LINK", mpi, mpi);
163 // TODO See if we can move this to the LoadBalancer plugin
164 root->type_->getOrCreateLinkType("MIGRATE_LINK", mpi, mpi);
165 mpi->getOrCreateStateType("MIGRATE_STATE");
168 if (TRACE_needs_platform()) {
169 currentContainer.push_back(root);
174 if (TRACE_needs_platform()) {
175 simgrid::instr::NetZoneContainer* container =
176 new simgrid::instr::NetZoneContainer(id, currentContainer.size(), currentContainer.back());
177 currentContainer.push_back(container);
181 static void instr_netzone_on_seal(simgrid::s4u::NetZone& /*netzone*/)
183 if (TRACE_needs_platform()) {
184 currentContainer.pop_back();
188 static void instr_link_on_creation(simgrid::s4u::Link& link)
190 if (currentContainer.empty()) // No ongoing parsing. Are you creating the loopback?
193 container_t father = currentContainer.back();
194 container_t container = new simgrid::instr::Container(link.get_name(), "LINK", father);
196 if ((TRACE_categorized() || TRACE_uncategorized() || TRACE_platform()) && (not TRACE_disable_link())) {
197 simgrid::instr::VariableType* bandwidth = container->type_->getOrCreateVariableType("bandwidth", "");
198 bandwidth->setCallingContainer(container);
199 bandwidth->setEvent(0, link.bandwidth());
200 simgrid::instr::VariableType* latency = container->type_->getOrCreateVariableType("latency", "");
201 latency->setCallingContainer(container);
202 latency->setEvent(0, link.latency());
204 if (TRACE_uncategorized()) {
205 container->type_->getOrCreateVariableType("bandwidth_used", "0.5 0.5 0.5");
209 static void instr_host_on_creation(simgrid::s4u::Host& host)
211 container_t container = new simgrid::instr::HostContainer(host, currentContainer.back());
212 container_t root = simgrid::instr::Container::getRoot();
214 if ((TRACE_categorized() || TRACE_uncategorized() || TRACE_platform()) && (not TRACE_disable_speed())) {
215 simgrid::instr::VariableType* power = container->type_->getOrCreateVariableType("power", "");
216 power->setCallingContainer(container);
217 power->setEvent(0, host.getSpeed());
220 if (TRACE_uncategorized())
221 container->type_->getOrCreateVariableType("power_used", "0.5 0.5 0.5");
223 if (TRACE_smpi_is_enabled() && TRACE_smpi_is_grouped()) {
224 simgrid::instr::ContainerType* mpi = container->type_->getOrCreateContainerType("MPI");
225 mpi->getOrCreateStateType("MPI_STATE");
226 // TODO See if we can move this to the LoadBalancer plugin
227 root->type_->getOrCreateLinkType("MIGRATE_LINK", mpi, mpi);
228 mpi->getOrCreateStateType("MIGRATE_STATE");
231 if (TRACE_actor_is_enabled()) {
232 simgrid::instr::ContainerType* actor = container->type_->getOrCreateContainerType("ACTOR");
233 simgrid::instr::StateType* state = actor->getOrCreateStateType("ACTOR_STATE");
234 state->addEntityValue("suspend", "1 0 1");
235 state->addEntityValue("sleep", "1 1 0");
236 state->addEntityValue("receive", "1 0 0");
237 state->addEntityValue("send", "0 0 1");
238 state->addEntityValue("task_execute", "0 1 1");
239 root->type_->getOrCreateLinkType("ACTOR_LINK", actor, actor);
240 root->type_->getOrCreateLinkType("ACTOR_TASK_LINK", actor, actor);
243 if (TRACE_vm_is_enabled()) {
244 simgrid::instr::ContainerType* msg_vm = container->type_->getOrCreateContainerType("MSG_VM");
245 simgrid::instr::StateType* state = msg_vm->getOrCreateStateType("MSG_VM_STATE");
246 state->addEntityValue("suspend", "1 0 1");
247 state->addEntityValue("sleep", "1 1 0");
248 state->addEntityValue("receive", "1 0 0");
249 state->addEntityValue("send", "0 0 1");
250 state->addEntityValue("task_execute", "0 1 1");
251 root->type_->getOrCreateLinkType("MSG_VM_LINK", msg_vm, msg_vm);
252 root->type_->getOrCreateLinkType("MSG_VM_ACTOR_LINK", msg_vm, msg_vm);
256 static void instr_netpoint_on_creation(simgrid::kernel::routing::NetPoint* netpoint)
258 if (netpoint->is_router() && TRACE_needs_platform() && TRACE_is_enabled())
259 new simgrid::instr::RouterContainer(netpoint->get_cname(), currentContainer.back());
262 static void instr_on_platform_created()
264 currentContainer.clear();
265 std::set<std::string>* filter = new std::set<std::string>;
266 XBT_DEBUG("Starting graph extraction.");
267 recursiveGraphExtraction(simgrid::s4u::Engine::getInstance()->getNetRoot(), simgrid::instr::Container::getRoot(),
269 XBT_DEBUG("Graph extraction finished.");
271 TRACE_paje_dump_buffer(true);
274 void instr_define_callbacks()
276 // always need the callbacks to zones (we need only the root zone), to create the rootContainer and the rootType
278 if (TRACE_needs_platform()) {
279 simgrid::s4u::on_platform_created.connect(instr_on_platform_created);
280 simgrid::s4u::Host::onCreation.connect(instr_host_on_creation);
281 simgrid::s4u::Link::onCreation.connect(instr_link_on_creation);
283 simgrid::s4u::NetZone::onCreation.connect(instr_netzone_on_creation);
284 simgrid::s4u::NetZone::onSeal.connect(instr_netzone_on_seal);
285 simgrid::kernel::routing::NetPoint::onCreation.connect(instr_netpoint_on_creation);
288 * user categories support
290 static void recursiveNewVariableType(std::string new_typename, std::string color, simgrid::instr::Type* root)
292 if (root->get_name() == "HOST" || root->get_name() == "MSG_VM")
293 root->getOrCreateVariableType(std::string("p") + new_typename, color);
295 if (root->get_name() == "LINK")
296 root->getOrCreateVariableType(std::string("b") + new_typename, color);
298 for (auto elm : root->children_) {
299 recursiveNewVariableType(new_typename, color, elm.second);
303 void instr_new_variable_type(std::string new_typename, std::string color)
305 recursiveNewVariableType(new_typename, color, simgrid::instr::Container::getRoot()->type_);
308 static void recursiveNewUserVariableType(std::string father_type, std::string new_typename, std::string color,
309 simgrid::instr::Type* root)
311 if (root->get_name() == father_type) {
312 root->getOrCreateVariableType(new_typename, color);
314 for (auto elm : root->children_)
315 recursiveNewUserVariableType(father_type, new_typename, color, elm.second);
318 void instr_new_user_variable_type(std::string father_type, std::string new_typename, std::string color)
320 recursiveNewUserVariableType(father_type, new_typename, color, simgrid::instr::Container::getRoot()->type_);
323 static void recursiveNewUserStateType(std::string father_type, std::string new_typename, simgrid::instr::Type* root)
325 if (root->get_name() == father_type)
326 root->getOrCreateStateType(new_typename);
328 for (auto elm : root->children_)
329 recursiveNewUserStateType(father_type, new_typename, elm.second);
332 void instr_new_user_state_type(std::string father_type, std::string new_typename)
334 recursiveNewUserStateType(father_type, new_typename, simgrid::instr::Container::getRoot()->type_);
337 static void recursiveNewValueForUserStateType(std::string type_name, const char* val, std::string color,
338 simgrid::instr::Type* root)
340 if (root->get_name() == type_name)
341 static_cast<simgrid::instr::StateType*>(root)->addEntityValue(val, color);
343 for (auto elm : root->children_)
344 recursiveNewValueForUserStateType(type_name, val, color, elm.second);
347 void instr_new_value_for_user_state_type(std::string type_name, const char* value, std::string color)
349 recursiveNewValueForUserStateType(type_name, value, color, simgrid::instr::Container::getRoot()->type_);
352 #define GRAPHICATOR_SUPPORT_FUNCTIONS
354 static void recursiveXBTGraphExtraction(xbt_graph_t graph, std::map<std::string, xbt_node_t>* nodes,
355 std::map<std::string, xbt_edge_t>* edges, sg_netzone_t netzone,
356 container_t container)
358 if (not netzone->getChildren()->empty()) {
359 // bottom-up recursion
360 for (auto const& netzone_child : *netzone->getChildren()) {
361 container_t child_container = container->children_.at(netzone_child->get_cname());
362 recursiveXBTGraphExtraction(graph, nodes, edges, netzone_child, child_container);
366 static_cast<simgrid::kernel::routing::NetZoneImpl*>(netzone)->get_graph(graph, nodes, edges);
369 xbt_graph_t instr_routing_platform_graph()
371 xbt_graph_t ret = xbt_graph_new_graph(0, nullptr);
372 std::map<std::string, xbt_node_t>* nodes = new std::map<std::string, xbt_node_t>;
373 std::map<std::string, xbt_edge_t>* edges = new std::map<std::string, xbt_edge_t>;
374 recursiveXBTGraphExtraction(ret, nodes, edges, simgrid::s4u::Engine::getInstance()->getNetRoot(),
375 simgrid::instr::Container::getRoot());
381 void instr_routing_platform_graph_export_graphviz(xbt_graph_t g, const char* filename)
383 unsigned int cursor = 0;
384 xbt_node_t node = nullptr;
385 xbt_edge_t edge = nullptr;
387 FILE* file = fopen(filename, "w");
388 xbt_assert(file, "Failed to open %s \n", filename);
391 fprintf(file, "digraph test {\n");
393 fprintf(file, "graph test {\n");
395 fprintf(file, " graph [overlap=scale]\n");
397 fprintf(file, " node [shape=box, style=filled]\n");
398 fprintf(file, " node [width=.3, height=.3, style=filled, color=skyblue]\n\n");
400 xbt_dynar_foreach (g->nodes, cursor, node) {
401 fprintf(file, " \"%s\";\n", instr_node_name(node));
403 xbt_dynar_foreach (g->edges, cursor, edge) {
404 const char* src_s = instr_node_name(edge->src);
405 const char* dst_s = instr_node_name(edge->dst);
407 fprintf(file, " \"%s\" -> \"%s\";\n", src_s, dst_s);
409 fprintf(file, " \"%s\" -- \"%s\";\n", src_s, dst_s);
411 fprintf(file, "}\n");