All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/3] auto-t: hwsim.py: turn Hwsim into singleton (per-namespace)
@ 2021-08-26 20:33 James Prestwood
  2021-08-26 20:33 ` [PATCH 2/3] auto-t: hostapd.py: properly implement singleton (per-config) James Prestwood
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: James Prestwood @ 2021-08-26 20:33 UTC (permalink / raw)
  To: iwd

[-- Attachment #1: Type: text/plain, Size: 1251 bytes --]

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(+)

diff --git a/autotests/util/hwsim.py b/autotests/util/hwsim.py
index db106dcb..cc62495c 100755
--- a/autotests/util/hwsim.py
+++ b/autotests/util/hwsim.py
@@ -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(
-- 
2.31.1

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/3] auto-t: hostapd.py: properly implement singleton (per-config)
  2021-08-26 20:33 [PATCH 1/3] auto-t: hwsim.py: turn Hwsim into singleton (per-namespace) James Prestwood
@ 2021-08-26 20:33 ` James Prestwood
  2021-08-26 20:33 ` [PATCH 3/3] test-runner: only import Hwsim/HostapdCLI once James Prestwood
  2021-08-28  1:07 ` [PATCH 1/3] auto-t: hwsim.py: turn Hwsim into singleton (per-namespace) Denis Kenzior
  2 siblings, 0 replies; 4+ messages in thread
From: James Prestwood @ 2021-08-26 20:33 UTC (permalink / raw)
  To: iwd

[-- Attachment #1: Type: text/plain, Size: 2300 bytes --]

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(-)

diff --git a/autotests/util/hostapd.py b/autotests/util/hostapd.py
index 10104862..07d27f1b 100644
--- a/autotests/util/hostapd.py
+++ b/autotests/util/hostapd.py
@@ -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()
diff --git a/tools/test-runner b/tools/test-runner
index 8b244e99..b7a283d1 100755
--- a/tools/test-runner
+++ b/tools/test-runner
@@ -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
-- 
2.31.1

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 3/3] test-runner: only import Hwsim/HostapdCLI once
  2021-08-26 20:33 [PATCH 1/3] auto-t: hwsim.py: turn Hwsim into singleton (per-namespace) James Prestwood
  2021-08-26 20:33 ` [PATCH 2/3] auto-t: hostapd.py: properly implement singleton (per-config) James Prestwood
@ 2021-08-26 20:33 ` James Prestwood
  2021-08-28  1:07 ` [PATCH 1/3] auto-t: hwsim.py: turn Hwsim into singleton (per-namespace) Denis Kenzior
  2 siblings, 0 replies; 4+ messages in thread
From: James Prestwood @ 2021-08-26 20:33 UTC (permalink / raw)
  To: iwd

[-- Attachment #1: Type: text/plain, Size: 2150 bytes --]

These 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(-)

diff --git a/tools/test-runner b/tools/test-runner
index b7a283d1..513af2ab 100755
--- a/tools/test-runner
+++ b/tools/test-runner
@@ -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
-- 
2.31.1

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/3] auto-t: hwsim.py: turn Hwsim into singleton (per-namespace)
  2021-08-26 20:33 [PATCH 1/3] auto-t: hwsim.py: turn Hwsim into singleton (per-namespace) James Prestwood
  2021-08-26 20:33 ` [PATCH 2/3] auto-t: hostapd.py: properly implement singleton (per-config) James Prestwood
  2021-08-26 20:33 ` [PATCH 3/3] test-runner: only import Hwsim/HostapdCLI once James Prestwood
@ 2021-08-28  1:07 ` Denis Kenzior
  2 siblings, 0 replies; 4+ messages in thread
From: Denis Kenzior @ 2021-08-28  1:07 UTC (permalink / raw)
  To: iwd

[-- Attachment #1: Type: text/plain, Size: 334 bytes --]

Hi James,

On 8/26/21 3:33 PM, James Prestwood wrote:
> 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

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-08-28  1:07 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-26 20:33 [PATCH 1/3] auto-t: hwsim.py: turn Hwsim into singleton (per-namespace) James Prestwood
2021-08-26 20:33 ` [PATCH 2/3] auto-t: hostapd.py: properly implement singleton (per-config) James Prestwood
2021-08-26 20:33 ` [PATCH 3/3] test-runner: only import Hwsim/HostapdCLI once James Prestwood
2021-08-28  1:07 ` [PATCH 1/3] auto-t: hwsim.py: turn Hwsim into singleton (per-namespace) Denis Kenzior

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.