Skip to content

Commit

Permalink
Use ProcessCollection object instead of list. Add terminate to Proces…
Browse files Browse the repository at this point in the history
…sCollection.
  • Loading branch information
azaidy committed Aug 20, 2024
1 parent 1f37f99 commit 43d395c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 35 deletions.
22 changes: 14 additions & 8 deletions switchboard/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ def __init__(self, cmdline=False, tool: str = 'verilator', trace: bool = False,

self.name = name

# keep track of processes started
self.process_collection = ProcessCollection()

if cleanup:
import atexit

Expand Down Expand Up @@ -520,10 +523,6 @@ def simulate(self, start_delay=None, run=None, intf_objs=True, plusargs=None):
if plusargs is None:
plusargs = []

# keep track of processes started

process_collection = ProcessCollection()

# create interface objects

if self.single_netlist:
Expand All @@ -538,7 +537,7 @@ def simulate(self, start_delay=None, run=None, intf_objs=True, plusargs=None):
plusargs = plusargs_processed
process = self.dut.simulate(start_delay=start_delay, run=run,
intf_objs=intf_objs, plusargs=plusargs)
process_collection.add(process)
self.process_collection.add(process)

if intf_objs:
self.intfs = self.dut.intfs
Expand Down Expand Up @@ -590,14 +589,21 @@ def simulate(self, start_delay=None, run=None, intf_objs=True, plusargs=None):
process = block.simulate(start_delay=start_delay, run=inst.name,
intf_objs=False, plusargs=inst_plusargs)

process_collection.add(process)
self.process_collection.add(process)

# start TCP bridges as needed
for tcp_kwargs in self.tcp_intfs.values():
process = start_tcp_bridge(**tcp_kwargs)
process_collection.add(process)
self.process_collection.add(process)

return self.process_collection

return process_collection
def terminate(
self,
stop_timeout=10,
use_sigint=False
):
self.process_collection.terminate(stop_timeout=stop_timeout, use_sigint=use_sigint)

def generate_inst_name(self, prefix):
if prefix not in self.inst_name_counters:
Expand Down
33 changes: 6 additions & 27 deletions switchboard/sbdut.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from .switchboard import path as sb_path
from .verilator import verilator_run
from .icarus import icarus_build_vpi, icarus_find_vpi, icarus_run
from .util import plusargs_to_args, binary_run
from .util import plusargs_to_args, binary_run, ProcessCollection
from .xyce import xyce_flags
from .ams import make_ams_spice_wrapper, make_ams_verilog_wrapper, parse_spice_subckts
from .autowrap import (normalize_clocks, normalize_interfaces, normalize_resets, normalize_tieoffs,
Expand Down Expand Up @@ -217,7 +217,9 @@ def __init__(
# initialization

self.intfs = {}
self.subprocess_list = []

# keep track of processes started
self.process_collection = ProcessCollection()

# simulator-agnostic settings

Expand Down Expand Up @@ -617,7 +619,7 @@ def simulate(
)

# Add newly created Popen object to subprocess list
self.subprocess_list.append(p)
self.process_collection.add(p)

# return a Popen object that one can wait() on

Expand All @@ -628,30 +630,7 @@ def terminate(
stop_timeout=10,
use_sigint=False
):
if not self.subprocess_list:
raise Exception('No ongoing simulation.'
'Please call simulate before trying to terminate.')

for p in self.subprocess_list:
poll = p.poll()
if poll is not None:
# process has stopped
return

if use_sigint:
try:
p.send_signal(signal.SIGINT)
p.wait(stop_timeout)
return
except: # noqa: E722
# if there is an exception for any reason, including
# Ctrl-C during the wait() call, want to make sure
# that the process is actually terminated
pass

# if we get to this point, the process is still running
# and sigint didn't work (or we didn't try it)
p.terminate()
self.process_collection.terminate(stop_timeout=stop_timeout, use_sigint=use_sigint)

def input_analog(
self,
Expand Down
32 changes: 32 additions & 0 deletions switchboard/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,35 @@ def wait(self):
process.join()
else:
raise Exception(f'Unknown process type: {type(process)}')

def terminate(
self,
stop_timeout=10,
use_sigint=False
):
if not self.processes:
return

for p in self.processes:
if isinstance(p, ProcessCollection):
p.terminate(stop_timeout=stop_timeout, use_sigint=use_sigint)
else:
poll = p.poll()
if poll is not None:
# process has stopped
return

if use_sigint:
try:
p.send_signal(signal.SIGINT)
p.wait(stop_timeout)
return
except: # noqa: E722
# if there is an exception for any reason, including
# Ctrl-C during the wait() call, want to make sure
# that the process is actually terminated
pass

# if we get to this point, the process is still running
# and sigint didn't work (or we didn't try it)
p.terminate()

0 comments on commit 43d395c

Please sign in to comment.