Skip to content

Instantly share code, notes, and snippets.

@d0k
Created July 25, 2010 13:14
Show Gist options
  • Save d0k/489558 to your computer and use it in GitHub Desktop.
Save d0k/489558 to your computer and use it in GitHub Desktop.
set of dtrace scripts to analyze memory allocations
#pragma D option quiet
pid$target::malloc:entry {
@size[probefunc] = sum(arg0);
@totalsize = sum(arg0);
@calls[probefunc] = count();
@totalcalls = count();
}
pid$target::calloc:entry {
@size[probefunc] = sum(arg0 * arg1);
@totalsize = sum(arg0 * arg1);
@calls[probefunc] = count();
@totalcalls = count();
}
pid$target::realloc:entry {
@size[probefunc] = sum(arg1);
@totalsize = sum(arg1);
@calls[probefunc] = count();
@totalcalls = count();
}
pid$target::reallocf:entry {
@size[probefunc] = sum(arg1);
@totalsize = sum(arg1);
@calls[probefunc] = count();
@totalcalls = count();
}
pid$target::valloc:entry {
@size[probefunc] = sum(arg0);
@totalsize = sum(arg0);
@calls[probefunc] = count();
@totalcalls = count();
}
pid$target::free:entry {
@frees = count();
}
dtrace:::END {
printa("%s\t%@16d calls %@16d bytes\n", @calls, @size);
printa("\ntotal\t%@16d calls %@16d bytes", @totalcalls, @totalsize);
printa("\nfree\t%@16d calls", @frees);
}
#pragma D option quiet
pid$target::calloc:entry
/ arg0 * arg1 > 15 * 1024 / { /* OSX LARGE_THRESHOLD */
/* / arg0 * arg1 > 128 * 1024 / { / BSD */
@ = sum(arg0 * arg1);
}
dtrace:::END {
printa("calloc avoided zeroing %@d bytes.", @);
}
#pragma D option quiet
pid$target::realloc:entry {
self->ptr = arg0;
self->size = arg1;
}
pid$target::realloc:return
/ arg1 == self->ptr / {
@calls = count();
@size = sum(self->size);
}
dtrace:::END {
printa("realloc saved %@d mallocs with %@d bytes.", @calls, @size);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment