https://mooseframework.inl.gov
MemoryUsage.C
Go to the documentation of this file.
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 
19 {
21  params.addClassDescription("Memory usage statistics for the running simulation.");
22  MooseEnum mem_type("physical_memory virtual_memory page_faults", "physical_memory");
23  params.addParam<MooseEnum>("mem_type", mem_type, "Memory metric to report.");
24  MooseEnum value_type("total average max_process min_process", "total");
25  params.addParam<MooseEnum>(
26  "value_type", value_type, "Aggregation method to apply to the requested memory metric.");
27  params.addParam<MooseEnum>(
28  "mem_units", MemoryUtils::getMemUnitsEnum(), "The unit prefix used to report memory usage.");
29  params.addParam<bool>("report_peak_value",
30  true,
31  "If the postprocessor is executed more than once "
32  "during a time step, report the aggregated peak "
33  "value.");
34  return params;
35 }
36 
38  : GeneralPostprocessor(parameters),
39  MemoryUsageReporter(this),
40  _mem_type(getParam<MooseEnum>("mem_type").getEnum<MemType>()),
41  _value_type(getParam<MooseEnum>("value_type").getEnum<ValueType>()),
42  _mem_units(getParam<MooseEnum>("mem_units").getEnum<MemoryUtils::MemUnits>()),
43  _value(0.0),
44  _peak_value(0.0),
45  _report_peak_value(getParam<bool>("report_peak_value"))
46 {
47 }
48 
49 void
51 {
52  _peak_value = 0.0;
53 }
54 
55 void
57 {
58  // skip during checking uo/aux state so that this postprocessor returns the memory
59  // usage during the regular execution
61  return;
62 
63  MemoryUtils::Stats stats;
65 
66  // get the requested per core metric
67  switch (_mem_type)
68  {
71  break;
72 
75  break;
76 
78  // major page faults are currently only reported on Linux systems
79  _value = stats._page_faults;
80  break;
81  }
82 }
83 
84 void
86 {
87  // skip during checking uo/aux state so that this postprocessor returns the memory
88  // usage during the regular execution
90  return;
91 
92  // perform the requested reduction
93  switch (_value_type)
94  {
95  case ValueType::total:
97  break;
98 
99  case ValueType::average:
100  gatherSum(_value);
101  _value /= n_processors();
102  break;
103 
105  gatherMax(_value);
106  break;
107 
109  gatherMin(_value);
110  break;
111 
112  default:
113  mooseError("Invalid value_type");
114  }
115 
116  if (_value > _peak_value)
118 }
119 
122 {
124 }
registerMooseObject("MooseApp", MemoryUsage)
void gatherSum(T &value)
Gather the parallel sum of the variable passed in.
void gatherMax(T &value)
Gather the parallel max of the variable passed in.
MemoryUsage(const InputParameters &parameters)
Definition: MemoryUsage.C:37
enum MemoryUsage::MemType _mem_type
bool getMemoryStats(Stats &stats)
get all memory stats for the current process stats The Stats object to fill with the data ...
Definition: MemoryUtils.C:79
std::size_t _physical_memory
Definition: MemoryUtils.h:23
std::size_t _virtual_memory
Definition: MemoryUtils.h:24
std::size_t _page_faults
Definition: MemoryUtils.h:25
Real _value
memory usage metric in bytes
Definition: MemoryUsage.h:53
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
This class is here to combine the Postprocessor interface and the base class Postprocessor object alo...
MooseEnum getMemUnitsEnum()
get the moose enum for the mem_unit_prefix parameter
Definition: MemoryUtils.C:52
MemoryUtils::MemUnits _mem_units
The unit prefix for the reported memory statistics (kilobyte, megabyte, etc).
Definition: MemoryUsage.h:50
static InputParameters validParams()
FEProblemBase & _fe_problem
Reference to the FEProblemBase for this user object.
Mix-in class for querying memory metrics used by MemoryUsage and VectorMemoryUsage.
processor_id_type n_processors() const
static InputParameters validParams()
Definition: MemoryUsage.C:18
virtual void timestepSetup() override
Definition: MemoryUsage.C:50
void gatherMin(T &value)
Gather the parallel min of the variable passed in.
Real PostprocessorValue
various MOOSE typedefs
Definition: MooseTypes.h:230
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:54
Output maximum, average, or total process memory usage.
Definition: MemoryUsage.h:19
virtual void execute() override
Execute method.
Definition: MemoryUsage.C:56
Real _peak_value
peak memory usage metric in bytes (of multiple samples in the current time step)
Definition: MemoryUsage.h:56
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type and optionally a file path to the top-level block p...
Definition: MooseBase.h:281
virtual PostprocessorValue getValue() const override
This will get called to actually grab the final value the postprocessor has calculated.
Definition: MemoryUsage.C:121
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump...
const bool _report_peak_value
report peak value for multiple samples in a time step
Definition: MemoryUsage.h:59
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an optional parameter and a documentation string to the InputParameters object...
bool checkingUOAuxState() const
Return a flag to indicate whether we are executing user objects and auxliary kernels for state check ...
enum MemoryUsage::ValueType _value_type
std::size_t convertBytes(std::size_t bytes, MemUnits unit)
convert bytes to selected unit prefix
Definition: MemoryUtils.C:174
virtual void finalize() override
This is called after execute() and after threadJoin()! This is probably where you want to do MPI comm...
Definition: MemoryUsage.C:85