[dtrace-discuss] Script for Stackdepth by Thread/LWP?

Jonathan Adams jonathan.adams at sun.com
Thu Feb 16 13:06:54 PST 2006


On Fri, Feb 17, 2006 at 01:16:00AM +1100, Brendan Gregg wrote:
> G'Day Mike,
> 
> On Wed, 15 Feb 2006, Mike Kuhnkey wrote:
> 
> > I'm interested in monitoring the amount of stack used by a
> > multi-threaded program.  I assume 'stackdepth' built-in would be
> > useful...but not sure.  Been through demo's, ToolKit, and internals..but
> > it's just not clicking for me yet.
> >
> > Not sure how to measure start/end of stack size dynamically...Anyone
> > know how to break this down?
> 
> If you just want the stack size,
> 
> # dtrace -n 'on-cpu { @[execname] = max(curthread->t_procp->p_stksize); }'
> dtrace: description 'on-cpu ' matched 3 probes
> ^C
> 
>   svc.startd                                                     8192
>   fsflush                                                        8192
>   sched                                                          8192
>   nscd                                                           8192
>   bash                                                          12288
>   svcs                                                          12288
>   svc.configd                                                   16384
>   gnome-vfs-daemon                                              16384
>   dtrace                                                        16384
>   miniserv.pl                                                   24576
>   gnome-panel                                                   32768
>   gnome-terminal                                                32768
>   sshd                                                          57344
>   nautilus                                                      73728
>   snmpd                                                        348160

This doesn't help with multi-threaded programs, however.  You're going
to need to do something like:

--- cut here ---
#!/usr/sbin/dtrace -s

this uintptr_t stkinfoptr;
this uintptr_t stkptr;

sched:::on-cpu, profile:::profile-997
{
	this->stkinfoptr = 0;
	this->stkptr = 0;
}

sched:::on-cpu, profile:::profile-997
/execname != "sched"/
{
	this->stkinfoptr = curthread->t_lwp->lwp_ustack;
	this->stkptr = (uintptr_t)0;
}

sched:::on-cpu, profile:::profile-997
/this->stkinfoptr != 0 && curpsinfo->pr_dmodel == PR_MODEL_ILP32/
{
	this->stkinfo32 = *(stack32_t *)copyin(this->stkinfoptr,
	    sizeof (stack32_t));
	this->stktop = (uintptr_t)this->stkinfo32.ss_sp +
	    this->stkinfo32.ss_size;
	this->stkptr = (uintptr_t)uregs[R_SP];
}

sched:::on-cpu, profile:::profile-997
/this->stkinfoptr != 0 && curpsinfo->pr_dmodel == PR_MODEL_LP64/
{
	this->stkinfo = *(stack_t *)copyin(this->stkinfoptr,
	    sizeof (stack_t));
	this->stktop = (uintptr_t)this->stkinfo.ss_sp +
	    this->stkinfo.ss_size;
	this->stkptr = (uintptr_t)uregs[R_SP];
}

sched:::on-cpu, profile:::profile-997
/this->stkptr != 0/
{
	@a[execname] = quantize(this->stktop - this->stkptr);
}

dtrace:::ERROR
{
	@b[execname] = count();
}

dtrace:::END
{
	printa(@a);
	printf("\nErrors:\n");
	printa("    %@d %s\n", @b);
}
--- cut here ---

Cheers,
- jonathan

-- 
Jonathan Adams, Solaris Kernel Development



More information about the dtrace-discuss mailing list