[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 2/3] Add Lazy slab initialization


  • To: <win-pv-devel@xxxxxxxxxxxxxxxxxxxx>
  • From: Owen Smith <owen.smith@xxxxxxxxxx>
  • Date: Mon, 19 Jul 2021 11:03:33 +0100
  • Authentication-results: esa6.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none
  • Cc: Owen Smith <owen.smith@xxxxxxxxxx>
  • Delivery-date: Mon, 19 Jul 2021 10:05:45 +0000
  • Ironport-hdrordr: A9a23:XI9twKqAForZBHexI0Tn1RAaV5oTeYIsimQD101hICG8cqSj+f xG+85rsyMc6QxhIE3I9urhBEDtex/hHNtOkOws1NSZLW7bUQmTXeJfBOLZqlWKcUDDH6xmpM NdmsBFeaTN5DNB7PoSjjPWLz9Z+qjkzJyV
  • Ironport-sdr: jN0umM1CyN7jrD51/v0e8nbYmSrjRhADJExAHVsoyDGKg9DX5+ulffRX5qEPX8do/+O85w5eCg dxow2nrovQZP3YEYWjw8Ol+k4rEQpRgyRB1wSgVRhUOd+VY10F/g5totaxwWByLl/MA2qziZ5b 2NO50MKrNn0cLpXBeeE6B9eSRfNgFnR4SxSgR8/FsHguYvnZjyPyfCdvML6SY1p9u/4CCZQgwM Ej8PTD2qVVF3Mi+ju6Uk5yjZIlN9TyX9SSbgIc1xTVoMnvd4Uwr/a+8i8h2IzypmYWJBDG+TBv p6W31vHruA+I8lJBqgYOwSmF
  • List-id: Developer list for the Windows PV Drivers subproject <win-pv-devel.lists.xenproject.org>

Only initialize 1/4 of a slab initially, unless a reservation is defined.
Allocate and initialize each slab object once this initial allocation has been
exceeded.

This is to decrease the overall resource usage when each cache object's
constructor allocates additional memory.

Signed-off-by: Owen Smith <owen.smith@xxxxxxxxxx>
---
 src/xenbus/cache.c | 27 +++++++++++++++++++++++++--
 1 file changed, 25 insertions(+), 2 deletions(-)

diff --git a/src/xenbus/cache.c b/src/xenbus/cache.c
index 247f244..d695ee3 100644
--- a/src/xenbus/cache.c
+++ b/src/xenbus/cache.c
@@ -61,6 +61,7 @@ typedef struct _XENBUS_CACHE_SLAB {
     LIST_ENTRY      ListEntry;
     USHORT          MaximumOccupancy;
     USHORT          CurrentOccupancy;
+    USHORT          CurrentAllocated;
     ULONG           *Mask;
     UCHAR           Buffer[1];
 } XENBUS_CACHE_SLAB, *PXENBUS_CACHE_SLAB;
@@ -301,6 +302,7 @@ CacheCreateSlab(
     ULONG               Size;
     LONG                Index;
     LONG                SlabCount;
+    LONG                ObjectsToAllocate;
     NTSTATUS            status;
 
     NumberOfBytes = P2ROUNDUP(FIELD_OFFSET(XENBUS_CACHE_SLAB, Buffer) +
@@ -326,6 +328,7 @@ CacheCreateSlab(
     Slab->Magic = XENBUS_CACHE_SLAB_MAGIC;
     Slab->Cache = Cache;
     Slab->MaximumOccupancy = (USHORT)Count;
+    Slab->CurrentAllocated = 0;
 
     Size = P2ROUNDUP(Count, BITS_PER_ULONG);
     Size /= 8;
@@ -334,12 +337,18 @@ CacheCreateSlab(
     if (Slab->Mask == NULL)
         goto fail3;
 
-    for (Index = 0; Index < (LONG)Slab->MaximumOccupancy; Index++) {
+    ObjectsToAllocate = (Cache->Reservation == 0) ?
+                (LONG)Count / 4 :
+                min((LONG)Cache->Reservation, (LONG)Count);
+
+    for (Index = 0; Index < ObjectsToAllocate; Index++) {
         PVOID Object = (PVOID)&Slab->Buffer[Index * Cache->Size];
 
         status = __CacheCtor(Cache, Object);
         if (!NT_SUCCESS(status))
             goto fail4;
+
+        Slab->CurrentAllocated++;
     }
 
     CacheInsertSlab(Cache, Slab);
@@ -402,7 +411,7 @@ CacheDestroySlab(
     ASSERT(Cache->Cursor != &Cache->SlabList ||
            IsListEmpty(&Cache->SlabList));
 
-    Index = Slab->MaximumOccupancy;
+    Index = Slab->CurrentAllocated;
     while (--Index >= 0) {
         PVOID Object = (PVOID)&Slab->Buffer[Index * Cache->Size];
 
@@ -493,6 +502,20 @@ CacheGetObjectFromSlab(
     if (Slab->CurrentOccupancy == Slab->MaximumOccupancy)
         return NULL;
 
+    ASSERT3U(Slab->CurrentOccupancy, <=, Slab->CurrentAllocated);
+    ASSERT3U(Slab->CurrentAllocated, <=, Slab->MaximumOccupancy);
+    if (Slab->CurrentOccupancy == Slab->CurrentAllocated) {
+        NTSTATUS            status;
+
+        Object = (PVOID)&Slab->Buffer[Slab->CurrentAllocated * Cache->Size];
+
+        status = __CacheCtor(Cache, Object);
+        if (!NT_SUCCESS(status))
+            return NULL;
+
+        Slab->CurrentAllocated++;
+    }
+
     Index = __CacheMaskScan(Slab->Mask, Slab->MaximumOccupancy);
     BUG_ON(Index >= Slab->MaximumOccupancy);
 
-- 
2.31.1.windows.1




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.