42 #if defined(__GNUC__) && (__GNUC__ >= 6)
43 # pragma GCC diagnostic push
44 # pragma GCC diagnostic ignored "-Wignored-attributes"
53 :
base<malloc_gotcha, double>
56 #if defined(TIMEMORY_USE_CUDA)
72 #if defined(TIMEMORY_USE_CUDA)
73 return "GOTCHA wrapper for memory allocation functions: malloc, calloc, free, "
74 "cudaMalloc, cudaMallocHost, cudaMallocManaged, cudaHostAlloc, cudaFree, "
77 return "GOTCHA wrapper for memory allocation functions: malloc, calloc, free";
81 using base_type::accum;
84 using base_type::value;
86 template <
typename Tp>
89 template <
typename Tp>
93 template <
typename Tp>
98 for(
auto& itr : get_cleanup_list())
100 get_cleanup_list().clear();
104 template <
typename Tp>
107 template <
typename Tp>
142 value = (nmemb * size);
152 get_allocation_map()[ptr] = value;
161 auto itr = get_allocation_map().find(ptr);
162 if(itr != get_allocation_map().end())
166 get_allocation_map().erase(itr);
172 printf(
"[%s]> free of unknown pointer size: %p\n",
180 #if defined(TIMEMORY_USE_CUDA)
189 m_last_addr = devPtr;
199 m_last_addr = hostPtr;
206 void audit(audit::outgoing, cuda::error_t err)
210 void* ptr = (
void*) ((
char**) (m_last_addr)[0]);
211 get_allocation_map()[ptr] = value;
214 PRINT_HERE(
"%s did not return cudaSuccess, values may be corrupted",
245 using alloc_map_t = std::unordered_map<void*, size_t>;
246 using clean_list_t = std::vector<std::function<void()>>;
248 static clean_list_t& get_cleanup_list()
250 static clean_list_t _instance{};
254 static alloc_map_t& get_allocation_map()
256 static thread_local alloc_map_t _instance{};
261 const char* m_prefix =
nullptr;
262 #if defined(TIMEMORY_USE_CUDA)
263 void** m_last_addr =
nullptr;
269 #if defined(TIMEMORY_USE_GOTCHA)
271 template <
typename Tp>
278 using tuple_t = push_back_t<Tp, this_type>;
279 using local_gotcha_type = gotcha<data_size, tuple_t, type_list<this_type>>;
281 local_gotcha_type::get_default_ready() =
false;
282 local_gotcha_type::get_initializer() = []() {
283 local_gotcha_type::template configure<0, void*, size_t>(
"malloc");
284 local_gotcha_type::template configure<1, void*, size_t, size_t>(
"calloc");
285 local_gotcha_type::template configure<2, void, void*>(
"free");
289 # if defined(TIMEMORY_USE_CUDA)
290 local_gotcha_type::template configure<3, cudaError_t, void**, size_t>(
292 local_gotcha_type::template configure<4, cudaError_t, void**, size_t>(
294 local_gotcha_type::template
configure<5, cudaError_t,
void**, size_t,
295 unsigned int>(
"cudaMallocManaged");
296 local_gotcha_type::template
configure<6, cudaError_t,
void**, size_t,
297 unsigned int>(
"cudaHostAlloc");
298 local_gotcha_type::template configure<7, cudaError_t, void*>(
"cudaFree");
299 local_gotcha_type::template configure<8, cudaError_t, void*>(
"cudaFreeHost");
303 get_cleanup_list().emplace_back([]() { malloc_gotcha::tear_down<Tp>(); });
306 template <
typename Tp>
313 using tuple_t = push_back_t<Tp, this_type>;
314 using local_gotcha_type = gotcha<data_size, tuple_t, type_list<this_type>>;
316 local_gotcha_type::get_default_ready() =
false;
317 local_gotcha_type::get_initializer() = []() {};
318 local_gotcha_type::disable();
330 :
base<memory_allocations, void>
346 return "Number of bytes allocated/freed instead of peak/current memory usage: "
347 "free(malloc(10)) + free(malloc(10)) would use 10 bytes but this would "
351 static void global_init() { malloc_gotcha::configure<component_tuple_t<>>(); }
357 if(_cnt.first == 0 && _cnt.second == 0 && !get_data())
359 get_data() = std::make_unique<malloc_bundle_t>();
367 if(_cnt.first == 0 && _cnt.second == 0 && get_data())
370 get_data().reset(
nullptr);
385 #if defined(__GNUC__) && (__GNUC__ >= 6)
386 # pragma GCC diagnostic pop
Implementation of the gotcha component(s)
Definition for various functions for construct in operations.
void stop(TupleT< Tp... > &obj, Args &&... args)
void start(TupleT< Tp... > &obj, Args &&... args)
Inherit from this policy to add reference counting support. Useful if you want to turn a global setti...
char const std::string & _prefix
convert_t< mpl::available_t< concat< T... > >, component_tuple<> > component_tuple_t
void consume_parameters(ArgsT &&...) TIMEMORY_HIDDEN
typename mpl::push_back< Tuple, T >::type push_back_t
tim::mpl::apply< std::string > string
lightweight tuple-alternative for meta-programming logic
Used by component audit member function to designate the parameters being passed are incoming (e....
Used by component audit member function to designate the parameters being passed are outgoing (e....
static int64_t get_unit()
static std::string get_label()
void set_stopped()
store that stop has been called
storage< Tp, Value > storage_type
void set_started()
store that start has been called
The gotcha component rewrites the global offset table such that calling the wrapped function actually...
void set_prefix(const char *_prefix)
void audit(audit::outgoing, void *ptr)
void* is returned from malloc and calloc
this_type & operator+=(const this_type &rhs)
push_back_t< Tp, gotcha_type< Tp > > component_type
void audit(audit::incoming, size_t nbytes)
nbytes is passed to malloc
double get_display() const
void audit(audit::incoming, void *ptr)
void* is passed to free
this_type & operator-=(const this_type &rhs)
typename base_type::storage_type storage_type
std::hash< std::string > string_hash
static std::string label()
push_back_t< Tp, this_type > gotcha_component_type
static void global_finalize()
void audit(audit::incoming, size_t nmemb, size_t size)
nmemb and size is passed to calloc
static std::string description()
static constexpr size_t data_size
This component wraps malloc, calloc, free, cudaMalloc, cudaFree via GOTCHA and tracks the number of b...
std::unique_ptr< malloc_bundle_t > data_pointer_t
static void global_init()
typename malloc_gotcha::gotcha_type< component_tuple_t<> > malloc_gotcha_t
static std::string description()
component_tuple_t< malloc_gotcha_t > malloc_bundle_t
static std::string label()
static void global_finalize()
#define DEBUG_PRINT_HERE(...)