forked from idaholab/moose
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add memory usage diagnostic Postprocessor (idaholab#8619)
- Loading branch information
Showing
3 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/****************************************************************/ | ||
/* DO NOT MODIFY THIS HEADER */ | ||
/* MOOSE - Multiphysics Object Oriented Simulation Environment */ | ||
/* */ | ||
/* (c) 2010 Battelle Energy Alliance, LLC */ | ||
/* ALL RIGHTS RESERVED */ | ||
/* */ | ||
/* Prepared by Battelle Energy Alliance, LLC */ | ||
/* Under Contract No. DE-AC07-05ID14517 */ | ||
/* With the U. S. Department of Energy */ | ||
/* */ | ||
/* See COPYRIGHT for full restrictions */ | ||
/****************************************************************/ | ||
|
||
#ifndef MEMORYUSAGE_H | ||
#define MEMORYUSAGE_H | ||
|
||
#include "GeneralPostprocessor.h" | ||
|
||
class MemoryUsage; | ||
|
||
template<> | ||
InputParameters validParams<MemoryUsage>(); | ||
|
||
/** | ||
* Output maximum, average, or total process memory usage | ||
*/ | ||
class MemoryUsage : public GeneralPostprocessor | ||
{ | ||
public: | ||
MemoryUsage(const InputParameters & parameters); | ||
|
||
virtual void initialize() override; | ||
virtual void execute() override; | ||
virtual void finalize() override; | ||
virtual PostprocessorValue getValue() override; | ||
|
||
protected: | ||
enum class MemType { | ||
virtual_memory, | ||
physical_memory, | ||
page_faults | ||
} _mem_type; | ||
|
||
enum class ValueType { | ||
total, | ||
average, | ||
max_process, | ||
min_process | ||
} _value_type; | ||
|
||
/// memory usage metric in bytes | ||
Real _value; | ||
}; | ||
|
||
#endif // MEMORYUSAGE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/****************************************************************/ | ||
/* DO NOT MODIFY THIS HEADER */ | ||
/* MOOSE - Multiphysics Object Oriented Simulation Environment */ | ||
/* */ | ||
/* (c) 2010 Battelle Energy Alliance, LLC */ | ||
/* ALL RIGHTS RESERVED */ | ||
/* */ | ||
/* Prepared by Battelle Energy Alliance, LLC */ | ||
/* Under Contract No. DE-AC07-05ID14517 */ | ||
/* With the U. S. Department of Energy */ | ||
/* */ | ||
/* See COPYRIGHT for full restrictions */ | ||
/****************************************************************/ | ||
|
||
#include "MemoryUsage.h" | ||
|
||
#include <array> | ||
#include <unistd.h> | ||
|
||
#if defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__)) | ||
#include <sys/resource.h> | ||
#define RUSAGE_AVAILABLE | ||
#endif | ||
|
||
template<> | ||
InputParameters validParams<MemoryUsage>() | ||
{ | ||
InputParameters params = validParams<GeneralPostprocessor>(); | ||
MooseEnum mem_type("virtual_memory physical_memory page_faults", "virtual_memory"); | ||
params.addParam<MooseEnum>("mem_type", mem_type, "Memory metric to report."); | ||
MooseEnum value_type("total average max_process min_processs", "total"); | ||
params.addParam<MooseEnum>("value_type", value_type, "Aggregation method to apply to the requested memory metric."); | ||
return params; | ||
} | ||
|
||
MemoryUsage::MemoryUsage(const InputParameters & parameters) : | ||
GeneralPostprocessor(parameters), | ||
_mem_type(getParam<MooseEnum>("mem_type").getEnum<MemType>()), | ||
_value_type(getParam<MooseEnum>("value_type").getEnum<ValueType>()) | ||
{ | ||
} | ||
|
||
void | ||
MemoryUsage::initialize() | ||
{ | ||
_value = 0.0; | ||
} | ||
|
||
void | ||
MemoryUsage::execute() | ||
{ | ||
// inspect /proc | ||
std::ifstream stat_stream("/proc/self/stat", std::ios_base::in); | ||
std::array<unsigned long, 21> val; | ||
if (stat_stream) | ||
{ | ||
std::string pid, comm, state; | ||
stat_stream >> pid >> comm >> state; | ||
for (unsigned int i = 0; i < 21; ++i) | ||
stat_stream >> val[i]; | ||
} | ||
else | ||
{ | ||
val.fill(0); | ||
|
||
// obtain rusage data for limited functionality on mac OS | ||
#ifdef RUSAGE_AVAILABLE | ||
struct rusage rusage; | ||
getrusage( RUSAGE_SELF, &rusage ); | ||
val[8] = rusage.ru_maxrss * 1024; | ||
val[20] = rusage.ru_majflt * 1024; | ||
#endif | ||
} | ||
|
||
// get the requested per core metric | ||
switch (_mem_type) | ||
{ | ||
case MemType::virtual_memory: | ||
_value = val[19]; | ||
break; | ||
|
||
case MemType::physical_memory: | ||
_value = val[20] * sysconf(_SC_PAGE_SIZE); | ||
break; | ||
|
||
case MemType::page_faults: | ||
_value = val[8]; | ||
break; | ||
} | ||
} | ||
|
||
void | ||
MemoryUsage::finalize() | ||
{ | ||
// perform the requested reduction | ||
switch (_value_type) | ||
{ | ||
case ValueType::total: | ||
gatherSum(_value); | ||
break; | ||
|
||
case ValueType::average: | ||
gatherSum(_value); | ||
_value /= n_processors(); | ||
break; | ||
|
||
case ValueType::max_process: | ||
gatherMax(_value); | ||
break; | ||
|
||
case ValueType::min_process: | ||
gatherMin(_value); | ||
break; | ||
} | ||
} | ||
|
||
PostprocessorValue | ||
MemoryUsage::getValue() | ||
{ | ||
return _value; | ||
} | ||
|
||
#undef RUSAGE_AVAILABLE |