Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 12 Mar 2000 16:42:26 +0100 (CET)
From:      Andrzej Bialecki <abial@webgiro.com>
To:        Kris Kennaway <kris@hub.freebsd.org>
Cc:        freebsd-hackers@FreeBSD.ORG, jkh@freebsd.org
Subject:   Re: Getting CPU usage in FreeBSD
Message-ID:  <Pine.BSF.4.20.0003121640390.23703-300000@mx.webgiro.com>
In-Reply-To: <Pine.BSF.4.21.0003112336140.85709-100000@hub.freebsd.org>

next in thread | previous in thread | raw e-mail | index | archive | help

[-- Attachment #1 --]
On Sat, 11 Mar 2000, Kris Kennaway wrote:

> On Sun, 12 Mar 2000, Oliver Fromme wrote:
> 
> > Then look up the definition of kread() in the same file, and
> > how the contents of cur.cp_time are used in the cpustats()
> > function.  Note that "cur" is a "struct statinfo", which is
> > defined in /usr/include/devstat.h.  The CPU states are defined
> > in /usr/include/sys/dkstat.h.
> 
> We probably should make this into a sysctl to divorce the binaries from
> having to read kvm.

I'm definitely for it... If I can get permission from Jordan, perhaps the
attached patches can make it into upcoming release.

Andrzej Bialecki

//  <abial@webgiro.com> WebGiro AB, Sweden (http://www.webgiro.com)
// -------------------------------------------------------------------
// ------ FreeBSD: The Power to Serve. http://www.freebsd.org --------
// --- Small & Embedded FreeBSD: http://www.freebsd.org/~picobsd/ ----

[-- Attachment #2 --]
Index: sysctl.c
===================================================================
RCS file: /home/ncvs/src/sbin/sysctl/sysctl.c,v
retrieving revision 1.25
diff -u -r1.25 sysctl.c
--- sysctl.c	1999/11/22 08:43:00	1.25
+++ sysctl.c	2000/03/12 15:35:12
@@ -46,6 +46,7 @@
 #endif /* not lint */
 
 #include <sys/types.h>
+#include <sys/dkstat.h>
 #include <sys/stat.h>
 #include <sys/sysctl.h>
 #include <sys/resource.h>
@@ -219,6 +220,29 @@
 /* These functions will dump out various interesting structures. */
 
 static int
+S_cpu_time(int l2, void *p)
+{
+	long *cpt=(long *)p;
+	double d,total;
+	int i;
+
+	total=0;
+	for(i=0;i<CPUSTATES;i++)
+		total+=*(cpt+i);
+	d=100**(cpt+CP_USER)/total;
+	printf("{ user=%.0f%% ",d);
+	d=100**(cpt+CP_NICE)/total;
+	printf("nice=%.0f%% ",d);
+	d=100**(cpt+CP_SYS)/total;
+	printf("sys=%.0f%% ",d);
+	d=100**(cpt+CP_INTR)/total;
+	printf("intr=%.0f%% ",d);
+	d=100**(cpt+CP_IDLE)/total;
+	printf("idle=%.0f%% }",d);
+	return(0);
+}
+
+static int
 S_clockinfo(int l2, void *p)
 {
 	struct clockinfo *ci = (struct clockinfo*)p;
@@ -417,6 +441,7 @@
 	case 'S':
 		i = 0;
 		if (!strcmp(fmt, "S,clockinfo"))	func = S_clockinfo;
+		else if (!strcmp(fmt, "S,cpu_time"))	func = S_cpu_time;
 		else if (!strcmp(fmt, "S,timeval"))	func = S_timeval;
 		else if (!strcmp(fmt, "S,loadavg"))	func = S_loadavg;
 		else if (!strcmp(fmt, "T,dev_t"))	func = T_dev_t;

[-- Attachment #3 --]
Index: kern_clock.c
===================================================================
RCS file: /home/ncvs/src/sys/kern/kern_clock.c,v
retrieving revision 1.105
diff -u -r1.105 kern_clock.c
--- kern_clock.c	2000/02/13 10:56:32	1.105
+++ kern_clock.c	2000/03/12 15:35:44
@@ -105,6 +105,9 @@
 SYSCTL_STRUCT(_kern, KERN_BOOTTIME, boottime, CTLFLAG_RD,
     &boottime, timeval, "System boottime");
 
+SYSCTL_OPAQUE(_kern, OID_AUTO, cpu_time, CTLFLAG_RD, &cp_time,
+	sizeof(cp_time), "S,cpu_time", "CPU times");
+
 /*
  * Which update policy to use.
  *   0 - every tick, bad hardware may fail with "calcru negative..."

Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.BSF.4.20.0003121640390.23703-300000>