Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / src / instr / instr_interface.cpp
index 6e15de3..3158bfe 100644 (file)
@@ -1,34 +1,34 @@
-/* Copyright (c) 2010-2019. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2010-2022. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
-#include "simgrid/kernel/routing/NetPoint.hpp"
+#include <simgrid/Exception.hpp>
+#include <simgrid/kernel/routing/NetPoint.hpp>
+#include <xbt/random.hpp>
+
 #include "src/instr/instr_private.hpp"
-#include "src/surf/network_interface.hpp"
-#include "src/surf/surf_private.hpp"
-#include "surf/surf.hpp"
+#include "src/kernel/resource/StandardLinkImpl.hpp"
 #include <algorithm>
+#include <cmath>
 
 enum class InstrUserVariable { DECLARE, SET, ADD, SUB };
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_api, instr, "API");
 
-std::set<std::string> created_categories;
-std::set<std::string> declared_marks;
-std::set<std::string> user_host_variables;
-std::set<std::string> user_vm_variables;
-std::set<std::string> user_link_variables;
-extern std::set<std::string> trivaNodeTypes;
-extern std::set<std::string> trivaEdgeTypes;
+std::set<std::string, std::less<>> created_categories;
+std::set<std::string, std::less<>> declared_marks;
+std::set<std::string, std::less<>> user_host_variables;
+std::set<std::string, std::less<>> user_vm_variables;
+std::set<std::string, std::less<>> user_link_variables;
 
-static xbt_dynar_t instr_set_to_dynar(std::set<std::string>* filter)
+static xbt_dynar_t instr_set_to_dynar(const std::set<std::string, std::less<>>& filter)
 {
   if (not TRACE_is_enabled() || not TRACE_needs_platform())
     return nullptr;
 
   xbt_dynar_t ret = xbt_dynar_new (sizeof(char*), &xbt_free_ref);
-  for (auto const& name : *filter)
+  for (auto const& name : filter)
     xbt_dynar_push_as(ret, char*, xbt_strdup(name.c_str()));
 
   return ret;
@@ -57,7 +57,7 @@ void TRACE_category(const char *category)
 /** @ingroup TRACE_category
  *  @brief Declare a new category with a color.
  *
- *  Same as #TRACE_category, but let user specify a color encoded as a RGB-like string with three floats from 0 to 1.
+ *  Same as #TRACE_category, but let user specify a color encoded as an RGB-like string with three floats from 0 to 1.
  *  So, to specify a red color, pass "1 0 0" as color parameter. A light-gray color can be specified using "0.7 0.7 0.7"
  *   as color. This function has no effect if a category with the same name has been already declared.
  *
@@ -81,16 +81,16 @@ void TRACE_category_with_color (const char *category, const char *color)
   //check if category is already created
   if (created_categories.find(category) != created_categories.end())
     return;
-  else
-    created_categories.insert(category);
+
+  created_categories.emplace(category);
 
   //define final_color
   std::string final_color;
   if (not color) {
     //generate a random color
-    double red = drand48();
-    double green = drand48();
-    double blue = drand48();
+    double red   = simgrid::xbt::random::uniform_real(0.0, std::nextafter(1.0, 2.0));
+    double green = simgrid::xbt::random::uniform_real(0.0, std::nextafter(1.0, 2.0));
+    double blue  = simgrid::xbt::random::uniform_real(0.0, std::nextafter(1.0, 2.0));
     final_color  = std::to_string(red) + " " + std::to_string(green) + " " + std::to_string(blue);
   }else{
     final_color = std::string(color);
@@ -118,7 +118,7 @@ xbt_dynar_t TRACE_get_categories ()
 {
   if (not TRACE_is_enabled() || not TRACE_categorized())
     return nullptr;
-  return instr_set_to_dynar(&created_categories);
+  return instr_set_to_dynar(created_categories);
 }
 
 /** @ingroup TRACE_mark
@@ -133,7 +133,7 @@ xbt_dynar_t TRACE_get_categories ()
  */
 void TRACE_declare_mark(const char *mark_type)
 {
-  /* safe switchs. tracing has to be activated and if platform is not traced, we can't deal with marks */
+  /* safe switches. tracing has to be activated and if platform is not traced, we can't deal with marks */
   if (not TRACE_is_enabled() || not TRACE_needs_platform())
     return;
 
@@ -141,12 +141,13 @@ void TRACE_declare_mark(const char *mark_type)
 
   //check if mark_type is already declared
   if (declared_marks.find(mark_type) != declared_marks.end()) {
-    THROWF (tracing_error, 1, "mark_type with name (%s) is already declared", mark_type);
+    throw simgrid::TracingError(XBT_THROW_POINT,
+                                simgrid::xbt::string_printf("mark_type with name (%s) is already declared", mark_type));
   }
 
   XBT_DEBUG("MARK,declare %s", mark_type);
-  simgrid::instr::Container::get_root()->type_->by_name_or_create<simgrid::instr::EventType>(mark_type);
-  declared_marks.insert(mark_type);
+  simgrid::instr::Container::get_root()->get_type()->by_name_or_create<simgrid::instr::EventType>(mark_type);
+  declared_marks.emplace(mark_type);
 }
 
 /** @ingroup TRACE_mark
@@ -173,10 +174,11 @@ void TRACE_declare_mark_value_with_color (const char *mark_type, const char *mar
   xbt_assert(mark_type, "mark_type is nullptr");
   xbt_assert(mark_value, "mark_value is nullptr");
 
-  simgrid::instr::EventType* type =
-      static_cast<simgrid::instr::EventType*>(simgrid::instr::Container::get_root()->type_->by_name(mark_type));
+  auto* type =
+      static_cast<simgrid::instr::EventType*>(simgrid::instr::Container::get_root()->get_type()->by_name(mark_type));
   if (not type) {
-    THROWF (tracing_error, 1, "mark_type with name (%s) is not declared", mark_type);
+    throw simgrid::TracingError(XBT_THROW_POINT,
+                                simgrid::xbt::string_printf("mark_type with name (%s) is not declared", mark_type));
   } else {
     if (not mark_color)
       mark_color = "1.0 1.0 1.0" /*white*/;
@@ -227,13 +229,14 @@ void TRACE_mark(const char *mark_type, const char *mark_value)
   xbt_assert(mark_value, "mark_value is nullptr");
 
   //check if mark_type is already declared
-  simgrid::instr::EventType* type =
-      static_cast<simgrid::instr::EventType*>(simgrid::instr::Container::get_root()->type_->by_name(mark_type));
+  auto* type =
+      static_cast<simgrid::instr::EventType*>(simgrid::instr::Container::get_root()->get_type()->by_name(mark_type));
   if (not type) {
-    THROWF (tracing_error, 1, "mark_type with name (%s) is not declared", mark_type);
+    throw simgrid::TracingError(XBT_THROW_POINT,
+                                simgrid::xbt::string_printf("mark_type with name (%s) is not declared", mark_type));
   } else {
     XBT_DEBUG("MARK %s %s", mark_type, mark_value);
-    new simgrid::instr::NewEvent(MSG_get_clock(), simgrid::instr::Container::get_root(), type,
+    new simgrid::instr::NewEvent(simgrid_get_clock(), simgrid::instr::Container::get_root(), type,
                                  type->get_entity_value(mark_value));
   }
 }
@@ -250,11 +253,12 @@ xbt_dynar_t TRACE_get_marks ()
   if (not TRACE_is_enabled())
     return nullptr;
 
-  return instr_set_to_dynar(&declared_marks);
+  return instr_set_to_dynar(declared_marks);
 }
 
-static void instr_user_variable(double time, const char* resource, const char* variable_name, const char* father_type,
-                                double value, InstrUserVariable what, const char* color, std::set<std::string>* filter)
+static void instr_user_variable(double time, const char* resource, const char* variable_name, const char* parent_type,
+                                double value, InstrUserVariable what, const char* color,
+                                std::set<std::string, std::less<>>* filter)
 {
   /* safe switches. tracing has to be activated and if platform is not traced, we don't allow user variables */
   if (not TRACE_is_enabled() || not TRACE_needs_platform())
@@ -265,7 +269,7 @@ static void instr_user_variable(double time, const char* resource, const char* v
   if (what == InstrUserVariable::DECLARE) {
     if (created == filter->end()) { // not declared yet
       filter->insert(variable_name);
-      instr_new_user_variable_type(father_type, variable_name, color == nullptr ? "" : color);
+      instr_new_user_variable_type(parent_type, variable_name, color == nullptr ? "" : color);
     }
   }else{
     if (created != filter->end()) { // declared, let's work
@@ -283,27 +287,24 @@ static void instr_user_variable(double time, const char* resource, const char* v
           break;
         default:
           THROW_IMPOSSIBLE;
-          break;
       }
     }
   }
 }
 
-static void instr_user_srcdst_variable(double time, const char *src, const char *dst, const char *variable,
-                              const char *father_type, double value, InstrUserVariable what)
+static void instr_user_srcdst_variable(double time, const char* src, const char* dst, const char* variable,
+                                       const char* parent_type, double value, InstrUserVariable what)
 {
-  simgrid::kernel::routing::NetPoint* src_elm = sg_netpoint_by_name_or_null(src);
-  if (not src_elm)
-    xbt_die("Element '%s' not found!",src);
+  const simgrid::kernel::routing::NetPoint* src_elm = sg_netpoint_by_name_or_null(src);
+  xbt_assert(src_elm, "Element '%s' not found!", src);
 
-  simgrid::kernel::routing::NetPoint* dst_elm = sg_netpoint_by_name_or_null(dst);
-  if (not dst_elm)
-    xbt_die("Element '%s' not found!",dst);
+  const simgrid::kernel::routing::NetPoint* dst_elm = sg_netpoint_by_name_or_null(dst);
+  xbt_assert(dst_elm, "Element '%s' not found!", dst);
 
-  std::vector<simgrid::kernel::resource::LinkImpl*> route;
+  std::vector<simgrid::kernel::resource::StandardLinkImpl*> route;
   simgrid::kernel::routing::NetZoneImpl::get_global_route(src_elm, dst_elm, route, nullptr);
   for (auto const& link : route)
-    instr_user_variable(time, link->get_cname(), variable, father_type, value, what, nullptr, &user_link_variables);
+    instr_user_variable(time, link->get_cname(), variable, parent_type, value, what, nullptr, &user_link_variables);
 }
 
 /** @ingroup TRACE_API
@@ -311,7 +312,7 @@ static void instr_user_srcdst_variable(double time, const char *src, const char
  *
  *  The graph topology will have the following properties: all hosts, links and routers of the platform file are mapped
  *  to graph nodes; routes are mapped to edges.
- *  The platform's AS are not represented in the output.
+ *  The platform's zones are not represented in the output.
  *
  *  @param filename The name of the file that will hold the graph.
  *
@@ -319,14 +320,7 @@ static void instr_user_srcdst_variable(double time, const char *src, const char
  */
 int TRACE_platform_graph_export_graphviz (const char *filename)
 {
-  /* returns 1 if successful, 0 otherwise */
-  if (not TRACE_is_enabled())
-    return 0;
-  xbt_graph_t g = instr_routing_platform_graph();
-  if (g == nullptr)
-    return 0;
-  instr_routing_platform_graph_export_graphviz (g, filename);
-  xbt_graph_free_graph(g, xbt_free_f, xbt_free_f, nullptr);
+  simgrid::instr::platform_graph_export_graphviz(filename);
   return 1;
 }
 
@@ -378,7 +372,7 @@ void TRACE_vm_variable_declare_with_color (const char *variable, const char *col
  */
 void TRACE_vm_variable_set (const char *vm, const char *variable, double value)
 {
-  TRACE_vm_variable_set_with_time (MSG_get_clock(), vm, variable, value);
+  TRACE_vm_variable_set_with_time(simgrid_get_clock(), vm, variable, value);
 }
 
 /** @ingroup TRACE_user_variables
@@ -392,7 +386,7 @@ void TRACE_vm_variable_set (const char *vm, const char *variable, double value)
  */
 void TRACE_vm_variable_add (const char *vm, const char *variable, double value)
 {
-  TRACE_vm_variable_add_with_time (MSG_get_clock(), vm, variable, value);
+  TRACE_vm_variable_add_with_time(simgrid_get_clock(), vm, variable, value);
 }
 
 /** @ingroup TRACE_user_variables
@@ -406,7 +400,7 @@ void TRACE_vm_variable_add (const char *vm, const char *variable, double value)
  */
 void TRACE_vm_variable_sub (const char *vm, const char *variable, double value)
 {
-  TRACE_vm_variable_sub_with_time (MSG_get_clock(), vm, variable, value);
+  TRACE_vm_variable_sub_with_time(simgrid_get_clock(), vm, variable, value);
 }
 
 /** @ingroup TRACE_user_variables
@@ -512,7 +506,7 @@ void TRACE_host_variable_declare_with_color (const char *variable, const char *c
  */
 void TRACE_host_variable_set (const char *host, const char *variable, double value)
 {
-  TRACE_host_variable_set_with_time (MSG_get_clock(), host, variable, value);
+  TRACE_host_variable_set_with_time(simgrid_get_clock(), host, variable, value);
 }
 
 /** @ingroup TRACE_user_variables
@@ -526,7 +520,7 @@ void TRACE_host_variable_set (const char *host, const char *variable, double val
  */
 void TRACE_host_variable_add (const char *host, const char *variable, double value)
 {
-  TRACE_host_variable_add_with_time (MSG_get_clock(), host, variable, value);
+  TRACE_host_variable_add_with_time(simgrid_get_clock(), host, variable, value);
 }
 
 /** @ingroup TRACE_user_variables
@@ -540,7 +534,7 @@ void TRACE_host_variable_add (const char *host, const char *variable, double val
  */
 void TRACE_host_variable_sub (const char *host, const char *variable, double value)
 {
-  TRACE_host_variable_sub_with_time (MSG_get_clock(), host, variable, value);
+  TRACE_host_variable_sub_with_time(simgrid_get_clock(), host, variable, value);
 }
 
 /** @ingroup TRACE_user_variables
@@ -613,7 +607,7 @@ void TRACE_host_variable_sub_with_time (double time, const char *host, const cha
  */
 xbt_dynar_t TRACE_get_host_variables ()
 {
-  return instr_set_to_dynar(&user_host_variables);
+  return instr_set_to_dynar(user_host_variables);
 }
 
 /* for link variables */
@@ -659,7 +653,7 @@ void TRACE_link_variable_declare_with_color (const char *variable, const char *c
  */
 void TRACE_link_variable_set (const char *link, const char *variable, double value)
 {
-  TRACE_link_variable_set_with_time (MSG_get_clock(), link, variable, value);
+  TRACE_link_variable_set_with_time(simgrid_get_clock(), link, variable, value);
 }
 
 /** @ingroup TRACE_user_variables
@@ -673,7 +667,7 @@ void TRACE_link_variable_set (const char *link, const char *variable, double val
  */
 void TRACE_link_variable_add (const char *link, const char *variable, double value)
 {
-  TRACE_link_variable_add_with_time (MSG_get_clock(), link, variable, value);
+  TRACE_link_variable_add_with_time(simgrid_get_clock(), link, variable, value);
 }
 
 /** @ingroup TRACE_user_variables
@@ -687,7 +681,7 @@ void TRACE_link_variable_add (const char *link, const char *variable, double val
  */
 void TRACE_link_variable_sub (const char *link, const char *variable, double value)
 {
-  TRACE_link_variable_sub_with_time (MSG_get_clock(), link, variable, value);
+  TRACE_link_variable_sub_with_time(simgrid_get_clock(), link, variable, value);
 }
 
 /** @ingroup TRACE_user_variables
@@ -767,7 +761,7 @@ void TRACE_link_variable_sub_with_time (double time, const char *link, const cha
  */
 void TRACE_link_srcdst_variable_set (const char *src, const char *dst, const char *variable, double value)
 {
-  TRACE_link_srcdst_variable_set_with_time (MSG_get_clock(), src, dst, variable, value);
+  TRACE_link_srcdst_variable_set_with_time(simgrid_get_clock(), src, dst, variable, value);
 }
 
 /** @ingroup TRACE_user_variables
@@ -786,7 +780,7 @@ void TRACE_link_srcdst_variable_set (const char *src, const char *dst, const cha
  */
 void TRACE_link_srcdst_variable_add (const char *src, const char *dst, const char *variable, double value)
 {
-  TRACE_link_srcdst_variable_add_with_time (MSG_get_clock(), src, dst, variable, value);
+  TRACE_link_srcdst_variable_add_with_time(simgrid_get_clock(), src, dst, variable, value);
 }
 
 /** @ingroup TRACE_user_variables
@@ -805,7 +799,7 @@ void TRACE_link_srcdst_variable_add (const char *src, const char *dst, const cha
  */
 void TRACE_link_srcdst_variable_sub (const char *src, const char *dst, const char *variable, double value)
 {
-  TRACE_link_srcdst_variable_sub_with_time (MSG_get_clock(), src, dst, variable, value);
+  TRACE_link_srcdst_variable_sub_with_time(simgrid_get_clock(), src, dst, variable, value);
 }
 
 /** @ingroup TRACE_user_variables
@@ -884,7 +878,7 @@ void TRACE_link_srcdst_variable_sub_with_time (double time, const char *src, con
  */
 xbt_dynar_t TRACE_get_link_variables ()
 {
-  return instr_set_to_dynar(&user_link_variables);
+  return instr_set_to_dynar(user_link_variables);
 }
 
 /** @ingroup TRACE_user_variables
@@ -967,29 +961,3 @@ void TRACE_host_pop_state(const char* host, const char* state_name)
 {
   simgrid::instr::Container::by_name(host)->get_state(state_name)->pop_event();
 }
-
-/** @ingroup TRACE_API
- *  @brief Get Paje container types that can be mapped to the nodes of a graph.
- *
- *  This function can be used to create a user made  graph configuration file for Triva. Normally, it is used with the
- *  functions defined in @ref TRACE_user_variables.
- *
- *  @return A dynar with the types, must be freed with xbt_dynar_free.
- */
-xbt_dynar_t TRACE_get_node_types ()
-{
-  return instr_set_to_dynar(&trivaNodeTypes);
-}
-
-/** @ingroup TRACE_API
- *  @brief Get Paje container types that can be mapped to the edges of a graph.
- *
- *  This function can be used to create a user made graph configuration file for Triva. Normally, it is used with the
- *  functions defined in @ref TRACE_user_variables.
- *
- *  @return A dynar with the types, must be freed with xbt_dynar_free.
- */
-xbt_dynar_t TRACE_get_edge_types ()
-{
-  return instr_set_to_dynar(&trivaEdgeTypes);
-}