LCOV - code coverage report
Current view: top level - src/postprocessors - MemoryUsage.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 56 61 91.8 %
Date: 2025-07-17 01:28:37 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       15177 : MemoryUsage::validParams()
      19             : {
      20       15177 :   InputParameters params = GeneralPostprocessor::validParams();
      21       15177 :   params.addClassDescription("Memory usage statistics for the running simulation.");
      22       15177 :   MooseEnum mem_type("physical_memory virtual_memory page_faults", "physical_memory");
      23       15177 :   params.addParam<MooseEnum>("mem_type", mem_type, "Memory metric to report.");
      24       15177 :   MooseEnum value_type("total average max_process min_process", "total");
      25       15177 :   params.addParam<MooseEnum>(
      26             :       "value_type", value_type, "Aggregation method to apply to the requested memory metric.");
      27       45531 :   params.addParam<MooseEnum>(
      28       30354 :       "mem_units", MemoryUtils::getMemUnitsEnum(), "The unit prefix used to report memory usage.");
      29       45531 :   params.addParam<bool>("report_peak_value",
      30       30354 :                         true,
      31             :                         "If the postprocessor is executed more than once "
      32             :                         "during a time step, report the aggregated peak "
      33             :                         "value.");
      34       30354 :   return params;
      35       15177 : }
      36             : 
      37         456 : MemoryUsage::MemoryUsage(const InputParameters & parameters)
      38             :   : GeneralPostprocessor(parameters),
      39             :     MemoryUsageReporter(this),
      40         456 :     _mem_type(getParam<MooseEnum>("mem_type").getEnum<MemType>()),
      41         456 :     _value_type(getParam<MooseEnum>("value_type").getEnum<ValueType>()),
      42         456 :     _mem_units(getParam<MooseEnum>("mem_units").getEnum<MemoryUtils::MemUnits>()),
      43         456 :     _value(0.0),
      44         456 :     _peak_value(0.0),
      45         912 :     _report_peak_value(getParam<bool>("report_peak_value"))
      46             : {
      47         456 : }
      48             : 
      49             : void
      50         585 : MemoryUsage::timestepSetup()
      51             : {
      52         585 :   _peak_value = 0.0;
      53         585 : }
      54             : 
      55             : void
      56        1409 : MemoryUsage::execute()
      57             : {
      58             :   // skip during checking uo/aux state so that this postprocessor returns the memory
      59             :   // usage during the regular execution
      60        1409 :   if (_fe_problem.checkingUOAuxState())
      61         198 :     return;
      62             : 
      63             :   MemoryUtils::Stats stats;
      64        1211 :   MemoryUtils::getMemoryStats(stats);
      65             : 
      66             :   // get the requested per core metric
      67        1211 :   switch (_mem_type)
      68             :   {
      69        1079 :     case MemType::physical_memory:
      70        1079 :       _value = MemoryUtils::convertBytes(stats._physical_memory, _mem_units);
      71        1079 :       break;
      72             : 
      73          66 :     case MemType::virtual_memory:
      74          66 :       _value = MemoryUtils::convertBytes(stats._virtual_memory, _mem_units);
      75          66 :       break;
      76             : 
      77          66 :     case MemType::page_faults:
      78             :       // major page faults are currently only reported on Linux systems
      79          66 :       _value = stats._page_faults;
      80          66 :       break;
      81             :   }
      82             : }
      83             : 
      84             : void
      85        1409 : MemoryUsage::finalize()
      86             : {
      87             :   // skip during checking uo/aux state so that this postprocessor returns the memory
      88             :   // usage during the regular execution
      89        1409 :   if (_fe_problem.checkingUOAuxState())
      90         198 :     return;
      91             : 
      92             :   // perform the requested reduction
      93        1211 :   switch (_value_type)
      94             :   {
      95         651 :     case ValueType::total:
      96         651 :       gatherSum(_value);
      97         651 :       break;
      98             : 
      99         280 :     case ValueType::average:
     100         280 :       gatherSum(_value);
     101         280 :       _value /= n_processors();
     102         280 :       break;
     103             : 
     104         280 :     case ValueType::max_process:
     105         280 :       gatherMax(_value);
     106         280 :       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        1211 :   if (_value > _peak_value)
     117        1028 :     _peak_value = _value;
     118             : }
     119             : 
     120             : PostprocessorValue
     121        1409 : MemoryUsage::getValue() const
     122             : {
     123        1409 :   return _report_peak_value ? _peak_value : _value;
     124             : }

Generated by: LCOV version 1.14