auto-t: hwsim.py: turn Hwsim into singleton (per-namespace)
PATCH 1/3 auto-t: hwsim.py: turn Hwsim into singleton (per-namespace)
This prevents reallocation of new Hwsim classes on each call if one already exists. This is a bit more efficient on memory.
--- autotests/util/hwsim.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
autotests/util/hwsim.py +19 -0
@@ -3,6 +3,7 @@ import dbus import sys import collections +from weakref import WeakValueDictionary from abc import ABCMeta, abstractmethod from enum import Enum
@@ -297,7 +298,25 @@ class RadioList(collections.Mapping): return obj class Hwsim(iwd.AsyncOpAbstract): + _instances = WeakValueDictionary() + + def __new__(cls, namespace=ctx): + name = namespace.name + + if name not in cls._instances.keys(): + obj = object.__new__(cls) + obj._initialized = False + + cls._instances[name] = obj + + return cls._instances[name] + def __init__(self, namespace=ctx): + if self._initialized: + return + + self._initialized = True + self._bus = namespace.get_bus() self._rule_manager_if = dbus.Interface(
Signature · 1 line
2.31.1
PATCH 2/3 auto-t: hostapd.py: properly implement singleton (per-config)
This shouldn't change any functionality but it is much more convenient in the cleanup path i.e. nothing special needs to be done.
--- autotests/util/hostapd.py | 16 ++++++---------- tools/test-runner | 7 ------- 2 files changed, 6 insertions(+), 17 deletions(-)
autotests/util/hostapd.py +6 -10
@@ -5,6 +5,7 @@ import socket import select import time from gi.repository import GLib +from weakref import WeakValueDictionary from config import ctx chan_freq_map = [
@@ -29,7 +30,7 @@ ctrl_count = 0 mainloop = GLib.MainLoop() class HostapdCLI(object): - _instances = {} + _instances = WeakValueDictionary() def __new__(cls, config=None, *args, **kwargs): hapd = ctx.hostapd[config]
@@ -38,8 +39,10 @@ class HostapdCLI(object): config = hapd.config if not config in cls._instances.keys(): - cls._instances[config] = object.__new__(cls, *args, **kwargs) - cls._instances[config]._initialized = False + obj = object.__new__(cls, *args, **kwargs) + obj._initialized = False + + cls._instances[config] = obj return cls._instances[config]
@@ -123,13 +126,6 @@ class HostapdCLI(object): except: pass - HostapdCLI._instances[self.config] = None - - # Check if this is the final instance - destroy = len([hapd for hapd in HostapdCLI._instances.values() if hapd is not None]) == 0 - if destroy: - HostapdCLI._instances = {} - def set_value(self, key, value): cmd = self.cmdline + ['set', key, value] ctx.start_process(cmd).wait()
tools/test-runner +0 -7
@@ -541,13 +541,6 @@ class Hostapd: except: print("Failed to remove %s" % self.global_ctrl_iface) - for hapd in self.instances: - if not hapd.cli: - continue - - hapd.cli._instances[hapd.config] = None - hapd.cli = None - self.instances = None # Hostapd may have already been stopped
Signature · 1 line
2.31.1
PATCH 3/3 test-runner: only import Hwsim/HostapdCLI once
In reply to James PrestwoodThese modules only needed to be imported a single time for the entire run of tests. This is significantly cheaper in terms of memory and should prevent random OOM exceptions.
--- tools/test-runner | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-)
tools/test-runner +16 -7
@@ -377,17 +377,20 @@ class VirtualRadio(Radio): TODO: Using D-Bus to create and destroy radios is more desireable than the command line. ''' - def __init__(self, name, config=None): + + def __init__(self, name, cfg=None): + global config + self.disable_cipher = None self.disable_iftype = None - hwsim = importlib.import_module('hwsim').Hwsim() + self.hwsim = config.hwsim.Hwsim() - if config: - self.disable_iftype = config.get('iftype_disable', None) - self.disable_cipher = config.get('cipher_disable', None) + if cfg: + self.disable_iftype = cfg.get('iftype_disable', None) + self.disable_cipher = cfg.get('cipher_disable', None) - self._radio = hwsim.radios.create(name, p2p_device=True, + self._radio = self.hwsim.radios.create(name, p2p_device=True, iftype_disable=self.disable_iftype, cipher_disable=self.disable_cipher)
@@ -499,8 +502,10 @@ class Hostapd: self.process.wait_for_socket(hapd.intf.ctrl_interface, 30) def attach_cli(self): + global config + for hapd in self.instances: - hapd.cli = importlib.import_module('hostapd').HostapdCLI(config=hapd.config) + hapd.cli = config.hostapd.HostapdCLI(config=hapd.config) def _rewrite_config(self, config): '''
@@ -1502,6 +1507,10 @@ def run_tests(): config = importlib.import_module('config') config.ctx = TestContext(args) + # Must import these after config so ctx gets set + config.hwsim = importlib.import_module('hwsim') + config.hostapd = importlib.import_module('hostapd') + if args.log: mount('logdir', args.log, '9p', 0, 'trans=virtio,version=9p2000.L,msize=10240') # Clear out any log files from other test runs
Signature · 1 line
2.31.1
Hi James,
Quoted 6 lines · James Prestwood
This prevents reallocation of new Hwsim classes on each call if one already exists. This is a bit more efficient on memory.
--- autotests/util/hwsim.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
All applied, thanks.
Regards,
-Denis