win-pvdrivers

diff xenpci/xenpci_fdo.c @ 924:35ec9d4ebf94

Updates to manage ballooning a bit better
author James Harper <james.harper@bendigoit.com.au>
date Sat May 21 19:38:53 2011 +1000 (2011-05-21)
parents 1ee7940af105
children 8f483a2b2991
line diff
     1.1 --- a/xenpci/xenpci_fdo.c	Sat May 21 19:36:29 2011 +1000
     1.2 +++ b/xenpci/xenpci_fdo.c	Sat May 21 19:38:53 2011 +1000
     1.3 @@ -230,7 +230,7 @@ static VOID
     1.4  XenPci_BalloonThreadProc(PVOID StartContext)
     1.5  {
     1.6    PXENPCI_DEVICE_DATA xpdd = StartContext;
     1.7 -  ULONG new_target = xpdd->current_memory;
     1.8 +  ULONG new_target_kb = xpdd->current_memory_kb;
     1.9    LARGE_INTEGER timeout;
    1.10    PLARGE_INTEGER ptimeout;
    1.11    PMDL head;
    1.12 @@ -267,7 +267,7 @@ XenPci_BalloonThreadProc(PVOID StartCont
    1.13    for(;;)
    1.14    {
    1.15      /* back off exponentially if we have adjustments to make, or wait for event if we don't */
    1.16 -    if (xpdd->current_memory != new_target)
    1.17 +    if (xpdd->current_memory_kb != new_target_kb)
    1.18      {
    1.19        timeout.QuadPart = WDF_REL_TIMEOUT_IN_MS(timeout_ms);
    1.20        ptimeout = &timeout;
    1.21 @@ -283,22 +283,22 @@ XenPci_BalloonThreadProc(PVOID StartCont
    1.22      KeWaitForSingleObject(&xpdd->balloon_event, Executive, KernelMode, FALSE, ptimeout);
    1.23      if (xpdd->balloon_shutdown)
    1.24        PsTerminateSystemThread(0);
    1.25 -    KdPrint((__DRIVER_NAME "     Got balloon event, current = %d, target = %d\n", xpdd->current_memory, xpdd->target_memory));
    1.26 +    KdPrint((__DRIVER_NAME "     Got balloon event, current = %d, target = %d\n", xpdd->current_memory_kb, xpdd->target_memory_kb));
    1.27      /* not really worried about races here, but cache target so we only read it once */
    1.28 -    new_target = xpdd->target_memory;
    1.29 +    new_target_kb = xpdd->target_memory_kb;
    1.30      // perform some sanity checks on target_memory
    1.31      // make sure target <= initial
    1.32      // make sure target > some % of initial
    1.33      
    1.34 -    if (xpdd->current_memory == new_target)
    1.35 +    if (xpdd->current_memory_kb == new_target_kb)
    1.36      {
    1.37        KdPrint((__DRIVER_NAME "     No change to memory\n"));
    1.38        continue;
    1.39      }
    1.40 -    else if (xpdd->current_memory < new_target)
    1.41 +    else if (xpdd->current_memory_kb < new_target_kb)
    1.42      {
    1.43 -      KdPrint((__DRIVER_NAME "     Trying to take %d MB from Xen\n", new_target - xpdd->current_memory));
    1.44 -      while ((mdl = head) != NULL && xpdd->current_memory < new_target)
    1.45 +      KdPrint((__DRIVER_NAME "     Trying to take %d MB from Xen\n", new_target_kb - xpdd->current_memory_kb));
    1.46 +      while ((mdl = head) != NULL && xpdd->current_memory_kb < new_target_kb)
    1.47        {
    1.48          pfn_count = ADDRESS_AND_SIZE_TO_SPAN_PAGES(MmGetMdlVirtualAddress(mdl), MmGetMdlByteCount(mdl));
    1.49          pfns = ExAllocatePoolWithTag(NonPagedPool, pfn_count * sizeof(xen_pfn_t), XENPCI_POOL_TAG);
    1.50 @@ -332,13 +332,13 @@ XenPci_BalloonThreadProc(PVOID StartCont
    1.51          mdl->Next = NULL;        
    1.52          MmFreePagesFromMdl(mdl);
    1.53          ExFreePool(mdl);
    1.54 -        xpdd->current_memory++;
    1.55 +        xpdd->current_memory_kb += BALLOON_UNITS_KB;
    1.56        }
    1.57      }
    1.58      else
    1.59      {
    1.60 -      KdPrint((__DRIVER_NAME "     Trying to give %d MB to Xen\n", xpdd->current_memory - new_target));
    1.61 -      while (xpdd->current_memory > new_target)
    1.62 +      KdPrint((__DRIVER_NAME "     Trying to give %d MB to Xen\n", xpdd->current_memory_kb - new_target_kb));
    1.63 +      while (xpdd->current_memory_kb > new_target_kb)
    1.64        {
    1.65          PHYSICAL_ADDRESS alloc_low;
    1.66          PHYSICAL_ADDRESS alloc_high;
    1.67 @@ -358,9 +358,9 @@ XenPci_BalloonThreadProc(PVOID StartCont
    1.68  
    1.69          #if (NTDDI_VERSION >= NTDDI_WS03SP1)
    1.70          /* our contract says that we must zero pages before returning to xen, so we can't use MM_DONT_ZERO_ALLOCATION */
    1.71 -        mdl = MmAllocatePagesForMdlEx(alloc_low, alloc_high, alloc_skip, BALLOON_UNITS, MmCached, 0);
    1.72 +        mdl = MmAllocatePagesForMdlEx(alloc_low, alloc_high, alloc_skip, BALLOON_UNITS_KB * 1024, MmCached, 0);
    1.73          #else
    1.74 -        mdl = MmAllocatePagesForMdl(alloc_low, alloc_high, alloc_skip, BALLOON_UNITS);
    1.75 +        mdl = MmAllocatePagesForMdl(alloc_low, alloc_high, alloc_skip, BALLOON_UNITS_KB * 1024);
    1.76          #endif
    1.77          if (!mdl)
    1.78          {
    1.79 @@ -404,11 +404,11 @@ XenPci_BalloonThreadProc(PVOID StartCont
    1.80            {
    1.81              head = mdl;
    1.82            }
    1.83 -          xpdd->current_memory--;
    1.84 +          xpdd->current_memory_kb -= BALLOON_UNITS_KB;
    1.85          }
    1.86        }
    1.87      }
    1.88 -    KdPrint((__DRIVER_NAME "     Memory = %d, Balloon Target = %d\n", xpdd->current_memory, new_target));
    1.89 +    KdPrint((__DRIVER_NAME "     Memory = %d, Balloon Target = %d\n", xpdd->current_memory_kb, new_target_kb));
    1.90    }
    1.91    //FUNCTION_EXIT();
    1.92  }
    1.93 @@ -439,9 +439,9 @@ XenPci_BalloonHandler(char *Path, PVOID 
    1.94    }
    1.95  
    1.96    if (atoi(value) > 0)
    1.97 -    xpdd->target_memory = atoi(value) >> 10; /* convert to MB */
    1.98 +    xpdd->target_memory_kb = atoi(value);
    1.99  
   1.100 -  KdPrint((__DRIVER_NAME "     target memory value = %d (%s)\n", xpdd->target_memory, value));
   1.101 +  KdPrint((__DRIVER_NAME "     target memory value = %d (%s)\n", xpdd->target_memory_kb, value));
   1.102  
   1.103    XenBus_EndTransaction(xpdd, xbt, 0, &retry);
   1.104  
   1.105 @@ -844,16 +844,16 @@ XenPci_EvtDeviceD0EntryPostInterruptsEna
   1.106  
   1.107      response = XenBus_AddWatch(xpdd, XBT_NIL, "device", XenPci_DeviceWatchHandler, xpdd);
   1.108  
   1.109 -    if (!xpdd->initial_memory)
   1.110 +    if (!xpdd->initial_memory_kb)
   1.111      {
   1.112        XenBus_Read(xpdd, XBT_NIL, BALLOON_PATH, &value);
   1.113        if (atoi(value) > 0)
   1.114        {
   1.115 -        xpdd->initial_memory = atoi(value) >> 10; /* convert to MB */
   1.116 -        xpdd->current_memory = xpdd->initial_memory;
   1.117 -        xpdd->target_memory = xpdd->initial_memory;
   1.118 +        xpdd->initial_memory_kb = atoi(value);
   1.119 +        xpdd->current_memory_kb = xpdd->initial_memory_kb;
   1.120 +        xpdd->target_memory_kb = xpdd->initial_memory_kb;
   1.121        }
   1.122 -      KdPrint((__DRIVER_NAME "     Initial Memory Value = %d (%s)\n", xpdd->initial_memory, value));
   1.123 +      KdPrint((__DRIVER_NAME "     Initial Memory Value = %d (%s)\n", xpdd->initial_memory_kb, value));
   1.124        KeInitializeEvent(&xpdd->balloon_event, SynchronizationEvent, FALSE);
   1.125        xpdd->balloon_shutdown = FALSE;
   1.126        status = PsCreateSystemThread(&thread_handle, THREAD_ALL_ACCESS, NULL, NULL, NULL, XenPci_BalloonThreadProc, xpdd);