Bläddra i källkod

Print memory use for all resources in ResourceCache::PrintMemoryUsage(). Change memory budget & use API to use unsigned long long (64bit). Expose GetFileSizeString() to script. Refactor Engine to use PrintMemoryUsage(). Match width of profiler and resource outputs. Rename Profiler::GetData() to Profiler::PrintData() to match the new ResourceCache function.

Lasse Öörni 10 år sedan
förälder
incheckning
6f60ee7f48

+ 1 - 0
Source/Urho3D/AngelScript/IOAPI.cpp

@@ -351,6 +351,7 @@ void RegisterFileSystem(asIScriptEngine* engine)
     engine->RegisterGlobalFunction("String GetParentPath(const String&in)", asFUNCTION(GetParentPath), asCALL_CDECL);
     engine->RegisterGlobalFunction("String GetInternalPath(const String&in)", asFUNCTION(GetInternalPath), asCALL_CDECL);
     engine->RegisterGlobalFunction("bool IsAbsolutePath(const String&in)", asFUNCTION(IsAbsolutePath), asCALL_CDECL);
+    engine->RegisterGlobalFunction("String GetFileSizeString(uint64)", asFUNCTION(GetFileSizeString), asCALL_CDECL);
 }
 
 static PackageFile* ConstructPackageFile()

+ 7 - 7
Source/Urho3D/AngelScript/ResourceAPI.cpp

@@ -69,17 +69,17 @@ static void ResourceCacheReleaseResourcesPartial(const String& type, const Strin
     ptr->ReleaseResources(type, partialName, force);
 }
 
-static void ResourceCacheSetMemoryBudget(const String& type, unsigned budget, ResourceCache* ptr)
+static void ResourceCacheSetMemoryBudget(const String& type, unsigned long long budget, ResourceCache* ptr)
 {
     ptr->SetMemoryBudget(type, budget);
 }
 
-static unsigned ResourceCacheGetMemoryBudget(const String& type, ResourceCache* ptr)
+static unsigned long long ResourceCacheGetMemoryBudget(const String& type, ResourceCache* ptr)
 {
     return ptr->GetMemoryBudget(type);
 }
 
-static unsigned ResourceCacheGetMemoryUse(const String& type, ResourceCache* ptr)
+static unsigned long long ResourceCacheGetMemoryUse(const String& type, ResourceCache* ptr)
 {
     return ptr->GetMemoryUse(type);
 }
@@ -154,10 +154,10 @@ static void RegisterResourceCache(asIScriptEngine* engine)
     engine->RegisterObjectMethod("ResourceCache", "Resource@+ GetExistingResource(const String&in, const String&in)", asFUNCTION(ResourceCacheGetExistingResource), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectMethod("ResourceCache", "Resource@+ GetExistingResource(StringHash, const String&in)", asMETHODPR(ResourceCache, GetExistingResource, (StringHash, const String&), Resource*), asCALL_THISCALL);
     engine->RegisterObjectMethod("ResourceCache", "bool BackgroundLoadResource(const String&in, const String&in, bool sendEventOnFailure = true)", asFUNCTION(ResourceCacheBackgroundLoadResource), asCALL_CDECL_OBJLAST);
-    engine->RegisterObjectMethod("ResourceCache", "void set_memoryBudget(const String&in, uint)", asFUNCTION(ResourceCacheSetMemoryBudget), asCALL_CDECL_OBJLAST);
-    engine->RegisterObjectMethod("ResourceCache", "uint get_memoryBudget(const String&in) const", asFUNCTION(ResourceCacheGetMemoryBudget), asCALL_CDECL_OBJLAST);
-    engine->RegisterObjectMethod("ResourceCache", "uint get_memoryUse(const String&in) const", asFUNCTION(ResourceCacheGetMemoryUse), asCALL_CDECL_OBJLAST);
-    engine->RegisterObjectMethod("ResourceCache", "uint get_totalMemoryUse() const", asMETHOD(ResourceCache, GetTotalMemoryUse), asCALL_THISCALL);
+    engine->RegisterObjectMethod("ResourceCache", "void set_memoryBudget(const String&in, uint64)", asFUNCTION(ResourceCacheSetMemoryBudget), asCALL_CDECL_OBJLAST);
+    engine->RegisterObjectMethod("ResourceCache", "uint64 get_memoryBudget(const String&in) const", asFUNCTION(ResourceCacheGetMemoryBudget), asCALL_CDECL_OBJLAST);
+    engine->RegisterObjectMethod("ResourceCache", "uint64 get_memoryUse(const String&in) const", asFUNCTION(ResourceCacheGetMemoryUse), asCALL_CDECL_OBJLAST);
+    engine->RegisterObjectMethod("ResourceCache", "uint64 get_totalMemoryUse() const", asMETHOD(ResourceCache, GetTotalMemoryUse), asCALL_THISCALL);
     engine->RegisterObjectMethod("ResourceCache", "Array<String>@ get_resourceDirs() const", asFUNCTION(ResourceCacheGetResourceDirs), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectMethod("ResourceCache", "Array<PackageFile@>@ get_packageFiles() const", asFUNCTION(ResourceCacheGetPackageFiles), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectMethod("ResourceCache", "void set_searchPackagesFirst(bool)", asMETHOD(ResourceCache, SetSearchPackagesFirst), asCALL_THISCALL);

+ 4 - 4
Source/Urho3D/Core/Profiler.cpp

@@ -80,7 +80,7 @@ void Profiler::BeginInterval()
     intervalFrames_ = 0;
 }
 
-String Profiler::GetData(bool showUnused, bool showTotal, unsigned maxDepth) const
+String Profiler::PrintData(bool showUnused, bool showTotal, unsigned maxDepth) const
 {
     String output;
 
@@ -95,12 +95,12 @@ String Profiler::GetData(bool showUnused, bool showTotal, unsigned maxDepth) con
     if (!maxDepth)
         maxDepth = 1;
 
-    GetData(root_, output, 0, maxDepth, showUnused, showTotal);
+    PrintData(root_, output, 0, maxDepth, showUnused, showTotal);
 
     return output;
 }
 
-void Profiler::GetData(ProfilerBlock* block, String& output, unsigned depth, unsigned maxDepth, bool showUnused,
+void Profiler::PrintData(ProfilerBlock* block, String& output, unsigned depth, unsigned maxDepth, bool showUnused,
     bool showTotal) const
 {
     char line[LINE_MAX_LENGTH];
@@ -153,7 +153,7 @@ void Profiler::GetData(ProfilerBlock* block, String& output, unsigned depth, uns
     }
 
     for (PODVector<ProfilerBlock*>::ConstIterator i = block->children_.Begin(); i != block->children_.End(); ++i)
-        GetData(*i, output, depth, maxDepth, showUnused, showTotal);
+        PrintData(*i, output, depth, maxDepth, showUnused, showTotal);
 }
 
 }

+ 2 - 2
Source/Urho3D/Core/Profiler.h

@@ -211,7 +211,7 @@ public:
     void BeginInterval();
     
     /// Return profiling data as text output.
-    String GetData(bool showUnused = false, bool showTotal = false, unsigned maxDepth = M_MAX_UNSIGNED) const;
+    String PrintData(bool showUnused = false, bool showTotal = false, unsigned maxDepth = M_MAX_UNSIGNED) const;
     /// Return the current profiling block.
     const ProfilerBlock* GetCurrentBlock() { return current_; }
     /// Return the root profiling block.
@@ -219,7 +219,7 @@ public:
     
 private:
     /// Return profiling data as text output for a specified profiling block.
-    void GetData(ProfilerBlock* block, String& output, unsigned depth, unsigned maxDepth, bool showUnused, bool showTotal) const;
+    void PrintData(ProfilerBlock* block, String& output, unsigned depth, unsigned maxDepth, bool showUnused, bool showTotal) const;
     
     /// Current profiling block.
     ProfilerBlock* current_;

+ 1 - 1
Source/Urho3D/Engine/DebugHud.cpp

@@ -175,7 +175,7 @@ void DebugHud::Update()
 
             if (profilerText_->IsVisible())
             {
-                String profilerOutput = profiler->GetData(false, false, profilerMaxDepth_);
+                String profilerOutput = profiler->PrintData(false, false, profilerMaxDepth_);
                 profilerText_->SetText(profilerOutput);
             }
 

+ 10 - 28
Source/Urho3D/Engine/Engine.cpp

@@ -544,7 +544,7 @@ void Engine::DumpProfiler()
 {
     Profiler* profiler = GetSubsystem<Profiler>();
     if (profiler)
-        LOGRAW(profiler->GetData(true, true) + "\n");
+        LOGRAW(profiler->PrintData(true, true) + "\n");
 }
 
 void Engine::DumpResources(bool dumpFileName)
@@ -557,38 +557,20 @@ void Engine::DumpResources(bool dumpFileName)
     if (dumpFileName)
     {
         LOGRAW("Used resources:\n");
-    }
-
-    for (HashMap<StringHash, ResourceGroup>::ConstIterator i = resourceGroups.Begin();
-         i != resourceGroups.End(); ++i)
-    {
-        const HashMap<StringHash, SharedPtr<Resource> >& resources = i->second_.resources_;
-        if (dumpFileName)
-        {
-            for (HashMap<StringHash, SharedPtr<Resource> >::ConstIterator j = resources.Begin();
-                 j != resources.End(); ++j)
-            {
-                LOGRAW(j->second_->GetName() + "\n");
-            }
-
-        }
-        else
+        for (HashMap<StringHash, ResourceGroup>::ConstIterator i = resourceGroups.Begin();
+            i != resourceGroups.End(); ++i)
         {
-            unsigned num = resources.Size();
-            unsigned memoryUse = i->second_.memoryUse_;
-
-            if (num)
+            const HashMap<StringHash, SharedPtr<Resource> >& resources = i->second_.resources_;
+            if (dumpFileName)
             {
-                LOGRAW("Resource type " + resources.Begin()->second_->GetTypeName() +
-                       ": count " + String(num) + " memory use " + String(memoryUse) + "\n");
+                for (HashMap<StringHash, SharedPtr<Resource> >::ConstIterator j = resources.Begin();
+                    j != resources.End(); ++j)
+                    LOGRAW(j->second_->GetName() + "\n");
             }
         }
     }
-
-    if (!dumpFileName)
-    {
-        LOGRAW("Total memory use of all resources " + String(cache->GetTotalMemoryUse()) + "\n\n");
-    }
+    else
+        LOGRAW(cache->PrintMemoryUsage());
 #endif
 }
 

+ 2 - 2
Source/Urho3D/Graphics/Direct3D11/D3D11Texture.cpp

@@ -429,8 +429,8 @@ unsigned Texture::GetSRGBFormat(unsigned format)
 void Texture::CheckTextureBudget(StringHash type)
 {
     ResourceCache* cache = GetSubsystem<ResourceCache>();
-    unsigned textureBudget = cache->GetMemoryBudget(type);
-    unsigned textureUse = cache->GetMemoryUse(type);
+    unsigned long long textureBudget = cache->GetMemoryBudget(type);
+    unsigned long long textureUse = cache->GetMemoryUse(type);
     if (!textureBudget)
         return;
 

+ 2 - 2
Source/Urho3D/Graphics/Direct3D9/D3D9Texture.cpp

@@ -302,8 +302,8 @@ void Texture::SetParameters(const XMLElement& element)
 void Texture::CheckTextureBudget(StringHash type)
 {
     ResourceCache* cache = GetSubsystem<ResourceCache>();
-    unsigned textureBudget = cache->GetMemoryBudget(type);
-    unsigned textureUse = cache->GetMemoryUse(type);
+    unsigned long long textureBudget = cache->GetMemoryBudget(type);
+    unsigned long long textureUse = cache->GetMemoryUse(type);
     if (!textureBudget)
         return;
 

+ 2 - 2
Source/Urho3D/Graphics/OpenGL/OGLTexture.cpp

@@ -514,8 +514,8 @@ unsigned Texture::GetSRGBFormat(unsigned format)
 void Texture::CheckTextureBudget(StringHash type)
 {
     ResourceCache* cache = GetSubsystem<ResourceCache>();
-    unsigned textureBudget = cache->GetMemoryBudget(type);
-    unsigned textureUse = cache->GetMemoryUse(type);
+    unsigned long long textureBudget = cache->GetMemoryBudget(type);
+    unsigned long long textureUse = cache->GetMemoryUse(type);
     if (!textureBudget)
         return;
 

+ 1 - 1
Source/Urho3D/IO/FileSystem.cpp

@@ -1069,7 +1069,7 @@ bool IsAbsolutePath(const String& pathName)
 }
 
 /// Convert a memory size into a formatted size string, of the style "1.5 Mb"
-String GetFileSizeString(unsigned long memorySize)
+String GetFileSizeString(unsigned long long memorySize)
 {
     const char* memorySizeStrings = "kMGTPE";
 

+ 1 - 1
Source/Urho3D/IO/FileSystem.h

@@ -151,6 +151,6 @@ URHO3D_API WString GetWideNativePath(const String& pathName);
 /// Return whether a path is absolute.
 URHO3D_API bool IsAbsolutePath(const String& pathName);
 /// Convert a memory size into a formatted size string, of the style "1.5 Mb"
-URHO3D_API String GetFileSizeString(unsigned long memorySize);
+URHO3D_API String GetFileSizeString(unsigned long long memorySize);
 
 }

+ 1 - 0
Source/Urho3D/LuaScript/pkgs/IO/FileSystem.pkg

@@ -44,6 +44,7 @@ String GetParentPath(const String pathName);
 String GetInternalPath(const String pathName);
 String GetNativePath(const String pathName);
 bool IsAbsolutePath(const String pathName);
+String GetFileSizeString(unsigned long long memorySize);
 
 FileSystem * GetFileSystem();
 tolua_readonly tolua_property__get_set FileSystem* fileSystem;

+ 6 - 6
Source/Urho3D/LuaScript/pkgs/Resource/ResourceCache.pkg

@@ -6,8 +6,8 @@ class ResourceCache
     bool ReloadResource(Resource* resource);
     void ReloadResourceWithDependencies(const String fileName);
 
-    void SetMemoryBudget(StringHash type, unsigned budget);
-    void SetMemoryBudget(const String type, unsigned budget);
+    void SetMemoryBudget(StringHash type, unsigned long long budget);
+    void SetMemoryBudget(const String type, unsigned long long budget);
     
     void SetAutoReloadResources(bool enable);
     void SetReturnFailedResources(bool enable);
@@ -23,9 +23,9 @@ class ResourceCache
     const Vector<String>& GetResourceDirs() const;
 
     bool Exists(const String name) const;
-    unsigned GetMemoryBudget(StringHash type) const;
-    unsigned GetMemoryUse(StringHash type) const;
-    unsigned GetTotalMemoryUse() const;
+    unsigned long long GetMemoryBudget(StringHash type) const;
+    unsigned long long GetMemoryUse(StringHash type) const;
+    unsigned long long GetTotalMemoryUse() const;
     String GetResourceFileName(const String name) const;
 
     bool GetAutoReloadResources() const;
@@ -37,7 +37,7 @@ class ResourceCache
     String SanitateResourceName(const String name) const;
     String SanitateResourceDirName(const String name) const;
 
-    tolua_readonly tolua_property__get_set unsigned totalMemoryUse;
+    tolua_readonly tolua_property__get_set unsigned long long totalMemoryUse;
     tolua_property__get_set bool autoReloadResources;
     tolua_property__get_set bool returnFailedResources;
     tolua_property__get_set bool searchPackagesFirst;

+ 34 - 18
Source/Urho3D/Resource/ResourceCache.cpp

@@ -432,7 +432,7 @@ void ResourceCache::ReloadResourceWithDependencies(const String& fileName)
     }
 }
 
-void ResourceCache::SetMemoryBudget(StringHash type, unsigned budget)
+void ResourceCache::SetMemoryBudget(StringHash type, unsigned long long budget)
 {
     resourceGroups_[type].memoryBudget_ = budget;
 }
@@ -761,21 +761,21 @@ bool ResourceCache::Exists(const String& nameIn) const
     return fileSystem->FileExists(name);
 }
 
-unsigned ResourceCache::GetMemoryBudget(StringHash type) const
+unsigned long long ResourceCache::GetMemoryBudget(StringHash type) const
 {
     HashMap<StringHash, ResourceGroup>::ConstIterator i = resourceGroups_.Find(type);
     return i != resourceGroups_.End() ? i->second_.memoryBudget_ : 0;
 }
 
-unsigned ResourceCache::GetMemoryUse(StringHash type) const
+unsigned long long ResourceCache::GetMemoryUse(StringHash type) const
 {
     HashMap<StringHash, ResourceGroup>::ConstIterator i = resourceGroups_.Find(type);
     return i != resourceGroups_.End() ? i->second_.memoryUse_ : 0;
 }
 
-unsigned ResourceCache::GetTotalMemoryUse() const
+unsigned long long ResourceCache::GetTotalMemoryUse() const
 {
-    unsigned total = 0;
+    unsigned long long total = 0;
     for (HashMap<StringHash, ResourceGroup>::ConstIterator i = resourceGroups_.Begin(); i != resourceGroups_.End(); ++i)
         total += i->second_.memoryUse_;
     return total;
@@ -913,44 +913,60 @@ void ResourceCache::ResetDependencies(Resource* resource)
 
 String ResourceCache::PrintMemoryUsage() const
 {
-    String output = "Resource Type                            Cnt     Avg      Max     Budget    Total\n\n";
+    String output = "Resource Type                 Cnt       Avg       Max    Budget     Total\n\n";
+    char outputLine[256];
+
+    unsigned totalResourceCt = 0;
+    unsigned long long totalLargest = 0;
+    unsigned long long totalAverage = 0;
+    unsigned long long totalUse = GetTotalMemoryUse();
 
     for (HashMap<StringHash, ResourceGroup>::ConstIterator cit = resourceGroups_.Begin(); cit != resourceGroups_.End(); ++cit)
-    {        
+    {
         const unsigned resourceCt = cit->second_.resources_.Size();
-        unsigned average = 0;
+        unsigned long long average = 0;
         if (resourceCt > 0)
             average = cit->second_.memoryUse_ / resourceCt;
         else
             average = 0;
-        unsigned long largest = 0;
+        unsigned long long largest = 0;
         for (HashMap<StringHash, SharedPtr<Resource> >::ConstIterator resIt = cit->second_.resources_.Begin(); resIt != cit->second_.resources_.End(); ++resIt)
         {
             if (resIt->second_->GetMemoryUse() > largest)
                 largest = resIt->second_->GetMemoryUse();
+            if (largest > totalLargest)
+                totalLargest = largest;
         }
         
-        char outputLine[256];
-        memset(outputLine, ' ', 256);
-        outputLine[255] = 0;
+        totalResourceCt += resourceCt;
         
         const String countString(cit->second_.resources_.Size());
-
         const String memUseString = GetFileSizeString(average);
-
         const String memMaxString = GetFileSizeString(largest);
-
         const String memBudgetString = GetFileSizeString(cit->second_.memoryBudget_);
-
         const String memTotalString = GetFileSizeString(cit->second_.memoryUse_);
-
         const String resTypeName = context_->GetTypeName(cit->first_);
 
-        sprintf(outputLine, "%-38s %4s %9s %9s %9s %9s\n", resTypeName.CString(), countString.CString(), memUseString.CString(), memMaxString.CString(), memBudgetString.CString(), memTotalString.CString());
+        memset(outputLine, ' ', 256);
+        outputLine[255] = 0;
+        sprintf(outputLine, "%-28s %4s %9s %9s %9s %9s\n", resTypeName.CString(), countString.CString(), memUseString.CString(), memMaxString.CString(), memBudgetString.CString(), memTotalString.CString());
 
         output += ((const char*)outputLine);
     }
 
+    if (totalResourceCt > 0)
+        totalAverage = totalUse / totalResourceCt;
+
+    const String countString(totalResourceCt);
+    const String memUseString = GetFileSizeString(totalAverage);
+    const String memMaxString = GetFileSizeString(totalLargest);
+    const String memTotalString = GetFileSizeString(totalUse);
+
+    memset(outputLine, ' ', 256);
+    outputLine[255] = 0;
+    sprintf(outputLine, "%-28s %4s %9s %9s %9s %9s\n", "All", countString.CString(), memUseString.CString(), memMaxString.CString(), "-", memTotalString.CString());
+    output += ((const char*)outputLine);
+
     return output;
 }
 

+ 6 - 6
Source/Urho3D/Resource/ResourceCache.h

@@ -49,9 +49,9 @@ struct ResourceGroup
     }
 
     /// Memory budget.
-    unsigned memoryBudget_;
+    unsigned long long memoryBudget_;
     /// Current memory use.
-    unsigned memoryUse_;
+    unsigned long long memoryUse_;
     /// Resources.
     HashMap<StringHash, SharedPtr<Resource> > resources_;
 };
@@ -117,7 +117,7 @@ public:
     /// Reload a resource based on filename. Causes also reload of dependent resources if necessary.
     void ReloadResourceWithDependencies(const String& fileName);
     /// Set memory budget for a specific resource type, default 0 is unlimited.
-    void SetMemoryBudget(StringHash type, unsigned budget);
+    void SetMemoryBudget(StringHash type, unsigned long long budget);
     /// Enable or disable automatic reloading of resources as files are modified. Default false.
     void SetAutoReloadResources(bool enable);
     /// Enable or disable returning resources that failed to load. Default false. This may be useful in editing to not lose resource ref attributes.
@@ -171,11 +171,11 @@ public:
     /// Return whether a file exists by name.
     bool Exists(const String& name) const;
     /// Return memory budget for a resource type.
-    unsigned GetMemoryBudget(StringHash type) const;
+    unsigned long long GetMemoryBudget(StringHash type) const;
     /// Return total memory use for a resource type.
-    unsigned GetMemoryUse(StringHash type) const;
+    unsigned long long GetMemoryUse(StringHash type) const;
     /// Return total memory use for all resources.
-    unsigned GetTotalMemoryUse() const;
+    unsigned long long GetTotalMemoryUse() const;
     /// Return full absolute file name of resource if possible.
     String GetResourceFileName(const String& name) const;