-
Notifications
You must be signed in to change notification settings - Fork 606
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
api: add pvt::heapsize() and pvt::footprint() methods and image cache…
… memory tracking (#4322) First PR of two, following @lgritz and I discussions on memory tracking in the OIIO::ImageCache. - Add two template methods and their specializations for types various to help memory tracking: ```c++ // return the total heap allocated memory held by the object and its members template<typename T> inline size_t heapsize(const T& t); // return the total memory footprint including the size of the structure itself template<typename T> inline size_t footprint(const T& t); ``` - Specialized for: ParamValue, ParamValueList, ImageSpec, ImageInput, ImageOutput, std::vector, std::shared_ptr, std::unique_ptr, oiio::intrusive_ptr, ImageCacheImpl related objects. - New files: - `include/memory.h` : adds base and few types specialization of heapsize and footprint. - `libtexture/imagecache_memory_pvt.h` : adds specilializations for ImageCacheImpl and related objects. - `libtexture/imagecache_memory_print.h` : adds a helper function that print a breakdown of the ImageCacheImpl total memory usage as well, as well as per image format. Note: this is slow, but gives accurate memory estimation. Related PR from Larry : #4317 Signed-off-by: Basile Fraboni <[email protected]>
- Loading branch information
Showing
15 changed files
with
609 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// Copyright Contributors to the OpenImageIO project. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// https://github.com/AcademySoftwareFoundation/OpenImageIO | ||
|
||
|
||
///////////////////////////////////////////////////////////////////////// | ||
/// @file memory.h | ||
/// | ||
/// @brief Utilities for memory tracking. | ||
///////////////////////////////////////////////////////////////////////// | ||
|
||
|
||
#pragma once | ||
|
||
#define OPENIMAGEIO_MEMORY_H | ||
|
||
#include <cstring> | ||
#include <memory> | ||
#include <vector> | ||
|
||
OIIO_NAMESPACE_BEGIN | ||
|
||
namespace pvt { | ||
|
||
/// Return the total heap memory allocated by `object`. | ||
/// The template specialization can be used to give improved results for non trivial types | ||
/// that perform heap allocation, and to include members allocations recursively. | ||
template<typename T> | ||
inline size_t | ||
heapsize(const T& t) | ||
{ | ||
return 0; | ||
} | ||
|
||
/// Return the total memory footprint of `object`. If possible, including any heap | ||
/// allocations done by any constituent parts. The default implementation just reduces | ||
/// to sizeof(object), given that heapsize(object) would return 0. | ||
/// The template specialization can be used to give improved results for non trivial types | ||
/// that perform heap allocation. | ||
template<typename T> | ||
inline size_t | ||
footprint(const T& t) | ||
{ | ||
return sizeof(T) + heapsize(t); | ||
} | ||
|
||
template<typename T> | ||
inline size_t | ||
footprint(const T* t) | ||
{ | ||
return sizeof(T) + (t ? footprint(*t) : 0); | ||
} | ||
|
||
/// Specializations for common STL types | ||
|
||
|
||
// heapsize specialization for std::string | ||
template<> | ||
inline size_t | ||
heapsize<std::string>(const std::string& s) | ||
{ | ||
// accounts for small string optimization that does not | ||
// use any heap allocations | ||
const char* const sbegin = (const char*)&s; | ||
const char* const send = sbegin + sizeof(std::string); | ||
const char* const sdata = s.data(); | ||
const bool is_small = sdata >= sbegin && sdata < send; | ||
return is_small ? 0 : s.capacity(); | ||
} | ||
|
||
// heapsize specialization for std::vector | ||
template<typename T> | ||
inline size_t | ||
heapsize(const std::vector<T>& vec) | ||
{ | ||
size_t size = 0; | ||
for (const T& elem : vec) | ||
size += footprint(elem); | ||
return size; | ||
} | ||
|
||
// heapsize specialization for std::shared_ptr | ||
template<typename T> | ||
inline size_t | ||
heapsize(const std::shared_ptr<T>& ref) | ||
{ | ||
return ref ? footprint(*ref.get()) : 0; | ||
} | ||
|
||
// footprint specialization for std::shared_ptr | ||
template<typename T> | ||
inline size_t | ||
footprint(const std::shared_ptr<T>& ref) | ||
{ | ||
return sizeof(std::shared_ptr<T>) + heapsize(ref); | ||
} | ||
|
||
// heapsize specialization for std::unique_ptr | ||
template<typename T> | ||
inline size_t | ||
heapsize(const std::unique_ptr<T>& ref) | ||
{ | ||
return ref ? footprint(*ref.get()) : 0; | ||
} | ||
|
||
// footprint specialization for std::unique_ptr | ||
template<typename T> | ||
inline size_t | ||
footprint(const std::unique_ptr<T>& ref) | ||
{ | ||
return sizeof(std::unique_ptr<T>) + heapsize(ref); | ||
} | ||
|
||
} // namespace pvt | ||
|
||
|
||
OIIO_NAMESPACE_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.