Skip to content

Commit 519f0a7

Browse files
authored
Merge pull request #1335 from Idclip/feature/memusage
memUsageIfLoaded
2 parents 9bf2128 + c0d83f5 commit 519f0a7

File tree

13 files changed

+241
-23
lines changed

13 files changed

+241
-23
lines changed

openvdb/openvdb/points/AttributeArray.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,14 @@ class OPENVDB_API AttributeArray
194194
/// Return the number of bytes of memory used by this attribute.
195195
virtual size_t memUsage() const = 0;
196196

197+
#if OPENVDB_ABI_VERSION_NUMBER >= 10
198+
/// Return the number of bytes of memory used by this attribute array once it
199+
/// has been deserialized (this may be different to memUsage() if delay-loading
200+
/// is in use). Note that this method does NOT consider the fact that a
201+
/// uniform attribute could be expanded and only deals with delay-loading.
202+
virtual size_t memUsageIfLoaded() const = 0;
203+
#endif
204+
197205
/// Create a new attribute array of the given (registered) type, length and stride.
198206
/// @details If @a lock is non-null, the AttributeArray registry mutex
199207
/// has already been locked
@@ -638,6 +646,14 @@ class TypedAttributeArray final: public AttributeArray
638646
/// Return the number of bytes of memory used by this attribute.
639647
size_t memUsage() const override;
640648

649+
#if OPENVDB_ABI_VERSION_NUMBER >= 10
650+
/// Return the number of bytes of memory used by this attribute array once it
651+
/// has been deserialized (this may be different to memUsage() if delay-loading
652+
/// is in use). Note that this method does NOT consider the fact that a
653+
/// uniform attribute could be expanded and only deals with delay-loading.
654+
size_t memUsageIfLoaded() const override;
655+
#endif
656+
641657
/// Return the value at index @a n (assumes in-core)
642658
ValueType getUnsafe(Index n) const;
643659
/// Return the value at index @a n
@@ -1357,6 +1373,15 @@ TypedAttributeArray<ValueType_, Codec_>::memUsage() const
13571373
return sizeof(*this) + (bool(mData) ? this->arrayMemUsage() : 0);
13581374
}
13591375

1376+
#if OPENVDB_ABI_VERSION_NUMBER >= 10
1377+
template<typename ValueType_, typename Codec_>
1378+
size_t
1379+
TypedAttributeArray<ValueType_, Codec_>::memUsageIfLoaded() const
1380+
{
1381+
return sizeof(*this) + (mIsUniform ? 1 : this->dataSize()) * sizeof(StorageType);
1382+
}
1383+
#endif
1384+
13601385

13611386
template<typename ValueType_, typename Codec_>
13621387
typename TypedAttributeArray<ValueType_, Codec_>::ValueType

openvdb/openvdb/points/AttributeSet.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,19 @@ AttributeSet::memUsage() const
139139
}
140140

141141

142+
#if OPENVDB_ABI_VERSION_NUMBER >= 10
143+
size_t
144+
AttributeSet::memUsageIfLoaded() const
145+
{
146+
size_t bytes = sizeof(*this) + mDescr->memUsage();
147+
for (const auto& attr : mAttrs) {
148+
bytes += attr->memUsageIfLoaded();
149+
}
150+
return bytes;
151+
}
152+
#endif
153+
154+
142155
size_t
143156
AttributeSet::find(const std::string& name) const
144157
{

openvdb/openvdb/points/AttributeSet.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ class OPENVDB_API AttributeSet
113113
/// Return the number of bytes of memory used by this attribute set.
114114
size_t memUsage() const;
115115

116+
#if OPENVDB_ABI_VERSION_NUMBER >= 10
117+
/// Return the number of bytes of memory used by this attribute set once it
118+
/// has been deserialized (this may be different to memUsage() if delay-loading
119+
/// is in use).
120+
size_t memUsageIfLoaded() const;
121+
#endif
122+
116123
/// @brief Return the position of the attribute array whose name is @a name,
117124
/// or @c INVALID_POS if no match is found.
118125
size_t find(const std::string& name) const;

openvdb/openvdb/points/PointDataGrid.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,9 @@ class PointDataLeafNode : public tree::LeafNode<T, Log2Dim>, io::MultiPass {
503503

504504

505505
Index64 memUsage() const;
506+
#if OPENVDB_ABI_VERSION_NUMBER >= 10
507+
Index64 memUsageIfLoaded() const;
508+
#endif
506509

507510
void evalActiveBoundingBox(CoordBBox& bbox, bool visitVoxels = true) const;
508511

@@ -1520,6 +1523,15 @@ PointDataLeafNode<T, Log2Dim>::memUsage() const
15201523
return BaseLeaf::memUsage() + mAttributeSet->memUsage();
15211524
}
15221525

1526+
#if OPENVDB_ABI_VERSION_NUMBER >= 10
1527+
template<typename T, Index Log2Dim>
1528+
inline Index64
1529+
PointDataLeafNode<T, Log2Dim>::memUsageIfLoaded() const
1530+
{
1531+
return BaseLeaf::memUsageIfLoaded() + mAttributeSet->memUsageIfLoaded();
1532+
}
1533+
#endif
1534+
15231535
template<typename T, Index Log2Dim>
15241536
inline void
15251537
PointDataLeafNode<T, Log2Dim>::evalActiveBoundingBox(CoordBBox& bbox, bool visitVoxels) const

openvdb/openvdb/tools/Count.h

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,21 @@ Index64 countActiveTiles(const TreeT& tree, bool threaded = true);
6060

6161

6262
/// @brief Return the total amount of memory in bytes occupied by this tree.
63+
/// @details This method returns the total in-core memory usage which can be
64+
/// different to the maximum possible memory usage for trees which have not
65+
/// been fully deserialized (via delay-loading). Thus, this is the current
66+
/// true memory consumption.
6367
template <typename TreeT>
6468
Index64 memUsage(const TreeT& tree, bool threaded = true);
6569

70+
71+
/// @brief Return the deserialized memory usage of this tree. This is not
72+
/// necessarily equal to the current memory usage (returned by tools::memUsage)
73+
/// if delay-loading is enabled. See File::open.
74+
template <typename TreeT>
75+
Index64 memUsageIfLoaded(const TreeT& tree, bool threaded = true);
76+
77+
6678
/// @brief Return the minimum and maximum active values in this tree.
6779
/// @note Returns zeroVal<ValueType> for empty trees.
6880
template <typename TreeT>
@@ -287,21 +299,22 @@ struct MemUsageOp
287299
using RootT = typename TreeType::RootNodeType;
288300
using LeafT = typename TreeType::LeafNodeType;
289301

290-
MemUsageOp() = default;
291-
MemUsageOp(const MemUsageOp&, tbb::split) { }
302+
MemUsageOp(const bool inCoreOnly) : mInCoreOnly(inCoreOnly) {}
303+
MemUsageOp(const MemUsageOp& other) : mCount(0), mInCoreOnly(other.mInCoreOnly) {}
304+
MemUsageOp(const MemUsageOp& other, tbb::split) : MemUsageOp(other) {}
292305

293306
// accumulate size of the root node in bytes
294307
bool operator()(const RootT& root, size_t)
295308
{
296-
count += sizeof(root);
309+
mCount += sizeof(root);
297310
return true;
298311
}
299312

300313
// accumulate size of all child nodes in bytes
301314
template<typename NodeT>
302315
bool operator()(const NodeT& node, size_t)
303316
{
304-
count += NodeT::NUM_VALUES * sizeof(typename NodeT::UnionType) +
317+
mCount += NodeT::NUM_VALUES * sizeof(typename NodeT::UnionType) +
305318
node.getChildMask().memUsage() + node.getValueMask().memUsage() +
306319
sizeof(Coord);
307320
return true;
@@ -310,16 +323,18 @@ struct MemUsageOp
310323
// accumulate size of leaf node in bytes
311324
bool operator()(const LeafT& leaf, size_t)
312325
{
313-
count += leaf.memUsage();
326+
if (mInCoreOnly) mCount += leaf.memUsage();
327+
else mCount += leaf.memUsageIfLoaded();
314328
return false;
315329
}
316330

317331
void join(const MemUsageOp& other)
318332
{
319-
count += other.count;
333+
mCount += other.mCount;
320334
}
321335

322-
openvdb::Index64 count{0};
336+
openvdb::Index64 mCount{0};
337+
const bool mInCoreOnly;
323338
}; // struct MemUsageOp
324339

325340
/// @brief A DynamicNodeManager operator to find the minimum and maximum active values in this tree.
@@ -477,10 +492,24 @@ Index64 countActiveTiles(const TreeT& tree, bool threaded)
477492
template <typename TreeT>
478493
Index64 memUsage(const TreeT& tree, bool threaded)
479494
{
480-
count_internal::MemUsageOp<TreeT> op;
495+
count_internal::MemUsageOp<TreeT> op(true);
496+
tree::DynamicNodeManager<const TreeT> nodeManager(tree);
497+
nodeManager.reduceTopDown(op, threaded);
498+
return op.mCount + sizeof(tree);
499+
}
500+
501+
template <typename TreeT>
502+
Index64 memUsageIfLoaded(const TreeT& tree, bool threaded)
503+
{
504+
/// @note For numeric (non-point) grids this really doesn't need to
505+
/// traverse the tree and could instead be computed from the node counts.
506+
/// We do so anyway as it ties this method into the tree data structure
507+
/// which makes sure that changes to the tree/nodes are reflected/kept in
508+
/// sync here.
509+
count_internal::MemUsageOp<TreeT> op(false);
481510
tree::DynamicNodeManager<const TreeT> nodeManager(tree);
482511
nodeManager.reduceTopDown(op, threaded);
483-
return op.count + sizeof(tree);
512+
return op.mCount + sizeof(tree);
484513
}
485514

486515
template <typename TreeT>

openvdb/openvdb/tools/PointIndexGrid.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,6 +1490,7 @@ struct PointIndexLeafNode : public tree::LeafNode<T, Log2Dim>
14901490

14911491

14921492
Index64 memUsage() const;
1493+
Index64 memUsageIfLoaded() const;
14931494

14941495

14951496
////////////////////////////////////////
@@ -1787,6 +1788,13 @@ PointIndexLeafNode<T, Log2Dim>::memUsage() const
17871788
return BaseLeaf::memUsage() + Index64((sizeof(T)*mIndices.capacity()) + sizeof(mIndices));
17881789
}
17891790

1791+
template<typename T, Index Log2Dim>
1792+
inline Index64
1793+
PointIndexLeafNode<T, Log2Dim>::memUsageIfLoaded() const
1794+
{
1795+
return BaseLeaf::memUsageIfLoaded() + Index64((sizeof(T)*mIndices.capacity()) + sizeof(mIndices));
1796+
}
1797+
17901798
} // namespace tools
17911799

17921800

openvdb/openvdb/tree/LeafBuffer.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class LeafBuffer
8787

8888
/// Return the memory footprint of this buffer in bytes.
8989
inline Index memUsage() const;
90+
inline Index memUsageIfLoaded() const;
9091
/// Return the number of values contained in this buffer.
9192
static Index size() { return SIZE; }
9293

@@ -275,6 +276,16 @@ LeafBuffer<T, Log2Dim>::memUsage() const
275276
}
276277

277278

279+
template<typename T, Index Log2Dim>
280+
inline Index
281+
LeafBuffer<T, Log2Dim>::memUsageIfLoaded() const
282+
{
283+
size_t n = sizeof(*this);
284+
n += SIZE * sizeof(ValueType);
285+
return static_cast<Index>(n);
286+
}
287+
288+
278289
template<typename T, Index Log2Dim>
279290
inline const typename LeafBuffer<T, Log2Dim>::ValueType*
280291
LeafBuffer<T, Log2Dim>::data() const
@@ -425,6 +436,7 @@ class LeafBuffer<bool, Log2Dim>
425436
void swap(LeafBuffer& other) { if (&other != this) std::swap(mData, other.mData); }
426437

427438
Index memUsage() const { return sizeof(*this); }
439+
Index memUsageIfLoaded() const { return sizeof(*this); }
428440
static Index size() { return SIZE; }
429441

430442
/// @brief Return a pointer to the C-style array of words encoding the bits.

openvdb/openvdb/tree/LeafNode.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ class LeafNode
155155

156156
/// Return the memory in bytes occupied by this node.
157157
Index64 memUsage() const;
158+
Index64 memUsageIfLoaded() const;
158159

159160
/// Expand the given bounding box so that it includes this leaf node's active voxels.
160161
/// If visitVoxels is false this LeafNode will be approximated as dense, i.e. with all
@@ -1469,6 +1470,16 @@ LeafNode<T, Log2Dim>::memUsage() const
14691470
}
14701471

14711472

1473+
template<typename T, Index Log2Dim>
1474+
inline Index64
1475+
LeafNode<T, Log2Dim>::memUsageIfLoaded() const
1476+
{
1477+
// Use sizeof(*this) to capture alignment-related padding
1478+
// (but note that sizeof(*this) includes sizeof(mBuffer)).
1479+
return sizeof(*this) + mBuffer.memUsageIfLoaded() - sizeof(mBuffer);
1480+
}
1481+
1482+
14721483
template<typename T, Index Log2Dim>
14731484
inline void
14741485
LeafNode<T, Log2Dim>::evalActiveBoundingBox(CoordBBox& bbox, bool visitVoxels) const

openvdb/openvdb/tree/LeafNodeBool.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ class LeafNode<bool, Log2Dim>
137137

138138
/// Return the memory in bytes occupied by this node.
139139
Index64 memUsage() const;
140+
Index64 memUsageIfLoaded() const;
140141

141142
/// Expand the given bounding box so that it includes this leaf node's active voxels.
142143
/// If visitVoxels is false this LeafNode will be approximated as dense, i.e. with all
@@ -896,6 +897,15 @@ LeafNode<bool, Log2Dim>::memUsage() const
896897
}
897898

898899

900+
template<Index Log2Dim>
901+
inline Index64
902+
LeafNode<bool, Log2Dim>::memUsageIfLoaded() const
903+
{
904+
// Use sizeof(*this) to capture alignment-related padding
905+
return sizeof(*this);
906+
}
907+
908+
899909
template<Index Log2Dim>
900910
inline void
901911
LeafNode<bool, Log2Dim>::evalActiveBoundingBox(CoordBBox& bbox, bool visitVoxels) const

openvdb/openvdb/tree/LeafNodeMask.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ class LeafNode<ValueMask, Log2Dim>
141141

142142
/// Return the memory in bytes occupied by this node.
143143
Index64 memUsage() const;
144+
Index64 memUsageIfLoaded() const;
144145

145146
/// Expand the given bounding box so that it includes this leaf node's active voxels.
146147
/// If visitVoxels is false this LeafNode will be approximated as dense, i.e. with all
@@ -891,6 +892,15 @@ LeafNode<ValueMask, Log2Dim>::memUsage() const
891892
}
892893

893894

895+
template<Index Log2Dim>
896+
inline Index64
897+
LeafNode<ValueMask, Log2Dim>::memUsageIfLoaded() const
898+
{
899+
// Use sizeof(*this) to capture alignment-related padding
900+
return sizeof(*this);
901+
}
902+
903+
894904
template<Index Log2Dim>
895905
inline void
896906
LeafNode<ValueMask, Log2Dim>::evalActiveBoundingBox(CoordBBox& bbox, bool visitVoxels) const

0 commit comments

Comments
 (0)