← Back to team overview

curtin-dev team mailing list archive

[Merge] ~dbungert/curtin:swap-size-avail into curtin:master

 

Dan Bungert has proposed merging ~dbungert/curtin:swap-size-avail into curtin:master.

Commit message:
swap: support avail parameter

Add an avail parameter to allow swap size projections based on expected
available space.  Add unit tests for existing and new (avail) usage.
Add docstring to `suggested_swapsize()`.



Requested reviews:
  curtin developers (curtin-dev)

For more details, see:
https://code.launchpad.net/~dbungert/curtin/+git/curtin/+merge/447668
-- 
Your team curtin developers is requested to review the proposed merge of ~dbungert/curtin:swap-size-avail into curtin:master.
diff --git a/curtin/swap.py b/curtin/swap.py
index 7acc69e..95f88fe 100644
--- a/curtin/swap.py
+++ b/curtin/swap.py
@@ -9,19 +9,37 @@ from curtin import paths
 from curtin import distro
 
 
-def suggested_swapsize(memsize=None, maxsize=None, fsys=None):
-    # make a suggestion on the size of swap for this system.
+def suggested_swapsize(memsize=None, maxsize=None, fsys=None, avail=None):
+    """Calculate a suggestion for system swap size.
+
+    :param memsize: The scaling formula looks at available memory, and chooses
+    a larger swap size if there is more memory.  If None, use the system total
+    available memory.
+
+    :param maxsize: The maximum size to suggest.  If None, a maximum will be
+    calculated by looking at avilable space on the filesystem, or 8GiB if fsys
+    is None.
+
+    :param fsys: The filesystem to check for maxsize.  Optional.
+
+    :param avail: When calculating a maxsize, fsys will be consulted to find
+    the avialable space.  Avail is a shortcut for this value, useful for sizing
+    projections when the filesystem hasn't yet been populated.  If avail and
+    fsys are both supplied, fsys takes precedence.
+    """
     if memsize is None:
         memsize = util.get_meminfo()['total']
 
     GB = 2 ** 30
     sugg_max = 8 * GB
 
-    if fsys is None and maxsize is None:
+    if fsys is None and avail is None and maxsize is None:
         # set max to 8GB default if no filesystem given
         maxsize = sugg_max
     elif fsys:
         avail = util.get_fs_use_info(fsys)[1]
+
+    if avail is not None:
         if maxsize is None:
             # set to 25% of filesystem space
             maxsize = min(int(avail / 4), sugg_max)
diff --git a/tests/unittests/test_swap.py b/tests/unittests/test_swap.py
index b41cd39..d37d519 100644
--- a/tests/unittests/test_swap.py
+++ b/tests/unittests/test_swap.py
@@ -4,6 +4,12 @@ from curtin import swap
 from curtin import util
 from .helpers import CiTestCase
 
+from parameterized import parameterized
+
+
+def gigify(val):
+    return int(val * (2 ** 30))
+
 
 class TestSwap(CiTestCase):
     def _valid_swap_contents(self):
@@ -70,3 +76,51 @@ class TestSwap(CiTestCase):
     def test_swapfile_btrfs_ok(self, mock_gtkv):
         mock_gtkv.return_value = dict(major=5)
         self.assertTrue(swap.can_use_swapfile(None, 'btrfs'))
+
+    @parameterized.expand([
+        [2, 1],
+        [2, 1.9],
+        [2, 2],
+        [2.1, 2.1],
+        [3.9, 3.9],
+        [4, 4],
+        [4, 4.1],
+        [4, 15.9],
+        [4, 16],
+        # above 16GB memsize hits suggested max
+        [8, 16.1],
+        [8, 64],
+    ])
+    def test_swapsize(self, expected, memsize):
+        expected = gigify(expected)
+        memsize = gigify(memsize)
+        self.assertEqual(expected, swap.suggested_swapsize(memsize=memsize))
+
+    @parameterized.expand([
+        [4, 16, 16],
+        [4, 16, 24],
+        [4, 16, 32],
+        [4, 16.1, 16],
+        [6, 16.1, 24],
+        [8, 16.1, 32],
+        [8, 16.1, 64],
+    ])
+    def test_swapsize_with_avail(self, expected, memsize, avail):
+        expected = gigify(expected)
+        memsize = gigify(memsize)
+        avail = gigify(avail)
+        actual = swap.suggested_swapsize(memsize=memsize, avail=avail)
+        self.assertEqual(expected, actual)
+
+    @parameterized.expand([
+        [16, 16],
+        [24, 24],
+        [32, 32],
+        [32, 64],
+    ])
+    def test_swapsize_with_larger_max(self, expected, maxsize):
+        expected = gigify(expected)
+        memsize = gigify(64)
+        maxsize = gigify(maxsize)
+        actual = swap.suggested_swapsize(memsize=memsize, maxsize=maxsize)
+        self.assertEqual(expected, actual)

Follow ups