1 #ifndef MADNESS_WORLD_WORLDMEM_H__INCLUDED
2 #define MADNESS_WORLD_WORLDMEM_H__INCLUDED
40 #ifdef WORLD_GATHER_MEM_STATS
47 #if defined(MADNESS_HAS_GOOGLE_PERF_TCMALLOC)
48 #include <gperftools/malloc_extension.h>
49 #elif defined(HAVE_IBMBGQ)
50 #include <spi/include/kernel/memory.h>
51 #elif defined(ON_A_MAC)
52 #include <malloc/malloc.h>
56 #include <sys/types.h>
58 #include <sys/sysinfo.h>
72 #ifdef WORLD_GATHER_MEM_STATS
73 friend void* ::operator
new(
size_t size)
throw (std::bad_alloc);
74 friend void ::operator
delete(
void *
p)
throw();
78 void do_new(
void *
p, std::size_t size);
81 void do_del(
void *
p, std::size_t size);
84 static const unsigned long overhead = 4*
sizeof(long) + 16;
116 template <
typename Char>
const Char*
Vm_cstr();
150 int rank = -1,
const std::string filename_prefix = std::string(
""));
163 int rank,
const String& tag,
164 const std::string filename_prefix = std::string(
"MEMORY"),
165 bool verbose =
false) {
166 #if defined(WORLD_MEM_PROFILE_ENABLE)
168 std::basic_ofstream<wchar_t> local_ofstream;
172 filename << filename_prefix <<
"." << rank;
174 local_ofstream.open(
filename.str().c_str(),
175 std::basic_ios<wchar_t>::out |
176 std::basic_ios<wchar_t>::app);
179 if constexpr (std::is_same_v<String, std::basic_string<char>> || std::is_same_v<String, std::basic_string_view<char>>) {
180 std::wstring wtag; wtag.resize(tag.size());
181 std::copy(tag.begin(), tag.end(), wtag.begin());
182 memoryfile << wtag << std::endl;
185 memoryfile << tag << std::endl;
187 const double to_MiB =
188 1 / (1024.0 * 1024.0);
189 #if defined(MADNESS_HAS_GOOGLE_PERF_TCMALLOC)
192 MallocExtension::instance()->GetStats(&buf[0], 100000);
193 memoryfile << buf << std::endl;
197 MallocExtension::instance()->GetStats(&buf[0], 9999);
198 memoryfile << buf << std::endl;
200 #elif defined(HAVE_IBMBGQ)
201 uint64_t shared, persist, heapavail, stackavail, stack, heap, guard,
204 Kernel_GetMemorySize(KERNEL_MEMSIZE_SHARED, &shared);
205 Kernel_GetMemorySize(KERNEL_MEMSIZE_PERSIST, &persist);
206 Kernel_GetMemorySize(KERNEL_MEMSIZE_HEAPAVAIL, &heapavail);
207 Kernel_GetMemorySize(KERNEL_MEMSIZE_STACKAVAIL, &stackavail);
208 Kernel_GetMemorySize(KERNEL_MEMSIZE_STACK, &stack);
209 Kernel_GetMemorySize(KERNEL_MEMSIZE_HEAP, &heap);
210 Kernel_GetMemorySize(KERNEL_MEMSIZE_GUARD, &guard);
211 Kernel_GetMemorySize(KERNEL_MEMSIZE_MMAP, &mmap);
213 memoryfile <<
"Heap size (MiB): " << (heap * to_MiB)
214 <<
", available: " << (heapavail * to_MiB) << std::endl;
215 memoryfile <<
"Stack size (MiB): " << (stack * to_MiB)
216 <<
", available: " << (stackavail * to_MiB) << std::endl;
217 memoryfile <<
"Memory (MiB): shared: " << (shared * to_MiB)
218 <<
", persist: " << (persist * to_MiB)
219 <<
", guard: " << (guard * to_MiB) <<
", mmap: " << (mmap * to_MiB)
221 #elif defined(ON_A_MAC)
223 struct malloc_statistics_t mi;
225 malloc_zone_statistics(
nullptr, &mi);
227 memoryfile <<
"Heap allocated (MiB): " << (mi.size_allocated * to_MiB) << std::endl;
228 memoryfile <<
"Heap used (MiB): " << (mi.size_in_use * to_MiB) << std::endl;
229 memoryfile <<
"Heap max used (MiB): " << (mi.max_size_in_use * to_MiB) << std::endl;
230 #elif defined(X86_32)
235 memoryfile <<
"Non-mmap (MiB): " << (mi.arena * to_MiB) << std::endl;
236 memoryfile <<
"Mmap (MiB): " << (mi.hblkhd * to_MiB) << std::endl;
237 memoryfile <<
"Total malloc chunks (MiB): " << (mi.uordblks * to_MiB)
239 #elif defined(X86_64)
241 std::string status_fname =
242 std::string(
"/proc/") + std::to_string(getpid()) + std::string(
"/status");
243 std::basic_ifstream<char> status_stream(status_fname);
244 if (status_stream.good()) {
245 std::basic_string<char> line;
246 while (std::getline(status_stream, line)) {
248 memoryfile << line.c_str() << std::endl;
250 status_stream.close();
260 memoryfile <<
"Total RAM (MiB): " << (si.totalram * to_MiB) << std::endl;
261 memoryfile <<
"Free RAM (MiB): " << (si.freeram * to_MiB) << std::endl;
262 memoryfile <<
"Buffer (MiB): " << (si.bufferram * to_MiB) << std::endl;
263 memoryfile <<
"RAM in use (MiB): "
264 << ((si.totalram - si.freeram + si.bufferram) * to_MiB)
Used to output memory statistics and control tracing, etc.
Definition: worldmem.h:71
void print() const
Prints memory use statistics to std::cout.
Definition: worldmem.cc:91
void reset()
Resets all counters to zero.
Definition: worldmem.cc:105
void set_trace(bool trace)
If trace is set true a message is printed for every new and delete.
Definition: worldmem.h:102
void set_max_mem_limit(unsigned long max_mem_limit)
Set the maximum memory limit (trying to allocate more will throw MadnessException)
Definition: worldmem.h:107
unsigned long num_new_calls
If you add new stats be sure that the initialization in worldmem.cc is OK.
Definition: worldmem.h:86
void do_del(void *p, std::size_t size)
Invoked when user pointer p is deleted with size bytes.
Definition: worldmem.cc:82
unsigned long cur_num_bytes
Current amount of allocated memory in bytes.
Definition: worldmem.h:90
void do_new(void *p, std::size_t size)
Invoked when user pointer p is allocated with size bytes.
Definition: worldmem.cc:71
unsigned long max_num_frags
Lifetime maximum number of allocated fragments.
Definition: worldmem.h:89
unsigned long cur_num_frags
Current number of allocated fragments.
Definition: worldmem.h:88
unsigned long num_del_calls
Counts calls to delete.
Definition: worldmem.h:87
unsigned long max_mem_limit
if size+cur_num_bytes>max_mem_limit new will throw MadnessException
Definition: worldmem.h:92
static const unsigned long overhead
Definition: worldmem.h:84
unsigned long max_num_bytes
Lifetime maximum number of allocated bytes.
Definition: worldmem.h:91
bool trace
Definition: worldmem.h:93
char * p(char *buf, const char *name, int k, int initial_level, double thresh, int order)
Definition: derivatives.cc:72
Fcwf copy(Fcwf psi)
Definition: fcwf.cc:338
Macros and tools pertaining to the configuration of MADNESS.
const char * Vm_cstr< char >()
Definition: worldmem.cc:181
bool & print_meminfo_flag_accessor()
Definition: worldmem.cc:184
File holds all helper structures necessary for the CC_Operator and CC2 class.
Definition: DFParameters.h:10
WorldMemInfo * world_mem_info()
Returns pointer to internal structure.
Definition: worldmem.cc:67
static const char * filename
Definition: legendre.cc:96
bool print_meminfo_enabled()
Definition: worldmem.cc:200
std::basic_ofstream< wchar_t > & print_meminfo_ostream(int rank, const std::string filename_prefix)
Definition: worldmem.cc:213
void print_meminfo(int rank, const String &tag, const std::string filename_prefix=std::string("MEMORY"), bool verbose=false)
print aggregate memory stats to file filename_prefix.<rank> , tagged with tag
Definition: worldmem.h:162
void print_meminfo_keep_ostream_open(bool keep_open)
Definition: worldmem.cc:204
void print_meminfo_disable()
disables print_meminfo() profiling (i.e. calling it is a no-op)
Definition: worldmem.cc:194
void print_meminfo_enable()
Definition: worldmem.cc:197