Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
fix Issue 14755 - Could -profile=gc also give the number of allocatio…
Browse files Browse the repository at this point in the history
…ns that led to X bytes being allocated?
  • Loading branch information
CyberShadow committed Aug 31, 2015
1 parent 44e41db commit 01293e3
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions src/rt/profilegc.d
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ import core.stdc.string;

import core.exception : onOutOfMemoryError;

struct Entry { size_t count, size; }

char[] buffer;
size_t[string] newCounts;
Entry[string] newCounts;

__gshared
{
size_t[string] globalNewCounts;
Entry[string] globalNewCounts;
string logfilename = "profilegc.log";
}

Expand Down Expand Up @@ -72,9 +74,12 @@ public void accumulate(string file, uint line, string funcname, string type, siz
type.length + 1 + funcname.length + 1 + file.length + 1 + buflen] = buf[0 .. buflen];

if (auto pcount = cast(string)buffer[0 .. length] in newCounts)
*pcount += sz; // existing entry
{ // existing entry
pcount.count++;
pcount.size += sz;
}
else
newCounts[buffer[0..length].idup] = sz; // new entry
newCounts[buffer[0..length].idup] = Entry(1, sz); // new entry
}

// Merge thread local newCounts into globalNewCounts
Expand All @@ -87,9 +92,10 @@ static ~this()
if (globalNewCounts.length)
{
// Merge
foreach (name, count; newCounts)
foreach (name, entry; newCounts)
{
globalNewCounts[name] += count;
globalNewCounts[name].count += entry.count;
globalNewCounts[name].size += entry.size;
}
}
else
Expand All @@ -108,25 +114,27 @@ shared static ~this()
static struct Result
{
string name;
size_t count;
Entry entry;

// qsort() comparator to sort by count field
extern (C) static int qsort_cmp(const void *r1, const void *r2)
{
auto result1 = cast(Result*)r1;
auto result2 = cast(Result*)r2;
ptrdiff_t cmp = result2.count - result1.count;
ptrdiff_t cmp = result2.entry.size - result1.entry.size;
if (cmp) return cmp < 0 ? -1 : 1;
cmp = result2.entry.count - result1.entry.count;
return cmp < 0 ? -1 : (cmp > 0 ? 1 : 0);
}
}

Result[] counts = new Result[globalNewCounts.length];

size_t i;
foreach (name, count; globalNewCounts)
foreach (name, entry; globalNewCounts)
{
counts[i].name = name;
counts[i].count = count;
counts[i].entry = entry;
++i;
}

Expand All @@ -137,10 +145,12 @@ shared static ~this()
FILE* fp = logfilename.length == 0 ? stdout : fopen(logfilename.ptr, "w");
if (fp)
{
fprintf(fp, "bytes allocated, type, function, file:line\n");
fprintf(fp, "bytes allocated, allocations, type, function, file:line\n");
foreach (ref c; counts)
{
fprintf(fp, "%8llu\t%8.*s\n", cast(ulong)c.count, c.name.length, c.name.ptr);
fprintf(fp, "%15llu\t%15llu\t%8.*s\n",
cast(ulong)c.entry.size, cast(ulong)c.entry.count,
c.name.length, c.name.ptr);
}
if (logfilename.length)
fclose(fp);
Expand Down

0 comments on commit 01293e3

Please sign in to comment.