From a96f5ce401286f2c948de6ff55523e59970006c9 Mon Sep 17 00:00:00 2001 From: trociny Date: Thu, 2 Feb 2012 18:22:25 +0000 Subject: [PATCH] MFC r227956, r228090, r228446, r230471, r230548: r227956: Add -l flag to display resource limits. PR: bin/161257 Reviewed by: kib r228090: Update SYNOPSIS to include the flags added recently. Spotted by: jhb r228446: Make procstat -l output similar to the output of limits(1). Suggested by: jhb r230471, r230548: Make procstat -l to work with the new version of kern.proc.rlimit. Submitted by: Andrey Zonov git-svn-id: svn://svn.freebsd.org/base/stable/9@230918 ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f --- usr.bin/procstat/Makefile | 1 + usr.bin/procstat/procstat.1 | 6 +- usr.bin/procstat/procstat.c | 18 +++-- usr.bin/procstat/procstat.h | 1 + usr.bin/procstat/procstat_rlimit.c | 119 +++++++++++++++++++++++++++++ 5 files changed, 137 insertions(+), 8 deletions(-) create mode 100644 usr.bin/procstat/procstat_rlimit.c diff --git a/usr.bin/procstat/Makefile b/usr.bin/procstat/Makefile index 07b8fb1c9..76c6fa363 100644 --- a/usr.bin/procstat/Makefile +++ b/usr.bin/procstat/Makefile @@ -10,6 +10,7 @@ SRCS= procstat.c \ procstat_cred.c \ procstat_files.c \ procstat_kstack.c \ + procstat_rlimit.c \ procstat_sigs.c \ procstat_threads.c \ procstat_vm.c diff --git a/usr.bin/procstat/procstat.1 b/usr.bin/procstat/procstat.1 index b9a6a4c30..6f3a28741 100644 --- a/usr.bin/procstat/procstat.1 +++ b/usr.bin/procstat/procstat.1 @@ -25,7 +25,7 @@ .\" .\" $FreeBSD$ .\" -.Dd November 22, 2011 +.Dd November 28, 2011 .Dt PROCSTAT 1 .Os .Sh NAME @@ -37,7 +37,7 @@ .Op Fl n .Op Fl C .Op Fl w Ar interval -.Op Fl b | c | f | i | j | k | s | t | v +.Op Fl b | c | e | f | i | j | k | l | s | t | v | x .Op Fl a | Ar pid ... .Sh DESCRIPTION The @@ -69,6 +69,8 @@ Display the stacks of kernel threads in the process, excluding stacks of threads currently running on a CPU and threads with stacks swapped to disk. If the flag is repeated, function offsets as well as function names are printed. +.It Fl l +Display resource limits for the process. .It Fl s Display security credential information for the process. .It Fl t diff --git a/usr.bin/procstat/procstat.c b/usr.bin/procstat/procstat.c index 680325bee..b0cee6b4e 100644 --- a/usr.bin/procstat/procstat.c +++ b/usr.bin/procstat/procstat.c @@ -39,8 +39,8 @@ #include "procstat.h" -static int aflag, bflag, cflag, eflag, fflag, iflag, jflag, kflag, sflag, tflag; -static int vflag, xflag; +static int aflag, bflag, cflag, eflag, fflag, iflag, jflag, kflag, lflag, sflag; +static int tflag, vflag, xflag; int hflag, nflag, Cflag; static void @@ -50,7 +50,7 @@ usage(void) fprintf(stderr, "usage: procstat [-h] [-C] [-M core] [-N system] " "[-w interval] \n"); fprintf(stderr, " [-b | -c | -e | -f | -i | -j | -k | " - "-s | -t | -v | -x] [-a | pid ...]\n"); + "-l | -s | -t | -v | -x] [-a | pid ...]\n"); exit(EX_USAGE); } @@ -72,6 +72,8 @@ procstat(struct procstat *prstat, struct kinfo_proc *kipp) procstat_threads_sigs(prstat, kipp); else if (kflag) procstat_kstack(kipp, kflag); + else if (lflag) + procstat_rlimit(kipp); else if (sflag) procstat_cred(kipp); else if (tflag) @@ -123,7 +125,7 @@ main(int argc, char *argv[]) interval = 0; memf = nlistf = NULL; - while ((ch = getopt(argc, argv, "CN:M:abcefijkhstvw:x")) != -1) { + while ((ch = getopt(argc, argv, "CN:M:abcefijklhstvw:x")) != -1) { switch (ch) { case 'C': Cflag++; @@ -167,6 +169,10 @@ main(int argc, char *argv[]) kflag++; break; + case 'l': + lflag++; + break; + case 'n': nflag++; break; @@ -210,8 +216,8 @@ main(int argc, char *argv[]) argv += optind; /* We require that either 0 or 1 mode flags be set. */ - tmp = bflag + cflag + eflag + fflag + (kflag ? 1 : 0) + sflag + tflag + - vflag + xflag; + tmp = bflag + cflag + eflag + fflag + (kflag ? 1 : 0) + lflag + sflag + + tflag + vflag + xflag; if (!(tmp == 0 || tmp == 1)) usage(); diff --git a/usr.bin/procstat/procstat.h b/usr.bin/procstat/procstat.h index 2f722d021..e65436dad 100644 --- a/usr.bin/procstat/procstat.h +++ b/usr.bin/procstat/procstat.h @@ -42,6 +42,7 @@ void procstat_cred(struct kinfo_proc *kipp); void procstat_env(struct kinfo_proc *kipp); void procstat_files(struct procstat *prstat, struct kinfo_proc *kipp); void procstat_kstack(struct kinfo_proc *kipp, int kflag); +void procstat_rlimit(struct kinfo_proc *kipp); void procstat_sigs(struct procstat *prstat, struct kinfo_proc *kipp); void procstat_threads(struct kinfo_proc *kipp); void procstat_threads_sigs(struct procstat *prstat, struct kinfo_proc *kipp); diff --git a/usr.bin/procstat/procstat_rlimit.c b/usr.bin/procstat/procstat_rlimit.c new file mode 100644 index 000000000..c5429c4dd --- /dev/null +++ b/usr.bin/procstat/procstat_rlimit.c @@ -0,0 +1,119 @@ +/*- + * Copyright (c) 2011 Mikolaj Golub + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#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 +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) +{ + struct rlimit rlimit; + int error, i, name[5]; + size_t len; + + if (!hflag) { + printf("%5s %-16s %-16s %16s %16s\n", + "PID", "COMM", "RLIMIT", "SOFT ", "HARD "); + } + len = sizeof(struct rlimit); + name[0] = CTL_KERN; + name[1] = KERN_PROC; + name[2] = KERN_PROC_RLIMIT; + name[3] = kipp->ki_pid; + for (i = 0; i < RLIM_NLIMITS; i++) { + name[4] = i; + error = sysctl(name, 5, &rlimit, &len, NULL, 0); + if (error < 0 && errno != ESRCH) { + warn("sysctl: kern.proc.rlimit: %d", kipp->ki_pid); + return; + } + if (error < 0 || len != sizeof(struct rlimit)) + return; + + printf("%5d %-16s %-16s ", kipp->ki_pid, kipp->ki_comm, + rlimit_param[i].name); + printf("%16s ", humanize_rlimit(i, rlimit.rlim_cur)); + printf("%16s\n", humanize_rlimit(i, rlimit.rlim_max)); + } +} -- 2.45.0