From jdc@koitsu.dyndns.org  Mon Sep  5 22:46:08 2011
Return-Path: <jdc@koitsu.dyndns.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 1B5101065674
	for <freebsd-gnats-submit@freebsd.org>; Mon,  5 Sep 2011 22:46:08 +0000 (UTC)
	(envelope-from jdc@koitsu.dyndns.org)
Received: from qmta12.westchester.pa.mail.comcast.net (qmta12.westchester.pa.mail.comcast.net [76.96.59.227])
	by mx1.freebsd.org (Postfix) with ESMTP id D094D8FC13
	for <freebsd-gnats-submit@freebsd.org>; Mon,  5 Sep 2011 22:46:07 +0000 (UTC)
Received: from omta18.westchester.pa.mail.comcast.net ([76.96.62.90])
	by qmta12.westchester.pa.mail.comcast.net with comcast
	id VADS1h0021wpRvQ5CAYs9a; Mon, 05 Sep 2011 22:32:52 +0000
Received: from koitsu.dyndns.org ([67.180.84.87])
	by omta18.westchester.pa.mail.comcast.net with comcast
	id VAYq1h00G1t3BNj3eAYrAN; Mon, 05 Sep 2011 22:32:51 +0000
Received: by icarus.home.lan (Postfix, from userid 1000)
	id E9779102C1B; Mon,  5 Sep 2011 15:32:48 -0700 (PDT)
Message-Id: <20110905223248.E9779102C1B@icarus.home.lan>
Date: Mon,  5 Sep 2011 15:32:48 -0700 (PDT)
From: Jeremy Chadwick <freebsd@jdc.parodius.com>
Reply-To: Jeremy Chadwick <freebsd@jdc.parodius.com>
To: FreeBSD-gnats-submit@freebsd.org
Cc: sylvio@FreeBSD.org
Subject: bsnmpd returns inaccurate data for hrSystemProcesses OID
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         160494
>Category:       bin
>Synopsis:       [patch] bsnmpd(1) returns inaccurate data for hrSystemProcesses OID
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    glebius
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Mon Sep 05 22:50:03 UTC 2011
>Closed-Date:    Wed Oct 24 11:37:16 UTC 2012
>Last-Modified:  Wed Oct 24 11:37:16 UTC 2012
>Originator:     Jeremy Chadwick
>Release:        FreeBSD 8.2-STABLE amd64
>Organization:
>Environment:
System: FreeBSD icarus.home.lan 8.2-STABLE FreeBSD 8.2-STABLE #0: Tue Aug 23 18:20:42 PDT 2011 root@icarus.home.lan:/usr/obj/usr/src/sys/X7SBA_RELENG_8_amd64 amd64
>Description:
	(CC'ing maintainer of ports/net-mgmt/net-snmp, since said port has
	the exact same problem described below.  Maintainer may want to report
	this upstream to the net-snmp folks, since based on my review of the
	net-snmp code this looks like it affects multiple BSDs (any BSD that
	implements kvm_getprocs(3), sans Darwin which has its own code))

	bsnmpd(8)'s support for HOST-MIB has a "bug" where the hrSystemProcesses
	OID returns an inaccurate number of system processes.  On a machine with
	~50 actual processes, hrSystemProcesses.0 reports 240 or so (this will
	vary per machine configuration/driver/etc. given the nature of the issue).

	The root cause is use of KERN_PROC_ALL when calling kvm_getprocs(3) in
	src/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_scalars.c, function
	OS_getSystemProcesses.  KERN_PROC_ALL, per the kvm_getprocs(3) man page,
	includes system processes *as well* as visible kernel threads.

	According to the official HOST-MIB, hrSystemProcesses should be the
	"number of process contexts currently loaded or running"; the "industry
	norm" is to return system process counts only:

	http://www.net-snmp.org/docs/mibs/host.html

	Comparatively, for hrSystemProcesses, both Linux and Solaris 10 return the
	actual number of processes running on the box and DO NOT include kernel
	threads.

	Use of KERN_PROC_ALL is also in function swrun_OS_get_procs in
	src/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_swrun_tbl.c, however
	the comment at the top of the code indicates this is intentional ("Get all
	visible proceses including the kernel visible threads").  This code is
	used for the hrSWRun and hrSWRunPert OIDs.  I'm of the opinion that code
	SHOULD NOT be changed; hrSWRun can include things like kernel modules,
	so kernel thread count seems relevant there.

	However, the spelling mistake ("proceses") should be corrected.  :-)

	hrSystemProcesses behaviour varies per FreeBSD version.  Old RELENG_6
	(specifically 6.4-STABLE) returns just the process count; RELENG_7 and
	newer return the number of system processes in addition to kernel threads.
	I imagine the explanation is that some kernel innards changed between
	FreeBSD releases, but there is no mention of that change here:

	http://www.freebsd.org/doc/en/books/porters-handbook/freebsd-versions.html

	I would need to dig through kernel code to find the actual change that
	induced this.

	Finally, I want to point out that existing FreeBSD utilities like pkill
	have noted this change in the past.  See lines 318-319:

	http://www.freebsd.org/cgi/cvsweb.cgi/src/bin/pkill/pkill.c?annotate=1.4.2.5
>How-To-Repeat:
	Compare "snmpwalk -v2c -c community some.system.name hrSystemProcesses"
	count to that of ps -auxwww.
>Fix:
	In usr.sbin/bsnmpd/modules/snmp_hostres/hostres_scalars.c, function
	OS_getSystemProcesses, change KERN_PROC_ALL to KERN_PROC_PROC.

>Release-Note:
>Audit-Trail:

From: Jeremy Chadwick <freebsd@jdc.parodius.com>
To: FreeBSD-gnats-submit@FreeBSD.org, freebsd-bugs@FreeBSD.org
Cc: Sylvio C?sar Teixeira Amorim <scjamorim@bsd.com.br>
Subject: Re: bin/160494: bsnmpd returns inaccurate data for hrSystemProcesses
 OID
Date: Tue, 6 Sep 2011 08:55:52 -0700

 On Tue, Sep 06, 2011 at 08:41:39AM -0300, Sylvio C?sar Teixeira Amorim wrote:
 > Do a patch and send me.
 
 I had a chance to review the net-snmp code.  My previous claim was
 incorrect -- net-snmp DOES NOT suffer from the problem documented in
 this PR.  The code in net-snmp which uses kvm_getprocs(3) is for the
 hrSW*Run OIDs, which (properly) uses KERN_PROC_ALL to ensure they count
 kernel threads for kernel modules.
 
 The hrSystemProcesses OID with net-snmp returns the correct number of
 system processes (not including threads).  Verification:
 
 icarus# snmpwalk -v2c -c public localhost hrSystemProcesses
 HOST-RESOURCES-MIB::hrSystemProcesses.0 = Gauge32: 60
 
 So this PR/issue is specific to bsnmpd.  Sorry to waste your time,
 Sylvio!
 
 The PR should remain open as bsnmpd needs to be fixed.
 
 -- 
 | Jeremy Chadwick                                jdc at parodius.com |
 | Parodius Networking                       http://www.parodius.com/ |
 | UNIX Systems Administrator                   Mountain View, CA, US |
 | Making life hard for others since 1977.               PGP 4BD6C0CB |
 

From: Jeremy Chadwick <freebsd@jdc.parodius.com>
To: FreeBSD-gnats-submit@FreeBSD.org, freebsd-bugs@FreeBSD.org
Cc:  
Subject: Re: bin/160494: bsnmpd returns inaccurate data for hrSystemProcesses
 OID
Date: Tue, 6 Sep 2011 09:18:49 -0700

 A patch has been made for this issue, and has been tested/verified:
 
 http://jdc.parodius.com/freebsd/160494/
 
 -- 
 | Jeremy Chadwick                                jdc at parodius.com |
 | Parodius Networking                       http://www.parodius.com/ |
 | UNIX Systems Administrator                   Mountain View, CA, US |
 | Making life hard for others since 1977.               PGP 4BD6C0CB |
 
State-Changed-From-To: open->patched 
State-Changed-By: glebius 
State-Changed-When: Tue Sep 11 08:23:21 UTC 2012 
State-Changed-Why:  
Fixed in head. 


Responsible-Changed-From-To: freebsd-bugs->glebius 
Responsible-Changed-By: glebius 
Responsible-Changed-When: Tue Sep 11 08:23:21 UTC 2012 
Responsible-Changed-Why:  
Fixed in head. 

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

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/160494: commit references a PR
Date: Tue, 11 Sep 2012 08:33:32 +0000 (UTC)

 Author: glebius
 Date: Tue Sep 11 08:33:16 2012
 New Revision: 240354
 URL: http://svn.freebsd.org/changeset/base/240354
 
 Log:
   Do not count kernel threads as processes for hrSystemProcesses OID.
   
   PR:		bin/160494
   Submitted by:	Jeremy Chadwick <freebsd jdc.parodius.com>
 
 Modified:
   head/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_scalars.c
 
 Modified: head/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_scalars.c
 ==============================================================================
 --- head/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_scalars.c	Tue Sep 11 08:30:48 2012	(r240353)
 +++ head/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_scalars.c	Tue Sep 11 08:33:16 2012	(r240354)
 @@ -238,7 +238,7 @@ OS_getSystemProcesses(uint32_t *proc_cou
  	if (hr_kd == NULL)
  		return (SNMP_ERR_GENERR);
  
 -	if (kvm_getprocs(hr_kd, KERN_PROC_ALL, 0, &pc) == NULL) {
 +	if (kvm_getprocs(hr_kd, KERN_PROC_PROC, 0, &pc) == NULL) {
  		syslog(LOG_ERR, "kvm_getprocs failed: %m");
  		return (SNMP_ERR_GENERR);
  	}
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: patched->closed 
State-Changed-By: glebius 
State-Changed-When: Wed Oct 24 11:37:03 UTC 2012 
State-Changed-Why:  
Fixed in stable/9. 

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