sysresmonitoring/oommonitor/src/oommemorymonitor.cpp
branchRCL_3
changeset 18 0818dd463d41
parent 1 0fdb7f6b0309
child 19 924385140d98
equal deleted inserted replaced
17:5e7d68cc22e0 18:0818dd463d41
     1 /*
     1 /*
     2 * Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies). 
     2 * Copyright (c) 2006-2010 Nokia Corporation and/or its subsidiary(-ies). 
     3 * All rights reserved.
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     4 * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     5 * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
    15 *
    15 *
    16 */
    16 */
    17 
    17 
    18 
    18 
    19 #include <hal.h>
    19 #include <hal.h>
       
    20 
    20 
    21 
    21 #include <UikonInternalPSKeys.h>
    22 #include <UikonInternalPSKeys.h>
    22 
    23 
    23 #include "oommemorymonitor.h"
    24 #include "oommemorymonitor.h"
    24 #include "oommonitorplugin.h"
    25 #include "oommonitorplugin.h"
   160     iWatchdogStatusSubscriber = new (ELeave) CSubscribeHelper(TCallBack(WatchdogStatusStatusChanged, this), iWatchdogStatusProperty);
   161     iWatchdogStatusSubscriber = new (ELeave) CSubscribeHelper(TCallBack(WatchdogStatusStatusChanged, this), iWatchdogStatusProperty);
   161     iWatchdogStatusSubscriber->Subscribe();
   162     iWatchdogStatusSubscriber->Subscribe();
   162 #endif
   163 #endif
   163     
   164     
   164     
   165     
   165     iOOMWatcher = COutOfMemoryWatcher::NewL(*this, iLowThreshold, iGoodThreshold);
   166     iOOMWatcher = COutOfMemoryWatcher::NewL(*this, iLowRamThreshold, iGoodRamThreshold, iConfig->GlobalConfig().iSwapUsageMonitored, iLowSwapThreshold, iGoodSwapThreshold);
   166     iOOMWatcher->Start();
   167     iOOMWatcher->Start();
   167 
   168 
   168     iWservEventReceiver = new(ELeave) CWservEventReceiver(*this, iWs);
   169     iWservEventReceiver = new(ELeave) CWservEventReceiver(*this, iWs);
   169     iWservEventReceiver->ConstructL();
   170     iWservEventReceiver->ConstructL();
   170     }
   171     }
   183 void CMemoryMonitor::FreeMemThresholdCrossedL()
   184 void CMemoryMonitor::FreeMemThresholdCrossedL()
   184     {
   185     {
   185     FUNC_LOG;
   186     FUNC_LOG;
   186 
   187 
   187     iActionTrigger = ERamRotation;
   188     iActionTrigger = ERamRotation;
   188     StartFreeSomeRamL(iGoodThreshold);
   189     StartFreeSomeRamL(iGoodRamThreshold, iGoodSwapThreshold);
   189     }
   190     }
   190 
   191 
   191 void CMemoryMonitor::HandleFocusedWgChangeL()
   192 void CMemoryMonitor::HandleFocusedWgChangeL()
   192     {
   193     {
   193     FUNC_LOG;
   194     FUNC_LOG;
   194 
   195 
   195     TInt oldGoodThreshold = iGoodThreshold;
   196     TInt oldGoodRamThreshold = iGoodRamThreshold;
   196     TInt oldLowThreshold = iLowThreshold;
   197     TInt oldLowRamThreshold = iLowRamThreshold;
   197     
   198     TInt oldGoodSwapThreshold = iGoodSwapThreshold;
       
   199     TInt oldLowSwapThreshold = iLowSwapThreshold;
       
   200         
   198     // Refresh the low and good memory thresholds as they may have changed due to the new foreground application
   201     // Refresh the low and good memory thresholds as they may have changed due to the new foreground application
   199     RefreshThresholds();
   202     RefreshThresholds();
   200        
   203        
   201     if ((oldGoodThreshold != iGoodThreshold)
   204     if ((oldGoodRamThreshold != iGoodRamThreshold)
   202             || (oldLowThreshold != iLowThreshold))
   205             || (oldLowRamThreshold != iLowRamThreshold)
       
   206             || (oldGoodSwapThreshold != iGoodSwapThreshold)
       
   207             || (oldLowSwapThreshold != iLowSwapThreshold))
   203         // If the thresholds have changed then update the memory watched
   208         // If the thresholds have changed then update the memory watched
   204         {
   209         {
   205         iOOMWatcher->UpdateThresholds(iLowThreshold, iGoodThreshold);
   210         iOOMWatcher->UpdateThresholds(iLowRamThreshold, iGoodRamThreshold, iLowSwapThreshold, iGoodSwapThreshold);
   206         }
   211         }
   207     
   212     
   208     // If the available memory is less than the low memory threshold then free some RAM
   213     // If the available memory is less than the low memory threshold then free some RAM
   209     User::CompressAllHeaps();
   214     User::CompressAllHeaps();
   210     TInt current = 0;
   215     TInt currentFreeRam = 0;
   211     HAL::Get( HALData::EMemoryRAMFree, current );
   216     HAL::Get( HALData::EMemoryRAMFree, currentFreeRam );
   212     
   217 	TInt currentFreeSwap = 0;
   213     if (current < iLowThreshold)
   218 	if (iConfig->GlobalConfig().iSwapUsageMonitored)
       
   219 		{
       
   220         SVMSwapInfo swapInfo;
       
   221 		UserSvr::HalFunction(EHalGroupVM, EVMHalGetSwapInfo, &swapInfo, 0);
       
   222 		currentFreeSwap = swapInfo.iSwapFree;
       
   223 		}
       
   224     
       
   225     if ((currentFreeRam < iLowRamThreshold) ||
       
   226 		(iConfig->GlobalConfig().iSwapUsageMonitored && (currentFreeSwap < iLowSwapThreshold)))
   214         {
   227         {
   215         iActionTrigger = ERamRotation;
   228         iActionTrigger = ERamRotation;
   216         StartFreeSomeRamL(iGoodThreshold);
   229         StartFreeSomeRamL(iGoodRamThreshold, iGoodSwapThreshold);
   217         }
   230         }
   218     }
   231     }
   219 
   232 
   220 void CMemoryMonitor::StartFreeSomeRamL(TInt aTargetFree)
   233 void CMemoryMonitor::StartFreeSomeRamL(TInt aFreeRamTarget, TInt aFreeSwapTarget)
   221     {
   234     {
   222     StartFreeSomeRamL(aTargetFree, KOomPriorityInfinate - 1);
   235     StartFreeSomeRamL(aFreeRamTarget, aFreeSwapTarget, KOomPriorityInfinate - 1);
   223     }
   236     }
   224 
   237 
   225 void CMemoryMonitor::StartFreeSomeRamL(TInt aTargetFree, TInt aMaxPriority) // The maximum priority of action to run
   238 void CMemoryMonitor::StartFreeSomeRamL(TInt aFreeRamTarget, TInt aFreeSwapTarget, TInt aMaxPriority) // The maximum priority of action to run
   226     {
   239     {
   227     FUNC_LOG;
   240     FUNC_LOG;
   228 
   241 
   229     TRACES2("MemoryMonitor::StartFreeSomeRamL: aTargetFree = %d, iCurrentTarget = %d", aTargetFree, iCurrentTarget);
   242     TRACES4("MemoryMonitor::StartFreeSomeRamL: aFreeRamTarget = %d, iCurrentRamTarget = %d, aFreeSwapSpaceTarget = %d, iCurrentSwapTarget = %d", aFreeRamTarget, iCurrentRamTarget, aFreeSwapTarget, iCurrentSwapTarget);
   230     
   243     
   231     // Update the target if new target is higher. If the target is lower than the current target and memory 
   244     // Update the target if new target is higher. If the target is lower than the current target and memory 
   232     // is currently being freed then we do not want to reduce the amount of memory this operation frees.
   245     // is currently being freed then we do not want to reduce the amount of memory this operation frees.
   233     if (aTargetFree > iCurrentTarget)
   246     if (aFreeRamTarget > iCurrentRamTarget)
   234         iCurrentTarget = aTargetFree;
   247         {
       
   248         iCurrentRamTarget = aFreeRamTarget;
       
   249         }
       
   250     
       
   251     if (aFreeSwapTarget > iCurrentSwapTarget)
       
   252         {
       
   253         iCurrentSwapTarget = aFreeSwapTarget;
       
   254         }
   235 
   255 
   236     // check if there is enough free memory already.
   256     // check if there is enough free memory already.
   237     TInt freeMemory;
   257     TInt freeMemory = 0;
   238     GetFreeMemory(freeMemory);
   258     GetFreeMemory(freeMemory);
   239 
   259     TInt freeSwap = 0;
   240     TRACES1("MemoryMonitor::StartFreeSomeRamL, freeMemory = %d", freeMemory);
   260     if (iConfig->GlobalConfig().iSwapUsageMonitored)
   241     
   261         {
   242     if (freeMemory >= iCurrentTarget)
   262         GetFreeSwapSpace(freeSwap);
       
   263         }
       
   264 
       
   265     TRACES2("MemoryMonitor::StartFreeSomeRamL, freeMemory = %d, freeSwap = %d", freeMemory, freeSwap);
       
   266     
       
   267     if ((freeMemory >= iCurrentRamTarget) &&
       
   268         ((!iConfig->GlobalConfig().iSwapUsageMonitored) || (freeSwap >= iCurrentSwapTarget)))
   243         {
   269         {
   244         if (iLastMemoryMonitorStatusProperty != EFreeingMemory)
   270         if (iLastMemoryMonitorStatusProperty != EFreeingMemory)
   245             {
   271             {
   246         ResetTargets();
   272         ResetTargets();
   247         iOomActionList->SwitchOffPlugins();
   273         iOomActionList->SwitchOffPlugins();
   260 #endif
   286 #endif
   261 	
   287 	
   262     // Build the list of memory freeing actions
   288     // Build the list of memory freeing actions
   263     iOomActionList->BuildActionListL(*iOomWindowGroupList, *iConfig);
   289     iOomActionList->BuildActionListL(*iOomWindowGroupList, *iConfig);
   264     
   290     
   265     iOomActionList->SetCurrentTarget(iCurrentTarget);
   291 	iOomActionList->SetCurrentTargets(iCurrentRamTarget, iCurrentSwapTarget);
   266     
   292     
   267     // Run the memory freeing actions
   293     // Run the memory freeing actions
   268     iOomActionList->FreeMemory(aMaxPriority);
   294     iOomActionList->FreeMemory(aMaxPriority);
   269     }
   295     }
   270 
   296 
   271 void CMemoryMonitor::RequestFreeMemoryPandSL(TInt aBytesRequested)
   297 void CMemoryMonitor::RequestFreeMemoryPandSL(TInt aBytesRequested)
   272     {
   298     {
   273     FUNC_LOG;
   299     FUNC_LOG;
   274     
   300     
   275     iActionTrigger = EPublishAndSubscribe;
   301     iActionTrigger = EPublishAndSubscribe;
   276     StartFreeSomeRamL(aBytesRequested + iLowThreshold);
   302     StartFreeSomeRamL(aBytesRequested + iLowRamThreshold, iLowSwapThreshold);
   277     }
   303     }
   278 
   304 
   279 void CMemoryMonitor::RequestFreeMemoryL(TInt aBytesRequested)
   305 void CMemoryMonitor::RequestFreeMemoryL(TInt aBytesRequested, TBool aDataPaged)
   280     {
   306     {
   281     FUNC_LOG;
   307     FUNC_LOG;
   282     
   308     
   283     iActionTrigger = EClientServerRequestFreeMemory;
   309     iActionTrigger = EClientServerRequestFreeMemory;
   284     StartFreeSomeRamL(aBytesRequested + iLowThreshold);
   310     iDataPaged = aDataPaged;
   285     }
   311     StartFreeSomeRamL(iLowRamThreshold, aBytesRequested + iLowSwapThreshold);
   286 
   312     }
   287 void CMemoryMonitor::FreeOptionalRamL(TInt aBytesRequested, TInt aPluginId) // The ID of the plugin that will clear up the allocation, used to determine the priority of the allocation
   313 
       
   314 void CMemoryMonitor::FreeOptionalRamL(TInt aBytesRequested, TInt aPluginId, TBool aDataPaged) // The ID of the plugin that will clear up the allocation, used to determine the priority of the allocation
   288     {
   315     {
   289     FUNC_LOG;
   316     FUNC_LOG;
   290     
   317     
   291     iActionTrigger = EClientServerRequestOptionalRam;
   318     iActionTrigger = EClientServerRequestOptionalRam;
   292 
   319 
       
   320     iDataPaged = aDataPaged;
       
   321        
   293     // Calculate the priority of the allocation (the priority of the plugin that will clear it up - 1)
   322     // Calculate the priority of the allocation (the priority of the plugin that will clear it up - 1)
   294     TInt priorityOfAllocation = iConfig->GetPluginConfig(aPluginId).CalculatePluginPriority(*iOomWindowGroupList) - 1;
   323     TInt priorityOfAllocation = iConfig->GetPluginConfig(aPluginId).CalculatePluginPriority(*iOomWindowGroupList) - 1;
   295    
   324    
   296     StartFreeSomeRamL(aBytesRequested + iGoodThreshold, priorityOfAllocation);
   325     StartFreeSomeRamL(aBytesRequested + iGoodRamThreshold, iLowSwapThreshold, priorityOfAllocation);
   297     }
   326     }
   298 
   327 
   299 void CMemoryMonitor::GetFreeMemory(TInt& aCurrentFreeMemory)
   328 void CMemoryMonitor::GetFreeMemory(TInt& aCurrentFreeMemory)
   300     {
   329     {
   301     FUNC_LOG;
   330     FUNC_LOG;
   304     User::CompressAllHeaps();
   333     User::CompressAllHeaps();
   305 
   334 
   306     HAL::Get( HALData::EMemoryRAMFree, aCurrentFreeMemory );
   335     HAL::Get( HALData::EMemoryRAMFree, aCurrentFreeMemory );
   307 
   336 
   308     TRACES1("CMemoryMonitor::GetFreeMemory: Free RAM now %d", aCurrentFreeMemory);
   337     TRACES1("CMemoryMonitor::GetFreeMemory: Free RAM now %d", aCurrentFreeMemory);
       
   338     }
       
   339 
       
   340 void CMemoryMonitor::GetFreeSwapSpace(TInt& aCurrentFreeSwapSpace)
       
   341     {
       
   342     FUNC_LOG;
       
   343     
       
   344     SVMSwapInfo swapInfo;
       
   345     UserSvr::HalFunction(EHalGroupVM, EVMHalGetSwapInfo, &swapInfo, 0);
       
   346     aCurrentFreeSwapSpace = swapInfo.iSwapFree;
       
   347         
       
   348     TRACES1("CMemoryMonitor::GetFreeSwapSpace: Free swap space now %d", aCurrentFreeSwapSpace);
   309     }
   349     }
   310 
   350 
   311 #ifndef CLIENT_REQUEST_QUEUE 
   351 #ifndef CLIENT_REQUEST_QUEUE 
   312 TInt CMemoryMonitor::WatchdogStatusStatusChanged(TAny* aPtr)
   352 TInt CMemoryMonitor::WatchdogStatusStatusChanged(TAny* aPtr)
   313     {
   353     {
   356     FUNC_LOG;
   396     FUNC_LOG;
   357 
   397 
   358     iOomWindowGroupList->Refresh();
   398     iOomWindowGroupList->Refresh();
   359     
   399     
   360     // Calculate the desired good threshold, this could be the globally configured value...
   400     // Calculate the desired good threshold, this could be the globally configured value...
   361     iGoodThreshold = CMemoryMonitor::GlobalConfig().iGoodRamThreshold;
   401     iGoodRamThreshold = CMemoryMonitor::GlobalConfig().iGoodRamThreshold;
   362     iLowThreshold = CMemoryMonitor::GlobalConfig().iLowRamThreshold;
   402     iLowRamThreshold = CMemoryMonitor::GlobalConfig().iLowRamThreshold;
   363     TRACES2("CMemoryMonitor::RefreshThresholds: Global Good Threshold = %d, Global Low Threshold = %d", iGoodThreshold, iLowThreshold);
   403     iGoodSwapThreshold = CMemoryMonitor::GlobalConfig().iGoodSwapThreshold;
       
   404     iLowSwapThreshold = CMemoryMonitor::GlobalConfig().iLowSwapThreshold;
       
   405     TRACES4("CMemoryMonitor::RefreshThresholds: Global Good Ram Threshold = %d, Global Low Ram Threshold = %d, Global Good Swap Threshold = %d, Global Low Swap Threshold = %d", iGoodRamThreshold, iLowRamThreshold, iGoodSwapThreshold, iLowSwapThreshold);
   364 
   406 
   365 #ifdef _DEBUG
   407 #ifdef _DEBUG
   366     TRACES("CMemoryMonitor::RefreshThresholds: Dumping Window Group List");
   408     TRACES("CMemoryMonitor::RefreshThresholds: Dumping Window Group List");
   367     TInt wgIndex = iOomWindowGroupList->Count() - 1;
   409     TInt wgIndex = iOomWindowGroupList->Count() - 1;
   368     while (wgIndex >= 0)        
   410     while (wgIndex >= 0)        
   391             }
   433             }
   392 
   434 
   393         // If this application configuration overrides the good_ram_threshold then set it
   435         // If this application configuration overrides the good_ram_threshold then set it
   394         if (iConfig->GetApplicationConfig(foregroundAppId).iGoodRamThreshold != KOomThresholdUnset)
   436         if (iConfig->GetApplicationConfig(foregroundAppId).iGoodRamThreshold != KOomThresholdUnset)
   395             {
   437             {
   396             iGoodThreshold = iConfig->GetApplicationConfig(foregroundAppId).iGoodRamThreshold;
   438             iGoodRamThreshold = iConfig->GetApplicationConfig(foregroundAppId).iGoodRamThreshold;
   397             TRACES2("CMemoryMonitor::RefreshThresholds: For foreground app %x, Good Threshold = %d", foregroundAppId, iGoodThreshold);
   439             TRACES2("CMemoryMonitor::RefreshThresholds: For foreground app %x, Good Ram Threshold = %d", foregroundAppId, iGoodRamThreshold);
   398             }
   440             }
   399         // If this application configuration overrides the low_ram_threshold then set it
   441         // If this application configuration overrides the low_ram_threshold then set it
   400         if (iConfig->GetApplicationConfig(foregroundAppId).iLowRamThreshold != KOomThresholdUnset)
   442         if (iConfig->GetApplicationConfig(foregroundAppId).iLowRamThreshold != KOomThresholdUnset)
   401             {
   443             {
   402             iLowThreshold = iConfig->GetApplicationConfig(foregroundAppId).iLowRamThreshold;
   444             iLowRamThreshold = iConfig->GetApplicationConfig(foregroundAppId).iLowRamThreshold;
   403             TRACES2("CMemoryMonitor::RefreshThresholds: For foreground app %x, Low Threshold = %d", foregroundAppId, iLowThreshold);
   445             TRACES2("CMemoryMonitor::RefreshThresholds: For foreground app %x, Low Ram Threshold = %d", foregroundAppId, iLowRamThreshold);
   404             }
   446             }
   405 
   447 
       
   448         if (iConfig->GetApplicationConfig(foregroundAppId).iGoodSwapThreshold != KOomThresholdUnset)
       
   449             {
       
   450             iGoodSwapThreshold = iConfig->GetApplicationConfig(foregroundAppId).iGoodSwapThreshold;
       
   451             TRACES2("CMemoryMonitor::RefreshThresholds: For foreground app %x, Good Swap Threshold = %d", foregroundAppId, iGoodSwapThreshold);
       
   452             }
       
   453         // If this application configuration overrides the low_swap_threshold then set it
       
   454         if (iConfig->GetApplicationConfig(foregroundAppId).iLowSwapThreshold != KOomThresholdUnset)
       
   455             {
       
   456             iLowSwapThreshold = iConfig->GetApplicationConfig(foregroundAppId).iLowSwapThreshold;
       
   457             TRACES2("CMemoryMonitor::RefreshThresholds: For foreground app %x, Low Swap Threshold = %d", foregroundAppId, iLowSwapThreshold);
       
   458             }
   406         }
   459         }
   407     }
   460     }
   408 
   461 
   409 // SetMemoryMonitorStatusProperty - updates the property value only if it has changed
   462 // SetMemoryMonitorStatusProperty - updates the property value only if it has changed
   410 void CMemoryMonitor::SetMemoryMonitorStatusProperty(const TMemoryMonitorStatusPropertyValues aValue)
   463 void CMemoryMonitor::SetMemoryMonitorStatusProperty(const TMemoryMonitorStatusPropertyValues aValue)
   421     {
   474     {
   422     FUNC_LOG;
   475     FUNC_LOG;
   423 
   476 
   424     //we reset the target when a memory free operation completes, to deal with the case 
   477     //we reset the target when a memory free operation completes, to deal with the case 
   425     //where the operation was initiated with a target larger than the current good threshold
   478     //where the operation was initiated with a target larger than the current good threshold
   426     iCurrentTarget = iGoodThreshold;
   479     iCurrentRamTarget = iGoodRamThreshold;
   427     iOomActionList->SetCurrentTarget(iCurrentTarget);   
   480     iCurrentSwapTarget = iGoodSwapThreshold;
       
   481     iOomActionList->SetCurrentTargets(iCurrentRamTarget, iCurrentSwapTarget);
   428     }
   482     }
   429 
   483 
   430 void CMemoryMonitor::SetPriorityBusy(TInt aWgId)
   484 void CMemoryMonitor::SetPriorityBusy(TInt aWgId)
   431     {
   485     {
   432     FUNC_LOG;
   486     FUNC_LOG;
   450     {
   504     {
   451     return iActionTrigger;
   505     return iActionTrigger;
   452     }
   506     }
   453 
   507 
   454 #ifdef CLIENT_REQUEST_QUEUE 
   508 #ifdef CLIENT_REQUEST_QUEUE 
   455 TInt CMemoryMonitor::GoodThreshold() const
   509 TInt CMemoryMonitor::GoodRamThreshold() const
   456     {
   510     {
   457     return iGoodThreshold;
   511     return iGoodRamThreshold;
   458     }
   512     }
   459 
   513 
   460 TInt CMemoryMonitor::LowThreshold() const
   514 TInt CMemoryMonitor::LowRamThreshold() const
   461     {
   515     {
   462     return iLowThreshold;
   516     return iLowRamThreshold;
   463     }
   517     }
   464 
   518 
   465 void CMemoryMonitor::ActionsCompleted(TInt aBytesFree, TBool aMemoryGood)
   519 void CMemoryMonitor::ActionsCompleted(TInt aBytesFree, TBool aMemoryGood)
   466     {
   520     {
   467     iQueue->ActionsCompleted(aBytesFree, aMemoryGood);
   521     iQueue->ActionsCompleted(aBytesFree, aMemoryGood);