DragonFly BSD
DragonFly users List (threaded) for 2010-04
[Date Prev][Date Next]  [Thread Prev][Thread Next]  [Date Index][Thread Index]

Re: Monitoring CPU time


From: Francois Tigeot <ftigeot@xxxxxxxxxxxx>
Date: Thu, 8 Apr 2010 22:29:41 +0200

On Mon, Apr 05, 2010 at 07:00:06PM +0200, Francois Tigeot wrote:
> On Mon, Apr 05, 2010 at 09:21:41AM -0700, Matthew Dillon wrote:
> > 
> >     systat -pv 1
> > 
> >     In terms of extracting it in a script... I dunno about that.
> 
> I'm beginning to think I will need to write a C program for that. top(1)
> uses a kinfo_proc structure to get its data and AFAIK there's no easy-to-use
> sysctl.
> 
> systat and vmstat give only an instant snapshot of the system state with
> 1 second precision or so. An average on 5 minutes would be much better for my
> purposes.

I have written a small C program to obtain the same sort of results
sysctl kern.cp_time returns on Free, Open and possibly NetBSD.

The actual values are a sum of the relevant fields on all CPUs. You just have
to be careful to adjust N_CPUS.

-- 
Francois Tigeot
/*
 * cp_time.c
 * returns user,nice,system,intr and idle values from cputime counters
 * similar to some BSDs kern.cp_time sysctl
 */

#include <stdio.h>
#include <kinfo.h>

#define N_CPUS	2

int main( void ) {
  int cpu, len;
  struct kinfo_cputime cp_t[N_CPUS];
  uint64_t user, nice, sys, intr, idle;

  bzero( cp_t, sizeof(struct kinfo_cputime)*N_CPUS );

  len = sizeof( cp_t[0]) * N_CPUS;
  if (sysctlbyname("kern.cputime", &cp_t, &len, NULL, 0))
			err(1, "kern.cputime");

  user = nice = sys = intr = idle = 0;
  /* Sum up cp_time for all cpus */
  for (cpu = 0; cpu < N_CPUS; cpu++) {
    user += cp_t[cpu].cp_user;
    nice += cp_t[cpu].cp_nice;
    sys  += cp_t[cpu].cp_sys;
    intr += cp_t[cpu].cp_intr;
    idle += cp_t[cpu].cp_idle;
  }

  printf("%llu %llu %llu %llu %llu\n", user, nice, sys, intr, idle );

  return 0;
}


[Date Prev][Date Next]  [Thread Prev][Thread Next]  [Date Index][Thread Index]