From nobody@FreeBSD.org  Sun Jan 10 16:52:47 2010
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 A657F1065672
	for <freebsd-gnats-submit@FreeBSD.org>; Sun, 10 Jan 2010 16:52:47 +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 9560E8FC0A
	for <freebsd-gnats-submit@FreeBSD.org>; Sun, 10 Jan 2010 16:52:47 +0000 (UTC)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.14.3/8.14.3) with ESMTP id o0AGqlRR005844
	for <freebsd-gnats-submit@FreeBSD.org>; Sun, 10 Jan 2010 16:52:47 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.14.3/8.14.3/Submit) id o0AGqksG005843;
	Sun, 10 Jan 2010 16:52:47 GMT
	(envelope-from nobody)
Message-Id: <201001101652.o0AGqksG005843@www.freebsd.org>
Date: Sun, 10 Jan 2010 16:52:47 GMT
From: Ivan Klymenko <fidaj@ukr.net>
To: freebsd-gnats-submit@FreeBSD.org
Subject: Programm conky does not display information for each core CPU is more than one
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         142577
>Category:       ports
>Synopsis:       sysutils/onky does not display information for each core CPU is more than one
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    amdmi3
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sun Jan 10 17:00:16 UTC 2010
>Closed-Date:    Thu Jan 28 01:59:39 UTC 2010
>Last-Modified:  Thu Jan 28 02:00:02 UTC 2010
>Originator:     Ivan Klymenko
>Release:        FreeBSD 8.0-RELEASE-p2
>Organization:
individual
>Environment:
FreeBSD nonamehost 8.0-RELEASE-p2 FreeBSD 8.0-RELEASE-p2 #0: Thu Jan  7 14:31:29 EET 2010     user@nonamehost:/usr/obj/usr/src/sys/mk8  amd64
>Description:
Programm conky does not display information for each core CPU is more than one.
In the source code freebsd.c in written:
/* XXX: FreeBSD doesn't allow to get per CPU load stats on SMP machines.
* It's possible to get a CPU count, but as we fulfill only
* info.cpu_usage[0], it's better to report there's only one CPU.
* It should fix some bugs (e.g. cpugraph) */

>How-To-Repeat:
ever
>Fix:
I created a patch

Patch attached with submission follows:

diff -u freebsd.c.orig freebsd.c
--- freebsd.c.orig	2010-01-09 21:49:10.000000000 +0200
+++ freebsd.c	2010-01-10 10:21:22.000000000 +0200
@@ -289,22 +289,22 @@
 };
 
 struct cpu_load_struct fresh = { {0, 0, 0, 0, 0} };
-long cpu_used, oldtotal, oldused;
+long cpu_used;//, oldtotal, oldused;
+extern long * g_oldtotal, * g_oldused;
 
 void get_cpu_count(void)
 {
-	/* int cpu_count = 0; */
+	int cpu_count = 0;
+	size_t cpu_count_len = sizeof(cpu_count);
 
-	/* XXX: FreeBSD doesn't allow to get per CPU load stats on SMP machines.
-	 * It's possible to get a CPU count, but as we fulfill only
-	 * info.cpu_usage[0], it's better to report there's only one CPU.
-	 * It should fix some bugs (e.g. cpugraph) */
-#if 0
-	if (GETSYSCTL("hw.ncpu", cpu_count) == 0) {
-		info.cpu_count = cpu_count;
+        if (sysctlbyname("hw.ncpu", &cpu_count, &cpu_count_len, NULL, 0) < 0) {
+		fprintf(stderr, "Cannot get hw.ncpu");
+		info.cpu_count = 1;
+	}
+	else
+	{
+	 info.cpu_count = cpu_count;
 	}
-#endif
-	info.cpu_count = 1;
 
 	info.cpu_usage = malloc(info.cpu_count * sizeof(float));
 	if (info.cpu_usage == NULL) {
@@ -315,9 +315,11 @@
 /* XXX: SMP support */
 void update_cpu_usage(void)
 {
-	long used, total;
-	long cp_time[CPUSTATES];
-	size_t cp_len = sizeof(cp_time);
+	int i, j = 0;
+	long *used, *total;
+        static long *cp_time_per_cpu;
+	size_t cp_len = info.cpu_count * CPUSTATES * sizeof(long);
+	size_t len = info.cpu_count * sizeof(long);
 
 	/* add check for !info.cpu_usage since that mem is freed on a SIGUSR1 */
 	if ((cpu_setup == 0) || (!info.cpu_usage)) {
@@ -325,28 +327,41 @@
 		cpu_setup = 1;
 	}
 
-	if (sysctlbyname("kern.cp_time", &cp_time, &cp_len, NULL, 0) < 0) {
-		fprintf(stderr, "Cannot get kern.cp_time");
-	}
-
-	fresh.load[0] = cp_time[CP_USER];
-	fresh.load[1] = cp_time[CP_NICE];
-	fresh.load[2] = cp_time[CP_SYS];
-	fresh.load[3] = cp_time[CP_IDLE];
-	fresh.load[4] = cp_time[CP_IDLE];
-
-	used = fresh.load[0] + fresh.load[1] + fresh.load[2];
-	total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
-
-	if ((total - oldtotal) != 0) {
-		info.cpu_usage[0] = ((double) (used - oldused)) /
-			(double) (total - oldtotal);
-	} else {
-		info.cpu_usage[0] = 0;
-	}
-
-	oldused = used;
-	oldtotal = total;
+	cp_time_per_cpu = malloc(cp_len);
+	used = malloc(len);
+	total = malloc(len);
+
+	if (sysctlbyname("kern.cp_times", cp_time_per_cpu, &cp_len, NULL, 0) < 0) {
+		fprintf(stderr, "Cannot get kern.cp_times");
+	}
+
+	for (i=0; i<info.cpu_count; i++)
+	{
+	 fresh.load[0] = cp_time_per_cpu[CP_USER+j];
+	 fresh.load[1] = cp_time_per_cpu[CP_NICE+j];
+	 fresh.load[2] = cp_time_per_cpu[CP_SYS+j];
+	 fresh.load[3] = cp_time_per_cpu[CP_IDLE+j];
+	 fresh.load[4] = cp_time_per_cpu[CP_IDLE+j];
+
+	 j+=CPUSTATES;
+
+	 used[i] = fresh.load[0] + fresh.load[1] + fresh.load[2];
+	 total[i] = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
+
+	 if ((total[i] - g_oldtotal[i]) != 0) {
+		 info.cpu_usage[i] = ((double) (used[i] - g_oldused[i])) /
+			(double) (total[i] - g_oldtotal[i]);
+	 } else {
+	 	 info.cpu_usage[i] = 0;
+	 }
+
+	 g_oldused[i] = used[i];
+	 g_oldtotal[i] = total[i];
+	}
+	
+	free(cp_time_per_cpu);
+	free(used);
+	free(total);
 }
 
 void update_load_average(void)
diff -u conky.c.orig conky.c
--- conky.c.orig2010-01-10 00:49:54.000000000 +0200
+++ conky.c2010-01-10 01:04:44.000000000 +0200
@@ -151,6 +151,10 @@
 double update_interval;
 void *global_cpu = NULL;
 
+#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
+long *g_oldused, *g_oldtotal;
+#endif
+
 int argc_copy;
 char** argv_copy;
 
@@ -3485,6 +3489,10 @@
 
 obj = construct_text_object(buf, arg,
 line, &ifblock_opaque, orig_p);
+#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
+g_oldused = malloc((size_t)info.cpu_count * sizeof(long));
+    g_oldtotal = malloc((size_t)info.cpu_count * sizeof(long));
+#endif
 if (obj != NULL) {
 append_object(retval, obj);
 }
@@ -9386,6 +9394,9 @@
 main_loop();
 
 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
+free(g_oldused);
+free(g_oldtotal);
+
 kvm_close(kd);
 #endif


>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-ports-bugs->amdmi3 
Responsible-Changed-By: amdmi3 
Responsible-Changed-When: Sun Jan 10 19:20:50 UTC 2010 
Responsible-Changed-Why:  
I'll take it. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=142577 
State-Changed-From-To: open->feedback 
State-Changed-By: amdmi3 
State-Changed-When: Sun Jan 10 19:21:07 UTC 2010 
State-Changed-Why:  
Awaiting maintainer's feedback 

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

From: Dmitry Marakasov <amdmi3@amdmi3.ru>
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: ports/142577: Programm conky does not display information for
 each core CPU is more than one
Date: Mon, 11 Jan 2010 02:06:40 +0300

 > Synopsis: Programm conky does not display information for each core CPU is more than one
 > 
 > http://www.freebsd.org/cgi/query-pr.cgi?pr=142577
 
 Better version of a patch, discussed with original submitter:
 
 http://people.freebsd.org/~amdmi3/patch-src-freebsd.c
 
 -- 
 Dmitry Marakasov   .   55B5 0596 FF1E 8D84 5F56  9510 D35A 80DD F9D2 F77D
 amdmi3@amdmi3.ru  ..:  jabber: amdmi3@jabber.ru    http://www.amdmi3.ru
State-Changed-From-To: feedback->open 
State-Changed-By: amdmi3 
State-Changed-When: Thu Jan 28 01:58:01 UTC 2010 
State-Changed-Why:  
Maintainer timeout 

http://www.freebsd.org/cgi/query-pr.cgi?pr=142577 
State-Changed-From-To: open->closed 
State-Changed-By: amdmi3 
State-Changed-When: Thu Jan 28 01:59:38 UTC 2010 
State-Changed-Why:  
Committed. Thanks! 

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

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: ports/142577: commit references a PR
Date: Thu, 28 Jan 2010 01:59:43 +0000 (UTC)

 amdmi3      2010-01-28 01:59:35 UTC
 
   FreeBSD ports repository
 
   Modified files:
     sysutils/conky       Makefile 
   Added files:
     sysutils/conky/files patch-src-freebsd.c 
   Log:
   - Support reporting per-core CPU load statistics
   
   PR:             142577
   Submitted by:   Ivan Klymenko <fidaj@ukr.net>
   Approved by:    maintainer timeout
   
   Revision  Changes    Path
   1.51      +1 -1      ports/sysutils/conky/Makefile
   1.15      +145 -0    ports/sysutils/conky/files/patch-src-freebsd.c (new)
 _______________________________________________
 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:
