Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c++-ify interface to instr
authorSUTER Frederic <frederic.suter@cc.in2p3.fr>
Mon, 10 Jan 2022 12:41:22 +0000 (13:41 +0100)
committerSUTER Frederic <frederic.suter@cc.in2p3.fr>
Mon, 10 Jan 2022 12:50:19 +0000 (13:50 +0100)
examples/cpp/trace-categories/s4u-trace-categories.cpp
examples/cpp/trace-host-user-variables/s4u-trace-host-user-variables.cpp
examples/cpp/trace-link-user-variables/s4u-trace-link-user-variables.cpp
examples/cpp/trace-masterworkers/s4u-trace-masterworkers.cpp
examples/cpp/trace-process-migration/s4u-trace-process-migration.cpp
examples/cpp/trace-route-user-variables/s4u-trace-route-user-variables.cpp
include/simgrid/instr.h
src/instr/instr_interface.cpp
src/smpi/bindings/smpi_pmpi.cpp

index 98d5041..23d329f 100644 (file)
@@ -59,10 +59,10 @@ int main(int argc, char* argv[])
   e.load_platform(argv[1]);
 
   // declaring user categories with RGB colors
-  TRACE_category_with_color("compute", "1 0 0");  // red
-  TRACE_category_with_color("request", "0 1 0");  // green
-  TRACE_category_with_color("data", "0 0 1");     // blue
-  TRACE_category_with_color("finalize", "0 0 0"); // black
+  simgrid::instr::declare_tracing_category("compute", "1 0 0");  // red
+  simgrid::instr::declare_tracing_category("request", "0 1 0");  // green
+  simgrid::instr::declare_tracing_category("data", "0 0 1");     // blue
+  simgrid::instr::declare_tracing_category("finalize", "0 0 0"); // black
 
   simgrid::s4u::Actor::create("master", e.host_by_name("Tremblay"), master);
   simgrid::s4u::Actor::create("worker", e.host_by_name("Fafard"), worker);
index fd02dcd..00e4d6e 100644 (file)
@@ -15,18 +15,18 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example")
 
 static void trace_fun()
 {
-  const char* hostname = simgrid::s4u::this_actor::get_host()->get_cname();
+  auto* host = simgrid::s4u::this_actor::get_host();
 
   // the hostname has an empty HDD with a capacity of 100000 (bytes)
-  TRACE_host_variable_set(hostname, "HDD_capacity", 100000);
-  TRACE_host_variable_set(hostname, "HDD_utilization", 0);
+  simgrid::instr::set_host_variable(host, "HDD_capacity", 100000);
+  simgrid::instr::set_host_variable(host, "HDD_utilization", 0);
 
   for (int i = 0; i < 10; i++) {
     // create and execute a task just to make the simulated time advance
     simgrid::s4u::this_actor::execute(1e4);
 
     // ADD: after the execution of this task, the HDD utilization increases by 100 (bytes)
-    TRACE_host_variable_add(hostname, "HDD_utilization", 100);
+    simgrid::instr::add_host_variable(host, "HDD_utilization", 100);
   }
 
   for (int i = 0; i < 10; i++) {
@@ -34,7 +34,7 @@ static void trace_fun()
     simgrid::s4u::this_actor::execute(1e4);
 
     // SUB: after the execution of this task, the HDD utilization decreases by 100 (bytes)
-    TRACE_host_variable_sub(hostname, "HDD_utilization", 100);
+    simgrid::instr::sub_host_variable(host, "HDD_utilization", 100);
   }
 }
 
@@ -46,29 +46,22 @@ int main(int argc, char* argv[])
   e.load_platform(argv[1]);
 
   // declaring user variables
-  TRACE_host_variable_declare("HDD_capacity");
-  TRACE_host_variable_declare("HDD_utilization");
+  simgrid::instr::declare_host_variable("HDD_capacity");
+  simgrid::instr::declare_host_variable("HDD_utilization", "1 0 0"); // red color
 
   simgrid::s4u::Actor::create("master", e.host_by_name("Tremblay"), trace_fun);
 
   e.run();
 
   // get user declared variables
-  xbt_dynar_t host_variables = TRACE_get_host_variables();
-  if (host_variables) {
+  auto host_variables = simgrid::instr::get_host_variables();
+  if (not host_variables.empty()) {
     XBT_INFO("Declared host variables:");
-    unsigned int cursor;
-    char* variable;
-    xbt_dynar_foreach (host_variables, cursor, variable) {
-      XBT_INFO("%s", variable);
-    }
-    xbt_dynar_free(&host_variables);
-  }
-  xbt_dynar_t link_variables = TRACE_get_link_variables();
-  if (link_variables) {
-    xbt_assert(xbt_dynar_is_empty(link_variables), "Should not have any declared link variable!");
-    xbt_dynar_free(&link_variables);
+    for (const auto& var : host_variables)
+      XBT_INFO("%s", var.c_str());
   }
+  auto link_variables = simgrid::instr::get_link_variables();
+  xbt_assert(link_variables.empty(), "Should not have any declared link variable!");
 
   return 0;
 }
index 2758c47..59de93d 100644 (file)
@@ -15,30 +15,31 @@ static void trace_fun()
 {
   // set initial values for the link user variables this example only shows for links identified by "6" and "3" in the
   // platform file
-
+  auto* link_3 = simgrid::s4u::Link::by_name("3");
+  auto* link_6 = simgrid::s4u::Link::by_name("6");
   // Set the Link_Capacity variable
-  TRACE_link_variable_set("6", "Link_Capacity", 12.34);
-  TRACE_link_variable_set("3", "Link_Capacity", 56.78);
+  simgrid::instr::set_link_variable(link_6, "Link_Capacity", 12.34);
+  simgrid::instr::set_link_variable(link_3, "Link_Capacity", 56.78);
 
   // Set the Link_Utilization variable
-  TRACE_link_variable_set("3", "Link_Utilization", 1.2);
-  TRACE_link_variable_set("6", "Link_Utilization", 3.4);
+  simgrid::instr::set_link_variable(link_3, "Link_Utilization", 1.2);
+  simgrid::instr::set_link_variable(link_6, "Link_Utilization", 3.4);
 
   // run the simulation, update my variables accordingly
   for (int i = 0; i < 10; i++) {
     simgrid::s4u::this_actor::execute(1e6);
 
     // Add to link user variables
-    TRACE_link_variable_add("3", "Link_Utilization", 5.6);
-    TRACE_link_variable_add("6", "Link_Utilization", 7.8);
+    simgrid::instr::add_link_variable(link_3, "Link_Utilization", 5.6);
+    simgrid::instr::add_link_variable(link_6, "Link_Utilization", 7.8);
   }
 
   for (int i = 0; i < 10; i++) {
     simgrid::s4u::this_actor::execute(1e6);
 
     // Subtract from link user variables
-    TRACE_link_variable_sub("3", "Link_Utilization", 3.4);
-    TRACE_link_variable_sub("6", "Link_Utilization", 5.6);
+    simgrid::instr::sub_link_variable(link_3, "Link_Utilization", 3.4);
+    simgrid::instr::sub_link_variable(link_6, "Link_Utilization", 5.6);
   }
 }
 
@@ -50,8 +51,8 @@ int main(int argc, char* argv[])
   e.load_platform(argv[1]);
 
   // declaring link user variables (one without, another with an RGB color)
-  TRACE_link_variable_declare("Link_Capacity");
-  TRACE_link_variable_declare_with_color("Link_Utilization", "0.9 0.1 0.1");
+  simgrid::instr::declare_link_variable("Link_Capacity");
+  simgrid::instr::declare_link_variable("Link_Utilization", "0.9 0.1 0.1");
 
   simgrid::s4u::Actor::create("master", e.host_by_name("Tremblay"), trace_fun);
   simgrid::s4u::Actor::create("worker", e.host_by_name("Tremblay"), trace_fun);
index 15719c0..e3c3703 100644 (file)
@@ -22,24 +22,24 @@ static void master(std::vector<std::string> args)
   double compute_cost     = std::stod(args[2]);
   long communication_cost = std::stol(args[3]);
   size_t workers_count    = args.size() - 4;
-  const char* my_hostname = simgrid::s4u::this_actor::get_host()->get_cname();
+  auto* my_host           = simgrid::s4u::this_actor::get_host();
   auto mailbox            = simgrid::s4u::Mailbox::by_name("master_mailbox");
 
   XBT_DEBUG("Got %zu workers and %ld tasks to process", workers_count, tasks_count);
 
   // setting the variable "is_master" (previously declared) to value 1
-  TRACE_host_variable_set(my_hostname, "is_master", 1);
+  simgrid::instr::set_host_variable(my_host, "is_master", 1);
 
-  TRACE_mark("msmark", "start_send_tasks");
+  simgrid::instr::mark("msmark", "start_send_tasks");
   for (int i = 0; i < tasks_count; i++) {
     // setting the variable "task_creation" to value i
-    TRACE_host_variable_set(my_hostname, "task_creation", i);
+    simgrid::instr::set_host_variable(my_host, "task_creation", i);
 
     // setting the category of task to "compute"
     Task task = {"task", "compute", compute_cost};
     mailbox->put(new Task(task), communication_cost);
   }
-  TRACE_mark("msmark", "finish_send_tasks");
+  simgrid::instr::mark("msmark", "finish_send_tasks");
 
   XBT_DEBUG("All tasks have been dispatched. Request all workers to stop.");
   for (unsigned int i = 0; i < workers_count; i++) {
@@ -52,11 +52,11 @@ static void worker(std::vector<std::string> args)
 {
   xbt_assert(args.size() == 1, "The worker expects no argument");
 
-  const char* my_hostname = simgrid::s4u::this_actor::get_host()->get_cname();
-  auto mailbox            = simgrid::s4u::Mailbox::by_name("master_mailbox");
+  auto my_host = simgrid::s4u::this_actor::get_host();
+  auto mailbox = simgrid::s4u::Mailbox::by_name("master_mailbox");
 
-  TRACE_host_variable_set(my_hostname, "is_worker", 1);
-  TRACE_host_variable_set(my_hostname, "task_computation", 0);
+  simgrid::instr::set_host_variable(my_host, "is_worker", 1);
+  simgrid::instr::set_host_variable(my_host, "task_computation", 0);
 
   while (true) {
     auto task = mailbox->get_unique<Task>();
@@ -64,7 +64,7 @@ static void worker(std::vector<std::string> args)
       break;
     }
     // adding the task's cost to the variable "task_computation"
-    TRACE_host_variable_add(my_hostname, "task_computation", task->flops);
+    simgrid::instr::add_host_variable(my_host, "task_computation", task->flops);
     simgrid::s4u::this_actor::exec_init(task->flops)
         ->set_name(task->name)
         ->set_tracing_category(task->category)
@@ -82,22 +82,22 @@ int main(int argc, char* argv[])
   e.load_platform(argv[1]);
 
   // declaring user variables
-  TRACE_host_variable_declare("is_worker");
-  TRACE_host_variable_declare("is_master");
-  TRACE_host_variable_declare("task_creation");
-  TRACE_host_variable_declare("task_computation");
+  simgrid::instr::declare_host_variable("is_worker");
+  simgrid::instr::declare_host_variable("is_master");
+  simgrid::instr::declare_host_variable("task_creation");
+  simgrid::instr::declare_host_variable("task_computation");
 
   // declaring user markers and values
-  TRACE_declare_mark("msmark");
-  TRACE_declare_mark_value("msmark", "start_send_tasks");
-  TRACE_declare_mark_value("msmark", "finish_send_tasks");
+  simgrid::instr::declare_mark("msmark");
+  simgrid::instr::declare_mark_value("msmark", "start_send_tasks");
+  simgrid::instr::declare_mark_value("msmark", "finish_send_tasks");
 
   // declaring user categories with RGB colors (values from 0 to 1)
-  TRACE_category_with_color("compute", "1 0 0");  // compute is red
-  TRACE_category_with_color("finalize", "0 1 0"); // finalize is green
+  simgrid::instr::declare_tracing_category("compute", "1 0 0");  // compute is red
+  simgrid::instr::declare_tracing_category("finalize", "0 1 0"); // finalize is green
   // categories without user-defined colors receive random colors generated by the tracing system
-  TRACE_category("request");
-  TRACE_category_with_color("report", nullptr);
+  simgrid::instr::declare_tracing_category("request");
+  simgrid::instr::declare_tracing_category("report");
 
   e.register_function("master", &master);
   e.register_function("worker", &worker);
@@ -107,25 +107,18 @@ int main(int argc, char* argv[])
 
   XBT_DEBUG("Simulation is over");
 
-  unsigned int cursor;
-  xbt_dynar_t categories = TRACE_get_categories();
-  if (categories) {
+  auto categories = simgrid::instr::get_tracing_categories();
+  if (not categories.empty()) {
     XBT_INFO("Declared tracing categories:");
-    char* category;
-    xbt_dynar_foreach (categories, cursor, category) {
-      XBT_INFO("%s", category);
-    }
-    xbt_dynar_free(&categories);
+    for (const auto& category : categories)
+      XBT_INFO("%s", category.c_str());
   }
 
-  xbt_dynar_t marks = TRACE_get_marks();
-  if (marks) {
+  auto marks = simgrid::instr::get_marks();
+  if (not marks.empty()) {
     XBT_INFO("Declared marks:");
-    char* mark;
-    xbt_dynar_foreach (marks, cursor, mark) {
-      XBT_INFO("%s", mark);
-    }
-    xbt_dynar_free(&marks);
+    for (const auto& mark : marks)
+      XBT_INFO("%s", mark.c_str());
   }
 
   return 0;
index 18b2d17..0f4506f 100644 (file)
@@ -49,7 +49,7 @@ int main(int argc, char* argv[])
 
   e.load_platform(argv[1]);
 
-  TRACE_category("migration_order");
+  simgrid::instr::declare_tracing_category("migration_order");
 
   sg4::Actor::create("emigrant", e.host_by_name("Fafard"), emigrant);
   sg4::Actor::create("policeman", e.host_by_name("Tremblay"), policeman);
index 77ee554..53a11ce 100644 (file)
@@ -15,33 +15,38 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example")
 
 static void trace_fun()
 {
+  auto bourassa = simgrid::s4u::Host::by_name("Bourassa");
+  auto fafard   = simgrid::s4u::Host::by_name("Fafard");
+  auto ginette  = simgrid::s4u::Host::by_name("Ginette");
+  auto tremblay = simgrid::s4u::Host::by_name("Tremblay");
+
   // Set initial values for the link user variables
   // This example uses source and destination where source and destination are the name of hosts in the platform file.
   // The functions will set/change the value of the variable for all links in the route between source and destination.
 
   // Set the Link_Capacity variable
-  TRACE_link_srcdst_variable_set("Tremblay", "Bourassa", "Link_Capacity", 12.34);
-  TRACE_link_srcdst_variable_set("Fafard", "Ginette", "Link_Capacity", 56.78);
+  simgrid::instr::set_link_variable(tremblay, bourassa, "Link_Capacity", 12.34);
+  simgrid::instr::set_link_variable(fafard, ginette, "Link_Capacity", 56.78);
 
   // Set the Link_Utilization variable
-  TRACE_link_srcdst_variable_set("Tremblay", "Bourassa", "Link_Utilization", 1.2);
-  TRACE_link_srcdst_variable_set("Fafard", "Ginette", "Link_Utilization", 3.4);
+  simgrid::instr::set_link_variable(tremblay, bourassa, "Link_Utilization", 1.2);
+  simgrid::instr::set_link_variable(fafard, ginette, "Link_Utilization", 3.4);
 
   // run the simulation, update my variables accordingly
   for (int i = 0; i < 10; i++) {
     simgrid::s4u::this_actor::execute(1e6);
 
     // Add to link user variables
-    TRACE_link_srcdst_variable_add("Tremblay", "Bourassa", "Link_Utilization", 5.6);
-    TRACE_link_srcdst_variable_add("Fafard", "Ginette", "Link_Utilization", 7.8);
+    simgrid::instr::add_link_variable(tremblay, bourassa, "Link_Utilization", 5.6);
+    simgrid::instr::add_link_variable(fafard, ginette, "Link_Utilization", 7.8);
   }
 
   for (int i = 0; i < 10; i++) {
     simgrid::s4u::this_actor::execute(1e6);
 
     // Subtract from link user variables
-    TRACE_link_srcdst_variable_sub("Tremblay", "Bourassa", "Link_Utilization", 3.4);
-    TRACE_link_srcdst_variable_sub("Fafard", "Ginette", "Link_Utilization", 5.6);
+    simgrid::instr::sub_link_variable(tremblay, bourassa, "Link_Utilization", 3.4);
+    simgrid::instr::sub_link_variable(fafard, ginette, "Link_Utilization", 5.6);
   }
 }
 
@@ -53,8 +58,8 @@ int main(int argc, char* argv[])
   e.load_platform(argv[1]);
 
   // declaring link user variables (one without, another with an RGB color)
-  TRACE_link_variable_declare("Link_Capacity");
-  TRACE_link_variable_declare_with_color("Link_Utilization", "0.9 0.1 0.1");
+  simgrid::instr::declare_link_variable("Link_Capacity");
+  simgrid::instr::declare_link_variable("Link_Utilization", "0.9 0.1 0.1");
 
   simgrid::s4u::Actor::create("master", e.host_by_name("Tremblay"), trace_fun);
   simgrid::s4u::Actor::create("worker", e.host_by_name("Tremblay"), trace_fun);
index ad9c8ae..7121613 100644 (file)
@@ -8,6 +8,48 @@
 
 #include <simgrid/msg.h>
 
+#ifdef __cplusplus
+#include <set>
+#include <string>
+
+namespace simgrid {
+namespace instr {
+void declare_host_variable(const std::string& variable, const std::string& color = std::string(""));
+void set_host_variable(const s4u::Host* host, const std::string& variable, double value,
+                       double time = simgrid_get_clock());
+void add_host_variable(const s4u::Host* host, const std::string& variable, double value,
+                       double time = simgrid_get_clock());
+void sub_host_variable(const s4u::Host* host, const std::string& variable, double value,
+                       double time = simgrid_get_clock());
+const std::set<std::string, std::less<>>& get_host_variables();
+
+void declare_link_variable(const std::string& variable, const std::string& color = std::string(""));
+void set_link_variable(const s4u::Link* link, const std::string& variable, double value,
+                       double time = simgrid_get_clock());
+void set_link_variable(const s4u::Host* src, const s4u::Host* dst, const std::string& variable, double value,
+                       double time = simgrid_get_clock());
+void add_link_variable(const s4u::Link* link, const std::string& variable, double value,
+                       double time = simgrid_get_clock());
+void add_link_variable(const s4u::Host* src, const s4u::Host* dst, const std::string& variable, double value,
+                       double time = simgrid_get_clock());
+void sub_link_variable(const s4u::Link* link, const std::string& variable, double value,
+                       double time = simgrid_get_clock());
+void sub_link_variable(const s4u::Host* src, const s4u::Host* dst, const std::string& variable, double value,
+                       double time = simgrid_get_clock());
+const std::set<std::string, std::less<>>& get_link_variables();
+
+void declare_mark(const std::string& mark_type);
+void declare_mark_value(const std::string& mark_type, const std::string& mark_value,
+                        const std::string& mark_color = std::string("1 1 1"));
+void mark(const std::string& mark_type, const std::string& mark_value);
+const std::set<std::string, std::less<>>& get_marks();
+
+void declare_tracing_category(const std::string& name, const std::string& color = "");
+const std::set<std::string, std::less<>>& get_tracing_categories();
+} // namespace instr
+} // namespace simgrid
+
+#endif
 SG_BEGIN_DECL
 
 /* Functions to manage tracing categories */
index 3158bfe..bcbeba4 100644 (file)
@@ -5,6 +5,7 @@
 
 #include <simgrid/Exception.hpp>
 #include <simgrid/kernel/routing/NetPoint.hpp>
+#include <simgrid/s4u/Host.hpp>
 #include <xbt/random.hpp>
 
 #include "src/instr/instr_private.hpp"
@@ -22,6 +23,227 @@ 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 void instr_user_variable(double time, const char* resource, const std::string& variable_name,
+                                const std::string& parent_type, double value, InstrUserVariable what,
+                                const std::string& 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())
+    return;
+
+  // check if variable is already declared
+  auto created = filter->find(variable_name);
+  if (what == InstrUserVariable::DECLARE) {
+    if (created == filter->end()) { // not declared yet
+      filter->insert(variable_name);
+      instr_new_user_variable_type(parent_type, variable_name, color);
+    }
+  } else {
+    if (created != filter->end()) { // declared, let's work
+      simgrid::instr::VariableType* variable =
+          simgrid::instr::Container::by_name(resource)->get_variable(variable_name);
+      switch (what) {
+        case InstrUserVariable::SET:
+          variable->set_event(time, value);
+          break;
+        case InstrUserVariable::ADD:
+          variable->add_event(time, value);
+          break;
+        case InstrUserVariable::SUB:
+          variable->sub_event(time, value);
+          break;
+        default:
+          THROW_IMPOSSIBLE;
+      }
+    }
+  }
+}
+
+static void instr_user_srcdst_variable(double time, const char* src, const char* dst, const std::string& variable,
+                                       const std::string& parent_type, double value, InstrUserVariable what)
+{
+  const simgrid::kernel::routing::NetPoint* src_elm = sg_netpoint_by_name_or_null(src);
+  xbt_assert(src_elm, "Element '%s' not found!", src);
+
+  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::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, parent_type, value, what, "", &user_link_variables);
+}
+
+namespace simgrid {
+namespace instr {
+
+void declare_host_variable(const std::string& variable, const std::string& color)
+{
+  instr_user_variable(0, nullptr, variable, "HOST", 0, InstrUserVariable::DECLARE, color, &user_host_variables);
+}
+
+void set_host_variable(const s4u::Host* host, const std::string& variable, double value, double time)
+{
+  instr_user_variable(time, host->get_cname(), variable, "HOST", value, InstrUserVariable::SET, "",
+                      &user_host_variables);
+}
+
+void add_host_variable(const s4u::Host* host, const std::string& variable, double value, double time)
+{
+  instr_user_variable(time, host->get_cname(), variable, "HOST", value, InstrUserVariable::ADD, "",
+                      &user_host_variables);
+}
+
+void sub_host_variable(const s4u::Host* host, const std::string& variable, double value, double time)
+{
+  instr_user_variable(time, host->get_cname(), variable, "HOST", value, InstrUserVariable::SUB, "",
+                      &user_host_variables);
+}
+
+const std::set<std::string, std::less<>>& get_host_variables()
+{
+  return user_host_variables;
+}
+
+void declare_link_variable(const std::string& variable, const std::string& color)
+{
+  instr_user_variable(0, nullptr, variable, "LINK", 0, InstrUserVariable::DECLARE, color, &user_link_variables);
+}
+
+void set_link_variable(const s4u::Link* link, const std::string& variable, double value, double time)
+{
+  instr_user_variable(time, link->get_cname(), variable, "LINK", value, InstrUserVariable::SET, "",
+                      &user_link_variables);
+}
+
+void set_link_variable(const s4u::Host* src, const s4u::Host* dst, const std::string& variable, double value,
+                       double time)
+{
+  instr_user_srcdst_variable(time, src->get_cname(), dst->get_cname(), variable, "LINK", value, InstrUserVariable::SET);
+}
+
+void add_link_variable(const s4u::Link* link, const std::string& variable, double value, double time)
+{
+  instr_user_variable(time, link->get_cname(), variable, "LINK", value, InstrUserVariable::ADD, "",
+                      &user_link_variables);
+}
+
+void add_link_variable(const s4u::Host* src, const s4u::Host* dst, const std::string& variable, double value,
+                       double time)
+{
+  instr_user_srcdst_variable(time, src->get_cname(), dst->get_cname(), variable, "LINK", value, InstrUserVariable::ADD);
+}
+
+void sub_link_variable(const s4u::Link* link, const std::string& variable, double value, double time)
+{
+  instr_user_variable(time, link->get_cname(), variable, "LINK", value, InstrUserVariable::SUB, "",
+                      &user_link_variables);
+}
+
+void sub_link_variable(const s4u::Host* src, const s4u::Host* dst, const std::string& variable, double value,
+                       double time)
+{
+  instr_user_srcdst_variable(time, src->get_cname(), dst->get_cname(), variable, "LINK", value, InstrUserVariable::SUB);
+}
+
+const std::set<std::string, std::less<>>& get_link_variables()
+{
+  return user_link_variables;
+}
+
+void declare_mark(const std::string& mark_type)
+{
+  /* 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;
+
+  // check if mark_type is already declared
+  if (declared_marks.find(mark_type) != declared_marks.end()) {
+    throw TracingError(XBT_THROW_POINT,
+                       xbt::string_printf("mark_type with name (%s) is already declared", mark_type.c_str()));
+  }
+
+  XBT_DEBUG("MARK,declare %s", mark_type.c_str());
+  Container::get_root()->get_type()->by_name_or_create<EventType>(mark_type);
+  declared_marks.emplace(mark_type);
+}
+
+void declare_mark_value(const std::string& mark_type, const std::string& mark_value, const std::string& mark_color)
+{
+  /* 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;
+
+  auto* type = static_cast<EventType*>(Container::get_root()->get_type()->by_name(mark_type));
+  if (not type) {
+    throw TracingError(XBT_THROW_POINT,
+                       xbt::string_printf("mark_type with name (%s) is not declared", mark_type.c_str()));
+  } else {
+    XBT_DEBUG("MARK, declare_value %s %s %s", mark_type.c_str(), mark_value.c_str(), mark_color.c_str());
+    type->add_entity_value(mark_value, mark_color);
+  }
+}
+
+void mark(const std::string& mark_type, const std::string& mark_value)
+{
+  /* 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;
+
+  // check if mark_type is already declared
+  auto* type = static_cast<EventType*>(Container::get_root()->get_type()->by_name(mark_type));
+  if (not type) {
+    throw TracingError(XBT_THROW_POINT,
+                       xbt::string_printf("mark_type with name (%s) is not declared", mark_type.c_str()));
+  } else {
+    XBT_DEBUG("MARK %s %s", mark_type.c_str(), mark_value.c_str());
+    new NewEvent(simgrid_get_clock(), Container::get_root(), type, type->get_entity_value(mark_value));
+  }
+}
+
+const std::set<std::string, std::less<>>& get_marks()
+{
+  return declared_marks;
+}
+
+void declare_tracing_category(const std::string& name, const std::string& color)
+{
+  /* safe switches. tracing has to be activated and if platform is not traced, we can't deal with categories */
+  if (not TRACE_is_enabled() || not TRACE_needs_platform() || not TRACE_categorized())
+    return;
+
+  // check if category is already created
+  if (created_categories.find(name) != created_categories.end())
+    return;
+
+  created_categories.emplace(name);
+
+  // define final_color
+  std::string final_color;
+  if (color.empty()) {
+    // generate a random color
+    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);
+  }
+
+  XBT_DEBUG("CAT,declare %s, \"%s\" \"%s\"", name.c_str(), color.c_str(), final_color.c_str());
+
+  // define the type of this category on top of hosts and links
+  instr_new_variable_type(name, final_color);
+}
+
+const std::set<std::string, std::less<>>& get_tracing_categories()
+{
+  return created_categories;
+}
+
+} // namespace instr
+} // namespace simgrid
+
 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())
@@ -47,11 +269,11 @@ static xbt_dynar_t instr_set_to_dynar(const std::set<std::string, std::less<>>&
  *
  *  @param category The name of the new tracing category to be created.
  *
- *  @see TRACE_category_with_color, MSG_task_set_category, SD_task_set_category
+ *  @see TRACE_category_with_color
  */
 void TRACE_category(const char *category)
 {
-  TRACE_category_with_color (category, nullptr);
+  simgrid::instr::declare_tracing_category(category);
 }
 
 /** @ingroup TRACE_category
@@ -67,39 +289,10 @@ void TRACE_category(const char *category)
  *  @param color The color of the category (see @ref outcomes_vizu to
  *  know how to correctly specify the color)
  *
- *  @see MSG_task_set_category, SD_task_set_category
  */
 void TRACE_category_with_color (const char *category, const char *color)
 {
-  /* safe switches. tracing has to be activated and if platform is not traced, we can't deal with categories */
-  if (not TRACE_is_enabled() || not TRACE_needs_platform())
-    return;
-
-  if (not(TRACE_categorized() && category != nullptr))
-    return;
-
-  //check if category is already created
-  if (created_categories.find(category) != created_categories.end())
-    return;
-
-  created_categories.emplace(category);
-
-  //define final_color
-  std::string final_color;
-  if (not color) {
-    //generate a random color
-    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);
-  }
-
-  XBT_DEBUG("CAT,declare %s, \"%s\" \"%s\"", category, color, final_color.c_str());
-
-  //define the type of this category on top of hosts and links
-  instr_new_variable_type (category, final_color);
+  simgrid::instr::declare_tracing_category(category, color);
 }
 
 /** @ingroup TRACE_category
@@ -112,7 +305,6 @@ void TRACE_category_with_color (const char *category, const char *color)
  *
  * @return A dynar with the declared categories, must be freed with xbt_dynar_free.
  *
- *  @see MSG_task_set_category, SD_task_set_category
  */
 xbt_dynar_t TRACE_get_categories ()
 {
@@ -133,21 +325,7 @@ xbt_dynar_t TRACE_get_categories ()
  */
 void TRACE_declare_mark(const char *mark_type)
 {
-  /* 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;
-
-  xbt_assert(mark_type, "mark_type is nullptr");
-
-  //check if mark_type is already declared
-  if (declared_marks.find(mark_type) != declared_marks.end()) {
-    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()->get_type()->by_name_or_create<simgrid::instr::EventType>(mark_type);
-  declared_marks.emplace(mark_type);
+  simgrid::instr::declare_mark(mark_type);
 }
 
 /** @ingroup TRACE_mark
@@ -167,25 +345,7 @@ void TRACE_declare_mark(const char *mark_type)
  */
 void TRACE_declare_mark_value_with_color (const char *mark_type, const char *mark_value, const char *mark_color)
 {
-  /* 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;
-
-  xbt_assert(mark_type, "mark_type is nullptr");
-  xbt_assert(mark_value, "mark_value is nullptr");
-
-  auto* type =
-      static_cast<simgrid::instr::EventType*>(simgrid::instr::Container::get_root()->get_type()->by_name(mark_type));
-  if (not 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*/;
-
-    XBT_DEBUG("MARK,declare_value %s %s %s", mark_type, mark_value, mark_color);
-    type->add_entity_value(mark_value, mark_color);
-  }
+  simgrid::instr::declare_mark_value(mark_type, mark_value, mark_color);
 }
 
 /** @ingroup TRACE_mark
@@ -202,7 +362,7 @@ void TRACE_declare_mark_value_with_color (const char *mark_type, const char *mar
  */
 void TRACE_declare_mark_value (const char *mark_type, const char *mark_value)
 {
-  TRACE_declare_mark_value_with_color (mark_type, mark_value, nullptr);
+  simgrid::instr::declare_mark_value(mark_type, mark_value);
 }
 
 /**
@@ -221,24 +381,7 @@ void TRACE_declare_mark_value (const char *mark_type, const char *mark_value)
  */
 void TRACE_mark(const char *mark_type, const char *mark_value)
 {
-  /* 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;
-
-  xbt_assert(mark_type, "mark_type is nullptr");
-  xbt_assert(mark_value, "mark_value is nullptr");
-
-  //check if mark_type is already declared
-  auto* type =
-      static_cast<simgrid::instr::EventType*>(simgrid::instr::Container::get_root()->get_type()->by_name(mark_type));
-  if (not 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(simgrid_get_clock(), simgrid::instr::Container::get_root(), type,
-                                 type->get_entity_value(mark_value));
-  }
+  simgrid::instr::mark(mark_type, mark_value);
 }
 
 /** @ingroup TRACE_mark
@@ -256,57 +399,6 @@ xbt_dynar_t TRACE_get_marks ()
   return instr_set_to_dynar(declared_marks);
 }
 
-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())
-    return;
-
-  //check if variable is already declared
-  auto created = filter->find(variable_name);
-  if (what == InstrUserVariable::DECLARE) {
-    if (created == filter->end()) { // not declared yet
-      filter->insert(variable_name);
-      instr_new_user_variable_type(parent_type, variable_name, color == nullptr ? "" : color);
-    }
-  }else{
-    if (created != filter->end()) { // declared, let's work
-      simgrid::instr::VariableType* variable =
-          simgrid::instr::Container::by_name(resource)->get_variable(variable_name);
-      switch (what){
-        case InstrUserVariable::SET:
-          variable->set_event(time, value);
-          break;
-        case InstrUserVariable::ADD:
-          variable->add_event(time, value);
-          break;
-        case InstrUserVariable::SUB:
-          variable->sub_event(time, value);
-          break;
-        default:
-          THROW_IMPOSSIBLE;
-      }
-    }
-  }
-}
-
-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)
-{
-  const simgrid::kernel::routing::NetPoint* src_elm = sg_netpoint_by_name_or_null(src);
-  xbt_assert(src_elm, "Element '%s' not found!", src);
-
-  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::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, parent_type, value, what, nullptr, &user_link_variables);
-}
-
 /** @ingroup TRACE_API
  *  @brief Creates a file with the topology of the platform file used for the simulator.
  *
index 8ba6926..f2638de 100644 (file)
@@ -25,7 +25,7 @@ void TRACE_smpi_set_category(const char *category)
 
   if (category != nullptr) {
     // declare category
-    TRACE_category(category);
+    simgrid::instr::declare_tracing_category(category);
     smpi_process()->set_tracing_category(category);
   }
 }