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