From nobody@FreeBSD.org  Thu Jan  7 18:30:35 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 947301065676
	for <freebsd-gnats-submit@FreeBSD.org>; Thu,  7 Jan 2010 18:30:35 +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 837068FC0C
	for <freebsd-gnats-submit@FreeBSD.org>; Thu,  7 Jan 2010 18:30:35 +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 o07IUZTe050896
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 7 Jan 2010 18:30:35 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.14.3/8.14.3/Submit) id o07IUY5n050895;
	Thu, 7 Jan 2010 18:30:34 GMT
	(envelope-from nobody)
Message-Id: <201001071830.o07IUY5n050895@www.freebsd.org>
Date: Thu, 7 Jan 2010 18:30:34 GMT
From: Miroslav Lachman <000.fbsd@quip.cz>
To: freebsd-gnats-submit@FreeBSD.org
Subject: [patch] Add cpuset(1) support to rc.subr
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         142434
>Category:       conf
>Synopsis:       [patch] Add cpuset(1) support to rc.subr(8)
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-rc
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Thu Jan 07 18:40:00 UTC 2010
>Closed-Date:    
>Last-Modified:  Tue Apr 26 11:50:08 UTC 2011
>Originator:     Miroslav Lachman
>Release:        7.2-RELEASE
>Organization:
codeLab.cz
>Environment:
tested on:
7.2-RELEASE-p4 GENERIC amd64
9.0-CURRENT-200912 GENERIC i386
>Description:
Add support for cpuset(1) to rc.subr
If ${name}_cpuset is specified, command will be runned on specified CPUs.

cpuset is available in STABLE branch for more than one year (7.1+), but is still not widely known / used and there is no general support to use cpuset in rc scripts.
FreeBSD can benefit from feature like this compared to other OSes.
>How-To-Repeat:

>Fix:
Apply attached patch and try something like this in /etc/rc.conf

sshd_enable="YES"
sshd_cpuset="0"
mysql_enable="YES" 
mysql_cpuset="1-3" 
lighttpd_enable="YES" 
lighttpd_cpuset="2" 
proftpd_enable="YES" 
proftpd_cpuset="1,2" 


Then after boot or restart of services, you will have services assigned to defined CPUs like this:

# /usr/local/etc/rc.d/mysql-server status
mysql is running as pid 11952.
on CPU(s) 1,2,3

# /usr/local/etc/rc.d/lighttpd status
lighttpd is running as pid 12011.
on CPU(s) 2

# /usr/local/etc/rc.d/proftpd status
proftpd is running as pid 11882.
on CPU(s) 1,2


The patch is not fully tested, but allows most currently available services to be assigned to CPUs without modification of existing rc scripts.

Note: some complex rc scripts can not use cpuset, for example rc.d/jail

patch is for 9-CURRENT but should work on older releases too

Patch attached with submission follows:

--- /etc/rc.subr.orig	2009-12-10 07:01:55.000000000 +0100
+++ /etc/rc.subr	2010-01-07 18:43:15.000000000 +0100
@@ -58,6 +58,7 @@
 IDCMD="if [ -x $ID ]; then $ID -un; fi"
 PS="/bin/ps -ww"
 JID=`$PS -p $$ -o jid=`
+CPUSET="/usr/bin/cpuset"
 
 case ${OSTYPE} in
 FreeBSD)
@@ -464,6 +465,9 @@
 #	${name}_chdir	n	Directory to cd to before running ${command}
 #				(if not using ${name}_chroot).
 #
+#	${name}_cpuset	n	A list of CPUs to run ${command} on.
+#				Requires /usr to be mounted.
+#
 #	${name}_flags	n	Arguments to call ${command} with.
 #				NOTE:	$flags from the parent environment
 #					can be used to override this.
@@ -623,6 +627,17 @@
 	_pidcmd=
 	_procname=${procname:-${command}}
 
+	eval _cpuset=\$${name}_cpuset
+	# fix for background-fsck problem / check if value start with number
+	case "$_cpuset" in
+	[0-9]*)	;;
+	*)	_cpuset="" ;;
+	esac
+	_cpusetcmd=
+	if [ -n "$_cpuset" -a -x $CPUSET ]; then
+		_cpusetcmd="$CPUSET -l $_cpuset"
+	fi
+
 					# setup pid check command
 	if [ -n "$_procname" ]; then
 		if [ -n "$pidfile" ]; then
@@ -686,7 +701,7 @@
 
 		if [ -n "$_cmd" ]; then
 			_run_rc_precmd || return 1
-			_run_rc_doit "$_cmd $rc_extra_args" || return 1
+			_run_rc_doit "$_cpusetcmd $_cmd $rc_extra_args" || return 1
 			_run_rc_postcmd
 			return $_return
 		fi
@@ -697,6 +712,11 @@
 			_run_rc_precmd || return 1
 			if [ -n "$rc_pid" ]; then
 				echo "${name} is running as pid $rc_pid."
+				# for cpuset debug only, not committable (cut)
+				if [ -n "$_cpuset" -a -x $CPUSET ]; then
+					echo -n "on CPU(s)"
+					$CPUSET -g -p "$rc_pid" | cut -s -d: -f 2
+				fi
 			else
 				echo "${name} is not running."
 				return 1
@@ -725,13 +745,13 @@
 			check_startmsgs && echo "Starting ${name}."
 			if [ -n "$_chroot" ]; then
 				_doit="\
-${_nice:+nice -n $_nice }\
+${_nice:+nice -n $_nice } $_cpusetcmd\
 chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
 $_chroot $command $rc_flags $command_args"
 			else
 				_doit="\
 ${_chdir:+cd $_chdir && }\
-$command $rc_flags $command_args"
+$_cpusetcmd $command $rc_flags $command_args"
 				if [ -n "$_user" ]; then
 				    _doit="su -m $_user -c 'sh -c \"$_doit\"'"
 				fi


>Release-Note:
>Audit-Trail:

From: Miroslav Lachman <000.fbsd@quip.cz>
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: conf/142434: [patch] Add cpuset(1) support to rc.subr
Date: Thu, 07 Jan 2010 19:58:46 +0100

 This is a multi-part message in MIME format.
 --------------000406030203050904060109
 Content-Type: text/plain; charset=ISO-8859-2; format=flowed
 Content-Transfer-Encoding: 7bit
 
 Attached patch is for 7.2-RELEASE
 
 --------------000406030203050904060109
 Content-Type: text/plain;
  name="cpuset_rc.subr_72.patch.txt"
 Content-Transfer-Encoding: 7bit
 Content-Disposition: attachment;
  filename="cpuset_rc.subr_72.patch.txt"
 
 --- /etc/rc.subr.orig	2009-05-28 17:17:57.000000000 +0200
 +++ /etc/rc.subr	2010-01-07 18:27:27.000000000 +0100
 @@ -58,6 +58,7 @@
  IDCMD="if [ -x $ID ]; then $ID -un; fi"
  PS="/bin/ps -ww"
  JID=`$PS -p $$ -o jid=`
 +CPUSET="/usr/bin/cpuset"
  
  case ${OSTYPE} in
  FreeBSD)
 @@ -421,6 +422,9 @@
  #	${name}_chdir	n	Directory to cd to before running ${command}
  #				(if not using ${name}_chroot).
  #
 +#	${name}_cpuset	n	A list of CPUs to run ${command} on.
 +#				Requires /usr to be mounted.
 +#
  #	${name}_flags	n	Arguments to call ${command} with.
  #				NOTE:	$flags from the parent environment
  #					can be used to override this.
 @@ -572,6 +576,17 @@
  	_pidcmd=
  	_procname=${procname:-${command}}
  
 +	eval _cpuset=\$${name}_cpuset
 +	# fix for background-fsck problem / check if value start with number
 +	case "$_cpuset" in
 +	[0-9]*)	;;
 +	*)	_cpuset="" ;;
 +	esac
 +	_cpusetcmd=
 +	if [ -n "$_cpuset" -a -x $CPUSET ]; then
 +		_cpusetcmd="$CPUSET -l $_cpuset"
 +	fi
 +
  					# setup pid check command
  	if [ -n "$_procname" ]; then
  		if [ -n "$pidfile" ]; then
 @@ -629,7 +644,7 @@
  
  		if [ -n "$_cmd" ]; then
  			_run_rc_precmd || return 1
 -			_run_rc_doit "$_cmd $rc_extra_args" || return 1
 +			_run_rc_doit "$_cpusetcmd $_cmd $rc_extra_args" || return 1
  			_run_rc_postcmd
  			return $_return
  		fi
 @@ -640,6 +655,11 @@
  			_run_rc_precmd || return 1
  			if [ -n "$rc_pid" ]; then
  				echo "${name} is running as pid $rc_pid."
 +				# for cpuset debug only, not committable (cut)
 +				if [ -n "$_cpuset" -a -x $CPUSET ]; then
 +					echo -n "on CPU(s)"
 +					$CPUSET -g -p "$rc_pid" | cut -s -d: -f 2
 +				fi
  			else
  				echo "${name} is not running."
  				return 1
 @@ -665,13 +685,13 @@
  			echo "Starting ${name}."
  			if [ -n "$_chroot" ]; then
  				_doit="\
 -${_nice:+nice -n $_nice }\
 +${_nice:+nice -n $_nice } $_cpusetcmd\
  chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
  $_chroot $command $rc_flags $command_args"
  			else
  				_doit="\
  ${_chdir:+cd $_chdir && }\
 -$command $rc_flags $command_args"
 +$_cpusetcmd $command $rc_flags $command_args"
  				if [ -n "$_user" ]; then
  				    _doit="su -m $_user -c 'sh -c \"$_doit\"'"
  				fi
 
 --------------000406030203050904060109--
Responsible-Changed-From-To: freebsd-bugs->freebsd-rc 
Responsible-Changed-By: linimon 
Responsible-Changed-When: Thu Jan 7 19:15:18 UTC 2010 
Responsible-Changed-Why:  
Over to maintainer(s). 

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

From: Miroslav Lachman <000.fbsd@quip.cz>
To: bug-followup@FreeBSD.org, 000.fbsd@quip.cz
Cc:  
Subject: Re: conf/142434: [patch] Add cpuset(1) support to rc.subr(8)
Date: Tue, 26 Apr 2011 13:24:19 +0200

 Can some committer reply to this PR if there are any interest to have 
 this feature in base rc.subr for 9.0 RELEASE?
 Should I change something in the patch?
 
 Otherwise this PR can be closed if there is some reason not to have it.
 
 Miroslav Lachman
>Unformatted:
