Skip to contentSkip to conversation

verification/dot2k: Improve template management

PATCH New Gabriele Monaco 1 participant 5 messages Nov 21, 2024

Subsystems (2)
lore
Share & tools
lore

Gabriele Monaco

author maintainer
#0 lore raw

cover verification/dot2k: Improve template management

This patchset implements a couple of options in the dot2k command and makes the templates a little more robust.

The base is linux-next.

Gabriele Monaco (4):
verification/dot2k: Fix template directory if not installed verification/dot2k: Unify main.c templates verification/dot2k: More robust template variables verification/dot2k: Add support for name and description options

 tools/verification/dot2/automata.py           |  4 +-
 tools/verification/dot2/dot2c.py              |  4 +-
 tools/verification/dot2/dot2k                 |  6 +-
 tools/verification/dot2/dot2k.py              | 41 +++++----
 .../dot2k_templates/{main_global.c => main.c} | 50 +++++-----
 .../dot2/dot2k_templates/main_per_cpu.c       | 91 -------------------
 .../dot2/dot2k_templates/main_per_task.c      | 91 -------------------
 7 files changed, 54 insertions(+), 233 deletions(-)
 rename tools/verification/dot2/dot2k_templates/{main_global.c => main.c} (50%)
 delete mode 100644 tools/verification/dot2/dot2k_templates/main_per_cpu.c
 delete mode 100644 tools/verification/dot2/dot2k_templates/main_per_task.c

base-commit: ac24e26aa08fe026804f678599f805eb13374a5d

Signature · 1 line
2.47.0
author maintainer
#1 lore raw

PATCH 1/4 verification/dot2k: Fix template directory if not installed

This patch adjusts the directory where dot2k looks for templates if we are in the kernel tree: we can run dot2k without installing it if the current directory is <linux>/tools/verification by running something like python dot2/dot2k ...

Additionally we fix a few simple pylint warnings in boolean expressions.

---
 tools/verification/dot2/dot2k.py | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
tools/verification/dot2/dot2k.py +8 -8
index 016550fccf1f..f6d02e3406a3 100644
@@ -14,14 +14,14 @@ import os
 
 class dot2k(Dot2c):
     monitor_types = { "global" : 1, "per_cpu" : 2, "per_task" : 3 }
-    monitor_templates_dir = "dot2k/rv_templates/"
+    monitor_templates_dir = "dot2/dot2k_templates/"
     monitor_type = "per_cpu"
 
     def __init__(self, file_path, MonitorType):
         super().__init__(file_path)
 
         self.monitor_type = self.monitor_types.get(MonitorType)
-        if self.monitor_type == None:
+        if self.monitor_type is None:
             raise Exception("Unknown monitor type: %s" % MonitorType)
 
         self.monitor_type = MonitorType
@@ -31,7 +31,7 @@ class dot2k(Dot2c):
 
     def __fill_rv_templates_dir(self):
 
-        if os.path.exists(self.monitor_templates_dir) == True:
+        if os.path.exists(self.monitor_templates_dir):
             return
 
         if platform.system() != "Linux":
@@ -39,11 +39,11 @@ class dot2k(Dot2c):
 
         kernel_path = "/lib/modules/%s/build/tools/verification/dot2/dot2k_templates/" % (platform.release())
 
-        if os.path.exists(kernel_path) == True:
+        if os.path.exists(kernel_path):
             self.monitor_templates_dir = kernel_path
             return
 
-        if os.path.exists("/usr/share/dot2/dot2k_templates/") == True:
+        if os.path.exists("/usr/share/dot2/dot2k_templates/"):
             self.monitor_templates_dir = "/usr/share/dot2/dot2k_templates/"
             return
 
@@ -98,7 +98,7 @@ class dot2k(Dot2c):
     def fill_main_c(self):
         main_c = self.main_c
         min_type = self.get_minimun_type()
-        nr_events = self.events.__len__()
+        nr_events = len(self.events)
         tracepoint_handlers = self.fill_tracepoint_handlers_skel()
         tracepoint_attach = self.fill_tracepoint_attach_probe()
         tracepoint_detach = self.fill_tracepoint_detach_helper()
@@ -160,8 +160,8 @@ class dot2k(Dot2c):
 
     def __get_main_name(self):
         path = "%s/%s" % (self.name, "main.c")
-        if os.path.exists(path) == False:
-           return "main.c"
+        if not os.path.exists(path):
+            return "main.c"
         return "__main.c"
 
     def print_files(self):
Signature · 1 line
2.47.0
author maintainer
#2 lore raw

PATCH 2/4 verification/dot2k: Unify main.c templates

In reply to Gabriele Monaco

This patch removes the 3 dot2k templates and replaces them with a more general one. The only difference among the 3 templates was the DECLARE_DA_MON_* call, which can be adjusted by the python code.

This change eases changing the templates, which will be done in following patches.

---
 tools/verification/dot2/dot2k.py              |  7 +-
 .../dot2k_templates/{main_global.c => main.c} |  2 +-
 .../dot2/dot2k_templates/main_per_cpu.c       | 91 -------------------
 .../dot2/dot2k_templates/main_per_task.c      | 91 -------------------
 4 files changed, 7 insertions(+), 184 deletions(-)
 rename tools/verification/dot2/dot2k_templates/{main_global.c => main.c} (97%)
 delete mode 100644 tools/verification/dot2/dot2k_templates/main_per_cpu.c
 delete mode 100644 tools/verification/dot2/dot2k_templates/main_per_task.c
tools/verification/dot2/dot2k.py +6 -1
index f6d02e3406a3..15d6f7048f8d 100644
@@ -26,7 +26,7 @@ class dot2k(Dot2c):
 
         self.monitor_type = MonitorType
         self.__fill_rv_templates_dir()
-        self.main_c = self.__open_file(self.monitor_templates_dir + "main_" + MonitorType + ".c")
+        self.main_c = self.__open_file(self.monitor_templates_dir + "main.c")
         self.enum_suffix = "_%s" % self.name
 
     def __fill_rv_templates_dir(self):
@@ -69,6 +69,9 @@ class dot2k(Dot2c):
         # cut off the last \n
         return string[:-1]
 
+    def fill_monitor_type(self):
+        return self.monitor_type.upper()
+
     def fill_tracepoint_handlers_skel(self):
         buff = []
         for event in self.events:
@@ -97,12 +100,14 @@ class dot2k(Dot2c):
 
     def fill_main_c(self):
         main_c = self.main_c
+        monitor_type = self.fill_monitor_type()
         min_type = self.get_minimun_type()
         nr_events = len(self.events)
         tracepoint_handlers = self.fill_tracepoint_handlers_skel()
         tracepoint_attach = self.fill_tracepoint_attach_probe()
         tracepoint_detach = self.fill_tracepoint_detach_helper()
 
+        main_c = main_c.replace("MONITOR_TYPE", monitor_type)
         main_c = main_c.replace("MIN_TYPE", min_type)
         main_c = main_c.replace("MODEL_NAME", self.name)
         main_c = main_c.replace("NR_EVENTS", str(nr_events))
tools/verification/dot2/dot2k_templates/main.c renamed from tools/verification/dot2/dot2k_templates/main_global.c +1 -1
similarity index 97%
rename from tools/verification/dot2/dot2k_templates/main_global.c
rename to tools/verification/dot2/dot2k_templates/main.c
index a5658bfb9044..2419a6f89cd8 100644
@@ -28,7 +28,7 @@
  * The rv monitor reference is needed for the monitor declaration.
  */
 static struct rv_monitor rv_MODEL_NAME;
-DECLARE_DA_MON_GLOBAL(MODEL_NAME, MIN_TYPE);
+DECLARE_DA_MON_MONITOR_TYPE(MODEL_NAME, MIN_TYPE);
 
 /*
  * This is the instrumentation part of the monitor.
tools/verification/dot2/dot2k_templates/main_per_cpu.c deleted +0 -91

91 lines not on this page · raw message

tools/verification/dot2/dot2k_templates/main_per_task.c deleted +0 -91

91 lines not on this page · raw message

Signature · 1 line
2.47.0
author maintainer
#3 lore raw

PATCH 3/4 verification/dot2k: More robust template variables

In reply to Gabriele Monaco

This patch switches the variables in the template that are automatically filled by the python script from the VARIABLE notation to the %%VARIABLE%% one.
This makes the pattern substitution more robust.

---
 tools/verification/dot2/dot2k.py              | 14 +++---
 .../verification/dot2/dot2k_templates/main.c  | 50 +++++++++----------
 2 files changed, 32 insertions(+), 32 deletions(-)
tools/verification/dot2/dot2k.py +7 -7
index 15d6f7048f8d..c88b3c011706 100644
@@ -107,13 +107,13 @@ class dot2k(Dot2c):
         tracepoint_attach = self.fill_tracepoint_attach_probe()
         tracepoint_detach = self.fill_tracepoint_detach_helper()
 
-        main_c = main_c.replace("MONITOR_TYPE", monitor_type)
-        main_c = main_c.replace("MIN_TYPE", min_type)
-        main_c = main_c.replace("MODEL_NAME", self.name)
-        main_c = main_c.replace("NR_EVENTS", str(nr_events))
-        main_c = main_c.replace("TRACEPOINT_HANDLERS_SKEL", tracepoint_handlers)
-        main_c = main_c.replace("TRACEPOINT_ATTACH", tracepoint_attach)
-        main_c = main_c.replace("TRACEPOINT_DETACH", tracepoint_detach)
+        main_c = main_c.replace("%%MONITOR_TYPE%%", monitor_type)
+        main_c = main_c.replace("%%MIN_TYPE%%", min_type)
+        main_c = main_c.replace("%%MODEL_NAME%%", self.name)
+        main_c = main_c.replace("%%NR_EVENTS%%", str(nr_events))
+        main_c = main_c.replace("%%TRACEPOINT_HANDLERS_SKEL%%", tracepoint_handlers)
+        main_c = main_c.replace("%%TRACEPOINT_ATTACH%%", tracepoint_attach)
+        main_c = main_c.replace("%%TRACEPOINT_DETACH%%", tracepoint_detach)
 
         return main_c
 
tools/verification/dot2/dot2k_templates/main.c +25 -25

102 lines not on this page · raw message

Signature · 1 line
2.47.0
author maintainer
#4 lore raw

PATCH 4/4 verification/dot2k: Add support for name and description options

In reply to Gabriele Monaco

The dot2k command allows specifying a model name with -n and a description with -D, however those are not used in practice. This patch allows to specify a custom model name (by default the name of the dot file without extension) and a description which overrides the one in the C file.

---
 tools/verification/dot2/automata.py            | 4 ++--
 tools/verification/dot2/dot2c.py               | 4 ++--
 tools/verification/dot2/dot2k                  | 6 +-----
 tools/verification/dot2/dot2k.py               | 6 ++++--
 tools/verification/dot2/dot2k_templates/main.c | 2 +-
 5 files changed, 10 insertions(+), 12 deletions(-)
tools/verification/dot2/automata.py +2 -2
index bdeb98baa8b0..2c4639a73474 100644
@@ -19,9 +19,9 @@ class Automata:
 
     invalid_state_str = "INVALID_STATE"
 
-    def __init__(self, file_path):
+    def __init__(self, file_path, model_name=None):
         self.__dot_path = file_path
-        self.name = self.__get_model_name()
+        self.name = model_name if model_name is not None else self.__get_model_name()
         self.__dot_lines = self.__open_dot()
         self.states, self.initial_state, self.final_states = self.__get_state_variables()
         self.events = self.__get_event_variables()
tools/verification/dot2/dot2c.py +2 -2
index 87d8a1e1470c..fa2816ac7b61 100644
@@ -22,8 +22,8 @@ class Dot2c(Automata):
     struct_automaton_def = "automaton"
     var_automaton_def = "aut"
 
-    def __init__(self, file_path):
-        super().__init__(file_path)
+    def __init__(self, file_path, model_name=None):
+        super().__init__(file_path, model_name)
         self.line_length = 100
 
     def __buff_to_string(self, buff):
tools/verification/dot2/dot2k +1 -5
index d4d7e52d549e..2d580f26d7c0 100644
@@ -25,16 +25,12 @@ if __name__ == '__main__':
 
     print("Opening and parsing the dot file %s" % params.dot_file)
     try:
-        monitor=dot2k(params.dot_file, params.monitor_type)
+        monitor=dot2k(params.dot_file, params.monitor_type, params.model_name, params.description)
     except Exception as e:
         print('Error: '+ str(e))
         print("Sorry : :-(")
         sys.exit(1)
 
-    # easier than using argparse action.
-    if params.model_name != None:
-        print(params.model_name)
-
     print("Writing the monitor into the directory %s" % monitor.name)
     monitor.print_files()
     print("Almost done, checklist")
tools/verification/dot2/dot2k.py +4 -2
index c88b3c011706..f5f829a03f84 100644
@@ -17,8 +17,8 @@ class dot2k(Dot2c):
     monitor_templates_dir = "dot2/dot2k_templates/"
     monitor_type = "per_cpu"
 
-    def __init__(self, file_path, MonitorType):
-        super().__init__(file_path)
+    def __init__(self, file_path, MonitorType, model_name=None, description=None):
+        super().__init__(file_path, model_name)
 
         self.monitor_type = self.monitor_types.get(MonitorType)
         if self.monitor_type is None:
@@ -28,6 +28,7 @@ class dot2k(Dot2c):
         self.__fill_rv_templates_dir()
         self.main_c = self.__open_file(self.monitor_templates_dir + "main.c")
         self.enum_suffix = "_%s" % self.name
+        self.description = description if description is not None else self.name
 
     def __fill_rv_templates_dir(self):
 
@@ -114,6 +115,7 @@ class dot2k(Dot2c):
         main_c = main_c.replace("%%TRACEPOINT_HANDLERS_SKEL%%", tracepoint_handlers)
         main_c = main_c.replace("%%TRACEPOINT_ATTACH%%", tracepoint_attach)
         main_c = main_c.replace("%%TRACEPOINT_DETACH%%", tracepoint_detach)
+        main_c = main_c.replace("%%DESCRIPTION%%", self.description)
 
         return main_c
 
tools/verification/dot2/dot2k_templates/main.c +1 -1
index 4a05fef7f3c7..0987c5e56ec8 100644
@@ -88,4 +88,4 @@ module_exit(unregister_%%MODEL_NAME%%);
 
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("dot2k: auto-generated");
-MODULE_DESCRIPTION("%%MODEL_NAME%%");
+MODULE_DESCRIPTION("%%DESCRIPTION%%");
Signature · 1 line
2.47.0