Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 10 Dec 2011 10:26:46 +0200
From:      Mikolaj Golub <trociny@freebsd.org>
To:        John Baldwin <jhb@FreeBSD.org>
Cc:        svn-src-head@freebsd.org, Robert Watson <rwatson@freebsd.org>, svn-src-all@freebsd.org, src-committers@freebsd.org, Kostik Belousov <kib@freebsd.org>
Subject:   Re: svn commit: r227956 - head/usr.bin/procstat
Message-ID:  <86y5ukvnll.fsf@kopusha.home.net>
In-Reply-To: <4EE12CE0.5070803@FreeBSD.org> (John Baldwin's message of "Thu, 08 Dec 2011 16:32:16 -0500")
References:  <201111242054.pAOKs6vj012296@svn.freebsd.org> <201111281330.11720.jhb@freebsd.org> <86liqt1ier.fsf@kopusha.home.net> <4EE1024C.6040800@FreeBSD.org> <86k466aip3.fsf@kopusha.home.net> <4EE12CE0.5070803@FreeBSD.org>

next in thread | previous in thread | raw e-mail | index | archive | help
--=-=-=


On Thu, 08 Dec 2011 16:32:16 -0500 John Baldwin wrote:

 >>   JB>  Hmm, I would stick as close to limits output as possible.  I would
 >>   JB>  consider duplicating the unit field in each of soft and hard, so you
 >>   JB>  end up with something like this:
 >>
 >>   JB>    PID  COMM            RLIMIT     SOFT           HARD
 >>   JB>  48798  zsh             cputime      100000 secs  infinity secs
 >>   JB>  48798  zsh             filesize   infinity kb    infinity kb
 >>   JB>  48798  zsh             datasize     524288 kb      524288 kb
 >>
 >>   JB>  etc.
 >>
 >> Ok.
 >>
 >>   JB>  (Things like 'openfiles' is simply more intuitive than 'nofile' (no
 >>   JB>  file?, huh? oh, num open files.. (except not all users will make the
 >>   JB>  last step there).
 >>
 >> Then why do we have so non-intuitive rlimit_ident names?
 >>
 >> It looks like they are used only in procfs_rlimit.c. Do procfs(5) users always
 >> make that last step with 'nofile'? :-)

 JB> Well, I suspect it's best not to change the names in procfs in case
 JB> there are existing binaries that parse the output of that file
 JB> (unfortunately).

 >> Is it possible to change rlimit_ident names? Just to ones that are used by
 >> limit(1) or (if they look too long) to something like below:

 JB> Hmm, I have no idea what other things might use rlimit_ident.  Probably
 JB> not many.  (Also, for fun, note that the 'ulimit' and 'limit' built-in
 JB> commands in sh and csh also have their own sets of names, fun!)  I would
 JB> maybe add a rlimit_names[] (just leave rlimit_ident alone), and give
 JB> that the names from limits(1), and change both procstat and sh's
 JB> ulimit' command to use those.

Adding yet another rlimit names to the header file does not look so attractive
for me as it was just using/reusing what we had :-). So I decided to hardcode
the names in procstat_rlimit.c (see the attached patch).

Output example:

  PID COMM             RLIMIT                  SOFT             HARD     
11949 zsh              cputime                10000 sec     infinity     
11949 zsh              filesize            infinity         infinity     
11949 zsh              datasize              524288 kB        524288 kB  
11949 zsh              stacksize              65536 kB         65536 kB  
11949 zsh              coredumpsize          190734 MB        190734 MB  
11949 zsh              memoryuse           infinity         infinity     
11949 zsh              memorylocked        infinity         infinity     
11949 zsh              maxprocesses            5547             5547     
11949 zsh              openfiles              11095            11095     
11949 zsh              sbsize              infinity         infinity     
11949 zsh              vmemoryuse          infinity         infinity     
11949 zsh              pseudo-terminals    infinity         infinity     
11949 zsh              swapuse             infinity         infinity     

-- 
Mikolaj Golub


--=-=-=
Content-Type: text/x-patch
Content-Disposition: inline; filename=procstat_rlimit.c.patch

Index: usr.bin/procstat/procstat_rlimit.c
===================================================================
--- usr.bin/procstat/procstat_rlimit.c	(revision 228285)
+++ usr.bin/procstat/procstat_rlimit.c	(working copy)
@@ -28,7 +28,6 @@
 
 #include <sys/param.h>
 #include <sys/time.h>
-#define _RLIMIT_IDENT
 #include <sys/resourcevar.h>
 #include <sys/sysctl.h>
 #include <sys/user.h>
@@ -36,6 +35,7 @@
 #include <err.h>
 #include <errno.h>
 #include <libprocstat.h>
+#include <libutil.h>
 #include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -43,17 +43,60 @@
 
 #include "procstat.h"
 
+static struct {
+	const char *name;
+	const char *suffix;
+} rlimit_param[13] = {
+	{"cputime",          "sec"},
+	{"filesize",         "B  "},
+	{"datasize",         "B  "},
+	{"stacksize",        "B  "},
+	{"coredumpsize",     "B  "},
+	{"memoryuse",        "B  "},
+	{"memorylocked",     "B  "},
+	{"maxprocesses",     "   "},
+	{"openfiles",        "   "},
+	{"sbsize",           "B  "},
+	{"vmemoryuse",       "B  "},
+	{"pseudo-terminals", "   "},
+	{"swapuse",          "B  "},
+};
+
+#if RLIM_NLIMITS > 13
+#error "Resource limits have grown. Add new entries to rlimit_param[]."
+#endif
+
 static struct rlimit rlimit[RLIM_NLIMITS];
 
+static
+const char *humanize_rlimit(int indx, rlim_t limit)
+{
+	static char buf[14];
+	int scale;
+
+	if (limit == RLIM_INFINITY)
+		return "infinity     ";
+
+	scale = humanize_number(buf, sizeof(buf) - 1, (int64_t)limit,
+	    rlimit_param[indx].suffix, HN_AUTOSCALE | HN_GETSCALE, HN_DECIMAL);
+	(void)humanize_number(buf, sizeof(buf) - 1, (int64_t)limit,
+	    rlimit_param[indx].suffix, HN_AUTOSCALE, HN_DECIMAL);
+	/* Pad with one space if there is no suffix prefix. */
+	if (scale == 0)
+		sprintf(buf + strlen(buf), " ");
+	return (buf);
+}
+
 void
 procstat_rlimit(struct kinfo_proc *kipp)
 {
 	int error, i, name[4];
 	size_t len;
 
-	if (!hflag)
-		printf("%5s %-16s %-10s %12s %12s\n", "PID", "COMM", "RLIMIT",
-		    "CURRENT", "MAX");
+	if (!hflag) {
+		printf("%5s %-16s %-16s %16s %16s\n",
+		    "PID", "COMM", "RLIMIT", "SOFT     ", "HARD     ");
+	}
 	name[0] = CTL_KERN;
 	name[1] = KERN_PROC;
 	name[2] = KERN_PROC_RLIMIT;
@@ -68,11 +111,9 @@ procstat_rlimit(struct kinfo_proc *kipp)
 		return;
 
 	for (i = 0; i < RLIM_NLIMITS; i++) {
-		printf("%5d %-16s %-10s %12jd %12jd\n", kipp->ki_pid,
-		    kipp->ki_comm, rlimit_ident[i],
-		    rlimit[i].rlim_cur == RLIM_INFINITY ?
-		    -1 : rlimit[i].rlim_cur,
-		    rlimit[i].rlim_max == RLIM_INFINITY ?
-		    -1 : rlimit[i].rlim_max);
+		printf("%5d %-16s %-16s ", kipp->ki_pid, kipp->ki_comm,
+		    rlimit_param[i].name);
+		printf("%16s ", humanize_rlimit(i, rlimit[i].rlim_cur));
+		printf("%16s\n", humanize_rlimit(i, rlimit[i].rlim_max));
         }
 }

--=-=-=--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?86y5ukvnll.fsf>