LCOV - code coverage report
Current view: top level - src/postprocessors - MemoryUsage.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 419b9d Lines: 56 61 91.8 %
Date: 2025-08-08 20:01:16 Functions: 6 6 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //* This file is part of the MOOSE framework
       2             : //* https://mooseframework.inl.gov
       3             : //*
       4             : //* All rights reserved, see COPYRIGHT for full restrictions
       5             : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
       6             : //*
       7             : //* Licensed under LGPL 2.1, please see LICENSE for details
       8             : //* https://www.gnu.org/licenses/lgpl-2.1.html
       9             : 
      10             : #include "MemoryUsage.h"
      11             : #include "MemoryUtils.h"
      12             : 
      13             : #include <array>
      14             : 
      15             : registerMooseObject("MooseApp", MemoryUsage);
      16             : 
      17             : InputParameters
      18       15303 : MemoryUsage::validParams()
      19             : {
      20       15303 :   InputParameters params = GeneralPostprocessor::validParams();
      21       15303 :   params.addClassDescription("Memory usage statistics for the running simulation.");
      22       15303 :   MooseEnum mem_type("physical_memory virtual_memory page_faults", "physical_memory");
      23       15303 :   params.addParam<MooseEnum>("mem_type", mem_type, "Memory metric to report.");
      24       15303 :   MooseEnum value_type("total average max_process min_process", "total");
      25       15303 :   params.addParam<MooseEnum>(
      26             :       "value_type", value_type, "Aggregation method to apply to the requested memory metric.");
      27       45909 :   params.addParam<MooseEnum>(
      28       30606 :       "mem_units", MemoryUtils::getMemUnitsEnum(), "The unit prefix used to report memory usage.");
      29       45909 :   params.addParam<bool>("report_peak_value",
      30       30606 :                         true,
      31             :                         "If the postprocessor is executed more than once "
      32             :                         "during a time step, report the aggregated peak "
      33             :                         "value.");
      34       30606 :   return params;
      35       15303 : }
      36             : 
      37         519 : MemoryUsage::MemoryUsage(const InputParameters & parameters)
      38             :   : GeneralPostprocessor(parameters),
      39             :     MemoryUsageReporter(this),
      40         519 :     _mem_type(getParam<MooseEnum>("mem_type").getEnum<MemType>()),
      41         519 :     _value_type(getParam<MooseEnum>("value_type").getEnum<ValueType>()),
      42         519 :     _mem_units(getParam<MooseEnum>("mem_units").getEnum<MemoryUtils::MemUnits>()),
      43         519 :     _value(0.0),
      44         519 :     _peak_value(0.0),
      45        1038 :     _report_peak_value(getParam<bool>("report_peak_value"))
      46             : {
      47         519 : }
      48             : 
      49             : void
      50         663 : MemoryUsage::timestepSetup()
      51             : {
      52         663 :   _peak_value = 0.0;
      53         663 : }
      54             : 
      55             : void
      56        1589 : MemoryUsage::execute()
      57             : {
      58             :   // skip during checking uo/aux state so that this postprocessor returns the memory
      59             :   // usage during the regular execution
      60        1589 :   if (_fe_problem.checkingUOAuxState())
      61         219 :     return;
      62             : 
      63             :   MemoryUtils::Stats stats;
      64        1370 :   MemoryUtils::getMemoryStats(stats);
      65             : 
      66             :   // get the requested per core metric
      67        1370 :   switch (_mem_type)
      68             :   {
      69        1224 :     case MemType::physical_memory:
      70        1224 :       _value = MemoryUtils::convertBytes(stats._physical_memory, _mem_units);
      71        1224 :       break;
      72             : 
      73          73 :     case MemType::virtual_memory:
      74          73 :       _value = MemoryUtils::convertBytes(stats._virtual_memory, _mem_units);
      75          73 :       break;
      76             : 
      77          73 :     case MemType::page_faults:
      78             :       // major page faults are currently only reported on Linux systems
      79          73 :       _value = stats._page_faults;
      80          73 :       break;
      81             :   }
      82             : }
      83             : 
      84             : void
      85        1589 : MemoryUsage::finalize()
      86             : {
      87             :   // skip during checking uo/aux state so that this postprocessor returns the memory
      88             :   // usage during the regular execution
      89        1589 :   if (_fe_problem.checkingUOAuxState())
      90         219 :     return;
      91             : 
      92             :   // perform the requested reduction
      93        1370 :   switch (_value_type)
      94             :   {
      95         730 :     case ValueType::total:
      96         730 :       gatherSum(_value);
      97         730 :       break;
      98             : 
      99         320 :     case ValueType::average:
     100         320 :       gatherSum(_value);
     101         320 :       _value /= n_processors();
     102         320 :       break;
     103             : 
     104         320 :     case ValueType::max_process:
     105         320 :       gatherMax(_value);
     106         320 :       break;
     107             : 
     108           0 :     case ValueType::min_process:
     109           0 :       gatherMin(_value);
     110           0 :       break;
     111             : 
     112           0 :     default:
     113           0 :       mooseError("Invalid value_type");
     114             :   }
     115             : 
     116        1370 :   if (_value > _peak_value)
     117        1164 :     _peak_value = _value;
     118             : }
     119             : 
     120             : PostprocessorValue
     121        1589 : MemoryUsage::getValue() const
     122             : {
     123        1589 :   return _report_peak_value ? _peak_value : _value;
     124             : }

Generated by: LCOV version 1.14