Skip to content

Commit

Permalink
add new command "bysize"
Browse files Browse the repository at this point in the history
show statistics by userSize.
  • Loading branch information
yoichi committed Nov 9, 2012
1 parent 8e6b573 commit d8391af
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 0 deletions.
60 changes: 60 additions & 0 deletions BySizeProcessor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <windows.h>
#include "common.h"
#include "BySizeProcessor.h"

BySizeProcessor::BySizeProcessor()
{
}

void BySizeProcessor::Register(ULONG64 ustAddress,
ULONG64 size, ULONG64 address,
ULONG64 userSize, ULONG64 userAddress)
{
UNREFERENCED_PARAMETER(size);
UNREFERENCED_PARAMETER(address);
UNREFERENCED_PARAMETER(userAddress);

std::map<ULONG64, SizeRecord>::iterator itr = records_.find(userSize);
if (itr == records_.end())
{
SizeRecord record;
record.userSize = userSize;
record.count = 1;
record.ustAddress.insert(ustAddress);
records_[userSize] = record;
}
else
{
SizeRecord &record = itr->second;
record.count++;
record.ustAddress.insert(ustAddress);
}
}

void BySizeProcessor::Print()
{
std::multiset<SizeRecord> sorted;
for (std::map<ULONG64, SizeRecord>::iterator itr = records_.begin(); itr != records_.end(); ++itr)
{
sorted.insert(itr->second);
}

if (IsPtr64())
{
dprintf(" userSize( count) ust0, ust1,...\n");
}
else
{
dprintf("userSize( count) ust0, ust1,...\n");
}
for (std::multiset<SizeRecord>::reverse_iterator itr = sorted.rbegin(); itr != sorted.rend(); ++itr)
{
dprintf("%p(%p)", itr->userSize, itr->count);
for (std::set<ULONG64>::iterator itr_ = itr->ustAddress.begin(); itr_ != itr->ustAddress.end(); ++itr_)
{
dprintf("%p,", *itr_);
}
dprintf("\n");
}
dprintf("\n");
}
52 changes: 52 additions & 0 deletions BySizeProcessor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#pragma once

#include <map>
#include <set>
#include "IProcessor.h"

class BySizeProcessor : public IProcessor
{
private:
struct SizeRecord {
ULONG64 userSize;
ULONG64 count;
std::set<ULONG64> ustAddress;
bool operator< (const BySizeProcessor::SizeRecord& rhs) const
{
return count < rhs.count;
}
};

/**
* @brief userSize to SizeRecord map
*/
std::map<ULONG64, SizeRecord> records_;

public:
/**
* @brief constructor
*/
BySizeProcessor();

/**
* @copydoc IProcessor::StartHeap()
*/
void StartHeap(ULONG64 /*heapAddress*/) {}

/**
* @copydoc IProcessor::Register()
*/
void Register(ULONG64 ustAddress,
ULONG64 size, ULONG64 address,
ULONG64 userSize, ULONG64 userAddress);

/**
* @copydoc IProcessor::FinishHeap()
*/
void FinishHeap(ULONG64 /*heapAddress*/) {}

/**
* @brief print summary of heap usage
*/
void Print();
};
37 changes: 37 additions & 0 deletions heapstat.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "common.h"
#include "Utility.h"
#include "SummaryProcessor.h"
#include "BySizeProcessor.h"
#include "UmdhProcessor.h"
#include <list>

Expand Down Expand Up @@ -1361,6 +1362,7 @@ DECLARE_API(help)

dprintf("Help for extension dll heapstat.dll\n"
" heapstat [-v] [-k module!symbol] - Shows statistics of heaps\n"
" bysize [-v] - Shows statistics of heaps by size\n"
" umdh <file> - Generate umdh output\n"
" ust <addr> - Shows stacktrace of the ust record at <addr>\n"
" help - Shows this help\n");
Expand Down Expand Up @@ -1419,6 +1421,41 @@ DECLARE_API(heapstat)
}
}

DECLARE_API(bysize)
{
UNREFERENCED_PARAMETER(dwProcessor);
UNREFERENCED_PARAMETER(dwCurrentPc);
UNREFERENCED_PARAMETER(hCurrentThread);
UNREFERENCED_PARAMETER(hCurrentProcess);

BOOL verbose = FALSE;

std::vector<char> buffer;
buffer.resize(strlen(args) + 1);
memcpy(&buffer[0], args, buffer.size());
char *token, *nextToken;
const char *delim = " ";
token = strtok_s(&buffer[0], delim, &nextToken);
while (token != NULL)
{
if (strcmp("-v", token) == 0)
{
dprintf("verbose mode\n");
verbose = TRUE;
}
token = strtok_s(NULL, delim, &nextToken);
}

BySizeProcessor processor;

if (!AnalyzeHeap(&processor, verbose))
{
return;
}

processor.Print();
}

DECLARE_API(umdh)
{
UNREFERENCED_PARAMETER(dwProcessor);
Expand Down
1 change: 1 addition & 0 deletions heapstat.def
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ EXPORTS
;--------------------------------------------------------------------
help
heapstat
bysize
umdh
ust

Expand Down
8 changes: 8 additions & 0 deletions heapstat.vcproj
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,10 @@
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath=".\BySizeProcessor.cpp"
>
</File>
<File
RelativePath=".\common.c"
>
Expand All @@ -341,6 +345,10 @@
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath=".\BySizeProcessor.h"
>
</File>
<File
RelativePath=".\common.h"
>
Expand Down

0 comments on commit d8391af

Please sign in to comment.