www.mooseframework.org
MemoryUtils.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #include "MemoryUtils.h"
11 #include "MooseError.h"
12 
13 #include <unistd.h>
14 #include <mpi.h>
15 #include <fstream>
16 #include <array>
17 
18 #ifdef __APPLE__
19 #include <mach/task.h>
20 #include <mach/clock.h>
21 #include <mach/mach.h>
22 #include <mach/vm_page_size.h>
23 #include <sys/types.h>
24 #include <sys/sysctl.h>
25 #include <sys/vmmeter.h>
26 #else
27 #include <sys/sysinfo.h>
28 #endif
29 
30 namespace MemoryUtils
31 {
32 
33 std::string
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 }
45 
48 {
49  return MooseEnum("bytes kibibytes mebibytes gibibytes kilobytes megabytes gigabytes",
50  "mebibytes");
51 }
52 
53 std::size_t
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 }
68 
69 void
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 }
125 
126 std::size_t
127 convertBytes(std::size_t bytes, MemUnits unit)
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 }
148 
149 } // namespace MemoryUtils
std::string getMPIProcessorName()
get the MPI hostname
Definition: MemoryUtils.C:34
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:207
std::size_t _physical_memory
Definition: MemoryUtils.h:23
void getMemoryStats(Stats &stats)
get all memory stats for the current process
Definition: MemoryUtils.C:70
std::size_t _virtual_memory
Definition: MemoryUtils.h:24
void mooseWarning(Args &&... args)
Emit a warning message with the given stringified, concatenated args.
Definition: MooseError.h:219
std::size_t _page_faults
Definition: MemoryUtils.h:25
MooseEnum getMemUnitsEnum()
get the moose enum for the mem_unit_prefix parameter
Definition: MemoryUtils.C:47
std::size_t getTotalRAM()
get the total amount of physical RAM available
Definition: MemoryUtils.C:54
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:31
MPI_Comm comm
std::size_t convertBytes(std::size_t bytes, MemUnits unit)
convert bytes to selected unit prefix
Definition: MemoryUtils.C:127