www.mooseframework.org
MemoryUsage.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 #include "MemoryUsage.h"
16 
17 #include <array>
18 #include <unistd.h>
19 #include <fstream>
20 
21 #ifdef __APPLE__
22 #include <mach/task.h>
23 #include <mach/clock.h>
24 #include <mach/mach.h>
25 #endif
26 
27 template <>
30 {
32  params.addClassDescription("Memory usage statistics for the running simulation.");
33  MooseEnum mem_type("virtual_memory physical_memory page_faults", "virtual_memory");
34  params.addParam<MooseEnum>("mem_type", mem_type, "Memory metric to report.");
35  MooseEnum value_type("total average max_process min_processs", "total");
36  params.addParam<MooseEnum>(
37  "value_type", value_type, "Aggregation method to apply to the requested memory metric.");
38  params.addParam<bool>("report_peak_value",
39  true,
40  "If the postprocessor is executed more than one "
41  "during a time step, report the aggregated peak "
42  "value.");
43  return params;
44 }
45 
47  : GeneralPostprocessor(parameters),
48  _mem_type(getParam<MooseEnum>("mem_type").getEnum<MemType>()),
49  _value_type(getParam<MooseEnum>("value_type").getEnum<ValueType>()),
50  _value(0.0),
51  _peak_value(0.0),
52  _report_peak_value(getParam<bool>("report_peak_value"))
53 {
54 }
55 
56 void
58 {
59  _peak_value = 0.0;
60 }
61 
62 void
64 {
65  _value = 0.0;
66 }
67 
68 void
70 {
71  // data entries are numbered according to their position in /proc/self/stat
72  enum StatItem
73  {
74  index_page_faults = 8,
75  index_virtual_size = 19,
76  index_resident_size = 20,
77  num = 21 // total number of entries read
78  };
79 
80  // inspect /proc
81  std::ifstream stat_stream("/proc/self/stat", std::ios_base::in);
82  std::array<unsigned long, 21> val;
83  if (stat_stream)
84  {
85  // if the proc filesystem file is found (Linux) read its contents
86  std::string pid, comm, state;
87  stat_stream >> pid >> comm >> state;
88  for (unsigned int i = 0; i < num; ++i)
89  stat_stream >> val[i];
90 
91  // resident size is reported as number of pages in /proc
92  val[index_resident_size] *= sysconf(_SC_PAGE_SIZE);
93  }
94  else
95  {
96  // set all data entries to zero (if all else should fail)
97  val.fill(0);
98 
99 // obtain mach task info on mac OS
100 #ifdef __APPLE__
101  struct task_basic_info t_info;
102  mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
103  if (KERN_SUCCESS == task_info(mach_task_self(),
104  TASK_BASIC_INFO,
105  reinterpret_cast<task_info_t>(&t_info),
106  &t_info_count))
107  {
108  val[index_virtual_size] = t_info.virtual_size;
109  val[index_resident_size] = t_info.resident_size;
110  }
111 #endif
112  }
113 
114  // get the requested per core metric
115  switch (_mem_type)
116  {
118  _value = val[index_virtual_size];
119  break;
120 
122  _value = val[index_resident_size];
123  break;
124 
126  // major page faults are currently only reported on Linux systems
127  _value = val[index_page_faults];
128  break;
129  }
130 }
131 
132 void
134 {
135  // perform the requested reduction
136  switch (_value_type)
137  {
138  case ValueType::total:
139  gatherSum(_value);
140  break;
141 
142  case ValueType::average:
143  gatherSum(_value);
144  _value /= n_processors();
145  break;
146 
148  gatherMax(_value);
149  break;
150 
152  gatherMin(_value);
153  break;
154 
155  default:
156  mooseError("Invalid value_type");
157  }
158 
159  if (_value > _peak_value)
161 }
162 
165 {
167 }
MemoryUsage(const InputParameters &parameters)
Definition: MemoryUsage.C:46
virtual PostprocessorValue getValue() override
This will get called to actually grab the final value the postprocessor has calculated.
Definition: MemoryUsage.C:164
enum MemoryUsage::MemType _mem_type
void gatherMax(T &value)
Definition: UserObject.h:125
Real _value
memory usage metric in bytes
Definition: MemoryUsage.h:57
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:131
This class is here to combine the Postprocessor interface and the base class Postprocessor object alo...
void gatherSum(T &value)
Gather the parallel sum of the variable passed in.
Definition: UserObject.h:119
virtual void timestepSetup() override
Gets called at the beginning of the timestep before this object is asked to do its job...
Definition: MemoryUsage.C:57
Real PostprocessorValue
MOOSE typedefs.
Definition: MooseTypes.h:73
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:37
virtual void execute() override
Execute method.
Definition: MemoryUsage.C:69
Real _peak_value
peak memory usage metric in bytes (of multiple samples in the current time step)
Definition: MemoryUsage.h:60
MPI_Comm comm
InputParameters validParams< MemoryUsage >()
Definition: MemoryUsage.C:29
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:63
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...
void mooseError(Args &&...args) const
Definition: MooseObject.h:80
virtual void initialize() override
Called before execute() is ever called so that data can be cleared.
Definition: MemoryUsage.C:63
enum MemoryUsage::ValueType _value_type
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:133