LCOV - code coverage report
Current view: top level - shared-main-lib - memorystatus.cpp (source / functions) Coverage Total Hit
Test: QtApplicationManager Lines: 88.9 % 18 16
Test Date: 2024-06-22 10:03:32 Functions: 80.0 % 5 4
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 50.0 % 12 6

             Branch data     Line data    Source code
       1                 :             : // Copyright (C) 2021 The Qt Company Ltd.
       2                 :             : // Copyright (C) 2019 Luxoft Sweden AB
       3                 :             : // Copyright (C) 2018 Pelagicore AG
       4                 :             : // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
       5                 :             : 
       6                 :             : #include "memorystatus.h"
       7                 :             : 
       8                 :             : using namespace Qt::StringLiterals;
       9                 :             : 
      10                 :             : /*!
      11                 :             :     \qmltype MemoryStatus
      12                 :             :     \inqmlmodule QtApplicationManager
      13                 :             :     \ingroup common-instantiatable
      14                 :             :     \brief Provides information on the status of the RAM.
      15                 :             : 
      16                 :             :     MemoryStatus provides information on the status of the system's RAM (random-access memory).
      17                 :             :     Its property values are updated whenever the method update() is called.
      18                 :             : 
      19                 :             :     You can use this component as a MonitorModel data source if you want to plot its
      20                 :             :     previous values over time.
      21                 :             : 
      22                 :             :     \qml
      23                 :             :     import QtQuick
      24                 :             :     import QtApplicationManager
      25                 :             :     ...
      26                 :             :     MonitorModel {
      27                 :             :         MemoryStatus {}
      28                 :             :     }
      29                 :             :     \endqml
      30                 :             : 
      31                 :             :     You can also use it alongside a Timer for instance, when you're only interested in its current value.
      32                 :             : 
      33                 :             :     \qml
      34                 :             :     import QtQuick
      35                 :             :     import QtApplicationManager
      36                 :             :     ...
      37                 :             :     MemoryStatus { id: memoryStatus }
      38                 :             :     Timer {
      39                 :             :         interval: 500
      40                 :             :         running: true
      41                 :             :         repeat: true
      42                 :             :         onTriggered: memoryStatus.update()
      43                 :             :     }
      44                 :             :     Text {
      45                 :             :         text: "memory used: " + (memoryStatus.memoryUsed / 1e6).toFixed(0) + " MB"
      46                 :             :     }
      47                 :             :     \endqml
      48                 :             : */
      49                 :             : 
      50                 :             : QT_USE_NAMESPACE_AM
      51                 :             : 
      52                 :           2 : MemoryStatus::MemoryStatus(QObject *parent)
      53                 :             :     : QObject(parent)
      54         [ +  - ]:           2 :     , m_memoryReader(new MemoryReader)
      55         [ +  - ]:           2 :     , m_memoryUsed(0)
      56                 :             : {
      57                 :           2 : }
      58                 :             : 
      59                 :             : /*!
      60                 :             :     \qmlproperty int MemoryStatus::totalMemory
      61                 :             :     \readonly
      62                 :             : 
      63                 :             :     The total amount of physical memory (RAM) installed on the system in bytes.
      64                 :             : 
      65                 :             :     \sa MemoryStatus::memoryUsed
      66                 :             : */
      67                 :           6 : quint64 MemoryStatus::totalMemory() const
      68                 :             : {
      69                 :             : #if defined(Q_OS_LINUX)
      70                 :           6 :     auto limit = m_memoryReader->groupLimit();
      71   [ -  +  -  - ]:           6 :     if (limit > 0 && limit < m_memoryReader->totalValue())
      72                 :             :         return limit;
      73                 :             :     else
      74                 :           6 :         return m_memoryReader->totalValue();
      75                 :             : #else
      76                 :             :     return m_memoryReader->totalValue();
      77                 :             : #endif
      78                 :             : }
      79                 :             : 
      80                 :             : /*!
      81                 :             :     \qmlproperty int MemoryStatus::memoryUsed
      82                 :             :     \readonly
      83                 :             : 
      84                 :             :     The amount of physical memory (RAM) used in bytes.
      85                 :             : 
      86                 :             :     The value of this property is updated when MemoryStatus::update is called.
      87                 :             : 
      88                 :             :     \sa totalMemory
      89                 :             : */
      90                 :          46 : quint64 MemoryStatus::memoryUsed() const
      91                 :             : {
      92                 :          46 :     return m_memoryUsed;
      93                 :             : }
      94                 :             : 
      95                 :             : /*!
      96                 :             :     \qmlproperty list<string> MemoryStatus::roleNames
      97                 :             :     \readonly
      98                 :             : 
      99                 :             :     Names of the roles provided by MemoryStatus when used as a MonitorModel data source.
     100                 :             : 
     101                 :             :     \sa MonitorModel
     102                 :             : */
     103                 :           2 : QStringList MemoryStatus::roleNames() const
     104                 :             : {
     105         [ +  + ]:           4 :     return { u"memoryUsed"_s };
     106                 :             : }
     107                 :             : 
     108                 :             : /*!
     109                 :             :     \qmlmethod MemoryStatus::update
     110                 :             : 
     111                 :             :     Updates the memoryUsed property.
     112                 :             : 
     113                 :             :     \sa memoryUsed
     114                 :             : */
     115                 :          46 : void MemoryStatus::update()
     116                 :             : {
     117                 :          46 :     quint64 newReading = m_memoryReader->readUsedValue();
     118         [ -  + ]:          46 :     if (m_memoryUsed != newReading) {
     119                 :           0 :         m_memoryUsed = newReading;
     120                 :           0 :         emit memoryUsedChanged();
     121                 :             :     }
     122                 :          46 : }
     123                 :             : 
     124                 :             : #include "moc_memorystatus.cpp"
        

Generated by: LCOV version 2.0-1