You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2020/11/18 21:26:54 UTC

[GitHub] [incubator-tvm] areusch commented on a change in pull request #6930: [µTVM] Fix problems with the debug flow

areusch commented on a change in pull request #6930:
URL: https://github.com/apache/incubator-tvm/pull/6930#discussion_r526431416



##########
File path: python/tvm/micro/debugger.py
##########
@@ -82,23 +73,56 @@ def _stop_all(cls):
     def __init__(self):
         super(GdbDebugger, self).__init__()
         self._is_running = False
-        self._child_alive_lock = threading.RLock()
-        self._is_child_alive = False
+        self._is_running_lock = threading.RLock()
+        self._child_exited_event = threading.Event()
+        self._signals_reset_event = threading.Event()
 
     @abc.abstractmethod
     def popen_kwargs(self):
         raise NotImplementedError()
 
+    def _internal_stop(self):
+        if not self._is_running:
+            return
+
+        os.kill(os.getpid(), signal.SIGUSR1)
+        self._signals_reset_event.wait()
+        termios.tcsetattr(sys.stdin.fileno(), termios.TCSAFLUSH, self.old_termios)
+
+        try:
+            children = psutil.Process(self.popen.pid).children(recursive=True)
+            for c in children:
+                c.terminate()
+                _, alive = psutil.wait_procs(children, timeout=self._GRACEFUL_SHUTDOWN_TIMEOUT_SEC)
+                for a in alive:
+                    a.kill()
+        except psutil.NoSuchProcess:
+            pass
+        finally:
+            self.__class__._STARTED_INSTANCE = None
+            self._is_running = False
+            self._child_exited_event.set()
+
     def _wait_for_child(self):
         self.popen.wait()
-        with self._child_alive_lock:
-            self._is_child_alive = True
+        with self._is_running_lock:
+            self._internal_stop()
+
+    @classmethod
+    def _sigusr1_handler(cls, signum, stack_frame):  # pylint: disable=unused-argument
+        if cls._STARTED_INSTANCE is not None:

Review comment:
       good call, i've inverted the logic and made the check stricter.

##########
File path: python/tvm/micro/debugger.py
##########
@@ -82,23 +73,56 @@ def _stop_all(cls):
     def __init__(self):
         super(GdbDebugger, self).__init__()
         self._is_running = False
-        self._child_alive_lock = threading.RLock()
-        self._is_child_alive = False
+        self._is_running_lock = threading.RLock()
+        self._child_exited_event = threading.Event()
+        self._signals_reset_event = threading.Event()
 
     @abc.abstractmethod
     def popen_kwargs(self):
         raise NotImplementedError()
 
+    def _internal_stop(self):
+        if not self._is_running:
+            return
+
+        os.kill(os.getpid(), signal.SIGUSR1)
+        self._signals_reset_event.wait()
+        termios.tcsetattr(sys.stdin.fileno(), termios.TCSAFLUSH, self.old_termios)
+
+        try:
+            children = psutil.Process(self.popen.pid).children(recursive=True)
+            for c in children:
+                c.terminate()
+                _, alive = psutil.wait_procs(children, timeout=self._GRACEFUL_SHUTDOWN_TIMEOUT_SEC)
+                for a in alive:
+                    a.kill()
+        except psutil.NoSuchProcess:
+            pass
+        finally:
+            self.__class__._STARTED_INSTANCE = None
+            self._is_running = False
+            self._child_exited_event.set()
+
     def _wait_for_child(self):
         self.popen.wait()
-        with self._child_alive_lock:
-            self._is_child_alive = True
+        with self._is_running_lock:
+            self._internal_stop()
+
+    @classmethod
+    def _sigusr1_handler(cls, signum, stack_frame):  # pylint: disable=unused-argument
+        if cls._STARTED_INSTANCE is not None:
+            signal.signal(signal.SIGINT, cls._STARTED_INSTANCE.old_sigint_handler)
+            signal.signal(signal.SIGUSR1, cls._STARTED_INSTANCE.old_sigusr1_handler)
+            cls._STARTED_INSTANCE._signals_reset_event.set()
+            return
+
+        raise Exception()

Review comment:
       done

##########
File path: python/tvm/micro/debugger.py
##########
@@ -109,46 +133,32 @@ def _sigint_handler(cls, signum, stack_frame):  # pylint: disable=unused-argumen
         raise Exception()

Review comment:
       done




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org