Hi Justin!

I made the following changes to your script.  The explanations follow 
the script:

syscall::stat:entry,
syscall::lstat:entry,
syscall::stat64:entry,
syscall::lstat64:entry
{
    self->fp = arg0;
}

syscall::stat:return,
syscall::lstat:return,
syscall::stat64:return,
syscall::lstat64:return
/ self->fp && dirname (copyinstr (self->fp)) == "/gold/tmcache" /
{
    calls++;
    self->fp = 0;
}

/* log once per hour */
tick-1s
/++n == 3600/
{
    printf("%Y %d\n", walltimestamp, calls);
    n = 0;
    calls = 0;
}

1) You shouldn't inspect user data that is passed to a system call on 
entry to the system call, because it might not be paged in yet.  Since 
dtrace clauses run in the kernel and with interrupts turned off, they 
can't handle page faults.  So the way we handle this is to save the 
string address in a thread local variable, and then inspect the string 
on return from the system call.  (Note that the thread local gets 
assigned a 0 to clear it, when we're done with it, because they will 
build up in memory otherwise.)

2) The "self->fp" variable is tested to see if it is non-zero at the 
beginning of the predicate, on the off chance that there was a stat call 
in the works at the time you started the script.  If that scenario 
happened,  a "return" would fire without a matching "entry", and the 
zero pointer would cause an error.

3) We use the "copyinstr" function because the arg0 pointer from "stat" 
is in user space.  The string has to be copied into kernel space 
temporarily so that the D clause can see it.  At the end of the clause, 
this storage is automatically released.

4) The "dirname" function strips away the filename, so that we can do 
the comparison.  This function also creates a temporary string that is 
released at the end of the clause.

5) Just in case you didn't think of it, if the path passed to arg0 is a 
relative pathname instead of an absolute pathname, this algorithm won't 
work.  In that situation, you'll need to do something involving the 
"cwd" build-in variable.  I didn't go so far as to figure out what.  
Also, degenerate absolute pathnames, like "/gold/../gold/tmcache" won't 
match either.

Chip
_______________________________________________
dtrace-discuss mailing list
[email protected]

Reply via email to