Skip to content

Commit

Permalink
fm_agent: Wait for fastmodel subprocess to terminate when resetting o…
Browse files Browse the repository at this point in the history
…r terminating it

Releasing the model via IRIS does not reliably kill the subprocess

This gives the FVP up to 3 seconds to terminate gracefully, then force kills it

Signed-off-by: Chris Swinchatt <christopher.swinchatt@arm.com>
  • Loading branch information
chrisswinchatt-arm committed Sep 2, 2022
1 parent 8cf0a18 commit bac4926
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions fm_agent/fm_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
limitations under the License.
"""

import atexit
import multiprocessing
import sys
import os
from subprocess import Popen
import time
Expand Down Expand Up @@ -86,10 +86,12 @@ def __init__(self, model_name=None, model_config=None, logger=None, enable_gdbse
@param model_config is the config file to the fast model
"""

atexit.register(self.__del__)

self.fastmodel_name = model_name
self.config_name = model_config
self.enable_gdbserver = enable_gdbserver
self.subprocess = None
self.subprocess:Popen = None

#If logging not provided, use default log
if logger:
Expand All @@ -111,9 +113,10 @@ def __init__(self, model_name=None, model_config=None, logger=None, enable_gdbse
pass

def __del__(self):
if isinstance(self.subprocess, Popen):
self.subprocess.terminate()
self.subprocess.wait()
self.__closeConnection()
self.__releaseModel()
self.__terminateSubprocess()
atexit.unregister(self.__del__)

def setup_simulator(self, model_name, model_config):
""" setup the simulator, this is crucial before you can start a simulator.
Expand Down Expand Up @@ -257,8 +260,8 @@ def reset_simulator(self):
if self.is_simulator_alive():
self.logger.prn_wrn("STOP and RESTART FastModel")
self.__closeConnection()
self.model.release(shutdown=True)
time.sleep(1)
self.__releaseModel()
self.__terminateSubprocess()

self.__spawn_simulator()

Expand Down Expand Up @@ -336,8 +339,6 @@ def __closeConnection(self):
self.socket.close()
self.logger.prn_inf("Closing terminal socket connection")
self.socket = None
else:
self.logger.prn_inf("Terminal socket connection already closed")

def __run_to_breakpoint(self):
try:
Expand Down Expand Up @@ -425,12 +426,26 @@ def shutdown_simulator(self):
self.__CodeCoverage()
self.logger.prn_inf("Fast-Model agent shutting down model")
self.__closeConnection()
self.model.release(shutdown=True)
self.model=None
time.sleep(1)
self.__releaseModel()
self.__terminateSubprocess()
else:
self.logger.prn_inf("Model already shutdown")

def __releaseModel(self):
if self.model:
self.model.release(shutdown=True)
del self.model
self.model = None

def __terminateSubprocess(self):
if self.subprocess:
self.subprocess.terminate()
if self.subprocess.wait(3) is None:
self.subprocess.kill()
self.subprocess.wait()
del self.subprocess
self.subprocess = None

def list_avaliable_models(self):
""" return a dictionary of models and configs """
return self.configuration.get_all_configs()
Expand Down

0 comments on commit bac4926

Please sign in to comment.