www.mooseframework.org
MemoryUsage.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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>("mem_units",
29  "The unit prefix used to report memory usage, default: Mebibytes");
30  params.addParam<bool>("report_peak_value",
31  true,
32  "If the postprocessor is executed more than once "
33  "during a time step, report the aggregated peak "
34  "value.");
35  return params;
36 }
37 
39  : GeneralPostprocessor(parameters),
40  MemoryUsageReporter(this),
41  _mem_type(getParam<MooseEnum>("mem_type").getEnum<MemType>()),
42  _value_type(getParam<MooseEnum>("value_type").getEnum<ValueType>()),
43  _mem_units(getParam<MooseEnum>("mem_units").getEnum<MemoryUtils::MemUnits>()),
44  _value(0.0),
45  _peak_value(0.0),
46  _report_peak_value(getParam<bool>("report_peak_value"))
47 {
48 }
49 
50 void
52 {
53  _peak_value = 0.0;
54 }
55 
56 void
58 {
59  // skip during checking uo/aux state so that this postprocessor returns the memory
60  // usage during the regular execution
62  return;
63 
64  MemoryUtils::Stats stats;
66 
67  // get the requested per core metric
68  switch (_mem_type)
69  {
72  break;
73 
76  break;
77 
79  // major page faults are currently only reported on Linux systems
80  _value = stats._page_faults;
81  break;
82  }
83 }
84 
85 void
87 {
88  // skip during checking uo/aux state so that this postprocessor returns the memory
89  // usage during the regular execution
91  return;
92 
93  // perform the requested reduction
94  switch (_value_type)
95  {
96  case ValueType::total:
98  break;
99 
100  case ValueType::average:
101  gatherSum(_value);
102  _value /= n_processors();
103  break;
104 
106  gatherMax(_value);
107  break;
108 
110  gatherMin(_value);
111  break;
112 
113  default:
114  mooseError("Invalid value_type");
115  }
116 
117  if (_value > _peak_value)
119 }
120 
123 {
125 }
registerMooseObject("MooseApp", MemoryUsage)
MemoryUsage(const InputParameters &parameters)
Definition: MemoryUsage.C:38
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
void gatherMax(T &value)
Gather the parallel max of the variable passed in.
Definition: UserObject.h:137
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...
void gatherMin(T &value)
Gather the parallel min of the variable passed in.
Definition: UserObject.h:149
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()
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
void gatherSum(T &value)
Gather the parallel sum of the variable passed in.
Definition: UserObject.h:125
virtual void timestepSetup() override
Definition: MemoryUsage.C:51
Real PostprocessorValue
various MOOSE typedefs
Definition: MooseTypes.h:191
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:31
Output maximum, average, or total process memory usage.
Definition: MemoryUsage.h:19
virtual void execute() override
Execute method.
Definition: MemoryUsage.C:57
Real _peak_value
peak memory usage metric in bytes (of multiple samples in the current time step)
Definition: MemoryUsage.h:56
FEProblemBase & _fe_problem
Reference to the FEProblemBase for this user object.
Definition: UserObject.h:210
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
virtual PostprocessorValue getValue() const override
This will get called to actually grab the final value the postprocessor has calculated.
Definition: MemoryUsage.C:122
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 option 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:86