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
This commit is contained in:
madmaxoft@gmail.com
2013-07-06 19:44:54 +00:00
parent 209ad87026
commit 567df6aacf
25 changed files with 34 additions and 13506 deletions
+10
View File
@@ -0,0 +1,10 @@
// Globals.cpp
// Used for precompiled header generation
#include "Globals.h"
+35
View File
@@ -0,0 +1,35 @@
// Globals.h
// Used for precompiled header generation
#pragma once
#include "../../source/Globals.h"
/*
// System headers:
#include "targetver.h"
#include <stdio.h>
// STL headers:
#include <vector>
#include <list>
#include <map>
#include <string>
// Common:
#include "../source/StringUtils.h"
#include "../source/OSSupport/File.h"
*/
// Libraries:
#include "../../expat/expat.h"
+321
View File
@@ -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<AString> 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<AString, cFunction> 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<AString, int> & a_First, const std::pair<AString, int> & a_Second)
{
return (a_First.second < a_Second.second);
}
void WriteSizeStatistics(void)
{
typedef std::vector<std::pair<AString, int> > 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<AString, int>(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<std::pair<AString, int> > 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<AString, int>(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("&lt;<BR/>"); break;
case '>': res.append("<BR/>&gt;"); break;
case '&': res.append("&amp;"); 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<BR/>%d bytes (%d KiB)<BR/>%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;
}
+20
View File
@@ -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
@@ -0,0 +1,439 @@
<?xml version="1.0" encoding="windows-1250"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9,00"
Name="MemDumpAnalysis"
ProjectGUID="{110861A3-EF64-494F-8FC2-7F7ACFD3FEAC}"
RootNamespace="MemDumpAnalysis"
Keyword="Win32Proj"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="2"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE;COMPILED_FROM_DSP;XML_STATIC"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
UsePrecompiledHeader="2"
PrecompiledHeaderThrough="Globals.h"
WarningLevel="3"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
OutputFile="$(ProjectDir)\..\MCServer\$(ProjectName)_debug.exe"
LinkIncremental="2"
GenerateDebugInformation="true"
SubSystem="1"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="2"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="true"
PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE;COMPILED_FROM_DSP;XML_STATIC"
RuntimeLibrary="2"
EnableFunctionLevelLinking="true"
UsePrecompiledHeader="2"
PrecompiledHeaderThrough="Globals.h"
WarningLevel="3"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
OutputFile="$(ProjectDir)\..\MCServer\$(ProjectName).exe"
LinkIncremental="1"
GenerateDebugInformation="true"
SubSystem="1"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Source Files"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath=".\Globals.cpp"
>
<FileConfiguration
Name="Debug|Win32"
>
<Tool
Name="VCCLCompilerTool"
UsePrecompiledHeader="1"
/>
</FileConfiguration>
<FileConfiguration
Name="Release|Win32"
>
<Tool
Name="VCCLCompilerTool"
UsePrecompiledHeader="1"
/>
</FileConfiguration>
</File>
<File
RelativePath=".\Globals.h"
>
</File>
<File
RelativePath=".\MemDumpAnalysis.cpp"
>
</File>
<File
RelativePath=".\targetver.h"
>
</File>
</Filter>
<Filter
Name="expat"
>
<File
RelativePath="..\..\expat\ascii.h"
>
</File>
<File
RelativePath="..\..\expat\asciitab.h"
>
</File>
<File
RelativePath="..\..\expat\expat.h"
>
</File>
<File
RelativePath="..\..\expat\expat_external.h"
>
</File>
<File
RelativePath="..\..\expat\iasciitab.h"
>
</File>
<File
RelativePath="..\..\expat\internal.h"
>
</File>
<File
RelativePath="..\..\expat\latin1tab.h"
>
</File>
<File
RelativePath="..\..\expat\nametab.h"
>
</File>
<File
RelativePath="..\..\expat\utf8tab.h"
>
</File>
<File
RelativePath="..\..\expat\winconfig.h"
>
</File>
<File
RelativePath="..\..\expat\xmlparse.c"
>
<FileConfiguration
Name="Debug|Win32"
>
<Tool
Name="VCCLCompilerTool"
UsePrecompiledHeader="0"
/>
</FileConfiguration>
<FileConfiguration
Name="Release|Win32"
>
<Tool
Name="VCCLCompilerTool"
UsePrecompiledHeader="0"
/>
</FileConfiguration>
</File>
<File
RelativePath="..\..\expat\xmlrole.c"
>
<FileConfiguration
Name="Debug|Win32"
>
<Tool
Name="VCCLCompilerTool"
UsePrecompiledHeader="0"
/>
</FileConfiguration>
<FileConfiguration
Name="Release|Win32"
>
<Tool
Name="VCCLCompilerTool"
UsePrecompiledHeader="0"
/>
</FileConfiguration>
</File>
<File
RelativePath="..\..\expat\xmlrole.h"
>
</File>
<File
RelativePath="..\..\expat\xmltok.c"
>
<FileConfiguration
Name="Debug|Win32"
>
<Tool
Name="VCCLCompilerTool"
UsePrecompiledHeader="0"
/>
</FileConfiguration>
<FileConfiguration
Name="Release|Win32"
>
<Tool
Name="VCCLCompilerTool"
UsePrecompiledHeader="0"
/>
</FileConfiguration>
</File>
<File
RelativePath="..\..\expat\xmltok.h"
>
</File>
<File
RelativePath="..\..\expat\xmltok_impl.c"
>
<FileConfiguration
Name="Debug|Win32"
>
<Tool
Name="VCCLCompilerTool"
UsePrecompiledHeader="0"
/>
</FileConfiguration>
<FileConfiguration
Name="Release|Win32"
>
<Tool
Name="VCCLCompilerTool"
UsePrecompiledHeader="0"
/>
</FileConfiguration>
</File>
<File
RelativePath="..\..\expat\xmltok_impl.h"
>
</File>
<File
RelativePath="..\..\expat\xmltok_ns.c"
>
<FileConfiguration
Name="Debug|Win32"
>
<Tool
Name="VCCLCompilerTool"
UsePrecompiledHeader="0"
/>
</FileConfiguration>
<FileConfiguration
Name="Release|Win32"
>
<Tool
Name="VCCLCompilerTool"
UsePrecompiledHeader="0"
/>
</FileConfiguration>
</File>
</Filter>
<Filter
Name="Resource Files"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
>
</Filter>
<Filter
Name="shared"
>
<File
RelativePath="..\..\source\Log.cpp"
>
</File>
<File
RelativePath="..\..\source\Log.h"
>
</File>
<File
RelativePath="..\..\source\MCLogger.cpp"
>
</File>
<File
RelativePath="..\..\source\MCLogger.h"
>
</File>
<File
RelativePath="..\..\source\StringUtils.cpp"
>
</File>
<File
RelativePath="..\..\source\StringUtils.h"
>
</File>
<Filter
Name="OSSupport"
>
<File
RelativePath="..\..\source\OSSupport\CriticalSection.cpp"
>
</File>
<File
RelativePath="..\..\source\OSSupport\CriticalSection.h"
>
</File>
<File
RelativePath="..\..\source\OSSupport\File.cpp"
>
</File>
<File
RelativePath="..\..\source\OSSupport\File.h"
>
</File>
<File
RelativePath="..\..\source\OSSupport\IsThread.cpp"
>
</File>
<File
RelativePath="..\..\source\OSSupport\IsThread.h"
>
</File>
<File
RelativePath="..\..\source\OSSupport\MakeDir.cpp"
>
</File>
<File
RelativePath="..\..\source\OSSupport\MakeDir.h"
>
</File>
</Filter>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="windows-1250"?>
<VisualStudioUserFile
ProjectType="Visual C++"
Version="9,00"
ShowAllFiles="false"
>
<Configurations>
<Configuration
Name="Debug|Win32"
>
<DebugSettings
Command="$(TargetPath)"
WorkingDirectory="$(TargetDir)"
CommandArguments=""
Attach="false"
DebuggerType="3"
/>
</Configuration>
<Configuration
Name="Release|Win32"
>
<DebugSettings
Command="$(TargetPath)"
WorkingDirectory="$(TargetDir)"
CommandArguments=""
Attach="false"
DebuggerType="3"
/>
</Configuration>
</Configurations>
</VisualStudioUserFile>
+25
View File
@@ -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