From nobody@FreeBSD.org  Wed Jan 23 13:51:20 2008
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 1AC3116A418
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 23 Jan 2008 13:51:20 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [IPv6:2001:4f8:fff6::21])
	by mx1.freebsd.org (Postfix) with ESMTP id 010F413C4D5
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 23 Jan 2008 13:51:20 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.14.2/8.14.2) with ESMTP id m0NDnk7o034863
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 23 Jan 2008 13:49:46 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.14.2/8.14.1/Submit) id m0NDnk0f034862;
	Wed, 23 Jan 2008 13:49:46 GMT
	(envelope-from nobody)
Message-Id: <200801231349.m0NDnk0f034862@www.freebsd.org>
Date: Wed, 23 Jan 2008 13:49:46 GMT
From: KOIE Hidetaka <koie@suri.co.jp>
To: freebsd-gnats-submit@FreeBSD.org
Subject: [patch] sysutil/ascpu does not work FreeBSD 8.0-CURRENT
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         119923
>Category:       ports
>Synopsis:       [patch] sysutils/ascpu does not work FreeBSD 8.0-CURRENT
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    demon
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Wed Jan 23 14:00:05 UTC 2008
>Closed-Date:    Wed Dec 23 09:45:08 UTC 2009
>Last-Modified:  Wed Dec 23 09:50:04 UTC 2009
>Originator:     KOIE Hidetaka
>Release:        8.0-CURRENT
>Organization:
surigiken
>Environment:
FreeBSD guriandgura 8.0-CURRENT FreeBSD 8.0-CURRENT #1: Wed Jan 16 16:41:01 JST 2008 koie@guriandgura:/usr/obj/usr/src/sys/GURIANDGURA amd64

>Description:
ascpu exits 1, because kvm_nlist(kd, nlst={{"_cp_time"}, {0}}) is faild.
>How-To-Repeat:
        koie@guriandgura% ascpu
        kvm_open: kvm_nlist: No such file or directory
        ascpu : cannot get nlist
        koie@guriandgura%

>Fix:
sysctl cp_time/cp_times should be used.
(If sysctl is only used, no need to set the setgid bit by do-install in Makefile.)
the option -cpu will be accepted by this patch.




Patch attached with submission follows:

--- ascpu_x.c.orig2	2008-01-23 18:36:30.354893235 +0900
+++ ascpu_x.c	2008-01-23 18:36:51.855717860 +0900
@@ -19,6 +19,11 @@
 #include <fcntl.h>
 #include <kvm.h>
 #include <sys/types.h>
+#include <err.h>
+#include <string.h>
+#include <sys/resource.h>
+#include <sys/sysctl.h>
+#include <sys/wait.h>
 #endif
 
 #ifdef __hpux__
@@ -82,6 +87,8 @@
 int update_request = 0;
 
 #ifdef	__FreeBSD__
+static int		cp_time_mib[2];
+static int		cp_times_mib[2];
 static kvm_t		*kd;
 static struct nlist	nlst[] = {
 	{"_cp_time"}, {0}
@@ -555,12 +562,45 @@
 	memcpy(&last, &fresh, sizeof(last));
 
 #ifdef __FreeBSD__
-	if (kvm_read(kd, nlst[0].n_value, &fresh, sizeof(fresh)) != sizeof(fresh)) {
-		printf("ascpu : cannot read kvm\n");
-		exit(1);
+	if (state.cpu_number >= 0 && cp_times_mib[0] != 0) {
+		long cp_times[MAX_CPU][CPUSTATES];
+		size_t cp_times_len = sizeof(cp_times);
+		int error = sysctl(cp_times_mib, 2, cp_times, &cp_times_len, NULL, 0);
+		if (error) {
+			printf("ascpu: cannot sysctl cp_times\n");
+			exit(1);
+		}
+
+		long *cp_time = cp_times[state.cpu_number];
+		fresh.load[0] = cp_time[CP_USER];
+		fresh.load[1] = cp_time[CP_NICE];
+		fresh.load[2] = cp_time[CP_SYS] + cp_time[CP_INTR];
+		fresh.load[3] = cp_time[CP_IDLE];
+	} else if (state.cpu_number == -1 && cp_time_mib[0] != 0) {
+		long cp_time[CPUSTATES];
+		size_t cp_time_len = sizeof(cp_time);
+		int error = sysctl(cp_time_mib, 2, cp_time, &cp_time_len, NULL, 0);
+		if (error) {
+			printf("ascpu: cannot sysctl cp_time\n");
+			exit(1);
+		}
+
+		fresh.load[0] = cp_time[CP_USER];
+		fresh.load[1] = cp_time[CP_NICE];
+		fresh.load[2] = cp_time[CP_SYS] + cp_time[CP_INTR];
+		fresh.load[3] = cp_time[CP_IDLE];
+	} else {
+		if (nlst[0].n_type == 0) {
+			printf("ascpu : cannot get nlist\n");
+			exit(1);
+		}
+		if (kvm_read(kd, nlst[0].n_value, &fresh, sizeof(fresh)) != sizeof(fresh)) {
+			printf("ascpu : cannot read kvm\n");
+			exit(1);
+		}
+		/* compatible with Linux(overwrite 'interrupt' with 'idle' field) */
+		fresh.load[3] = fresh.load[4];
 	}
-	/* compatible with Linux(overwrite 'interrupt' with 'idle' field) */
-	fresh.load[3] = fresh.load[4];
 #endif
 
 #ifdef __hpux__
@@ -1094,14 +1134,12 @@
 		ascpu_cleanup();
 	}
 #ifdef	__FreeBSD__
-	if ((kd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open")) == NULL) {
-		printf("ascpu : cannot open kvm\n");
-		exit(1);
-	}
-	kvm_nlist(kd, nlst);
-	if (nlst[0].n_type == 0) {
-		printf("ascpu : cannot get nlist\n");
-		exit(1);
+	size_t len = 2;
+	sysctlnametomib("kern.cp_times", cp_times_mib, &len);
+	len = 2;
+	sysctlnametomib("kern.cp_time", cp_time_mib, &len);
+	if ((kd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open")) != NULL) {
+		kvm_nlist(kd, nlst);
 	}
 #endif
 


>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-ports-bugs->demon 
Responsible-Changed-By: edwin 
Responsible-Changed-When: Wed Jan 23 20:46:51 UTC 2008 
Responsible-Changed-Why:  
Over to maintainer (via the GNATS Auto Assign Tool) 

http://www.freebsd.org/cgi/query-pr.cgi?pr=119923 

From: martinko <gamato@users.sf.net>
To: bug-followup@FreeBSD.org, demon@FreeBSD.org
Cc: koie@suri.co.jp, Soeren Straarup <xride@freebsd.org>, 
 Albert 'Tigr' Dorofeev <albert@tigr.net>
Subject: Re: ports/119923: [patch] sysutils/ascpu does not work FreeBSD 8.0-CURRENT
Date: Tue, 22 Dec 2009 18:04:12 +0100

 Hallo,
 
 The PR with patch has been waiting for nearly 2 years.
 Now 8.0-RELEASE is out and the port is not fixed yet.
 Could someone have a look and commit pls ?
 (Or at least submit a follow up describing why not.)
 
 Thanks and regards,
 
 Martin
 
 PS: Since there's a similar issue with sysutils/wmmemload I'm CCing 
 Soeren. PR is ports/141898.
State-Changed-From-To: open->closed 
State-Changed-By: demon 
State-Changed-When: Wed Dec 23 09:43:32 UTC 2009 
State-Changed-Why:  
Patch committed, thanks! 

http://www.freebsd.org/cgi/query-pr.cgi?pr=119923 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: ports/119923: commit references a PR
Date: Wed, 23 Dec 2009 09:43:02 +0000 (UTC)

 demon       2009-12-23 09:42:39 UTC
 
   FreeBSD ports repository
 
   Modified files:
     sysutils/ascpu       Makefile 
   Added files:
     sysutils/ascpu/files patch-ascpu_x.c 
   Removed files:
     sysutils/ascpu/files patch-sec01 
   Log:
   Fix for 8.0.
   (patch-sec01 was merged into patch-ascpu_x.c).
   
   PR:             119923
   Submitted by:   KOIE Hidetaka <koie@suri.co.jp>
   
   Revision  Changes    Path
   1.21      +1 -1      ports/sysutils/ascpu/Makefile
   1.1       +109 -0    ports/sysutils/ascpu/files/patch-ascpu_x.c (new)
   1.3       +0 -23     ports/sysutils/ascpu/files/patch-sec01 (dead)
 _______________________________________________
 cvs-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/cvs-all
 To unsubscribe, send any mail to "cvs-all-unsubscribe@freebsd.org"
 
>Unformatted:
