From nork@FreeBSD.org  Sat Sep  2 16:39:35 2006
Return-Path: <nork@FreeBSD.org>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id BA7DC16A4E1
	for <FreeBSD-gnats-submit@FreeBSD.org>; Sat,  2 Sep 2006 16:39:35 +0000 (UTC)
	(envelope-from nork@FreeBSD.org)
Received: from sakura.ninth-nine.com (sakura.ninth-nine.com [219.127.74.120])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 2B7E343D55
	for <FreeBSD-gnats-submit@FreeBSD.org>; Sat,  2 Sep 2006 16:39:31 +0000 (GMT)
	(envelope-from nork@FreeBSD.org)
Received: from nadesico.ninth-nine.com (nadesico.ninth-nine.com [219.127.74.122]) by sakura.ninth-nine.com (8.13.6/8.13.6/NinthNine) with ESMTP id k82GdMH5019431 for <FreeBSD-gnats-submit@freebsd.org>; Sun, 3 Sep 2006 01:39:24 +0900 (JST) (envelope-from Message-Id: <20060903013922.04fbe6c5.nork@FreeBSD.org>
Date: Sun, 3 Sep 2006 01:39:22 +0900
From: Norikatsu Shigemura <nork@FreeBSD.org>
To: FreeBSD-gnats-submit@FreeBSD.org
Subject: top(1): display feature of current CPU frequency

>Number:         102793
>Category:       bin
>Synopsis:       [patch] [request] top(1): display feature of current CPU frequency
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    edwin
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Sat Sep 02 16:40:18 GMT 2006
>Closed-Date:    
>Last-Modified:  Thu Sep 25 15:41:21 UTC 2008
>Originator:     Norikatsu Shigemura
>Release:        FreeBSD 6.1-STABLE i386
>Organization:
Personal
>Environment:
System: FreeBSD melfina.ninth-nine.com 6.1-STABLE FreeBSD 6.1-STABLE #4: Sat Sep 2 16:14:54 JST 2006 nork@melfina.ninth-nine.com:/usr/obj/usr/src/sys/MELFINA i386
>Description:
	The recent CPUs can change the operating frequency.  The feature
	makes CPUs for servers workable.  So I want to see relation of
	process status (CPU usage) and CPU current frequency using powerd(8)
	on the fly.

>How-To-Repeat:
	We can get CPU frequency by only sysctl on CUI.
	$ sysctl dev.cpu.0.freq
	If you can use GNOME, we can get CPU frequency by
	cpufreq-selector on GUI.
>Fix:
	I confirmed following environment:
	1. Intel Tualatin Core (PentiumIII-S): no display
	   It has no dev.cpu.?.freq.
	2. Intel Dothan Core (Pentium-M): works
	   It has one dev.cpu.?.freq.
	3. Intel Nocona Core (Xeon): works
	   It has one dev.cpu.?.freq.

	I don't run patched top(1), I know that following environment
	will works:

	4. Intel Yonah Core(Core Duo): will works
	   It has one dev.cpu.?.freq.

	I make two patches for 7-current and 6-stable.
	Please apply following patches.

	These patches are following state:
	No objection: hackers

Index: machine.c
===================================================================
RCS file: /home/ncvs/src/usr.bin/top/machine.c,v
retrieving revision 1.77
diff -u -r1.77 machine.c
--- machine.c	11 Jun 2006 19:18:39 -0000	1.77
+++ machine.c	26 Aug 2006 08:41:29 -0000
@@ -58,6 +58,8 @@
 extern struct process_select ps;
 extern char* printable(char *);
 static int smpmode;
+static int ncpu;
+#define	NCPU	2		/* support max 2cpu to display frequency */
 enum displaymodes displaymode;
 static int namelength = 8;
 static int cmdlengthdelta;
@@ -147,10 +149,10 @@
 
 /* these are for detailing the process states */
 
-int process_states[8];
+int process_states[8+NCPU];
 char *procstatenames[] = {
 	"", " starting, ", " running, ", " sleeping, ", " stopped, ",
-	" zombie, ", " waiting, ", " lock, ",
+	" zombie, ", " waiting, ", " lock, ", " MHz, ", " MHz, ",
 	NULL
 };
 
@@ -234,6 +236,13 @@
 	    modelen != sizeof(smpmode))
 		smpmode = 0;
 
+	for (ncpu = -1; ncpu < NCPU; ncpu++) {
+		char buf[32];
+		snprintf(buf, sizeof buf-1, "dev.cpu.%d.freq", ncpu+1);
+		if (sysctlbyname(buf, NULL, NULL, NULL, 0) < 0)
+			break;
+	}
+
 	while ((pw = getpwent()) != NULL) {
 		if (strlen(pw->pw_name) > namelength)
 			namelength = strlen(pw->pw_name);
@@ -632,6 +641,16 @@
 		}
 	}
 
+	/* CPU current frequency */
+	if (ncpu != -1) {
+		int j;
+		for(j = 0; j <= ncpu; j++) {
+			char buf[32];
+			snprintf(buf, sizeof buf-1, "dev.cpu.%d.freq", j);
+			GETSYSCTL(buf, process_states[j+8]);
+		}
+	}
+
 	/* if requested, sort the "interesting" processes */
 	if (compare != NULL)
 		qsort(pref, active_procs, sizeof(*pref), compare);



Index: machine.c
===================================================================
RCS file: /home/ncvs/src/usr.bin/top/machine.c,v
retrieving revision 1.74.2.1
diff -u -r1.74.2.1 machine.c
--- machine.c	1 Sep 2006 18:12:10 -0000	1.74.2.1
+++ machine.c	2 Sep 2006 16:28:59 -0000
@@ -61,6 +61,8 @@
 extern char* printable(char *);
 int swapmode(int *retavail, int *retfree);
 static int smpmode;
+static int ncpu;
+#define	NCPU	2		/* support max 2cpu to display frequency */
 enum displaymodes displaymode;
 static int namelength = 8;
 static int cmdlengthdelta;
@@ -153,10 +155,10 @@
 
 /* these are for detailing the process states */
 
-int process_states[8];
+int process_states[8+NCPU];
 char *procstatenames[] = {
 	"", " starting, ", " running, ", " sleeping, ", " stopped, ",
-	" zombie, ", " waiting, ", " lock, ",
+	" zombie, ", " waiting, ", " lock, ", " MHz, ", " MHz, ",
 	NULL
 };
 
@@ -235,6 +237,13 @@
 	    modelen != sizeof(smpmode))
 		smpmode = 0;
 
+	for (ncpu = -1; ncpu < NCPU; ncpu++) {
+		char buf[32];
+		snprintf(buf, sizeof buf-1, "dev.cpu.%d.freq", ncpu+1);
+		if (sysctlbyname(buf, NULL, NULL, NULL, 0) < 0)
+			break;
+	}
+
 	while ((pw = getpwent()) != NULL) {
 		if (strlen(pw->pw_name) > namelength)
 			namelength = strlen(pw->pw_name);
@@ -630,6 +639,17 @@
 		}
 	}
 
+	/* CPU current frequency */
+	if (ncpu != -1) {
+		int j;
+		for(j = 0; j <= ncpu; j++) {
+			char buf[32];
+			snprintf(buf, sizeof buf-1, "dev.cpu.%d.freq", j);
+			GETSYSCTL(buf, process_states[j+8]);
+		}
+	}
+
+
 	/* if requested, sort the "interesting" processes */
 	if (compare != NULL)
 		qsort(pref, active_procs, sizeof(*pref), compare);
>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->fimp 
Responsible-Changed-By: nork 
Responsible-Changed-When: Sun Sep 10 01:39:45 UTC 2006 
Responsible-Changed-Why:  
He was interesting. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=102793 
Responsible-Changed-From-To: fimp->imp 
Responsible-Changed-By: nork 
Responsible-Changed-When: Sun Sep 10 01:42:00 UTC 2006 
Responsible-Changed-Why:  
He was intersting. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=102793 
Responsible-Changed-From-To: imp->edwin 
Responsible-Changed-By: edwin 
Responsible-Changed-When: Thu Sep 25 15:41:04 UTC 2008 
Responsible-Changed-Why:  
I will see if this can be added for the import of 3.8b1 

http://www.freebsd.org/cgi/query-pr.cgi?pr=102793 
>Unformatted:
