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

Classes

struct  Stats
 

Enumerations

enum  MemUnit { MemUnit::Bytes, MemUnit::Kilobytes, MemUnit::Megabytes, MemUnit::Gigabytes }
 

Functions

std::size_t getTotalRAM ()
 get the total amount of physical RAM available More...
 
std::string getMPIProcessorName ()
 get the MPI hostname More...
 
void getMemoryStats (Stats &stats, MemUnit units=MemUnit::Megabytes)
 get all memory stats for the current process More...
 

Enumeration Type Documentation

◆ MemUnit

enum MemoryUtils::MemUnit
strong
Enumerator
Bytes 
Kilobytes 
Megabytes 
Gigabytes 

Definition at line 31 of file MemoryUtils.h.

Function Documentation

◆ getMemoryStats()

void MemoryUtils::getMemoryStats ( Stats stats,
MemUnit  units = MemUnit::Megabytes 
)

get all memory stats for the current process

Definition at line 63 of file MemoryUtils.C.

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

64 {
65  enum StatItem
66  {
67  index_page_faults = 8,
68  index_virtual_size = 19,
69  index_resident_size = 20,
70  num = 21 // total number of entries read
71  };
72 
73  // inspect /proc
74  std::ifstream stat_stream("/proc/self/stat", std::ios_base::in);
75  std::array<std::size_t, 21> val;
76  if (stat_stream)
77  {
78  // if the proc filesystem file is found (Linux) read its contents
79  std::string pid, comm, state;
80  stat_stream >> pid >> comm >> state;
81  for (unsigned int i = 0; i < num; ++i)
82  stat_stream >> val[i];
83 
84  // resident size is reported as number of pages in /proc
85  val[index_resident_size] *= sysconf(_SC_PAGE_SIZE);
86  }
87  else
88  {
89  // set all data entries to zero (if all else should fail)
90  val.fill(0);
91 
92 // obtain mach task info on mac OS
93 #ifdef __APPLE__
94  struct task_basic_info t_info;
95  mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
96  if (KERN_SUCCESS == task_info(mach_task_self(),
97  TASK_BASIC_INFO,
98  reinterpret_cast<task_info_t>(&t_info),
99  &t_info_count))
100  {
101  val[index_virtual_size] = t_info.virtual_size; // in bytes
102  val[index_resident_size] = t_info.resident_size; // in bytes
103  }
104  else
105  mooseDoOnce(::mooseWarning("task_info call failed, memory usage numbers will be incorrect"));
106 #endif
107  }
108 
109  // Create the proper divisor based on the units
110  const long divisor = 1 << (static_cast<unsigned int>(units) * 10);
111 
112  // physical mem
113  stats._physical_memory = val[index_resident_size] / divisor;
114 
115  // virtual mem
116  stats._virtual_memory = val[index_virtual_size] / divisor;
117 
118  // page faults
119  stats._page_faults = val[index_page_faults];
120 }
void mooseWarning(Args &&... args)
Emit a warning message with the given stringified, concatenated args.
Definition: MooseError.h:220
MPI_Comm comm

◆ getMPIProcessorName()

std::string MemoryUtils::getMPIProcessorName ( )

get the MPI hostname

Definition at line 50 of file MemoryUtils.C.

Referenced by MemoryUsageReporter::sharedMemoryRanksByProcessorname().

51 {
52 #ifdef LIBMESH_HAVE_MPI
53  int mpi_namelen;
54  char mpi_name[MPI_MAX_PROCESSOR_NAME];
55  MPI_Get_processor_name(mpi_name, &mpi_namelen);
56  return mpi_name;
57 #else
58  return "serial";
59 #endif
60 }

◆ getTotalRAM()

std::size_t MemoryUtils::getTotalRAM ( )

get the total amount of physical RAM available

Definition at line 34 of file MemoryUtils.C.

Referenced by MemoryUsageReporter::MemoryUsageReporter().

35 {
36 #ifdef __APPLE__
37  uint64_t hwmem_size;
38  size_t length = sizeof(hwmem_size);
39  if (0 <= sysctlbyname("hw.memsize", &hwmem_size, &length, NULL, 0))
40  return hwmem_size;
41 #else
42  struct sysinfo si_data;
43  if (!sysinfo(&si_data))
44  return si_data.totalram * si_data.mem_unit;
45 #endif
46  return 0;
47 }