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 
17 template <>
20 {
22  params.addClassDescription("Memory usage statistics for the running simulation.");
23  MooseEnum mem_type("physical_memory virtual_memory page_faults", "physical_memory");
24  params.addParam<MooseEnum>("mem_type", mem_type, "Memory metric to report.");
25  MooseEnum value_type("total average max_process min_process", "total");
26  params.addParam<MooseEnum>(
27  "value_type", value_type, "Aggregation method to apply to the requested memory metric.");
28  params.addParam<MooseEnum>("mem_units",
30  "The unit prefix used to report memory usage, default: Mebibytes");
31  params.addParam<bool>("report_peak_value",
32  true,
33  "If the postprocessor is executed more than once "
34  "during a time step, report the aggregated peak "
35  "value.");
36  return params;
37 }
38 
40  : GeneralPostprocessor(parameters),
41  MemoryUsageReporter(this),
42  _mem_type(getParam<MooseEnum>("mem_type").getEnum<MemType>()),
43  _value_type(getParam<MooseEnum>("value_type").getEnum<ValueType>()),
44  _mem_units(getParam<MooseEnum>("mem_units").getEnum<MemoryUtils::MemUnits>()),
45  _value(0.0),
46  _peak_value(0.0),
47  _report_peak_value(getParam<bool>("report_peak_value"))
48 {
49 }
50 
51 void
53 {
54  _peak_value = 0.0;
55 }
56 
57 void
59 {
60  MemoryUtils::Stats stats;
62 
63  // get the requested per core metric
64  switch (_mem_type)
65  {
68  break;
69 
72  break;
73 
75  // major page faults are currently only reported on Linux systems
76  _value = stats._page_faults;
77  break;
78  }
79 }
80 
81 void
83 {
84  // perform the requested reduction
85  switch (_value_type)
86  {
87  case ValueType::total:
89  break;
90 
91  case ValueType::average:
93  _value /= n_processors();
94  break;
95 
98  break;
99 
101  gatherMin(_value);
102  break;
103 
104  default:
105  mooseError("Invalid value_type");
106  }
107 
108  if (_value > _peak_value)
110 }
111 
114 {
116 }
registerMooseObject("MooseApp", MemoryUsage)
MemoryUsage(const InputParameters &parameters)
Definition: MemoryUsage.C:39
virtual PostprocessorValue getValue() override
This will get called to actually grab the final value the postprocessor has calculated.
Definition: MemoryUsage.C:113
enum MemoryUsage::MemType _mem_type
std::size_t _physical_memory
Definition: MemoryUtils.h:23
void getMemoryStats(Stats &stats)
get all memory stats for the current process
Definition: MemoryUtils.C:70
std::size_t _virtual_memory
Definition: MemoryUtils.h:24
std::size_t _page_faults
Definition: MemoryUtils.h:25
void gatherMax(T &value)
Definition: UserObject.h:109
Real _value
memory usage metric in bytes
Definition: MemoryUsage.h:56
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
InputParameters validParams< GeneralPostprocessor >()
void gatherMin(T &value)
Definition: UserObject.h:115
void mooseError(Args &&... args) const
Definition: MooseObject.h:147
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:47
MemoryUtils::MemUnits _mem_units
The unit prefix for the reported memory statistics (kilobyte, megabyte, etc).
Definition: MemoryUsage.h:53
Mix-in class for querying memory metrics used by MemoryUsage and VectorMemoryUsage.
void gatherSum(T &value)
Gather the parallel sum of the variable passed in.
Definition: UserObject.h:103
virtual void timestepSetup() override
Gets called at the beginning of the timestep before this object is asked to do its job...
Definition: MemoryUsage.C:52
Real PostprocessorValue
MOOSE typedefs.
Definition: MooseTypes.h:154
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:24
virtual void execute() override
Execute method.
Definition: MemoryUsage.C:58
Real _peak_value
peak memory usage metric in bytes (of multiple samples in the current time step)
Definition: MemoryUsage.h:59
InputParameters validParams< MemoryUsage >()
Definition: MemoryUsage.C:19
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:62
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...
enum MemoryUsage::ValueType _value_type
std::size_t convertBytes(std::size_t bytes, MemUnits unit)
convert bytes to selected unit prefix
Definition: MemoryUtils.C:127
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:82