Subject: Re: correctly counting user/sys/interrupt time
To: Matt Thomas <>
From: David Laight <>
List: tech-kern
Date: 04/08/2006 22:15:52
On Sat, Apr 08, 2006 at 01:52:03PM -0700, Matt Thomas wrote:
> Exactly where and what are you counting?  Do you add members to lwp's l_md
> struct?  I assume you've added stuff to cpu_info as well.

I've added 2 items to the struct lwp:
	uint32_t l_syscall_time;
	uint64_t *l_syscall_counter;

Syscall entry does:
	l->l_syscall_time = cpu_counter32();
	l->l_syscall_counter = &syscall_times[code];

Syscall exit and mi_sleep do:
	*l->l_syscall_counter += cpu_counter32() - l->syscall_time;

Return from m_sleep does:
	l->l_syscall_time = cpu_counter32();

(code added to syscall_plain/fancy in i386/i386/syscalls.c)

Except that there are some other macros lurking.
(The code I had working was a bit different to the above).
syscall_counts[] and syscall_times[] can be retrieved through the
sysctl interface and displayed by systat.

Unfortunately, if a process is switched out during an interrupt the
wrong places will get incremented, and an interrupt in the kernel
will count as system call time.

Now I can add the elapsed times to p->p_[usi]ticks.  But then really
do need to know about non-system call entries into the kernel.

The basic code is MI, it just needs the macro calls added to the
relevant syscall code.

I'm also not really sure what to do about emulations.  But that is
just a SMOP.


David Laight: