diff options
author | Piotr Pawel Stefaniak <pstef@FreeBSD.org> | 2020-06-27 19:09:33 +0000 |
---|---|---|
committer | Piotr Pawel Stefaniak <pstef@FreeBSD.org> | 2020-06-27 19:09:33 +0000 |
commit | 4f47f5111f820343df1510600605fe6bd0259571 (patch) | |
tree | 87615f629766a4927a8f6d4f2f7feb70e41d5704 /bin/ps | |
parent | 2dcf8c45934e4632a7d5780afc0058554245f1f1 (diff) | |
download | src-4f47f5111f820343df1510600605fe6bd0259571.tar.gz src-4f47f5111f820343df1510600605fe6bd0259571.zip |
ps(1): reuse keyword "cpu" to show CPU number
This flag will now show the processor number on which a process is running.
This change was inspired by PR129965. Initially I didn't think that the
patch attached to it was correct -- it sacrificed ki_estcpu use in "cpu"
for ki_lastcpu and I thought that the old functionality should be kept and
the new (cpu#) one added to it. But I've since discovered that ki_estcpu is
sched_4bsd-specific. What's worse, it represents the same thing as
ki_pctcpu, except ki_pctcpu is universal -- so "%cpu" has been using it
successfully. Therefore, I've decided to replace information based on
ki_estcpu with information based on ki_oncpu/ki_lastcpu.
Key parts of the code and manual changes were borrowed from top(1).
PR: 129965
Reported by: Nikola Knežević
MFC after: 1 week
Differential Revision: https://reviews.freebsd.org/D25377
Notes
Notes:
svn path=/head/; revision=362705
Diffstat (limited to 'bin/ps')
-rw-r--r-- | bin/ps/extern.h | 3 | ||||
-rw-r--r-- | bin/ps/keyword.c | 3 | ||||
-rw-r--r-- | bin/ps/print.c | 16 | ||||
-rw-r--r-- | bin/ps/ps.1 | 5 | ||||
-rw-r--r-- | bin/ps/ps.c | 15 |
5 files changed, 37 insertions, 5 deletions
diff --git a/bin/ps/extern.h b/bin/ps/extern.h index dc58b1e9d00b..2e72632b6542 100644 --- a/bin/ps/extern.h +++ b/bin/ps/extern.h @@ -41,13 +41,14 @@ extern fixpt_t ccpu; extern int cflag, eval, fscale, nlistread, rawcpu; extern unsigned long mempages; extern time_t now; -extern int showthreads, sumrusage, termwidth; +extern int showthreads, sumrusage, termwidth, smp; extern STAILQ_HEAD(velisthead, varent) varlist; __BEGIN_DECLS char *arguments(KINFO *, VARENT *); char *command(KINFO *, VARENT *); char *cputime(KINFO *, VARENT *); +char *cpunum(KINFO *, VARENT *); int donlist(void); char *elapsed(KINFO *, VARENT *); char *elapseds(KINFO *, VARENT *); diff --git a/bin/ps/keyword.c b/bin/ps/keyword.c index d1253dc9ea3a..2a8382a2273e 100644 --- a/bin/ps/keyword.c +++ b/bin/ps/keyword.c @@ -83,8 +83,7 @@ static VAR var[] = { CHAR, NULL, 0}, {"cow", "COW", NULL, "copy-on-write-faults", 0, kvar, KOFF(ki_cow), UINT, "u", 0}, - {"cpu", "CPU", NULL, "cpu-usage", 0, kvar, KOFF(ki_estcpu), UINT, "d", - 0}, + {"cpu", "C", NULL, "on-cpu", 0, cpunum, 0, CHAR, NULL, 0}, {"cputime", "", "time", NULL, 0, NULL, 0, CHAR, NULL, 0}, {"dsiz", "DSIZ", NULL, "data-size", 0, kvar, KOFF(ki_dsize), PGTOK, "ld", 0}, diff --git a/bin/ps/print.c b/bin/ps/print.c index 98a0142031d4..d137056aa580 100644 --- a/bin/ps/print.c +++ b/bin/ps/print.c @@ -551,6 +551,22 @@ cputime(KINFO *k, VARENT *ve) } char * +cpunum(KINFO *k, VARENT *ve __unused) +{ + char *cpu; + + if (!smp) + return (NULL); + + if (k->ki_p->ki_stat == SRUN && k->ki_p->ki_oncpu != NOCPU) { + asprintf(&cpu, "%d", k->ki_p->ki_oncpu); + } else { + asprintf(&cpu, "%d", k->ki_p->ki_lastcpu); + } + return (cpu); +} + +char * systime(KINFO *k, VARENT *ve) { long secs, psecs; diff --git a/bin/ps/ps.1 b/bin/ps/ps.1 index d3d279d76d43..25373adb7d73 100644 --- a/bin/ps/ps.1 +++ b/bin/ps/ps.1 @@ -29,7 +29,7 @@ .\" @(#)ps.1 8.3 (Berkeley) 4/18/94 .\" $FreeBSD$ .\" -.Dd October 31, 2018 +.Dd June 27, 2020 .Dt PS 1 .Os .Sh NAME @@ -545,7 +545,8 @@ command and arguments .It Cm cow number of copy-on-write faults .It Cm cpu -short-term CPU usage factor (for scheduling) +The processor number on which the process is executing (visible only on SMP +systems). .It Cm dsiz data size (in Kbytes) .It Cm emul diff --git a/bin/ps/ps.c b/bin/ps/ps.c index f6d32e5411fe..0bc388c25260 100644 --- a/bin/ps/ps.c +++ b/bin/ps/ps.c @@ -105,6 +105,7 @@ int rawcpu; /* -C */ int sumrusage; /* -S */ int termwidth; /* Width of the screen (0 == infinity). */ int showthreads; /* will threads be shown? */ +int smp; /* multi-processor support */ struct velisthead varlist = STAILQ_HEAD_INITIALIZER(varlist); @@ -157,6 +158,7 @@ static void saveuser(KINFO *); static void scanvars(void); static void sizevars(void); static void pidmax_init(void); +static void smp_init(void); static void usage(void); static char dfmt[] = "pid,tt,state,time,command"; @@ -226,6 +228,7 @@ main(int argc, char *argv[]) argv[1] = kludge_oldps_options(PS_ARGS, argv[1], argv[2]); pidmax_init(); + smp_init(); all = descendancy = _fmt = nselectors = optfatal = 0; prtheader = showthreads = wflag = xkeep_implied = 0; @@ -1456,6 +1459,18 @@ pidmax_init(void) } } +static void +smp_init(void) +{ + size_t size; + + size = sizeof(smp); + if ((sysctlbyname("machdep.smp_active", &smp, &size, NULL, 0) != 0 && + sysctlbyname("kern.smp.active", &smp, &size, NULL, 0) != 0) || + size != sizeof(smp)) + smp = 0; +} + static void __dead2 usage(void) { |