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

[win-pv-devel] [PATCH 1/2] Pre-calculate slab object size



There is no need to re-calculate the slab object size for each new slab.
It can be calculated once, when the cache is created.

Signed-off-by: Paul Durrant <paul.durrant@xxxxxxxxxx>
---
 src/xenbus/cache.c | 31 ++++++++++++++++++-------------
 1 file changed, 18 insertions(+), 13 deletions(-)

diff --git a/src/xenbus/cache.c b/src/xenbus/cache.c
index da1597a..d9aab76 100644
--- a/src/xenbus/cache.c
+++ b/src/xenbus/cache.c
@@ -57,8 +57,8 @@ typedef struct _XENBUS_CACHE_MAGAZINE {
 
 typedef struct _XENBUS_CACHE_SLAB {
     ULONG           Magic;
+    PXENBUS_CACHE   Cache;
     LIST_ENTRY      ListEntry;
-    ULONG           Size;
     ULONG           Count;
     ULONG           Allocated;
     UCHAR           Buffer[1];
@@ -197,7 +197,6 @@ CacheCreateSlab(
     IN  PXENBUS_CACHE   Cache
     )
 {
-    ULONG               Size;
     PXENBUS_CACHE_SLAB  Slab;
     ULONG               NumberOfBytes;
     LARGE_INTEGER       LowAddress;
@@ -206,11 +205,8 @@ CacheCreateSlab(
     LONG                Index;
     NTSTATUS            status;
 
-    Size = __max(Cache->Size, MINIMUM_OBJECT_SIZE);
-    Size = P2ROUNDUP(Size, sizeof (ULONG_PTR));
-
     NumberOfBytes = P2ROUNDUP(FIELD_OFFSET(XENBUS_CACHE_SLAB, Buffer) +
-                              Size,
+                              Cache->Size,
                               PAGE_SIZE);
 
     LowAddress.QuadPart = 0ull;
@@ -231,14 +227,14 @@ CacheCreateSlab(
     RtlZeroMemory(Slab, NumberOfBytes);
 
     Slab->Magic = XENBUS_CACHE_SLAB_MAGIC;
-    Slab->Size = Size;
+    Slab->Cache = Cache;
     Slab->Count = (NumberOfBytes -
                    FIELD_OFFSET(XENBUS_CACHE_SLAB, Buffer)) /
-                  Size;
+                  Cache->Size;
     ASSERT(Slab->Count != 0);
 
     for (Index = 0; Index < (LONG)Slab->Count; Index++) {
-        PVOID Object = (PVOID)&Slab->Buffer[Index * Size];
+        PVOID Object = (PVOID)&Slab->Buffer[Index * Cache->Size];
 
         status = __CacheCtor(Cache, Object);
         if (!NT_SUCCESS(status))
@@ -252,7 +248,7 @@ CacheCreateSlab(
 
 fail2:
     while (--Index >= 0) {
-        PVOID Object = (PVOID)&Slab->Buffer[Index * Size];
+        PVOID Object = (PVOID)&Slab->Buffer[Index * Cache->Size];
 
         __CacheDtor(Cache, Object);
     }
@@ -284,7 +280,7 @@ CacheDestroySlab(
 
     Index = Slab->Count;
     while (--Index >= 0) {
-        PVOID Object = (PVOID)&Slab->Buffer[Index * Slab->Size];
+        PVOID Object = (PVOID)&Slab->Buffer[Index * Cache->Size];
 
         __CacheDtor(Cache, Object);
     }
@@ -300,10 +296,13 @@ CacheGetObjectFromSlab(
     IN  PXENBUS_CACHE_SLAB  Slab
     )
 {
+    PXENBUS_CACHE           Cache;
     ULONG                   Free;
     ULONG                   Index;
     ULONG                   Set;
 
+    Cache = Slab->Cache;
+
     Free = ~Slab->Allocated;
     if (!_BitScanForward(&Index, Free) || Index >= Slab->Count)
         return NULL;
@@ -311,7 +310,7 @@ CacheGetObjectFromSlab(
     Set = InterlockedBitTestAndSet((LONG *)&Slab->Allocated, Index);
     ASSERT(!Set);
 
-    return (PVOID)&Slab->Buffer[Index * Slab->Size];
+    return (PVOID)&Slab->Buffer[Index * Cache->Size];
 }
 
 // May be called with or without lock held
@@ -321,9 +320,12 @@ CachePutObjectToSlab(
     IN  PVOID               Object
     )
 {
+    PXENBUS_CACHE           Cache;
     ULONG                   Index;
 
-    Index = (ULONG)((PUCHAR)Object - &Slab->Buffer[0]) / Slab->Size;
+    Cache = Slab->Cache;
+
+    Index = (ULONG)((PUCHAR)Object - &Slab->Buffer[0]) / Cache->Size;
     BUG_ON(Index >= Slab->Count);
 
     (VOID) InterlockedBitTestAndReset((LONG *)&Slab->Allocated, Index);
@@ -559,6 +561,9 @@ CacheCreate(
     if (!NT_SUCCESS(status))
         goto fail2;
 
+    Size = __max(Size, MINIMUM_OBJECT_SIZE);
+    Size = P2ROUNDUP(Size, sizeof (ULONG_PTR));
+
     (*Cache)->Size = Size;
     (*Cache)->Reservation = Reservation;
     (*Cache)->Ctor = Ctor;
-- 
2.5.3


_______________________________________________
win-pv-devel mailing list
win-pv-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/win-pv-devel

 


Rackspace

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