Turi Create  4.0
memory_info.hpp
1 /* Copyright © 2017 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
5  */
6 #ifndef TURI_MEMORY_INFO_HPP
7 #define TURI_MEMORY_INFO_HPP
8 
9 #include <string>
10 #include <cstdint>
11 #include <iostream>
12 #ifdef HAS_TCMALLOC
13 #include <google/malloc_extension.h>
14 #endif
15 #include <core/logging/assertions.hpp>
16 namespace turi {
17  /**
18  * \internal \brief Memory info namespace contains functions used to
19  * compute memory usage.
20  *
21  * The memory info functions require TCMalloc to actually compute
22  * memory usage values. If TCMalloc is not present then calls to
23  * memory info will generate warnings and return the default value.
24  */
25  namespace memory_info {
26 
27  /**
28  * \internal
29  *
30  * \brief Returns whether memory info reporting is
31  * available on this system (if memory_info was built with TCMalloc)
32  *
33  * @return if memory info is available on this system.
34  */
35  inline bool available() {
36 #ifdef HAS_TCMALLOC
37  return true;
38 #else
39  return false;
40 #endif
41  }
42 
43  /**
44  * \internal
45  *
46  * \brief Estimates the total current size of the memory heap in
47  * bytes. If memory info is not available then 0 is returned.
48  *
49  * @return size of heap in bytes
50  */
51  inline size_t heap_bytes() {
52  size_t heap_size(0);
53 #ifdef HAS_TCMALLOC
54  MallocExtension::instance()->
55  GetNumericProperty("generic.heap_size", &heap_size);
56 #else
58  "memory_info::heap_bytes() requires tcmalloc" << std::endl;
59 #endif
60  return heap_size;
61  }
62 
63  /**
64  * \internal
65  *
66  * \brief Determines the total number of allocated bytes. If
67  * memory info is not available then 0 is returned.
68  *
69  * @return the total bytes allocated
70  */
71  inline size_t allocated_bytes() {
72  size_t allocated_size(0);
73 #ifdef HAS_TCMALLOC
74  MallocExtension::instance()->
75  GetNumericProperty("generic.current_allocated_bytes",
76  &allocated_size);
77 #else
79  "memory_info::allocated_bytes() requires tcmalloc" << std::endl;
80 #endif
81  return allocated_size;
82  }
83 
84  /**
85  * \internal
86  *
87  * \brief Print a memory usage summary prefixed by the string
88  * argument.
89  *
90  * @param [in] label the string to print before the memory usage summary.
91  */
92  inline void print_usage(const std::string& label = "") {
93 #ifdef HAS_TCMALLOC
94  const double BYTES_TO_MB = double(1) / double(1024 * 1024);
95  std::cerr
96  << "Memory Info: " << label << std::endl
97  << "\t Heap: " << (heap_bytes() * BYTES_TO_MB) << " MB"
98  << std::endl
99  << "\t Allocated: " << (allocated_bytes() * BYTES_TO_MB) << " MB"
100  << std::endl;
101 #else
103  << "Unable to print memory info for: " << label << ". "
104  << "No memory extensions api available." << std::endl;
105 #endif
106  }
107 
108  /**
109  * \internal
110  *
111  * \brief Log a memory usage summary prefixed by the string
112  * argument.
113  *
114  * @param [in] label the string to print before the memory usage summary.
115  */
116  inline void log_usage(const std::string& label = "") {
117 #ifdef HAS_TCMALLOC
118  const double BYTES_TO_MB = double(1) / double(1024 * 1024);
120  << "Memory Info: " << label
121  << "\n\t Heap: " << (heap_bytes() * BYTES_TO_MB) << " MB"
122  << "\n\t Allocated: " << (allocated_bytes() * BYTES_TO_MB) << " MB"
123  << std::endl;
124 #else
126  << "Unable to print memory info for: " << label << ". "
127  << "No memory extensions api available." << std::endl;
128 #endif
129  }
130  } // end of namespace memory info
131 };
132 
133 #endif
#define logstream(lvl)
Definition: logger.hpp:276
#define LOG_INFO
Definition: logger.hpp:101
void log_usage(const std::string &label="")
Log a memory usage summary prefixed by the string argument.
#define LOG_WARNING
Definition: logger.hpp:98
size_t heap_bytes()
Estimates the total current size of the memory heap in bytes. If memory info is not available then 0 ...
Definition: memory_info.hpp:51
bool available()
Returns whether memory info reporting is available on this system (if memory_info was built with TCMa...
Definition: memory_info.hpp:35
size_t allocated_bytes()
Determines the total number of allocated bytes. If memory info is not available then 0 is returned...
Definition: memory_info.hpp:71
void print_usage(const std::string &label="")
Print a memory usage summary prefixed by the string argument.
Definition: memory_info.hpp:92
#define logstream_once(lvl)
Definition: logger.hpp:291