From 567df6aacfe4c35a979c178b8c7ffd7c19037a4f Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Sat, 6 Jul 2013 19:44:54 +0000 Subject: MemDumpAnalysis: moved into the Tools folder; uses the global Expat library now. git-svn-id: http://mc-server.googlecode.com/svn/trunk@1656 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- Tools/MemDumpAnalysis/Globals.cpp | 10 + Tools/MemDumpAnalysis/Globals.h | 35 ++ Tools/MemDumpAnalysis/MemDumpAnalysis.cpp | 321 ++++++++++++++++ Tools/MemDumpAnalysis/MemDumpAnalysis.sln | 20 + Tools/MemDumpAnalysis/MemDumpAnalysis.vcproj | 439 ++++++++++++++++++++++ Tools/MemDumpAnalysis/MemDumpAnalysis.vcproj.user | 31 ++ Tools/MemDumpAnalysis/targetver.h | 25 ++ 7 files changed, 881 insertions(+) create mode 100644 Tools/MemDumpAnalysis/Globals.cpp create mode 100644 Tools/MemDumpAnalysis/Globals.h create mode 100644 Tools/MemDumpAnalysis/MemDumpAnalysis.cpp create mode 100644 Tools/MemDumpAnalysis/MemDumpAnalysis.sln create mode 100644 Tools/MemDumpAnalysis/MemDumpAnalysis.vcproj create mode 100644 Tools/MemDumpAnalysis/MemDumpAnalysis.vcproj.user create mode 100644 Tools/MemDumpAnalysis/targetver.h (limited to 'Tools') diff --git a/Tools/MemDumpAnalysis/Globals.cpp b/Tools/MemDumpAnalysis/Globals.cpp new file mode 100644 index 000000000..6ec09efda --- /dev/null +++ b/Tools/MemDumpAnalysis/Globals.cpp @@ -0,0 +1,10 @@ + +// Globals.cpp + +// Used for precompiled header generation + +#include "Globals.h" + + + + diff --git a/Tools/MemDumpAnalysis/Globals.h b/Tools/MemDumpAnalysis/Globals.h new file mode 100644 index 000000000..5208a9fa5 --- /dev/null +++ b/Tools/MemDumpAnalysis/Globals.h @@ -0,0 +1,35 @@ + +// Globals.h + +// Used for precompiled header generation + + + + + +#pragma once + +#include "../../source/Globals.h" + +/* +// System headers: +#include "targetver.h" +#include + +// STL headers: +#include +#include +#include +#include + +// Common: +#include "../source/StringUtils.h" +#include "../source/OSSupport/File.h" +*/ + +// Libraries: +#include "../../expat/expat.h" + + + + diff --git a/Tools/MemDumpAnalysis/MemDumpAnalysis.cpp b/Tools/MemDumpAnalysis/MemDumpAnalysis.cpp new file mode 100644 index 000000000..9e7759085 --- /dev/null +++ b/Tools/MemDumpAnalysis/MemDumpAnalysis.cpp @@ -0,0 +1,321 @@ + +// MemDumpAnalysis.cpp + +// Defines the entry point for the console application. + +#include "Globals.h" + +#ifdef _WIN32 + #pragma comment(lib, "ws2_32.lib") // Needed for StringUtils' RawBEToUtf8() et al. +#endif // _WIN32 + + + + + +typedef std::set AStringSet; + + + + + +class cFunction +{ +public: + int m_Size; ///< Sum of memory block sizes allocated by this function or its children + int m_Count; ///< Total number of memory blocks allocated by this function or its children + AStringSet m_ChildrenNames; + + cFunction(void) : + m_Size(0), + m_Count(0) + { + } +} ; + +typedef std::map FunctionMap; + + + + + +int g_CurrentID = 0; +int g_CurrentSize = 0; +FunctionMap g_FnMap; +AString g_PrevFunctionName; + + + + + +bool IsFnBlackListed(const char * a_FnName) +{ + static const char * BlackList[] = + { + "MyAllocHook", + "_heap_alloc_dbg_impl", + "_nh_malloc_dbg_impl", + "_nh_malloc_dbg", + "malloc", + "operator new", + "_malloc_dbg", + "realloc_help", + "_realloc_dbg", + "realloc", + "l_alloc", + "luaM_realloc_", + "", + } ; + + for (int i = 0; i < ARRAYCOUNT(BlackList); i++) + { + if (strcmp(BlackList[i], a_FnName) == 0) + { + return true; + } + } + return false; +} + + + + + +const char * FindAttr(const char ** a_Attrs, const char * a_AttrName) +{ + for (const char ** Attr = a_Attrs; *Attr != NULL; Attr += 2) + { + if (strcmp(*Attr, a_AttrName) == 0) + { + return *(Attr + 1); + } + } // for Attr - a_Attrs[] + return NULL; +} + + + + + +void OnStartElement(void * a_Data, const char * a_Element, const char ** a_Attrs) +{ + if (strcmp(a_Element, "LEAK") == 0) + { + const char * attrID = FindAttr(a_Attrs, "requestID"); + const char * attrSize = FindAttr(a_Attrs, "size"); + g_CurrentID = atoi((attrID == NULL) ? "-1" : attrID); + g_CurrentSize = atoi((attrSize == NULL) ? "-1" : attrSize); + g_PrevFunctionName.clear(); + return; + } + if (strcmp(a_Element, "STACKENTRY") == 0) + { + const char * fnName = FindAttr(a_Attrs, "decl"); + if (fnName == NULL) + { + g_CurrentID = -1; + g_CurrentSize = -1; + return; + } + if (g_CurrentSize < 0) + { + return; + } + if (IsFnBlackListed(fnName)) + { + return; + } + AString FunctionName = fnName; + cFunction & Function = g_FnMap[FunctionName]; + Function.m_Size += g_CurrentSize; + Function.m_Count += 1; + if (!g_PrevFunctionName.empty()) + { + Function.m_ChildrenNames.insert(g_PrevFunctionName); + } + std::swap(g_PrevFunctionName, FunctionName); // We only care about moving FunctionName into g_PrevFunctionName + return; + } +} + + + + + +void OnEndElement(void * a_Data, const char * a_Element) +{ + if (strcmp(a_Element, "LEAK") == 0) + { + g_CurrentID = -1; + g_CurrentSize = -1; + return; + } +} + + + + + +bool CompareFnInt(const std::pair & a_First, const std::pair & a_Second) +{ + return (a_First.second < a_Second.second); +} + + + + + +void WriteSizeStatistics(void) +{ + typedef std::vector > StringIntPairs; + StringIntPairs FnSizes; + + cFile f("memdump_totals.txt", cFile::fmWrite); + if (!f.IsOpen()) + { + LOGERROR("Cannot open memdump_totals.txt"); + return; + } + + for (FunctionMap::iterator itr = g_FnMap.begin(), end = g_FnMap.end(); itr != end; ++itr) + { + FnSizes.push_back(std::pair(itr->first, itr->second.m_Size)); + } // for itr - g_FnSizes[] + std::sort(FnSizes.begin(), FnSizes.end(), CompareFnInt); + + for (StringIntPairs::const_iterator itr = FnSizes.begin(), end = FnSizes.end(); itr != end; ++itr) + { + f.Printf("%d\t%s\n", itr->second, itr->first.c_str()); + } // for itr - FnSizes[] +} + + + + + +void WriteCountStatistics(void) +{ + typedef std::vector > StringIntPairs; + StringIntPairs FnCounts; + + cFile f("memdump_counts.txt", cFile::fmWrite); + if (!f.IsOpen()) + { + LOGERROR("Cannot open memdump_counts.txt"); + return; + } + + for (FunctionMap::iterator itr = g_FnMap.begin(), end = g_FnMap.end(); itr != end; ++itr) + { + FnCounts.push_back(std::pair(itr->first, itr->second.m_Count)); + } // for itr - g_FnSizes[] + std::sort(FnCounts.begin(), FnCounts.end(), CompareFnInt); + + for (StringIntPairs::const_iterator itr = FnCounts.begin(), end = FnCounts.end(); itr != end; ++itr) + { + f.Printf("%d\t%s\n", itr->second, itr->first.c_str()); + } // for itr - FnSizes[] +} + + + + + +AString HTMLEscape(const AString & a_Text) +{ + AString res; + res.reserve(a_Text.size()); + size_t len = a_Text.length(); + for (size_t i = 0; i < len; i++) + { + switch (a_Text[i]) + { + case '<': res.append("<
"); break; + case '>': res.append("
>"); break; + case '&': res.append("&"); break; + default: + { + res.push_back(a_Text[i]); + } + } + } // for i - a_Text[] + return res; +} + + + + + +void WriteDotGraph(void) +{ + cFile f("memdump.dot", cFile::fmWrite); + if (!f.IsOpen()) + { + LOGERROR("Cannot open memdump.dot"); + return; + } + + f.Printf("digraph {\n\tnode [shape=plaintext]\n\n"); + for (FunctionMap::const_iterator itrF = g_FnMap.begin(), endF = g_FnMap.end(); itrF != endF; ++itrF) + { + f.Printf("\t\"%s\" [label=<%s
%d bytes (%d KiB)
%d blocks>]\n", + itrF->first.c_str(), + HTMLEscape(itrF->first).c_str(), + itrF->second.m_Size, + (itrF->second.m_Size + 1023) / 1024, + itrF->second.m_Count + ); + const AStringSet & Children = itrF->second.m_ChildrenNames; + for (AStringSet::const_iterator itrN = Children.begin(), endN = Children.end(); itrN != endN; ++itrN) + { + f.Printf("\t\t\"%s\" -> \"%s\"\n", itrF->first.c_str(), itrN->c_str()); + } + f.Printf("\n"); + } // for itr + f.Printf("}\n"); +} + + + + + +int main(int argc, char * argv[]) +{ + // Open the dump file: + cFile f("memdump.xml", cFile::fmRead); + if (!f.IsOpen()) + { + printf("Cannot open memdump.xml\n"); + return 1; + } + + // Create the XML parser: + XML_Parser Parser = XML_ParserCreate(NULL); + XML_SetElementHandler(Parser, OnStartElement, OnEndElement); + + // Feed the file through XML parser: + char Buffer[512 KiB]; + while (true) + { + int NumBytes = f.Read(Buffer, sizeof(Buffer)); + if (NumBytes <= 0) + { + break; + } + XML_Parse(Parser, Buffer, NumBytes, false); + putc('.', stdout); + } + XML_Parse(Parser, "", 0, true); + f.Close(); + + // Output the statistics + WriteSizeStatistics(); + WriteCountStatistics(); + WriteDotGraph(); + + return 0; +} + + + + diff --git a/Tools/MemDumpAnalysis/MemDumpAnalysis.sln b/Tools/MemDumpAnalysis/MemDumpAnalysis.sln new file mode 100644 index 000000000..45fbd933f --- /dev/null +++ b/Tools/MemDumpAnalysis/MemDumpAnalysis.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 10.00 +# Visual C++ Express 2008 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MemDumpAnalysis", "MemDumpAnalysis.vcproj", "{110861A3-EF64-494F-8FC2-7F7ACFD3FEAC}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Release|Win32 = Release|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {110861A3-EF64-494F-8FC2-7F7ACFD3FEAC}.Debug|Win32.ActiveCfg = Debug|Win32 + {110861A3-EF64-494F-8FC2-7F7ACFD3FEAC}.Debug|Win32.Build.0 = Debug|Win32 + {110861A3-EF64-494F-8FC2-7F7ACFD3FEAC}.Release|Win32.ActiveCfg = Release|Win32 + {110861A3-EF64-494F-8FC2-7F7ACFD3FEAC}.Release|Win32.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Tools/MemDumpAnalysis/MemDumpAnalysis.vcproj b/Tools/MemDumpAnalysis/MemDumpAnalysis.vcproj new file mode 100644 index 000000000..ffb935868 --- /dev/null +++ b/Tools/MemDumpAnalysis/MemDumpAnalysis.vcproj @@ -0,0 +1,439 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Tools/MemDumpAnalysis/MemDumpAnalysis.vcproj.user b/Tools/MemDumpAnalysis/MemDumpAnalysis.vcproj.user new file mode 100644 index 000000000..11ddaa32b --- /dev/null +++ b/Tools/MemDumpAnalysis/MemDumpAnalysis.vcproj.user @@ -0,0 +1,31 @@ + + + + + + + + + + + diff --git a/Tools/MemDumpAnalysis/targetver.h b/Tools/MemDumpAnalysis/targetver.h new file mode 100644 index 000000000..56ab74a5e --- /dev/null +++ b/Tools/MemDumpAnalysis/targetver.h @@ -0,0 +1,25 @@ + +// targetver.h + +// Used by MSWin builds to target the appropriate OS version + + + + + +#pragma once + +// The following macros define the minimum required platform. The minimum required platform +// is the earliest version of Windows, Internet Explorer etc. that has the necessary features to run +// your application. The macros work by enabling all features available on platform versions up to and +// including the version specified. + +// Modify the following defines if you have to target a platform prior to the ones specified below. +// Refer to MSDN for the latest info on corresponding values for different platforms. +#ifndef _WIN32_WINNT // Specifies that the minimum required platform is Windows Vista. +#define _WIN32_WINNT 0x0600 // Change this to the appropriate value to target other versions of Windows. +#endif + + + + -- cgit v1.2.3