www.mooseframework.org
Classes | Enumerations | Functions
MemoryUtils Namespace Reference

Classes

struct  Stats
 

Enumerations

enum  MemUnits {
  MemUnits::Bytes, MemUnits::Kibibytes, MemUnits::Mebibytes, MemUnits::Gibibytes = 3,
  MemUnits::Kilobytes, MemUnits::Megabytes, MemUnits::Gigabytes = 6
}
 

Functions

std::string getMPIProcessorName ()
 get the MPI hostname More...
 
MooseEnum getMemUnitsEnum ()
 get the moose enum for the mem_unit_prefix parameter More...
 
std::size_t getTotalRAM ()
 get the total amount of physical RAM available More...
 
void getMemoryStats (Stats &stats)
 get all memory stats for the current process More...
 
std::size_t convertBytes (std::size_t bytes, MemUnits unit)
 convert bytes to selected unit prefix More...
 

Enumeration Type Documentation

◆ MemUnits

enum MemoryUtils::MemUnits
strong
Enumerator
Bytes 
Kibibytes 
Mebibytes 
Gibibytes 
Kilobytes 
Megabytes 
Gigabytes 

Definition at line 29 of file MemoryUtils.h.

Function Documentation

◆ convertBytes()

std::size_t MemoryUtils::convertBytes ( std::size_t  bytes,
MemUnits  unit 
)

convert bytes to selected unit prefix

Definition at line 127 of file MemoryUtils.C.

Referenced by MemoryUsage::execute(), VectorMemoryUsage::finalize(), and VectorMemoryUsage::VectorMemoryUsage().

128 {
129  if (unit == MemUnits::Bytes)
130  return bytes;
131 
132  unsigned int nunit = static_cast<unsigned int>(unit);
133 
134  // kibi, mebi, gibi
135  if (nunit <= 3)
136  return bytes >> (nunit * 10);
137 
138  // kilo, mega, giga
139  if (nunit <= 6)
140  {
141  while (nunit-- > 3)
142  bytes /= 1000;
143  return bytes;
144  }
145 
146  mooseError("Unknown memory unit");
147 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:208

◆ getMemoryStats()

void MemoryUtils::getMemoryStats ( Stats stats)

get all memory stats for the current process

Definition at line 70 of file MemoryUtils.C.

Referenced by MemoryUsage::execute(), and VectorMemoryUsage::execute().

71 {
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<std::size_t, 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; // in bytes
109  val[index_resident_size] = t_info.resident_size; // in bytes
110  }
111  else
112  mooseDoOnce(::mooseWarning("task_info call failed, memory usage numbers will be incorrect"));
113 #endif
114  }
115 
116  // physical mem
117  stats._physical_memory = val[index_resident_size];
118 
119  // virtual mem
120  stats._virtual_memory = val[index_virtual_size];
121 
122  // page faults
123  stats._page_faults = val[index_page_faults];
124 }
void mooseWarning(Args &&... args)
Emit a warning message with the given stringified, concatenated args.
Definition: MooseError.h:220
MPI_Comm comm

◆ getMemUnitsEnum()

MooseEnum MemoryUtils::getMemUnitsEnum ( )

get the moose enum for the mem_unit_prefix parameter

Definition at line 47 of file MemoryUtils.C.

Referenced by validParams< MemoryUsage >(), and validParams< VectorMemoryUsage >().

48 {
49  return MooseEnum("bytes kibibytes mebibytes gibibytes kilobytes megabytes gigabytes",
50  "mebibytes");
51 }
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:32

◆ getMPIProcessorName()

std::string MemoryUtils::getMPIProcessorName ( )

get the MPI hostname

Definition at line 34 of file MemoryUtils.C.

Referenced by MemoryUsageReporter::sharedMemoryRanksByProcessorname().

35 {
36 #ifdef LIBMESH_HAVE_MPI
37  int mpi_namelen;
38  char mpi_name[MPI_MAX_PROCESSOR_NAME];
39  MPI_Get_processor_name(mpi_name, &mpi_namelen);
40  return mpi_name;
41 #else
42  return "serial";
43 #endif
44 }

◆ getTotalRAM()

std::size_t MemoryUtils::getTotalRAM ( )

get the total amount of physical RAM available

Definition at line 54 of file MemoryUtils.C.

Referenced by MemoryUsageReporter::MemoryUsageReporter().

55 {
56 #ifdef __APPLE__
57  uint64_t hwmem_size;
58  size_t length = sizeof(hwmem_size);
59  if (0 <= sysctlbyname("hw.memsize", &hwmem_size, &length, NULL, 0))
60  return hwmem_size;
61 #else
62  struct sysinfo si_data;
63  if (!sysinfo(&si_data))
64  return si_data.totalram * si_data.mem_unit;
65 #endif
66  return 0;
67 }