www.mooseframework.org
Classes | Functions
MemoryUtils Namespace Reference

Classes

struct  Stats
 

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)
 get all memory stats for the current process More...
 

Function Documentation

◆ getMemoryStats()

void MemoryUtils::getMemoryStats ( Stats stats)

get all memory stats for the current process

Definition at line 61 of file MemoryUtils.C.

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

62 {
63  enum StatItem
64  {
65  index_page_faults = 8,
66  index_virtual_size = 19,
67  index_resident_size = 20,
68  num = 21 // total number of entries read
69  };
70 
71  // inspect /proc
72  std::ifstream stat_stream("/proc/self/stat", std::ios_base::in);
73  std::array<std::size_t, 21> val;
74  if (stat_stream)
75  {
76  // if the proc filesystem file is found (Linux) read its contents
77  std::string pid, comm, state;
78  stat_stream >> pid >> comm >> state;
79  for (unsigned int i = 0; i < num; ++i)
80  stat_stream >> val[i];
81 
82  // resident size is reported as number of pages in /proc
83  val[index_resident_size] *= sysconf(_SC_PAGE_SIZE);
84  }
85  else
86  {
87  // set all data entries to zero (if all else should fail)
88  val.fill(0);
89 
90 // obtain mach task info on mac OS
91 #ifdef __APPLE__
92  struct task_basic_info t_info;
93  mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
94  if (KERN_SUCCESS == task_info(mach_task_self(),
95  TASK_BASIC_INFO,
96  reinterpret_cast<task_info_t>(&t_info),
97  &t_info_count))
98  {
99  val[index_virtual_size] = t_info.virtual_size;
100  val[index_resident_size] = t_info.resident_size;
101  }
102 #endif
103  }
104 
105  // physical mem
106  stats._physical_memory = val[index_resident_size];
107 
108  // virtual mem
109  stats._virtual_memory = val[index_virtual_size];
110 
111  // page faults
112  stats._page_faults = val[index_page_faults];
113 }
MPI_Comm comm

◆ getMPIProcessorName()

std::string MemoryUtils::getMPIProcessorName ( )

get the MPI hostname

Definition at line 48 of file MemoryUtils.C.

Referenced by MemoryUsageReporter::sharedMemoryRanksByProcessorname().

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

◆ getTotalRAM()

std::size_t MemoryUtils::getTotalRAM ( )

get the total amount of physical RAM available

Definition at line 32 of file MemoryUtils.C.

Referenced by MemoryUsageReporter::MemoryUsageReporter().

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