Add memory usage query for various asset types

This commit is contained in:
Wojciech Figat
2022-12-09 15:48:43 +01:00
parent ed700cb070
commit d4601ecb44
13 changed files with 116 additions and 11 deletions

View File

@@ -86,6 +86,11 @@ public:
{
return Position.GetKeyframes().Count() + Rotation.GetKeyframes().Count() + Scale.GetKeyframes().Count();
}
uint64 GetMemoryUsage() const
{
return NodeName.Length() * sizeof(Char) + Position.GetMemoryUsage() + Rotation.GetMemoryUsage() + Scale.GetMemoryUsage();
}
};
/// <summary>
@@ -131,6 +136,14 @@ public:
return static_cast<float>(Duration / FramesPerSecond);
}
uint64 GetMemoryUsage() const
{
uint64 result = RootNodeName.Length() * sizeof(Char) + Channels.Capacity() * sizeof(NodeAnimationData);
for (const auto& e : Channels)
result += e.GetMemoryUsage();
return result;
}
/// <summary>
/// Gets the total amount of keyframes in the all animation channels.
/// </summary>

View File

@@ -733,6 +733,11 @@ public:
_keyframes[i].Time = _keyframes[i].Time * timeScale + timeOffset;;
}
uint64 GetMemoryUsage() const
{
return _keyframes.Capacity() * sizeof(KeyFrame);
}
public:
FORCE_INLINE KeyFrame& operator[](int32 index)
{

View File

@@ -569,6 +569,23 @@ void Animation::OnSkinnedModelUnloaded(Asset* obj)
MappingCache.Remove(i);
}
uint64 Animation::GetMemoryUsage() const
{
Locker.Lock();
uint64 result = BinaryAsset::GetMemoryUsage();
result += sizeof(Animation) - sizeof(BinaryAsset);
result += Data.GetMemoryUsage();
result += Events.Capacity() * sizeof(Pair<String, StepCurve<AnimEventData>>);
for (const auto& e : Events)
result += e.First.Length() * sizeof(Char) + e.Second.GetMemoryUsage();
result += NestedAnims.Capacity() * sizeof(Pair<String, NestedAnimData>);
result += MappingCache.Capacity() * sizeof(Pair<String, StepCurve<AnimEventData>>);
for (const auto& e : MappingCache)
result += e.Value.Capacity() * sizeof(int32);
Locker.Unlock();
return result;
}
void Animation::OnScriptingDispose()
{
// Dispose any events to prevent crashes (scripting is released before content)

View File

@@ -152,7 +152,6 @@ public:
const NodeToChannel* GetMapping(SkinnedModel* obj);
#if USE_EDITOR
/// <summary>
/// Gets the animation as serialized timeline data. Used to show it in Editor.
/// </summary>
@@ -173,7 +172,6 @@ public:
/// <remarks>The cannot be used by virtual assets.</remarks>
/// <returns><c>true</c> failed to save data; otherwise, <c>false</c>.</returns>
bool Save(const StringView& path = StringView::Empty);
#endif
private:
@@ -181,6 +179,7 @@ private:
public:
// [BinaryAsset]
uint64 GetMemoryUsage() const override;
void OnScriptingDispose() override;
protected:

View File

@@ -64,6 +64,16 @@ bool RawDataAsset::Save(const StringView& path)
#endif
uint64 RawDataAsset::GetMemoryUsage() const
{
Locker.Lock();
uint64 result = BinaryAsset::GetMemoryUsage();
result += sizeof(RawDataAsset) - sizeof(BinaryAsset);
result += Data.Count();
Locker.Unlock();
return result;
}
Asset::LoadResult RawDataAsset::load()
{
auto chunk0 = GetChunk(0);

View File

@@ -28,6 +28,10 @@ public:
#endif
public:
// [BinaryAsset]
uint64 GetMemoryUsage() const override;
protected:
// [BinaryAsset]
LoadResult load() override;

View File

@@ -452,6 +452,22 @@ const String& BinaryAsset::GetPath() const
#endif
}
uint64 BinaryAsset::GetMemoryUsage() const
{
Locker.Lock();
uint64 result = Asset::GetMemoryUsage();
result += sizeof(BinaryAsset) - sizeof(Asset);
result += _dependantAssets.Capacity() * sizeof(BinaryAsset*);
for (int32 i = 0; i < ASSET_FILE_DATA_CHUNKS; i++)
{
auto chunk = _header.Chunks[i];
if (chunk != nullptr && chunk->IsLoaded())
result += chunk->Size();
}
Locker.Unlock();
return result;
}
/// <summary>
/// Helper task used to initialize binary asset and upgrade it if need to in background.
/// </summary>

View File

@@ -41,7 +41,6 @@ public:
FlaxStorage* Storage;
#if USE_EDITOR
/// <summary>
/// The asset metadata information. Stored in a Json format.
/// </summary>
@@ -51,14 +50,12 @@ public:
/// Asset dependencies list used by the asset for tracking (eg. material functions used by material asset). The pair of asset ID and cached file edit time (for tracking modification).
/// </summary>
Array<Pair<Guid, DateTime>> Dependencies;
#endif
public:
/// <summary>
/// Gets the asset serialized version.
/// </summary>
/// <returns>Version number.</returns>
virtual uint32 GetSerializedVersion() const = 0;
/// <summary>
@@ -84,14 +81,11 @@ public:
public:
#if USE_EDITOR
#if COMPILE_WITH_ASSETS_IMPORTER
/// <summary>
/// Reimports asset from the source file.
/// </summary>
API_FUNCTION() void Reimport() const;
#endif
/// <summary>
@@ -130,7 +124,6 @@ protected:
virtual void OnDependencyModified(BinaryAsset* asset)
{
}
#endif
protected:
@@ -255,7 +248,6 @@ public:
bool LoadChunks(AssetChunksFlag chunks);
#if USE_EDITOR
/// <summary>
/// Saves this asset to the storage container.
/// </summary>
@@ -281,7 +273,6 @@ public:
/// <param name="silentMode">In silent mode don't reload opened storage container that is using target file.</param>
/// <returns>True if failed, otherwise false.</returns>
static bool SaveToAsset(const StringView& path, AssetInitData& data, bool silentMode = false);
#endif
protected:
@@ -302,6 +293,7 @@ public:
void OnDeleteObject() override;
#endif
const String& GetPath() const final override;
uint64 GetMemoryUsage() const override;
protected:
// [Asset]

View File

@@ -93,6 +93,17 @@ const String& JsonAssetBase::GetPath() const
#endif
}
uint64 JsonAssetBase::GetMemoryUsage() const
{
Locker.Lock();
uint64 result = Asset::GetMemoryUsage();
result += sizeof(JsonAssetBase) - sizeof(Asset);
if (Data)
result += Document.GetAllocator().Capacity();
Locker.Unlock();
return result;
}
#if USE_EDITOR
void FindIds(ISerializable::DeserializeStream& node, Array<Guid>& output)
@@ -248,6 +259,17 @@ JsonAsset::JsonAsset(const SpawnParams& params, const AssetInfo* info)
{
}
uint64 JsonAsset::GetMemoryUsage() const
{
Locker.Lock();
uint64 result = JsonAssetBase::GetMemoryUsage();
result += sizeof(JsonAsset) - sizeof(JsonAssetBase);
if (Instance && InstanceType)
result += InstanceType.GetType().Size;
Locker.Unlock();
return result;
}
Asset::LoadResult JsonAsset::loadAsset()
{
const auto result = JsonAssetBase::loadAsset();

View File

@@ -77,6 +77,7 @@ public:
public:
// [Asset]
const String& GetPath() const override;
uint64 GetMemoryUsage() const override;
#if USE_EDITOR
void GetReferences(Array<Guid, HeapAllocation>& output) const override;
#endif
@@ -122,6 +123,10 @@ public:
return Instance && InstanceType.IsAssignableFrom(T::TypeInitializer) ? (T*)Instance : nullptr;
}
public:
// [JsonAssetBase]
uint64 GetMemoryUsage() const override;
protected:
// [JsonAssetBase]
LoadResult loadAsset() override;

View File

@@ -636,6 +636,21 @@ bool TextureBase::Init(void* ptr)
return Init(initData);
}
uint64 TextureBase::GetMemoryUsage() const
{
Locker.Lock();
uint64 result = BinaryAsset::GetMemoryUsage();
result += sizeof(TextureBase) - sizeof(BinaryAsset);
if (_customData)
{
result += sizeof(InitData);
for (auto& mip : _customData->Mips)
result += mip.Data.Length();
}
Locker.Unlock();
return result;
}
void TextureBase::CancelStreaming()
{
_texture.CancelStreamingTasks();

View File

@@ -221,6 +221,7 @@ private:
public:
// [BinaryAsset]
uint64 GetMemoryUsage() const override;
void CancelStreaming() override;
// [ITextureOwner]

View File

@@ -2304,6 +2304,12 @@ public:
return *allocator_;
}
//! Get the allocator of this document.
const Allocator& GetAllocator() const {
RAPIDJSON_ASSERT(allocator_);
return *allocator_;
}
//! Get the capacity of stack in bytes.
size_t GetStackCapacity() const { return stack_.GetCapacity(); }