From a53900b5bd9b1241dd4f8b2a1c9cf91d136955e1 Mon Sep 17 00:00:00 2001 From: Lukas Rothenberger Date: Wed, 25 Oct 2023 16:37:21 +0200 Subject: [PATCH 1/2] feat: add pattern id to each pattern. Threadsafe by relying on blocking i/o --- .../pattern_detectors/PatternInfo.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/discopop_explorer/pattern_detectors/PatternInfo.py b/discopop_explorer/pattern_detectors/PatternInfo.py index 126c7537a..2701885da 100644 --- a/discopop_explorer/pattern_detectors/PatternInfo.py +++ b/discopop_explorer/pattern_detectors/PatternInfo.py @@ -6,6 +6,7 @@ # the 3-Clause BSD License. See the LICENSE file in the package base # directory for details. import json +import os from typing import Optional from ..PETGraphX import LoopNode, Node, NodeID, LineID, PETGraphX @@ -15,6 +16,7 @@ class PatternInfo(object): """Base class for pattern detection info""" + pattern_id: int _node: Node node_id: NodeID start_line: LineID @@ -31,6 +33,19 @@ def __init__(self, node: Node): """ :param node: node, where pipeline was detected """ + # use blocking file i/o to synchronize threads + with open(os.path.join(os.getcwd(), "next_free_pattern_id.txt"), "r+") as f: + lines = f.readlines() + f.truncate(0) + f.seek(0) + if len(lines) == 0: + self.pattern_id = 0 + f.write(str(0)) + else: + for line in lines: + line = line.replace("\n", "").replace("\x00", "") + self.pattern_id = int(line) + f.write(str(self.pattern_id + 1)) self._node = node self.node_id = node.id self.start_line = node.start_position() From 05bd2f0fa6c80d8224296e98f8cb3211d3bd8570 Mon Sep 17 00:00:00 2001 From: Lukas Rothenberger Date: Wed, 25 Oct 2023 16:44:05 +0200 Subject: [PATCH 2/2] fix: initialize folder structure --- discopop_explorer/discopop_explorer.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/discopop_explorer/discopop_explorer.py b/discopop_explorer/discopop_explorer.py index 557512265..991e9c401 100644 --- a/discopop_explorer/discopop_explorer.py +++ b/discopop_explorer/discopop_explorer.py @@ -144,6 +144,10 @@ def run(arguments: ExplorerArguments): # create explorer directory if not already present if not os.path.exists(os.path.join(arguments.project_path, "explorer")): os.mkdir(os.path.join(arguments.project_path, "explorer")) + # create file to store next free pattern ids if not already present + if not os.path.exists("next_free_pattern_id.txt"): + with open("next_free_pattern_id.txt", "w") as f: + f.write(str(0)) if arguments.enable_profiling_dump_file is not None: profile = cProfile.Profile()