/**************************************************************************** Copyright (c) 2007, Radon Labs GmbH Copyright (c) 2011-2013,WebJet Business Division,CYOU http://www.genesis-3d.com.cn Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ #include "stdneb.h" #include "memory/debug/memorypagehandler.h" #include "memory/heap.h" #include "http/html/htmlpagewriter.h" #include "memory/poolarrayallocator.h" namespace Debug { __ImplementClass(Debug::MemoryPageHandler, 'MPGH', Http::HttpRequestHandler); using namespace IO; using namespace Http; using namespace Util; using namespace Memory; //------------------------------------------------------------------------------ /** */ MemoryPageHandler::MemoryPageHandler() { this->SetName("Memory"); this->SetDesc("show memory debug information"); this->SetRootLocation("memory"); } //------------------------------------------------------------------------------ /** */ void MemoryPageHandler::HandleRequest(const GPtr& request) { n_assert(HttpMethod::Get == request->GetMethod()); // configure a HTML page writer GPtr htmlWriter = HtmlPageWriter::Create(); htmlWriter->SetStream(request->GetResponseContentStream()); htmlWriter->SetTitle("Genesis Memory Info"); if (htmlWriter->Open()) { htmlWriter->Element(HtmlElement::Heading1, "Memory"); htmlWriter->AddAttr("href", "/index.html"); htmlWriter->Element(HtmlElement::Anchor, "Home"); htmlWriter->LineBreak(); htmlWriter->LineBreak(); // if not compiled with NEBULA3_MEMORY_STATS, display a message #if (NEBULA3_MEMORY_STATS == 0) htmlWriter->Text("Memory stats not available because NEBULA3_MEMORY_STATS was not defined " "when application was compiled. Go to /foundation/core/config.h, change " "NEBULA3_MEMORY_STATS to (1) and recompile the application!"); #else htmlWriter->Text("This includes all allocations that go through the Memory subsystem " "functions (this includes the creation of all Nebula objects). Other memory " "allocations such as DirectX resources are NOT included!"); htmlWriter->LineBreak(); htmlWriter->LineBreak(); // display overall stats Array heapStats = Heap::GetAllHeapStats(); int heapAllocCount = 0; int heapAllocSize = 0; IndexT i; for (i = 0; i < heapStats.Size(); i++) { heapAllocCount += heapStats[i].allocCount; heapAllocSize += heapStats[i].allocSize; }; htmlWriter->Element(HtmlElement::Heading3, "Genesis Overall Stats"); htmlWriter->Begin(HtmlElement::Table); htmlWriter->Begin(HtmlElement::TableRow); htmlWriter->Element(HtmlElement::TableData, "Genesis Global Heaps Alloc Count: "); htmlWriter->Element(HtmlElement::TableData, String::FromInt(Memory::TotalAllocCount)); htmlWriter->End(HtmlElement::TableRow); htmlWriter->Begin(HtmlElement::TableRow); htmlWriter->Element(HtmlElement::TableData, "Genesis Global Heaps Alloc Size: "); htmlWriter->Element(HtmlElement::TableData, String::FromInt(Memory::TotalAllocSize) + " bytes"); htmlWriter->End(HtmlElement::TableRow); htmlWriter->Begin(HtmlElement::TableRow); htmlWriter->Element(HtmlElement::TableData, "Genesis Local Heaps Alloc Count: "); htmlWriter->Element(HtmlElement::TableData, String::FromInt(heapAllocCount)); htmlWriter->End(HtmlElement::TableRow); htmlWriter->Begin(HtmlElement::TableRow); htmlWriter->Element(HtmlElement::TableData, "Genesis Local Heaps Alloc Size: "); htmlWriter->Element(HtmlElement::TableData, String::FromInt(heapAllocSize) + " bytes"); htmlWriter->End(HtmlElement::TableRow); htmlWriter->Begin(HtmlElement::TableRow); htmlWriter->Element(HtmlElement::TableData, "Genesis Overall Alloc Count: "); htmlWriter->Element(HtmlElement::TableData, String::FromInt(heapAllocCount + Memory::TotalAllocCount)); htmlWriter->End(HtmlElement::TableRow); htmlWriter->Begin(HtmlElement::TableRow); htmlWriter->Element(HtmlElement::TableData, "Genesis Overall Alloc Size: "); htmlWriter->Element(HtmlElement::TableData, String::FromInt(heapAllocSize + Memory::TotalAllocSize) + " bytes"); htmlWriter->End(HtmlElement::TableRow); htmlWriter->End(HtmlElement::Table); // lowlevel system stats Memory::TotalMemoryStatus globalStats = Memory::GetTotalMemoryStatus(); const int mega = 1024 * 1024; htmlWriter->Element(HtmlElement::Heading3, "System Memory Stats"); htmlWriter->Begin(HtmlElement::Table); htmlWriter->Begin(HtmlElement::TableRow); htmlWriter->Element(HtmlElement::TableData, "Total Physical Memory:"); htmlWriter->Element(HtmlElement::TableData, String::FromInt(globalStats.totalPhysical / mega) + String(" MB")); htmlWriter->End(HtmlElement::TableRow); htmlWriter->Begin(HtmlElement::TableRow); htmlWriter->Element(HtmlElement::TableData, "Available Physical Memory:"); htmlWriter->Element(HtmlElement::TableData, String::FromInt(globalStats.availPhysical / mega) + String(" MB")); htmlWriter->End(HtmlElement::TableRow); htmlWriter->Begin(HtmlElement::TableRow); htmlWriter->Element(HtmlElement::TableData, "Total Virtual Memory:"); htmlWriter->Element(HtmlElement::TableData, String::FromInt(globalStats.totalVirtual / mega) + String(" MB")); htmlWriter->End(HtmlElement::TableRow); htmlWriter->Begin(HtmlElement::TableRow); htmlWriter->Element(HtmlElement::TableData, "Available Virtual Memory:"); htmlWriter->Element(HtmlElement::TableData, String::FromInt(globalStats.availVirtual / mega) + String(" MB")); htmlWriter->End(HtmlElement::TableRow); htmlWriter->End(HtmlElement::Table); // display global heaps info htmlWriter->Element(HtmlElement::Heading3, "Global Heap Stats"); htmlWriter->AddAttr("border", "1"); htmlWriter->AddAttr("rules", "cols"); htmlWriter->Begin(HtmlElement::Table); htmlWriter->AddAttr("bgcolor", "lightsteelblue"); htmlWriter->Begin(HtmlElement::TableRow); htmlWriter->Element(HtmlElement::TableHeader, "Heap Name"); htmlWriter->Element(HtmlElement::TableHeader, "Alloc Count"); htmlWriter->Element(HtmlElement::TableHeader, "Alloc Size"); htmlWriter->End(HtmlElement::TableRow); for (i = 0; i < Memory::NumHeapTypes; i++) { htmlWriter->Begin(HtmlElement::TableRow); htmlWriter->Element(HtmlElement::TableData, Memory::GetHeapTypeName((Memory::HeapType)i)); htmlWriter->Element(HtmlElement::TableData, String::FromInt(Memory::HeapTypeAllocCount[i])); htmlWriter->Element(HtmlElement::TableData, String::FromInt(Memory::HeapTypeAllocSize[i])); htmlWriter->End(HtmlElement::TableRow); } htmlWriter->End(HtmlElement::Table); // display local heaps info htmlWriter->Element(HtmlElement::Heading3, "Local Heap Stats"); htmlWriter->AddAttr("border", "1"); htmlWriter->AddAttr("rules", "cols"); htmlWriter->Begin(HtmlElement::Table); htmlWriter->AddAttr("bgcolor", "lightsteelblue"); htmlWriter->Begin(HtmlElement::TableRow); htmlWriter->Element(HtmlElement::TableHeader, "Heap Name"); htmlWriter->Element(HtmlElement::TableHeader, "Alloc Count"); htmlWriter->Element(HtmlElement::TableHeader, "Alloc Size"); htmlWriter->End(HtmlElement::TableRow); for (i = 0; i < heapStats.Size(); i++) { htmlWriter->Begin(HtmlElement::TableRow); htmlWriter->Element(HtmlElement::TableData, heapStats[i].name); htmlWriter->Element(HtmlElement::TableData, String::FromInt(heapStats[i].allocCount)); htmlWriter->Element(HtmlElement::TableData, String::FromInt(heapStats[i].allocSize)); htmlWriter->End(HtmlElement::TableRow); } htmlWriter->End(HtmlElement::Table); // dump RefCounted pool allocator stats #if NEBULA3_OBJECTS_USE_MEMORYPOOL htmlWriter->Element(HtmlElement::Heading3, "Object PoolArrayAllocator Stats"); htmlWriter->AddAttr("border", "1"); htmlWriter->AddAttr("rules", "cols"); htmlWriter->Begin(HtmlElement::Table); htmlWriter->AddAttr("bgcolor", "lightsteelblue"); htmlWriter->Begin(HtmlElement::TableRow); htmlWriter->Element(HtmlElement::TableHeader, " Block Size "); htmlWriter->Element(HtmlElement::TableHeader, " Max Blocks "); htmlWriter->Element(HtmlElement::TableHeader, " Num Blocks "); htmlWriter->Element(HtmlElement::TableHeader, " Percent Full "); htmlWriter->End(HtmlElement::TableRow); for (i = 0; i < PoolArrayAllocator::NumPools; i++) { const MemoryPool& pool = ObjectPoolAllocator->GetMemoryPool(i); uint blockSize = pool.GetBlockSize(); uint numBlocks = pool.GetNumBlocks(); uint allocCount = pool.GetAllocCount(); uint percent = (allocCount * 100) / numBlocks; if (percent < 50) { // orange if too few allocations htmlWriter->AddAttr("bgcolor", "orange"); } else if (percent < 75) { // green if just right htmlWriter->AddAttr("bgcolor", "green"); } else { // red if almost full htmlWriter->AddAttr("bgcolor", "red"); } htmlWriter->Begin(HtmlElement::TableRow); htmlWriter->Element(HtmlElement::TableData, String::FromInt(blockSize)); htmlWriter->Element(HtmlElement::TableData, String::FromInt(numBlocks)); htmlWriter->Element(HtmlElement::TableData, String::FromInt(allocCount)); String percentStr; percentStr.Format("%d %%", percent); htmlWriter->Element(HtmlElement::TableData, String::FromInt(percent)); htmlWriter->End(HtmlElement::TableRow); } htmlWriter->End(HtmlElement::Table); #endif // NEBULA3_OBJECTS_USE_MEMORYPOOL #endif // NEBULA3_MEMORY_STATS htmlWriter->Close(); request->SetStatus(HttpStatus::OK); } else { request->SetStatus(HttpStatus::InternalServerError); } } } // namespace Debug