Skip to content

Commit

Permalink
Add memory usage diagnostic Postprocessor (idaholab#8619)
Browse files Browse the repository at this point in the history
  • Loading branch information
dschwen committed Feb 23, 2017
1 parent 5fb44d7 commit beb0ff9
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 0 deletions.
56 changes: 56 additions & 0 deletions framework/include/postprocessors/MemoryUsage.h
@@ -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
2 changes: 2 additions & 0 deletions framework/src/base/Moose.C
Expand Up @@ -192,6 +192,7 @@
#include "TimestepSize.h"
#include "RunTime.h"
#include "PerformanceData.h"
#include "MemoryUsage.h"
#include "NumElems.h"
#include "NumNodes.h"
#include "NumNonlinearIterations.h"
Expand Down Expand Up @@ -627,6 +628,7 @@ registerObjects(Factory & factory)
registerPostprocessor(TimestepSize);
registerPostprocessor(RunTime);
registerPostprocessor(PerformanceData);
registerPostprocessor(MemoryUsage);
registerPostprocessor(NumElems);
registerPostprocessor(NumNodes);
registerPostprocessor(NumNonlinearIterations);
Expand Down
123 changes: 123 additions & 0 deletions framework/src/postprocessors/MemoryUsage.C
@@ -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

0 comments on commit beb0ff9

Please sign in to comment.