From 22dfb86c4e47a86d7775c744138501acc6b4a81a Mon Sep 17 00:00:00 2001 From: Basile Fraboni Date: Mon, 2 Sep 2024 09:02:29 -0700 Subject: [PATCH] api!: add virtual heapsize() and footprint() to ImageInput and ImageOutput (#4323) Second PR of two, following @lgritz and I discussions on memory tracking in the OIIO::ImageCache. - the memory tracking system from https://github.com/AcademySoftwareFoundation/OpenImageIO/pull/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 : https://github.com/AcademySoftwareFoundation/OpenImageIO/pull/4317 First PR: https://github.com/AcademySoftwareFoundation/OpenImageIO/pull/4322 Signed-off-by: Basile Fraboni Signed-off-by: Zach Lewis --- src/include/OpenImageIO/imageio.h | 30 +++++++++++++++++++--- src/libOpenImageIO/imageinput.cpp | 40 +++++++++++++++++++++++++++--- src/libOpenImageIO/imageoutput.cpp | 40 +++++++++++++++++++++++++++--- 3 files changed, 100 insertions(+), 10 deletions(-) diff --git a/src/include/OpenImageIO/imageio.h b/src/include/OpenImageIO/imageio.h index 927e15119b..4cd773501f 100644 --- a/src/include/OpenImageIO/imageio.h +++ b/src/include/OpenImageIO/imageio.h @@ -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 @@ -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 friend size_t pvt::heapsize(const T&); }; @@ -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. @@ -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 friend size_t pvt::heapsize(const T&); }; @@ -2804,11 +2824,13 @@ class OIIO_API ImageOutput { // heapsize specialization for `ImageSpec` template <> OIIO_API size_t pvt::heapsize(const ImageSpec&); -// heapsize specialization for `ImageInput` +// heapsize and footprint specializations for `ImageInput` template <> OIIO_API size_t pvt::heapsize(const ImageInput&); +template <> OIIO_API size_t pvt::footprint(const ImageInput&); -// heapsize specialization for `ImageOutput` +// heapsize and footprint specializations for `ImageOutput` template <> OIIO_API size_t pvt::heapsize(const ImageOutput&); +template <> OIIO_API size_t pvt::footprint(const ImageOutput&); diff --git a/src/libOpenImageIO/imageinput.cpp b/src/libOpenImageIO/imageinput.cpp index baddc36e08..c4aa28cdf0 100644 --- a/src/libOpenImageIO/imageinput.cpp +++ b/src/libOpenImageIO/imageinput.cpp @@ -1341,13 +1341,47 @@ ImageInput::check_open(const ImageSpec& spec, ROI range, uint64_t /*flags*/) +template<> +inline size_t +pvt::heapsize(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(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(const ImageInput& input) +{ + return input.footprint(); } diff --git a/src/libOpenImageIO/imageoutput.cpp b/src/libOpenImageIO/imageoutput.cpp index 002429fc93..f233b395b8 100644 --- a/src/libOpenImageIO/imageoutput.cpp +++ b/src/libOpenImageIO/imageoutput.cpp @@ -1021,13 +1021,47 @@ ImageOutput::check_open(OpenMode mode, const ImageSpec& userspec, ROI range, +template<> +inline size_t +pvt::heapsize(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(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(const ImageOutput& output) +{ + return output.footprint(); }