From nobody@FreeBSD.org  Thu Jul 28 23:30:44 2011
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 332491065670
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 28 Jul 2011 23:30:44 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from red.freebsd.org (red.freebsd.org [IPv6:2001:4f8:fff6::22])
	by mx1.freebsd.org (Postfix) with ESMTP id 0A1058FC08
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 28 Jul 2011 23:30:44 +0000 (UTC)
Received: from red.freebsd.org (localhost [127.0.0.1])
	by red.freebsd.org (8.14.4/8.14.4) with ESMTP id p6SNUh5l033207
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 28 Jul 2011 23:30:43 GMT
	(envelope-from nobody@red.freebsd.org)
Received: (from nobody@localhost)
	by red.freebsd.org (8.14.4/8.14.4/Submit) id p6SNUhJX033206;
	Thu, 28 Jul 2011 23:30:43 GMT
	(envelope-from nobody)
Message-Id: <201107282330.p6SNUhJX033206@red.freebsd.org>
Date: Thu, 28 Jul 2011 23:30:43 GMT
From: Robert Millan <rmh@debian.org>
To: freebsd-gnats-submit@FreeBSD.org
Subject: [PATCH] Linux-like /proc/swaps for linprocfs
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         159281
>Category:       kern
>Synopsis:       [linprocfs] [patch] Linux-like /proc/swaps for linprocfs
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    kib
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Thu Jul 28 23:40:07 UTC 2011
>Closed-Date:    Fri Dec 16 09:17:31 UTC 2011
>Last-Modified:  Fri Dec 16 09:17:31 UTC 2011
>Originator:     Robert Millan
>Release:        Debian GNU/kFreeBSD "sid"
>Organization:
>Environment:
>Description:
Please consider this patch, it implements Linux-like /proc/swaps for linprocfs.

E.g.

$ cat /proc/swaps
Filename                                Type            Size    Used    Priority
/dev/zvol/dimoni/swap                   unknown         2097152 0       -1

>How-To-Repeat:

>Fix:


Patch attached with submission follows:

--- a/sys/compat/linprocfs/linprocfs.c
+++ b/sys/compat/linprocfs/linprocfs.c
@@ -498,6 +498,44 @@
 	return (0);
 }
 
+static int
+linprocfs_doswaps(PFS_FILL_ARGS)
+{
+	struct xswdev xsw;
+	int mib[3], mibsize;
+	size_t size;
+	int n;
+	long long total, used;
+	char devname[SPECNAMELEN + 1];
+
+	sbuf_printf(sb, "Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
+
+	mibsize = sizeof mib / sizeof mib[0];
+
+	mib[0] = CTL_VM;
+	mib[1] = VM_SWAP_INFO;
+
+	for (n = 0; ; n++) {
+		mib[2] = n;
+		size = sizeof(xsw);
+		if (kernel_sysctl(td, mib, mibsize, &xsw, &size, NULL, 0,
+			NULL, 0) != 0)
+			break;
+
+		size = sizeof(devname);
+		if (kernel_sysctlbyname(td, "kern.devname", devname, &size,
+			&xsw.xsw_dev, sizeof (xsw.xsw_dev), NULL, 0) != 0)
+			break;
+
+		total = (long long)xsw.xsw_nblks * PAGE_SIZE / 1024;
+		used  = (long long)xsw.xsw_used * PAGE_SIZE / 1024;
+
+		sbuf_printf(sb, "/dev/%-34s unknown\t\t%u\t%u\t-1\n", devname, total, used);
+	}
+
+	return (0);
+}
+
 /*
  * Filler function for proc/uptime
  */
@@ -1486,6 +1524,8 @@
 	    NULL, NULL, NULL, 0);
 	pfs_create_file(root, "stat", &linprocfs_dostat,
 	    NULL, NULL, NULL, PFS_RD);
+	pfs_create_file(root, "swaps", &linprocfs_doswaps,
+	    NULL, NULL, NULL, PFS_RD);
 	pfs_create_file(root, "uptime", &linprocfs_douptime,
 	    NULL, NULL, NULL, PFS_RD);
 	pfs_create_file(root, "version", &linprocfs_doversion,
--- a/sys/vm/swap_pager.c
+++ b/sys/vm/swap_pager.c
@@ -2398,7 +2398,7 @@
 
 SYSCTL_INT(_vm, OID_AUTO, nswapdev, CTLFLAG_RD, &nswapdev, 0,
     "Number of swap devices");
-SYSCTL_NODE(_vm, OID_AUTO, swap_info, CTLFLAG_RD, sysctl_vm_swap_info,
+SYSCTL_NODE(_vm, VM_SWAP_INFO, swap_info, CTLFLAG_RD, sysctl_vm_swap_info,
     "Swap statistics by device");
 
 /*
--- a/sys/vm/vm_param.h
+++ b/sys/vm/vm_param.h
@@ -84,7 +84,8 @@
 #define VM_V_PAGEOUT_FREE_MIN	9	/* cnt.v_pageout_free_min */
 #define	VM_PAGEOUT_ALGORITHM	10	/* pageout algorithm */
 #define VM_SWAPPING_ENABLED	11	/* swapping enabled */
-#define	VM_MAXID		12	/* number of valid vm ids */
+#define	VM_SWAP_INFO		12	/* swap_info */
+#define	VM_MAXID		13	/* number of valid vm ids */
 
 #define CTL_VM_NAMES { \
 	{ 0, 0 }, \
@@ -99,6 +100,7 @@
 	{ "v_pageout_free_min", CTLTYPE_UINT}, \
 	{ "pageout_algorithm", CTLTYPE_INT}, \
 	{ "swap_enabled", CTLTYPE_INT},\
+	{ "swap_info", CTLTYPE_STRUCT},\
 }
 
 /*


>Release-Note:
>Audit-Trail:

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: kern/159281: commit references a PR
Date: Mon,  1 Aug 2011 19:12:30 +0000 (UTC)

 Author: kib
 Date: Mon Aug  1 19:12:15 2011
 New Revision: 224582
 URL: http://svn.freebsd.org/changeset/base/224582
 
 Log:
   Implement the linprocfs swaps file, providing information about the
   configured swap devices in the Linux-compatible format.
   
   Based on the submission by:	Robert Millan <rmh debian org>
   PR:	kern/159281
   Reviewed by:	bde
   Approved by:	re (kensmith)
   MFC after:	2 weeks
 
 Modified:
   head/sys/compat/linprocfs/linprocfs.c
   head/sys/vm/swap_pager.c
   head/sys/vm/swap_pager.h
 
 Modified: head/sys/compat/linprocfs/linprocfs.c
 ==============================================================================
 --- head/sys/compat/linprocfs/linprocfs.c	Mon Aug  1 19:07:03 2011	(r224581)
 +++ head/sys/compat/linprocfs/linprocfs.c	Mon Aug  1 19:12:15 2011	(r224582)
 @@ -502,6 +502,33 @@ linprocfs_dostat(PFS_FILL_ARGS)
  	return (0);
  }
  
 +static int
 +linprocfs_doswaps(PFS_FILL_ARGS)
 +{
 +	struct xswdev xsw;
 +	uintmax_t total, used;
 +	int n;
 +	char devname[SPECNAMELEN + 1];
 +
 +	sbuf_printf(sb, "Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
 +	mtx_lock(&Giant);
 +	for (n = 0; ; n++) {
 +		if (swap_dev_info(n, &xsw, devname, sizeof(devname)) != 0)
 +			break;
 +		total = (uintmax_t)xsw.xsw_nblks * PAGE_SIZE / 1024;
 +		used  = (uintmax_t)xsw.xsw_used * PAGE_SIZE / 1024;
 +
 +		/*
 +		 * The space and not tab after the device name is on
 +		 * purpose.  Linux does so.
 +		 */
 +		sbuf_printf(sb, "/dev/%-34s unknown\t\t%jd\t%jd\t-1\n",
 +		    devname, total, used);
 +	}
 +	mtx_unlock(&Giant);
 +	return (0);
 +}
 +
  /*
   * Filler function for proc/uptime
   */
 @@ -1490,6 +1517,8 @@ linprocfs_init(PFS_INIT_ARGS)
  	    NULL, NULL, NULL, 0);
  	pfs_create_file(root, "stat", &linprocfs_dostat,
  	    NULL, NULL, NULL, PFS_RD);
 +	pfs_create_file(root, "swaps", &linprocfs_doswaps,
 +	    NULL, NULL, NULL, PFS_RD);
  	pfs_create_file(root, "uptime", &linprocfs_douptime,
  	    NULL, NULL, NULL, PFS_RD);
  	pfs_create_file(root, "version", &linprocfs_doversion,
 
 Modified: head/sys/vm/swap_pager.c
 ==============================================================================
 --- head/sys/vm/swap_pager.c	Mon Aug  1 19:07:03 2011	(r224581)
 +++ head/sys/vm/swap_pager.c	Mon Aug  1 19:12:15 2011	(r224582)
 @@ -2365,35 +2365,53 @@ swap_pager_status(int *total, int *used)
  	mtx_unlock(&sw_dev_mtx);
  }
  
 -static int
 -sysctl_vm_swap_info(SYSCTL_HANDLER_ARGS)
 +int
 +swap_dev_info(int name, struct xswdev *xs, char *devname, size_t len)
  {
 -	int	*name = (int *)arg1;
 -	int	error, n;
 -	struct xswdev xs;
  	struct swdevt *sp;
 -
 -	if (arg2 != 1) /* name length */
 -		return (EINVAL);
 +	char *tmp_devname;
 +	int error, n;
  
  	n = 0;
 +	error = ENOENT;
  	mtx_lock(&sw_dev_mtx);
  	TAILQ_FOREACH(sp, &swtailq, sw_list) {
 -		if (n == *name) {
 -			mtx_unlock(&sw_dev_mtx);
 -			xs.xsw_version = XSWDEV_VERSION;
 -			xs.xsw_dev = sp->sw_dev;
 -			xs.xsw_flags = sp->sw_flags;
 -			xs.xsw_nblks = sp->sw_nblks;
 -			xs.xsw_used = sp->sw_used;
 -
 -			error = SYSCTL_OUT(req, &xs, sizeof(xs));
 -			return (error);
 +		if (n != name) {
 +			n++;
 +			continue;
 +		}
 +		xs->xsw_version = XSWDEV_VERSION;
 +		xs->xsw_dev = sp->sw_dev;
 +		xs->xsw_flags = sp->sw_flags;
 +		xs->xsw_nblks = sp->sw_nblks;
 +		xs->xsw_used = sp->sw_used;
 +		if (devname != NULL) {
 +			if (vn_isdisk(sp->sw_vp, NULL))
 +				tmp_devname = sp->sw_vp->v_rdev->si_name;
 +			else
 +				tmp_devname = "[file]";
 +			strncpy(devname, tmp_devname, len);
  		}
 -		n++;
 +		error = 0;
 +		break;
  	}
  	mtx_unlock(&sw_dev_mtx);
 -	return (ENOENT);
 +	return (error);
 +}
 +
 +static int
 +sysctl_vm_swap_info(SYSCTL_HANDLER_ARGS)
 +{
 +	struct xswdev xs;
 +	int error;
 +
 +	if (arg2 != 1)			/* name length */
 +		return (EINVAL);
 +	error = swap_dev_info(*(int *)arg1, &xs, NULL, 0);
 +	if (error != 0)
 +		return (error);
 +	error = SYSCTL_OUT(req, &xs, sizeof(xs));
 +	return (error);
  }
  
  SYSCTL_INT(_vm, OID_AUTO, nswapdev, CTLFLAG_RD, &nswapdev, 0,
 
 Modified: head/sys/vm/swap_pager.h
 ==============================================================================
 --- head/sys/vm/swap_pager.h	Mon Aug  1 19:07:03 2011	(r224581)
 +++ head/sys/vm/swap_pager.h	Mon Aug  1 19:12:15 2011	(r224582)
 @@ -75,7 +75,8 @@ struct swdevt {
  extern int swap_pager_full;
  extern int swap_pager_avail;
  
 -struct swdevt;
 +struct xswdev;
 +int swap_dev_info(int name, struct xswdev *xs, char *devname, size_t len);
  void swap_pager_copy(vm_object_t, vm_object_t, vm_pindex_t, int);
  void swap_pager_freespace(vm_object_t, vm_pindex_t, vm_size_t);
  void swap_pager_swap_init(void);
 _______________________________________________
 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: open->patched 
State-Changed-By: eadler 
State-Changed-When: Thu Dec 15 16:18:18 UTC 2011 
State-Changed-Why:  
commited in r224582 


Responsible-Changed-From-To: freebsd-bugs->kib 
Responsible-Changed-By: eadler 
Responsible-Changed-When: Thu Dec 15 16:18:18 UTC 2011 
Responsible-Changed-Why:  
commited in r224582 

http://www.freebsd.org/cgi/query-pr.cgi?pr=159281 
State-Changed-From-To: patched->closed 
State-Changed-By: kib 
State-Changed-When: Fri Dec 16 09:16:49 UTC 2011 
State-Changed-Why:  
I do not need reminders. 

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