From 15b07521c4eee3715b986ad1ab8ca854461f5c66 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20Trahay?=
 <francois.trahay@telecom-sudparis.eu>
Date: Thu, 8 Aug 2024 16:03:42 +0200
Subject: [PATCH] OTF2: show the thread begin/end events

---
 CMakeLists.txt                                |  2 +-
 .../OTF2Parser/ParserDefinitionOTF2.cpp       |  8 ++
 .../OTF2Parser/ParserDefinitionOTF2.hpp       |  1 +
 src/parser/OTF2Parser/ParserEventOTF2.cpp     | 98 ++++++++++++++++++-
 src/parser/ParserFactory.cpp                  |  2 +-
 5 files changed, 104 insertions(+), 7 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 2a615df3..33a723fc 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -5,7 +5,7 @@ project(VITE CXX C)
 set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/")
 
 # Set c++11 support
-set(CMAKE_CXX_STANDARD 11)
+set(CMAKE_CXX_STANDARD 17)
 set(CMAKE_CXX_STANDARD_REQUIRED ON)
 set(CMAKE_CXX_EXTENSIONS OFF)
 
diff --git a/src/parser/OTF2Parser/ParserDefinitionOTF2.cpp b/src/parser/OTF2Parser/ParserDefinitionOTF2.cpp
index ab903250..5a6c3aef 100644
--- a/src/parser/OTF2Parser/ParserDefinitionOTF2.cpp
+++ b/src/parser/OTF2Parser/ParserDefinitionOTF2.cpp
@@ -232,9 +232,12 @@ OTF2_CallbackCode ParserDefinitionOTF2::handler_DefLocation(void *userData,
                                                             uint64_t numberOfEvents,
                                                             OTF2_LocationGroupRef locationGroup) {
     OTF2_Reader *reader = (OTF2_Reader *)userData;
+    OTF2_Reader_SelectLocation(reader, locationIdentifier);
+
     OTF2_EvtReader *evt_reader = OTF2_Reader_GetEvtReader(reader, locationIdentifier);
     OTF2_DefReader *def_reader = OTF2_Reader_GetDefReader(reader, locationIdentifier);
     uint64_t definitions_read = 0;
+
     OTF2_Reader_ReadAllLocalDefinitions(reader, def_reader, &definitions_read);
 
     OTF2_Location *temp = new OTF2_Location();
@@ -537,6 +540,11 @@ Color *ParserDefinitionOTF2::get_color(uint32_t func_id) {
     return new Color(*_default_colors[func_id % NB_COLORS]);
 }
 
+ Color *ParserDefinitionOTF2::get_color(String function_name) {
+     uint32_t func_id = std::hash<std::string>{}(function_name.to_string());
+     return get_color(func_id);
+}
+
 //
 // Other public functions
 //
diff --git a/src/parser/OTF2Parser/ParserDefinitionOTF2.hpp b/src/parser/OTF2Parser/ParserDefinitionOTF2.hpp
index c5588711..dbfaa2da 100644
--- a/src/parser/OTF2Parser/ParserDefinitionOTF2.hpp
+++ b/src/parser/OTF2Parser/ParserDefinitionOTF2.hpp
@@ -454,6 +454,7 @@ public:
     static double get_timestamp(OTF2_TimeStamp ts);
 
     static Color *get_color(uint32_t func_id);
+    static Color *get_color(String function_name);
 
     static const char *get_string_by_id(uint32_t id);
 };
diff --git a/src/parser/OTF2Parser/ParserEventOTF2.cpp b/src/parser/OTF2Parser/ParserEventOTF2.cpp
index b1164c85..1417b9f0 100644
--- a/src/parser/OTF2Parser/ParserEventOTF2.cpp
+++ b/src/parser/OTF2Parser/ParserEventOTF2.cpp
@@ -178,6 +178,84 @@ OTF2_CallbackCode ParserEventOTF2::callback_ProgramEnd(OTF2_LocationRef location
     return OTF2_CALLBACK_SUCCESS;
 }
 
+static OTF2_CallbackCode pushState(OTF2_LocationRef locationID,
+                                   OTF2_TimeStamp time,
+                                   Trace *t,
+                                   String function_name) {
+
+    Date d = ParserDefinitionOTF2::get_timestamp(time);
+    map<string, Value *> extra_fields;
+
+    // get the container
+    OTF2_Location *temp_location = ParserDefinitionOTF2::get_location_by_id(locationID);
+    const String proc_name = String(ParserDefinitionOTF2::get_string_id(temp_location));
+    Container *temp_container = temp_location->container;
+
+#if defined(OTF2_DEBUG)
+    cout << d.to_string() << "  Enter_print(location=" << proc_name.to_string() << ", fname=" << function_name.to_string() << ")\n";
+#endif // defined(OTF2_DEBUG)
+
+    // get the state type
+    String state_type_string("Function");
+    StateType *temp_state_type = t->search_state_type(state_type_string);
+    if (temp_state_type == 0) {
+        Name name_temp(state_type_string);
+        ContainerType cont_type = *temp_container->get_type();
+        t->define_state_type(name_temp, &cont_type, extra_fields);
+        temp_state_type = t->search_state_type(state_type_string);
+        assert(temp_state_type != 0);
+    }
+
+    // get the entity value
+    EntityValue *temp_value = NULL;
+    temp_value = t->search_entity_value(function_name, temp_state_type);
+    if (temp_value == NULL) {
+        map<string, Value *> opt;
+        Name entity_name(function_name);
+        opt["Color"] = ParserDefinitionOTF2::get_color(function_name);
+        t->define_entity_value(entity_name, temp_state_type, opt);
+        temp_value = t->search_entity_value(function_name, temp_state_type);
+    }
+    assert(temp_value);
+
+    t->push_state(d, temp_state_type, temp_container, temp_value, extra_fields);
+
+    return OTF2_CALLBACK_SUCCESS;
+}
+
+
+static OTF2_CallbackCode popState(OTF2_LocationRef locationID,
+                                  OTF2_TimeStamp time,
+                                  Trace *t,
+                                  String function_name) {
+    Date d = ParserDefinitionOTF2::get_timestamp(time);
+    map<string, Value *> extra_fields;
+
+    // get the container
+    OTF2_Location *temp_location = ParserDefinitionOTF2::get_location_by_id(locationID);
+    const String proc_name = String(ParserDefinitionOTF2::get_string_id(temp_location));
+    Container *temp_container = temp_location->container;
+
+#if defined(OTF2_DEBUG)
+    cout << d.to_string() << "  Leave_print(location=" << proc_name.to_string() << ", fname=" << function_name.to_string() << ")\n";
+#endif // defined(OTF2_DEBUG)
+
+    // get the state type
+    String state_type_string("Function");
+    StateType *temp_state_type = t->search_state_type(state_type_string);
+    if (temp_state_type == 0) {
+        Name name_temp(state_type_string);
+        ContainerType cont_type = *temp_container->get_type();
+        t->define_state_type(name_temp, &cont_type, extra_fields);
+        temp_state_type = t->search_state_type(state_type_string);
+        assert(temp_state_type != 0);
+    }
+
+    t->pop_state(d, temp_state_type, temp_container, extra_fields);
+
+    return OTF2_CALLBACK_SUCCESS;
+}
+
 //
 // Start definition of handlers for OTF2 event records
 //
@@ -188,6 +266,11 @@ OTF2_CallbackCode ParserEventOTF2::callback_Enter(OTF2_LocationRef locationID,
                                                   OTF2_RegionRef regionID) {
     ENTER_CALLBACK(time, locationID);
     Trace *t = (Trace *)userData;
+    OTF2_Function temp_function = ParserDefinitionOTF2::get_function_by_id(regionID);
+    const String function_name = String(ParserDefinitionOTF2::get_string_by_id(temp_function._name_id));
+    return pushState(locationID, time, t, function_name);
+
+#if 0
     Date d = ParserDefinitionOTF2::get_timestamp(time);
     OTF2_Function temp_function = ParserDefinitionOTF2::get_function_by_id(regionID);
     const String function_name = String(ParserDefinitionOTF2::get_string_by_id(temp_function._name_id));
@@ -226,7 +309,7 @@ OTF2_CallbackCode ParserEventOTF2::callback_Enter(OTF2_LocationRef locationID,
     assert(temp_value);
 
     t->push_state(d, temp_state_type, temp_container, temp_value, extra_fields);
-
+#endif
     return OTF2_CALLBACK_SUCCESS;
 }
 
@@ -1025,14 +1108,15 @@ OTF2_CallbackCode ParserEventOTF2::callback_ThreadCreate(OTF2_LocationRef locati
 
 OTF2_CallbackCode ParserEventOTF2::callback_ThreadBegin(OTF2_LocationRef locationID,
                                                         OTF2_TimeStamp time,
-                                                        void * /*userData*/,
+                                                        void * userData,
                                                         OTF2_AttributeList * /*attributeList*/,
                                                         OTF2_CommRef /*threadContingent*/,
                                                         uint64_t /*sequenceCount*/) {
 
     ENTER_CALLBACK(time, locationID);
-    // Nothing to do here
-    return OTF2_CALLBACK_SUCCESS;
+    Trace *t = (Trace *)userData;
+    const String function_name("");
+    return pushState(locationID, time, t, function_name);
 }
 
 OTF2_CallbackCode ParserEventOTF2::callback_ThreadWait(OTF2_LocationRef locationID,
@@ -1048,12 +1132,16 @@ OTF2_CallbackCode ParserEventOTF2::callback_ThreadWait(OTF2_LocationRef location
 
 OTF2_CallbackCode ParserEventOTF2::callback_ThreadEnd(OTF2_LocationRef locationID,
                                                       OTF2_TimeStamp time,
-                                                      void * /*userData*/,
+                                                      void * userData,
                                                       OTF2_AttributeList * /*attributeList*/,
                                                       OTF2_CommRef /*threadContingent*/,
                                                       uint64_t /*sequenceCount*/) {
     ENTER_CALLBACK(time, locationID);
     // Nothing to do here
+    Trace *t = (Trace *)userData;
+    const String function_name("");
+    return popState(locationID, time, t, function_name);
+
     return OTF2_CALLBACK_SUCCESS;
 }
 
diff --git a/src/parser/ParserFactory.cpp b/src/parser/ParserFactory.cpp
index b55167a4..30d5505b 100644
--- a/src/parser/ParserFactory.cpp
+++ b/src/parser/ParserFactory.cpp
@@ -78,7 +78,7 @@ bool ParserFactory::create(Parser **parser,
         else if (ext == ".ept") {
             *parser = new ParserVite(filename);
         }
-        else if (ext == ".otf2") {
+        else if (ext == ".pallas") {
 #ifdef WITH_OTF2
             *parser = new ParserOTF2(filename);
 #else
-- 
GitLab