camerauis/cameraxui/cxengine/src/cxememorymonitorprivate.cpp
author hgs
Fri, 23 Jul 2010 11:35:41 +0300
changeset 45 24fd82631616
parent 28 3075d9b614e6
permissions -rw-r--r--
201029
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28
3075d9b614e6 201017_1
hgs
parents:
diff changeset
     1
/*
45
hgs
parents: 28
diff changeset
     2
* Copyright (c) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
28
3075d9b614e6 201017_1
hgs
parents:
diff changeset
     3
* All rights reserved.
3075d9b614e6 201017_1
hgs
parents:
diff changeset
     4
* This component and the accompanying materials are made available
3075d9b614e6 201017_1
hgs
parents:
diff changeset
     5
* under the terms of "Eclipse Public License v1.0"
3075d9b614e6 201017_1
hgs
parents:
diff changeset
     6
* which accompanies this distribution, and is available
3075d9b614e6 201017_1
hgs
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
3075d9b614e6 201017_1
hgs
parents:
diff changeset
     8
*
3075d9b614e6 201017_1
hgs
parents:
diff changeset
     9
* Initial Contributors:
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    11
*
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    12
* Contributors:
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    13
*
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    14
* Description:
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    15
*
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    16
*/
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    17
45
hgs
parents: 28
diff changeset
    18
#include "cxememorymonitorprivate.h"
hgs
parents: 28
diff changeset
    19
hgs
parents: 28
diff changeset
    20
#ifdef Q_OS_SYMBIAN
28
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    21
#include <hal.h>
45
hgs
parents: 28
diff changeset
    22
#endif // Q_OS_SYMBIAN
28
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    23
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    24
#include "cxutils.h"
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    25
#include "cxenamespace.h"
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    26
#include "cxefeaturemanager.h"
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    27
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    28
namespace
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    29
{
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    30
    const int FREEING_RETRIES = 2;
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    31
    const int MONITORING_INTERVAL = 10*1000; // in milliseconds
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    32
}
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    33
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    34
/*!
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    35
* Constructor
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    36
*/
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    37
CxeMemoryMonitorPrivate::CxeMemoryMonitorPrivate(CxeFeatureManager &features)
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    38
    : mFeatures(features)
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    39
{
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    40
    CX_DEBUG_ENTER_FUNCTION();
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    41
45
hgs
parents: 28
diff changeset
    42
    mFeatures.configuredValues(CxeVariationKeys::FREE_MEMORY_LEVELS, mLevels);
28
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    43
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    44
    CX_DEBUG(("CxeMemoryMonitorPrivate - trigger level: %d bytes", mLevels.value(Cxe::FreeMemoryTrigger)));
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    45
    CX_DEBUG(("CxeMemoryMonitorPrivate - target level:  %d bytes", mLevels.value(Cxe::FreeMemoryTarget)));
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    46
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    47
#ifdef Q_OS_SYMBIAN
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    48
    // Request free memory as defined in Feature Manager.
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    49
    // If we cannot have enough free memory, throw error.
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    50
    bool ok = requestFreeMemory(mLevels.value(Cxe::FreeMemoryTarget));
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    51
    qt_symbian_throwIfError(ok ? KErrNone : KErrNoMemory);
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    52
#endif // Q_OS_SYMBIAN
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    53
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    54
    CX_DEBUG_EXIT_FUNCTION();
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    55
}
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    56
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    57
/*!
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    58
* Destructor
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    59
*/
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    60
CxeMemoryMonitorPrivate::~CxeMemoryMonitorPrivate()
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    61
{
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    62
    CX_DEBUG_ENTER_FUNCTION();
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    63
    stopMonitoring();
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    64
    CX_DEBUG_EXIT_FUNCTION();
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    65
}
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    66
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    67
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    68
/*!
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    69
* Get the amount of free memory (RAM).
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    70
* @return Free RAM in bytes.
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    71
*/
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    72
int CxeMemoryMonitorPrivate::free() const
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    73
{
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    74
#ifdef Q_OS_SYMBIAN
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    75
    int ram(0);
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    76
    HAL::Get(HALData::EMemoryRAMFree, ram);
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    77
    return ram;
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    78
#else
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    79
    //!@todo: real implementation for desktop etc?
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    80
    return 1024*1024*1024;
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    81
#endif // Q_OS_SYMBIAN
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    82
}
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    83
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    84
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    85
/*!
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    86
* Request to free memory (RAM) to achieve asked level.
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    87
* @param required Required free memory after this call.
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    88
* @return Was it possible to achieve the required level of free memory.
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    89
*/
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    90
bool CxeMemoryMonitorPrivate::requestFreeMemory(int required)
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    91
{
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    92
    CX_DEBUG_ENTER_FUNCTION();
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    93
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    94
    if (free() < required) {
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    95
#ifdef Q_OS_SYMBIAN
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    96
    qt_symbian_throwIfError(mOomMonitor.Connect());
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    97
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    98
    int i(0);
3075d9b614e6 201017_1
hgs
parents:
diff changeset
    99
    int status(KErrGeneral);
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   100
    do {
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   101
        status = mOomMonitor.RequestFreeMemory(required);
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   102
    } while (status != KErrNone && ++i < FREEING_RETRIES);
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   103
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   104
    mOomMonitor.Close();
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   105
#endif // Q_OS_SYMBIAN
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   106
    }
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   107
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   108
    // If we have the free RAM we were asked for, all good.
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   109
    bool ok = free() >= required;
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   110
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   111
    CX_DEBUG_EXIT_FUNCTION();
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   112
    return ok;
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   113
}
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   114
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   115
/*!
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   116
* Start monitoring free memory.
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   117
*/
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   118
void CxeMemoryMonitorPrivate::startMonitoring()
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   119
{
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   120
    if (!mTimer.isActive()) {
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   121
        connect(&mTimer, SIGNAL(timeout()), this, SLOT(checkMemory()), Qt::UniqueConnection);
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   122
        mTimer.start(MONITORING_INTERVAL);
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   123
    }
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   124
}
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   125
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   126
/*!
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   127
* Stop monitoring free memory.
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   128
*/
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   129
void CxeMemoryMonitorPrivate::stopMonitoring()
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   130
{
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   131
    mTimer.stop();
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   132
}
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   133
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   134
/*!
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   135
* Check free memory. Try to release memory if below trigger level.
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   136
*/
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   137
void CxeMemoryMonitorPrivate::checkMemory()
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   138
{
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   139
    CX_DEBUG_ENTER_FUNCTION();
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   140
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   141
    if(free() < mLevels.value(Cxe::FreeMemoryTrigger)) {
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   142
        // Memory too low, request more free memory.
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   143
        CX_DEBUG(("[WARNING] CxeMemoryMonitorPrivate - memory low, requesting more.."));
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   144
        requestFreeMemory(mLevels.value(Cxe::FreeMemoryTarget));
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   145
    }
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   146
    CX_DEBUG_EXIT_FUNCTION();
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   147
}
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   148
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   149
3075d9b614e6 201017_1
hgs
parents:
diff changeset
   150
// end of file