Philip Beevers
2005-Dec-12 22:40 UTC
[dtrace-discuss] Getting number of bytes returned to application byfree()?
Hi Ryan,> And was curious if there is a way to use DTrace to grab the number of > bytes free() returns to the application? The malloc(), free() > and brk() > manual pages don''t provide a whole lot of info on what happens when > free() is called, and I was wondering if anyone has written some D to > grab this info? If not, I will start digging through the > source code for > malloc() and free() if they are available on opensolaris.org.The standard malloc() and free() implementations in libc are open - they''re relatively simple to follow, and it should be fairly easy to use the malloc header on each block to tell you the size. On the one hand, I like this solution because it''s stateless - it doesn''t rely on any prior knowledge stored elsewhere. But on the other, I''m not so keen because it''s very specific to the libc malloc implementation. There are enough alternative malloc implementations out there - libmtmalloc, libwatchmalloc, and now libumem - to make this lack of generality a bit of a concern. In particularly, it''s likely that threaded applications which are heap-intensive will be using either libmtmalloc or preferably libumem to increase scalability. A stateful solution would be to store the size of the block in an array on return from malloc, then look this up again on entry to free. For example: #!/usr/sbin/dtrace -qs pid24169::malloc:entry { self->size = arg0; } pid24169::malloc:return / self->size / { sz[arg1] = self->size; self->size = 0; } pid24169::free:entry / sz[arg0] / { printf("Freeing %p (size %d)\n", arg0, sz[arg0]); sz[arg0] = 0; } I''m sure Bryan, Adam, Jonathan or another list contributor will be along with a better solution in a moment :-) Hope this helps, -- Philip Beevers Fidessa Infrastructure Development mailto:philip.beevers at fidessa.com phone: +44 1483 206571 ****************************************************************** This message is intended only for the stated addressee(s) and may be confidential. Access to this email by anyone else is unauthorised. Any opinions expressed in this email do not necessarily reflect the opinions of royalblue. Any unauthorised disclosure, use or dissemination, either whole or in part is prohibited. If you are not the intended recipient of this message, please notify the sender immediately. ******************************************************************