Skip to content

Commit

Permalink
api!: add virtual heapsize() and footprint() to ImageInput and ImageO…
Browse files Browse the repository at this point in the history
…utput (AcademySoftwareFoundation#4323)

Second PR of two, following @lgritz and I discussions on memory tracking
in the OIIO::ImageCache.

- the memory tracking system from
AcademySoftwareFoundation#4322 is
not sufficient to track OIIO public objects that can be overriden.
- add virtual `heapsize()` method to ImageInput and ImageOutput that
return the total heap allocated memory held by the structure and its
members.
- [ ] **TODO**: override for every internal OIIO type (bmp, tiff, etc). 

Related PR from Larry :
AcademySoftwareFoundation#4317
First PR:
AcademySoftwareFoundation#4322

Signed-off-by: Basile Fraboni <[email protected]>
Signed-off-by: Zach Lewis <[email protected]>
  • Loading branch information
bfraboni authored and zachlewis committed Sep 16, 2024
1 parent f784415 commit 22dfb86
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 10 deletions.
30 changes: 26 additions & 4 deletions src/include/OpenImageIO/imageio.h
Original file line number Diff line number Diff line change
Expand Up @@ -1778,6 +1778,16 @@ class OIIO_API ImageInput {
/// `ImageInput*`.
typedef ImageInput* (*Creator)();

/// Memory tracking method.
/// Return the total heap memory allocated by `ImageInput`.
/// Overridable version of heapsize defined in memory.h.
virtual size_t heapsize() const;

/// Memory tracking method.
/// Return the total memory footprint of `ImageInput`.
/// Overridable version of footprint defined in memory.h.
virtual size_t footprint() const;

protected:
ImageSpec m_spec; // format spec of the current open subimage/MIPlevel
// BEWARE using m_spec directly -- not thread-safe
Expand Down Expand Up @@ -1886,7 +1896,7 @@ class OIIO_API ImageInput {

void append_error(string_view message) const; // add to error message

/// declare a friend heapsize definition
/// declare friend heapsize and footprint definitions
template <typename T> friend size_t pvt::heapsize(const T&);
};

Expand Down Expand Up @@ -2570,6 +2580,16 @@ class OIIO_API ImageOutput {
/// `ImageOutput*`.
typedef ImageOutput* (*Creator)();

/// Memory tracking method.
/// Return the total heap memory allocated by `ImageOutput`.
/// Overridable version of heapsize defined in memory.h.
virtual size_t heapsize() const;

/// Memory tracking method.
/// Return the total memory footprint of `ImageOutput`.
/// Overridable version of footprint defined in memory.h.
virtual size_t footprint() const;

protected:
/// @{
/// @name Helper functions for ImageOutput implementations.
Expand Down Expand Up @@ -2793,7 +2813,7 @@ class OIIO_API ImageOutput {

void append_error(string_view message) const; // add to m_errmessage

/// declare a friend heapsize definition
/// declare friend heapsize and footprint definitions
template <typename T> friend size_t pvt::heapsize(const T&);
};

Expand All @@ -2804,11 +2824,13 @@ class OIIO_API ImageOutput {
// heapsize specialization for `ImageSpec`
template <> OIIO_API size_t pvt::heapsize<ImageSpec>(const ImageSpec&);

// heapsize specialization for `ImageInput`
// heapsize and footprint specializations for `ImageInput`
template <> OIIO_API size_t pvt::heapsize<ImageInput>(const ImageInput&);
template <> OIIO_API size_t pvt::footprint<ImageInput>(const ImageInput&);

// heapsize specialization for `ImageOutput`
// heapsize and footprint specializations for `ImageOutput`
template <> OIIO_API size_t pvt::heapsize<ImageOutput>(const ImageOutput&);
template <> OIIO_API size_t pvt::footprint<ImageOutput>(const ImageOutput&);



Expand Down
40 changes: 37 additions & 3 deletions src/libOpenImageIO/imageinput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1341,13 +1341,47 @@ ImageInput::check_open(const ImageSpec& spec, ROI range, uint64_t /*flags*/)



template<>
inline size_t
pvt::heapsize<ImageInput::Impl>(const ImageInput::Impl& impl)
{
return impl.m_io_local ? sizeof(Filesystem::IOProxy) : 0;
}



size_t
ImageInput::heapsize() const
{
size_t size = pvt::heapsize(m_impl);
size += pvt::heapsize(m_spec);
return size;
}



size_t
ImageInput::footprint() const
{
return sizeof(ImageInput) + heapsize();
}



template<>
size_t
pvt::heapsize<ImageInput>(const ImageInput& input)
{
//! TODO: change ImageInput API to add a virtual heapsize() function
//! to allow per image input override, and call that function here.
return pvt::heapsize(input.m_spec);
return input.heapsize();
}



template<>
size_t
pvt::footprint<ImageInput>(const ImageInput& input)
{
return input.footprint();
}


Expand Down
40 changes: 37 additions & 3 deletions src/libOpenImageIO/imageoutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1021,13 +1021,47 @@ ImageOutput::check_open(OpenMode mode, const ImageSpec& userspec, ROI range,



template<>
inline size_t
pvt::heapsize<ImageOutput::Impl>(const ImageOutput::Impl& impl)
{
return impl.m_io_local ? sizeof(Filesystem::IOProxy) : 0;
}



size_t
ImageOutput::heapsize() const
{
size_t size = pvt::heapsize(m_impl);
size += pvt::heapsize(m_spec);
return size;
}



size_t
ImageOutput::footprint() const
{
return sizeof(ImageOutput) + heapsize();
}



template<>
size_t
pvt::heapsize<ImageOutput>(const ImageOutput& output)
{
//! TODO: change ImageOutput API to add a virtual heapsize() function
//! to allow per image output override, and call that function here.
return pvt::heapsize(output.m_spec);
return output.heapsize();
}



template<>
size_t
pvt::footprint<ImageOutput>(const ImageOutput& output)
{
return output.footprint();
}


Expand Down

0 comments on commit 22dfb86

Please sign in to comment.