History log of /openbsd-current/usr.bin/top/machine.c
Revision (<<< Hide revision tags) (Show revision tags >>>) Date Author Comments
# 1.113 07-Jan-2023 guenther

Add {get,set}thrname(2) for putting thread names in the kernel and
exposed in a new field returned by sysctl(KERN_PROC). Update
pthread_{get,set}_name_np(3) to use the syscalls. Show them, when
set, in ps -H and top -H output.

libc and libpthread minor bumps

ok mpi@, mvs@, deraadt@


Revision tags: OPENBSD_7_2_BASE
# 1.112 10-Sep-2022 cheloha

top(1): remove last vestiges of "last pid" support

millert@ removed most of the "last pid" support from top(1) in 1997.
See, e.g. top/machine.c,v1.7:

http://cvsweb.openbsd.org/src/usr.bin/top/machine.c?rev=1.7&content-type=text/x-cvsweb-markup

Let's remove the rest of it:

- Eliminate system_info.last_pid.

- Remove mpid parameter and "last pid" printing code from i_loadave().

Link: https://marc.info/?l=openbsd-tech&m=166277253606823&w=2

ok millert@


Revision tags: OPENBSD_7_1_BASE
# 1.111 22-Feb-2022 deraadt

MAXCOMLEN is no longer needed in these programs, so remove the annotation
from sys/param.h include lines, or remove the include lines entirely if
it this was the least requirement.
ok millert


Revision tags: OPENBSD_6_8_BASE OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.110 26-Aug-2020 kn

Add "t" to toggle the display of routing tables

Swap the WAIT column with RTABLE (and vice versa); WAIT is wide enough to
fit RTABLE, somewhat adds additional value to STATE and seems therefore most
appropiate to hide in favour of RTABLE.

Filtering rtables with "T" does not toggle the column, just like filtering
users with "u" does not toggle between user and thread id.

Feedback jmc
OK remi


# 1.109 25-Aug-2020 kn

Rename rtable filter variables and simplify buffer handling

No functional change, the rename is for consistency and the buffer
handling reduces churn in an upcoming diff.


# 1.108 23-Aug-2020 kn

Filter by routing table

"-T-0" for processes outside the default routing table, "-T3" for those in
a specific one; same semantics as with other filters.

Manual wording and command line flag taken from pgrep(1) being the only way
to identify processes by routing table; After netstat(1)'s recent addition
of "-R", filtering in top makes for handy tooling around rtable(4).

"looks good to me" millert
OK remi


# 1.107 06-Jul-2020 kn

Use mnemonic KERN_PROC_ALL not literal zero

No object change.


# 1.106 26-Jun-2020 kn

Remove duplicate initialisation

Those are global variables are (zero) initialised as such already and
machine_init() is called only once upon startup.

OK mvs


# 1.105 25-Jun-2020 kn

Remove unused "remaining" member in struct handle

Only ever set or decremented since import with machine.c r1.1 (1997);

While here, simplify the skip semantics behind the scroll functionality.

OK millert


# 1.104 24-Jun-2020 kn

Remove obsolete comment about "extra nulls"

Those were removed in r1.39 in 2004.


# 1.103 23-Jun-2020 kn

Remove redundant NULL check

For the sake of simplicity and to reflect that the process list is always
sorted (default is "cpu"), even if not explicitly requested; this makes it
easier to argue about the code around sort order and its selection.

OK millert


Revision tags: OPENBSD_6_7_BASE
# 1.102 06-Jan-2020 zhuk

Implement scrolling in top(1) using 9 and 0 keys.

Planned improvements: scroll position displaying, using arrow/pgup/pgdown keys.

okay tedu@


# 1.101 16-Dec-2019 guenther

Delete tests for P_THREAD that predate the existence of
KERN_PROC_SHOW_THREADS and have been rendered superfluous by it.
Similarly, some P_SYSTEM tests can be deleted or pushed to the
kernel by using KERN_PROC_ALL instead of KERN_PROC_KTHREAD.

ok visa@ mpi@


Revision tags: OPENBSD_6_6_BASE
# 1.100 08-Oct-2019 kn

Simplify threads flag handling

Instead of always passing the threads flag which decides whether other
function arguments are used or not, make callers pass those arguments or
NULL depending on the threads flag.

This reflects better how thread IDs are shown in place of user names/IDs.

OK millert


# 1.99 06-Oct-2019 kn

Avoid gasting around get_process_info()

get_process_info() returns a pointer to the global handle later only be
used in format_next_process(); treat this struct handle as such without
casting the pointer to caddr_t and back again.

No object change.
OK millert deraadt


# 1.98 06-Oct-2019 kn

Zap intermediate structs in compare_*()

SETORDER() can assign from function arguments directly without additional
identical structs in each function.

No object change.
OK millert


# 1.97 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


Revision tags: OPENBSD_6_5_BASE
# 1.96 28-Nov-2018 kn

Allow reverse sort order

Prefixing the field with a dash will sort processes in reverse order.
`o -pid' will therefore list PID 1 on top.

"looks good" tedu, improvements and OK cheloha


# 1.95 17-Nov-2018 cheloha

Add new KERN_CPUSTATS sysctl(2) so we can identify offline CPUs.

Because of hw.smt we need a way to determine whether a given CPU is "online"
or "offline" from userspace. KERN_CPTIME2 is an array, and so cannot be
cleanly extended for this purpose, so add a new sysctl(2) KERN_CPUSTATS
with an extensible struct. At the moment it's just KERN_CPTIME2 with a
flags member, but it can grow as needed.

KERN_CPUSTATS appears to have been defined by BSDi long ago, but there are
few (if any) packages in the wild still using the symbol so breakage in ports
should be near zero. No other system inherited the symbol from BSDi, either.

Then, use the new sysctl(2) in systat(1) and top(1):

- systat(1) draws placeholder marks ('-') instead of percentages for
offline CPUs in the cpu view.

- systat(1) omits offline CPU ticks when drawing the "big bar" in
the vmstat view. The upshot is that the bar isn't half idle when
half your logical CPUs are disabled.

- top(1) does not draw lines for offline CPUs; if CPUs toggle on or
offline in interactive mode we redraw the display to expand/reduce
space for the new/missing CPUs. This is consistent with what some
top(1) implementations do on Linux.

- top(1) omits offline CPUs from the totals when CPU totals are
combined into a single line (the '-1' flag).

Originally prompted by deraadt@. Discussed endlessly with deraadt@,
ketennis@, and sthen@. Tested by jmc@ and jca@. Earlier versions also
discussed with jca@. Earlier versions tested by jmc@, tb@, and many
others.

docs ok jmc@, kernel bits ok ketennis@, everything ok sthen@,
"Is your stuff in yet?" deraadt@


Revision tags: OPENBSD_6_4_BASE
# 1.94 05-Oct-2018 cheloha

Revert KERN_CPTIME2 ENODEV changes in kernel and userspace.

ok kettenis deraadt


# 1.93 26-Sep-2018 cheloha

KERN_CPTIME2: set ENODEV if the CPU is offline.

This lets userspace distinguish between idle CPUs and those that are
not schedulable because hw.smt=0.

A subsequent commit probably needs to add documentation for this
to sysctl.2 (and perhaps elsewhere) after the dust settles.

Also included here are changes to systat(1) and top(1) that account
for the ENODEV case and adjust behavior accordingly:

- systat(1)'s cpu view prints placeholder marks ('-') instead of
percentages for each state if the given CPU is offline.

- systat(1)'s vmstat view checks for offline CPUs when computing the
machine state total and excludes them, so the CPU usage graph
only represents the states for online CPUs.

- top(1) does not draw CPU rows for offline CPUs when the view is
redrawn. If CPUs "go offline", percentages for each state are
replaced by placeholder marks ('-'); the view will need to be
redrawn to remove these rows. If CPUs "go online" the view will
need to be redrawn to show these new CPUs. In "combined CPU" mode,
the count and the state totals only represent online CPUs.

Ports using KERN_CPTIME2 will need to be updated. The changes
described above to make systat(1) and top(1) aware of the ENODEV
case *and* gracefully handle a changing HW_NCPUONLINE while the
application is running are not necessarily appropriate for each
and every port.

The changes described above are so extensive in part to demonstrate
one way a program *might* be made robust to changing CPU availability.
In particular, changing hw.smt after boot is an extremely rare event,
and this needs to be weighed when updating ports.

The logic needed to account for the KERN_CPTIME2 ENODEV case is
very roughly:

if (sysctl(...) == -1) {
if (errno != ENODEV) {
/* Actual error occurred. */
} else {
/* CPU is offline. */
}
} else {
/* CPU is online and CPU states were set by sysctl(2). */
}

Prompted by deraadt@. Basic idea for ENODEV from kettenis@. Discussed at
length with kettenis@. Additional testing by tb@.

No complaints from hackers@ after a week.

ok kettenis@, "I think you should commit [now]" deraadt@


# 1.92 22-Sep-2018 millert

Use user_from_uid() and uid_from_user() directly. The wrappers
in username.c are now so simple there is no longer a good reason
to use them. OK deraadt@


# 1.91 13-Sep-2018 millert

Fix warnings caused by user_from_uid() and group_from_gid() now
returning const char *.


# 1.90 14-May-2018 mpi

Stopping counting and reporting CPU time spent spinning on a lock as
system time.

Introduce a new CP_SPIN "scheduler state" and modify userland tools
to display the % of timer a CPU spents spinning.

Based on a diff from jmatthew@, ok pirofti@, bluhm@, visa@, deraadt@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.89 30-May-2017 tedu

avoid some shadow warnings. from Brian Callahan


Revision tags: OPENBSD_6_1_BASE
# 1.88 15-Mar-2017 deraadt

annoying whitespace die die die


# 1.87 28-Jul-2016 tedu

rework realloc loop. there's no need to shrink the allocation between
calls. if we need a big space once, we'll likely need a big space again.


Revision tags: OPENBSD_6_0_BASE
# 1.86 11-May-2016 edd

Allow top(1) to filter process args if they are being displayed.

OK tedu@, deraadt@, and with input from Michal Mazurek.

Thanks


Revision tags: OPENBSD_5_9_BASE
# 1.85 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_8_BASE
# 1.84 06-May-2015 mpi

Display thread IDs instead of the name of the process's owner when "-H"
is used.

The rationnal is that when you're looking at threads you're generally
already filtereing by PID and this allow you to see which thread is a
pig.

Written some time ago with mikeb@

ok sthen@, krw@, guenther@


Revision tags: OPENBSD_5_7_BASE
# 1.83 19-Jan-2015 millert

Kill loadavg.h and just use the fscale value directly.
There's no need for crazy casts of p_pctcpu in ORDERKEY_PCTCPU
since kinfo_proc exports it as uint32_t. OK deraadt@


# 1.82 19-Jan-2015 deraadt

like in ps(1), fetch the FSCALE value using sysctl rather than using
the header version
ok guenther


# 1.81 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.80 17-Sep-2014 dlg

if there are more than 8 cpus, default to combined cpu stats (like
you passed -1 on the command line).

ok kettenis@ tedu@


# 1.79 15-Sep-2014 miod

Remove non-standard <sys/dkstat.h> header. It has not contained anything
related to disk stastics for almost 17 years, and the remaining
userland-visible defines duplicate those found in <sys/sched.h>.

Move the remaining _KERNEL defines to <sys/tty.h> where they belong, and
update all users to cope with this.

ok kettenis@


Revision tags: OPENBSD_5_6_BASE
# 1.78 04-Jul-2014 guenther

Track whether a process is a zombie or not yet fully built via flags
PS_{ZOMBIE,EMBRYO} on the process instead of peeking into the process's
thread data. This eliminates the need for the thread-level SDEAD state.

Change kvm_getprocs() (both the sysctl() and kvm backends) to report the
"most active" scheduler state for the process's threads.

tweaks kettenis@
feedback and ok matthew@


# 1.77 08-Apr-2014 mpi

Use VM_UVMEXP instead of VM_METER for memory usages and directly
include <sys/vmmeter.h> where it is needed instead of relying on
it being included by <uvm/uvm_extern.h>.

miod@ likes it, ok guenther@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.76 23-Mar-2013 tedu

wchan is no longer exposed, just check if wmesg is not empty.
(wmesg hasn't itself been a pointer since conversion to kinfo_proc.)
noticed by sthen.
ok deraadt jsing millert sthen


Revision tags: OPENBSD_5_3_BASE
# 1.75 18-Dec-2012 millert

We no longer use struct eproc for kinfo_proc in sysctl.h so there
is no direct need for sys/proc.h or sys/resource.h. Some consumers
of kinfo_proc need these for the proc flags and rlimit defines like
RLIM_INF so add the appropriate includes to them.
OK deraadt@ sthen@


Revision tags: OPENBSD_5_2_BASE
# 1.74 09-Jul-2012 deraadt

According to miod, KERNBASE used as a userland symbol should die
in a fire. Start by removing the definitions, before we clean the
headers later.


# 1.73 05-Jun-2012 brynet

Add support for hiding a user's processes in top.

feedback & ok lum@


# 1.72 21-Apr-2012 guenther

Remove a check made superfluous by the KERN_PROC_SHOW_THREADS flag.
This lets new binaries work (at least w/o the -H option) with old kernels.

ok pirofti@ deraadt@


# 1.71 17-Apr-2012 pirofti

Make it optional for kvm_getprocs() (and related sysctl) to return
thread information.

Add a KERN_PROC_SHOW_THREADS flag that has to be set in order to get the
thread info and make it off by default. This creates backwards compatibility
for applications that relied on a given size/number of items to be returned.

Modify ps(1) and top(1) accordingly.

Okay guenther@.


# 1.70 12-Apr-2012 pirofti

Add per thread accounting, mainly for usage & friends.

This expands the already bloated FILL_KPROC macro to take an extra
parameter that indicates if the callee is a thread or a process.

The userland bits are adjusted accordingly and ps(1) and top(1) now
display per thread usage times when -H is used.

Also pkill(1) had to be adjusted so that duplicates don't pop up.

libkvm does basically the same thing as the kernel bits.

Okay guenther@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.69 12-Jul-2011 tedu

print out the amount of memory used for cache. ok otto


# 1.68 10-Apr-2011 guenther

Switch back from KERN_PROC2/kinfo_proc2 to KERN_PROC/kinfo_proc now
that we've got name we want for the API we want

"ZAP!" deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.67 26-Apr-2010 deraadt

more portable code


# 1.66 26-Mar-2010 lum

Make the source of process TIME for displaying and sorting the same.
top(1) and ps(1) now display the same info.
ok tedu@ millert@ otto@ and tested on sparc64 by landry@


Revision tags: OPENBSD_4_7_BASE
# 1.65 29-Jan-2010 tedu

Allow sorting by command and pid. Also, allow partial matches with strncmp.
ok jmc otto


Revision tags: OPENBSD_4_6_BASE
# 1.64 28-Apr-2009 deraadt

extend STATE column to fit > 9 cpus; Aivar Jaakson; PR6131


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.63 01-Nov-2007 otto

bump arbitrary limit on arg length shown by -C; requested by and ok deraadt@


Revision tags: OPENBSD_4_2_BASE
# 1.62 27-Jul-2007 deraadt

replace a few more malloc(n*m) idioms with calloc(n,m) for safety;
inspired by zinovik@cs.karelia.ru


# 1.61 29-May-2007 otto

Instead of using hand-crafted redraws minimizing screen updates, use curses.
Enables nice things like process highlighting without hurting the brain.
ok deraadt@


# 1.60 16-Apr-2007 otto

don't truncate wchan, ok miod@


# 1.59 13-Apr-2007 otto

include cleanup and fix in comment; from Mark Lumsden


# 1.58 04-Apr-2007 otto

missing prototypes; from Mark Lumsden with a twist by me.


Revision tags: OPENBSD_4_1_BASE
# 1.57 04-Feb-2007 otto

long and long long is not the same on 32 bits archs


# 1.56 03-Jan-2007 otto

Add a 'g' command to only show processes having a string in their
command name. ok deraadt@


# 1.55 02-Jan-2007 otto

top(1) doesn't use any math.h function, so do not include math.h and
do not link with libm. ok ray@


# 1.54 29-Nov-2006 miod

Do not test for processes being swapped out since this can't happen anymore.


# 1.53 20-Sep-2006 ray

Recommit memory leak fix.


Revision tags: OPENBSD_4_0_BASE
# 1.52 29-Apr-2006 otto

use calloc(3) instead of malloc(3) to allocate cpu state structs; it's
better style here; plus it prevents bogus data to show up on first display.
report + ok pedro@


Revision tags: OPENBSD_3_9_BASE
# 1.51 21-Dec-2005 millert

Userland programs should include <errno.h> not <sys/errno.h>
OK deraadt@


# 1.50 04-Dec-2005 tedu

support for toggling thread viewing.
ok brad, hints from jmc


Revision tags: OPENBSD_3_8_BASE
# 1.49 17-Jun-2005 markus

add a 'C' command ) that toggles the display of the full cmdline;
with Jared Yanovich; ok deraadt


# 1.48 08-Jun-2005 millert

Multi-cpu support for top. Each cpu now gets its own stats line.
Based on work by todd@ at the hackathon.


Revision tags: OPENBSD_3_7_BASE
# 1.47 06-Dec-2004 markus

missing space; bluhm at genua.de


# 1.46 22-Nov-2004 pat

simplify loop iteration

ok otto millert


# 1.45 17-Nov-2004 markus

off-by-one in process_states[], fixes display of memory; ok deraadt, aaron


# 1.44 07-Oct-2004 otto

introduce -p option and p command to only show a single process.
From Patrick Latifi. ok deraadt@ millert@


Revision tags: OPENBSD_3_6_BASE
# 1.43 13-Jun-2004 otto

Typos in comments and a redundant prototype. From Brian Poole via jmc@
ok jmc@


# 1.42 11-Jun-2004 deraadt

handle the KI_NOCPU case, pointed out by markus


# 1.41 11-Jun-2004 deraadt

only print /# if > 1 cpu on a machine


# 1.40 11-Jun-2004 deraadt

export cpuid via kproc2, and make ps & top aware... from niklas


# 1.39 11-Jun-2004 deraadt

hide top breakage, until the next commit which will do it right


# 1.38 09-May-2004 deraadt

spaces


Revision tags: OPENBSD_3_5_BASE
# 1.37 08-Jan-2004 millert

Convert to kinfo_proc2; ok deraadt@


Revision tags: OPENBSD_3_4_BASE
# 1.36 07-Jul-2003 deraadt

protos


# 1.35 18-Jun-2003 deraadt

remove unused variables


# 1.34 17-Jun-2003 jfb

properly typecast to uid_t the return value of proc_owner() and report
ESRCH instead of EACCES if it returns -1

ok millert@


# 1.33 15-Jun-2003 millert

use uid_t and fix some sign compare warnings; OK krw@ and deraadt@


# 1.32 14-Jun-2003 avsm

use correct length instead of sizeof(char *) for some strl* bound values
millert@ ok


# 1.31 13-Jun-2003 deraadt

readable code


# 1.30 12-Jun-2003 deraadt

knf


# 1.29 12-Jun-2003 pvalchev

cleanup; ok deraadt


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.28 02-Jul-2002 tholo

Add missing copyright


Revision tags: OPENBSD_3_1_BASE
# 1.27 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.26 05-Dec-2001 art

Get maxslp with sysctl. Not the constant.


Revision tags: OPENBSD_3_0_BASE
# 1.25 12-Jul-2001 deraadt

first pass at a -Wall cleanup


# 1.24 03-Jun-2001 angelos

Use KERN_NPROCS to get the number of processes on the system, to
determine the amount of space we'll need to store the
information. The alternative, calling sysctl() with a NULL argument
for data, meant the kernel had to go through the process table.


Revision tags: OPENBSD_2_9_BASE
# 1.23 22-Feb-2001 deraadt

plug mem leak i introduced; spotted by millert


# 1.22 17-Feb-2001 deraadt

bye bye setgid kvm


# 1.21 17-Feb-2001 deraadt

do not use nlist anymore, kvm gets the bullet next


# 1.20 17-Feb-2001 deraadt

partial KNF, because i cannot read the diffs i am trying to debug


Revision tags: OPENBSD_2_8_BASE
# 1.19 18-Jun-2000 niklas

Mark kernel threads as system processes. Make ps hide them by default,
show them with -k. Do not try to show RSS based values for them as they
mess up column alignment. vmstat -f now shows kernel threads separately
from rforks too.


Revision tags: OPENBSD_2_7_BASE
# 1.18 14-Nov-1999 deraadt

stathz instead of hz, plus improvements by me; camield@inet.unisource.nl


# 1.17 29-Oct-1999 todd

writen


Revision tags: OPENBSD_2_6_BASE
# 1.16 21-Jun-1999 art

stop casting the third argument to kvm_read to char *. it is a void *.


# 1.15 22-May-1999 weingart

Update for new vmswap code.


Revision tags: OPENBSD_2_5_BASE
# 1.14 28-Nov-1998 kstailey

do not count swap spaces that were not swapon(2)ed


Revision tags: OPENBSD_2_4_BASE
# 1.13 20-Sep-1998 niklas

cp_time is long, deal with it.


# 1.12 20-Sep-1998 niklas

widen unsigned entities to signed ones before subtracting, fixes sorting on alpha


# 1.11 21-Aug-1998 kstailey

support "-o field" and the interactive "o" command


# 1.10 08-Jul-1998 deraadt

whack kmem gid after kvm_openfiles()


# 1.9 25-Jun-1998 deraadt

handle missing swap


Revision tags: OPENBSD_2_3_BASE
# 1.8 08-Nov-1997 millert

Sort idle processes by total CPU usage, not cpticks since cpticks
get zeroed every second; dan@dan.emsphone.com


Revision tags: OPENBSD_2_2_BASE
# 1.7 09-Sep-1997 millert

"last pid" stuff is useless with random pids.


# 1.6 09-Sep-1997 millert

Use kvm_openfiles() not the sunos compat kvm_open().
Use warn/warnx where it makes sense and check some more ret vals.


# 1.5 24-Aug-1997 millert

Fix compiler warnings.


# 1.4 22-Aug-1997 downsj

Two more alpha warnings.


# 1.3 22-Aug-1997 downsj

First sweep. Prototype, type fixes, long fixes, mostly compiles with
-Wall.


# 1.2 17-Aug-1997 kstailey

print "idle" for idle processes


# 1.1 14-Aug-1997 downsj

top 3.4, with a few changes. Still needs more work.


# 1.112 10-Sep-2022 cheloha

top(1): remove last vestiges of "last pid" support

millert@ removed most of the "last pid" support from top(1) in 1997.
See, e.g. top/machine.c,v1.7:

http://cvsweb.openbsd.org/src/usr.bin/top/machine.c?rev=1.7&content-type=text/x-cvsweb-markup

Let's remove the rest of it:

- Eliminate system_info.last_pid.

- Remove mpid parameter and "last pid" printing code from i_loadave().

Link: https://marc.info/?l=openbsd-tech&m=166277253606823&w=2

ok millert@


Revision tags: OPENBSD_7_1_BASE
# 1.111 22-Feb-2022 deraadt

MAXCOMLEN is no longer needed in these programs, so remove the annotation
from sys/param.h include lines, or remove the include lines entirely if
it this was the least requirement.
ok millert


Revision tags: OPENBSD_6_8_BASE OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.110 26-Aug-2020 kn

Add "t" to toggle the display of routing tables

Swap the WAIT column with RTABLE (and vice versa); WAIT is wide enough to
fit RTABLE, somewhat adds additional value to STATE and seems therefore most
appropiate to hide in favour of RTABLE.

Filtering rtables with "T" does not toggle the column, just like filtering
users with "u" does not toggle between user and thread id.

Feedback jmc
OK remi


# 1.109 25-Aug-2020 kn

Rename rtable filter variables and simplify buffer handling

No functional change, the rename is for consistency and the buffer
handling reduces churn in an upcoming diff.


# 1.108 23-Aug-2020 kn

Filter by routing table

"-T-0" for processes outside the default routing table, "-T3" for those in
a specific one; same semantics as with other filters.

Manual wording and command line flag taken from pgrep(1) being the only way
to identify processes by routing table; After netstat(1)'s recent addition
of "-R", filtering in top makes for handy tooling around rtable(4).

"looks good to me" millert
OK remi


# 1.107 06-Jul-2020 kn

Use mnemonic KERN_PROC_ALL not literal zero

No object change.


# 1.106 26-Jun-2020 kn

Remove duplicate initialisation

Those are global variables are (zero) initialised as such already and
machine_init() is called only once upon startup.

OK mvs


# 1.105 25-Jun-2020 kn

Remove unused "remaining" member in struct handle

Only ever set or decremented since import with machine.c r1.1 (1997);

While here, simplify the skip semantics behind the scroll functionality.

OK millert


# 1.104 24-Jun-2020 kn

Remove obsolete comment about "extra nulls"

Those were removed in r1.39 in 2004.


# 1.103 23-Jun-2020 kn

Remove redundant NULL check

For the sake of simplicity and to reflect that the process list is always
sorted (default is "cpu"), even if not explicitly requested; this makes it
easier to argue about the code around sort order and its selection.

OK millert


Revision tags: OPENBSD_6_7_BASE
# 1.102 06-Jan-2020 zhuk

Implement scrolling in top(1) using 9 and 0 keys.

Planned improvements: scroll position displaying, using arrow/pgup/pgdown keys.

okay tedu@


# 1.101 16-Dec-2019 guenther

Delete tests for P_THREAD that predate the existence of
KERN_PROC_SHOW_THREADS and have been rendered superfluous by it.
Similarly, some P_SYSTEM tests can be deleted or pushed to the
kernel by using KERN_PROC_ALL instead of KERN_PROC_KTHREAD.

ok visa@ mpi@


Revision tags: OPENBSD_6_6_BASE
# 1.100 08-Oct-2019 kn

Simplify threads flag handling

Instead of always passing the threads flag which decides whether other
function arguments are used or not, make callers pass those arguments or
NULL depending on the threads flag.

This reflects better how thread IDs are shown in place of user names/IDs.

OK millert


# 1.99 06-Oct-2019 kn

Avoid gasting around get_process_info()

get_process_info() returns a pointer to the global handle later only be
used in format_next_process(); treat this struct handle as such without
casting the pointer to caddr_t and back again.

No object change.
OK millert deraadt


# 1.98 06-Oct-2019 kn

Zap intermediate structs in compare_*()

SETORDER() can assign from function arguments directly without additional
identical structs in each function.

No object change.
OK millert


# 1.97 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


Revision tags: OPENBSD_6_5_BASE
# 1.96 28-Nov-2018 kn

Allow reverse sort order

Prefixing the field with a dash will sort processes in reverse order.
`o -pid' will therefore list PID 1 on top.

"looks good" tedu, improvements and OK cheloha


# 1.95 17-Nov-2018 cheloha

Add new KERN_CPUSTATS sysctl(2) so we can identify offline CPUs.

Because of hw.smt we need a way to determine whether a given CPU is "online"
or "offline" from userspace. KERN_CPTIME2 is an array, and so cannot be
cleanly extended for this purpose, so add a new sysctl(2) KERN_CPUSTATS
with an extensible struct. At the moment it's just KERN_CPTIME2 with a
flags member, but it can grow as needed.

KERN_CPUSTATS appears to have been defined by BSDi long ago, but there are
few (if any) packages in the wild still using the symbol so breakage in ports
should be near zero. No other system inherited the symbol from BSDi, either.

Then, use the new sysctl(2) in systat(1) and top(1):

- systat(1) draws placeholder marks ('-') instead of percentages for
offline CPUs in the cpu view.

- systat(1) omits offline CPU ticks when drawing the "big bar" in
the vmstat view. The upshot is that the bar isn't half idle when
half your logical CPUs are disabled.

- top(1) does not draw lines for offline CPUs; if CPUs toggle on or
offline in interactive mode we redraw the display to expand/reduce
space for the new/missing CPUs. This is consistent with what some
top(1) implementations do on Linux.

- top(1) omits offline CPUs from the totals when CPU totals are
combined into a single line (the '-1' flag).

Originally prompted by deraadt@. Discussed endlessly with deraadt@,
ketennis@, and sthen@. Tested by jmc@ and jca@. Earlier versions also
discussed with jca@. Earlier versions tested by jmc@, tb@, and many
others.

docs ok jmc@, kernel bits ok ketennis@, everything ok sthen@,
"Is your stuff in yet?" deraadt@


Revision tags: OPENBSD_6_4_BASE
# 1.94 05-Oct-2018 cheloha

Revert KERN_CPTIME2 ENODEV changes in kernel and userspace.

ok kettenis deraadt


# 1.93 26-Sep-2018 cheloha

KERN_CPTIME2: set ENODEV if the CPU is offline.

This lets userspace distinguish between idle CPUs and those that are
not schedulable because hw.smt=0.

A subsequent commit probably needs to add documentation for this
to sysctl.2 (and perhaps elsewhere) after the dust settles.

Also included here are changes to systat(1) and top(1) that account
for the ENODEV case and adjust behavior accordingly:

- systat(1)'s cpu view prints placeholder marks ('-') instead of
percentages for each state if the given CPU is offline.

- systat(1)'s vmstat view checks for offline CPUs when computing the
machine state total and excludes them, so the CPU usage graph
only represents the states for online CPUs.

- top(1) does not draw CPU rows for offline CPUs when the view is
redrawn. If CPUs "go offline", percentages for each state are
replaced by placeholder marks ('-'); the view will need to be
redrawn to remove these rows. If CPUs "go online" the view will
need to be redrawn to show these new CPUs. In "combined CPU" mode,
the count and the state totals only represent online CPUs.

Ports using KERN_CPTIME2 will need to be updated. The changes
described above to make systat(1) and top(1) aware of the ENODEV
case *and* gracefully handle a changing HW_NCPUONLINE while the
application is running are not necessarily appropriate for each
and every port.

The changes described above are so extensive in part to demonstrate
one way a program *might* be made robust to changing CPU availability.
In particular, changing hw.smt after boot is an extremely rare event,
and this needs to be weighed when updating ports.

The logic needed to account for the KERN_CPTIME2 ENODEV case is
very roughly:

if (sysctl(...) == -1) {
if (errno != ENODEV) {
/* Actual error occurred. */
} else {
/* CPU is offline. */
}
} else {
/* CPU is online and CPU states were set by sysctl(2). */
}

Prompted by deraadt@. Basic idea for ENODEV from kettenis@. Discussed at
length with kettenis@. Additional testing by tb@.

No complaints from hackers@ after a week.

ok kettenis@, "I think you should commit [now]" deraadt@


# 1.92 22-Sep-2018 millert

Use user_from_uid() and uid_from_user() directly. The wrappers
in username.c are now so simple there is no longer a good reason
to use them. OK deraadt@


# 1.91 13-Sep-2018 millert

Fix warnings caused by user_from_uid() and group_from_gid() now
returning const char *.


# 1.90 14-May-2018 mpi

Stopping counting and reporting CPU time spent spinning on a lock as
system time.

Introduce a new CP_SPIN "scheduler state" and modify userland tools
to display the % of timer a CPU spents spinning.

Based on a diff from jmatthew@, ok pirofti@, bluhm@, visa@, deraadt@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.89 30-May-2017 tedu

avoid some shadow warnings. from Brian Callahan


Revision tags: OPENBSD_6_1_BASE
# 1.88 15-Mar-2017 deraadt

annoying whitespace die die die


# 1.87 28-Jul-2016 tedu

rework realloc loop. there's no need to shrink the allocation between
calls. if we need a big space once, we'll likely need a big space again.


Revision tags: OPENBSD_6_0_BASE
# 1.86 11-May-2016 edd

Allow top(1) to filter process args if they are being displayed.

OK tedu@, deraadt@, and with input from Michal Mazurek.

Thanks


Revision tags: OPENBSD_5_9_BASE
# 1.85 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_8_BASE
# 1.84 06-May-2015 mpi

Display thread IDs instead of the name of the process's owner when "-H"
is used.

The rationnal is that when you're looking at threads you're generally
already filtereing by PID and this allow you to see which thread is a
pig.

Written some time ago with mikeb@

ok sthen@, krw@, guenther@


Revision tags: OPENBSD_5_7_BASE
# 1.83 19-Jan-2015 millert

Kill loadavg.h and just use the fscale value directly.
There's no need for crazy casts of p_pctcpu in ORDERKEY_PCTCPU
since kinfo_proc exports it as uint32_t. OK deraadt@


# 1.82 19-Jan-2015 deraadt

like in ps(1), fetch the FSCALE value using sysctl rather than using
the header version
ok guenther


# 1.81 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.80 17-Sep-2014 dlg

if there are more than 8 cpus, default to combined cpu stats (like
you passed -1 on the command line).

ok kettenis@ tedu@


# 1.79 15-Sep-2014 miod

Remove non-standard <sys/dkstat.h> header. It has not contained anything
related to disk stastics for almost 17 years, and the remaining
userland-visible defines duplicate those found in <sys/sched.h>.

Move the remaining _KERNEL defines to <sys/tty.h> where they belong, and
update all users to cope with this.

ok kettenis@


Revision tags: OPENBSD_5_6_BASE
# 1.78 04-Jul-2014 guenther

Track whether a process is a zombie or not yet fully built via flags
PS_{ZOMBIE,EMBRYO} on the process instead of peeking into the process's
thread data. This eliminates the need for the thread-level SDEAD state.

Change kvm_getprocs() (both the sysctl() and kvm backends) to report the
"most active" scheduler state for the process's threads.

tweaks kettenis@
feedback and ok matthew@


# 1.77 08-Apr-2014 mpi

Use VM_UVMEXP instead of VM_METER for memory usages and directly
include <sys/vmmeter.h> where it is needed instead of relying on
it being included by <uvm/uvm_extern.h>.

miod@ likes it, ok guenther@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.76 23-Mar-2013 tedu

wchan is no longer exposed, just check if wmesg is not empty.
(wmesg hasn't itself been a pointer since conversion to kinfo_proc.)
noticed by sthen.
ok deraadt jsing millert sthen


Revision tags: OPENBSD_5_3_BASE
# 1.75 18-Dec-2012 millert

We no longer use struct eproc for kinfo_proc in sysctl.h so there
is no direct need for sys/proc.h or sys/resource.h. Some consumers
of kinfo_proc need these for the proc flags and rlimit defines like
RLIM_INF so add the appropriate includes to them.
OK deraadt@ sthen@


Revision tags: OPENBSD_5_2_BASE
# 1.74 09-Jul-2012 deraadt

According to miod, KERNBASE used as a userland symbol should die
in a fire. Start by removing the definitions, before we clean the
headers later.


# 1.73 05-Jun-2012 brynet

Add support for hiding a user's processes in top.

feedback & ok lum@


# 1.72 21-Apr-2012 guenther

Remove a check made superfluous by the KERN_PROC_SHOW_THREADS flag.
This lets new binaries work (at least w/o the -H option) with old kernels.

ok pirofti@ deraadt@


# 1.71 17-Apr-2012 pirofti

Make it optional for kvm_getprocs() (and related sysctl) to return
thread information.

Add a KERN_PROC_SHOW_THREADS flag that has to be set in order to get the
thread info and make it off by default. This creates backwards compatibility
for applications that relied on a given size/number of items to be returned.

Modify ps(1) and top(1) accordingly.

Okay guenther@.


# 1.70 12-Apr-2012 pirofti

Add per thread accounting, mainly for usage & friends.

This expands the already bloated FILL_KPROC macro to take an extra
parameter that indicates if the callee is a thread or a process.

The userland bits are adjusted accordingly and ps(1) and top(1) now
display per thread usage times when -H is used.

Also pkill(1) had to be adjusted so that duplicates don't pop up.

libkvm does basically the same thing as the kernel bits.

Okay guenther@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.69 12-Jul-2011 tedu

print out the amount of memory used for cache. ok otto


# 1.68 10-Apr-2011 guenther

Switch back from KERN_PROC2/kinfo_proc2 to KERN_PROC/kinfo_proc now
that we've got name we want for the API we want

"ZAP!" deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.67 26-Apr-2010 deraadt

more portable code


# 1.66 26-Mar-2010 lum

Make the source of process TIME for displaying and sorting the same.
top(1) and ps(1) now display the same info.
ok tedu@ millert@ otto@ and tested on sparc64 by landry@


Revision tags: OPENBSD_4_7_BASE
# 1.65 29-Jan-2010 tedu

Allow sorting by command and pid. Also, allow partial matches with strncmp.
ok jmc otto


Revision tags: OPENBSD_4_6_BASE
# 1.64 28-Apr-2009 deraadt

extend STATE column to fit > 9 cpus; Aivar Jaakson; PR6131


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.63 01-Nov-2007 otto

bump arbitrary limit on arg length shown by -C; requested by and ok deraadt@


Revision tags: OPENBSD_4_2_BASE
# 1.62 27-Jul-2007 deraadt

replace a few more malloc(n*m) idioms with calloc(n,m) for safety;
inspired by zinovik@cs.karelia.ru


# 1.61 29-May-2007 otto

Instead of using hand-crafted redraws minimizing screen updates, use curses.
Enables nice things like process highlighting without hurting the brain.
ok deraadt@


# 1.60 16-Apr-2007 otto

don't truncate wchan, ok miod@


# 1.59 13-Apr-2007 otto

include cleanup and fix in comment; from Mark Lumsden


# 1.58 04-Apr-2007 otto

missing prototypes; from Mark Lumsden with a twist by me.


Revision tags: OPENBSD_4_1_BASE
# 1.57 04-Feb-2007 otto

long and long long is not the same on 32 bits archs


# 1.56 03-Jan-2007 otto

Add a 'g' command to only show processes having a string in their
command name. ok deraadt@


# 1.55 02-Jan-2007 otto

top(1) doesn't use any math.h function, so do not include math.h and
do not link with libm. ok ray@


# 1.54 29-Nov-2006 miod

Do not test for processes being swapped out since this can't happen anymore.


# 1.53 20-Sep-2006 ray

Recommit memory leak fix.


Revision tags: OPENBSD_4_0_BASE
# 1.52 29-Apr-2006 otto

use calloc(3) instead of malloc(3) to allocate cpu state structs; it's
better style here; plus it prevents bogus data to show up on first display.
report + ok pedro@


Revision tags: OPENBSD_3_9_BASE
# 1.51 21-Dec-2005 millert

Userland programs should include <errno.h> not <sys/errno.h>
OK deraadt@


# 1.50 04-Dec-2005 tedu

support for toggling thread viewing.
ok brad, hints from jmc


Revision tags: OPENBSD_3_8_BASE
# 1.49 17-Jun-2005 markus

add a 'C' command ) that toggles the display of the full cmdline;
with Jared Yanovich; ok deraadt


# 1.48 08-Jun-2005 millert

Multi-cpu support for top. Each cpu now gets its own stats line.
Based on work by todd@ at the hackathon.


Revision tags: OPENBSD_3_7_BASE
# 1.47 06-Dec-2004 markus

missing space; bluhm at genua.de


# 1.46 22-Nov-2004 pat

simplify loop iteration

ok otto millert


# 1.45 17-Nov-2004 markus

off-by-one in process_states[], fixes display of memory; ok deraadt, aaron


# 1.44 07-Oct-2004 otto

introduce -p option and p command to only show a single process.
From Patrick Latifi. ok deraadt@ millert@


Revision tags: OPENBSD_3_6_BASE
# 1.43 13-Jun-2004 otto

Typos in comments and a redundant prototype. From Brian Poole via jmc@
ok jmc@


# 1.42 11-Jun-2004 deraadt

handle the KI_NOCPU case, pointed out by markus


# 1.41 11-Jun-2004 deraadt

only print /# if > 1 cpu on a machine


# 1.40 11-Jun-2004 deraadt

export cpuid via kproc2, and make ps & top aware... from niklas


# 1.39 11-Jun-2004 deraadt

hide top breakage, until the next commit which will do it right


# 1.38 09-May-2004 deraadt

spaces


Revision tags: OPENBSD_3_5_BASE
# 1.37 08-Jan-2004 millert

Convert to kinfo_proc2; ok deraadt@


Revision tags: OPENBSD_3_4_BASE
# 1.36 07-Jul-2003 deraadt

protos


# 1.35 18-Jun-2003 deraadt

remove unused variables


# 1.34 17-Jun-2003 jfb

properly typecast to uid_t the return value of proc_owner() and report
ESRCH instead of EACCES if it returns -1

ok millert@


# 1.33 15-Jun-2003 millert

use uid_t and fix some sign compare warnings; OK krw@ and deraadt@


# 1.32 14-Jun-2003 avsm

use correct length instead of sizeof(char *) for some strl* bound values
millert@ ok


# 1.31 13-Jun-2003 deraadt

readable code


# 1.30 12-Jun-2003 deraadt

knf


# 1.29 12-Jun-2003 pvalchev

cleanup; ok deraadt


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.28 02-Jul-2002 tholo

Add missing copyright


Revision tags: OPENBSD_3_1_BASE
# 1.27 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.26 05-Dec-2001 art

Get maxslp with sysctl. Not the constant.


Revision tags: OPENBSD_3_0_BASE
# 1.25 12-Jul-2001 deraadt

first pass at a -Wall cleanup


# 1.24 03-Jun-2001 angelos

Use KERN_NPROCS to get the number of processes on the system, to
determine the amount of space we'll need to store the
information. The alternative, calling sysctl() with a NULL argument
for data, meant the kernel had to go through the process table.


Revision tags: OPENBSD_2_9_BASE
# 1.23 22-Feb-2001 deraadt

plug mem leak i introduced; spotted by millert


# 1.22 17-Feb-2001 deraadt

bye bye setgid kvm


# 1.21 17-Feb-2001 deraadt

do not use nlist anymore, kvm gets the bullet next


# 1.20 17-Feb-2001 deraadt

partial KNF, because i cannot read the diffs i am trying to debug


Revision tags: OPENBSD_2_8_BASE
# 1.19 18-Jun-2000 niklas

Mark kernel threads as system processes. Make ps hide them by default,
show them with -k. Do not try to show RSS based values for them as they
mess up column alignment. vmstat -f now shows kernel threads separately
from rforks too.


Revision tags: OPENBSD_2_7_BASE
# 1.18 14-Nov-1999 deraadt

stathz instead of hz, plus improvements by me; camield@inet.unisource.nl


# 1.17 29-Oct-1999 todd

writen


Revision tags: OPENBSD_2_6_BASE
# 1.16 21-Jun-1999 art

stop casting the third argument to kvm_read to char *. it is a void *.


# 1.15 22-May-1999 weingart

Update for new vmswap code.


Revision tags: OPENBSD_2_5_BASE
# 1.14 28-Nov-1998 kstailey

do not count swap spaces that were not swapon(2)ed


Revision tags: OPENBSD_2_4_BASE
# 1.13 20-Sep-1998 niklas

cp_time is long, deal with it.


# 1.12 20-Sep-1998 niklas

widen unsigned entities to signed ones before subtracting, fixes sorting on alpha


# 1.11 21-Aug-1998 kstailey

support "-o field" and the interactive "o" command


# 1.10 08-Jul-1998 deraadt

whack kmem gid after kvm_openfiles()


# 1.9 25-Jun-1998 deraadt

handle missing swap


Revision tags: OPENBSD_2_3_BASE
# 1.8 08-Nov-1997 millert

Sort idle processes by total CPU usage, not cpticks since cpticks
get zeroed every second; dan@dan.emsphone.com


Revision tags: OPENBSD_2_2_BASE
# 1.7 09-Sep-1997 millert

"last pid" stuff is useless with random pids.


# 1.6 09-Sep-1997 millert

Use kvm_openfiles() not the sunos compat kvm_open().
Use warn/warnx where it makes sense and check some more ret vals.


# 1.5 24-Aug-1997 millert

Fix compiler warnings.


# 1.4 22-Aug-1997 downsj

Two more alpha warnings.


# 1.3 22-Aug-1997 downsj

First sweep. Prototype, type fixes, long fixes, mostly compiles with
-Wall.


# 1.2 17-Aug-1997 kstailey

print "idle" for idle processes


# 1.1 14-Aug-1997 downsj

top 3.4, with a few changes. Still needs more work.


# 1.111 22-Feb-2022 deraadt

MAXCOMLEN is no longer needed in these programs, so remove the annotation
from sys/param.h include lines, or remove the include lines entirely if
it this was the least requirement.
ok millert


Revision tags: OPENBSD_6_8_BASE OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.110 26-Aug-2020 kn

Add "t" to toggle the display of routing tables

Swap the WAIT column with RTABLE (and vice versa); WAIT is wide enough to
fit RTABLE, somewhat adds additional value to STATE and seems therefore most
appropiate to hide in favour of RTABLE.

Filtering rtables with "T" does not toggle the column, just like filtering
users with "u" does not toggle between user and thread id.

Feedback jmc
OK remi


# 1.109 25-Aug-2020 kn

Rename rtable filter variables and simplify buffer handling

No functional change, the rename is for consistency and the buffer
handling reduces churn in an upcoming diff.


# 1.108 23-Aug-2020 kn

Filter by routing table

"-T-0" for processes outside the default routing table, "-T3" for those in
a specific one; same semantics as with other filters.

Manual wording and command line flag taken from pgrep(1) being the only way
to identify processes by routing table; After netstat(1)'s recent addition
of "-R", filtering in top makes for handy tooling around rtable(4).

"looks good to me" millert
OK remi


# 1.107 06-Jul-2020 kn

Use mnemonic KERN_PROC_ALL not literal zero

No object change.


# 1.106 26-Jun-2020 kn

Remove duplicate initialisation

Those are global variables are (zero) initialised as such already and
machine_init() is called only once upon startup.

OK mvs


# 1.105 25-Jun-2020 kn

Remove unused "remaining" member in struct handle

Only ever set or decremented since import with machine.c r1.1 (1997);

While here, simplify the skip semantics behind the scroll functionality.

OK millert


# 1.104 24-Jun-2020 kn

Remove obsolete comment about "extra nulls"

Those were removed in r1.39 in 2004.


# 1.103 23-Jun-2020 kn

Remove redundant NULL check

For the sake of simplicity and to reflect that the process list is always
sorted (default is "cpu"), even if not explicitly requested; this makes it
easier to argue about the code around sort order and its selection.

OK millert


Revision tags: OPENBSD_6_7_BASE
# 1.102 06-Jan-2020 zhuk

Implement scrolling in top(1) using 9 and 0 keys.

Planned improvements: scroll position displaying, using arrow/pgup/pgdown keys.

okay tedu@


# 1.101 16-Dec-2019 guenther

Delete tests for P_THREAD that predate the existence of
KERN_PROC_SHOW_THREADS and have been rendered superfluous by it.
Similarly, some P_SYSTEM tests can be deleted or pushed to the
kernel by using KERN_PROC_ALL instead of KERN_PROC_KTHREAD.

ok visa@ mpi@


Revision tags: OPENBSD_6_6_BASE
# 1.100 08-Oct-2019 kn

Simplify threads flag handling

Instead of always passing the threads flag which decides whether other
function arguments are used or not, make callers pass those arguments or
NULL depending on the threads flag.

This reflects better how thread IDs are shown in place of user names/IDs.

OK millert


# 1.99 06-Oct-2019 kn

Avoid gasting around get_process_info()

get_process_info() returns a pointer to the global handle later only be
used in format_next_process(); treat this struct handle as such without
casting the pointer to caddr_t and back again.

No object change.
OK millert deraadt


# 1.98 06-Oct-2019 kn

Zap intermediate structs in compare_*()

SETORDER() can assign from function arguments directly without additional
identical structs in each function.

No object change.
OK millert


# 1.97 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


Revision tags: OPENBSD_6_5_BASE
# 1.96 28-Nov-2018 kn

Allow reverse sort order

Prefixing the field with a dash will sort processes in reverse order.
`o -pid' will therefore list PID 1 on top.

"looks good" tedu, improvements and OK cheloha


# 1.95 17-Nov-2018 cheloha

Add new KERN_CPUSTATS sysctl(2) so we can identify offline CPUs.

Because of hw.smt we need a way to determine whether a given CPU is "online"
or "offline" from userspace. KERN_CPTIME2 is an array, and so cannot be
cleanly extended for this purpose, so add a new sysctl(2) KERN_CPUSTATS
with an extensible struct. At the moment it's just KERN_CPTIME2 with a
flags member, but it can grow as needed.

KERN_CPUSTATS appears to have been defined by BSDi long ago, but there are
few (if any) packages in the wild still using the symbol so breakage in ports
should be near zero. No other system inherited the symbol from BSDi, either.

Then, use the new sysctl(2) in systat(1) and top(1):

- systat(1) draws placeholder marks ('-') instead of percentages for
offline CPUs in the cpu view.

- systat(1) omits offline CPU ticks when drawing the "big bar" in
the vmstat view. The upshot is that the bar isn't half idle when
half your logical CPUs are disabled.

- top(1) does not draw lines for offline CPUs; if CPUs toggle on or
offline in interactive mode we redraw the display to expand/reduce
space for the new/missing CPUs. This is consistent with what some
top(1) implementations do on Linux.

- top(1) omits offline CPUs from the totals when CPU totals are
combined into a single line (the '-1' flag).

Originally prompted by deraadt@. Discussed endlessly with deraadt@,
ketennis@, and sthen@. Tested by jmc@ and jca@. Earlier versions also
discussed with jca@. Earlier versions tested by jmc@, tb@, and many
others.

docs ok jmc@, kernel bits ok ketennis@, everything ok sthen@,
"Is your stuff in yet?" deraadt@


Revision tags: OPENBSD_6_4_BASE
# 1.94 05-Oct-2018 cheloha

Revert KERN_CPTIME2 ENODEV changes in kernel and userspace.

ok kettenis deraadt


# 1.93 26-Sep-2018 cheloha

KERN_CPTIME2: set ENODEV if the CPU is offline.

This lets userspace distinguish between idle CPUs and those that are
not schedulable because hw.smt=0.

A subsequent commit probably needs to add documentation for this
to sysctl.2 (and perhaps elsewhere) after the dust settles.

Also included here are changes to systat(1) and top(1) that account
for the ENODEV case and adjust behavior accordingly:

- systat(1)'s cpu view prints placeholder marks ('-') instead of
percentages for each state if the given CPU is offline.

- systat(1)'s vmstat view checks for offline CPUs when computing the
machine state total and excludes them, so the CPU usage graph
only represents the states for online CPUs.

- top(1) does not draw CPU rows for offline CPUs when the view is
redrawn. If CPUs "go offline", percentages for each state are
replaced by placeholder marks ('-'); the view will need to be
redrawn to remove these rows. If CPUs "go online" the view will
need to be redrawn to show these new CPUs. In "combined CPU" mode,
the count and the state totals only represent online CPUs.

Ports using KERN_CPTIME2 will need to be updated. The changes
described above to make systat(1) and top(1) aware of the ENODEV
case *and* gracefully handle a changing HW_NCPUONLINE while the
application is running are not necessarily appropriate for each
and every port.

The changes described above are so extensive in part to demonstrate
one way a program *might* be made robust to changing CPU availability.
In particular, changing hw.smt after boot is an extremely rare event,
and this needs to be weighed when updating ports.

The logic needed to account for the KERN_CPTIME2 ENODEV case is
very roughly:

if (sysctl(...) == -1) {
if (errno != ENODEV) {
/* Actual error occurred. */
} else {
/* CPU is offline. */
}
} else {
/* CPU is online and CPU states were set by sysctl(2). */
}

Prompted by deraadt@. Basic idea for ENODEV from kettenis@. Discussed at
length with kettenis@. Additional testing by tb@.

No complaints from hackers@ after a week.

ok kettenis@, "I think you should commit [now]" deraadt@


# 1.92 22-Sep-2018 millert

Use user_from_uid() and uid_from_user() directly. The wrappers
in username.c are now so simple there is no longer a good reason
to use them. OK deraadt@


# 1.91 13-Sep-2018 millert

Fix warnings caused by user_from_uid() and group_from_gid() now
returning const char *.


# 1.90 14-May-2018 mpi

Stopping counting and reporting CPU time spent spinning on a lock as
system time.

Introduce a new CP_SPIN "scheduler state" and modify userland tools
to display the % of timer a CPU spents spinning.

Based on a diff from jmatthew@, ok pirofti@, bluhm@, visa@, deraadt@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.89 30-May-2017 tedu

avoid some shadow warnings. from Brian Callahan


Revision tags: OPENBSD_6_1_BASE
# 1.88 15-Mar-2017 deraadt

annoying whitespace die die die


# 1.87 28-Jul-2016 tedu

rework realloc loop. there's no need to shrink the allocation between
calls. if we need a big space once, we'll likely need a big space again.


Revision tags: OPENBSD_6_0_BASE
# 1.86 11-May-2016 edd

Allow top(1) to filter process args if they are being displayed.

OK tedu@, deraadt@, and with input from Michal Mazurek.

Thanks


Revision tags: OPENBSD_5_9_BASE
# 1.85 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_8_BASE
# 1.84 06-May-2015 mpi

Display thread IDs instead of the name of the process's owner when "-H"
is used.

The rationnal is that when you're looking at threads you're generally
already filtereing by PID and this allow you to see which thread is a
pig.

Written some time ago with mikeb@

ok sthen@, krw@, guenther@


Revision tags: OPENBSD_5_7_BASE
# 1.83 19-Jan-2015 millert

Kill loadavg.h and just use the fscale value directly.
There's no need for crazy casts of p_pctcpu in ORDERKEY_PCTCPU
since kinfo_proc exports it as uint32_t. OK deraadt@


# 1.82 19-Jan-2015 deraadt

like in ps(1), fetch the FSCALE value using sysctl rather than using
the header version
ok guenther


# 1.81 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.80 17-Sep-2014 dlg

if there are more than 8 cpus, default to combined cpu stats (like
you passed -1 on the command line).

ok kettenis@ tedu@


# 1.79 15-Sep-2014 miod

Remove non-standard <sys/dkstat.h> header. It has not contained anything
related to disk stastics for almost 17 years, and the remaining
userland-visible defines duplicate those found in <sys/sched.h>.

Move the remaining _KERNEL defines to <sys/tty.h> where they belong, and
update all users to cope with this.

ok kettenis@


Revision tags: OPENBSD_5_6_BASE
# 1.78 04-Jul-2014 guenther

Track whether a process is a zombie or not yet fully built via flags
PS_{ZOMBIE,EMBRYO} on the process instead of peeking into the process's
thread data. This eliminates the need for the thread-level SDEAD state.

Change kvm_getprocs() (both the sysctl() and kvm backends) to report the
"most active" scheduler state for the process's threads.

tweaks kettenis@
feedback and ok matthew@


# 1.77 08-Apr-2014 mpi

Use VM_UVMEXP instead of VM_METER for memory usages and directly
include <sys/vmmeter.h> where it is needed instead of relying on
it being included by <uvm/uvm_extern.h>.

miod@ likes it, ok guenther@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.76 23-Mar-2013 tedu

wchan is no longer exposed, just check if wmesg is not empty.
(wmesg hasn't itself been a pointer since conversion to kinfo_proc.)
noticed by sthen.
ok deraadt jsing millert sthen


Revision tags: OPENBSD_5_3_BASE
# 1.75 18-Dec-2012 millert

We no longer use struct eproc for kinfo_proc in sysctl.h so there
is no direct need for sys/proc.h or sys/resource.h. Some consumers
of kinfo_proc need these for the proc flags and rlimit defines like
RLIM_INF so add the appropriate includes to them.
OK deraadt@ sthen@


Revision tags: OPENBSD_5_2_BASE
# 1.74 09-Jul-2012 deraadt

According to miod, KERNBASE used as a userland symbol should die
in a fire. Start by removing the definitions, before we clean the
headers later.


# 1.73 05-Jun-2012 brynet

Add support for hiding a user's processes in top.

feedback & ok lum@


# 1.72 21-Apr-2012 guenther

Remove a check made superfluous by the KERN_PROC_SHOW_THREADS flag.
This lets new binaries work (at least w/o the -H option) with old kernels.

ok pirofti@ deraadt@


# 1.71 17-Apr-2012 pirofti

Make it optional for kvm_getprocs() (and related sysctl) to return
thread information.

Add a KERN_PROC_SHOW_THREADS flag that has to be set in order to get the
thread info and make it off by default. This creates backwards compatibility
for applications that relied on a given size/number of items to be returned.

Modify ps(1) and top(1) accordingly.

Okay guenther@.


# 1.70 12-Apr-2012 pirofti

Add per thread accounting, mainly for usage & friends.

This expands the already bloated FILL_KPROC macro to take an extra
parameter that indicates if the callee is a thread or a process.

The userland bits are adjusted accordingly and ps(1) and top(1) now
display per thread usage times when -H is used.

Also pkill(1) had to be adjusted so that duplicates don't pop up.

libkvm does basically the same thing as the kernel bits.

Okay guenther@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.69 12-Jul-2011 tedu

print out the amount of memory used for cache. ok otto


# 1.68 10-Apr-2011 guenther

Switch back from KERN_PROC2/kinfo_proc2 to KERN_PROC/kinfo_proc now
that we've got name we want for the API we want

"ZAP!" deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.67 26-Apr-2010 deraadt

more portable code


# 1.66 26-Mar-2010 lum

Make the source of process TIME for displaying and sorting the same.
top(1) and ps(1) now display the same info.
ok tedu@ millert@ otto@ and tested on sparc64 by landry@


Revision tags: OPENBSD_4_7_BASE
# 1.65 29-Jan-2010 tedu

Allow sorting by command and pid. Also, allow partial matches with strncmp.
ok jmc otto


Revision tags: OPENBSD_4_6_BASE
# 1.64 28-Apr-2009 deraadt

extend STATE column to fit > 9 cpus; Aivar Jaakson; PR6131


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.63 01-Nov-2007 otto

bump arbitrary limit on arg length shown by -C; requested by and ok deraadt@


Revision tags: OPENBSD_4_2_BASE
# 1.62 27-Jul-2007 deraadt

replace a few more malloc(n*m) idioms with calloc(n,m) for safety;
inspired by zinovik@cs.karelia.ru


# 1.61 29-May-2007 otto

Instead of using hand-crafted redraws minimizing screen updates, use curses.
Enables nice things like process highlighting without hurting the brain.
ok deraadt@


# 1.60 16-Apr-2007 otto

don't truncate wchan, ok miod@


# 1.59 13-Apr-2007 otto

include cleanup and fix in comment; from Mark Lumsden


# 1.58 04-Apr-2007 otto

missing prototypes; from Mark Lumsden with a twist by me.


Revision tags: OPENBSD_4_1_BASE
# 1.57 04-Feb-2007 otto

long and long long is not the same on 32 bits archs


# 1.56 03-Jan-2007 otto

Add a 'g' command to only show processes having a string in their
command name. ok deraadt@


# 1.55 02-Jan-2007 otto

top(1) doesn't use any math.h function, so do not include math.h and
do not link with libm. ok ray@


# 1.54 29-Nov-2006 miod

Do not test for processes being swapped out since this can't happen anymore.


# 1.53 20-Sep-2006 ray

Recommit memory leak fix.


Revision tags: OPENBSD_4_0_BASE
# 1.52 29-Apr-2006 otto

use calloc(3) instead of malloc(3) to allocate cpu state structs; it's
better style here; plus it prevents bogus data to show up on first display.
report + ok pedro@


Revision tags: OPENBSD_3_9_BASE
# 1.51 21-Dec-2005 millert

Userland programs should include <errno.h> not <sys/errno.h>
OK deraadt@


# 1.50 04-Dec-2005 tedu

support for toggling thread viewing.
ok brad, hints from jmc


Revision tags: OPENBSD_3_8_BASE
# 1.49 17-Jun-2005 markus

add a 'C' command ) that toggles the display of the full cmdline;
with Jared Yanovich; ok deraadt


# 1.48 08-Jun-2005 millert

Multi-cpu support for top. Each cpu now gets its own stats line.
Based on work by todd@ at the hackathon.


Revision tags: OPENBSD_3_7_BASE
# 1.47 06-Dec-2004 markus

missing space; bluhm at genua.de


# 1.46 22-Nov-2004 pat

simplify loop iteration

ok otto millert


# 1.45 17-Nov-2004 markus

off-by-one in process_states[], fixes display of memory; ok deraadt, aaron


# 1.44 07-Oct-2004 otto

introduce -p option and p command to only show a single process.
From Patrick Latifi. ok deraadt@ millert@


Revision tags: OPENBSD_3_6_BASE
# 1.43 13-Jun-2004 otto

Typos in comments and a redundant prototype. From Brian Poole via jmc@
ok jmc@


# 1.42 11-Jun-2004 deraadt

handle the KI_NOCPU case, pointed out by markus


# 1.41 11-Jun-2004 deraadt

only print /# if > 1 cpu on a machine


# 1.40 11-Jun-2004 deraadt

export cpuid via kproc2, and make ps & top aware... from niklas


# 1.39 11-Jun-2004 deraadt

hide top breakage, until the next commit which will do it right


# 1.38 09-May-2004 deraadt

spaces


Revision tags: OPENBSD_3_5_BASE
# 1.37 08-Jan-2004 millert

Convert to kinfo_proc2; ok deraadt@


Revision tags: OPENBSD_3_4_BASE
# 1.36 07-Jul-2003 deraadt

protos


# 1.35 18-Jun-2003 deraadt

remove unused variables


# 1.34 17-Jun-2003 jfb

properly typecast to uid_t the return value of proc_owner() and report
ESRCH instead of EACCES if it returns -1

ok millert@


# 1.33 15-Jun-2003 millert

use uid_t and fix some sign compare warnings; OK krw@ and deraadt@


# 1.32 14-Jun-2003 avsm

use correct length instead of sizeof(char *) for some strl* bound values
millert@ ok


# 1.31 13-Jun-2003 deraadt

readable code


# 1.30 12-Jun-2003 deraadt

knf


# 1.29 12-Jun-2003 pvalchev

cleanup; ok deraadt


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.28 02-Jul-2002 tholo

Add missing copyright


Revision tags: OPENBSD_3_1_BASE
# 1.27 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.26 05-Dec-2001 art

Get maxslp with sysctl. Not the constant.


Revision tags: OPENBSD_3_0_BASE
# 1.25 12-Jul-2001 deraadt

first pass at a -Wall cleanup


# 1.24 03-Jun-2001 angelos

Use KERN_NPROCS to get the number of processes on the system, to
determine the amount of space we'll need to store the
information. The alternative, calling sysctl() with a NULL argument
for data, meant the kernel had to go through the process table.


Revision tags: OPENBSD_2_9_BASE
# 1.23 22-Feb-2001 deraadt

plug mem leak i introduced; spotted by millert


# 1.22 17-Feb-2001 deraadt

bye bye setgid kvm


# 1.21 17-Feb-2001 deraadt

do not use nlist anymore, kvm gets the bullet next


# 1.20 17-Feb-2001 deraadt

partial KNF, because i cannot read the diffs i am trying to debug


Revision tags: OPENBSD_2_8_BASE
# 1.19 18-Jun-2000 niklas

Mark kernel threads as system processes. Make ps hide them by default,
show them with -k. Do not try to show RSS based values for them as they
mess up column alignment. vmstat -f now shows kernel threads separately
from rforks too.


Revision tags: OPENBSD_2_7_BASE
# 1.18 14-Nov-1999 deraadt

stathz instead of hz, plus improvements by me; camield@inet.unisource.nl


# 1.17 29-Oct-1999 todd

writen


Revision tags: OPENBSD_2_6_BASE
# 1.16 21-Jun-1999 art

stop casting the third argument to kvm_read to char *. it is a void *.


# 1.15 22-May-1999 weingart

Update for new vmswap code.


Revision tags: OPENBSD_2_5_BASE
# 1.14 28-Nov-1998 kstailey

do not count swap spaces that were not swapon(2)ed


Revision tags: OPENBSD_2_4_BASE
# 1.13 20-Sep-1998 niklas

cp_time is long, deal with it.


# 1.12 20-Sep-1998 niklas

widen unsigned entities to signed ones before subtracting, fixes sorting on alpha


# 1.11 21-Aug-1998 kstailey

support "-o field" and the interactive "o" command


# 1.10 08-Jul-1998 deraadt

whack kmem gid after kvm_openfiles()


# 1.9 25-Jun-1998 deraadt

handle missing swap


Revision tags: OPENBSD_2_3_BASE
# 1.8 08-Nov-1997 millert

Sort idle processes by total CPU usage, not cpticks since cpticks
get zeroed every second; dan@dan.emsphone.com


Revision tags: OPENBSD_2_2_BASE
# 1.7 09-Sep-1997 millert

"last pid" stuff is useless with random pids.


# 1.6 09-Sep-1997 millert

Use kvm_openfiles() not the sunos compat kvm_open().
Use warn/warnx where it makes sense and check some more ret vals.


# 1.5 24-Aug-1997 millert

Fix compiler warnings.


# 1.4 22-Aug-1997 downsj

Two more alpha warnings.


# 1.3 22-Aug-1997 downsj

First sweep. Prototype, type fixes, long fixes, mostly compiles with
-Wall.


# 1.2 17-Aug-1997 kstailey

print "idle" for idle processes


# 1.1 14-Aug-1997 downsj

top 3.4, with a few changes. Still needs more work.


# 1.110 26-Aug-2020 kn

Add "t" to toggle the display of routing tables

Swap the WAIT column with RTABLE (and vice versa); WAIT is wide enough to
fit RTABLE, somewhat adds additional value to STATE and seems therefore most
appropiate to hide in favour of RTABLE.

Filtering rtables with "T" does not toggle the column, just like filtering
users with "u" does not toggle between user and thread id.

Feedback jmc
OK remi


# 1.109 25-Aug-2020 kn

Rename rtable filter variables and simplify buffer handling

No functional change, the rename is for consistency and the buffer
handling reduces churn in an upcoming diff.


# 1.108 23-Aug-2020 kn

Filter by routing table

"-T-0" for processes outside the default routing table, "-T3" for those in
a specific one; same semantics as with other filters.

Manual wording and command line flag taken from pgrep(1) being the only way
to identify processes by routing table; After netstat(1)'s recent addition
of "-R", filtering in top makes for handy tooling around rtable(4).

"looks good to me" millert
OK remi


# 1.107 06-Jul-2020 kn

Use mnemonic KERN_PROC_ALL not literal zero

No object change.


# 1.106 26-Jun-2020 kn

Remove duplicate initialisation

Those are global variables are (zero) initialised as such already and
machine_init() is called only once upon startup.

OK mvs


# 1.105 25-Jun-2020 kn

Remove unused "remaining" member in struct handle

Only ever set or decremented since import with machine.c r1.1 (1997);

While here, simplify the skip semantics behind the scroll functionality.

OK millert


# 1.104 24-Jun-2020 kn

Remove obsolete comment about "extra nulls"

Those were removed in r1.39 in 2004.


# 1.103 23-Jun-2020 kn

Remove redundant NULL check

For the sake of simplicity and to reflect that the process list is always
sorted (default is "cpu"), even if not explicitly requested; this makes it
easier to argue about the code around sort order and its selection.

OK millert


Revision tags: OPENBSD_6_7_BASE
# 1.102 06-Jan-2020 zhuk

Implement scrolling in top(1) using 9 and 0 keys.

Planned improvements: scroll position displaying, using arrow/pgup/pgdown keys.

okay tedu@


# 1.101 16-Dec-2019 guenther

Delete tests for P_THREAD that predate the existence of
KERN_PROC_SHOW_THREADS and have been rendered superfluous by it.
Similarly, some P_SYSTEM tests can be deleted or pushed to the
kernel by using KERN_PROC_ALL instead of KERN_PROC_KTHREAD.

ok visa@ mpi@


Revision tags: OPENBSD_6_6_BASE
# 1.100 08-Oct-2019 kn

Simplify threads flag handling

Instead of always passing the threads flag which decides whether other
function arguments are used or not, make callers pass those arguments or
NULL depending on the threads flag.

This reflects better how thread IDs are shown in place of user names/IDs.

OK millert


# 1.99 06-Oct-2019 kn

Avoid gasting around get_process_info()

get_process_info() returns a pointer to the global handle later only be
used in format_next_process(); treat this struct handle as such without
casting the pointer to caddr_t and back again.

No object change.
OK millert deraadt


# 1.98 06-Oct-2019 kn

Zap intermediate structs in compare_*()

SETORDER() can assign from function arguments directly without additional
identical structs in each function.

No object change.
OK millert


# 1.97 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


Revision tags: OPENBSD_6_5_BASE
# 1.96 28-Nov-2018 kn

Allow reverse sort order

Prefixing the field with a dash will sort processes in reverse order.
`o -pid' will therefore list PID 1 on top.

"looks good" tedu, improvements and OK cheloha


# 1.95 17-Nov-2018 cheloha

Add new KERN_CPUSTATS sysctl(2) so we can identify offline CPUs.

Because of hw.smt we need a way to determine whether a given CPU is "online"
or "offline" from userspace. KERN_CPTIME2 is an array, and so cannot be
cleanly extended for this purpose, so add a new sysctl(2) KERN_CPUSTATS
with an extensible struct. At the moment it's just KERN_CPTIME2 with a
flags member, but it can grow as needed.

KERN_CPUSTATS appears to have been defined by BSDi long ago, but there are
few (if any) packages in the wild still using the symbol so breakage in ports
should be near zero. No other system inherited the symbol from BSDi, either.

Then, use the new sysctl(2) in systat(1) and top(1):

- systat(1) draws placeholder marks ('-') instead of percentages for
offline CPUs in the cpu view.

- systat(1) omits offline CPU ticks when drawing the "big bar" in
the vmstat view. The upshot is that the bar isn't half idle when
half your logical CPUs are disabled.

- top(1) does not draw lines for offline CPUs; if CPUs toggle on or
offline in interactive mode we redraw the display to expand/reduce
space for the new/missing CPUs. This is consistent with what some
top(1) implementations do on Linux.

- top(1) omits offline CPUs from the totals when CPU totals are
combined into a single line (the '-1' flag).

Originally prompted by deraadt@. Discussed endlessly with deraadt@,
ketennis@, and sthen@. Tested by jmc@ and jca@. Earlier versions also
discussed with jca@. Earlier versions tested by jmc@, tb@, and many
others.

docs ok jmc@, kernel bits ok ketennis@, everything ok sthen@,
"Is your stuff in yet?" deraadt@


Revision tags: OPENBSD_6_4_BASE
# 1.94 05-Oct-2018 cheloha

Revert KERN_CPTIME2 ENODEV changes in kernel and userspace.

ok kettenis deraadt


# 1.93 26-Sep-2018 cheloha

KERN_CPTIME2: set ENODEV if the CPU is offline.

This lets userspace distinguish between idle CPUs and those that are
not schedulable because hw.smt=0.

A subsequent commit probably needs to add documentation for this
to sysctl.2 (and perhaps elsewhere) after the dust settles.

Also included here are changes to systat(1) and top(1) that account
for the ENODEV case and adjust behavior accordingly:

- systat(1)'s cpu view prints placeholder marks ('-') instead of
percentages for each state if the given CPU is offline.

- systat(1)'s vmstat view checks for offline CPUs when computing the
machine state total and excludes them, so the CPU usage graph
only represents the states for online CPUs.

- top(1) does not draw CPU rows for offline CPUs when the view is
redrawn. If CPUs "go offline", percentages for each state are
replaced by placeholder marks ('-'); the view will need to be
redrawn to remove these rows. If CPUs "go online" the view will
need to be redrawn to show these new CPUs. In "combined CPU" mode,
the count and the state totals only represent online CPUs.

Ports using KERN_CPTIME2 will need to be updated. The changes
described above to make systat(1) and top(1) aware of the ENODEV
case *and* gracefully handle a changing HW_NCPUONLINE while the
application is running are not necessarily appropriate for each
and every port.

The changes described above are so extensive in part to demonstrate
one way a program *might* be made robust to changing CPU availability.
In particular, changing hw.smt after boot is an extremely rare event,
and this needs to be weighed when updating ports.

The logic needed to account for the KERN_CPTIME2 ENODEV case is
very roughly:

if (sysctl(...) == -1) {
if (errno != ENODEV) {
/* Actual error occurred. */
} else {
/* CPU is offline. */
}
} else {
/* CPU is online and CPU states were set by sysctl(2). */
}

Prompted by deraadt@. Basic idea for ENODEV from kettenis@. Discussed at
length with kettenis@. Additional testing by tb@.

No complaints from hackers@ after a week.

ok kettenis@, "I think you should commit [now]" deraadt@


# 1.92 22-Sep-2018 millert

Use user_from_uid() and uid_from_user() directly. The wrappers
in username.c are now so simple there is no longer a good reason
to use them. OK deraadt@


# 1.91 13-Sep-2018 millert

Fix warnings caused by user_from_uid() and group_from_gid() now
returning const char *.


# 1.90 14-May-2018 mpi

Stopping counting and reporting CPU time spent spinning on a lock as
system time.

Introduce a new CP_SPIN "scheduler state" and modify userland tools
to display the % of timer a CPU spents spinning.

Based on a diff from jmatthew@, ok pirofti@, bluhm@, visa@, deraadt@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.89 30-May-2017 tedu

avoid some shadow warnings. from Brian Callahan


Revision tags: OPENBSD_6_1_BASE
# 1.88 15-Mar-2017 deraadt

annoying whitespace die die die


# 1.87 28-Jul-2016 tedu

rework realloc loop. there's no need to shrink the allocation between
calls. if we need a big space once, we'll likely need a big space again.


Revision tags: OPENBSD_6_0_BASE
# 1.86 11-May-2016 edd

Allow top(1) to filter process args if they are being displayed.

OK tedu@, deraadt@, and with input from Michal Mazurek.

Thanks


Revision tags: OPENBSD_5_9_BASE
# 1.85 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_8_BASE
# 1.84 06-May-2015 mpi

Display thread IDs instead of the name of the process's owner when "-H"
is used.

The rationnal is that when you're looking at threads you're generally
already filtereing by PID and this allow you to see which thread is a
pig.

Written some time ago with mikeb@

ok sthen@, krw@, guenther@


Revision tags: OPENBSD_5_7_BASE
# 1.83 19-Jan-2015 millert

Kill loadavg.h and just use the fscale value directly.
There's no need for crazy casts of p_pctcpu in ORDERKEY_PCTCPU
since kinfo_proc exports it as uint32_t. OK deraadt@


# 1.82 19-Jan-2015 deraadt

like in ps(1), fetch the FSCALE value using sysctl rather than using
the header version
ok guenther


# 1.81 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.80 17-Sep-2014 dlg

if there are more than 8 cpus, default to combined cpu stats (like
you passed -1 on the command line).

ok kettenis@ tedu@


# 1.79 15-Sep-2014 miod

Remove non-standard <sys/dkstat.h> header. It has not contained anything
related to disk stastics for almost 17 years, and the remaining
userland-visible defines duplicate those found in <sys/sched.h>.

Move the remaining _KERNEL defines to <sys/tty.h> where they belong, and
update all users to cope with this.

ok kettenis@


Revision tags: OPENBSD_5_6_BASE
# 1.78 04-Jul-2014 guenther

Track whether a process is a zombie or not yet fully built via flags
PS_{ZOMBIE,EMBRYO} on the process instead of peeking into the process's
thread data. This eliminates the need for the thread-level SDEAD state.

Change kvm_getprocs() (both the sysctl() and kvm backends) to report the
"most active" scheduler state for the process's threads.

tweaks kettenis@
feedback and ok matthew@


# 1.77 08-Apr-2014 mpi

Use VM_UVMEXP instead of VM_METER for memory usages and directly
include <sys/vmmeter.h> where it is needed instead of relying on
it being included by <uvm/uvm_extern.h>.

miod@ likes it, ok guenther@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.76 23-Mar-2013 tedu

wchan is no longer exposed, just check if wmesg is not empty.
(wmesg hasn't itself been a pointer since conversion to kinfo_proc.)
noticed by sthen.
ok deraadt jsing millert sthen


Revision tags: OPENBSD_5_3_BASE
# 1.75 18-Dec-2012 millert

We no longer use struct eproc for kinfo_proc in sysctl.h so there
is no direct need for sys/proc.h or sys/resource.h. Some consumers
of kinfo_proc need these for the proc flags and rlimit defines like
RLIM_INF so add the appropriate includes to them.
OK deraadt@ sthen@


Revision tags: OPENBSD_5_2_BASE
# 1.74 09-Jul-2012 deraadt

According to miod, KERNBASE used as a userland symbol should die
in a fire. Start by removing the definitions, before we clean the
headers later.


# 1.73 05-Jun-2012 brynet

Add support for hiding a user's processes in top.

feedback & ok lum@


# 1.72 21-Apr-2012 guenther

Remove a check made superfluous by the KERN_PROC_SHOW_THREADS flag.
This lets new binaries work (at least w/o the -H option) with old kernels.

ok pirofti@ deraadt@


# 1.71 17-Apr-2012 pirofti

Make it optional for kvm_getprocs() (and related sysctl) to return
thread information.

Add a KERN_PROC_SHOW_THREADS flag that has to be set in order to get the
thread info and make it off by default. This creates backwards compatibility
for applications that relied on a given size/number of items to be returned.

Modify ps(1) and top(1) accordingly.

Okay guenther@.


# 1.70 12-Apr-2012 pirofti

Add per thread accounting, mainly for usage & friends.

This expands the already bloated FILL_KPROC macro to take an extra
parameter that indicates if the callee is a thread or a process.

The userland bits are adjusted accordingly and ps(1) and top(1) now
display per thread usage times when -H is used.

Also pkill(1) had to be adjusted so that duplicates don't pop up.

libkvm does basically the same thing as the kernel bits.

Okay guenther@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.69 12-Jul-2011 tedu

print out the amount of memory used for cache. ok otto


# 1.68 10-Apr-2011 guenther

Switch back from KERN_PROC2/kinfo_proc2 to KERN_PROC/kinfo_proc now
that we've got name we want for the API we want

"ZAP!" deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.67 26-Apr-2010 deraadt

more portable code


# 1.66 26-Mar-2010 lum

Make the source of process TIME for displaying and sorting the same.
top(1) and ps(1) now display the same info.
ok tedu@ millert@ otto@ and tested on sparc64 by landry@


Revision tags: OPENBSD_4_7_BASE
# 1.65 29-Jan-2010 tedu

Allow sorting by command and pid. Also, allow partial matches with strncmp.
ok jmc otto


Revision tags: OPENBSD_4_6_BASE
# 1.64 28-Apr-2009 deraadt

extend STATE column to fit > 9 cpus; Aivar Jaakson; PR6131


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.63 01-Nov-2007 otto

bump arbitrary limit on arg length shown by -C; requested by and ok deraadt@


Revision tags: OPENBSD_4_2_BASE
# 1.62 27-Jul-2007 deraadt

replace a few more malloc(n*m) idioms with calloc(n,m) for safety;
inspired by zinovik@cs.karelia.ru


# 1.61 29-May-2007 otto

Instead of using hand-crafted redraws minimizing screen updates, use curses.
Enables nice things like process highlighting without hurting the brain.
ok deraadt@


# 1.60 16-Apr-2007 otto

don't truncate wchan, ok miod@


# 1.59 13-Apr-2007 otto

include cleanup and fix in comment; from Mark Lumsden


# 1.58 04-Apr-2007 otto

missing prototypes; from Mark Lumsden with a twist by me.


Revision tags: OPENBSD_4_1_BASE
# 1.57 04-Feb-2007 otto

long and long long is not the same on 32 bits archs


# 1.56 03-Jan-2007 otto

Add a 'g' command to only show processes having a string in their
command name. ok deraadt@


# 1.55 02-Jan-2007 otto

top(1) doesn't use any math.h function, so do not include math.h and
do not link with libm. ok ray@


# 1.54 29-Nov-2006 miod

Do not test for processes being swapped out since this can't happen anymore.


# 1.53 20-Sep-2006 ray

Recommit memory leak fix.


Revision tags: OPENBSD_4_0_BASE
# 1.52 29-Apr-2006 otto

use calloc(3) instead of malloc(3) to allocate cpu state structs; it's
better style here; plus it prevents bogus data to show up on first display.
report + ok pedro@


Revision tags: OPENBSD_3_9_BASE
# 1.51 21-Dec-2005 millert

Userland programs should include <errno.h> not <sys/errno.h>
OK deraadt@


# 1.50 04-Dec-2005 tedu

support for toggling thread viewing.
ok brad, hints from jmc


Revision tags: OPENBSD_3_8_BASE
# 1.49 17-Jun-2005 markus

add a 'C' command ) that toggles the display of the full cmdline;
with Jared Yanovich; ok deraadt


# 1.48 08-Jun-2005 millert

Multi-cpu support for top. Each cpu now gets its own stats line.
Based on work by todd@ at the hackathon.


Revision tags: OPENBSD_3_7_BASE
# 1.47 06-Dec-2004 markus

missing space; bluhm at genua.de


# 1.46 22-Nov-2004 pat

simplify loop iteration

ok otto millert


# 1.45 17-Nov-2004 markus

off-by-one in process_states[], fixes display of memory; ok deraadt, aaron


# 1.44 07-Oct-2004 otto

introduce -p option and p command to only show a single process.
From Patrick Latifi. ok deraadt@ millert@


Revision tags: OPENBSD_3_6_BASE
# 1.43 13-Jun-2004 otto

Typos in comments and a redundant prototype. From Brian Poole via jmc@
ok jmc@


# 1.42 11-Jun-2004 deraadt

handle the KI_NOCPU case, pointed out by markus


# 1.41 11-Jun-2004 deraadt

only print /# if > 1 cpu on a machine


# 1.40 11-Jun-2004 deraadt

export cpuid via kproc2, and make ps & top aware... from niklas


# 1.39 11-Jun-2004 deraadt

hide top breakage, until the next commit which will do it right


# 1.38 09-May-2004 deraadt

spaces


Revision tags: OPENBSD_3_5_BASE
# 1.37 08-Jan-2004 millert

Convert to kinfo_proc2; ok deraadt@


Revision tags: OPENBSD_3_4_BASE
# 1.36 07-Jul-2003 deraadt

protos


# 1.35 18-Jun-2003 deraadt

remove unused variables


# 1.34 17-Jun-2003 jfb

properly typecast to uid_t the return value of proc_owner() and report
ESRCH instead of EACCES if it returns -1

ok millert@


# 1.33 15-Jun-2003 millert

use uid_t and fix some sign compare warnings; OK krw@ and deraadt@


# 1.32 14-Jun-2003 avsm

use correct length instead of sizeof(char *) for some strl* bound values
millert@ ok


# 1.31 13-Jun-2003 deraadt

readable code


# 1.30 12-Jun-2003 deraadt

knf


# 1.29 12-Jun-2003 pvalchev

cleanup; ok deraadt


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.28 02-Jul-2002 tholo

Add missing copyright


Revision tags: OPENBSD_3_1_BASE
# 1.27 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.26 05-Dec-2001 art

Get maxslp with sysctl. Not the constant.


Revision tags: OPENBSD_3_0_BASE
# 1.25 12-Jul-2001 deraadt

first pass at a -Wall cleanup


# 1.24 03-Jun-2001 angelos

Use KERN_NPROCS to get the number of processes on the system, to
determine the amount of space we'll need to store the
information. The alternative, calling sysctl() with a NULL argument
for data, meant the kernel had to go through the process table.


Revision tags: OPENBSD_2_9_BASE
# 1.23 22-Feb-2001 deraadt

plug mem leak i introduced; spotted by millert


# 1.22 17-Feb-2001 deraadt

bye bye setgid kvm


# 1.21 17-Feb-2001 deraadt

do not use nlist anymore, kvm gets the bullet next


# 1.20 17-Feb-2001 deraadt

partial KNF, because i cannot read the diffs i am trying to debug


Revision tags: OPENBSD_2_8_BASE
# 1.19 18-Jun-2000 niklas

Mark kernel threads as system processes. Make ps hide them by default,
show them with -k. Do not try to show RSS based values for them as they
mess up column alignment. vmstat -f now shows kernel threads separately
from rforks too.


Revision tags: OPENBSD_2_7_BASE
# 1.18 14-Nov-1999 deraadt

stathz instead of hz, plus improvements by me; camield@inet.unisource.nl


# 1.17 29-Oct-1999 todd

writen


Revision tags: OPENBSD_2_6_BASE
# 1.16 21-Jun-1999 art

stop casting the third argument to kvm_read to char *. it is a void *.


# 1.15 22-May-1999 weingart

Update for new vmswap code.


Revision tags: OPENBSD_2_5_BASE
# 1.14 28-Nov-1998 kstailey

do not count swap spaces that were not swapon(2)ed


Revision tags: OPENBSD_2_4_BASE
# 1.13 20-Sep-1998 niklas

cp_time is long, deal with it.


# 1.12 20-Sep-1998 niklas

widen unsigned entities to signed ones before subtracting, fixes sorting on alpha


# 1.11 21-Aug-1998 kstailey

support "-o field" and the interactive "o" command


# 1.10 08-Jul-1998 deraadt

whack kmem gid after kvm_openfiles()


# 1.9 25-Jun-1998 deraadt

handle missing swap


Revision tags: OPENBSD_2_3_BASE
# 1.8 08-Nov-1997 millert

Sort idle processes by total CPU usage, not cpticks since cpticks
get zeroed every second; dan@dan.emsphone.com


Revision tags: OPENBSD_2_2_BASE
# 1.7 09-Sep-1997 millert

"last pid" stuff is useless with random pids.


# 1.6 09-Sep-1997 millert

Use kvm_openfiles() not the sunos compat kvm_open().
Use warn/warnx where it makes sense and check some more ret vals.


# 1.5 24-Aug-1997 millert

Fix compiler warnings.


# 1.4 22-Aug-1997 downsj

Two more alpha warnings.


# 1.3 22-Aug-1997 downsj

First sweep. Prototype, type fixes, long fixes, mostly compiles with
-Wall.


# 1.2 17-Aug-1997 kstailey

print "idle" for idle processes


# 1.1 14-Aug-1997 downsj

top 3.4, with a few changes. Still needs more work.


# 1.109 25-Aug-2020 kn

Rename rtable filter variables and simplify buffer handling

No functional change, the rename is for consistency and the buffer
handling reduces churn in an upcoming diff.


# 1.108 23-Aug-2020 kn

Filter by routing table

"-T-0" for processes outside the default routing table, "-T3" for those in
a specific one; same semantics as with other filters.

Manual wording and command line flag taken from pgrep(1) being the only way
to identify processes by routing table; After netstat(1)'s recent addition
of "-R", filtering in top makes for handy tooling around rtable(4).

"looks good to me" millert
OK remi


# 1.107 06-Jul-2020 kn

Use mnemonic KERN_PROC_ALL not literal zero

No object change.


# 1.106 26-Jun-2020 kn

Remove duplicate initialisation

Those are global variables are (zero) initialised as such already and
machine_init() is called only once upon startup.

OK mvs


# 1.105 25-Jun-2020 kn

Remove unused "remaining" member in struct handle

Only ever set or decremented since import with machine.c r1.1 (1997);

While here, simplify the skip semantics behind the scroll functionality.

OK millert


# 1.104 24-Jun-2020 kn

Remove obsolete comment about "extra nulls"

Those were removed in r1.39 in 2004.


# 1.103 23-Jun-2020 kn

Remove redundant NULL check

For the sake of simplicity and to reflect that the process list is always
sorted (default is "cpu"), even if not explicitly requested; this makes it
easier to argue about the code around sort order and its selection.

OK millert


Revision tags: OPENBSD_6_7_BASE
# 1.102 06-Jan-2020 zhuk

Implement scrolling in top(1) using 9 and 0 keys.

Planned improvements: scroll position displaying, using arrow/pgup/pgdown keys.

okay tedu@


# 1.101 16-Dec-2019 guenther

Delete tests for P_THREAD that predate the existence of
KERN_PROC_SHOW_THREADS and have been rendered superfluous by it.
Similarly, some P_SYSTEM tests can be deleted or pushed to the
kernel by using KERN_PROC_ALL instead of KERN_PROC_KTHREAD.

ok visa@ mpi@


Revision tags: OPENBSD_6_6_BASE
# 1.100 08-Oct-2019 kn

Simplify threads flag handling

Instead of always passing the threads flag which decides whether other
function arguments are used or not, make callers pass those arguments or
NULL depending on the threads flag.

This reflects better how thread IDs are shown in place of user names/IDs.

OK millert


# 1.99 06-Oct-2019 kn

Avoid gasting around get_process_info()

get_process_info() returns a pointer to the global handle later only be
used in format_next_process(); treat this struct handle as such without
casting the pointer to caddr_t and back again.

No object change.
OK millert deraadt


# 1.98 06-Oct-2019 kn

Zap intermediate structs in compare_*()

SETORDER() can assign from function arguments directly without additional
identical structs in each function.

No object change.
OK millert


# 1.97 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


Revision tags: OPENBSD_6_5_BASE
# 1.96 28-Nov-2018 kn

Allow reverse sort order

Prefixing the field with a dash will sort processes in reverse order.
`o -pid' will therefore list PID 1 on top.

"looks good" tedu, improvements and OK cheloha


# 1.95 17-Nov-2018 cheloha

Add new KERN_CPUSTATS sysctl(2) so we can identify offline CPUs.

Because of hw.smt we need a way to determine whether a given CPU is "online"
or "offline" from userspace. KERN_CPTIME2 is an array, and so cannot be
cleanly extended for this purpose, so add a new sysctl(2) KERN_CPUSTATS
with an extensible struct. At the moment it's just KERN_CPTIME2 with a
flags member, but it can grow as needed.

KERN_CPUSTATS appears to have been defined by BSDi long ago, but there are
few (if any) packages in the wild still using the symbol so breakage in ports
should be near zero. No other system inherited the symbol from BSDi, either.

Then, use the new sysctl(2) in systat(1) and top(1):

- systat(1) draws placeholder marks ('-') instead of percentages for
offline CPUs in the cpu view.

- systat(1) omits offline CPU ticks when drawing the "big bar" in
the vmstat view. The upshot is that the bar isn't half idle when
half your logical CPUs are disabled.

- top(1) does not draw lines for offline CPUs; if CPUs toggle on or
offline in interactive mode we redraw the display to expand/reduce
space for the new/missing CPUs. This is consistent with what some
top(1) implementations do on Linux.

- top(1) omits offline CPUs from the totals when CPU totals are
combined into a single line (the '-1' flag).

Originally prompted by deraadt@. Discussed endlessly with deraadt@,
ketennis@, and sthen@. Tested by jmc@ and jca@. Earlier versions also
discussed with jca@. Earlier versions tested by jmc@, tb@, and many
others.

docs ok jmc@, kernel bits ok ketennis@, everything ok sthen@,
"Is your stuff in yet?" deraadt@


Revision tags: OPENBSD_6_4_BASE
# 1.94 05-Oct-2018 cheloha

Revert KERN_CPTIME2 ENODEV changes in kernel and userspace.

ok kettenis deraadt


# 1.93 26-Sep-2018 cheloha

KERN_CPTIME2: set ENODEV if the CPU is offline.

This lets userspace distinguish between idle CPUs and those that are
not schedulable because hw.smt=0.

A subsequent commit probably needs to add documentation for this
to sysctl.2 (and perhaps elsewhere) after the dust settles.

Also included here are changes to systat(1) and top(1) that account
for the ENODEV case and adjust behavior accordingly:

- systat(1)'s cpu view prints placeholder marks ('-') instead of
percentages for each state if the given CPU is offline.

- systat(1)'s vmstat view checks for offline CPUs when computing the
machine state total and excludes them, so the CPU usage graph
only represents the states for online CPUs.

- top(1) does not draw CPU rows for offline CPUs when the view is
redrawn. If CPUs "go offline", percentages for each state are
replaced by placeholder marks ('-'); the view will need to be
redrawn to remove these rows. If CPUs "go online" the view will
need to be redrawn to show these new CPUs. In "combined CPU" mode,
the count and the state totals only represent online CPUs.

Ports using KERN_CPTIME2 will need to be updated. The changes
described above to make systat(1) and top(1) aware of the ENODEV
case *and* gracefully handle a changing HW_NCPUONLINE while the
application is running are not necessarily appropriate for each
and every port.

The changes described above are so extensive in part to demonstrate
one way a program *might* be made robust to changing CPU availability.
In particular, changing hw.smt after boot is an extremely rare event,
and this needs to be weighed when updating ports.

The logic needed to account for the KERN_CPTIME2 ENODEV case is
very roughly:

if (sysctl(...) == -1) {
if (errno != ENODEV) {
/* Actual error occurred. */
} else {
/* CPU is offline. */
}
} else {
/* CPU is online and CPU states were set by sysctl(2). */
}

Prompted by deraadt@. Basic idea for ENODEV from kettenis@. Discussed at
length with kettenis@. Additional testing by tb@.

No complaints from hackers@ after a week.

ok kettenis@, "I think you should commit [now]" deraadt@


# 1.92 22-Sep-2018 millert

Use user_from_uid() and uid_from_user() directly. The wrappers
in username.c are now so simple there is no longer a good reason
to use them. OK deraadt@


# 1.91 13-Sep-2018 millert

Fix warnings caused by user_from_uid() and group_from_gid() now
returning const char *.


# 1.90 14-May-2018 mpi

Stopping counting and reporting CPU time spent spinning on a lock as
system time.

Introduce a new CP_SPIN "scheduler state" and modify userland tools
to display the % of timer a CPU spents spinning.

Based on a diff from jmatthew@, ok pirofti@, bluhm@, visa@, deraadt@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.89 30-May-2017 tedu

avoid some shadow warnings. from Brian Callahan


Revision tags: OPENBSD_6_1_BASE
# 1.88 15-Mar-2017 deraadt

annoying whitespace die die die


# 1.87 28-Jul-2016 tedu

rework realloc loop. there's no need to shrink the allocation between
calls. if we need a big space once, we'll likely need a big space again.


Revision tags: OPENBSD_6_0_BASE
# 1.86 11-May-2016 edd

Allow top(1) to filter process args if they are being displayed.

OK tedu@, deraadt@, and with input from Michal Mazurek.

Thanks


Revision tags: OPENBSD_5_9_BASE
# 1.85 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_8_BASE
# 1.84 06-May-2015 mpi

Display thread IDs instead of the name of the process's owner when "-H"
is used.

The rationnal is that when you're looking at threads you're generally
already filtereing by PID and this allow you to see which thread is a
pig.

Written some time ago with mikeb@

ok sthen@, krw@, guenther@


Revision tags: OPENBSD_5_7_BASE
# 1.83 19-Jan-2015 millert

Kill loadavg.h and just use the fscale value directly.
There's no need for crazy casts of p_pctcpu in ORDERKEY_PCTCPU
since kinfo_proc exports it as uint32_t. OK deraadt@


# 1.82 19-Jan-2015 deraadt

like in ps(1), fetch the FSCALE value using sysctl rather than using
the header version
ok guenther


# 1.81 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.80 17-Sep-2014 dlg

if there are more than 8 cpus, default to combined cpu stats (like
you passed -1 on the command line).

ok kettenis@ tedu@


# 1.79 15-Sep-2014 miod

Remove non-standard <sys/dkstat.h> header. It has not contained anything
related to disk stastics for almost 17 years, and the remaining
userland-visible defines duplicate those found in <sys/sched.h>.

Move the remaining _KERNEL defines to <sys/tty.h> where they belong, and
update all users to cope with this.

ok kettenis@


Revision tags: OPENBSD_5_6_BASE
# 1.78 04-Jul-2014 guenther

Track whether a process is a zombie or not yet fully built via flags
PS_{ZOMBIE,EMBRYO} on the process instead of peeking into the process's
thread data. This eliminates the need for the thread-level SDEAD state.

Change kvm_getprocs() (both the sysctl() and kvm backends) to report the
"most active" scheduler state for the process's threads.

tweaks kettenis@
feedback and ok matthew@


# 1.77 08-Apr-2014 mpi

Use VM_UVMEXP instead of VM_METER for memory usages and directly
include <sys/vmmeter.h> where it is needed instead of relying on
it being included by <uvm/uvm_extern.h>.

miod@ likes it, ok guenther@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.76 23-Mar-2013 tedu

wchan is no longer exposed, just check if wmesg is not empty.
(wmesg hasn't itself been a pointer since conversion to kinfo_proc.)
noticed by sthen.
ok deraadt jsing millert sthen


Revision tags: OPENBSD_5_3_BASE
# 1.75 18-Dec-2012 millert

We no longer use struct eproc for kinfo_proc in sysctl.h so there
is no direct need for sys/proc.h or sys/resource.h. Some consumers
of kinfo_proc need these for the proc flags and rlimit defines like
RLIM_INF so add the appropriate includes to them.
OK deraadt@ sthen@


Revision tags: OPENBSD_5_2_BASE
# 1.74 09-Jul-2012 deraadt

According to miod, KERNBASE used as a userland symbol should die
in a fire. Start by removing the definitions, before we clean the
headers later.


# 1.73 05-Jun-2012 brynet

Add support for hiding a user's processes in top.

feedback & ok lum@


# 1.72 21-Apr-2012 guenther

Remove a check made superfluous by the KERN_PROC_SHOW_THREADS flag.
This lets new binaries work (at least w/o the -H option) with old kernels.

ok pirofti@ deraadt@


# 1.71 17-Apr-2012 pirofti

Make it optional for kvm_getprocs() (and related sysctl) to return
thread information.

Add a KERN_PROC_SHOW_THREADS flag that has to be set in order to get the
thread info and make it off by default. This creates backwards compatibility
for applications that relied on a given size/number of items to be returned.

Modify ps(1) and top(1) accordingly.

Okay guenther@.


# 1.70 12-Apr-2012 pirofti

Add per thread accounting, mainly for usage & friends.

This expands the already bloated FILL_KPROC macro to take an extra
parameter that indicates if the callee is a thread or a process.

The userland bits are adjusted accordingly and ps(1) and top(1) now
display per thread usage times when -H is used.

Also pkill(1) had to be adjusted so that duplicates don't pop up.

libkvm does basically the same thing as the kernel bits.

Okay guenther@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.69 12-Jul-2011 tedu

print out the amount of memory used for cache. ok otto


# 1.68 10-Apr-2011 guenther

Switch back from KERN_PROC2/kinfo_proc2 to KERN_PROC/kinfo_proc now
that we've got name we want for the API we want

"ZAP!" deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.67 26-Apr-2010 deraadt

more portable code


# 1.66 26-Mar-2010 lum

Make the source of process TIME for displaying and sorting the same.
top(1) and ps(1) now display the same info.
ok tedu@ millert@ otto@ and tested on sparc64 by landry@


Revision tags: OPENBSD_4_7_BASE
# 1.65 29-Jan-2010 tedu

Allow sorting by command and pid. Also, allow partial matches with strncmp.
ok jmc otto


Revision tags: OPENBSD_4_6_BASE
# 1.64 28-Apr-2009 deraadt

extend STATE column to fit > 9 cpus; Aivar Jaakson; PR6131


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.63 01-Nov-2007 otto

bump arbitrary limit on arg length shown by -C; requested by and ok deraadt@


Revision tags: OPENBSD_4_2_BASE
# 1.62 27-Jul-2007 deraadt

replace a few more malloc(n*m) idioms with calloc(n,m) for safety;
inspired by zinovik@cs.karelia.ru


# 1.61 29-May-2007 otto

Instead of using hand-crafted redraws minimizing screen updates, use curses.
Enables nice things like process highlighting without hurting the brain.
ok deraadt@


# 1.60 16-Apr-2007 otto

don't truncate wchan, ok miod@


# 1.59 13-Apr-2007 otto

include cleanup and fix in comment; from Mark Lumsden


# 1.58 04-Apr-2007 otto

missing prototypes; from Mark Lumsden with a twist by me.


Revision tags: OPENBSD_4_1_BASE
# 1.57 04-Feb-2007 otto

long and long long is not the same on 32 bits archs


# 1.56 03-Jan-2007 otto

Add a 'g' command to only show processes having a string in their
command name. ok deraadt@


# 1.55 02-Jan-2007 otto

top(1) doesn't use any math.h function, so do not include math.h and
do not link with libm. ok ray@


# 1.54 29-Nov-2006 miod

Do not test for processes being swapped out since this can't happen anymore.


# 1.53 20-Sep-2006 ray

Recommit memory leak fix.


Revision tags: OPENBSD_4_0_BASE
# 1.52 29-Apr-2006 otto

use calloc(3) instead of malloc(3) to allocate cpu state structs; it's
better style here; plus it prevents bogus data to show up on first display.
report + ok pedro@


Revision tags: OPENBSD_3_9_BASE
# 1.51 21-Dec-2005 millert

Userland programs should include <errno.h> not <sys/errno.h>
OK deraadt@


# 1.50 04-Dec-2005 tedu

support for toggling thread viewing.
ok brad, hints from jmc


Revision tags: OPENBSD_3_8_BASE
# 1.49 17-Jun-2005 markus

add a 'C' command ) that toggles the display of the full cmdline;
with Jared Yanovich; ok deraadt


# 1.48 08-Jun-2005 millert

Multi-cpu support for top. Each cpu now gets its own stats line.
Based on work by todd@ at the hackathon.


Revision tags: OPENBSD_3_7_BASE
# 1.47 06-Dec-2004 markus

missing space; bluhm at genua.de


# 1.46 22-Nov-2004 pat

simplify loop iteration

ok otto millert


# 1.45 17-Nov-2004 markus

off-by-one in process_states[], fixes display of memory; ok deraadt, aaron


# 1.44 07-Oct-2004 otto

introduce -p option and p command to only show a single process.
From Patrick Latifi. ok deraadt@ millert@


Revision tags: OPENBSD_3_6_BASE
# 1.43 13-Jun-2004 otto

Typos in comments and a redundant prototype. From Brian Poole via jmc@
ok jmc@


# 1.42 11-Jun-2004 deraadt

handle the KI_NOCPU case, pointed out by markus


# 1.41 11-Jun-2004 deraadt

only print /# if > 1 cpu on a machine


# 1.40 11-Jun-2004 deraadt

export cpuid via kproc2, and make ps & top aware... from niklas


# 1.39 11-Jun-2004 deraadt

hide top breakage, until the next commit which will do it right


# 1.38 09-May-2004 deraadt

spaces


Revision tags: OPENBSD_3_5_BASE
# 1.37 08-Jan-2004 millert

Convert to kinfo_proc2; ok deraadt@


Revision tags: OPENBSD_3_4_BASE
# 1.36 07-Jul-2003 deraadt

protos


# 1.35 18-Jun-2003 deraadt

remove unused variables


# 1.34 17-Jun-2003 jfb

properly typecast to uid_t the return value of proc_owner() and report
ESRCH instead of EACCES if it returns -1

ok millert@


# 1.33 15-Jun-2003 millert

use uid_t and fix some sign compare warnings; OK krw@ and deraadt@


# 1.32 14-Jun-2003 avsm

use correct length instead of sizeof(char *) for some strl* bound values
millert@ ok


# 1.31 13-Jun-2003 deraadt

readable code


# 1.30 12-Jun-2003 deraadt

knf


# 1.29 12-Jun-2003 pvalchev

cleanup; ok deraadt


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.28 02-Jul-2002 tholo

Add missing copyright


Revision tags: OPENBSD_3_1_BASE
# 1.27 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.26 05-Dec-2001 art

Get maxslp with sysctl. Not the constant.


Revision tags: OPENBSD_3_0_BASE
# 1.25 12-Jul-2001 deraadt

first pass at a -Wall cleanup


# 1.24 03-Jun-2001 angelos

Use KERN_NPROCS to get the number of processes on the system, to
determine the amount of space we'll need to store the
information. The alternative, calling sysctl() with a NULL argument
for data, meant the kernel had to go through the process table.


Revision tags: OPENBSD_2_9_BASE
# 1.23 22-Feb-2001 deraadt

plug mem leak i introduced; spotted by millert


# 1.22 17-Feb-2001 deraadt

bye bye setgid kvm


# 1.21 17-Feb-2001 deraadt

do not use nlist anymore, kvm gets the bullet next


# 1.20 17-Feb-2001 deraadt

partial KNF, because i cannot read the diffs i am trying to debug


Revision tags: OPENBSD_2_8_BASE
# 1.19 18-Jun-2000 niklas

Mark kernel threads as system processes. Make ps hide them by default,
show them with -k. Do not try to show RSS based values for them as they
mess up column alignment. vmstat -f now shows kernel threads separately
from rforks too.


Revision tags: OPENBSD_2_7_BASE
# 1.18 14-Nov-1999 deraadt

stathz instead of hz, plus improvements by me; camield@inet.unisource.nl


# 1.17 29-Oct-1999 todd

writen


Revision tags: OPENBSD_2_6_BASE
# 1.16 21-Jun-1999 art

stop casting the third argument to kvm_read to char *. it is a void *.


# 1.15 22-May-1999 weingart

Update for new vmswap code.


Revision tags: OPENBSD_2_5_BASE
# 1.14 28-Nov-1998 kstailey

do not count swap spaces that were not swapon(2)ed


Revision tags: OPENBSD_2_4_BASE
# 1.13 20-Sep-1998 niklas

cp_time is long, deal with it.


# 1.12 20-Sep-1998 niklas

widen unsigned entities to signed ones before subtracting, fixes sorting on alpha


# 1.11 21-Aug-1998 kstailey

support "-o field" and the interactive "o" command


# 1.10 08-Jul-1998 deraadt

whack kmem gid after kvm_openfiles()


# 1.9 25-Jun-1998 deraadt

handle missing swap


Revision tags: OPENBSD_2_3_BASE
# 1.8 08-Nov-1997 millert

Sort idle processes by total CPU usage, not cpticks since cpticks
get zeroed every second; dan@dan.emsphone.com


Revision tags: OPENBSD_2_2_BASE
# 1.7 09-Sep-1997 millert

"last pid" stuff is useless with random pids.


# 1.6 09-Sep-1997 millert

Use kvm_openfiles() not the sunos compat kvm_open().
Use warn/warnx where it makes sense and check some more ret vals.


# 1.5 24-Aug-1997 millert

Fix compiler warnings.


# 1.4 22-Aug-1997 downsj

Two more alpha warnings.


# 1.3 22-Aug-1997 downsj

First sweep. Prototype, type fixes, long fixes, mostly compiles with
-Wall.


# 1.2 17-Aug-1997 kstailey

print "idle" for idle processes


# 1.1 14-Aug-1997 downsj

top 3.4, with a few changes. Still needs more work.


# 1.108 23-Aug-2020 kn

Filter by routing table

"-T-0" for processes outside the default routing table, "-T3" for those in
a specific one; same semantics as with other filters.

Manual wording and command line flag taken from pgrep(1) being the only way
to identify processes by routing table; After netstat(1)'s recent addition
of "-R", filtering in top makes for handy tooling around rtable(4).

"looks good to me" millert
OK remi


# 1.107 06-Jul-2020 kn

Use mnemonic KERN_PROC_ALL not literal zero

No object change.


# 1.106 26-Jun-2020 kn

Remove duplicate initialisation

Those are global variables are (zero) initialised as such already and
machine_init() is called only once upon startup.

OK mvs


# 1.105 25-Jun-2020 kn

Remove unused "remaining" member in struct handle

Only ever set or decremented since import with machine.c r1.1 (1997);

While here, simplify the skip semantics behind the scroll functionality.

OK millert


# 1.104 24-Jun-2020 kn

Remove obsolete comment about "extra nulls"

Those were removed in r1.39 in 2004.


# 1.103 23-Jun-2020 kn

Remove redundant NULL check

For the sake of simplicity and to reflect that the process list is always
sorted (default is "cpu"), even if not explicitly requested; this makes it
easier to argue about the code around sort order and its selection.

OK millert


Revision tags: OPENBSD_6_7_BASE
# 1.102 06-Jan-2020 zhuk

Implement scrolling in top(1) using 9 and 0 keys.

Planned improvements: scroll position displaying, using arrow/pgup/pgdown keys.

okay tedu@


# 1.101 16-Dec-2019 guenther

Delete tests for P_THREAD that predate the existence of
KERN_PROC_SHOW_THREADS and have been rendered superfluous by it.
Similarly, some P_SYSTEM tests can be deleted or pushed to the
kernel by using KERN_PROC_ALL instead of KERN_PROC_KTHREAD.

ok visa@ mpi@


Revision tags: OPENBSD_6_6_BASE
# 1.100 08-Oct-2019 kn

Simplify threads flag handling

Instead of always passing the threads flag which decides whether other
function arguments are used or not, make callers pass those arguments or
NULL depending on the threads flag.

This reflects better how thread IDs are shown in place of user names/IDs.

OK millert


# 1.99 06-Oct-2019 kn

Avoid gasting around get_process_info()

get_process_info() returns a pointer to the global handle later only be
used in format_next_process(); treat this struct handle as such without
casting the pointer to caddr_t and back again.

No object change.
OK millert deraadt


# 1.98 06-Oct-2019 kn

Zap intermediate structs in compare_*()

SETORDER() can assign from function arguments directly without additional
identical structs in each function.

No object change.
OK millert


# 1.97 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


Revision tags: OPENBSD_6_5_BASE
# 1.96 28-Nov-2018 kn

Allow reverse sort order

Prefixing the field with a dash will sort processes in reverse order.
`o -pid' will therefore list PID 1 on top.

"looks good" tedu, improvements and OK cheloha


# 1.95 17-Nov-2018 cheloha

Add new KERN_CPUSTATS sysctl(2) so we can identify offline CPUs.

Because of hw.smt we need a way to determine whether a given CPU is "online"
or "offline" from userspace. KERN_CPTIME2 is an array, and so cannot be
cleanly extended for this purpose, so add a new sysctl(2) KERN_CPUSTATS
with an extensible struct. At the moment it's just KERN_CPTIME2 with a
flags member, but it can grow as needed.

KERN_CPUSTATS appears to have been defined by BSDi long ago, but there are
few (if any) packages in the wild still using the symbol so breakage in ports
should be near zero. No other system inherited the symbol from BSDi, either.

Then, use the new sysctl(2) in systat(1) and top(1):

- systat(1) draws placeholder marks ('-') instead of percentages for
offline CPUs in the cpu view.

- systat(1) omits offline CPU ticks when drawing the "big bar" in
the vmstat view. The upshot is that the bar isn't half idle when
half your logical CPUs are disabled.

- top(1) does not draw lines for offline CPUs; if CPUs toggle on or
offline in interactive mode we redraw the display to expand/reduce
space for the new/missing CPUs. This is consistent with what some
top(1) implementations do on Linux.

- top(1) omits offline CPUs from the totals when CPU totals are
combined into a single line (the '-1' flag).

Originally prompted by deraadt@. Discussed endlessly with deraadt@,
ketennis@, and sthen@. Tested by jmc@ and jca@. Earlier versions also
discussed with jca@. Earlier versions tested by jmc@, tb@, and many
others.

docs ok jmc@, kernel bits ok ketennis@, everything ok sthen@,
"Is your stuff in yet?" deraadt@


Revision tags: OPENBSD_6_4_BASE
# 1.94 05-Oct-2018 cheloha

Revert KERN_CPTIME2 ENODEV changes in kernel and userspace.

ok kettenis deraadt


# 1.93 26-Sep-2018 cheloha

KERN_CPTIME2: set ENODEV if the CPU is offline.

This lets userspace distinguish between idle CPUs and those that are
not schedulable because hw.smt=0.

A subsequent commit probably needs to add documentation for this
to sysctl.2 (and perhaps elsewhere) after the dust settles.

Also included here are changes to systat(1) and top(1) that account
for the ENODEV case and adjust behavior accordingly:

- systat(1)'s cpu view prints placeholder marks ('-') instead of
percentages for each state if the given CPU is offline.

- systat(1)'s vmstat view checks for offline CPUs when computing the
machine state total and excludes them, so the CPU usage graph
only represents the states for online CPUs.

- top(1) does not draw CPU rows for offline CPUs when the view is
redrawn. If CPUs "go offline", percentages for each state are
replaced by placeholder marks ('-'); the view will need to be
redrawn to remove these rows. If CPUs "go online" the view will
need to be redrawn to show these new CPUs. In "combined CPU" mode,
the count and the state totals only represent online CPUs.

Ports using KERN_CPTIME2 will need to be updated. The changes
described above to make systat(1) and top(1) aware of the ENODEV
case *and* gracefully handle a changing HW_NCPUONLINE while the
application is running are not necessarily appropriate for each
and every port.

The changes described above are so extensive in part to demonstrate
one way a program *might* be made robust to changing CPU availability.
In particular, changing hw.smt after boot is an extremely rare event,
and this needs to be weighed when updating ports.

The logic needed to account for the KERN_CPTIME2 ENODEV case is
very roughly:

if (sysctl(...) == -1) {
if (errno != ENODEV) {
/* Actual error occurred. */
} else {
/* CPU is offline. */
}
} else {
/* CPU is online and CPU states were set by sysctl(2). */
}

Prompted by deraadt@. Basic idea for ENODEV from kettenis@. Discussed at
length with kettenis@. Additional testing by tb@.

No complaints from hackers@ after a week.

ok kettenis@, "I think you should commit [now]" deraadt@


# 1.92 22-Sep-2018 millert

Use user_from_uid() and uid_from_user() directly. The wrappers
in username.c are now so simple there is no longer a good reason
to use them. OK deraadt@


# 1.91 13-Sep-2018 millert

Fix warnings caused by user_from_uid() and group_from_gid() now
returning const char *.


# 1.90 14-May-2018 mpi

Stopping counting and reporting CPU time spent spinning on a lock as
system time.

Introduce a new CP_SPIN "scheduler state" and modify userland tools
to display the % of timer a CPU spents spinning.

Based on a diff from jmatthew@, ok pirofti@, bluhm@, visa@, deraadt@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.89 30-May-2017 tedu

avoid some shadow warnings. from Brian Callahan


Revision tags: OPENBSD_6_1_BASE
# 1.88 15-Mar-2017 deraadt

annoying whitespace die die die


# 1.87 28-Jul-2016 tedu

rework realloc loop. there's no need to shrink the allocation between
calls. if we need a big space once, we'll likely need a big space again.


Revision tags: OPENBSD_6_0_BASE
# 1.86 11-May-2016 edd

Allow top(1) to filter process args if they are being displayed.

OK tedu@, deraadt@, and with input from Michal Mazurek.

Thanks


Revision tags: OPENBSD_5_9_BASE
# 1.85 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_8_BASE
# 1.84 06-May-2015 mpi

Display thread IDs instead of the name of the process's owner when "-H"
is used.

The rationnal is that when you're looking at threads you're generally
already filtereing by PID and this allow you to see which thread is a
pig.

Written some time ago with mikeb@

ok sthen@, krw@, guenther@


Revision tags: OPENBSD_5_7_BASE
# 1.83 19-Jan-2015 millert

Kill loadavg.h and just use the fscale value directly.
There's no need for crazy casts of p_pctcpu in ORDERKEY_PCTCPU
since kinfo_proc exports it as uint32_t. OK deraadt@


# 1.82 19-Jan-2015 deraadt

like in ps(1), fetch the FSCALE value using sysctl rather than using
the header version
ok guenther


# 1.81 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.80 17-Sep-2014 dlg

if there are more than 8 cpus, default to combined cpu stats (like
you passed -1 on the command line).

ok kettenis@ tedu@


# 1.79 15-Sep-2014 miod

Remove non-standard <sys/dkstat.h> header. It has not contained anything
related to disk stastics for almost 17 years, and the remaining
userland-visible defines duplicate those found in <sys/sched.h>.

Move the remaining _KERNEL defines to <sys/tty.h> where they belong, and
update all users to cope with this.

ok kettenis@


Revision tags: OPENBSD_5_6_BASE
# 1.78 04-Jul-2014 guenther

Track whether a process is a zombie or not yet fully built via flags
PS_{ZOMBIE,EMBRYO} on the process instead of peeking into the process's
thread data. This eliminates the need for the thread-level SDEAD state.

Change kvm_getprocs() (both the sysctl() and kvm backends) to report the
"most active" scheduler state for the process's threads.

tweaks kettenis@
feedback and ok matthew@


# 1.77 08-Apr-2014 mpi

Use VM_UVMEXP instead of VM_METER for memory usages and directly
include <sys/vmmeter.h> where it is needed instead of relying on
it being included by <uvm/uvm_extern.h>.

miod@ likes it, ok guenther@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.76 23-Mar-2013 tedu

wchan is no longer exposed, just check if wmesg is not empty.
(wmesg hasn't itself been a pointer since conversion to kinfo_proc.)
noticed by sthen.
ok deraadt jsing millert sthen


Revision tags: OPENBSD_5_3_BASE
# 1.75 18-Dec-2012 millert

We no longer use struct eproc for kinfo_proc in sysctl.h so there
is no direct need for sys/proc.h or sys/resource.h. Some consumers
of kinfo_proc need these for the proc flags and rlimit defines like
RLIM_INF so add the appropriate includes to them.
OK deraadt@ sthen@


Revision tags: OPENBSD_5_2_BASE
# 1.74 09-Jul-2012 deraadt

According to miod, KERNBASE used as a userland symbol should die
in a fire. Start by removing the definitions, before we clean the
headers later.


# 1.73 05-Jun-2012 brynet

Add support for hiding a user's processes in top.

feedback & ok lum@


# 1.72 21-Apr-2012 guenther

Remove a check made superfluous by the KERN_PROC_SHOW_THREADS flag.
This lets new binaries work (at least w/o the -H option) with old kernels.

ok pirofti@ deraadt@


# 1.71 17-Apr-2012 pirofti

Make it optional for kvm_getprocs() (and related sysctl) to return
thread information.

Add a KERN_PROC_SHOW_THREADS flag that has to be set in order to get the
thread info and make it off by default. This creates backwards compatibility
for applications that relied on a given size/number of items to be returned.

Modify ps(1) and top(1) accordingly.

Okay guenther@.


# 1.70 12-Apr-2012 pirofti

Add per thread accounting, mainly for usage & friends.

This expands the already bloated FILL_KPROC macro to take an extra
parameter that indicates if the callee is a thread or a process.

The userland bits are adjusted accordingly and ps(1) and top(1) now
display per thread usage times when -H is used.

Also pkill(1) had to be adjusted so that duplicates don't pop up.

libkvm does basically the same thing as the kernel bits.

Okay guenther@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.69 12-Jul-2011 tedu

print out the amount of memory used for cache. ok otto


# 1.68 10-Apr-2011 guenther

Switch back from KERN_PROC2/kinfo_proc2 to KERN_PROC/kinfo_proc now
that we've got name we want for the API we want

"ZAP!" deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.67 26-Apr-2010 deraadt

more portable code


# 1.66 26-Mar-2010 lum

Make the source of process TIME for displaying and sorting the same.
top(1) and ps(1) now display the same info.
ok tedu@ millert@ otto@ and tested on sparc64 by landry@


Revision tags: OPENBSD_4_7_BASE
# 1.65 29-Jan-2010 tedu

Allow sorting by command and pid. Also, allow partial matches with strncmp.
ok jmc otto


Revision tags: OPENBSD_4_6_BASE
# 1.64 28-Apr-2009 deraadt

extend STATE column to fit > 9 cpus; Aivar Jaakson; PR6131


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.63 01-Nov-2007 otto

bump arbitrary limit on arg length shown by -C; requested by and ok deraadt@


Revision tags: OPENBSD_4_2_BASE
# 1.62 27-Jul-2007 deraadt

replace a few more malloc(n*m) idioms with calloc(n,m) for safety;
inspired by zinovik@cs.karelia.ru


# 1.61 29-May-2007 otto

Instead of using hand-crafted redraws minimizing screen updates, use curses.
Enables nice things like process highlighting without hurting the brain.
ok deraadt@


# 1.60 16-Apr-2007 otto

don't truncate wchan, ok miod@


# 1.59 13-Apr-2007 otto

include cleanup and fix in comment; from Mark Lumsden


# 1.58 04-Apr-2007 otto

missing prototypes; from Mark Lumsden with a twist by me.


Revision tags: OPENBSD_4_1_BASE
# 1.57 04-Feb-2007 otto

long and long long is not the same on 32 bits archs


# 1.56 03-Jan-2007 otto

Add a 'g' command to only show processes having a string in their
command name. ok deraadt@


# 1.55 02-Jan-2007 otto

top(1) doesn't use any math.h function, so do not include math.h and
do not link with libm. ok ray@


# 1.54 29-Nov-2006 miod

Do not test for processes being swapped out since this can't happen anymore.


# 1.53 20-Sep-2006 ray

Recommit memory leak fix.


Revision tags: OPENBSD_4_0_BASE
# 1.52 29-Apr-2006 otto

use calloc(3) instead of malloc(3) to allocate cpu state structs; it's
better style here; plus it prevents bogus data to show up on first display.
report + ok pedro@


Revision tags: OPENBSD_3_9_BASE
# 1.51 21-Dec-2005 millert

Userland programs should include <errno.h> not <sys/errno.h>
OK deraadt@


# 1.50 04-Dec-2005 tedu

support for toggling thread viewing.
ok brad, hints from jmc


Revision tags: OPENBSD_3_8_BASE
# 1.49 17-Jun-2005 markus

add a 'C' command ) that toggles the display of the full cmdline;
with Jared Yanovich; ok deraadt


# 1.48 08-Jun-2005 millert

Multi-cpu support for top. Each cpu now gets its own stats line.
Based on work by todd@ at the hackathon.


Revision tags: OPENBSD_3_7_BASE
# 1.47 06-Dec-2004 markus

missing space; bluhm at genua.de


# 1.46 22-Nov-2004 pat

simplify loop iteration

ok otto millert


# 1.45 17-Nov-2004 markus

off-by-one in process_states[], fixes display of memory; ok deraadt, aaron


# 1.44 07-Oct-2004 otto

introduce -p option and p command to only show a single process.
From Patrick Latifi. ok deraadt@ millert@


Revision tags: OPENBSD_3_6_BASE
# 1.43 13-Jun-2004 otto

Typos in comments and a redundant prototype. From Brian Poole via jmc@
ok jmc@


# 1.42 11-Jun-2004 deraadt

handle the KI_NOCPU case, pointed out by markus


# 1.41 11-Jun-2004 deraadt

only print /# if > 1 cpu on a machine


# 1.40 11-Jun-2004 deraadt

export cpuid via kproc2, and make ps & top aware... from niklas


# 1.39 11-Jun-2004 deraadt

hide top breakage, until the next commit which will do it right


# 1.38 09-May-2004 deraadt

spaces


Revision tags: OPENBSD_3_5_BASE
# 1.37 08-Jan-2004 millert

Convert to kinfo_proc2; ok deraadt@


Revision tags: OPENBSD_3_4_BASE
# 1.36 07-Jul-2003 deraadt

protos


# 1.35 18-Jun-2003 deraadt

remove unused variables


# 1.34 17-Jun-2003 jfb

properly typecast to uid_t the return value of proc_owner() and report
ESRCH instead of EACCES if it returns -1

ok millert@


# 1.33 15-Jun-2003 millert

use uid_t and fix some sign compare warnings; OK krw@ and deraadt@


# 1.32 14-Jun-2003 avsm

use correct length instead of sizeof(char *) for some strl* bound values
millert@ ok


# 1.31 13-Jun-2003 deraadt

readable code


# 1.30 12-Jun-2003 deraadt

knf


# 1.29 12-Jun-2003 pvalchev

cleanup; ok deraadt


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.28 02-Jul-2002 tholo

Add missing copyright


Revision tags: OPENBSD_3_1_BASE
# 1.27 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.26 05-Dec-2001 art

Get maxslp with sysctl. Not the constant.


Revision tags: OPENBSD_3_0_BASE
# 1.25 12-Jul-2001 deraadt

first pass at a -Wall cleanup


# 1.24 03-Jun-2001 angelos

Use KERN_NPROCS to get the number of processes on the system, to
determine the amount of space we'll need to store the
information. The alternative, calling sysctl() with a NULL argument
for data, meant the kernel had to go through the process table.


Revision tags: OPENBSD_2_9_BASE
# 1.23 22-Feb-2001 deraadt

plug mem leak i introduced; spotted by millert


# 1.22 17-Feb-2001 deraadt

bye bye setgid kvm


# 1.21 17-Feb-2001 deraadt

do not use nlist anymore, kvm gets the bullet next


# 1.20 17-Feb-2001 deraadt

partial KNF, because i cannot read the diffs i am trying to debug


Revision tags: OPENBSD_2_8_BASE
# 1.19 18-Jun-2000 niklas

Mark kernel threads as system processes. Make ps hide them by default,
show them with -k. Do not try to show RSS based values for them as they
mess up column alignment. vmstat -f now shows kernel threads separately
from rforks too.


Revision tags: OPENBSD_2_7_BASE
# 1.18 14-Nov-1999 deraadt

stathz instead of hz, plus improvements by me; camield@inet.unisource.nl


# 1.17 29-Oct-1999 todd

writen


Revision tags: OPENBSD_2_6_BASE
# 1.16 21-Jun-1999 art

stop casting the third argument to kvm_read to char *. it is a void *.


# 1.15 22-May-1999 weingart

Update for new vmswap code.


Revision tags: OPENBSD_2_5_BASE
# 1.14 28-Nov-1998 kstailey

do not count swap spaces that were not swapon(2)ed


Revision tags: OPENBSD_2_4_BASE
# 1.13 20-Sep-1998 niklas

cp_time is long, deal with it.


# 1.12 20-Sep-1998 niklas

widen unsigned entities to signed ones before subtracting, fixes sorting on alpha


# 1.11 21-Aug-1998 kstailey

support "-o field" and the interactive "o" command


# 1.10 08-Jul-1998 deraadt

whack kmem gid after kvm_openfiles()


# 1.9 25-Jun-1998 deraadt

handle missing swap


Revision tags: OPENBSD_2_3_BASE
# 1.8 08-Nov-1997 millert

Sort idle processes by total CPU usage, not cpticks since cpticks
get zeroed every second; dan@dan.emsphone.com


Revision tags: OPENBSD_2_2_BASE
# 1.7 09-Sep-1997 millert

"last pid" stuff is useless with random pids.


# 1.6 09-Sep-1997 millert

Use kvm_openfiles() not the sunos compat kvm_open().
Use warn/warnx where it makes sense and check some more ret vals.


# 1.5 24-Aug-1997 millert

Fix compiler warnings.


# 1.4 22-Aug-1997 downsj

Two more alpha warnings.


# 1.3 22-Aug-1997 downsj

First sweep. Prototype, type fixes, long fixes, mostly compiles with
-Wall.


# 1.2 17-Aug-1997 kstailey

print "idle" for idle processes


# 1.1 14-Aug-1997 downsj

top 3.4, with a few changes. Still needs more work.


# 1.107 06-Jul-2020 kn

Use mnemonic KERN_PROC_ALL not literal zero

No object change.


# 1.106 26-Jun-2020 kn

Remove duplicate initialisation

Those are global variables are (zero) initialised as such already and
machine_init() is called only once upon startup.

OK mvs


# 1.105 25-Jun-2020 kn

Remove unused "remaining" member in struct handle

Only ever set or decremented since import with machine.c r1.1 (1997);

While here, simplify the skip semantics behind the scroll functionality.

OK millert


# 1.104 24-Jun-2020 kn

Remove obsolete comment about "extra nulls"

Those were removed in r1.39 in 2004.


# 1.103 23-Jun-2020 kn

Remove redundant NULL check

For the sake of simplicity and to reflect that the process list is always
sorted (default is "cpu"), even if not explicitly requested; this makes it
easier to argue about the code around sort order and its selection.

OK millert


Revision tags: OPENBSD_6_7_BASE
# 1.102 06-Jan-2020 zhuk

Implement scrolling in top(1) using 9 and 0 keys.

Planned improvements: scroll position displaying, using arrow/pgup/pgdown keys.

okay tedu@


# 1.101 16-Dec-2019 guenther

Delete tests for P_THREAD that predate the existence of
KERN_PROC_SHOW_THREADS and have been rendered superfluous by it.
Similarly, some P_SYSTEM tests can be deleted or pushed to the
kernel by using KERN_PROC_ALL instead of KERN_PROC_KTHREAD.

ok visa@ mpi@


Revision tags: OPENBSD_6_6_BASE
# 1.100 08-Oct-2019 kn

Simplify threads flag handling

Instead of always passing the threads flag which decides whether other
function arguments are used or not, make callers pass those arguments or
NULL depending on the threads flag.

This reflects better how thread IDs are shown in place of user names/IDs.

OK millert


# 1.99 06-Oct-2019 kn

Avoid gasting around get_process_info()

get_process_info() returns a pointer to the global handle later only be
used in format_next_process(); treat this struct handle as such without
casting the pointer to caddr_t and back again.

No object change.
OK millert deraadt


# 1.98 06-Oct-2019 kn

Zap intermediate structs in compare_*()

SETORDER() can assign from function arguments directly without additional
identical structs in each function.

No object change.
OK millert


# 1.97 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


Revision tags: OPENBSD_6_5_BASE
# 1.96 28-Nov-2018 kn

Allow reverse sort order

Prefixing the field with a dash will sort processes in reverse order.
`o -pid' will therefore list PID 1 on top.

"looks good" tedu, improvements and OK cheloha


# 1.95 17-Nov-2018 cheloha

Add new KERN_CPUSTATS sysctl(2) so we can identify offline CPUs.

Because of hw.smt we need a way to determine whether a given CPU is "online"
or "offline" from userspace. KERN_CPTIME2 is an array, and so cannot be
cleanly extended for this purpose, so add a new sysctl(2) KERN_CPUSTATS
with an extensible struct. At the moment it's just KERN_CPTIME2 with a
flags member, but it can grow as needed.

KERN_CPUSTATS appears to have been defined by BSDi long ago, but there are
few (if any) packages in the wild still using the symbol so breakage in ports
should be near zero. No other system inherited the symbol from BSDi, either.

Then, use the new sysctl(2) in systat(1) and top(1):

- systat(1) draws placeholder marks ('-') instead of percentages for
offline CPUs in the cpu view.

- systat(1) omits offline CPU ticks when drawing the "big bar" in
the vmstat view. The upshot is that the bar isn't half idle when
half your logical CPUs are disabled.

- top(1) does not draw lines for offline CPUs; if CPUs toggle on or
offline in interactive mode we redraw the display to expand/reduce
space for the new/missing CPUs. This is consistent with what some
top(1) implementations do on Linux.

- top(1) omits offline CPUs from the totals when CPU totals are
combined into a single line (the '-1' flag).

Originally prompted by deraadt@. Discussed endlessly with deraadt@,
ketennis@, and sthen@. Tested by jmc@ and jca@. Earlier versions also
discussed with jca@. Earlier versions tested by jmc@, tb@, and many
others.

docs ok jmc@, kernel bits ok ketennis@, everything ok sthen@,
"Is your stuff in yet?" deraadt@


Revision tags: OPENBSD_6_4_BASE
# 1.94 05-Oct-2018 cheloha

Revert KERN_CPTIME2 ENODEV changes in kernel and userspace.

ok kettenis deraadt


# 1.93 26-Sep-2018 cheloha

KERN_CPTIME2: set ENODEV if the CPU is offline.

This lets userspace distinguish between idle CPUs and those that are
not schedulable because hw.smt=0.

A subsequent commit probably needs to add documentation for this
to sysctl.2 (and perhaps elsewhere) after the dust settles.

Also included here are changes to systat(1) and top(1) that account
for the ENODEV case and adjust behavior accordingly:

- systat(1)'s cpu view prints placeholder marks ('-') instead of
percentages for each state if the given CPU is offline.

- systat(1)'s vmstat view checks for offline CPUs when computing the
machine state total and excludes them, so the CPU usage graph
only represents the states for online CPUs.

- top(1) does not draw CPU rows for offline CPUs when the view is
redrawn. If CPUs "go offline", percentages for each state are
replaced by placeholder marks ('-'); the view will need to be
redrawn to remove these rows. If CPUs "go online" the view will
need to be redrawn to show these new CPUs. In "combined CPU" mode,
the count and the state totals only represent online CPUs.

Ports using KERN_CPTIME2 will need to be updated. The changes
described above to make systat(1) and top(1) aware of the ENODEV
case *and* gracefully handle a changing HW_NCPUONLINE while the
application is running are not necessarily appropriate for each
and every port.

The changes described above are so extensive in part to demonstrate
one way a program *might* be made robust to changing CPU availability.
In particular, changing hw.smt after boot is an extremely rare event,
and this needs to be weighed when updating ports.

The logic needed to account for the KERN_CPTIME2 ENODEV case is
very roughly:

if (sysctl(...) == -1) {
if (errno != ENODEV) {
/* Actual error occurred. */
} else {
/* CPU is offline. */
}
} else {
/* CPU is online and CPU states were set by sysctl(2). */
}

Prompted by deraadt@. Basic idea for ENODEV from kettenis@. Discussed at
length with kettenis@. Additional testing by tb@.

No complaints from hackers@ after a week.

ok kettenis@, "I think you should commit [now]" deraadt@


# 1.92 22-Sep-2018 millert

Use user_from_uid() and uid_from_user() directly. The wrappers
in username.c are now so simple there is no longer a good reason
to use them. OK deraadt@


# 1.91 13-Sep-2018 millert

Fix warnings caused by user_from_uid() and group_from_gid() now
returning const char *.


# 1.90 14-May-2018 mpi

Stopping counting and reporting CPU time spent spinning on a lock as
system time.

Introduce a new CP_SPIN "scheduler state" and modify userland tools
to display the % of timer a CPU spents spinning.

Based on a diff from jmatthew@, ok pirofti@, bluhm@, visa@, deraadt@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.89 30-May-2017 tedu

avoid some shadow warnings. from Brian Callahan


Revision tags: OPENBSD_6_1_BASE
# 1.88 15-Mar-2017 deraadt

annoying whitespace die die die


# 1.87 28-Jul-2016 tedu

rework realloc loop. there's no need to shrink the allocation between
calls. if we need a big space once, we'll likely need a big space again.


Revision tags: OPENBSD_6_0_BASE
# 1.86 11-May-2016 edd

Allow top(1) to filter process args if they are being displayed.

OK tedu@, deraadt@, and with input from Michal Mazurek.

Thanks


Revision tags: OPENBSD_5_9_BASE
# 1.85 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_8_BASE
# 1.84 06-May-2015 mpi

Display thread IDs instead of the name of the process's owner when "-H"
is used.

The rationnal is that when you're looking at threads you're generally
already filtereing by PID and this allow you to see which thread is a
pig.

Written some time ago with mikeb@

ok sthen@, krw@, guenther@


Revision tags: OPENBSD_5_7_BASE
# 1.83 19-Jan-2015 millert

Kill loadavg.h and just use the fscale value directly.
There's no need for crazy casts of p_pctcpu in ORDERKEY_PCTCPU
since kinfo_proc exports it as uint32_t. OK deraadt@


# 1.82 19-Jan-2015 deraadt

like in ps(1), fetch the FSCALE value using sysctl rather than using
the header version
ok guenther


# 1.81 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.80 17-Sep-2014 dlg

if there are more than 8 cpus, default to combined cpu stats (like
you passed -1 on the command line).

ok kettenis@ tedu@


# 1.79 15-Sep-2014 miod

Remove non-standard <sys/dkstat.h> header. It has not contained anything
related to disk stastics for almost 17 years, and the remaining
userland-visible defines duplicate those found in <sys/sched.h>.

Move the remaining _KERNEL defines to <sys/tty.h> where they belong, and
update all users to cope with this.

ok kettenis@


Revision tags: OPENBSD_5_6_BASE
# 1.78 04-Jul-2014 guenther

Track whether a process is a zombie or not yet fully built via flags
PS_{ZOMBIE,EMBRYO} on the process instead of peeking into the process's
thread data. This eliminates the need for the thread-level SDEAD state.

Change kvm_getprocs() (both the sysctl() and kvm backends) to report the
"most active" scheduler state for the process's threads.

tweaks kettenis@
feedback and ok matthew@


# 1.77 08-Apr-2014 mpi

Use VM_UVMEXP instead of VM_METER for memory usages and directly
include <sys/vmmeter.h> where it is needed instead of relying on
it being included by <uvm/uvm_extern.h>.

miod@ likes it, ok guenther@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.76 23-Mar-2013 tedu

wchan is no longer exposed, just check if wmesg is not empty.
(wmesg hasn't itself been a pointer since conversion to kinfo_proc.)
noticed by sthen.
ok deraadt jsing millert sthen


Revision tags: OPENBSD_5_3_BASE
# 1.75 18-Dec-2012 millert

We no longer use struct eproc for kinfo_proc in sysctl.h so there
is no direct need for sys/proc.h or sys/resource.h. Some consumers
of kinfo_proc need these for the proc flags and rlimit defines like
RLIM_INF so add the appropriate includes to them.
OK deraadt@ sthen@


Revision tags: OPENBSD_5_2_BASE
# 1.74 09-Jul-2012 deraadt

According to miod, KERNBASE used as a userland symbol should die
in a fire. Start by removing the definitions, before we clean the
headers later.


# 1.73 05-Jun-2012 brynet

Add support for hiding a user's processes in top.

feedback & ok lum@


# 1.72 21-Apr-2012 guenther

Remove a check made superfluous by the KERN_PROC_SHOW_THREADS flag.
This lets new binaries work (at least w/o the -H option) with old kernels.

ok pirofti@ deraadt@


# 1.71 17-Apr-2012 pirofti

Make it optional for kvm_getprocs() (and related sysctl) to return
thread information.

Add a KERN_PROC_SHOW_THREADS flag that has to be set in order to get the
thread info and make it off by default. This creates backwards compatibility
for applications that relied on a given size/number of items to be returned.

Modify ps(1) and top(1) accordingly.

Okay guenther@.


# 1.70 12-Apr-2012 pirofti

Add per thread accounting, mainly for usage & friends.

This expands the already bloated FILL_KPROC macro to take an extra
parameter that indicates if the callee is a thread or a process.

The userland bits are adjusted accordingly and ps(1) and top(1) now
display per thread usage times when -H is used.

Also pkill(1) had to be adjusted so that duplicates don't pop up.

libkvm does basically the same thing as the kernel bits.

Okay guenther@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.69 12-Jul-2011 tedu

print out the amount of memory used for cache. ok otto


# 1.68 10-Apr-2011 guenther

Switch back from KERN_PROC2/kinfo_proc2 to KERN_PROC/kinfo_proc now
that we've got name we want for the API we want

"ZAP!" deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.67 26-Apr-2010 deraadt

more portable code


# 1.66 26-Mar-2010 lum

Make the source of process TIME for displaying and sorting the same.
top(1) and ps(1) now display the same info.
ok tedu@ millert@ otto@ and tested on sparc64 by landry@


Revision tags: OPENBSD_4_7_BASE
# 1.65 29-Jan-2010 tedu

Allow sorting by command and pid. Also, allow partial matches with strncmp.
ok jmc otto


Revision tags: OPENBSD_4_6_BASE
# 1.64 28-Apr-2009 deraadt

extend STATE column to fit > 9 cpus; Aivar Jaakson; PR6131


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.63 01-Nov-2007 otto

bump arbitrary limit on arg length shown by -C; requested by and ok deraadt@


Revision tags: OPENBSD_4_2_BASE
# 1.62 27-Jul-2007 deraadt

replace a few more malloc(n*m) idioms with calloc(n,m) for safety;
inspired by zinovik@cs.karelia.ru


# 1.61 29-May-2007 otto

Instead of using hand-crafted redraws minimizing screen updates, use curses.
Enables nice things like process highlighting without hurting the brain.
ok deraadt@


# 1.60 16-Apr-2007 otto

don't truncate wchan, ok miod@


# 1.59 13-Apr-2007 otto

include cleanup and fix in comment; from Mark Lumsden


# 1.58 04-Apr-2007 otto

missing prototypes; from Mark Lumsden with a twist by me.


Revision tags: OPENBSD_4_1_BASE
# 1.57 04-Feb-2007 otto

long and long long is not the same on 32 bits archs


# 1.56 03-Jan-2007 otto

Add a 'g' command to only show processes having a string in their
command name. ok deraadt@


# 1.55 02-Jan-2007 otto

top(1) doesn't use any math.h function, so do not include math.h and
do not link with libm. ok ray@


# 1.54 29-Nov-2006 miod

Do not test for processes being swapped out since this can't happen anymore.


# 1.53 20-Sep-2006 ray

Recommit memory leak fix.


Revision tags: OPENBSD_4_0_BASE
# 1.52 29-Apr-2006 otto

use calloc(3) instead of malloc(3) to allocate cpu state structs; it's
better style here; plus it prevents bogus data to show up on first display.
report + ok pedro@


Revision tags: OPENBSD_3_9_BASE
# 1.51 21-Dec-2005 millert

Userland programs should include <errno.h> not <sys/errno.h>
OK deraadt@


# 1.50 04-Dec-2005 tedu

support for toggling thread viewing.
ok brad, hints from jmc


Revision tags: OPENBSD_3_8_BASE
# 1.49 17-Jun-2005 markus

add a 'C' command ) that toggles the display of the full cmdline;
with Jared Yanovich; ok deraadt


# 1.48 08-Jun-2005 millert

Multi-cpu support for top. Each cpu now gets its own stats line.
Based on work by todd@ at the hackathon.


Revision tags: OPENBSD_3_7_BASE
# 1.47 06-Dec-2004 markus

missing space; bluhm at genua.de


# 1.46 22-Nov-2004 pat

simplify loop iteration

ok otto millert


# 1.45 17-Nov-2004 markus

off-by-one in process_states[], fixes display of memory; ok deraadt, aaron


# 1.44 07-Oct-2004 otto

introduce -p option and p command to only show a single process.
From Patrick Latifi. ok deraadt@ millert@


Revision tags: OPENBSD_3_6_BASE
# 1.43 13-Jun-2004 otto

Typos in comments and a redundant prototype. From Brian Poole via jmc@
ok jmc@


# 1.42 11-Jun-2004 deraadt

handle the KI_NOCPU case, pointed out by markus


# 1.41 11-Jun-2004 deraadt

only print /# if > 1 cpu on a machine


# 1.40 11-Jun-2004 deraadt

export cpuid via kproc2, and make ps & top aware... from niklas


# 1.39 11-Jun-2004 deraadt

hide top breakage, until the next commit which will do it right


# 1.38 09-May-2004 deraadt

spaces


Revision tags: OPENBSD_3_5_BASE
# 1.37 08-Jan-2004 millert

Convert to kinfo_proc2; ok deraadt@


Revision tags: OPENBSD_3_4_BASE
# 1.36 07-Jul-2003 deraadt

protos


# 1.35 18-Jun-2003 deraadt

remove unused variables


# 1.34 17-Jun-2003 jfb

properly typecast to uid_t the return value of proc_owner() and report
ESRCH instead of EACCES if it returns -1

ok millert@


# 1.33 15-Jun-2003 millert

use uid_t and fix some sign compare warnings; OK krw@ and deraadt@


# 1.32 14-Jun-2003 avsm

use correct length instead of sizeof(char *) for some strl* bound values
millert@ ok


# 1.31 13-Jun-2003 deraadt

readable code


# 1.30 12-Jun-2003 deraadt

knf


# 1.29 12-Jun-2003 pvalchev

cleanup; ok deraadt


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.28 02-Jul-2002 tholo

Add missing copyright


Revision tags: OPENBSD_3_1_BASE
# 1.27 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.26 05-Dec-2001 art

Get maxslp with sysctl. Not the constant.


Revision tags: OPENBSD_3_0_BASE
# 1.25 12-Jul-2001 deraadt

first pass at a -Wall cleanup


# 1.24 03-Jun-2001 angelos

Use KERN_NPROCS to get the number of processes on the system, to
determine the amount of space we'll need to store the
information. The alternative, calling sysctl() with a NULL argument
for data, meant the kernel had to go through the process table.


Revision tags: OPENBSD_2_9_BASE
# 1.23 22-Feb-2001 deraadt

plug mem leak i introduced; spotted by millert


# 1.22 17-Feb-2001 deraadt

bye bye setgid kvm


# 1.21 17-Feb-2001 deraadt

do not use nlist anymore, kvm gets the bullet next


# 1.20 17-Feb-2001 deraadt

partial KNF, because i cannot read the diffs i am trying to debug


Revision tags: OPENBSD_2_8_BASE
# 1.19 18-Jun-2000 niklas

Mark kernel threads as system processes. Make ps hide them by default,
show them with -k. Do not try to show RSS based values for them as they
mess up column alignment. vmstat -f now shows kernel threads separately
from rforks too.


Revision tags: OPENBSD_2_7_BASE
# 1.18 14-Nov-1999 deraadt

stathz instead of hz, plus improvements by me; camield@inet.unisource.nl


# 1.17 29-Oct-1999 todd

writen


Revision tags: OPENBSD_2_6_BASE
# 1.16 21-Jun-1999 art

stop casting the third argument to kvm_read to char *. it is a void *.


# 1.15 22-May-1999 weingart

Update for new vmswap code.


Revision tags: OPENBSD_2_5_BASE
# 1.14 28-Nov-1998 kstailey

do not count swap spaces that were not swapon(2)ed


Revision tags: OPENBSD_2_4_BASE
# 1.13 20-Sep-1998 niklas

cp_time is long, deal with it.


# 1.12 20-Sep-1998 niklas

widen unsigned entities to signed ones before subtracting, fixes sorting on alpha


# 1.11 21-Aug-1998 kstailey

support "-o field" and the interactive "o" command


# 1.10 08-Jul-1998 deraadt

whack kmem gid after kvm_openfiles()


# 1.9 25-Jun-1998 deraadt

handle missing swap


Revision tags: OPENBSD_2_3_BASE
# 1.8 08-Nov-1997 millert

Sort idle processes by total CPU usage, not cpticks since cpticks
get zeroed every second; dan@dan.emsphone.com


Revision tags: OPENBSD_2_2_BASE
# 1.7 09-Sep-1997 millert

"last pid" stuff is useless with random pids.


# 1.6 09-Sep-1997 millert

Use kvm_openfiles() not the sunos compat kvm_open().
Use warn/warnx where it makes sense and check some more ret vals.


# 1.5 24-Aug-1997 millert

Fix compiler warnings.


# 1.4 22-Aug-1997 downsj

Two more alpha warnings.


# 1.3 22-Aug-1997 downsj

First sweep. Prototype, type fixes, long fixes, mostly compiles with
-Wall.


# 1.2 17-Aug-1997 kstailey

print "idle" for idle processes


# 1.1 14-Aug-1997 downsj

top 3.4, with a few changes. Still needs more work.


# 1.106 26-Jun-2020 kn

Remove duplicate initialisation

Those are global variables are (zero) initialised as such already and
machine_init() is called only once upon startup.

OK mvs


# 1.105 25-Jun-2020 kn

Remove unused "remaining" member in struct handle

Only ever set or decremented since import with machine.c r1.1 (1997);

While here, simplify the skip semantics behind the scroll functionality.

OK millert


# 1.104 24-Jun-2020 kn

Remove obsolete comment about "extra nulls"

Those were removed in r1.39 in 2004.


# 1.103 23-Jun-2020 kn

Remove redundant NULL check

For the sake of simplicity and to reflect that the process list is always
sorted (default is "cpu"), even if not explicitly requested; this makes it
easier to argue about the code around sort order and its selection.

OK millert


Revision tags: OPENBSD_6_7_BASE
# 1.102 06-Jan-2020 zhuk

Implement scrolling in top(1) using 9 and 0 keys.

Planned improvements: scroll position displaying, using arrow/pgup/pgdown keys.

okay tedu@


# 1.101 16-Dec-2019 guenther

Delete tests for P_THREAD that predate the existence of
KERN_PROC_SHOW_THREADS and have been rendered superfluous by it.
Similarly, some P_SYSTEM tests can be deleted or pushed to the
kernel by using KERN_PROC_ALL instead of KERN_PROC_KTHREAD.

ok visa@ mpi@


Revision tags: OPENBSD_6_6_BASE
# 1.100 08-Oct-2019 kn

Simplify threads flag handling

Instead of always passing the threads flag which decides whether other
function arguments are used or not, make callers pass those arguments or
NULL depending on the threads flag.

This reflects better how thread IDs are shown in place of user names/IDs.

OK millert


# 1.99 06-Oct-2019 kn

Avoid gasting around get_process_info()

get_process_info() returns a pointer to the global handle later only be
used in format_next_process(); treat this struct handle as such without
casting the pointer to caddr_t and back again.

No object change.
OK millert deraadt


# 1.98 06-Oct-2019 kn

Zap intermediate structs in compare_*()

SETORDER() can assign from function arguments directly without additional
identical structs in each function.

No object change.
OK millert


# 1.97 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


Revision tags: OPENBSD_6_5_BASE
# 1.96 28-Nov-2018 kn

Allow reverse sort order

Prefixing the field with a dash will sort processes in reverse order.
`o -pid' will therefore list PID 1 on top.

"looks good" tedu, improvements and OK cheloha


# 1.95 17-Nov-2018 cheloha

Add new KERN_CPUSTATS sysctl(2) so we can identify offline CPUs.

Because of hw.smt we need a way to determine whether a given CPU is "online"
or "offline" from userspace. KERN_CPTIME2 is an array, and so cannot be
cleanly extended for this purpose, so add a new sysctl(2) KERN_CPUSTATS
with an extensible struct. At the moment it's just KERN_CPTIME2 with a
flags member, but it can grow as needed.

KERN_CPUSTATS appears to have been defined by BSDi long ago, but there are
few (if any) packages in the wild still using the symbol so breakage in ports
should be near zero. No other system inherited the symbol from BSDi, either.

Then, use the new sysctl(2) in systat(1) and top(1):

- systat(1) draws placeholder marks ('-') instead of percentages for
offline CPUs in the cpu view.

- systat(1) omits offline CPU ticks when drawing the "big bar" in
the vmstat view. The upshot is that the bar isn't half idle when
half your logical CPUs are disabled.

- top(1) does not draw lines for offline CPUs; if CPUs toggle on or
offline in interactive mode we redraw the display to expand/reduce
space for the new/missing CPUs. This is consistent with what some
top(1) implementations do on Linux.

- top(1) omits offline CPUs from the totals when CPU totals are
combined into a single line (the '-1' flag).

Originally prompted by deraadt@. Discussed endlessly with deraadt@,
ketennis@, and sthen@. Tested by jmc@ and jca@. Earlier versions also
discussed with jca@. Earlier versions tested by jmc@, tb@, and many
others.

docs ok jmc@, kernel bits ok ketennis@, everything ok sthen@,
"Is your stuff in yet?" deraadt@


Revision tags: OPENBSD_6_4_BASE
# 1.94 05-Oct-2018 cheloha

Revert KERN_CPTIME2 ENODEV changes in kernel and userspace.

ok kettenis deraadt


# 1.93 26-Sep-2018 cheloha

KERN_CPTIME2: set ENODEV if the CPU is offline.

This lets userspace distinguish between idle CPUs and those that are
not schedulable because hw.smt=0.

A subsequent commit probably needs to add documentation for this
to sysctl.2 (and perhaps elsewhere) after the dust settles.

Also included here are changes to systat(1) and top(1) that account
for the ENODEV case and adjust behavior accordingly:

- systat(1)'s cpu view prints placeholder marks ('-') instead of
percentages for each state if the given CPU is offline.

- systat(1)'s vmstat view checks for offline CPUs when computing the
machine state total and excludes them, so the CPU usage graph
only represents the states for online CPUs.

- top(1) does not draw CPU rows for offline CPUs when the view is
redrawn. If CPUs "go offline", percentages for each state are
replaced by placeholder marks ('-'); the view will need to be
redrawn to remove these rows. If CPUs "go online" the view will
need to be redrawn to show these new CPUs. In "combined CPU" mode,
the count and the state totals only represent online CPUs.

Ports using KERN_CPTIME2 will need to be updated. The changes
described above to make systat(1) and top(1) aware of the ENODEV
case *and* gracefully handle a changing HW_NCPUONLINE while the
application is running are not necessarily appropriate for each
and every port.

The changes described above are so extensive in part to demonstrate
one way a program *might* be made robust to changing CPU availability.
In particular, changing hw.smt after boot is an extremely rare event,
and this needs to be weighed when updating ports.

The logic needed to account for the KERN_CPTIME2 ENODEV case is
very roughly:

if (sysctl(...) == -1) {
if (errno != ENODEV) {
/* Actual error occurred. */
} else {
/* CPU is offline. */
}
} else {
/* CPU is online and CPU states were set by sysctl(2). */
}

Prompted by deraadt@. Basic idea for ENODEV from kettenis@. Discussed at
length with kettenis@. Additional testing by tb@.

No complaints from hackers@ after a week.

ok kettenis@, "I think you should commit [now]" deraadt@


# 1.92 22-Sep-2018 millert

Use user_from_uid() and uid_from_user() directly. The wrappers
in username.c are now so simple there is no longer a good reason
to use them. OK deraadt@


# 1.91 13-Sep-2018 millert

Fix warnings caused by user_from_uid() and group_from_gid() now
returning const char *.


# 1.90 14-May-2018 mpi

Stopping counting and reporting CPU time spent spinning on a lock as
system time.

Introduce a new CP_SPIN "scheduler state" and modify userland tools
to display the % of timer a CPU spents spinning.

Based on a diff from jmatthew@, ok pirofti@, bluhm@, visa@, deraadt@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.89 30-May-2017 tedu

avoid some shadow warnings. from Brian Callahan


Revision tags: OPENBSD_6_1_BASE
# 1.88 15-Mar-2017 deraadt

annoying whitespace die die die


# 1.87 28-Jul-2016 tedu

rework realloc loop. there's no need to shrink the allocation between
calls. if we need a big space once, we'll likely need a big space again.


Revision tags: OPENBSD_6_0_BASE
# 1.86 11-May-2016 edd

Allow top(1) to filter process args if they are being displayed.

OK tedu@, deraadt@, and with input from Michal Mazurek.

Thanks


Revision tags: OPENBSD_5_9_BASE
# 1.85 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_8_BASE
# 1.84 06-May-2015 mpi

Display thread IDs instead of the name of the process's owner when "-H"
is used.

The rationnal is that when you're looking at threads you're generally
already filtereing by PID and this allow you to see which thread is a
pig.

Written some time ago with mikeb@

ok sthen@, krw@, guenther@


Revision tags: OPENBSD_5_7_BASE
# 1.83 19-Jan-2015 millert

Kill loadavg.h and just use the fscale value directly.
There's no need for crazy casts of p_pctcpu in ORDERKEY_PCTCPU
since kinfo_proc exports it as uint32_t. OK deraadt@


# 1.82 19-Jan-2015 deraadt

like in ps(1), fetch the FSCALE value using sysctl rather than using
the header version
ok guenther


# 1.81 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.80 17-Sep-2014 dlg

if there are more than 8 cpus, default to combined cpu stats (like
you passed -1 on the command line).

ok kettenis@ tedu@


# 1.79 15-Sep-2014 miod

Remove non-standard <sys/dkstat.h> header. It has not contained anything
related to disk stastics for almost 17 years, and the remaining
userland-visible defines duplicate those found in <sys/sched.h>.

Move the remaining _KERNEL defines to <sys/tty.h> where they belong, and
update all users to cope with this.

ok kettenis@


Revision tags: OPENBSD_5_6_BASE
# 1.78 04-Jul-2014 guenther

Track whether a process is a zombie or not yet fully built via flags
PS_{ZOMBIE,EMBRYO} on the process instead of peeking into the process's
thread data. This eliminates the need for the thread-level SDEAD state.

Change kvm_getprocs() (both the sysctl() and kvm backends) to report the
"most active" scheduler state for the process's threads.

tweaks kettenis@
feedback and ok matthew@


# 1.77 08-Apr-2014 mpi

Use VM_UVMEXP instead of VM_METER for memory usages and directly
include <sys/vmmeter.h> where it is needed instead of relying on
it being included by <uvm/uvm_extern.h>.

miod@ likes it, ok guenther@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.76 23-Mar-2013 tedu

wchan is no longer exposed, just check if wmesg is not empty.
(wmesg hasn't itself been a pointer since conversion to kinfo_proc.)
noticed by sthen.
ok deraadt jsing millert sthen


Revision tags: OPENBSD_5_3_BASE
# 1.75 18-Dec-2012 millert

We no longer use struct eproc for kinfo_proc in sysctl.h so there
is no direct need for sys/proc.h or sys/resource.h. Some consumers
of kinfo_proc need these for the proc flags and rlimit defines like
RLIM_INF so add the appropriate includes to them.
OK deraadt@ sthen@


Revision tags: OPENBSD_5_2_BASE
# 1.74 09-Jul-2012 deraadt

According to miod, KERNBASE used as a userland symbol should die
in a fire. Start by removing the definitions, before we clean the
headers later.


# 1.73 05-Jun-2012 brynet

Add support for hiding a user's processes in top.

feedback & ok lum@


# 1.72 21-Apr-2012 guenther

Remove a check made superfluous by the KERN_PROC_SHOW_THREADS flag.
This lets new binaries work (at least w/o the -H option) with old kernels.

ok pirofti@ deraadt@


# 1.71 17-Apr-2012 pirofti

Make it optional for kvm_getprocs() (and related sysctl) to return
thread information.

Add a KERN_PROC_SHOW_THREADS flag that has to be set in order to get the
thread info and make it off by default. This creates backwards compatibility
for applications that relied on a given size/number of items to be returned.

Modify ps(1) and top(1) accordingly.

Okay guenther@.


# 1.70 12-Apr-2012 pirofti

Add per thread accounting, mainly for usage & friends.

This expands the already bloated FILL_KPROC macro to take an extra
parameter that indicates if the callee is a thread or a process.

The userland bits are adjusted accordingly and ps(1) and top(1) now
display per thread usage times when -H is used.

Also pkill(1) had to be adjusted so that duplicates don't pop up.

libkvm does basically the same thing as the kernel bits.

Okay guenther@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.69 12-Jul-2011 tedu

print out the amount of memory used for cache. ok otto


# 1.68 10-Apr-2011 guenther

Switch back from KERN_PROC2/kinfo_proc2 to KERN_PROC/kinfo_proc now
that we've got name we want for the API we want

"ZAP!" deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.67 26-Apr-2010 deraadt

more portable code


# 1.66 26-Mar-2010 lum

Make the source of process TIME for displaying and sorting the same.
top(1) and ps(1) now display the same info.
ok tedu@ millert@ otto@ and tested on sparc64 by landry@


Revision tags: OPENBSD_4_7_BASE
# 1.65 29-Jan-2010 tedu

Allow sorting by command and pid. Also, allow partial matches with strncmp.
ok jmc otto


Revision tags: OPENBSD_4_6_BASE
# 1.64 28-Apr-2009 deraadt

extend STATE column to fit > 9 cpus; Aivar Jaakson; PR6131


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.63 01-Nov-2007 otto

bump arbitrary limit on arg length shown by -C; requested by and ok deraadt@


Revision tags: OPENBSD_4_2_BASE
# 1.62 27-Jul-2007 deraadt

replace a few more malloc(n*m) idioms with calloc(n,m) for safety;
inspired by zinovik@cs.karelia.ru


# 1.61 29-May-2007 otto

Instead of using hand-crafted redraws minimizing screen updates, use curses.
Enables nice things like process highlighting without hurting the brain.
ok deraadt@


# 1.60 16-Apr-2007 otto

don't truncate wchan, ok miod@


# 1.59 13-Apr-2007 otto

include cleanup and fix in comment; from Mark Lumsden


# 1.58 04-Apr-2007 otto

missing prototypes; from Mark Lumsden with a twist by me.


Revision tags: OPENBSD_4_1_BASE
# 1.57 04-Feb-2007 otto

long and long long is not the same on 32 bits archs


# 1.56 03-Jan-2007 otto

Add a 'g' command to only show processes having a string in their
command name. ok deraadt@


# 1.55 02-Jan-2007 otto

top(1) doesn't use any math.h function, so do not include math.h and
do not link with libm. ok ray@


# 1.54 29-Nov-2006 miod

Do not test for processes being swapped out since this can't happen anymore.


# 1.53 20-Sep-2006 ray

Recommit memory leak fix.


Revision tags: OPENBSD_4_0_BASE
# 1.52 29-Apr-2006 otto

use calloc(3) instead of malloc(3) to allocate cpu state structs; it's
better style here; plus it prevents bogus data to show up on first display.
report + ok pedro@


Revision tags: OPENBSD_3_9_BASE
# 1.51 21-Dec-2005 millert

Userland programs should include <errno.h> not <sys/errno.h>
OK deraadt@


# 1.50 04-Dec-2005 tedu

support for toggling thread viewing.
ok brad, hints from jmc


Revision tags: OPENBSD_3_8_BASE
# 1.49 17-Jun-2005 markus

add a 'C' command ) that toggles the display of the full cmdline;
with Jared Yanovich; ok deraadt


# 1.48 08-Jun-2005 millert

Multi-cpu support for top. Each cpu now gets its own stats line.
Based on work by todd@ at the hackathon.


Revision tags: OPENBSD_3_7_BASE
# 1.47 06-Dec-2004 markus

missing space; bluhm at genua.de


# 1.46 22-Nov-2004 pat

simplify loop iteration

ok otto millert


# 1.45 17-Nov-2004 markus

off-by-one in process_states[], fixes display of memory; ok deraadt, aaron


# 1.44 07-Oct-2004 otto

introduce -p option and p command to only show a single process.
From Patrick Latifi. ok deraadt@ millert@


Revision tags: OPENBSD_3_6_BASE
# 1.43 13-Jun-2004 otto

Typos in comments and a redundant prototype. From Brian Poole via jmc@
ok jmc@


# 1.42 11-Jun-2004 deraadt

handle the KI_NOCPU case, pointed out by markus


# 1.41 11-Jun-2004 deraadt

only print /# if > 1 cpu on a machine


# 1.40 11-Jun-2004 deraadt

export cpuid via kproc2, and make ps & top aware... from niklas


# 1.39 11-Jun-2004 deraadt

hide top breakage, until the next commit which will do it right


# 1.38 09-May-2004 deraadt

spaces


Revision tags: OPENBSD_3_5_BASE
# 1.37 08-Jan-2004 millert

Convert to kinfo_proc2; ok deraadt@


Revision tags: OPENBSD_3_4_BASE
# 1.36 07-Jul-2003 deraadt

protos


# 1.35 18-Jun-2003 deraadt

remove unused variables


# 1.34 17-Jun-2003 jfb

properly typecast to uid_t the return value of proc_owner() and report
ESRCH instead of EACCES if it returns -1

ok millert@


# 1.33 15-Jun-2003 millert

use uid_t and fix some sign compare warnings; OK krw@ and deraadt@


# 1.32 14-Jun-2003 avsm

use correct length instead of sizeof(char *) for some strl* bound values
millert@ ok


# 1.31 13-Jun-2003 deraadt

readable code


# 1.30 12-Jun-2003 deraadt

knf


# 1.29 12-Jun-2003 pvalchev

cleanup; ok deraadt


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.28 02-Jul-2002 tholo

Add missing copyright


Revision tags: OPENBSD_3_1_BASE
# 1.27 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.26 05-Dec-2001 art

Get maxslp with sysctl. Not the constant.


Revision tags: OPENBSD_3_0_BASE
# 1.25 12-Jul-2001 deraadt

first pass at a -Wall cleanup


# 1.24 03-Jun-2001 angelos

Use KERN_NPROCS to get the number of processes on the system, to
determine the amount of space we'll need to store the
information. The alternative, calling sysctl() with a NULL argument
for data, meant the kernel had to go through the process table.


Revision tags: OPENBSD_2_9_BASE
# 1.23 22-Feb-2001 deraadt

plug mem leak i introduced; spotted by millert


# 1.22 17-Feb-2001 deraadt

bye bye setgid kvm


# 1.21 17-Feb-2001 deraadt

do not use nlist anymore, kvm gets the bullet next


# 1.20 17-Feb-2001 deraadt

partial KNF, because i cannot read the diffs i am trying to debug


Revision tags: OPENBSD_2_8_BASE
# 1.19 18-Jun-2000 niklas

Mark kernel threads as system processes. Make ps hide them by default,
show them with -k. Do not try to show RSS based values for them as they
mess up column alignment. vmstat -f now shows kernel threads separately
from rforks too.


Revision tags: OPENBSD_2_7_BASE
# 1.18 14-Nov-1999 deraadt

stathz instead of hz, plus improvements by me; camield@inet.unisource.nl


# 1.17 29-Oct-1999 todd

writen


Revision tags: OPENBSD_2_6_BASE
# 1.16 21-Jun-1999 art

stop casting the third argument to kvm_read to char *. it is a void *.


# 1.15 22-May-1999 weingart

Update for new vmswap code.


Revision tags: OPENBSD_2_5_BASE
# 1.14 28-Nov-1998 kstailey

do not count swap spaces that were not swapon(2)ed


Revision tags: OPENBSD_2_4_BASE
# 1.13 20-Sep-1998 niklas

cp_time is long, deal with it.


# 1.12 20-Sep-1998 niklas

widen unsigned entities to signed ones before subtracting, fixes sorting on alpha


# 1.11 21-Aug-1998 kstailey

support "-o field" and the interactive "o" command


# 1.10 08-Jul-1998 deraadt

whack kmem gid after kvm_openfiles()


# 1.9 25-Jun-1998 deraadt

handle missing swap


Revision tags: OPENBSD_2_3_BASE
# 1.8 08-Nov-1997 millert

Sort idle processes by total CPU usage, not cpticks since cpticks
get zeroed every second; dan@dan.emsphone.com


Revision tags: OPENBSD_2_2_BASE
# 1.7 09-Sep-1997 millert

"last pid" stuff is useless with random pids.


# 1.6 09-Sep-1997 millert

Use kvm_openfiles() not the sunos compat kvm_open().
Use warn/warnx where it makes sense and check some more ret vals.


# 1.5 24-Aug-1997 millert

Fix compiler warnings.


# 1.4 22-Aug-1997 downsj

Two more alpha warnings.


# 1.3 22-Aug-1997 downsj

First sweep. Prototype, type fixes, long fixes, mostly compiles with
-Wall.


# 1.2 17-Aug-1997 kstailey

print "idle" for idle processes


# 1.1 14-Aug-1997 downsj

top 3.4, with a few changes. Still needs more work.


# 1.105 25-Jun-2020 kn

Remove unused "remaining" member in struct handle

Only ever set or decremented since import with machine.c r1.1 (1997);

While here, simplify the skip semantics behind the scroll functionality.

OK millert


# 1.104 24-Jun-2020 kn

Remove obsolete comment about "extra nulls"

Those were removed in r1.39 in 2004.


# 1.103 23-Jun-2020 kn

Remove redundant NULL check

For the sake of simplicity and to reflect that the process list is always
sorted (default is "cpu"), even if not explicitly requested; this makes it
easier to argue about the code around sort order and its selection.

OK millert


Revision tags: OPENBSD_6_7_BASE
# 1.102 06-Jan-2020 zhuk

Implement scrolling in top(1) using 9 and 0 keys.

Planned improvements: scroll position displaying, using arrow/pgup/pgdown keys.

okay tedu@


# 1.101 16-Dec-2019 guenther

Delete tests for P_THREAD that predate the existence of
KERN_PROC_SHOW_THREADS and have been rendered superfluous by it.
Similarly, some P_SYSTEM tests can be deleted or pushed to the
kernel by using KERN_PROC_ALL instead of KERN_PROC_KTHREAD.

ok visa@ mpi@


Revision tags: OPENBSD_6_6_BASE
# 1.100 08-Oct-2019 kn

Simplify threads flag handling

Instead of always passing the threads flag which decides whether other
function arguments are used or not, make callers pass those arguments or
NULL depending on the threads flag.

This reflects better how thread IDs are shown in place of user names/IDs.

OK millert


# 1.99 06-Oct-2019 kn

Avoid gasting around get_process_info()

get_process_info() returns a pointer to the global handle later only be
used in format_next_process(); treat this struct handle as such without
casting the pointer to caddr_t and back again.

No object change.
OK millert deraadt


# 1.98 06-Oct-2019 kn

Zap intermediate structs in compare_*()

SETORDER() can assign from function arguments directly without additional
identical structs in each function.

No object change.
OK millert


# 1.97 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


Revision tags: OPENBSD_6_5_BASE
# 1.96 28-Nov-2018 kn

Allow reverse sort order

Prefixing the field with a dash will sort processes in reverse order.
`o -pid' will therefore list PID 1 on top.

"looks good" tedu, improvements and OK cheloha


# 1.95 17-Nov-2018 cheloha

Add new KERN_CPUSTATS sysctl(2) so we can identify offline CPUs.

Because of hw.smt we need a way to determine whether a given CPU is "online"
or "offline" from userspace. KERN_CPTIME2 is an array, and so cannot be
cleanly extended for this purpose, so add a new sysctl(2) KERN_CPUSTATS
with an extensible struct. At the moment it's just KERN_CPTIME2 with a
flags member, but it can grow as needed.

KERN_CPUSTATS appears to have been defined by BSDi long ago, but there are
few (if any) packages in the wild still using the symbol so breakage in ports
should be near zero. No other system inherited the symbol from BSDi, either.

Then, use the new sysctl(2) in systat(1) and top(1):

- systat(1) draws placeholder marks ('-') instead of percentages for
offline CPUs in the cpu view.

- systat(1) omits offline CPU ticks when drawing the "big bar" in
the vmstat view. The upshot is that the bar isn't half idle when
half your logical CPUs are disabled.

- top(1) does not draw lines for offline CPUs; if CPUs toggle on or
offline in interactive mode we redraw the display to expand/reduce
space for the new/missing CPUs. This is consistent with what some
top(1) implementations do on Linux.

- top(1) omits offline CPUs from the totals when CPU totals are
combined into a single line (the '-1' flag).

Originally prompted by deraadt@. Discussed endlessly with deraadt@,
ketennis@, and sthen@. Tested by jmc@ and jca@. Earlier versions also
discussed with jca@. Earlier versions tested by jmc@, tb@, and many
others.

docs ok jmc@, kernel bits ok ketennis@, everything ok sthen@,
"Is your stuff in yet?" deraadt@


Revision tags: OPENBSD_6_4_BASE
# 1.94 05-Oct-2018 cheloha

Revert KERN_CPTIME2 ENODEV changes in kernel and userspace.

ok kettenis deraadt


# 1.93 26-Sep-2018 cheloha

KERN_CPTIME2: set ENODEV if the CPU is offline.

This lets userspace distinguish between idle CPUs and those that are
not schedulable because hw.smt=0.

A subsequent commit probably needs to add documentation for this
to sysctl.2 (and perhaps elsewhere) after the dust settles.

Also included here are changes to systat(1) and top(1) that account
for the ENODEV case and adjust behavior accordingly:

- systat(1)'s cpu view prints placeholder marks ('-') instead of
percentages for each state if the given CPU is offline.

- systat(1)'s vmstat view checks for offline CPUs when computing the
machine state total and excludes them, so the CPU usage graph
only represents the states for online CPUs.

- top(1) does not draw CPU rows for offline CPUs when the view is
redrawn. If CPUs "go offline", percentages for each state are
replaced by placeholder marks ('-'); the view will need to be
redrawn to remove these rows. If CPUs "go online" the view will
need to be redrawn to show these new CPUs. In "combined CPU" mode,
the count and the state totals only represent online CPUs.

Ports using KERN_CPTIME2 will need to be updated. The changes
described above to make systat(1) and top(1) aware of the ENODEV
case *and* gracefully handle a changing HW_NCPUONLINE while the
application is running are not necessarily appropriate for each
and every port.

The changes described above are so extensive in part to demonstrate
one way a program *might* be made robust to changing CPU availability.
In particular, changing hw.smt after boot is an extremely rare event,
and this needs to be weighed when updating ports.

The logic needed to account for the KERN_CPTIME2 ENODEV case is
very roughly:

if (sysctl(...) == -1) {
if (errno != ENODEV) {
/* Actual error occurred. */
} else {
/* CPU is offline. */
}
} else {
/* CPU is online and CPU states were set by sysctl(2). */
}

Prompted by deraadt@. Basic idea for ENODEV from kettenis@. Discussed at
length with kettenis@. Additional testing by tb@.

No complaints from hackers@ after a week.

ok kettenis@, "I think you should commit [now]" deraadt@


# 1.92 22-Sep-2018 millert

Use user_from_uid() and uid_from_user() directly. The wrappers
in username.c are now so simple there is no longer a good reason
to use them. OK deraadt@


# 1.91 13-Sep-2018 millert

Fix warnings caused by user_from_uid() and group_from_gid() now
returning const char *.


# 1.90 14-May-2018 mpi

Stopping counting and reporting CPU time spent spinning on a lock as
system time.

Introduce a new CP_SPIN "scheduler state" and modify userland tools
to display the % of timer a CPU spents spinning.

Based on a diff from jmatthew@, ok pirofti@, bluhm@, visa@, deraadt@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.89 30-May-2017 tedu

avoid some shadow warnings. from Brian Callahan


Revision tags: OPENBSD_6_1_BASE
# 1.88 15-Mar-2017 deraadt

annoying whitespace die die die


# 1.87 28-Jul-2016 tedu

rework realloc loop. there's no need to shrink the allocation between
calls. if we need a big space once, we'll likely need a big space again.


Revision tags: OPENBSD_6_0_BASE
# 1.86 11-May-2016 edd

Allow top(1) to filter process args if they are being displayed.

OK tedu@, deraadt@, and with input from Michal Mazurek.

Thanks


Revision tags: OPENBSD_5_9_BASE
# 1.85 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_8_BASE
# 1.84 06-May-2015 mpi

Display thread IDs instead of the name of the process's owner when "-H"
is used.

The rationnal is that when you're looking at threads you're generally
already filtereing by PID and this allow you to see which thread is a
pig.

Written some time ago with mikeb@

ok sthen@, krw@, guenther@


Revision tags: OPENBSD_5_7_BASE
# 1.83 19-Jan-2015 millert

Kill loadavg.h and just use the fscale value directly.
There's no need for crazy casts of p_pctcpu in ORDERKEY_PCTCPU
since kinfo_proc exports it as uint32_t. OK deraadt@


# 1.82 19-Jan-2015 deraadt

like in ps(1), fetch the FSCALE value using sysctl rather than using
the header version
ok guenther


# 1.81 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.80 17-Sep-2014 dlg

if there are more than 8 cpus, default to combined cpu stats (like
you passed -1 on the command line).

ok kettenis@ tedu@


# 1.79 15-Sep-2014 miod

Remove non-standard <sys/dkstat.h> header. It has not contained anything
related to disk stastics for almost 17 years, and the remaining
userland-visible defines duplicate those found in <sys/sched.h>.

Move the remaining _KERNEL defines to <sys/tty.h> where they belong, and
update all users to cope with this.

ok kettenis@


Revision tags: OPENBSD_5_6_BASE
# 1.78 04-Jul-2014 guenther

Track whether a process is a zombie or not yet fully built via flags
PS_{ZOMBIE,EMBRYO} on the process instead of peeking into the process's
thread data. This eliminates the need for the thread-level SDEAD state.

Change kvm_getprocs() (both the sysctl() and kvm backends) to report the
"most active" scheduler state for the process's threads.

tweaks kettenis@
feedback and ok matthew@


# 1.77 08-Apr-2014 mpi

Use VM_UVMEXP instead of VM_METER for memory usages and directly
include <sys/vmmeter.h> where it is needed instead of relying on
it being included by <uvm/uvm_extern.h>.

miod@ likes it, ok guenther@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.76 23-Mar-2013 tedu

wchan is no longer exposed, just check if wmesg is not empty.
(wmesg hasn't itself been a pointer since conversion to kinfo_proc.)
noticed by sthen.
ok deraadt jsing millert sthen


Revision tags: OPENBSD_5_3_BASE
# 1.75 18-Dec-2012 millert

We no longer use struct eproc for kinfo_proc in sysctl.h so there
is no direct need for sys/proc.h or sys/resource.h. Some consumers
of kinfo_proc need these for the proc flags and rlimit defines like
RLIM_INF so add the appropriate includes to them.
OK deraadt@ sthen@


Revision tags: OPENBSD_5_2_BASE
# 1.74 09-Jul-2012 deraadt

According to miod, KERNBASE used as a userland symbol should die
in a fire. Start by removing the definitions, before we clean the
headers later.


# 1.73 05-Jun-2012 brynet

Add support for hiding a user's processes in top.

feedback & ok lum@


# 1.72 21-Apr-2012 guenther

Remove a check made superfluous by the KERN_PROC_SHOW_THREADS flag.
This lets new binaries work (at least w/o the -H option) with old kernels.

ok pirofti@ deraadt@


# 1.71 17-Apr-2012 pirofti

Make it optional for kvm_getprocs() (and related sysctl) to return
thread information.

Add a KERN_PROC_SHOW_THREADS flag that has to be set in order to get the
thread info and make it off by default. This creates backwards compatibility
for applications that relied on a given size/number of items to be returned.

Modify ps(1) and top(1) accordingly.

Okay guenther@.


# 1.70 12-Apr-2012 pirofti

Add per thread accounting, mainly for usage & friends.

This expands the already bloated FILL_KPROC macro to take an extra
parameter that indicates if the callee is a thread or a process.

The userland bits are adjusted accordingly and ps(1) and top(1) now
display per thread usage times when -H is used.

Also pkill(1) had to be adjusted so that duplicates don't pop up.

libkvm does basically the same thing as the kernel bits.

Okay guenther@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.69 12-Jul-2011 tedu

print out the amount of memory used for cache. ok otto


# 1.68 10-Apr-2011 guenther

Switch back from KERN_PROC2/kinfo_proc2 to KERN_PROC/kinfo_proc now
that we've got name we want for the API we want

"ZAP!" deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.67 26-Apr-2010 deraadt

more portable code


# 1.66 26-Mar-2010 lum

Make the source of process TIME for displaying and sorting the same.
top(1) and ps(1) now display the same info.
ok tedu@ millert@ otto@ and tested on sparc64 by landry@


Revision tags: OPENBSD_4_7_BASE
# 1.65 29-Jan-2010 tedu

Allow sorting by command and pid. Also, allow partial matches with strncmp.
ok jmc otto


Revision tags: OPENBSD_4_6_BASE
# 1.64 28-Apr-2009 deraadt

extend STATE column to fit > 9 cpus; Aivar Jaakson; PR6131


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.63 01-Nov-2007 otto

bump arbitrary limit on arg length shown by -C; requested by and ok deraadt@


Revision tags: OPENBSD_4_2_BASE
# 1.62 27-Jul-2007 deraadt

replace a few more malloc(n*m) idioms with calloc(n,m) for safety;
inspired by zinovik@cs.karelia.ru


# 1.61 29-May-2007 otto

Instead of using hand-crafted redraws minimizing screen updates, use curses.
Enables nice things like process highlighting without hurting the brain.
ok deraadt@


# 1.60 16-Apr-2007 otto

don't truncate wchan, ok miod@


# 1.59 13-Apr-2007 otto

include cleanup and fix in comment; from Mark Lumsden


# 1.58 04-Apr-2007 otto

missing prototypes; from Mark Lumsden with a twist by me.


Revision tags: OPENBSD_4_1_BASE
# 1.57 04-Feb-2007 otto

long and long long is not the same on 32 bits archs


# 1.56 03-Jan-2007 otto

Add a 'g' command to only show processes having a string in their
command name. ok deraadt@


# 1.55 02-Jan-2007 otto

top(1) doesn't use any math.h function, so do not include math.h and
do not link with libm. ok ray@


# 1.54 29-Nov-2006 miod

Do not test for processes being swapped out since this can't happen anymore.


# 1.53 20-Sep-2006 ray

Recommit memory leak fix.


Revision tags: OPENBSD_4_0_BASE
# 1.52 29-Apr-2006 otto

use calloc(3) instead of malloc(3) to allocate cpu state structs; it's
better style here; plus it prevents bogus data to show up on first display.
report + ok pedro@


Revision tags: OPENBSD_3_9_BASE
# 1.51 21-Dec-2005 millert

Userland programs should include <errno.h> not <sys/errno.h>
OK deraadt@


# 1.50 04-Dec-2005 tedu

support for toggling thread viewing.
ok brad, hints from jmc


Revision tags: OPENBSD_3_8_BASE
# 1.49 17-Jun-2005 markus

add a 'C' command ) that toggles the display of the full cmdline;
with Jared Yanovich; ok deraadt


# 1.48 08-Jun-2005 millert

Multi-cpu support for top. Each cpu now gets its own stats line.
Based on work by todd@ at the hackathon.


Revision tags: OPENBSD_3_7_BASE
# 1.47 06-Dec-2004 markus

missing space; bluhm at genua.de


# 1.46 22-Nov-2004 pat

simplify loop iteration

ok otto millert


# 1.45 17-Nov-2004 markus

off-by-one in process_states[], fixes display of memory; ok deraadt, aaron


# 1.44 07-Oct-2004 otto

introduce -p option and p command to only show a single process.
From Patrick Latifi. ok deraadt@ millert@


Revision tags: OPENBSD_3_6_BASE
# 1.43 13-Jun-2004 otto

Typos in comments and a redundant prototype. From Brian Poole via jmc@
ok jmc@


# 1.42 11-Jun-2004 deraadt

handle the KI_NOCPU case, pointed out by markus


# 1.41 11-Jun-2004 deraadt

only print /# if > 1 cpu on a machine


# 1.40 11-Jun-2004 deraadt

export cpuid via kproc2, and make ps & top aware... from niklas


# 1.39 11-Jun-2004 deraadt

hide top breakage, until the next commit which will do it right


# 1.38 09-May-2004 deraadt

spaces


Revision tags: OPENBSD_3_5_BASE
# 1.37 08-Jan-2004 millert

Convert to kinfo_proc2; ok deraadt@


Revision tags: OPENBSD_3_4_BASE
# 1.36 07-Jul-2003 deraadt

protos


# 1.35 18-Jun-2003 deraadt

remove unused variables


# 1.34 17-Jun-2003 jfb

properly typecast to uid_t the return value of proc_owner() and report
ESRCH instead of EACCES if it returns -1

ok millert@


# 1.33 15-Jun-2003 millert

use uid_t and fix some sign compare warnings; OK krw@ and deraadt@


# 1.32 14-Jun-2003 avsm

use correct length instead of sizeof(char *) for some strl* bound values
millert@ ok


# 1.31 13-Jun-2003 deraadt

readable code


# 1.30 12-Jun-2003 deraadt

knf


# 1.29 12-Jun-2003 pvalchev

cleanup; ok deraadt


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.28 02-Jul-2002 tholo

Add missing copyright


Revision tags: OPENBSD_3_1_BASE
# 1.27 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.26 05-Dec-2001 art

Get maxslp with sysctl. Not the constant.


Revision tags: OPENBSD_3_0_BASE
# 1.25 12-Jul-2001 deraadt

first pass at a -Wall cleanup


# 1.24 03-Jun-2001 angelos

Use KERN_NPROCS to get the number of processes on the system, to
determine the amount of space we'll need to store the
information. The alternative, calling sysctl() with a NULL argument
for data, meant the kernel had to go through the process table.


Revision tags: OPENBSD_2_9_BASE
# 1.23 22-Feb-2001 deraadt

plug mem leak i introduced; spotted by millert


# 1.22 17-Feb-2001 deraadt

bye bye setgid kvm


# 1.21 17-Feb-2001 deraadt

do not use nlist anymore, kvm gets the bullet next


# 1.20 17-Feb-2001 deraadt

partial KNF, because i cannot read the diffs i am trying to debug


Revision tags: OPENBSD_2_8_BASE
# 1.19 18-Jun-2000 niklas

Mark kernel threads as system processes. Make ps hide them by default,
show them with -k. Do not try to show RSS based values for them as they
mess up column alignment. vmstat -f now shows kernel threads separately
from rforks too.


Revision tags: OPENBSD_2_7_BASE
# 1.18 14-Nov-1999 deraadt

stathz instead of hz, plus improvements by me; camield@inet.unisource.nl


# 1.17 29-Oct-1999 todd

writen


Revision tags: OPENBSD_2_6_BASE
# 1.16 21-Jun-1999 art

stop casting the third argument to kvm_read to char *. it is a void *.


# 1.15 22-May-1999 weingart

Update for new vmswap code.


Revision tags: OPENBSD_2_5_BASE
# 1.14 28-Nov-1998 kstailey

do not count swap spaces that were not swapon(2)ed


Revision tags: OPENBSD_2_4_BASE
# 1.13 20-Sep-1998 niklas

cp_time is long, deal with it.


# 1.12 20-Sep-1998 niklas

widen unsigned entities to signed ones before subtracting, fixes sorting on alpha


# 1.11 21-Aug-1998 kstailey

support "-o field" and the interactive "o" command


# 1.10 08-Jul-1998 deraadt

whack kmem gid after kvm_openfiles()


# 1.9 25-Jun-1998 deraadt

handle missing swap


Revision tags: OPENBSD_2_3_BASE
# 1.8 08-Nov-1997 millert

Sort idle processes by total CPU usage, not cpticks since cpticks
get zeroed every second; dan@dan.emsphone.com


Revision tags: OPENBSD_2_2_BASE
# 1.7 09-Sep-1997 millert

"last pid" stuff is useless with random pids.


# 1.6 09-Sep-1997 millert

Use kvm_openfiles() not the sunos compat kvm_open().
Use warn/warnx where it makes sense and check some more ret vals.


# 1.5 24-Aug-1997 millert

Fix compiler warnings.


# 1.4 22-Aug-1997 downsj

Two more alpha warnings.


# 1.3 22-Aug-1997 downsj

First sweep. Prototype, type fixes, long fixes, mostly compiles with
-Wall.


# 1.2 17-Aug-1997 kstailey

print "idle" for idle processes


# 1.1 14-Aug-1997 downsj

top 3.4, with a few changes. Still needs more work.


# 1.104 24-Jun-2020 kn

Remove obsolete comment about "extra nulls"

Those were removed in r1.39 in 2004.


# 1.103 23-Jun-2020 kn

Remove redundant NULL check

For the sake of simplicity and to reflect that the process list is always
sorted (default is "cpu"), even if not explicitly requested; this makes it
easier to argue about the code around sort order and its selection.

OK millert


Revision tags: OPENBSD_6_7_BASE
# 1.102 06-Jan-2020 zhuk

Implement scrolling in top(1) using 9 and 0 keys.

Planned improvements: scroll position displaying, using arrow/pgup/pgdown keys.

okay tedu@


# 1.101 16-Dec-2019 guenther

Delete tests for P_THREAD that predate the existence of
KERN_PROC_SHOW_THREADS and have been rendered superfluous by it.
Similarly, some P_SYSTEM tests can be deleted or pushed to the
kernel by using KERN_PROC_ALL instead of KERN_PROC_KTHREAD.

ok visa@ mpi@


Revision tags: OPENBSD_6_6_BASE
# 1.100 08-Oct-2019 kn

Simplify threads flag handling

Instead of always passing the threads flag which decides whether other
function arguments are used or not, make callers pass those arguments or
NULL depending on the threads flag.

This reflects better how thread IDs are shown in place of user names/IDs.

OK millert


# 1.99 06-Oct-2019 kn

Avoid gasting around get_process_info()

get_process_info() returns a pointer to the global handle later only be
used in format_next_process(); treat this struct handle as such without
casting the pointer to caddr_t and back again.

No object change.
OK millert deraadt


# 1.98 06-Oct-2019 kn

Zap intermediate structs in compare_*()

SETORDER() can assign from function arguments directly without additional
identical structs in each function.

No object change.
OK millert


# 1.97 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


Revision tags: OPENBSD_6_5_BASE
# 1.96 28-Nov-2018 kn

Allow reverse sort order

Prefixing the field with a dash will sort processes in reverse order.
`o -pid' will therefore list PID 1 on top.

"looks good" tedu, improvements and OK cheloha


# 1.95 17-Nov-2018 cheloha

Add new KERN_CPUSTATS sysctl(2) so we can identify offline CPUs.

Because of hw.smt we need a way to determine whether a given CPU is "online"
or "offline" from userspace. KERN_CPTIME2 is an array, and so cannot be
cleanly extended for this purpose, so add a new sysctl(2) KERN_CPUSTATS
with an extensible struct. At the moment it's just KERN_CPTIME2 with a
flags member, but it can grow as needed.

KERN_CPUSTATS appears to have been defined by BSDi long ago, but there are
few (if any) packages in the wild still using the symbol so breakage in ports
should be near zero. No other system inherited the symbol from BSDi, either.

Then, use the new sysctl(2) in systat(1) and top(1):

- systat(1) draws placeholder marks ('-') instead of percentages for
offline CPUs in the cpu view.

- systat(1) omits offline CPU ticks when drawing the "big bar" in
the vmstat view. The upshot is that the bar isn't half idle when
half your logical CPUs are disabled.

- top(1) does not draw lines for offline CPUs; if CPUs toggle on or
offline in interactive mode we redraw the display to expand/reduce
space for the new/missing CPUs. This is consistent with what some
top(1) implementations do on Linux.

- top(1) omits offline CPUs from the totals when CPU totals are
combined into a single line (the '-1' flag).

Originally prompted by deraadt@. Discussed endlessly with deraadt@,
ketennis@, and sthen@. Tested by jmc@ and jca@. Earlier versions also
discussed with jca@. Earlier versions tested by jmc@, tb@, and many
others.

docs ok jmc@, kernel bits ok ketennis@, everything ok sthen@,
"Is your stuff in yet?" deraadt@


Revision tags: OPENBSD_6_4_BASE
# 1.94 05-Oct-2018 cheloha

Revert KERN_CPTIME2 ENODEV changes in kernel and userspace.

ok kettenis deraadt


# 1.93 26-Sep-2018 cheloha

KERN_CPTIME2: set ENODEV if the CPU is offline.

This lets userspace distinguish between idle CPUs and those that are
not schedulable because hw.smt=0.

A subsequent commit probably needs to add documentation for this
to sysctl.2 (and perhaps elsewhere) after the dust settles.

Also included here are changes to systat(1) and top(1) that account
for the ENODEV case and adjust behavior accordingly:

- systat(1)'s cpu view prints placeholder marks ('-') instead of
percentages for each state if the given CPU is offline.

- systat(1)'s vmstat view checks for offline CPUs when computing the
machine state total and excludes them, so the CPU usage graph
only represents the states for online CPUs.

- top(1) does not draw CPU rows for offline CPUs when the view is
redrawn. If CPUs "go offline", percentages for each state are
replaced by placeholder marks ('-'); the view will need to be
redrawn to remove these rows. If CPUs "go online" the view will
need to be redrawn to show these new CPUs. In "combined CPU" mode,
the count and the state totals only represent online CPUs.

Ports using KERN_CPTIME2 will need to be updated. The changes
described above to make systat(1) and top(1) aware of the ENODEV
case *and* gracefully handle a changing HW_NCPUONLINE while the
application is running are not necessarily appropriate for each
and every port.

The changes described above are so extensive in part to demonstrate
one way a program *might* be made robust to changing CPU availability.
In particular, changing hw.smt after boot is an extremely rare event,
and this needs to be weighed when updating ports.

The logic needed to account for the KERN_CPTIME2 ENODEV case is
very roughly:

if (sysctl(...) == -1) {
if (errno != ENODEV) {
/* Actual error occurred. */
} else {
/* CPU is offline. */
}
} else {
/* CPU is online and CPU states were set by sysctl(2). */
}

Prompted by deraadt@. Basic idea for ENODEV from kettenis@. Discussed at
length with kettenis@. Additional testing by tb@.

No complaints from hackers@ after a week.

ok kettenis@, "I think you should commit [now]" deraadt@


# 1.92 22-Sep-2018 millert

Use user_from_uid() and uid_from_user() directly. The wrappers
in username.c are now so simple there is no longer a good reason
to use them. OK deraadt@


# 1.91 13-Sep-2018 millert

Fix warnings caused by user_from_uid() and group_from_gid() now
returning const char *.


# 1.90 14-May-2018 mpi

Stopping counting and reporting CPU time spent spinning on a lock as
system time.

Introduce a new CP_SPIN "scheduler state" and modify userland tools
to display the % of timer a CPU spents spinning.

Based on a diff from jmatthew@, ok pirofti@, bluhm@, visa@, deraadt@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.89 30-May-2017 tedu

avoid some shadow warnings. from Brian Callahan


Revision tags: OPENBSD_6_1_BASE
# 1.88 15-Mar-2017 deraadt

annoying whitespace die die die


# 1.87 28-Jul-2016 tedu

rework realloc loop. there's no need to shrink the allocation between
calls. if we need a big space once, we'll likely need a big space again.


Revision tags: OPENBSD_6_0_BASE
# 1.86 11-May-2016 edd

Allow top(1) to filter process args if they are being displayed.

OK tedu@, deraadt@, and with input from Michal Mazurek.

Thanks


Revision tags: OPENBSD_5_9_BASE
# 1.85 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_8_BASE
# 1.84 06-May-2015 mpi

Display thread IDs instead of the name of the process's owner when "-H"
is used.

The rationnal is that when you're looking at threads you're generally
already filtereing by PID and this allow you to see which thread is a
pig.

Written some time ago with mikeb@

ok sthen@, krw@, guenther@


Revision tags: OPENBSD_5_7_BASE
# 1.83 19-Jan-2015 millert

Kill loadavg.h and just use the fscale value directly.
There's no need for crazy casts of p_pctcpu in ORDERKEY_PCTCPU
since kinfo_proc exports it as uint32_t. OK deraadt@


# 1.82 19-Jan-2015 deraadt

like in ps(1), fetch the FSCALE value using sysctl rather than using
the header version
ok guenther


# 1.81 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.80 17-Sep-2014 dlg

if there are more than 8 cpus, default to combined cpu stats (like
you passed -1 on the command line).

ok kettenis@ tedu@


# 1.79 15-Sep-2014 miod

Remove non-standard <sys/dkstat.h> header. It has not contained anything
related to disk stastics for almost 17 years, and the remaining
userland-visible defines duplicate those found in <sys/sched.h>.

Move the remaining _KERNEL defines to <sys/tty.h> where they belong, and
update all users to cope with this.

ok kettenis@


Revision tags: OPENBSD_5_6_BASE
# 1.78 04-Jul-2014 guenther

Track whether a process is a zombie or not yet fully built via flags
PS_{ZOMBIE,EMBRYO} on the process instead of peeking into the process's
thread data. This eliminates the need for the thread-level SDEAD state.

Change kvm_getprocs() (both the sysctl() and kvm backends) to report the
"most active" scheduler state for the process's threads.

tweaks kettenis@
feedback and ok matthew@


# 1.77 08-Apr-2014 mpi

Use VM_UVMEXP instead of VM_METER for memory usages and directly
include <sys/vmmeter.h> where it is needed instead of relying on
it being included by <uvm/uvm_extern.h>.

miod@ likes it, ok guenther@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.76 23-Mar-2013 tedu

wchan is no longer exposed, just check if wmesg is not empty.
(wmesg hasn't itself been a pointer since conversion to kinfo_proc.)
noticed by sthen.
ok deraadt jsing millert sthen


Revision tags: OPENBSD_5_3_BASE
# 1.75 18-Dec-2012 millert

We no longer use struct eproc for kinfo_proc in sysctl.h so there
is no direct need for sys/proc.h or sys/resource.h. Some consumers
of kinfo_proc need these for the proc flags and rlimit defines like
RLIM_INF so add the appropriate includes to them.
OK deraadt@ sthen@


Revision tags: OPENBSD_5_2_BASE
# 1.74 09-Jul-2012 deraadt

According to miod, KERNBASE used as a userland symbol should die
in a fire. Start by removing the definitions, before we clean the
headers later.


# 1.73 05-Jun-2012 brynet

Add support for hiding a user's processes in top.

feedback & ok lum@


# 1.72 21-Apr-2012 guenther

Remove a check made superfluous by the KERN_PROC_SHOW_THREADS flag.
This lets new binaries work (at least w/o the -H option) with old kernels.

ok pirofti@ deraadt@


# 1.71 17-Apr-2012 pirofti

Make it optional for kvm_getprocs() (and related sysctl) to return
thread information.

Add a KERN_PROC_SHOW_THREADS flag that has to be set in order to get the
thread info and make it off by default. This creates backwards compatibility
for applications that relied on a given size/number of items to be returned.

Modify ps(1) and top(1) accordingly.

Okay guenther@.


# 1.70 12-Apr-2012 pirofti

Add per thread accounting, mainly for usage & friends.

This expands the already bloated FILL_KPROC macro to take an extra
parameter that indicates if the callee is a thread or a process.

The userland bits are adjusted accordingly and ps(1) and top(1) now
display per thread usage times when -H is used.

Also pkill(1) had to be adjusted so that duplicates don't pop up.

libkvm does basically the same thing as the kernel bits.

Okay guenther@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.69 12-Jul-2011 tedu

print out the amount of memory used for cache. ok otto


# 1.68 10-Apr-2011 guenther

Switch back from KERN_PROC2/kinfo_proc2 to KERN_PROC/kinfo_proc now
that we've got name we want for the API we want

"ZAP!" deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.67 26-Apr-2010 deraadt

more portable code


# 1.66 26-Mar-2010 lum

Make the source of process TIME for displaying and sorting the same.
top(1) and ps(1) now display the same info.
ok tedu@ millert@ otto@ and tested on sparc64 by landry@


Revision tags: OPENBSD_4_7_BASE
# 1.65 29-Jan-2010 tedu

Allow sorting by command and pid. Also, allow partial matches with strncmp.
ok jmc otto


Revision tags: OPENBSD_4_6_BASE
# 1.64 28-Apr-2009 deraadt

extend STATE column to fit > 9 cpus; Aivar Jaakson; PR6131


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.63 01-Nov-2007 otto

bump arbitrary limit on arg length shown by -C; requested by and ok deraadt@


Revision tags: OPENBSD_4_2_BASE
# 1.62 27-Jul-2007 deraadt

replace a few more malloc(n*m) idioms with calloc(n,m) for safety;
inspired by zinovik@cs.karelia.ru


# 1.61 29-May-2007 otto

Instead of using hand-crafted redraws minimizing screen updates, use curses.
Enables nice things like process highlighting without hurting the brain.
ok deraadt@


# 1.60 16-Apr-2007 otto

don't truncate wchan, ok miod@


# 1.59 13-Apr-2007 otto

include cleanup and fix in comment; from Mark Lumsden


# 1.58 04-Apr-2007 otto

missing prototypes; from Mark Lumsden with a twist by me.


Revision tags: OPENBSD_4_1_BASE
# 1.57 04-Feb-2007 otto

long and long long is not the same on 32 bits archs


# 1.56 03-Jan-2007 otto

Add a 'g' command to only show processes having a string in their
command name. ok deraadt@


# 1.55 02-Jan-2007 otto

top(1) doesn't use any math.h function, so do not include math.h and
do not link with libm. ok ray@


# 1.54 29-Nov-2006 miod

Do not test for processes being swapped out since this can't happen anymore.


# 1.53 20-Sep-2006 ray

Recommit memory leak fix.


Revision tags: OPENBSD_4_0_BASE
# 1.52 29-Apr-2006 otto

use calloc(3) instead of malloc(3) to allocate cpu state structs; it's
better style here; plus it prevents bogus data to show up on first display.
report + ok pedro@


Revision tags: OPENBSD_3_9_BASE
# 1.51 21-Dec-2005 millert

Userland programs should include <errno.h> not <sys/errno.h>
OK deraadt@


# 1.50 04-Dec-2005 tedu

support for toggling thread viewing.
ok brad, hints from jmc


Revision tags: OPENBSD_3_8_BASE
# 1.49 17-Jun-2005 markus

add a 'C' command ) that toggles the display of the full cmdline;
with Jared Yanovich; ok deraadt


# 1.48 08-Jun-2005 millert

Multi-cpu support for top. Each cpu now gets its own stats line.
Based on work by todd@ at the hackathon.


Revision tags: OPENBSD_3_7_BASE
# 1.47 06-Dec-2004 markus

missing space; bluhm at genua.de


# 1.46 22-Nov-2004 pat

simplify loop iteration

ok otto millert


# 1.45 17-Nov-2004 markus

off-by-one in process_states[], fixes display of memory; ok deraadt, aaron


# 1.44 07-Oct-2004 otto

introduce -p option and p command to only show a single process.
From Patrick Latifi. ok deraadt@ millert@


Revision tags: OPENBSD_3_6_BASE
# 1.43 13-Jun-2004 otto

Typos in comments and a redundant prototype. From Brian Poole via jmc@
ok jmc@


# 1.42 11-Jun-2004 deraadt

handle the KI_NOCPU case, pointed out by markus


# 1.41 11-Jun-2004 deraadt

only print /# if > 1 cpu on a machine


# 1.40 11-Jun-2004 deraadt

export cpuid via kproc2, and make ps & top aware... from niklas


# 1.39 11-Jun-2004 deraadt

hide top breakage, until the next commit which will do it right


# 1.38 09-May-2004 deraadt

spaces


Revision tags: OPENBSD_3_5_BASE
# 1.37 08-Jan-2004 millert

Convert to kinfo_proc2; ok deraadt@


Revision tags: OPENBSD_3_4_BASE
# 1.36 07-Jul-2003 deraadt

protos


# 1.35 18-Jun-2003 deraadt

remove unused variables


# 1.34 17-Jun-2003 jfb

properly typecast to uid_t the return value of proc_owner() and report
ESRCH instead of EACCES if it returns -1

ok millert@


# 1.33 15-Jun-2003 millert

use uid_t and fix some sign compare warnings; OK krw@ and deraadt@


# 1.32 14-Jun-2003 avsm

use correct length instead of sizeof(char *) for some strl* bound values
millert@ ok


# 1.31 13-Jun-2003 deraadt

readable code


# 1.30 12-Jun-2003 deraadt

knf


# 1.29 12-Jun-2003 pvalchev

cleanup; ok deraadt


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.28 02-Jul-2002 tholo

Add missing copyright


Revision tags: OPENBSD_3_1_BASE
# 1.27 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.26 05-Dec-2001 art

Get maxslp with sysctl. Not the constant.


Revision tags: OPENBSD_3_0_BASE
# 1.25 12-Jul-2001 deraadt

first pass at a -Wall cleanup


# 1.24 03-Jun-2001 angelos

Use KERN_NPROCS to get the number of processes on the system, to
determine the amount of space we'll need to store the
information. The alternative, calling sysctl() with a NULL argument
for data, meant the kernel had to go through the process table.


Revision tags: OPENBSD_2_9_BASE
# 1.23 22-Feb-2001 deraadt

plug mem leak i introduced; spotted by millert


# 1.22 17-Feb-2001 deraadt

bye bye setgid kvm


# 1.21 17-Feb-2001 deraadt

do not use nlist anymore, kvm gets the bullet next


# 1.20 17-Feb-2001 deraadt

partial KNF, because i cannot read the diffs i am trying to debug


Revision tags: OPENBSD_2_8_BASE
# 1.19 18-Jun-2000 niklas

Mark kernel threads as system processes. Make ps hide them by default,
show them with -k. Do not try to show RSS based values for them as they
mess up column alignment. vmstat -f now shows kernel threads separately
from rforks too.


Revision tags: OPENBSD_2_7_BASE
# 1.18 14-Nov-1999 deraadt

stathz instead of hz, plus improvements by me; camield@inet.unisource.nl


# 1.17 29-Oct-1999 todd

writen


Revision tags: OPENBSD_2_6_BASE
# 1.16 21-Jun-1999 art

stop casting the third argument to kvm_read to char *. it is a void *.


# 1.15 22-May-1999 weingart

Update for new vmswap code.


Revision tags: OPENBSD_2_5_BASE
# 1.14 28-Nov-1998 kstailey

do not count swap spaces that were not swapon(2)ed


Revision tags: OPENBSD_2_4_BASE
# 1.13 20-Sep-1998 niklas

cp_time is long, deal with it.


# 1.12 20-Sep-1998 niklas

widen unsigned entities to signed ones before subtracting, fixes sorting on alpha


# 1.11 21-Aug-1998 kstailey

support "-o field" and the interactive "o" command


# 1.10 08-Jul-1998 deraadt

whack kmem gid after kvm_openfiles()


# 1.9 25-Jun-1998 deraadt

handle missing swap


Revision tags: OPENBSD_2_3_BASE
# 1.8 08-Nov-1997 millert

Sort idle processes by total CPU usage, not cpticks since cpticks
get zeroed every second; dan@dan.emsphone.com


Revision tags: OPENBSD_2_2_BASE
# 1.7 09-Sep-1997 millert

"last pid" stuff is useless with random pids.


# 1.6 09-Sep-1997 millert

Use kvm_openfiles() not the sunos compat kvm_open().
Use warn/warnx where it makes sense and check some more ret vals.


# 1.5 24-Aug-1997 millert

Fix compiler warnings.


# 1.4 22-Aug-1997 downsj

Two more alpha warnings.


# 1.3 22-Aug-1997 downsj

First sweep. Prototype, type fixes, long fixes, mostly compiles with
-Wall.


# 1.2 17-Aug-1997 kstailey

print "idle" for idle processes


# 1.1 14-Aug-1997 downsj

top 3.4, with a few changes. Still needs more work.


# 1.103 23-Jun-2020 kn

Remove redundant NULL check

For the sake of simplicity and to reflect that the process list is always
sorted (default is "cpu"), even if not explicitly requested; this makes it
easier to argue about the code around sort order and its selection.

OK millert


Revision tags: OPENBSD_6_7_BASE
# 1.102 06-Jan-2020 zhuk

Implement scrolling in top(1) using 9 and 0 keys.

Planned improvements: scroll position displaying, using arrow/pgup/pgdown keys.

okay tedu@


# 1.101 16-Dec-2019 guenther

Delete tests for P_THREAD that predate the existence of
KERN_PROC_SHOW_THREADS and have been rendered superfluous by it.
Similarly, some P_SYSTEM tests can be deleted or pushed to the
kernel by using KERN_PROC_ALL instead of KERN_PROC_KTHREAD.

ok visa@ mpi@


Revision tags: OPENBSD_6_6_BASE
# 1.100 08-Oct-2019 kn

Simplify threads flag handling

Instead of always passing the threads flag which decides whether other
function arguments are used or not, make callers pass those arguments or
NULL depending on the threads flag.

This reflects better how thread IDs are shown in place of user names/IDs.

OK millert


# 1.99 06-Oct-2019 kn

Avoid gasting around get_process_info()

get_process_info() returns a pointer to the global handle later only be
used in format_next_process(); treat this struct handle as such without
casting the pointer to caddr_t and back again.

No object change.
OK millert deraadt


# 1.98 06-Oct-2019 kn

Zap intermediate structs in compare_*()

SETORDER() can assign from function arguments directly without additional
identical structs in each function.

No object change.
OK millert


# 1.97 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


Revision tags: OPENBSD_6_5_BASE
# 1.96 28-Nov-2018 kn

Allow reverse sort order

Prefixing the field with a dash will sort processes in reverse order.
`o -pid' will therefore list PID 1 on top.

"looks good" tedu, improvements and OK cheloha


# 1.95 17-Nov-2018 cheloha

Add new KERN_CPUSTATS sysctl(2) so we can identify offline CPUs.

Because of hw.smt we need a way to determine whether a given CPU is "online"
or "offline" from userspace. KERN_CPTIME2 is an array, and so cannot be
cleanly extended for this purpose, so add a new sysctl(2) KERN_CPUSTATS
with an extensible struct. At the moment it's just KERN_CPTIME2 with a
flags member, but it can grow as needed.

KERN_CPUSTATS appears to have been defined by BSDi long ago, but there are
few (if any) packages in the wild still using the symbol so breakage in ports
should be near zero. No other system inherited the symbol from BSDi, either.

Then, use the new sysctl(2) in systat(1) and top(1):

- systat(1) draws placeholder marks ('-') instead of percentages for
offline CPUs in the cpu view.

- systat(1) omits offline CPU ticks when drawing the "big bar" in
the vmstat view. The upshot is that the bar isn't half idle when
half your logical CPUs are disabled.

- top(1) does not draw lines for offline CPUs; if CPUs toggle on or
offline in interactive mode we redraw the display to expand/reduce
space for the new/missing CPUs. This is consistent with what some
top(1) implementations do on Linux.

- top(1) omits offline CPUs from the totals when CPU totals are
combined into a single line (the '-1' flag).

Originally prompted by deraadt@. Discussed endlessly with deraadt@,
ketennis@, and sthen@. Tested by jmc@ and jca@. Earlier versions also
discussed with jca@. Earlier versions tested by jmc@, tb@, and many
others.

docs ok jmc@, kernel bits ok ketennis@, everything ok sthen@,
"Is your stuff in yet?" deraadt@


Revision tags: OPENBSD_6_4_BASE
# 1.94 05-Oct-2018 cheloha

Revert KERN_CPTIME2 ENODEV changes in kernel and userspace.

ok kettenis deraadt


# 1.93 26-Sep-2018 cheloha

KERN_CPTIME2: set ENODEV if the CPU is offline.

This lets userspace distinguish between idle CPUs and those that are
not schedulable because hw.smt=0.

A subsequent commit probably needs to add documentation for this
to sysctl.2 (and perhaps elsewhere) after the dust settles.

Also included here are changes to systat(1) and top(1) that account
for the ENODEV case and adjust behavior accordingly:

- systat(1)'s cpu view prints placeholder marks ('-') instead of
percentages for each state if the given CPU is offline.

- systat(1)'s vmstat view checks for offline CPUs when computing the
machine state total and excludes them, so the CPU usage graph
only represents the states for online CPUs.

- top(1) does not draw CPU rows for offline CPUs when the view is
redrawn. If CPUs "go offline", percentages for each state are
replaced by placeholder marks ('-'); the view will need to be
redrawn to remove these rows. If CPUs "go online" the view will
need to be redrawn to show these new CPUs. In "combined CPU" mode,
the count and the state totals only represent online CPUs.

Ports using KERN_CPTIME2 will need to be updated. The changes
described above to make systat(1) and top(1) aware of the ENODEV
case *and* gracefully handle a changing HW_NCPUONLINE while the
application is running are not necessarily appropriate for each
and every port.

The changes described above are so extensive in part to demonstrate
one way a program *might* be made robust to changing CPU availability.
In particular, changing hw.smt after boot is an extremely rare event,
and this needs to be weighed when updating ports.

The logic needed to account for the KERN_CPTIME2 ENODEV case is
very roughly:

if (sysctl(...) == -1) {
if (errno != ENODEV) {
/* Actual error occurred. */
} else {
/* CPU is offline. */
}
} else {
/* CPU is online and CPU states were set by sysctl(2). */
}

Prompted by deraadt@. Basic idea for ENODEV from kettenis@. Discussed at
length with kettenis@. Additional testing by tb@.

No complaints from hackers@ after a week.

ok kettenis@, "I think you should commit [now]" deraadt@


# 1.92 22-Sep-2018 millert

Use user_from_uid() and uid_from_user() directly. The wrappers
in username.c are now so simple there is no longer a good reason
to use them. OK deraadt@


# 1.91 13-Sep-2018 millert

Fix warnings caused by user_from_uid() and group_from_gid() now
returning const char *.


# 1.90 14-May-2018 mpi

Stopping counting and reporting CPU time spent spinning on a lock as
system time.

Introduce a new CP_SPIN "scheduler state" and modify userland tools
to display the % of timer a CPU spents spinning.

Based on a diff from jmatthew@, ok pirofti@, bluhm@, visa@, deraadt@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.89 30-May-2017 tedu

avoid some shadow warnings. from Brian Callahan


Revision tags: OPENBSD_6_1_BASE
# 1.88 15-Mar-2017 deraadt

annoying whitespace die die die


# 1.87 28-Jul-2016 tedu

rework realloc loop. there's no need to shrink the allocation between
calls. if we need a big space once, we'll likely need a big space again.


Revision tags: OPENBSD_6_0_BASE
# 1.86 11-May-2016 edd

Allow top(1) to filter process args if they are being displayed.

OK tedu@, deraadt@, and with input from Michal Mazurek.

Thanks


Revision tags: OPENBSD_5_9_BASE
# 1.85 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_8_BASE
# 1.84 06-May-2015 mpi

Display thread IDs instead of the name of the process's owner when "-H"
is used.

The rationnal is that when you're looking at threads you're generally
already filtereing by PID and this allow you to see which thread is a
pig.

Written some time ago with mikeb@

ok sthen@, krw@, guenther@


Revision tags: OPENBSD_5_7_BASE
# 1.83 19-Jan-2015 millert

Kill loadavg.h and just use the fscale value directly.
There's no need for crazy casts of p_pctcpu in ORDERKEY_PCTCPU
since kinfo_proc exports it as uint32_t. OK deraadt@


# 1.82 19-Jan-2015 deraadt

like in ps(1), fetch the FSCALE value using sysctl rather than using
the header version
ok guenther


# 1.81 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.80 17-Sep-2014 dlg

if there are more than 8 cpus, default to combined cpu stats (like
you passed -1 on the command line).

ok kettenis@ tedu@


# 1.79 15-Sep-2014 miod

Remove non-standard <sys/dkstat.h> header. It has not contained anything
related to disk stastics for almost 17 years, and the remaining
userland-visible defines duplicate those found in <sys/sched.h>.

Move the remaining _KERNEL defines to <sys/tty.h> where they belong, and
update all users to cope with this.

ok kettenis@


Revision tags: OPENBSD_5_6_BASE
# 1.78 04-Jul-2014 guenther

Track whether a process is a zombie or not yet fully built via flags
PS_{ZOMBIE,EMBRYO} on the process instead of peeking into the process's
thread data. This eliminates the need for the thread-level SDEAD state.

Change kvm_getprocs() (both the sysctl() and kvm backends) to report the
"most active" scheduler state for the process's threads.

tweaks kettenis@
feedback and ok matthew@


# 1.77 08-Apr-2014 mpi

Use VM_UVMEXP instead of VM_METER for memory usages and directly
include <sys/vmmeter.h> where it is needed instead of relying on
it being included by <uvm/uvm_extern.h>.

miod@ likes it, ok guenther@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.76 23-Mar-2013 tedu

wchan is no longer exposed, just check if wmesg is not empty.
(wmesg hasn't itself been a pointer since conversion to kinfo_proc.)
noticed by sthen.
ok deraadt jsing millert sthen


Revision tags: OPENBSD_5_3_BASE
# 1.75 18-Dec-2012 millert

We no longer use struct eproc for kinfo_proc in sysctl.h so there
is no direct need for sys/proc.h or sys/resource.h. Some consumers
of kinfo_proc need these for the proc flags and rlimit defines like
RLIM_INF so add the appropriate includes to them.
OK deraadt@ sthen@


Revision tags: OPENBSD_5_2_BASE
# 1.74 09-Jul-2012 deraadt

According to miod, KERNBASE used as a userland symbol should die
in a fire. Start by removing the definitions, before we clean the
headers later.


# 1.73 05-Jun-2012 brynet

Add support for hiding a user's processes in top.

feedback & ok lum@


# 1.72 21-Apr-2012 guenther

Remove a check made superfluous by the KERN_PROC_SHOW_THREADS flag.
This lets new binaries work (at least w/o the -H option) with old kernels.

ok pirofti@ deraadt@


# 1.71 17-Apr-2012 pirofti

Make it optional for kvm_getprocs() (and related sysctl) to return
thread information.

Add a KERN_PROC_SHOW_THREADS flag that has to be set in order to get the
thread info and make it off by default. This creates backwards compatibility
for applications that relied on a given size/number of items to be returned.

Modify ps(1) and top(1) accordingly.

Okay guenther@.


# 1.70 12-Apr-2012 pirofti

Add per thread accounting, mainly for usage & friends.

This expands the already bloated FILL_KPROC macro to take an extra
parameter that indicates if the callee is a thread or a process.

The userland bits are adjusted accordingly and ps(1) and top(1) now
display per thread usage times when -H is used.

Also pkill(1) had to be adjusted so that duplicates don't pop up.

libkvm does basically the same thing as the kernel bits.

Okay guenther@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.69 12-Jul-2011 tedu

print out the amount of memory used for cache. ok otto


# 1.68 10-Apr-2011 guenther

Switch back from KERN_PROC2/kinfo_proc2 to KERN_PROC/kinfo_proc now
that we've got name we want for the API we want

"ZAP!" deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.67 26-Apr-2010 deraadt

more portable code


# 1.66 26-Mar-2010 lum

Make the source of process TIME for displaying and sorting the same.
top(1) and ps(1) now display the same info.
ok tedu@ millert@ otto@ and tested on sparc64 by landry@


Revision tags: OPENBSD_4_7_BASE
# 1.65 29-Jan-2010 tedu

Allow sorting by command and pid. Also, allow partial matches with strncmp.
ok jmc otto


Revision tags: OPENBSD_4_6_BASE
# 1.64 28-Apr-2009 deraadt

extend STATE column to fit > 9 cpus; Aivar Jaakson; PR6131


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.63 01-Nov-2007 otto

bump arbitrary limit on arg length shown by -C; requested by and ok deraadt@


Revision tags: OPENBSD_4_2_BASE
# 1.62 27-Jul-2007 deraadt

replace a few more malloc(n*m) idioms with calloc(n,m) for safety;
inspired by zinovik@cs.karelia.ru


# 1.61 29-May-2007 otto

Instead of using hand-crafted redraws minimizing screen updates, use curses.
Enables nice things like process highlighting without hurting the brain.
ok deraadt@


# 1.60 16-Apr-2007 otto

don't truncate wchan, ok miod@


# 1.59 13-Apr-2007 otto

include cleanup and fix in comment; from Mark Lumsden


# 1.58 04-Apr-2007 otto

missing prototypes; from Mark Lumsden with a twist by me.


Revision tags: OPENBSD_4_1_BASE
# 1.57 04-Feb-2007 otto

long and long long is not the same on 32 bits archs


# 1.56 03-Jan-2007 otto

Add a 'g' command to only show processes having a string in their
command name. ok deraadt@


# 1.55 02-Jan-2007 otto

top(1) doesn't use any math.h function, so do not include math.h and
do not link with libm. ok ray@


# 1.54 29-Nov-2006 miod

Do not test for processes being swapped out since this can't happen anymore.


# 1.53 20-Sep-2006 ray

Recommit memory leak fix.


Revision tags: OPENBSD_4_0_BASE
# 1.52 29-Apr-2006 otto

use calloc(3) instead of malloc(3) to allocate cpu state structs; it's
better style here; plus it prevents bogus data to show up on first display.
report + ok pedro@


Revision tags: OPENBSD_3_9_BASE
# 1.51 21-Dec-2005 millert

Userland programs should include <errno.h> not <sys/errno.h>
OK deraadt@


# 1.50 04-Dec-2005 tedu

support for toggling thread viewing.
ok brad, hints from jmc


Revision tags: OPENBSD_3_8_BASE
# 1.49 17-Jun-2005 markus

add a 'C' command ) that toggles the display of the full cmdline;
with Jared Yanovich; ok deraadt


# 1.48 08-Jun-2005 millert

Multi-cpu support for top. Each cpu now gets its own stats line.
Based on work by todd@ at the hackathon.


Revision tags: OPENBSD_3_7_BASE
# 1.47 06-Dec-2004 markus

missing space; bluhm at genua.de


# 1.46 22-Nov-2004 pat

simplify loop iteration

ok otto millert


# 1.45 17-Nov-2004 markus

off-by-one in process_states[], fixes display of memory; ok deraadt, aaron


# 1.44 07-Oct-2004 otto

introduce -p option and p command to only show a single process.
From Patrick Latifi. ok deraadt@ millert@


Revision tags: OPENBSD_3_6_BASE
# 1.43 13-Jun-2004 otto

Typos in comments and a redundant prototype. From Brian Poole via jmc@
ok jmc@


# 1.42 11-Jun-2004 deraadt

handle the KI_NOCPU case, pointed out by markus


# 1.41 11-Jun-2004 deraadt

only print /# if > 1 cpu on a machine


# 1.40 11-Jun-2004 deraadt

export cpuid via kproc2, and make ps & top aware... from niklas


# 1.39 11-Jun-2004 deraadt

hide top breakage, until the next commit which will do it right


# 1.38 09-May-2004 deraadt

spaces


Revision tags: OPENBSD_3_5_BASE
# 1.37 08-Jan-2004 millert

Convert to kinfo_proc2; ok deraadt@


Revision tags: OPENBSD_3_4_BASE
# 1.36 07-Jul-2003 deraadt

protos


# 1.35 18-Jun-2003 deraadt

remove unused variables


# 1.34 17-Jun-2003 jfb

properly typecast to uid_t the return value of proc_owner() and report
ESRCH instead of EACCES if it returns -1

ok millert@


# 1.33 15-Jun-2003 millert

use uid_t and fix some sign compare warnings; OK krw@ and deraadt@


# 1.32 14-Jun-2003 avsm

use correct length instead of sizeof(char *) for some strl* bound values
millert@ ok


# 1.31 13-Jun-2003 deraadt

readable code


# 1.30 12-Jun-2003 deraadt

knf


# 1.29 12-Jun-2003 pvalchev

cleanup; ok deraadt


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.28 02-Jul-2002 tholo

Add missing copyright


Revision tags: OPENBSD_3_1_BASE
# 1.27 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.26 05-Dec-2001 art

Get maxslp with sysctl. Not the constant.


Revision tags: OPENBSD_3_0_BASE
# 1.25 12-Jul-2001 deraadt

first pass at a -Wall cleanup


# 1.24 03-Jun-2001 angelos

Use KERN_NPROCS to get the number of processes on the system, to
determine the amount of space we'll need to store the
information. The alternative, calling sysctl() with a NULL argument
for data, meant the kernel had to go through the process table.


Revision tags: OPENBSD_2_9_BASE
# 1.23 22-Feb-2001 deraadt

plug mem leak i introduced; spotted by millert


# 1.22 17-Feb-2001 deraadt

bye bye setgid kvm


# 1.21 17-Feb-2001 deraadt

do not use nlist anymore, kvm gets the bullet next


# 1.20 17-Feb-2001 deraadt

partial KNF, because i cannot read the diffs i am trying to debug


Revision tags: OPENBSD_2_8_BASE
# 1.19 18-Jun-2000 niklas

Mark kernel threads as system processes. Make ps hide them by default,
show them with -k. Do not try to show RSS based values for them as they
mess up column alignment. vmstat -f now shows kernel threads separately
from rforks too.


Revision tags: OPENBSD_2_7_BASE
# 1.18 14-Nov-1999 deraadt

stathz instead of hz, plus improvements by me; camield@inet.unisource.nl


# 1.17 29-Oct-1999 todd

writen


Revision tags: OPENBSD_2_6_BASE
# 1.16 21-Jun-1999 art

stop casting the third argument to kvm_read to char *. it is a void *.


# 1.15 22-May-1999 weingart

Update for new vmswap code.


Revision tags: OPENBSD_2_5_BASE
# 1.14 28-Nov-1998 kstailey

do not count swap spaces that were not swapon(2)ed


Revision tags: OPENBSD_2_4_BASE
# 1.13 20-Sep-1998 niklas

cp_time is long, deal with it.


# 1.12 20-Sep-1998 niklas

widen unsigned entities to signed ones before subtracting, fixes sorting on alpha


# 1.11 21-Aug-1998 kstailey

support "-o field" and the interactive "o" command


# 1.10 08-Jul-1998 deraadt

whack kmem gid after kvm_openfiles()


# 1.9 25-Jun-1998 deraadt

handle missing swap


Revision tags: OPENBSD_2_3_BASE
# 1.8 08-Nov-1997 millert

Sort idle processes by total CPU usage, not cpticks since cpticks
get zeroed every second; dan@dan.emsphone.com


Revision tags: OPENBSD_2_2_BASE
# 1.7 09-Sep-1997 millert

"last pid" stuff is useless with random pids.


# 1.6 09-Sep-1997 millert

Use kvm_openfiles() not the sunos compat kvm_open().
Use warn/warnx where it makes sense and check some more ret vals.


# 1.5 24-Aug-1997 millert

Fix compiler warnings.


# 1.4 22-Aug-1997 downsj

Two more alpha warnings.


# 1.3 22-Aug-1997 downsj

First sweep. Prototype, type fixes, long fixes, mostly compiles with
-Wall.


# 1.2 17-Aug-1997 kstailey

print "idle" for idle processes


# 1.1 14-Aug-1997 downsj

top 3.4, with a few changes. Still needs more work.


# 1.102 06-Jan-2020 zhuk

Implement scrolling in top(1) using 9 and 0 keys.

Planned improvements: scroll position displaying, using arrow/pgup/pgdown keys.

okay tedu@


# 1.101 16-Dec-2019 guenther

Delete tests for P_THREAD that predate the existence of
KERN_PROC_SHOW_THREADS and have been rendered superfluous by it.
Similarly, some P_SYSTEM tests can be deleted or pushed to the
kernel by using KERN_PROC_ALL instead of KERN_PROC_KTHREAD.

ok visa@ mpi@


Revision tags: OPENBSD_6_6_BASE
# 1.100 08-Oct-2019 kn

Simplify threads flag handling

Instead of always passing the threads flag which decides whether other
function arguments are used or not, make callers pass those arguments or
NULL depending on the threads flag.

This reflects better how thread IDs are shown in place of user names/IDs.

OK millert


# 1.99 06-Oct-2019 kn

Avoid gasting around get_process_info()

get_process_info() returns a pointer to the global handle later only be
used in format_next_process(); treat this struct handle as such without
casting the pointer to caddr_t and back again.

No object change.
OK millert deraadt


# 1.98 06-Oct-2019 kn

Zap intermediate structs in compare_*()

SETORDER() can assign from function arguments directly without additional
identical structs in each function.

No object change.
OK millert


# 1.97 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


Revision tags: OPENBSD_6_5_BASE
# 1.96 28-Nov-2018 kn

Allow reverse sort order

Prefixing the field with a dash will sort processes in reverse order.
`o -pid' will therefore list PID 1 on top.

"looks good" tedu, improvements and OK cheloha


# 1.95 17-Nov-2018 cheloha

Add new KERN_CPUSTATS sysctl(2) so we can identify offline CPUs.

Because of hw.smt we need a way to determine whether a given CPU is "online"
or "offline" from userspace. KERN_CPTIME2 is an array, and so cannot be
cleanly extended for this purpose, so add a new sysctl(2) KERN_CPUSTATS
with an extensible struct. At the moment it's just KERN_CPTIME2 with a
flags member, but it can grow as needed.

KERN_CPUSTATS appears to have been defined by BSDi long ago, but there are
few (if any) packages in the wild still using the symbol so breakage in ports
should be near zero. No other system inherited the symbol from BSDi, either.

Then, use the new sysctl(2) in systat(1) and top(1):

- systat(1) draws placeholder marks ('-') instead of percentages for
offline CPUs in the cpu view.

- systat(1) omits offline CPU ticks when drawing the "big bar" in
the vmstat view. The upshot is that the bar isn't half idle when
half your logical CPUs are disabled.

- top(1) does not draw lines for offline CPUs; if CPUs toggle on or
offline in interactive mode we redraw the display to expand/reduce
space for the new/missing CPUs. This is consistent with what some
top(1) implementations do on Linux.

- top(1) omits offline CPUs from the totals when CPU totals are
combined into a single line (the '-1' flag).

Originally prompted by deraadt@. Discussed endlessly with deraadt@,
ketennis@, and sthen@. Tested by jmc@ and jca@. Earlier versions also
discussed with jca@. Earlier versions tested by jmc@, tb@, and many
others.

docs ok jmc@, kernel bits ok ketennis@, everything ok sthen@,
"Is your stuff in yet?" deraadt@


Revision tags: OPENBSD_6_4_BASE
# 1.94 05-Oct-2018 cheloha

Revert KERN_CPTIME2 ENODEV changes in kernel and userspace.

ok kettenis deraadt


# 1.93 26-Sep-2018 cheloha

KERN_CPTIME2: set ENODEV if the CPU is offline.

This lets userspace distinguish between idle CPUs and those that are
not schedulable because hw.smt=0.

A subsequent commit probably needs to add documentation for this
to sysctl.2 (and perhaps elsewhere) after the dust settles.

Also included here are changes to systat(1) and top(1) that account
for the ENODEV case and adjust behavior accordingly:

- systat(1)'s cpu view prints placeholder marks ('-') instead of
percentages for each state if the given CPU is offline.

- systat(1)'s vmstat view checks for offline CPUs when computing the
machine state total and excludes them, so the CPU usage graph
only represents the states for online CPUs.

- top(1) does not draw CPU rows for offline CPUs when the view is
redrawn. If CPUs "go offline", percentages for each state are
replaced by placeholder marks ('-'); the view will need to be
redrawn to remove these rows. If CPUs "go online" the view will
need to be redrawn to show these new CPUs. In "combined CPU" mode,
the count and the state totals only represent online CPUs.

Ports using KERN_CPTIME2 will need to be updated. The changes
described above to make systat(1) and top(1) aware of the ENODEV
case *and* gracefully handle a changing HW_NCPUONLINE while the
application is running are not necessarily appropriate for each
and every port.

The changes described above are so extensive in part to demonstrate
one way a program *might* be made robust to changing CPU availability.
In particular, changing hw.smt after boot is an extremely rare event,
and this needs to be weighed when updating ports.

The logic needed to account for the KERN_CPTIME2 ENODEV case is
very roughly:

if (sysctl(...) == -1) {
if (errno != ENODEV) {
/* Actual error occurred. */
} else {
/* CPU is offline. */
}
} else {
/* CPU is online and CPU states were set by sysctl(2). */
}

Prompted by deraadt@. Basic idea for ENODEV from kettenis@. Discussed at
length with kettenis@. Additional testing by tb@.

No complaints from hackers@ after a week.

ok kettenis@, "I think you should commit [now]" deraadt@


# 1.92 22-Sep-2018 millert

Use user_from_uid() and uid_from_user() directly. The wrappers
in username.c are now so simple there is no longer a good reason
to use them. OK deraadt@


# 1.91 13-Sep-2018 millert

Fix warnings caused by user_from_uid() and group_from_gid() now
returning const char *.


# 1.90 14-May-2018 mpi

Stopping counting and reporting CPU time spent spinning on a lock as
system time.

Introduce a new CP_SPIN "scheduler state" and modify userland tools
to display the % of timer a CPU spents spinning.

Based on a diff from jmatthew@, ok pirofti@, bluhm@, visa@, deraadt@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.89 30-May-2017 tedu

avoid some shadow warnings. from Brian Callahan


Revision tags: OPENBSD_6_1_BASE
# 1.88 15-Mar-2017 deraadt

annoying whitespace die die die


# 1.87 28-Jul-2016 tedu

rework realloc loop. there's no need to shrink the allocation between
calls. if we need a big space once, we'll likely need a big space again.


Revision tags: OPENBSD_6_0_BASE
# 1.86 11-May-2016 edd

Allow top(1) to filter process args if they are being displayed.

OK tedu@, deraadt@, and with input from Michal Mazurek.

Thanks


Revision tags: OPENBSD_5_9_BASE
# 1.85 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_8_BASE
# 1.84 06-May-2015 mpi

Display thread IDs instead of the name of the process's owner when "-H"
is used.

The rationnal is that when you're looking at threads you're generally
already filtereing by PID and this allow you to see which thread is a
pig.

Written some time ago with mikeb@

ok sthen@, krw@, guenther@


Revision tags: OPENBSD_5_7_BASE
# 1.83 19-Jan-2015 millert

Kill loadavg.h and just use the fscale value directly.
There's no need for crazy casts of p_pctcpu in ORDERKEY_PCTCPU
since kinfo_proc exports it as uint32_t. OK deraadt@


# 1.82 19-Jan-2015 deraadt

like in ps(1), fetch the FSCALE value using sysctl rather than using
the header version
ok guenther


# 1.81 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.80 17-Sep-2014 dlg

if there are more than 8 cpus, default to combined cpu stats (like
you passed -1 on the command line).

ok kettenis@ tedu@


# 1.79 15-Sep-2014 miod

Remove non-standard <sys/dkstat.h> header. It has not contained anything
related to disk stastics for almost 17 years, and the remaining
userland-visible defines duplicate those found in <sys/sched.h>.

Move the remaining _KERNEL defines to <sys/tty.h> where they belong, and
update all users to cope with this.

ok kettenis@


Revision tags: OPENBSD_5_6_BASE
# 1.78 04-Jul-2014 guenther

Track whether a process is a zombie or not yet fully built via flags
PS_{ZOMBIE,EMBRYO} on the process instead of peeking into the process's
thread data. This eliminates the need for the thread-level SDEAD state.

Change kvm_getprocs() (both the sysctl() and kvm backends) to report the
"most active" scheduler state for the process's threads.

tweaks kettenis@
feedback and ok matthew@


# 1.77 08-Apr-2014 mpi

Use VM_UVMEXP instead of VM_METER for memory usages and directly
include <sys/vmmeter.h> where it is needed instead of relying on
it being included by <uvm/uvm_extern.h>.

miod@ likes it, ok guenther@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.76 23-Mar-2013 tedu

wchan is no longer exposed, just check if wmesg is not empty.
(wmesg hasn't itself been a pointer since conversion to kinfo_proc.)
noticed by sthen.
ok deraadt jsing millert sthen


Revision tags: OPENBSD_5_3_BASE
# 1.75 18-Dec-2012 millert

We no longer use struct eproc for kinfo_proc in sysctl.h so there
is no direct need for sys/proc.h or sys/resource.h. Some consumers
of kinfo_proc need these for the proc flags and rlimit defines like
RLIM_INF so add the appropriate includes to them.
OK deraadt@ sthen@


Revision tags: OPENBSD_5_2_BASE
# 1.74 09-Jul-2012 deraadt

According to miod, KERNBASE used as a userland symbol should die
in a fire. Start by removing the definitions, before we clean the
headers later.


# 1.73 05-Jun-2012 brynet

Add support for hiding a user's processes in top.

feedback & ok lum@


# 1.72 21-Apr-2012 guenther

Remove a check made superfluous by the KERN_PROC_SHOW_THREADS flag.
This lets new binaries work (at least w/o the -H option) with old kernels.

ok pirofti@ deraadt@


# 1.71 17-Apr-2012 pirofti

Make it optional for kvm_getprocs() (and related sysctl) to return
thread information.

Add a KERN_PROC_SHOW_THREADS flag that has to be set in order to get the
thread info and make it off by default. This creates backwards compatibility
for applications that relied on a given size/number of items to be returned.

Modify ps(1) and top(1) accordingly.

Okay guenther@.


# 1.70 12-Apr-2012 pirofti

Add per thread accounting, mainly for usage & friends.

This expands the already bloated FILL_KPROC macro to take an extra
parameter that indicates if the callee is a thread or a process.

The userland bits are adjusted accordingly and ps(1) and top(1) now
display per thread usage times when -H is used.

Also pkill(1) had to be adjusted so that duplicates don't pop up.

libkvm does basically the same thing as the kernel bits.

Okay guenther@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.69 12-Jul-2011 tedu

print out the amount of memory used for cache. ok otto


# 1.68 10-Apr-2011 guenther

Switch back from KERN_PROC2/kinfo_proc2 to KERN_PROC/kinfo_proc now
that we've got name we want for the API we want

"ZAP!" deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.67 26-Apr-2010 deraadt

more portable code


# 1.66 26-Mar-2010 lum

Make the source of process TIME for displaying and sorting the same.
top(1) and ps(1) now display the same info.
ok tedu@ millert@ otto@ and tested on sparc64 by landry@


Revision tags: OPENBSD_4_7_BASE
# 1.65 29-Jan-2010 tedu

Allow sorting by command and pid. Also, allow partial matches with strncmp.
ok jmc otto


Revision tags: OPENBSD_4_6_BASE
# 1.64 28-Apr-2009 deraadt

extend STATE column to fit > 9 cpus; Aivar Jaakson; PR6131


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.63 01-Nov-2007 otto

bump arbitrary limit on arg length shown by -C; requested by and ok deraadt@


Revision tags: OPENBSD_4_2_BASE
# 1.62 27-Jul-2007 deraadt

replace a few more malloc(n*m) idioms with calloc(n,m) for safety;
inspired by zinovik@cs.karelia.ru


# 1.61 29-May-2007 otto

Instead of using hand-crafted redraws minimizing screen updates, use curses.
Enables nice things like process highlighting without hurting the brain.
ok deraadt@


# 1.60 16-Apr-2007 otto

don't truncate wchan, ok miod@


# 1.59 13-Apr-2007 otto

include cleanup and fix in comment; from Mark Lumsden


# 1.58 04-Apr-2007 otto

missing prototypes; from Mark Lumsden with a twist by me.


Revision tags: OPENBSD_4_1_BASE
# 1.57 04-Feb-2007 otto

long and long long is not the same on 32 bits archs


# 1.56 03-Jan-2007 otto

Add a 'g' command to only show processes having a string in their
command name. ok deraadt@


# 1.55 02-Jan-2007 otto

top(1) doesn't use any math.h function, so do not include math.h and
do not link with libm. ok ray@


# 1.54 29-Nov-2006 miod

Do not test for processes being swapped out since this can't happen anymore.


# 1.53 20-Sep-2006 ray

Recommit memory leak fix.


Revision tags: OPENBSD_4_0_BASE
# 1.52 29-Apr-2006 otto

use calloc(3) instead of malloc(3) to allocate cpu state structs; it's
better style here; plus it prevents bogus data to show up on first display.
report + ok pedro@


Revision tags: OPENBSD_3_9_BASE
# 1.51 21-Dec-2005 millert

Userland programs should include <errno.h> not <sys/errno.h>
OK deraadt@


# 1.50 04-Dec-2005 tedu

support for toggling thread viewing.
ok brad, hints from jmc


Revision tags: OPENBSD_3_8_BASE
# 1.49 17-Jun-2005 markus

add a 'C' command ) that toggles the display of the full cmdline;
with Jared Yanovich; ok deraadt


# 1.48 08-Jun-2005 millert

Multi-cpu support for top. Each cpu now gets its own stats line.
Based on work by todd@ at the hackathon.


Revision tags: OPENBSD_3_7_BASE
# 1.47 06-Dec-2004 markus

missing space; bluhm at genua.de


# 1.46 22-Nov-2004 pat

simplify loop iteration

ok otto millert


# 1.45 17-Nov-2004 markus

off-by-one in process_states[], fixes display of memory; ok deraadt, aaron


# 1.44 07-Oct-2004 otto

introduce -p option and p command to only show a single process.
From Patrick Latifi. ok deraadt@ millert@


Revision tags: OPENBSD_3_6_BASE
# 1.43 13-Jun-2004 otto

Typos in comments and a redundant prototype. From Brian Poole via jmc@
ok jmc@


# 1.42 11-Jun-2004 deraadt

handle the KI_NOCPU case, pointed out by markus


# 1.41 11-Jun-2004 deraadt

only print /# if > 1 cpu on a machine


# 1.40 11-Jun-2004 deraadt

export cpuid via kproc2, and make ps & top aware... from niklas


# 1.39 11-Jun-2004 deraadt

hide top breakage, until the next commit which will do it right


# 1.38 09-May-2004 deraadt

spaces


Revision tags: OPENBSD_3_5_BASE
# 1.37 08-Jan-2004 millert

Convert to kinfo_proc2; ok deraadt@


Revision tags: OPENBSD_3_4_BASE
# 1.36 07-Jul-2003 deraadt

protos


# 1.35 18-Jun-2003 deraadt

remove unused variables


# 1.34 17-Jun-2003 jfb

properly typecast to uid_t the return value of proc_owner() and report
ESRCH instead of EACCES if it returns -1

ok millert@


# 1.33 15-Jun-2003 millert

use uid_t and fix some sign compare warnings; OK krw@ and deraadt@


# 1.32 14-Jun-2003 avsm

use correct length instead of sizeof(char *) for some strl* bound values
millert@ ok


# 1.31 13-Jun-2003 deraadt

readable code


# 1.30 12-Jun-2003 deraadt

knf


# 1.29 12-Jun-2003 pvalchev

cleanup; ok deraadt


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.28 02-Jul-2002 tholo

Add missing copyright


Revision tags: OPENBSD_3_1_BASE
# 1.27 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.26 05-Dec-2001 art

Get maxslp with sysctl. Not the constant.


Revision tags: OPENBSD_3_0_BASE
# 1.25 12-Jul-2001 deraadt

first pass at a -Wall cleanup


# 1.24 03-Jun-2001 angelos

Use KERN_NPROCS to get the number of processes on the system, to
determine the amount of space we'll need to store the
information. The alternative, calling sysctl() with a NULL argument
for data, meant the kernel had to go through the process table.


Revision tags: OPENBSD_2_9_BASE
# 1.23 22-Feb-2001 deraadt

plug mem leak i introduced; spotted by millert


# 1.22 17-Feb-2001 deraadt

bye bye setgid kvm


# 1.21 17-Feb-2001 deraadt

do not use nlist anymore, kvm gets the bullet next


# 1.20 17-Feb-2001 deraadt

partial KNF, because i cannot read the diffs i am trying to debug


Revision tags: OPENBSD_2_8_BASE
# 1.19 18-Jun-2000 niklas

Mark kernel threads as system processes. Make ps hide them by default,
show them with -k. Do not try to show RSS based values for them as they
mess up column alignment. vmstat -f now shows kernel threads separately
from rforks too.


Revision tags: OPENBSD_2_7_BASE
# 1.18 14-Nov-1999 deraadt

stathz instead of hz, plus improvements by me; camield@inet.unisource.nl


# 1.17 29-Oct-1999 todd

writen


Revision tags: OPENBSD_2_6_BASE
# 1.16 21-Jun-1999 art

stop casting the third argument to kvm_read to char *. it is a void *.


# 1.15 22-May-1999 weingart

Update for new vmswap code.


Revision tags: OPENBSD_2_5_BASE
# 1.14 28-Nov-1998 kstailey

do not count swap spaces that were not swapon(2)ed


Revision tags: OPENBSD_2_4_BASE
# 1.13 20-Sep-1998 niklas

cp_time is long, deal with it.


# 1.12 20-Sep-1998 niklas

widen unsigned entities to signed ones before subtracting, fixes sorting on alpha


# 1.11 21-Aug-1998 kstailey

support "-o field" and the interactive "o" command


# 1.10 08-Jul-1998 deraadt

whack kmem gid after kvm_openfiles()


# 1.9 25-Jun-1998 deraadt

handle missing swap


Revision tags: OPENBSD_2_3_BASE
# 1.8 08-Nov-1997 millert

Sort idle processes by total CPU usage, not cpticks since cpticks
get zeroed every second; dan@dan.emsphone.com


Revision tags: OPENBSD_2_2_BASE
# 1.7 09-Sep-1997 millert

"last pid" stuff is useless with random pids.


# 1.6 09-Sep-1997 millert

Use kvm_openfiles() not the sunos compat kvm_open().
Use warn/warnx where it makes sense and check some more ret vals.


# 1.5 24-Aug-1997 millert

Fix compiler warnings.


# 1.4 22-Aug-1997 downsj

Two more alpha warnings.


# 1.3 22-Aug-1997 downsj

First sweep. Prototype, type fixes, long fixes, mostly compiles with
-Wall.


# 1.2 17-Aug-1997 kstailey

print "idle" for idle processes


# 1.1 14-Aug-1997 downsj

top 3.4, with a few changes. Still needs more work.


# 1.101 16-Dec-2019 guenther

Delete tests for P_THREAD that predate the existence of
KERN_PROC_SHOW_THREADS and have been rendered superfluous by it.
Similarly, some P_SYSTEM tests can be deleted or pushed to the
kernel by using KERN_PROC_ALL instead of KERN_PROC_KTHREAD.

ok visa@ mpi@


Revision tags: OPENBSD_6_6_BASE
# 1.100 08-Oct-2019 kn

Simplify threads flag handling

Instead of always passing the threads flag which decides whether other
function arguments are used or not, make callers pass those arguments or
NULL depending on the threads flag.

This reflects better how thread IDs are shown in place of user names/IDs.

OK millert


# 1.99 06-Oct-2019 kn

Avoid gasting around get_process_info()

get_process_info() returns a pointer to the global handle later only be
used in format_next_process(); treat this struct handle as such without
casting the pointer to caddr_t and back again.

No object change.
OK millert deraadt


# 1.98 06-Oct-2019 kn

Zap intermediate structs in compare_*()

SETORDER() can assign from function arguments directly without additional
identical structs in each function.

No object change.
OK millert


# 1.97 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


Revision tags: OPENBSD_6_5_BASE
# 1.96 28-Nov-2018 kn

Allow reverse sort order

Prefixing the field with a dash will sort processes in reverse order.
`o -pid' will therefore list PID 1 on top.

"looks good" tedu, improvements and OK cheloha


# 1.95 17-Nov-2018 cheloha

Add new KERN_CPUSTATS sysctl(2) so we can identify offline CPUs.

Because of hw.smt we need a way to determine whether a given CPU is "online"
or "offline" from userspace. KERN_CPTIME2 is an array, and so cannot be
cleanly extended for this purpose, so add a new sysctl(2) KERN_CPUSTATS
with an extensible struct. At the moment it's just KERN_CPTIME2 with a
flags member, but it can grow as needed.

KERN_CPUSTATS appears to have been defined by BSDi long ago, but there are
few (if any) packages in the wild still using the symbol so breakage in ports
should be near zero. No other system inherited the symbol from BSDi, either.

Then, use the new sysctl(2) in systat(1) and top(1):

- systat(1) draws placeholder marks ('-') instead of percentages for
offline CPUs in the cpu view.

- systat(1) omits offline CPU ticks when drawing the "big bar" in
the vmstat view. The upshot is that the bar isn't half idle when
half your logical CPUs are disabled.

- top(1) does not draw lines for offline CPUs; if CPUs toggle on or
offline in interactive mode we redraw the display to expand/reduce
space for the new/missing CPUs. This is consistent with what some
top(1) implementations do on Linux.

- top(1) omits offline CPUs from the totals when CPU totals are
combined into a single line (the '-1' flag).

Originally prompted by deraadt@. Discussed endlessly with deraadt@,
ketennis@, and sthen@. Tested by jmc@ and jca@. Earlier versions also
discussed with jca@. Earlier versions tested by jmc@, tb@, and many
others.

docs ok jmc@, kernel bits ok ketennis@, everything ok sthen@,
"Is your stuff in yet?" deraadt@


Revision tags: OPENBSD_6_4_BASE
# 1.94 05-Oct-2018 cheloha

Revert KERN_CPTIME2 ENODEV changes in kernel and userspace.

ok kettenis deraadt


# 1.93 26-Sep-2018 cheloha

KERN_CPTIME2: set ENODEV if the CPU is offline.

This lets userspace distinguish between idle CPUs and those that are
not schedulable because hw.smt=0.

A subsequent commit probably needs to add documentation for this
to sysctl.2 (and perhaps elsewhere) after the dust settles.

Also included here are changes to systat(1) and top(1) that account
for the ENODEV case and adjust behavior accordingly:

- systat(1)'s cpu view prints placeholder marks ('-') instead of
percentages for each state if the given CPU is offline.

- systat(1)'s vmstat view checks for offline CPUs when computing the
machine state total and excludes them, so the CPU usage graph
only represents the states for online CPUs.

- top(1) does not draw CPU rows for offline CPUs when the view is
redrawn. If CPUs "go offline", percentages for each state are
replaced by placeholder marks ('-'); the view will need to be
redrawn to remove these rows. If CPUs "go online" the view will
need to be redrawn to show these new CPUs. In "combined CPU" mode,
the count and the state totals only represent online CPUs.

Ports using KERN_CPTIME2 will need to be updated. The changes
described above to make systat(1) and top(1) aware of the ENODEV
case *and* gracefully handle a changing HW_NCPUONLINE while the
application is running are not necessarily appropriate for each
and every port.

The changes described above are so extensive in part to demonstrate
one way a program *might* be made robust to changing CPU availability.
In particular, changing hw.smt after boot is an extremely rare event,
and this needs to be weighed when updating ports.

The logic needed to account for the KERN_CPTIME2 ENODEV case is
very roughly:

if (sysctl(...) == -1) {
if (errno != ENODEV) {
/* Actual error occurred. */
} else {
/* CPU is offline. */
}
} else {
/* CPU is online and CPU states were set by sysctl(2). */
}

Prompted by deraadt@. Basic idea for ENODEV from kettenis@. Discussed at
length with kettenis@. Additional testing by tb@.

No complaints from hackers@ after a week.

ok kettenis@, "I think you should commit [now]" deraadt@


# 1.92 22-Sep-2018 millert

Use user_from_uid() and uid_from_user() directly. The wrappers
in username.c are now so simple there is no longer a good reason
to use them. OK deraadt@


# 1.91 13-Sep-2018 millert

Fix warnings caused by user_from_uid() and group_from_gid() now
returning const char *.


# 1.90 14-May-2018 mpi

Stopping counting and reporting CPU time spent spinning on a lock as
system time.

Introduce a new CP_SPIN "scheduler state" and modify userland tools
to display the % of timer a CPU spents spinning.

Based on a diff from jmatthew@, ok pirofti@, bluhm@, visa@, deraadt@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.89 30-May-2017 tedu

avoid some shadow warnings. from Brian Callahan


Revision tags: OPENBSD_6_1_BASE
# 1.88 15-Mar-2017 deraadt

annoying whitespace die die die


# 1.87 28-Jul-2016 tedu

rework realloc loop. there's no need to shrink the allocation between
calls. if we need a big space once, we'll likely need a big space again.


Revision tags: OPENBSD_6_0_BASE
# 1.86 11-May-2016 edd

Allow top(1) to filter process args if they are being displayed.

OK tedu@, deraadt@, and with input from Michal Mazurek.

Thanks


Revision tags: OPENBSD_5_9_BASE
# 1.85 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_8_BASE
# 1.84 06-May-2015 mpi

Display thread IDs instead of the name of the process's owner when "-H"
is used.

The rationnal is that when you're looking at threads you're generally
already filtereing by PID and this allow you to see which thread is a
pig.

Written some time ago with mikeb@

ok sthen@, krw@, guenther@


Revision tags: OPENBSD_5_7_BASE
# 1.83 19-Jan-2015 millert

Kill loadavg.h and just use the fscale value directly.
There's no need for crazy casts of p_pctcpu in ORDERKEY_PCTCPU
since kinfo_proc exports it as uint32_t. OK deraadt@


# 1.82 19-Jan-2015 deraadt

like in ps(1), fetch the FSCALE value using sysctl rather than using
the header version
ok guenther


# 1.81 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.80 17-Sep-2014 dlg

if there are more than 8 cpus, default to combined cpu stats (like
you passed -1 on the command line).

ok kettenis@ tedu@


# 1.79 15-Sep-2014 miod

Remove non-standard <sys/dkstat.h> header. It has not contained anything
related to disk stastics for almost 17 years, and the remaining
userland-visible defines duplicate those found in <sys/sched.h>.

Move the remaining _KERNEL defines to <sys/tty.h> where they belong, and
update all users to cope with this.

ok kettenis@


Revision tags: OPENBSD_5_6_BASE
# 1.78 04-Jul-2014 guenther

Track whether a process is a zombie or not yet fully built via flags
PS_{ZOMBIE,EMBRYO} on the process instead of peeking into the process's
thread data. This eliminates the need for the thread-level SDEAD state.

Change kvm_getprocs() (both the sysctl() and kvm backends) to report the
"most active" scheduler state for the process's threads.

tweaks kettenis@
feedback and ok matthew@


# 1.77 08-Apr-2014 mpi

Use VM_UVMEXP instead of VM_METER for memory usages and directly
include <sys/vmmeter.h> where it is needed instead of relying on
it being included by <uvm/uvm_extern.h>.

miod@ likes it, ok guenther@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.76 23-Mar-2013 tedu

wchan is no longer exposed, just check if wmesg is not empty.
(wmesg hasn't itself been a pointer since conversion to kinfo_proc.)
noticed by sthen.
ok deraadt jsing millert sthen


Revision tags: OPENBSD_5_3_BASE
# 1.75 18-Dec-2012 millert

We no longer use struct eproc for kinfo_proc in sysctl.h so there
is no direct need for sys/proc.h or sys/resource.h. Some consumers
of kinfo_proc need these for the proc flags and rlimit defines like
RLIM_INF so add the appropriate includes to them.
OK deraadt@ sthen@


Revision tags: OPENBSD_5_2_BASE
# 1.74 09-Jul-2012 deraadt

According to miod, KERNBASE used as a userland symbol should die
in a fire. Start by removing the definitions, before we clean the
headers later.


# 1.73 05-Jun-2012 brynet

Add support for hiding a user's processes in top.

feedback & ok lum@


# 1.72 21-Apr-2012 guenther

Remove a check made superfluous by the KERN_PROC_SHOW_THREADS flag.
This lets new binaries work (at least w/o the -H option) with old kernels.

ok pirofti@ deraadt@


# 1.71 17-Apr-2012 pirofti

Make it optional for kvm_getprocs() (and related sysctl) to return
thread information.

Add a KERN_PROC_SHOW_THREADS flag that has to be set in order to get the
thread info and make it off by default. This creates backwards compatibility
for applications that relied on a given size/number of items to be returned.

Modify ps(1) and top(1) accordingly.

Okay guenther@.


# 1.70 12-Apr-2012 pirofti

Add per thread accounting, mainly for usage & friends.

This expands the already bloated FILL_KPROC macro to take an extra
parameter that indicates if the callee is a thread or a process.

The userland bits are adjusted accordingly and ps(1) and top(1) now
display per thread usage times when -H is used.

Also pkill(1) had to be adjusted so that duplicates don't pop up.

libkvm does basically the same thing as the kernel bits.

Okay guenther@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.69 12-Jul-2011 tedu

print out the amount of memory used for cache. ok otto


# 1.68 10-Apr-2011 guenther

Switch back from KERN_PROC2/kinfo_proc2 to KERN_PROC/kinfo_proc now
that we've got name we want for the API we want

"ZAP!" deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.67 26-Apr-2010 deraadt

more portable code


# 1.66 26-Mar-2010 lum

Make the source of process TIME for displaying and sorting the same.
top(1) and ps(1) now display the same info.
ok tedu@ millert@ otto@ and tested on sparc64 by landry@


Revision tags: OPENBSD_4_7_BASE
# 1.65 29-Jan-2010 tedu

Allow sorting by command and pid. Also, allow partial matches with strncmp.
ok jmc otto


Revision tags: OPENBSD_4_6_BASE
# 1.64 28-Apr-2009 deraadt

extend STATE column to fit > 9 cpus; Aivar Jaakson; PR6131


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.63 01-Nov-2007 otto

bump arbitrary limit on arg length shown by -C; requested by and ok deraadt@


Revision tags: OPENBSD_4_2_BASE
# 1.62 27-Jul-2007 deraadt

replace a few more malloc(n*m) idioms with calloc(n,m) for safety;
inspired by zinovik@cs.karelia.ru


# 1.61 29-May-2007 otto

Instead of using hand-crafted redraws minimizing screen updates, use curses.
Enables nice things like process highlighting without hurting the brain.
ok deraadt@


# 1.60 16-Apr-2007 otto

don't truncate wchan, ok miod@


# 1.59 13-Apr-2007 otto

include cleanup and fix in comment; from Mark Lumsden


# 1.58 04-Apr-2007 otto

missing prototypes; from Mark Lumsden with a twist by me.


Revision tags: OPENBSD_4_1_BASE
# 1.57 04-Feb-2007 otto

long and long long is not the same on 32 bits archs


# 1.56 03-Jan-2007 otto

Add a 'g' command to only show processes having a string in their
command name. ok deraadt@


# 1.55 02-Jan-2007 otto

top(1) doesn't use any math.h function, so do not include math.h and
do not link with libm. ok ray@


# 1.54 29-Nov-2006 miod

Do not test for processes being swapped out since this can't happen anymore.


# 1.53 20-Sep-2006 ray

Recommit memory leak fix.


Revision tags: OPENBSD_4_0_BASE
# 1.52 29-Apr-2006 otto

use calloc(3) instead of malloc(3) to allocate cpu state structs; it's
better style here; plus it prevents bogus data to show up on first display.
report + ok pedro@


Revision tags: OPENBSD_3_9_BASE
# 1.51 21-Dec-2005 millert

Userland programs should include <errno.h> not <sys/errno.h>
OK deraadt@


# 1.50 04-Dec-2005 tedu

support for toggling thread viewing.
ok brad, hints from jmc


Revision tags: OPENBSD_3_8_BASE
# 1.49 17-Jun-2005 markus

add a 'C' command ) that toggles the display of the full cmdline;
with Jared Yanovich; ok deraadt


# 1.48 08-Jun-2005 millert

Multi-cpu support for top. Each cpu now gets its own stats line.
Based on work by todd@ at the hackathon.


Revision tags: OPENBSD_3_7_BASE
# 1.47 06-Dec-2004 markus

missing space; bluhm at genua.de


# 1.46 22-Nov-2004 pat

simplify loop iteration

ok otto millert


# 1.45 17-Nov-2004 markus

off-by-one in process_states[], fixes display of memory; ok deraadt, aaron


# 1.44 07-Oct-2004 otto

introduce -p option and p command to only show a single process.
From Patrick Latifi. ok deraadt@ millert@


Revision tags: OPENBSD_3_6_BASE
# 1.43 13-Jun-2004 otto

Typos in comments and a redundant prototype. From Brian Poole via jmc@
ok jmc@


# 1.42 11-Jun-2004 deraadt

handle the KI_NOCPU case, pointed out by markus


# 1.41 11-Jun-2004 deraadt

only print /# if > 1 cpu on a machine


# 1.40 11-Jun-2004 deraadt

export cpuid via kproc2, and make ps & top aware... from niklas


# 1.39 11-Jun-2004 deraadt

hide top breakage, until the next commit which will do it right


# 1.38 09-May-2004 deraadt

spaces


Revision tags: OPENBSD_3_5_BASE
# 1.37 08-Jan-2004 millert

Convert to kinfo_proc2; ok deraadt@


Revision tags: OPENBSD_3_4_BASE
# 1.36 07-Jul-2003 deraadt

protos


# 1.35 18-Jun-2003 deraadt

remove unused variables


# 1.34 17-Jun-2003 jfb

properly typecast to uid_t the return value of proc_owner() and report
ESRCH instead of EACCES if it returns -1

ok millert@


# 1.33 15-Jun-2003 millert

use uid_t and fix some sign compare warnings; OK krw@ and deraadt@


# 1.32 14-Jun-2003 avsm

use correct length instead of sizeof(char *) for some strl* bound values
millert@ ok


# 1.31 13-Jun-2003 deraadt

readable code


# 1.30 12-Jun-2003 deraadt

knf


# 1.29 12-Jun-2003 pvalchev

cleanup; ok deraadt


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.28 02-Jul-2002 tholo

Add missing copyright


Revision tags: OPENBSD_3_1_BASE
# 1.27 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.26 05-Dec-2001 art

Get maxslp with sysctl. Not the constant.


Revision tags: OPENBSD_3_0_BASE
# 1.25 12-Jul-2001 deraadt

first pass at a -Wall cleanup


# 1.24 03-Jun-2001 angelos

Use KERN_NPROCS to get the number of processes on the system, to
determine the amount of space we'll need to store the
information. The alternative, calling sysctl() with a NULL argument
for data, meant the kernel had to go through the process table.


Revision tags: OPENBSD_2_9_BASE
# 1.23 22-Feb-2001 deraadt

plug mem leak i introduced; spotted by millert


# 1.22 17-Feb-2001 deraadt

bye bye setgid kvm


# 1.21 17-Feb-2001 deraadt

do not use nlist anymore, kvm gets the bullet next


# 1.20 17-Feb-2001 deraadt

partial KNF, because i cannot read the diffs i am trying to debug


Revision tags: OPENBSD_2_8_BASE
# 1.19 18-Jun-2000 niklas

Mark kernel threads as system processes. Make ps hide them by default,
show them with -k. Do not try to show RSS based values for them as they
mess up column alignment. vmstat -f now shows kernel threads separately
from rforks too.


Revision tags: OPENBSD_2_7_BASE
# 1.18 14-Nov-1999 deraadt

stathz instead of hz, plus improvements by me; camield@inet.unisource.nl


# 1.17 29-Oct-1999 todd

writen


Revision tags: OPENBSD_2_6_BASE
# 1.16 21-Jun-1999 art

stop casting the third argument to kvm_read to char *. it is a void *.


# 1.15 22-May-1999 weingart

Update for new vmswap code.


Revision tags: OPENBSD_2_5_BASE
# 1.14 28-Nov-1998 kstailey

do not count swap spaces that were not swapon(2)ed


Revision tags: OPENBSD_2_4_BASE
# 1.13 20-Sep-1998 niklas

cp_time is long, deal with it.


# 1.12 20-Sep-1998 niklas

widen unsigned entities to signed ones before subtracting, fixes sorting on alpha


# 1.11 21-Aug-1998 kstailey

support "-o field" and the interactive "o" command


# 1.10 08-Jul-1998 deraadt

whack kmem gid after kvm_openfiles()


# 1.9 25-Jun-1998 deraadt

handle missing swap


Revision tags: OPENBSD_2_3_BASE
# 1.8 08-Nov-1997 millert

Sort idle processes by total CPU usage, not cpticks since cpticks
get zeroed every second; dan@dan.emsphone.com


Revision tags: OPENBSD_2_2_BASE
# 1.7 09-Sep-1997 millert

"last pid" stuff is useless with random pids.


# 1.6 09-Sep-1997 millert

Use kvm_openfiles() not the sunos compat kvm_open().
Use warn/warnx where it makes sense and check some more ret vals.


# 1.5 24-Aug-1997 millert

Fix compiler warnings.


# 1.4 22-Aug-1997 downsj

Two more alpha warnings.


# 1.3 22-Aug-1997 downsj

First sweep. Prototype, type fixes, long fixes, mostly compiles with
-Wall.


# 1.2 17-Aug-1997 kstailey

print "idle" for idle processes


# 1.1 14-Aug-1997 downsj

top 3.4, with a few changes. Still needs more work.


# 1.100 08-Oct-2019 kn

Simplify threads flag handling

Instead of always passing the threads flag which decides whether other
function arguments are used or not, make callers pass those arguments or
NULL depending on the threads flag.

This reflects better how thread IDs are shown in place of user names/IDs.

OK millert


# 1.99 06-Oct-2019 kn

Avoid gasting around get_process_info()

get_process_info() returns a pointer to the global handle later only be
used in format_next_process(); treat this struct handle as such without
casting the pointer to caddr_t and back again.

No object change.
OK millert deraadt


# 1.98 06-Oct-2019 kn

Zap intermediate structs in compare_*()

SETORDER() can assign from function arguments directly without additional
identical structs in each function.

No object change.
OK millert


# 1.97 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


Revision tags: OPENBSD_6_5_BASE
# 1.96 28-Nov-2018 kn

Allow reverse sort order

Prefixing the field with a dash will sort processes in reverse order.
`o -pid' will therefore list PID 1 on top.

"looks good" tedu, improvements and OK cheloha


# 1.95 17-Nov-2018 cheloha

Add new KERN_CPUSTATS sysctl(2) so we can identify offline CPUs.

Because of hw.smt we need a way to determine whether a given CPU is "online"
or "offline" from userspace. KERN_CPTIME2 is an array, and so cannot be
cleanly extended for this purpose, so add a new sysctl(2) KERN_CPUSTATS
with an extensible struct. At the moment it's just KERN_CPTIME2 with a
flags member, but it can grow as needed.

KERN_CPUSTATS appears to have been defined by BSDi long ago, but there are
few (if any) packages in the wild still using the symbol so breakage in ports
should be near zero. No other system inherited the symbol from BSDi, either.

Then, use the new sysctl(2) in systat(1) and top(1):

- systat(1) draws placeholder marks ('-') instead of percentages for
offline CPUs in the cpu view.

- systat(1) omits offline CPU ticks when drawing the "big bar" in
the vmstat view. The upshot is that the bar isn't half idle when
half your logical CPUs are disabled.

- top(1) does not draw lines for offline CPUs; if CPUs toggle on or
offline in interactive mode we redraw the display to expand/reduce
space for the new/missing CPUs. This is consistent with what some
top(1) implementations do on Linux.

- top(1) omits offline CPUs from the totals when CPU totals are
combined into a single line (the '-1' flag).

Originally prompted by deraadt@. Discussed endlessly with deraadt@,
ketennis@, and sthen@. Tested by jmc@ and jca@. Earlier versions also
discussed with jca@. Earlier versions tested by jmc@, tb@, and many
others.

docs ok jmc@, kernel bits ok ketennis@, everything ok sthen@,
"Is your stuff in yet?" deraadt@


Revision tags: OPENBSD_6_4_BASE
# 1.94 05-Oct-2018 cheloha

Revert KERN_CPTIME2 ENODEV changes in kernel and userspace.

ok kettenis deraadt


# 1.93 26-Sep-2018 cheloha

KERN_CPTIME2: set ENODEV if the CPU is offline.

This lets userspace distinguish between idle CPUs and those that are
not schedulable because hw.smt=0.

A subsequent commit probably needs to add documentation for this
to sysctl.2 (and perhaps elsewhere) after the dust settles.

Also included here are changes to systat(1) and top(1) that account
for the ENODEV case and adjust behavior accordingly:

- systat(1)'s cpu view prints placeholder marks ('-') instead of
percentages for each state if the given CPU is offline.

- systat(1)'s vmstat view checks for offline CPUs when computing the
machine state total and excludes them, so the CPU usage graph
only represents the states for online CPUs.

- top(1) does not draw CPU rows for offline CPUs when the view is
redrawn. If CPUs "go offline", percentages for each state are
replaced by placeholder marks ('-'); the view will need to be
redrawn to remove these rows. If CPUs "go online" the view will
need to be redrawn to show these new CPUs. In "combined CPU" mode,
the count and the state totals only represent online CPUs.

Ports using KERN_CPTIME2 will need to be updated. The changes
described above to make systat(1) and top(1) aware of the ENODEV
case *and* gracefully handle a changing HW_NCPUONLINE while the
application is running are not necessarily appropriate for each
and every port.

The changes described above are so extensive in part to demonstrate
one way a program *might* be made robust to changing CPU availability.
In particular, changing hw.smt after boot is an extremely rare event,
and this needs to be weighed when updating ports.

The logic needed to account for the KERN_CPTIME2 ENODEV case is
very roughly:

if (sysctl(...) == -1) {
if (errno != ENODEV) {
/* Actual error occurred. */
} else {
/* CPU is offline. */
}
} else {
/* CPU is online and CPU states were set by sysctl(2). */
}

Prompted by deraadt@. Basic idea for ENODEV from kettenis@. Discussed at
length with kettenis@. Additional testing by tb@.

No complaints from hackers@ after a week.

ok kettenis@, "I think you should commit [now]" deraadt@


# 1.92 22-Sep-2018 millert

Use user_from_uid() and uid_from_user() directly. The wrappers
in username.c are now so simple there is no longer a good reason
to use them. OK deraadt@


# 1.91 13-Sep-2018 millert

Fix warnings caused by user_from_uid() and group_from_gid() now
returning const char *.


# 1.90 14-May-2018 mpi

Stopping counting and reporting CPU time spent spinning on a lock as
system time.

Introduce a new CP_SPIN "scheduler state" and modify userland tools
to display the % of timer a CPU spents spinning.

Based on a diff from jmatthew@, ok pirofti@, bluhm@, visa@, deraadt@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.89 30-May-2017 tedu

avoid some shadow warnings. from Brian Callahan


Revision tags: OPENBSD_6_1_BASE
# 1.88 15-Mar-2017 deraadt

annoying whitespace die die die


# 1.87 28-Jul-2016 tedu

rework realloc loop. there's no need to shrink the allocation between
calls. if we need a big space once, we'll likely need a big space again.


Revision tags: OPENBSD_6_0_BASE
# 1.86 11-May-2016 edd

Allow top(1) to filter process args if they are being displayed.

OK tedu@, deraadt@, and with input from Michal Mazurek.

Thanks


Revision tags: OPENBSD_5_9_BASE
# 1.85 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_8_BASE
# 1.84 06-May-2015 mpi

Display thread IDs instead of the name of the process's owner when "-H"
is used.

The rationnal is that when you're looking at threads you're generally
already filtereing by PID and this allow you to see which thread is a
pig.

Written some time ago with mikeb@

ok sthen@, krw@, guenther@


Revision tags: OPENBSD_5_7_BASE
# 1.83 19-Jan-2015 millert

Kill loadavg.h and just use the fscale value directly.
There's no need for crazy casts of p_pctcpu in ORDERKEY_PCTCPU
since kinfo_proc exports it as uint32_t. OK deraadt@


# 1.82 19-Jan-2015 deraadt

like in ps(1), fetch the FSCALE value using sysctl rather than using
the header version
ok guenther


# 1.81 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.80 17-Sep-2014 dlg

if there are more than 8 cpus, default to combined cpu stats (like
you passed -1 on the command line).

ok kettenis@ tedu@


# 1.79 15-Sep-2014 miod

Remove non-standard <sys/dkstat.h> header. It has not contained anything
related to disk stastics for almost 17 years, and the remaining
userland-visible defines duplicate those found in <sys/sched.h>.

Move the remaining _KERNEL defines to <sys/tty.h> where they belong, and
update all users to cope with this.

ok kettenis@


Revision tags: OPENBSD_5_6_BASE
# 1.78 04-Jul-2014 guenther

Track whether a process is a zombie or not yet fully built via flags
PS_{ZOMBIE,EMBRYO} on the process instead of peeking into the process's
thread data. This eliminates the need for the thread-level SDEAD state.

Change kvm_getprocs() (both the sysctl() and kvm backends) to report the
"most active" scheduler state for the process's threads.

tweaks kettenis@
feedback and ok matthew@


# 1.77 08-Apr-2014 mpi

Use VM_UVMEXP instead of VM_METER for memory usages and directly
include <sys/vmmeter.h> where it is needed instead of relying on
it being included by <uvm/uvm_extern.h>.

miod@ likes it, ok guenther@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.76 23-Mar-2013 tedu

wchan is no longer exposed, just check if wmesg is not empty.
(wmesg hasn't itself been a pointer since conversion to kinfo_proc.)
noticed by sthen.
ok deraadt jsing millert sthen


Revision tags: OPENBSD_5_3_BASE
# 1.75 18-Dec-2012 millert

We no longer use struct eproc for kinfo_proc in sysctl.h so there
is no direct need for sys/proc.h or sys/resource.h. Some consumers
of kinfo_proc need these for the proc flags and rlimit defines like
RLIM_INF so add the appropriate includes to them.
OK deraadt@ sthen@


Revision tags: OPENBSD_5_2_BASE
# 1.74 09-Jul-2012 deraadt

According to miod, KERNBASE used as a userland symbol should die
in a fire. Start by removing the definitions, before we clean the
headers later.


# 1.73 05-Jun-2012 brynet

Add support for hiding a user's processes in top.

feedback & ok lum@


# 1.72 21-Apr-2012 guenther

Remove a check made superfluous by the KERN_PROC_SHOW_THREADS flag.
This lets new binaries work (at least w/o the -H option) with old kernels.

ok pirofti@ deraadt@


# 1.71 17-Apr-2012 pirofti

Make it optional for kvm_getprocs() (and related sysctl) to return
thread information.

Add a KERN_PROC_SHOW_THREADS flag that has to be set in order to get the
thread info and make it off by default. This creates backwards compatibility
for applications that relied on a given size/number of items to be returned.

Modify ps(1) and top(1) accordingly.

Okay guenther@.


# 1.70 12-Apr-2012 pirofti

Add per thread accounting, mainly for usage & friends.

This expands the already bloated FILL_KPROC macro to take an extra
parameter that indicates if the callee is a thread or a process.

The userland bits are adjusted accordingly and ps(1) and top(1) now
display per thread usage times when -H is used.

Also pkill(1) had to be adjusted so that duplicates don't pop up.

libkvm does basically the same thing as the kernel bits.

Okay guenther@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.69 12-Jul-2011 tedu

print out the amount of memory used for cache. ok otto


# 1.68 10-Apr-2011 guenther

Switch back from KERN_PROC2/kinfo_proc2 to KERN_PROC/kinfo_proc now
that we've got name we want for the API we want

"ZAP!" deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.67 26-Apr-2010 deraadt

more portable code


# 1.66 26-Mar-2010 lum

Make the source of process TIME for displaying and sorting the same.
top(1) and ps(1) now display the same info.
ok tedu@ millert@ otto@ and tested on sparc64 by landry@


Revision tags: OPENBSD_4_7_BASE
# 1.65 29-Jan-2010 tedu

Allow sorting by command and pid. Also, allow partial matches with strncmp.
ok jmc otto


Revision tags: OPENBSD_4_6_BASE
# 1.64 28-Apr-2009 deraadt

extend STATE column to fit > 9 cpus; Aivar Jaakson; PR6131


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.63 01-Nov-2007 otto

bump arbitrary limit on arg length shown by -C; requested by and ok deraadt@


Revision tags: OPENBSD_4_2_BASE
# 1.62 27-Jul-2007 deraadt

replace a few more malloc(n*m) idioms with calloc(n,m) for safety;
inspired by zinovik@cs.karelia.ru


# 1.61 29-May-2007 otto

Instead of using hand-crafted redraws minimizing screen updates, use curses.
Enables nice things like process highlighting without hurting the brain.
ok deraadt@


# 1.60 16-Apr-2007 otto

don't truncate wchan, ok miod@


# 1.59 13-Apr-2007 otto

include cleanup and fix in comment; from Mark Lumsden


# 1.58 04-Apr-2007 otto

missing prototypes; from Mark Lumsden with a twist by me.


Revision tags: OPENBSD_4_1_BASE
# 1.57 04-Feb-2007 otto

long and long long is not the same on 32 bits archs


# 1.56 03-Jan-2007 otto

Add a 'g' command to only show processes having a string in their
command name. ok deraadt@


# 1.55 02-Jan-2007 otto

top(1) doesn't use any math.h function, so do not include math.h and
do not link with libm. ok ray@


# 1.54 29-Nov-2006 miod

Do not test for processes being swapped out since this can't happen anymore.


# 1.53 20-Sep-2006 ray

Recommit memory leak fix.


Revision tags: OPENBSD_4_0_BASE
# 1.52 29-Apr-2006 otto

use calloc(3) instead of malloc(3) to allocate cpu state structs; it's
better style here; plus it prevents bogus data to show up on first display.
report + ok pedro@


Revision tags: OPENBSD_3_9_BASE
# 1.51 21-Dec-2005 millert

Userland programs should include <errno.h> not <sys/errno.h>
OK deraadt@


# 1.50 04-Dec-2005 tedu

support for toggling thread viewing.
ok brad, hints from jmc


Revision tags: OPENBSD_3_8_BASE
# 1.49 17-Jun-2005 markus

add a 'C' command ) that toggles the display of the full cmdline;
with Jared Yanovich; ok deraadt


# 1.48 08-Jun-2005 millert

Multi-cpu support for top. Each cpu now gets its own stats line.
Based on work by todd@ at the hackathon.


Revision tags: OPENBSD_3_7_BASE
# 1.47 06-Dec-2004 markus

missing space; bluhm at genua.de


# 1.46 22-Nov-2004 pat

simplify loop iteration

ok otto millert


# 1.45 17-Nov-2004 markus

off-by-one in process_states[], fixes display of memory; ok deraadt, aaron


# 1.44 07-Oct-2004 otto

introduce -p option and p command to only show a single process.
From Patrick Latifi. ok deraadt@ millert@


Revision tags: OPENBSD_3_6_BASE
# 1.43 13-Jun-2004 otto

Typos in comments and a redundant prototype. From Brian Poole via jmc@
ok jmc@


# 1.42 11-Jun-2004 deraadt

handle the KI_NOCPU case, pointed out by markus


# 1.41 11-Jun-2004 deraadt

only print /# if > 1 cpu on a machine


# 1.40 11-Jun-2004 deraadt

export cpuid via kproc2, and make ps & top aware... from niklas


# 1.39 11-Jun-2004 deraadt

hide top breakage, until the next commit which will do it right


# 1.38 09-May-2004 deraadt

spaces


Revision tags: OPENBSD_3_5_BASE
# 1.37 08-Jan-2004 millert

Convert to kinfo_proc2; ok deraadt@


Revision tags: OPENBSD_3_4_BASE
# 1.36 07-Jul-2003 deraadt

protos


# 1.35 18-Jun-2003 deraadt

remove unused variables


# 1.34 17-Jun-2003 jfb

properly typecast to uid_t the return value of proc_owner() and report
ESRCH instead of EACCES if it returns -1

ok millert@


# 1.33 15-Jun-2003 millert

use uid_t and fix some sign compare warnings; OK krw@ and deraadt@


# 1.32 14-Jun-2003 avsm

use correct length instead of sizeof(char *) for some strl* bound values
millert@ ok


# 1.31 13-Jun-2003 deraadt

readable code


# 1.30 12-Jun-2003 deraadt

knf


# 1.29 12-Jun-2003 pvalchev

cleanup; ok deraadt


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.28 02-Jul-2002 tholo

Add missing copyright


Revision tags: OPENBSD_3_1_BASE
# 1.27 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.26 05-Dec-2001 art

Get maxslp with sysctl. Not the constant.


Revision tags: OPENBSD_3_0_BASE
# 1.25 12-Jul-2001 deraadt

first pass at a -Wall cleanup


# 1.24 03-Jun-2001 angelos

Use KERN_NPROCS to get the number of processes on the system, to
determine the amount of space we'll need to store the
information. The alternative, calling sysctl() with a NULL argument
for data, meant the kernel had to go through the process table.


Revision tags: OPENBSD_2_9_BASE
# 1.23 22-Feb-2001 deraadt

plug mem leak i introduced; spotted by millert


# 1.22 17-Feb-2001 deraadt

bye bye setgid kvm


# 1.21 17-Feb-2001 deraadt

do not use nlist anymore, kvm gets the bullet next


# 1.20 17-Feb-2001 deraadt

partial KNF, because i cannot read the diffs i am trying to debug


Revision tags: OPENBSD_2_8_BASE
# 1.19 18-Jun-2000 niklas

Mark kernel threads as system processes. Make ps hide them by default,
show them with -k. Do not try to show RSS based values for them as they
mess up column alignment. vmstat -f now shows kernel threads separately
from rforks too.


Revision tags: OPENBSD_2_7_BASE
# 1.18 14-Nov-1999 deraadt

stathz instead of hz, plus improvements by me; camield@inet.unisource.nl


# 1.17 29-Oct-1999 todd

writen


Revision tags: OPENBSD_2_6_BASE
# 1.16 21-Jun-1999 art

stop casting the third argument to kvm_read to char *. it is a void *.


# 1.15 22-May-1999 weingart

Update for new vmswap code.


Revision tags: OPENBSD_2_5_BASE
# 1.14 28-Nov-1998 kstailey

do not count swap spaces that were not swapon(2)ed


Revision tags: OPENBSD_2_4_BASE
# 1.13 20-Sep-1998 niklas

cp_time is long, deal with it.


# 1.12 20-Sep-1998 niklas

widen unsigned entities to signed ones before subtracting, fixes sorting on alpha


# 1.11 21-Aug-1998 kstailey

support "-o field" and the interactive "o" command


# 1.10 08-Jul-1998 deraadt

whack kmem gid after kvm_openfiles()


# 1.9 25-Jun-1998 deraadt

handle missing swap


Revision tags: OPENBSD_2_3_BASE
# 1.8 08-Nov-1997 millert

Sort idle processes by total CPU usage, not cpticks since cpticks
get zeroed every second; dan@dan.emsphone.com


Revision tags: OPENBSD_2_2_BASE
# 1.7 09-Sep-1997 millert

"last pid" stuff is useless with random pids.


# 1.6 09-Sep-1997 millert

Use kvm_openfiles() not the sunos compat kvm_open().
Use warn/warnx where it makes sense and check some more ret vals.


# 1.5 24-Aug-1997 millert

Fix compiler warnings.


# 1.4 22-Aug-1997 downsj

Two more alpha warnings.


# 1.3 22-Aug-1997 downsj

First sweep. Prototype, type fixes, long fixes, mostly compiles with
-Wall.


# 1.2 17-Aug-1997 kstailey

print "idle" for idle processes


# 1.1 14-Aug-1997 downsj

top 3.4, with a few changes. Still needs more work.


# 1.99 06-Oct-2019 kn

Avoid gasting around get_process_info()

get_process_info() returns a pointer to the global handle later only be
used in format_next_process(); treat this struct handle as such without
casting the pointer to caddr_t and back again.

No object change.
OK millert deraadt


# 1.98 06-Oct-2019 kn

Zap intermediate structs in compare_*()

SETORDER() can assign from function arguments directly without additional
identical structs in each function.

No object change.
OK millert


# 1.97 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


Revision tags: OPENBSD_6_5_BASE
# 1.96 28-Nov-2018 kn

Allow reverse sort order

Prefixing the field with a dash will sort processes in reverse order.
`o -pid' will therefore list PID 1 on top.

"looks good" tedu, improvements and OK cheloha


# 1.95 17-Nov-2018 cheloha

Add new KERN_CPUSTATS sysctl(2) so we can identify offline CPUs.

Because of hw.smt we need a way to determine whether a given CPU is "online"
or "offline" from userspace. KERN_CPTIME2 is an array, and so cannot be
cleanly extended for this purpose, so add a new sysctl(2) KERN_CPUSTATS
with an extensible struct. At the moment it's just KERN_CPTIME2 with a
flags member, but it can grow as needed.

KERN_CPUSTATS appears to have been defined by BSDi long ago, but there are
few (if any) packages in the wild still using the symbol so breakage in ports
should be near zero. No other system inherited the symbol from BSDi, either.

Then, use the new sysctl(2) in systat(1) and top(1):

- systat(1) draws placeholder marks ('-') instead of percentages for
offline CPUs in the cpu view.

- systat(1) omits offline CPU ticks when drawing the "big bar" in
the vmstat view. The upshot is that the bar isn't half idle when
half your logical CPUs are disabled.

- top(1) does not draw lines for offline CPUs; if CPUs toggle on or
offline in interactive mode we redraw the display to expand/reduce
space for the new/missing CPUs. This is consistent with what some
top(1) implementations do on Linux.

- top(1) omits offline CPUs from the totals when CPU totals are
combined into a single line (the '-1' flag).

Originally prompted by deraadt@. Discussed endlessly with deraadt@,
ketennis@, and sthen@. Tested by jmc@ and jca@. Earlier versions also
discussed with jca@. Earlier versions tested by jmc@, tb@, and many
others.

docs ok jmc@, kernel bits ok ketennis@, everything ok sthen@,
"Is your stuff in yet?" deraadt@


Revision tags: OPENBSD_6_4_BASE
# 1.94 05-Oct-2018 cheloha

Revert KERN_CPTIME2 ENODEV changes in kernel and userspace.

ok kettenis deraadt


# 1.93 26-Sep-2018 cheloha

KERN_CPTIME2: set ENODEV if the CPU is offline.

This lets userspace distinguish between idle CPUs and those that are
not schedulable because hw.smt=0.

A subsequent commit probably needs to add documentation for this
to sysctl.2 (and perhaps elsewhere) after the dust settles.

Also included here are changes to systat(1) and top(1) that account
for the ENODEV case and adjust behavior accordingly:

- systat(1)'s cpu view prints placeholder marks ('-') instead of
percentages for each state if the given CPU is offline.

- systat(1)'s vmstat view checks for offline CPUs when computing the
machine state total and excludes them, so the CPU usage graph
only represents the states for online CPUs.

- top(1) does not draw CPU rows for offline CPUs when the view is
redrawn. If CPUs "go offline", percentages for each state are
replaced by placeholder marks ('-'); the view will need to be
redrawn to remove these rows. If CPUs "go online" the view will
need to be redrawn to show these new CPUs. In "combined CPU" mode,
the count and the state totals only represent online CPUs.

Ports using KERN_CPTIME2 will need to be updated. The changes
described above to make systat(1) and top(1) aware of the ENODEV
case *and* gracefully handle a changing HW_NCPUONLINE while the
application is running are not necessarily appropriate for each
and every port.

The changes described above are so extensive in part to demonstrate
one way a program *might* be made robust to changing CPU availability.
In particular, changing hw.smt after boot is an extremely rare event,
and this needs to be weighed when updating ports.

The logic needed to account for the KERN_CPTIME2 ENODEV case is
very roughly:

if (sysctl(...) == -1) {
if (errno != ENODEV) {
/* Actual error occurred. */
} else {
/* CPU is offline. */
}
} else {
/* CPU is online and CPU states were set by sysctl(2). */
}

Prompted by deraadt@. Basic idea for ENODEV from kettenis@. Discussed at
length with kettenis@. Additional testing by tb@.

No complaints from hackers@ after a week.

ok kettenis@, "I think you should commit [now]" deraadt@


# 1.92 22-Sep-2018 millert

Use user_from_uid() and uid_from_user() directly. The wrappers
in username.c are now so simple there is no longer a good reason
to use them. OK deraadt@


# 1.91 13-Sep-2018 millert

Fix warnings caused by user_from_uid() and group_from_gid() now
returning const char *.


# 1.90 14-May-2018 mpi

Stopping counting and reporting CPU time spent spinning on a lock as
system time.

Introduce a new CP_SPIN "scheduler state" and modify userland tools
to display the % of timer a CPU spents spinning.

Based on a diff from jmatthew@, ok pirofti@, bluhm@, visa@, deraadt@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.89 30-May-2017 tedu

avoid some shadow warnings. from Brian Callahan


Revision tags: OPENBSD_6_1_BASE
# 1.88 15-Mar-2017 deraadt

annoying whitespace die die die


# 1.87 28-Jul-2016 tedu

rework realloc loop. there's no need to shrink the allocation between
calls. if we need a big space once, we'll likely need a big space again.


Revision tags: OPENBSD_6_0_BASE
# 1.86 11-May-2016 edd

Allow top(1) to filter process args if they are being displayed.

OK tedu@, deraadt@, and with input from Michal Mazurek.

Thanks


Revision tags: OPENBSD_5_9_BASE
# 1.85 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_8_BASE
# 1.84 06-May-2015 mpi

Display thread IDs instead of the name of the process's owner when "-H"
is used.

The rationnal is that when you're looking at threads you're generally
already filtereing by PID and this allow you to see which thread is a
pig.

Written some time ago with mikeb@

ok sthen@, krw@, guenther@


Revision tags: OPENBSD_5_7_BASE
# 1.83 19-Jan-2015 millert

Kill loadavg.h and just use the fscale value directly.
There's no need for crazy casts of p_pctcpu in ORDERKEY_PCTCPU
since kinfo_proc exports it as uint32_t. OK deraadt@


# 1.82 19-Jan-2015 deraadt

like in ps(1), fetch the FSCALE value using sysctl rather than using
the header version
ok guenther


# 1.81 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.80 17-Sep-2014 dlg

if there are more than 8 cpus, default to combined cpu stats (like
you passed -1 on the command line).

ok kettenis@ tedu@


# 1.79 15-Sep-2014 miod

Remove non-standard <sys/dkstat.h> header. It has not contained anything
related to disk stastics for almost 17 years, and the remaining
userland-visible defines duplicate those found in <sys/sched.h>.

Move the remaining _KERNEL defines to <sys/tty.h> where they belong, and
update all users to cope with this.

ok kettenis@


Revision tags: OPENBSD_5_6_BASE
# 1.78 04-Jul-2014 guenther

Track whether a process is a zombie or not yet fully built via flags
PS_{ZOMBIE,EMBRYO} on the process instead of peeking into the process's
thread data. This eliminates the need for the thread-level SDEAD state.

Change kvm_getprocs() (both the sysctl() and kvm backends) to report the
"most active" scheduler state for the process's threads.

tweaks kettenis@
feedback and ok matthew@


# 1.77 08-Apr-2014 mpi

Use VM_UVMEXP instead of VM_METER for memory usages and directly
include <sys/vmmeter.h> where it is needed instead of relying on
it being included by <uvm/uvm_extern.h>.

miod@ likes it, ok guenther@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.76 23-Mar-2013 tedu

wchan is no longer exposed, just check if wmesg is not empty.
(wmesg hasn't itself been a pointer since conversion to kinfo_proc.)
noticed by sthen.
ok deraadt jsing millert sthen


Revision tags: OPENBSD_5_3_BASE
# 1.75 18-Dec-2012 millert

We no longer use struct eproc for kinfo_proc in sysctl.h so there
is no direct need for sys/proc.h or sys/resource.h. Some consumers
of kinfo_proc need these for the proc flags and rlimit defines like
RLIM_INF so add the appropriate includes to them.
OK deraadt@ sthen@


Revision tags: OPENBSD_5_2_BASE
# 1.74 09-Jul-2012 deraadt

According to miod, KERNBASE used as a userland symbol should die
in a fire. Start by removing the definitions, before we clean the
headers later.


# 1.73 05-Jun-2012 brynet

Add support for hiding a user's processes in top.

feedback & ok lum@


# 1.72 21-Apr-2012 guenther

Remove a check made superfluous by the KERN_PROC_SHOW_THREADS flag.
This lets new binaries work (at least w/o the -H option) with old kernels.

ok pirofti@ deraadt@


# 1.71 17-Apr-2012 pirofti

Make it optional for kvm_getprocs() (and related sysctl) to return
thread information.

Add a KERN_PROC_SHOW_THREADS flag that has to be set in order to get the
thread info and make it off by default. This creates backwards compatibility
for applications that relied on a given size/number of items to be returned.

Modify ps(1) and top(1) accordingly.

Okay guenther@.


# 1.70 12-Apr-2012 pirofti

Add per thread accounting, mainly for usage & friends.

This expands the already bloated FILL_KPROC macro to take an extra
parameter that indicates if the callee is a thread or a process.

The userland bits are adjusted accordingly and ps(1) and top(1) now
display per thread usage times when -H is used.

Also pkill(1) had to be adjusted so that duplicates don't pop up.

libkvm does basically the same thing as the kernel bits.

Okay guenther@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.69 12-Jul-2011 tedu

print out the amount of memory used for cache. ok otto


# 1.68 10-Apr-2011 guenther

Switch back from KERN_PROC2/kinfo_proc2 to KERN_PROC/kinfo_proc now
that we've got name we want for the API we want

"ZAP!" deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.67 26-Apr-2010 deraadt

more portable code


# 1.66 26-Mar-2010 lum

Make the source of process TIME for displaying and sorting the same.
top(1) and ps(1) now display the same info.
ok tedu@ millert@ otto@ and tested on sparc64 by landry@


Revision tags: OPENBSD_4_7_BASE
# 1.65 29-Jan-2010 tedu

Allow sorting by command and pid. Also, allow partial matches with strncmp.
ok jmc otto


Revision tags: OPENBSD_4_6_BASE
# 1.64 28-Apr-2009 deraadt

extend STATE column to fit > 9 cpus; Aivar Jaakson; PR6131


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.63 01-Nov-2007 otto

bump arbitrary limit on arg length shown by -C; requested by and ok deraadt@


Revision tags: OPENBSD_4_2_BASE
# 1.62 27-Jul-2007 deraadt

replace a few more malloc(n*m) idioms with calloc(n,m) for safety;
inspired by zinovik@cs.karelia.ru


# 1.61 29-May-2007 otto

Instead of using hand-crafted redraws minimizing screen updates, use curses.
Enables nice things like process highlighting without hurting the brain.
ok deraadt@


# 1.60 16-Apr-2007 otto

don't truncate wchan, ok miod@


# 1.59 13-Apr-2007 otto

include cleanup and fix in comment; from Mark Lumsden


# 1.58 04-Apr-2007 otto

missing prototypes; from Mark Lumsden with a twist by me.


Revision tags: OPENBSD_4_1_BASE
# 1.57 04-Feb-2007 otto

long and long long is not the same on 32 bits archs


# 1.56 03-Jan-2007 otto

Add a 'g' command to only show processes having a string in their
command name. ok deraadt@


# 1.55 02-Jan-2007 otto

top(1) doesn't use any math.h function, so do not include math.h and
do not link with libm. ok ray@


# 1.54 29-Nov-2006 miod

Do not test for processes being swapped out since this can't happen anymore.


# 1.53 20-Sep-2006 ray

Recommit memory leak fix.


Revision tags: OPENBSD_4_0_BASE
# 1.52 29-Apr-2006 otto

use calloc(3) instead of malloc(3) to allocate cpu state structs; it's
better style here; plus it prevents bogus data to show up on first display.
report + ok pedro@


Revision tags: OPENBSD_3_9_BASE
# 1.51 21-Dec-2005 millert

Userland programs should include <errno.h> not <sys/errno.h>
OK deraadt@


# 1.50 04-Dec-2005 tedu

support for toggling thread viewing.
ok brad, hints from jmc


Revision tags: OPENBSD_3_8_BASE
# 1.49 17-Jun-2005 markus

add a 'C' command ) that toggles the display of the full cmdline;
with Jared Yanovich; ok deraadt


# 1.48 08-Jun-2005 millert

Multi-cpu support for top. Each cpu now gets its own stats line.
Based on work by todd@ at the hackathon.


Revision tags: OPENBSD_3_7_BASE
# 1.47 06-Dec-2004 markus

missing space; bluhm at genua.de


# 1.46 22-Nov-2004 pat

simplify loop iteration

ok otto millert


# 1.45 17-Nov-2004 markus

off-by-one in process_states[], fixes display of memory; ok deraadt, aaron


# 1.44 07-Oct-2004 otto

introduce -p option and p command to only show a single process.
From Patrick Latifi. ok deraadt@ millert@


Revision tags: OPENBSD_3_6_BASE
# 1.43 13-Jun-2004 otto

Typos in comments and a redundant prototype. From Brian Poole via jmc@
ok jmc@


# 1.42 11-Jun-2004 deraadt

handle the KI_NOCPU case, pointed out by markus


# 1.41 11-Jun-2004 deraadt

only print /# if > 1 cpu on a machine


# 1.40 11-Jun-2004 deraadt

export cpuid via kproc2, and make ps & top aware... from niklas


# 1.39 11-Jun-2004 deraadt

hide top breakage, until the next commit which will do it right


# 1.38 09-May-2004 deraadt

spaces


Revision tags: OPENBSD_3_5_BASE
# 1.37 08-Jan-2004 millert

Convert to kinfo_proc2; ok deraadt@


Revision tags: OPENBSD_3_4_BASE
# 1.36 07-Jul-2003 deraadt

protos


# 1.35 18-Jun-2003 deraadt

remove unused variables


# 1.34 17-Jun-2003 jfb

properly typecast to uid_t the return value of proc_owner() and report
ESRCH instead of EACCES if it returns -1

ok millert@


# 1.33 15-Jun-2003 millert

use uid_t and fix some sign compare warnings; OK krw@ and deraadt@


# 1.32 14-Jun-2003 avsm

use correct length instead of sizeof(char *) for some strl* bound values
millert@ ok


# 1.31 13-Jun-2003 deraadt

readable code


# 1.30 12-Jun-2003 deraadt

knf


# 1.29 12-Jun-2003 pvalchev

cleanup; ok deraadt


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.28 02-Jul-2002 tholo

Add missing copyright


Revision tags: OPENBSD_3_1_BASE
# 1.27 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.26 05-Dec-2001 art

Get maxslp with sysctl. Not the constant.


Revision tags: OPENBSD_3_0_BASE
# 1.25 12-Jul-2001 deraadt

first pass at a -Wall cleanup


# 1.24 03-Jun-2001 angelos

Use KERN_NPROCS to get the number of processes on the system, to
determine the amount of space we'll need to store the
information. The alternative, calling sysctl() with a NULL argument
for data, meant the kernel had to go through the process table.


Revision tags: OPENBSD_2_9_BASE
# 1.23 22-Feb-2001 deraadt

plug mem leak i introduced; spotted by millert


# 1.22 17-Feb-2001 deraadt

bye bye setgid kvm


# 1.21 17-Feb-2001 deraadt

do not use nlist anymore, kvm gets the bullet next


# 1.20 17-Feb-2001 deraadt

partial KNF, because i cannot read the diffs i am trying to debug


Revision tags: OPENBSD_2_8_BASE
# 1.19 18-Jun-2000 niklas

Mark kernel threads as system processes. Make ps hide them by default,
show them with -k. Do not try to show RSS based values for them as they
mess up column alignment. vmstat -f now shows kernel threads separately
from rforks too.


Revision tags: OPENBSD_2_7_BASE
# 1.18 14-Nov-1999 deraadt

stathz instead of hz, plus improvements by me; camield@inet.unisource.nl


# 1.17 29-Oct-1999 todd

writen


Revision tags: OPENBSD_2_6_BASE
# 1.16 21-Jun-1999 art

stop casting the third argument to kvm_read to char *. it is a void *.


# 1.15 22-May-1999 weingart

Update for new vmswap code.


Revision tags: OPENBSD_2_5_BASE
# 1.14 28-Nov-1998 kstailey

do not count swap spaces that were not swapon(2)ed


Revision tags: OPENBSD_2_4_BASE
# 1.13 20-Sep-1998 niklas

cp_time is long, deal with it.


# 1.12 20-Sep-1998 niklas

widen unsigned entities to signed ones before subtracting, fixes sorting on alpha


# 1.11 21-Aug-1998 kstailey

support "-o field" and the interactive "o" command


# 1.10 08-Jul-1998 deraadt

whack kmem gid after kvm_openfiles()


# 1.9 25-Jun-1998 deraadt

handle missing swap


Revision tags: OPENBSD_2_3_BASE
# 1.8 08-Nov-1997 millert

Sort idle processes by total CPU usage, not cpticks since cpticks
get zeroed every second; dan@dan.emsphone.com


Revision tags: OPENBSD_2_2_BASE
# 1.7 09-Sep-1997 millert

"last pid" stuff is useless with random pids.


# 1.6 09-Sep-1997 millert

Use kvm_openfiles() not the sunos compat kvm_open().
Use warn/warnx where it makes sense and check some more ret vals.


# 1.5 24-Aug-1997 millert

Fix compiler warnings.


# 1.4 22-Aug-1997 downsj

Two more alpha warnings.


# 1.3 22-Aug-1997 downsj

First sweep. Prototype, type fixes, long fixes, mostly compiles with
-Wall.


# 1.2 17-Aug-1997 kstailey

print "idle" for idle processes


# 1.1 14-Aug-1997 downsj

top 3.4, with a few changes. Still needs more work.


# 1.97 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


Revision tags: OPENBSD_6_5_BASE
# 1.96 28-Nov-2018 kn

Allow reverse sort order

Prefixing the field with a dash will sort processes in reverse order.
`o -pid' will therefore list PID 1 on top.

"looks good" tedu, improvements and OK cheloha


# 1.95 17-Nov-2018 cheloha

Add new KERN_CPUSTATS sysctl(2) so we can identify offline CPUs.

Because of hw.smt we need a way to determine whether a given CPU is "online"
or "offline" from userspace. KERN_CPTIME2 is an array, and so cannot be
cleanly extended for this purpose, so add a new sysctl(2) KERN_CPUSTATS
with an extensible struct. At the moment it's just KERN_CPTIME2 with a
flags member, but it can grow as needed.

KERN_CPUSTATS appears to have been defined by BSDi long ago, but there are
few (if any) packages in the wild still using the symbol so breakage in ports
should be near zero. No other system inherited the symbol from BSDi, either.

Then, use the new sysctl(2) in systat(1) and top(1):

- systat(1) draws placeholder marks ('-') instead of percentages for
offline CPUs in the cpu view.

- systat(1) omits offline CPU ticks when drawing the "big bar" in
the vmstat view. The upshot is that the bar isn't half idle when
half your logical CPUs are disabled.

- top(1) does not draw lines for offline CPUs; if CPUs toggle on or
offline in interactive mode we redraw the display to expand/reduce
space for the new/missing CPUs. This is consistent with what some
top(1) implementations do on Linux.

- top(1) omits offline CPUs from the totals when CPU totals are
combined into a single line (the '-1' flag).

Originally prompted by deraadt@. Discussed endlessly with deraadt@,
ketennis@, and sthen@. Tested by jmc@ and jca@. Earlier versions also
discussed with jca@. Earlier versions tested by jmc@, tb@, and many
others.

docs ok jmc@, kernel bits ok ketennis@, everything ok sthen@,
"Is your stuff in yet?" deraadt@


Revision tags: OPENBSD_6_4_BASE
# 1.94 05-Oct-2018 cheloha

Revert KERN_CPTIME2 ENODEV changes in kernel and userspace.

ok kettenis deraadt


# 1.93 26-Sep-2018 cheloha

KERN_CPTIME2: set ENODEV if the CPU is offline.

This lets userspace distinguish between idle CPUs and those that are
not schedulable because hw.smt=0.

A subsequent commit probably needs to add documentation for this
to sysctl.2 (and perhaps elsewhere) after the dust settles.

Also included here are changes to systat(1) and top(1) that account
for the ENODEV case and adjust behavior accordingly:

- systat(1)'s cpu view prints placeholder marks ('-') instead of
percentages for each state if the given CPU is offline.

- systat(1)'s vmstat view checks for offline CPUs when computing the
machine state total and excludes them, so the CPU usage graph
only represents the states for online CPUs.

- top(1) does not draw CPU rows for offline CPUs when the view is
redrawn. If CPUs "go offline", percentages for each state are
replaced by placeholder marks ('-'); the view will need to be
redrawn to remove these rows. If CPUs "go online" the view will
need to be redrawn to show these new CPUs. In "combined CPU" mode,
the count and the state totals only represent online CPUs.

Ports using KERN_CPTIME2 will need to be updated. The changes
described above to make systat(1) and top(1) aware of the ENODEV
case *and* gracefully handle a changing HW_NCPUONLINE while the
application is running are not necessarily appropriate for each
and every port.

The changes described above are so extensive in part to demonstrate
one way a program *might* be made robust to changing CPU availability.
In particular, changing hw.smt after boot is an extremely rare event,
and this needs to be weighed when updating ports.

The logic needed to account for the KERN_CPTIME2 ENODEV case is
very roughly:

if (sysctl(...) == -1) {
if (errno != ENODEV) {
/* Actual error occurred. */
} else {
/* CPU is offline. */
}
} else {
/* CPU is online and CPU states were set by sysctl(2). */
}

Prompted by deraadt@. Basic idea for ENODEV from kettenis@. Discussed at
length with kettenis@. Additional testing by tb@.

No complaints from hackers@ after a week.

ok kettenis@, "I think you should commit [now]" deraadt@


# 1.92 22-Sep-2018 millert

Use user_from_uid() and uid_from_user() directly. The wrappers
in username.c are now so simple there is no longer a good reason
to use them. OK deraadt@


# 1.91 13-Sep-2018 millert

Fix warnings caused by user_from_uid() and group_from_gid() now
returning const char *.


# 1.90 14-May-2018 mpi

Stopping counting and reporting CPU time spent spinning on a lock as
system time.

Introduce a new CP_SPIN "scheduler state" and modify userland tools
to display the % of timer a CPU spents spinning.

Based on a diff from jmatthew@, ok pirofti@, bluhm@, visa@, deraadt@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.89 30-May-2017 tedu

avoid some shadow warnings. from Brian Callahan


Revision tags: OPENBSD_6_1_BASE
# 1.88 15-Mar-2017 deraadt

annoying whitespace die die die


# 1.87 28-Jul-2016 tedu

rework realloc loop. there's no need to shrink the allocation between
calls. if we need a big space once, we'll likely need a big space again.


Revision tags: OPENBSD_6_0_BASE
# 1.86 11-May-2016 edd

Allow top(1) to filter process args if they are being displayed.

OK tedu@, deraadt@, and with input from Michal Mazurek.

Thanks


Revision tags: OPENBSD_5_9_BASE
# 1.85 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_8_BASE
# 1.84 06-May-2015 mpi

Display thread IDs instead of the name of the process's owner when "-H"
is used.

The rationnal is that when you're looking at threads you're generally
already filtereing by PID and this allow you to see which thread is a
pig.

Written some time ago with mikeb@

ok sthen@, krw@, guenther@


Revision tags: OPENBSD_5_7_BASE
# 1.83 19-Jan-2015 millert

Kill loadavg.h and just use the fscale value directly.
There's no need for crazy casts of p_pctcpu in ORDERKEY_PCTCPU
since kinfo_proc exports it as uint32_t. OK deraadt@


# 1.82 19-Jan-2015 deraadt

like in ps(1), fetch the FSCALE value using sysctl rather than using
the header version
ok guenther


# 1.81 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.80 17-Sep-2014 dlg

if there are more than 8 cpus, default to combined cpu stats (like
you passed -1 on the command line).

ok kettenis@ tedu@


# 1.79 15-Sep-2014 miod

Remove non-standard <sys/dkstat.h> header. It has not contained anything
related to disk stastics for almost 17 years, and the remaining
userland-visible defines duplicate those found in <sys/sched.h>.

Move the remaining _KERNEL defines to <sys/tty.h> where they belong, and
update all users to cope with this.

ok kettenis@


Revision tags: OPENBSD_5_6_BASE
# 1.78 04-Jul-2014 guenther

Track whether a process is a zombie or not yet fully built via flags
PS_{ZOMBIE,EMBRYO} on the process instead of peeking into the process's
thread data. This eliminates the need for the thread-level SDEAD state.

Change kvm_getprocs() (both the sysctl() and kvm backends) to report the
"most active" scheduler state for the process's threads.

tweaks kettenis@
feedback and ok matthew@


# 1.77 08-Apr-2014 mpi

Use VM_UVMEXP instead of VM_METER for memory usages and directly
include <sys/vmmeter.h> where it is needed instead of relying on
it being included by <uvm/uvm_extern.h>.

miod@ likes it, ok guenther@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.76 23-Mar-2013 tedu

wchan is no longer exposed, just check if wmesg is not empty.
(wmesg hasn't itself been a pointer since conversion to kinfo_proc.)
noticed by sthen.
ok deraadt jsing millert sthen


Revision tags: OPENBSD_5_3_BASE
# 1.75 18-Dec-2012 millert

We no longer use struct eproc for kinfo_proc in sysctl.h so there
is no direct need for sys/proc.h or sys/resource.h. Some consumers
of kinfo_proc need these for the proc flags and rlimit defines like
RLIM_INF so add the appropriate includes to them.
OK deraadt@ sthen@


Revision tags: OPENBSD_5_2_BASE
# 1.74 09-Jul-2012 deraadt

According to miod, KERNBASE used as a userland symbol should die
in a fire. Start by removing the definitions, before we clean the
headers later.


# 1.73 05-Jun-2012 brynet

Add support for hiding a user's processes in top.

feedback & ok lum@


# 1.72 21-Apr-2012 guenther

Remove a check made superfluous by the KERN_PROC_SHOW_THREADS flag.
This lets new binaries work (at least w/o the -H option) with old kernels.

ok pirofti@ deraadt@


# 1.71 17-Apr-2012 pirofti

Make it optional for kvm_getprocs() (and related sysctl) to return
thread information.

Add a KERN_PROC_SHOW_THREADS flag that has to be set in order to get the
thread info and make it off by default. This creates backwards compatibility
for applications that relied on a given size/number of items to be returned.

Modify ps(1) and top(1) accordingly.

Okay guenther@.


# 1.70 12-Apr-2012 pirofti

Add per thread accounting, mainly for usage & friends.

This expands the already bloated FILL_KPROC macro to take an extra
parameter that indicates if the callee is a thread or a process.

The userland bits are adjusted accordingly and ps(1) and top(1) now
display per thread usage times when -H is used.

Also pkill(1) had to be adjusted so that duplicates don't pop up.

libkvm does basically the same thing as the kernel bits.

Okay guenther@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.69 12-Jul-2011 tedu

print out the amount of memory used for cache. ok otto


# 1.68 10-Apr-2011 guenther

Switch back from KERN_PROC2/kinfo_proc2 to KERN_PROC/kinfo_proc now
that we've got name we want for the API we want

"ZAP!" deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.67 26-Apr-2010 deraadt

more portable code


# 1.66 26-Mar-2010 lum

Make the source of process TIME for displaying and sorting the same.
top(1) and ps(1) now display the same info.
ok tedu@ millert@ otto@ and tested on sparc64 by landry@


Revision tags: OPENBSD_4_7_BASE
# 1.65 29-Jan-2010 tedu

Allow sorting by command and pid. Also, allow partial matches with strncmp.
ok jmc otto


Revision tags: OPENBSD_4_6_BASE
# 1.64 28-Apr-2009 deraadt

extend STATE column to fit > 9 cpus; Aivar Jaakson; PR6131


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.63 01-Nov-2007 otto

bump arbitrary limit on arg length shown by -C; requested by and ok deraadt@


Revision tags: OPENBSD_4_2_BASE
# 1.62 27-Jul-2007 deraadt

replace a few more malloc(n*m) idioms with calloc(n,m) for safety;
inspired by zinovik@cs.karelia.ru


# 1.61 29-May-2007 otto

Instead of using hand-crafted redraws minimizing screen updates, use curses.
Enables nice things like process highlighting without hurting the brain.
ok deraadt@


# 1.60 16-Apr-2007 otto

don't truncate wchan, ok miod@


# 1.59 13-Apr-2007 otto

include cleanup and fix in comment; from Mark Lumsden


# 1.58 04-Apr-2007 otto

missing prototypes; from Mark Lumsden with a twist by me.


Revision tags: OPENBSD_4_1_BASE
# 1.57 04-Feb-2007 otto

long and long long is not the same on 32 bits archs


# 1.56 03-Jan-2007 otto

Add a 'g' command to only show processes having a string in their
command name. ok deraadt@


# 1.55 02-Jan-2007 otto

top(1) doesn't use any math.h function, so do not include math.h and
do not link with libm. ok ray@


# 1.54 29-Nov-2006 miod

Do not test for processes being swapped out since this can't happen anymore.


# 1.53 20-Sep-2006 ray

Recommit memory leak fix.


Revision tags: OPENBSD_4_0_BASE
# 1.52 29-Apr-2006 otto

use calloc(3) instead of malloc(3) to allocate cpu state structs; it's
better style here; plus it prevents bogus data to show up on first display.
report + ok pedro@


Revision tags: OPENBSD_3_9_BASE
# 1.51 21-Dec-2005 millert

Userland programs should include <errno.h> not <sys/errno.h>
OK deraadt@


# 1.50 04-Dec-2005 tedu

support for toggling thread viewing.
ok brad, hints from jmc


Revision tags: OPENBSD_3_8_BASE
# 1.49 17-Jun-2005 markus

add a 'C' command ) that toggles the display of the full cmdline;
with Jared Yanovich; ok deraadt


# 1.48 08-Jun-2005 millert

Multi-cpu support for top. Each cpu now gets its own stats line.
Based on work by todd@ at the hackathon.


Revision tags: OPENBSD_3_7_BASE
# 1.47 06-Dec-2004 markus

missing space; bluhm at genua.de


# 1.46 22-Nov-2004 pat

simplify loop iteration

ok otto millert


# 1.45 17-Nov-2004 markus

off-by-one in process_states[], fixes display of memory; ok deraadt, aaron


# 1.44 07-Oct-2004 otto

introduce -p option and p command to only show a single process.
From Patrick Latifi. ok deraadt@ millert@


Revision tags: OPENBSD_3_6_BASE
# 1.43 13-Jun-2004 otto

Typos in comments and a redundant prototype. From Brian Poole via jmc@
ok jmc@


# 1.42 11-Jun-2004 deraadt

handle the KI_NOCPU case, pointed out by markus


# 1.41 11-Jun-2004 deraadt

only print /# if > 1 cpu on a machine


# 1.40 11-Jun-2004 deraadt

export cpuid via kproc2, and make ps & top aware... from niklas


# 1.39 11-Jun-2004 deraadt

hide top breakage, until the next commit which will do it right


# 1.38 09-May-2004 deraadt

spaces


Revision tags: OPENBSD_3_5_BASE
# 1.37 08-Jan-2004 millert

Convert to kinfo_proc2; ok deraadt@


Revision tags: OPENBSD_3_4_BASE
# 1.36 07-Jul-2003 deraadt

protos


# 1.35 18-Jun-2003 deraadt

remove unused variables


# 1.34 17-Jun-2003 jfb

properly typecast to uid_t the return value of proc_owner() and report
ESRCH instead of EACCES if it returns -1

ok millert@


# 1.33 15-Jun-2003 millert

use uid_t and fix some sign compare warnings; OK krw@ and deraadt@


# 1.32 14-Jun-2003 avsm

use correct length instead of sizeof(char *) for some strl* bound values
millert@ ok


# 1.31 13-Jun-2003 deraadt

readable code


# 1.30 12-Jun-2003 deraadt

knf


# 1.29 12-Jun-2003 pvalchev

cleanup; ok deraadt


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.28 02-Jul-2002 tholo

Add missing copyright


Revision tags: OPENBSD_3_1_BASE
# 1.27 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.26 05-Dec-2001 art

Get maxslp with sysctl. Not the constant.


Revision tags: OPENBSD_3_0_BASE
# 1.25 12-Jul-2001 deraadt

first pass at a -Wall cleanup


# 1.24 03-Jun-2001 angelos

Use KERN_NPROCS to get the number of processes on the system, to
determine the amount of space we'll need to store the
information. The alternative, calling sysctl() with a NULL argument
for data, meant the kernel had to go through the process table.


Revision tags: OPENBSD_2_9_BASE
# 1.23 22-Feb-2001 deraadt

plug mem leak i introduced; spotted by millert


# 1.22 17-Feb-2001 deraadt

bye bye setgid kvm


# 1.21 17-Feb-2001 deraadt

do not use nlist anymore, kvm gets the bullet next


# 1.20 17-Feb-2001 deraadt

partial KNF, because i cannot read the diffs i am trying to debug


Revision tags: OPENBSD_2_8_BASE
# 1.19 18-Jun-2000 niklas

Mark kernel threads as system processes. Make ps hide them by default,
show them with -k. Do not try to show RSS based values for them as they
mess up column alignment. vmstat -f now shows kernel threads separately
from rforks too.


Revision tags: OPENBSD_2_7_BASE
# 1.18 14-Nov-1999 deraadt

stathz instead of hz, plus improvements by me; camield@inet.unisource.nl


# 1.17 29-Oct-1999 todd

writen


Revision tags: OPENBSD_2_6_BASE
# 1.16 21-Jun-1999 art

stop casting the third argument to kvm_read to char *. it is a void *.


# 1.15 22-May-1999 weingart

Update for new vmswap code.


Revision tags: OPENBSD_2_5_BASE
# 1.14 28-Nov-1998 kstailey

do not count swap spaces that were not swapon(2)ed


Revision tags: OPENBSD_2_4_BASE
# 1.13 20-Sep-1998 niklas

cp_time is long, deal with it.


# 1.12 20-Sep-1998 niklas

widen unsigned entities to signed ones before subtracting, fixes sorting on alpha


# 1.11 21-Aug-1998 kstailey

support "-o field" and the interactive "o" command


# 1.10 08-Jul-1998 deraadt

whack kmem gid after kvm_openfiles()


# 1.9 25-Jun-1998 deraadt

handle missing swap


Revision tags: OPENBSD_2_3_BASE
# 1.8 08-Nov-1997 millert

Sort idle processes by total CPU usage, not cpticks since cpticks
get zeroed every second; dan@dan.emsphone.com


Revision tags: OPENBSD_2_2_BASE
# 1.7 09-Sep-1997 millert

"last pid" stuff is useless with random pids.


# 1.6 09-Sep-1997 millert

Use kvm_openfiles() not the sunos compat kvm_open().
Use warn/warnx where it makes sense and check some more ret vals.


# 1.5 24-Aug-1997 millert

Fix compiler warnings.


# 1.4 22-Aug-1997 downsj

Two more alpha warnings.


# 1.3 22-Aug-1997 downsj

First sweep. Prototype, type fixes, long fixes, mostly compiles with
-Wall.


# 1.2 17-Aug-1997 kstailey

print "idle" for idle processes


# 1.1 14-Aug-1997 downsj

top 3.4, with a few changes. Still needs more work.


# 1.96 28-Nov-2018 kn

Allow reverse sort order

Prefixing the field with a dash will sort processes in reverse order.
`o -pid' will therefore list PID 1 on top.

"looks good" tedu, improvements and OK cheloha


# 1.95 17-Nov-2018 cheloha

Add new KERN_CPUSTATS sysctl(2) so we can identify offline CPUs.

Because of hw.smt we need a way to determine whether a given CPU is "online"
or "offline" from userspace. KERN_CPTIME2 is an array, and so cannot be
cleanly extended for this purpose, so add a new sysctl(2) KERN_CPUSTATS
with an extensible struct. At the moment it's just KERN_CPTIME2 with a
flags member, but it can grow as needed.

KERN_CPUSTATS appears to have been defined by BSDi long ago, but there are
few (if any) packages in the wild still using the symbol so breakage in ports
should be near zero. No other system inherited the symbol from BSDi, either.

Then, use the new sysctl(2) in systat(1) and top(1):

- systat(1) draws placeholder marks ('-') instead of percentages for
offline CPUs in the cpu view.

- systat(1) omits offline CPU ticks when drawing the "big bar" in
the vmstat view. The upshot is that the bar isn't half idle when
half your logical CPUs are disabled.

- top(1) does not draw lines for offline CPUs; if CPUs toggle on or
offline in interactive mode we redraw the display to expand/reduce
space for the new/missing CPUs. This is consistent with what some
top(1) implementations do on Linux.

- top(1) omits offline CPUs from the totals when CPU totals are
combined into a single line (the '-1' flag).

Originally prompted by deraadt@. Discussed endlessly with deraadt@,
ketennis@, and sthen@. Tested by jmc@ and jca@. Earlier versions also
discussed with jca@. Earlier versions tested by jmc@, tb@, and many
others.

docs ok jmc@, kernel bits ok ketennis@, everything ok sthen@,
"Is your stuff in yet?" deraadt@


Revision tags: OPENBSD_6_4_BASE
# 1.94 05-Oct-2018 cheloha

Revert KERN_CPTIME2 ENODEV changes in kernel and userspace.

ok kettenis deraadt


# 1.93 26-Sep-2018 cheloha

KERN_CPTIME2: set ENODEV if the CPU is offline.

This lets userspace distinguish between idle CPUs and those that are
not schedulable because hw.smt=0.

A subsequent commit probably needs to add documentation for this
to sysctl.2 (and perhaps elsewhere) after the dust settles.

Also included here are changes to systat(1) and top(1) that account
for the ENODEV case and adjust behavior accordingly:

- systat(1)'s cpu view prints placeholder marks ('-') instead of
percentages for each state if the given CPU is offline.

- systat(1)'s vmstat view checks for offline CPUs when computing the
machine state total and excludes them, so the CPU usage graph
only represents the states for online CPUs.

- top(1) does not draw CPU rows for offline CPUs when the view is
redrawn. If CPUs "go offline", percentages for each state are
replaced by placeholder marks ('-'); the view will need to be
redrawn to remove these rows. If CPUs "go online" the view will
need to be redrawn to show these new CPUs. In "combined CPU" mode,
the count and the state totals only represent online CPUs.

Ports using KERN_CPTIME2 will need to be updated. The changes
described above to make systat(1) and top(1) aware of the ENODEV
case *and* gracefully handle a changing HW_NCPUONLINE while the
application is running are not necessarily appropriate for each
and every port.

The changes described above are so extensive in part to demonstrate
one way a program *might* be made robust to changing CPU availability.
In particular, changing hw.smt after boot is an extremely rare event,
and this needs to be weighed when updating ports.

The logic needed to account for the KERN_CPTIME2 ENODEV case is
very roughly:

if (sysctl(...) == -1) {
if (errno != ENODEV) {
/* Actual error occurred. */
} else {
/* CPU is offline. */
}
} else {
/* CPU is online and CPU states were set by sysctl(2). */
}

Prompted by deraadt@. Basic idea for ENODEV from kettenis@. Discussed at
length with kettenis@. Additional testing by tb@.

No complaints from hackers@ after a week.

ok kettenis@, "I think you should commit [now]" deraadt@


# 1.92 22-Sep-2018 millert

Use user_from_uid() and uid_from_user() directly. The wrappers
in username.c are now so simple there is no longer a good reason
to use them. OK deraadt@


# 1.91 13-Sep-2018 millert

Fix warnings caused by user_from_uid() and group_from_gid() now
returning const char *.


# 1.90 14-May-2018 mpi

Stopping counting and reporting CPU time spent spinning on a lock as
system time.

Introduce a new CP_SPIN "scheduler state" and modify userland tools
to display the % of timer a CPU spents spinning.

Based on a diff from jmatthew@, ok pirofti@, bluhm@, visa@, deraadt@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.89 30-May-2017 tedu

avoid some shadow warnings. from Brian Callahan


Revision tags: OPENBSD_6_1_BASE
# 1.88 15-Mar-2017 deraadt

annoying whitespace die die die


# 1.87 28-Jul-2016 tedu

rework realloc loop. there's no need to shrink the allocation between
calls. if we need a big space once, we'll likely need a big space again.


Revision tags: OPENBSD_6_0_BASE
# 1.86 11-May-2016 edd

Allow top(1) to filter process args if they are being displayed.

OK tedu@, deraadt@, and with input from Michal Mazurek.

Thanks


Revision tags: OPENBSD_5_9_BASE
# 1.85 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_8_BASE
# 1.84 06-May-2015 mpi

Display thread IDs instead of the name of the process's owner when "-H"
is used.

The rationnal is that when you're looking at threads you're generally
already filtereing by PID and this allow you to see which thread is a
pig.

Written some time ago with mikeb@

ok sthen@, krw@, guenther@


Revision tags: OPENBSD_5_7_BASE
# 1.83 19-Jan-2015 millert

Kill loadavg.h and just use the fscale value directly.
There's no need for crazy casts of p_pctcpu in ORDERKEY_PCTCPU
since kinfo_proc exports it as uint32_t. OK deraadt@


# 1.82 19-Jan-2015 deraadt

like in ps(1), fetch the FSCALE value using sysctl rather than using
the header version
ok guenther


# 1.81 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.80 17-Sep-2014 dlg

if there are more than 8 cpus, default to combined cpu stats (like
you passed -1 on the command line).

ok kettenis@ tedu@


# 1.79 15-Sep-2014 miod

Remove non-standard <sys/dkstat.h> header. It has not contained anything
related to disk stastics for almost 17 years, and the remaining
userland-visible defines duplicate those found in <sys/sched.h>.

Move the remaining _KERNEL defines to <sys/tty.h> where they belong, and
update all users to cope with this.

ok kettenis@


Revision tags: OPENBSD_5_6_BASE
# 1.78 04-Jul-2014 guenther

Track whether a process is a zombie or not yet fully built via flags
PS_{ZOMBIE,EMBRYO} on the process instead of peeking into the process's
thread data. This eliminates the need for the thread-level SDEAD state.

Change kvm_getprocs() (both the sysctl() and kvm backends) to report the
"most active" scheduler state for the process's threads.

tweaks kettenis@
feedback and ok matthew@


# 1.77 08-Apr-2014 mpi

Use VM_UVMEXP instead of VM_METER for memory usages and directly
include <sys/vmmeter.h> where it is needed instead of relying on
it being included by <uvm/uvm_extern.h>.

miod@ likes it, ok guenther@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.76 23-Mar-2013 tedu

wchan is no longer exposed, just check if wmesg is not empty.
(wmesg hasn't itself been a pointer since conversion to kinfo_proc.)
noticed by sthen.
ok deraadt jsing millert sthen


Revision tags: OPENBSD_5_3_BASE
# 1.75 18-Dec-2012 millert

We no longer use struct eproc for kinfo_proc in sysctl.h so there
is no direct need for sys/proc.h or sys/resource.h. Some consumers
of kinfo_proc need these for the proc flags and rlimit defines like
RLIM_INF so add the appropriate includes to them.
OK deraadt@ sthen@


Revision tags: OPENBSD_5_2_BASE
# 1.74 09-Jul-2012 deraadt

According to miod, KERNBASE used as a userland symbol should die
in a fire. Start by removing the definitions, before we clean the
headers later.


# 1.73 05-Jun-2012 brynet

Add support for hiding a user's processes in top.

feedback & ok lum@


# 1.72 21-Apr-2012 guenther

Remove a check made superfluous by the KERN_PROC_SHOW_THREADS flag.
This lets new binaries work (at least w/o the -H option) with old kernels.

ok pirofti@ deraadt@


# 1.71 17-Apr-2012 pirofti

Make it optional for kvm_getprocs() (and related sysctl) to return
thread information.

Add a KERN_PROC_SHOW_THREADS flag that has to be set in order to get the
thread info and make it off by default. This creates backwards compatibility
for applications that relied on a given size/number of items to be returned.

Modify ps(1) and top(1) accordingly.

Okay guenther@.


# 1.70 12-Apr-2012 pirofti

Add per thread accounting, mainly for usage & friends.

This expands the already bloated FILL_KPROC macro to take an extra
parameter that indicates if the callee is a thread or a process.

The userland bits are adjusted accordingly and ps(1) and top(1) now
display per thread usage times when -H is used.

Also pkill(1) had to be adjusted so that duplicates don't pop up.

libkvm does basically the same thing as the kernel bits.

Okay guenther@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.69 12-Jul-2011 tedu

print out the amount of memory used for cache. ok otto


# 1.68 10-Apr-2011 guenther

Switch back from KERN_PROC2/kinfo_proc2 to KERN_PROC/kinfo_proc now
that we've got name we want for the API we want

"ZAP!" deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.67 26-Apr-2010 deraadt

more portable code


# 1.66 26-Mar-2010 lum

Make the source of process TIME for displaying and sorting the same.
top(1) and ps(1) now display the same info.
ok tedu@ millert@ otto@ and tested on sparc64 by landry@


Revision tags: OPENBSD_4_7_BASE
# 1.65 29-Jan-2010 tedu

Allow sorting by command and pid. Also, allow partial matches with strncmp.
ok jmc otto


Revision tags: OPENBSD_4_6_BASE
# 1.64 28-Apr-2009 deraadt

extend STATE column to fit > 9 cpus; Aivar Jaakson; PR6131


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.63 01-Nov-2007 otto

bump arbitrary limit on arg length shown by -C; requested by and ok deraadt@


Revision tags: OPENBSD_4_2_BASE
# 1.62 27-Jul-2007 deraadt

replace a few more malloc(n*m) idioms with calloc(n,m) for safety;
inspired by zinovik@cs.karelia.ru


# 1.61 29-May-2007 otto

Instead of using hand-crafted redraws minimizing screen updates, use curses.
Enables nice things like process highlighting without hurting the brain.
ok deraadt@


# 1.60 16-Apr-2007 otto

don't truncate wchan, ok miod@


# 1.59 13-Apr-2007 otto

include cleanup and fix in comment; from Mark Lumsden


# 1.58 04-Apr-2007 otto

missing prototypes; from Mark Lumsden with a twist by me.


Revision tags: OPENBSD_4_1_BASE
# 1.57 04-Feb-2007 otto

long and long long is not the same on 32 bits archs


# 1.56 03-Jan-2007 otto

Add a 'g' command to only show processes having a string in their
command name. ok deraadt@


# 1.55 02-Jan-2007 otto

top(1) doesn't use any math.h function, so do not include math.h and
do not link with libm. ok ray@


# 1.54 29-Nov-2006 miod

Do not test for processes being swapped out since this can't happen anymore.


# 1.53 20-Sep-2006 ray

Recommit memory leak fix.


Revision tags: OPENBSD_4_0_BASE
# 1.52 29-Apr-2006 otto

use calloc(3) instead of malloc(3) to allocate cpu state structs; it's
better style here; plus it prevents bogus data to show up on first display.
report + ok pedro@


Revision tags: OPENBSD_3_9_BASE
# 1.51 21-Dec-2005 millert

Userland programs should include <errno.h> not <sys/errno.h>
OK deraadt@


# 1.50 04-Dec-2005 tedu

support for toggling thread viewing.
ok brad, hints from jmc


Revision tags: OPENBSD_3_8_BASE
# 1.49 17-Jun-2005 markus

add a 'C' command ) that toggles the display of the full cmdline;
with Jared Yanovich; ok deraadt


# 1.48 08-Jun-2005 millert

Multi-cpu support for top. Each cpu now gets its own stats line.
Based on work by todd@ at the hackathon.


Revision tags: OPENBSD_3_7_BASE
# 1.47 06-Dec-2004 markus

missing space; bluhm at genua.de


# 1.46 22-Nov-2004 pat

simplify loop iteration

ok otto millert


# 1.45 17-Nov-2004 markus

off-by-one in process_states[], fixes display of memory; ok deraadt, aaron


# 1.44 07-Oct-2004 otto

introduce -p option and p command to only show a single process.
From Patrick Latifi. ok deraadt@ millert@


Revision tags: OPENBSD_3_6_BASE
# 1.43 13-Jun-2004 otto

Typos in comments and a redundant prototype. From Brian Poole via jmc@
ok jmc@


# 1.42 11-Jun-2004 deraadt

handle the KI_NOCPU case, pointed out by markus


# 1.41 11-Jun-2004 deraadt

only print /# if > 1 cpu on a machine


# 1.40 11-Jun-2004 deraadt

export cpuid via kproc2, and make ps & top aware... from niklas


# 1.39 11-Jun-2004 deraadt

hide top breakage, until the next commit which will do it right


# 1.38 09-May-2004 deraadt

spaces


Revision tags: OPENBSD_3_5_BASE
# 1.37 08-Jan-2004 millert

Convert to kinfo_proc2; ok deraadt@


Revision tags: OPENBSD_3_4_BASE
# 1.36 07-Jul-2003 deraadt

protos


# 1.35 18-Jun-2003 deraadt

remove unused variables


# 1.34 17-Jun-2003 jfb

properly typecast to uid_t the return value of proc_owner() and report
ESRCH instead of EACCES if it returns -1

ok millert@


# 1.33 15-Jun-2003 millert

use uid_t and fix some sign compare warnings; OK krw@ and deraadt@


# 1.32 14-Jun-2003 avsm

use correct length instead of sizeof(char *) for some strl* bound values
millert@ ok


# 1.31 13-Jun-2003 deraadt

readable code


# 1.30 12-Jun-2003 deraadt

knf


# 1.29 12-Jun-2003 pvalchev

cleanup; ok deraadt


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.28 02-Jul-2002 tholo

Add missing copyright


Revision tags: OPENBSD_3_1_BASE
# 1.27 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.26 05-Dec-2001 art

Get maxslp with sysctl. Not the constant.


Revision tags: OPENBSD_3_0_BASE
# 1.25 12-Jul-2001 deraadt

first pass at a -Wall cleanup


# 1.24 03-Jun-2001 angelos

Use KERN_NPROCS to get the number of processes on the system, to
determine the amount of space we'll need to store the
information. The alternative, calling sysctl() with a NULL argument
for data, meant the kernel had to go through the process table.


Revision tags: OPENBSD_2_9_BASE
# 1.23 22-Feb-2001 deraadt

plug mem leak i introduced; spotted by millert


# 1.22 17-Feb-2001 deraadt

bye bye setgid kvm


# 1.21 17-Feb-2001 deraadt

do not use nlist anymore, kvm gets the bullet next


# 1.20 17-Feb-2001 deraadt

partial KNF, because i cannot read the diffs i am trying to debug


Revision tags: OPENBSD_2_8_BASE
# 1.19 18-Jun-2000 niklas

Mark kernel threads as system processes. Make ps hide them by default,
show them with -k. Do not try to show RSS based values for them as they
mess up column alignment. vmstat -f now shows kernel threads separately
from rforks too.


Revision tags: OPENBSD_2_7_BASE
# 1.18 14-Nov-1999 deraadt

stathz instead of hz, plus improvements by me; camield@inet.unisource.nl


# 1.17 29-Oct-1999 todd

writen


Revision tags: OPENBSD_2_6_BASE
# 1.16 21-Jun-1999 art

stop casting the third argument to kvm_read to char *. it is a void *.


# 1.15 22-May-1999 weingart

Update for new vmswap code.


Revision tags: OPENBSD_2_5_BASE
# 1.14 28-Nov-1998 kstailey

do not count swap spaces that were not swapon(2)ed


Revision tags: OPENBSD_2_4_BASE
# 1.13 20-Sep-1998 niklas

cp_time is long, deal with it.


# 1.12 20-Sep-1998 niklas

widen unsigned entities to signed ones before subtracting, fixes sorting on alpha


# 1.11 21-Aug-1998 kstailey

support "-o field" and the interactive "o" command


# 1.10 08-Jul-1998 deraadt

whack kmem gid after kvm_openfiles()


# 1.9 25-Jun-1998 deraadt

handle missing swap


Revision tags: OPENBSD_2_3_BASE
# 1.8 08-Nov-1997 millert

Sort idle processes by total CPU usage, not cpticks since cpticks
get zeroed every second; dan@dan.emsphone.com


Revision tags: OPENBSD_2_2_BASE
# 1.7 09-Sep-1997 millert

"last pid" stuff is useless with random pids.


# 1.6 09-Sep-1997 millert

Use kvm_openfiles() not the sunos compat kvm_open().
Use warn/warnx where it makes sense and check some more ret vals.


# 1.5 24-Aug-1997 millert

Fix compiler warnings.


# 1.4 22-Aug-1997 downsj

Two more alpha warnings.


# 1.3 22-Aug-1997 downsj

First sweep. Prototype, type fixes, long fixes, mostly compiles with
-Wall.


# 1.2 17-Aug-1997 kstailey

print "idle" for idle processes


# 1.1 14-Aug-1997 downsj

top 3.4, with a few changes. Still needs more work.


# 1.95 17-Nov-2018 cheloha

Add new KERN_CPUSTATS sysctl(2) so we can identify offline CPUs.

Because of hw.smt we need a way to determine whether a given CPU is "online"
or "offline" from userspace. KERN_CPTIME2 is an array, and so cannot be
cleanly extended for this purpose, so add a new sysctl(2) KERN_CPUSTATS
with an extensible struct. At the moment it's just KERN_CPTIME2 with a
flags member, but it can grow as needed.

KERN_CPUSTATS appears to have been defined by BSDi long ago, but there are
few (if any) packages in the wild still using the symbol so breakage in ports
should be near zero. No other system inherited the symbol from BSDi, either.

Then, use the new sysctl(2) in systat(1) and top(1):

- systat(1) draws placeholder marks ('-') instead of percentages for
offline CPUs in the cpu view.

- systat(1) omits offline CPU ticks when drawing the "big bar" in
the vmstat view. The upshot is that the bar isn't half idle when
half your logical CPUs are disabled.

- top(1) does not draw lines for offline CPUs; if CPUs toggle on or
offline in interactive mode we redraw the display to expand/reduce
space for the new/missing CPUs. This is consistent with what some
top(1) implementations do on Linux.

- top(1) omits offline CPUs from the totals when CPU totals are
combined into a single line (the '-1' flag).

Originally prompted by deraadt@. Discussed endlessly with deraadt@,
ketennis@, and sthen@. Tested by jmc@ and jca@. Earlier versions also
discussed with jca@. Earlier versions tested by jmc@, tb@, and many
others.

docs ok jmc@, kernel bits ok ketennis@, everything ok sthen@,
"Is your stuff in yet?" deraadt@


Revision tags: OPENBSD_6_4_BASE
# 1.94 05-Oct-2018 cheloha

Revert KERN_CPTIME2 ENODEV changes in kernel and userspace.

ok kettenis deraadt


# 1.93 26-Sep-2018 cheloha

KERN_CPTIME2: set ENODEV if the CPU is offline.

This lets userspace distinguish between idle CPUs and those that are
not schedulable because hw.smt=0.

A subsequent commit probably needs to add documentation for this
to sysctl.2 (and perhaps elsewhere) after the dust settles.

Also included here are changes to systat(1) and top(1) that account
for the ENODEV case and adjust behavior accordingly:

- systat(1)'s cpu view prints placeholder marks ('-') instead of
percentages for each state if the given CPU is offline.

- systat(1)'s vmstat view checks for offline CPUs when computing the
machine state total and excludes them, so the CPU usage graph
only represents the states for online CPUs.

- top(1) does not draw CPU rows for offline CPUs when the view is
redrawn. If CPUs "go offline", percentages for each state are
replaced by placeholder marks ('-'); the view will need to be
redrawn to remove these rows. If CPUs "go online" the view will
need to be redrawn to show these new CPUs. In "combined CPU" mode,
the count and the state totals only represent online CPUs.

Ports using KERN_CPTIME2 will need to be updated. The changes
described above to make systat(1) and top(1) aware of the ENODEV
case *and* gracefully handle a changing HW_NCPUONLINE while the
application is running are not necessarily appropriate for each
and every port.

The changes described above are so extensive in part to demonstrate
one way a program *might* be made robust to changing CPU availability.
In particular, changing hw.smt after boot is an extremely rare event,
and this needs to be weighed when updating ports.

The logic needed to account for the KERN_CPTIME2 ENODEV case is
very roughly:

if (sysctl(...) == -1) {
if (errno != ENODEV) {
/* Actual error occurred. */
} else {
/* CPU is offline. */
}
} else {
/* CPU is online and CPU states were set by sysctl(2). */
}

Prompted by deraadt@. Basic idea for ENODEV from kettenis@. Discussed at
length with kettenis@. Additional testing by tb@.

No complaints from hackers@ after a week.

ok kettenis@, "I think you should commit [now]" deraadt@


# 1.92 22-Sep-2018 millert

Use user_from_uid() and uid_from_user() directly. The wrappers
in username.c are now so simple there is no longer a good reason
to use them. OK deraadt@


# 1.91 13-Sep-2018 millert

Fix warnings caused by user_from_uid() and group_from_gid() now
returning const char *.


# 1.90 14-May-2018 mpi

Stopping counting and reporting CPU time spent spinning on a lock as
system time.

Introduce a new CP_SPIN "scheduler state" and modify userland tools
to display the % of timer a CPU spents spinning.

Based on a diff from jmatthew@, ok pirofti@, bluhm@, visa@, deraadt@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.89 30-May-2017 tedu

avoid some shadow warnings. from Brian Callahan


Revision tags: OPENBSD_6_1_BASE
# 1.88 15-Mar-2017 deraadt

annoying whitespace die die die


# 1.87 28-Jul-2016 tedu

rework realloc loop. there's no need to shrink the allocation between
calls. if we need a big space once, we'll likely need a big space again.


Revision tags: OPENBSD_6_0_BASE
# 1.86 11-May-2016 edd

Allow top(1) to filter process args if they are being displayed.

OK tedu@, deraadt@, and with input from Michal Mazurek.

Thanks


Revision tags: OPENBSD_5_9_BASE
# 1.85 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_8_BASE
# 1.84 06-May-2015 mpi

Display thread IDs instead of the name of the process's owner when "-H"
is used.

The rationnal is that when you're looking at threads you're generally
already filtereing by PID and this allow you to see which thread is a
pig.

Written some time ago with mikeb@

ok sthen@, krw@, guenther@


Revision tags: OPENBSD_5_7_BASE
# 1.83 19-Jan-2015 millert

Kill loadavg.h and just use the fscale value directly.
There's no need for crazy casts of p_pctcpu in ORDERKEY_PCTCPU
since kinfo_proc exports it as uint32_t. OK deraadt@


# 1.82 19-Jan-2015 deraadt

like in ps(1), fetch the FSCALE value using sysctl rather than using
the header version
ok guenther


# 1.81 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.80 17-Sep-2014 dlg

if there are more than 8 cpus, default to combined cpu stats (like
you passed -1 on the command line).

ok kettenis@ tedu@


# 1.79 15-Sep-2014 miod

Remove non-standard <sys/dkstat.h> header. It has not contained anything
related to disk stastics for almost 17 years, and the remaining
userland-visible defines duplicate those found in <sys/sched.h>.

Move the remaining _KERNEL defines to <sys/tty.h> where they belong, and
update all users to cope with this.

ok kettenis@


Revision tags: OPENBSD_5_6_BASE
# 1.78 04-Jul-2014 guenther

Track whether a process is a zombie or not yet fully built via flags
PS_{ZOMBIE,EMBRYO} on the process instead of peeking into the process's
thread data. This eliminates the need for the thread-level SDEAD state.

Change kvm_getprocs() (both the sysctl() and kvm backends) to report the
"most active" scheduler state for the process's threads.

tweaks kettenis@
feedback and ok matthew@


# 1.77 08-Apr-2014 mpi

Use VM_UVMEXP instead of VM_METER for memory usages and directly
include <sys/vmmeter.h> where it is needed instead of relying on
it being included by <uvm/uvm_extern.h>.

miod@ likes it, ok guenther@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.76 23-Mar-2013 tedu

wchan is no longer exposed, just check if wmesg is not empty.
(wmesg hasn't itself been a pointer since conversion to kinfo_proc.)
noticed by sthen.
ok deraadt jsing millert sthen


Revision tags: OPENBSD_5_3_BASE
# 1.75 18-Dec-2012 millert

We no longer use struct eproc for kinfo_proc in sysctl.h so there
is no direct need for sys/proc.h or sys/resource.h. Some consumers
of kinfo_proc need these for the proc flags and rlimit defines like
RLIM_INF so add the appropriate includes to them.
OK deraadt@ sthen@


Revision tags: OPENBSD_5_2_BASE
# 1.74 09-Jul-2012 deraadt

According to miod, KERNBASE used as a userland symbol should die
in a fire. Start by removing the definitions, before we clean the
headers later.


# 1.73 05-Jun-2012 brynet

Add support for hiding a user's processes in top.

feedback & ok lum@


# 1.72 21-Apr-2012 guenther

Remove a check made superfluous by the KERN_PROC_SHOW_THREADS flag.
This lets new binaries work (at least w/o the -H option) with old kernels.

ok pirofti@ deraadt@


# 1.71 17-Apr-2012 pirofti

Make it optional for kvm_getprocs() (and related sysctl) to return
thread information.

Add a KERN_PROC_SHOW_THREADS flag that has to be set in order to get the
thread info and make it off by default. This creates backwards compatibility
for applications that relied on a given size/number of items to be returned.

Modify ps(1) and top(1) accordingly.

Okay guenther@.


# 1.70 12-Apr-2012 pirofti

Add per thread accounting, mainly for usage & friends.

This expands the already bloated FILL_KPROC macro to take an extra
parameter that indicates if the callee is a thread or a process.

The userland bits are adjusted accordingly and ps(1) and top(1) now
display per thread usage times when -H is used.

Also pkill(1) had to be adjusted so that duplicates don't pop up.

libkvm does basically the same thing as the kernel bits.

Okay guenther@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.69 12-Jul-2011 tedu

print out the amount of memory used for cache. ok otto


# 1.68 10-Apr-2011 guenther

Switch back from KERN_PROC2/kinfo_proc2 to KERN_PROC/kinfo_proc now
that we've got name we want for the API we want

"ZAP!" deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.67 26-Apr-2010 deraadt

more portable code


# 1.66 26-Mar-2010 lum

Make the source of process TIME for displaying and sorting the same.
top(1) and ps(1) now display the same info.
ok tedu@ millert@ otto@ and tested on sparc64 by landry@


Revision tags: OPENBSD_4_7_BASE
# 1.65 29-Jan-2010 tedu

Allow sorting by command and pid. Also, allow partial matches with strncmp.
ok jmc otto


Revision tags: OPENBSD_4_6_BASE
# 1.64 28-Apr-2009 deraadt

extend STATE column to fit > 9 cpus; Aivar Jaakson; PR6131


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.63 01-Nov-2007 otto

bump arbitrary limit on arg length shown by -C; requested by and ok deraadt@


Revision tags: OPENBSD_4_2_BASE
# 1.62 27-Jul-2007 deraadt

replace a few more malloc(n*m) idioms with calloc(n,m) for safety;
inspired by zinovik@cs.karelia.ru


# 1.61 29-May-2007 otto

Instead of using hand-crafted redraws minimizing screen updates, use curses.
Enables nice things like process highlighting without hurting the brain.
ok deraadt@


# 1.60 16-Apr-2007 otto

don't truncate wchan, ok miod@


# 1.59 13-Apr-2007 otto

include cleanup and fix in comment; from Mark Lumsden


# 1.58 04-Apr-2007 otto

missing prototypes; from Mark Lumsden with a twist by me.


Revision tags: OPENBSD_4_1_BASE
# 1.57 04-Feb-2007 otto

long and long long is not the same on 32 bits archs


# 1.56 03-Jan-2007 otto

Add a 'g' command to only show processes having a string in their
command name. ok deraadt@


# 1.55 02-Jan-2007 otto

top(1) doesn't use any math.h function, so do not include math.h and
do not link with libm. ok ray@


# 1.54 29-Nov-2006 miod

Do not test for processes being swapped out since this can't happen anymore.


# 1.53 20-Sep-2006 ray

Recommit memory leak fix.


Revision tags: OPENBSD_4_0_BASE
# 1.52 29-Apr-2006 otto

use calloc(3) instead of malloc(3) to allocate cpu state structs; it's
better style here; plus it prevents bogus data to show up on first display.
report + ok pedro@


Revision tags: OPENBSD_3_9_BASE
# 1.51 21-Dec-2005 millert

Userland programs should include <errno.h> not <sys/errno.h>
OK deraadt@


# 1.50 04-Dec-2005 tedu

support for toggling thread viewing.
ok brad, hints from jmc


Revision tags: OPENBSD_3_8_BASE
# 1.49 17-Jun-2005 markus

add a 'C' command ) that toggles the display of the full cmdline;
with Jared Yanovich; ok deraadt


# 1.48 08-Jun-2005 millert

Multi-cpu support for top. Each cpu now gets its own stats line.
Based on work by todd@ at the hackathon.


Revision tags: OPENBSD_3_7_BASE
# 1.47 06-Dec-2004 markus

missing space; bluhm at genua.de


# 1.46 22-Nov-2004 pat

simplify loop iteration

ok otto millert


# 1.45 17-Nov-2004 markus

off-by-one in process_states[], fixes display of memory; ok deraadt, aaron


# 1.44 07-Oct-2004 otto

introduce -p option and p command to only show a single process.
From Patrick Latifi. ok deraadt@ millert@


Revision tags: OPENBSD_3_6_BASE
# 1.43 13-Jun-2004 otto

Typos in comments and a redundant prototype. From Brian Poole via jmc@
ok jmc@


# 1.42 11-Jun-2004 deraadt

handle the KI_NOCPU case, pointed out by markus


# 1.41 11-Jun-2004 deraadt

only print /# if > 1 cpu on a machine


# 1.40 11-Jun-2004 deraadt

export cpuid via kproc2, and make ps & top aware... from niklas


# 1.39 11-Jun-2004 deraadt

hide top breakage, until the next commit which will do it right


# 1.38 09-May-2004 deraadt

spaces


Revision tags: OPENBSD_3_5_BASE
# 1.37 08-Jan-2004 millert

Convert to kinfo_proc2; ok deraadt@


Revision tags: OPENBSD_3_4_BASE
# 1.36 07-Jul-2003 deraadt

protos


# 1.35 18-Jun-2003 deraadt

remove unused variables


# 1.34 17-Jun-2003 jfb

properly typecast to uid_t the return value of proc_owner() and report
ESRCH instead of EACCES if it returns -1

ok millert@


# 1.33 15-Jun-2003 millert

use uid_t and fix some sign compare warnings; OK krw@ and deraadt@


# 1.32 14-Jun-2003 avsm

use correct length instead of sizeof(char *) for some strl* bound values
millert@ ok


# 1.31 13-Jun-2003 deraadt

readable code


# 1.30 12-Jun-2003 deraadt

knf


# 1.29 12-Jun-2003 pvalchev

cleanup; ok deraadt


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.28 02-Jul-2002 tholo

Add missing copyright


Revision tags: OPENBSD_3_1_BASE
# 1.27 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.26 05-Dec-2001 art

Get maxslp with sysctl. Not the constant.


Revision tags: OPENBSD_3_0_BASE
# 1.25 12-Jul-2001 deraadt

first pass at a -Wall cleanup


# 1.24 03-Jun-2001 angelos

Use KERN_NPROCS to get the number of processes on the system, to
determine the amount of space we'll need to store the
information. The alternative, calling sysctl() with a NULL argument
for data, meant the kernel had to go through the process table.


Revision tags: OPENBSD_2_9_BASE
# 1.23 22-Feb-2001 deraadt

plug mem leak i introduced; spotted by millert


# 1.22 17-Feb-2001 deraadt

bye bye setgid kvm


# 1.21 17-Feb-2001 deraadt

do not use nlist anymore, kvm gets the bullet next


# 1.20 17-Feb-2001 deraadt

partial KNF, because i cannot read the diffs i am trying to debug


Revision tags: OPENBSD_2_8_BASE
# 1.19 18-Jun-2000 niklas

Mark kernel threads as system processes. Make ps hide them by default,
show them with -k. Do not try to show RSS based values for them as they
mess up column alignment. vmstat -f now shows kernel threads separately
from rforks too.


Revision tags: OPENBSD_2_7_BASE
# 1.18 14-Nov-1999 deraadt

stathz instead of hz, plus improvements by me; camield@inet.unisource.nl


# 1.17 29-Oct-1999 todd

writen


Revision tags: OPENBSD_2_6_BASE
# 1.16 21-Jun-1999 art

stop casting the third argument to kvm_read to char *. it is a void *.


# 1.15 22-May-1999 weingart

Update for new vmswap code.


Revision tags: OPENBSD_2_5_BASE
# 1.14 28-Nov-1998 kstailey

do not count swap spaces that were not swapon(2)ed


Revision tags: OPENBSD_2_4_BASE
# 1.13 20-Sep-1998 niklas

cp_time is long, deal with it.


# 1.12 20-Sep-1998 niklas

widen unsigned entities to signed ones before subtracting, fixes sorting on alpha


# 1.11 21-Aug-1998 kstailey

support "-o field" and the interactive "o" command


# 1.10 08-Jul-1998 deraadt

whack kmem gid after kvm_openfiles()


# 1.9 25-Jun-1998 deraadt

handle missing swap


Revision tags: OPENBSD_2_3_BASE
# 1.8 08-Nov-1997 millert

Sort idle processes by total CPU usage, not cpticks since cpticks
get zeroed every second; dan@dan.emsphone.com


Revision tags: OPENBSD_2_2_BASE
# 1.7 09-Sep-1997 millert

"last pid" stuff is useless with random pids.


# 1.6 09-Sep-1997 millert

Use kvm_openfiles() not the sunos compat kvm_open().
Use warn/warnx where it makes sense and check some more ret vals.


# 1.5 24-Aug-1997 millert

Fix compiler warnings.


# 1.4 22-Aug-1997 downsj

Two more alpha warnings.


# 1.3 22-Aug-1997 downsj

First sweep. Prototype, type fixes, long fixes, mostly compiles with
-Wall.


# 1.2 17-Aug-1997 kstailey

print "idle" for idle processes


# 1.1 14-Aug-1997 downsj

top 3.4, with a few changes. Still needs more work.


# 1.94 05-Oct-2018 cheloha

Revert KERN_CPTIME2 ENODEV changes in kernel and userspace.

ok kettenis deraadt


# 1.93 26-Sep-2018 cheloha

KERN_CPTIME2: set ENODEV if the CPU is offline.

This lets userspace distinguish between idle CPUs and those that are
not schedulable because hw.smt=0.

A subsequent commit probably needs to add documentation for this
to sysctl.2 (and perhaps elsewhere) after the dust settles.

Also included here are changes to systat(1) and top(1) that account
for the ENODEV case and adjust behavior accordingly:

- systat(1)'s cpu view prints placeholder marks ('-') instead of
percentages for each state if the given CPU is offline.

- systat(1)'s vmstat view checks for offline CPUs when computing the
machine state total and excludes them, so the CPU usage graph
only represents the states for online CPUs.

- top(1) does not draw CPU rows for offline CPUs when the view is
redrawn. If CPUs "go offline", percentages for each state are
replaced by placeholder marks ('-'); the view will need to be
redrawn to remove these rows. If CPUs "go online" the view will
need to be redrawn to show these new CPUs. In "combined CPU" mode,
the count and the state totals only represent online CPUs.

Ports using KERN_CPTIME2 will need to be updated. The changes
described above to make systat(1) and top(1) aware of the ENODEV
case *and* gracefully handle a changing HW_NCPUONLINE while the
application is running are not necessarily appropriate for each
and every port.

The changes described above are so extensive in part to demonstrate
one way a program *might* be made robust to changing CPU availability.
In particular, changing hw.smt after boot is an extremely rare event,
and this needs to be weighed when updating ports.

The logic needed to account for the KERN_CPTIME2 ENODEV case is
very roughly:

if (sysctl(...) == -1) {
if (errno != ENODEV) {
/* Actual error occurred. */
} else {
/* CPU is offline. */
}
} else {
/* CPU is online and CPU states were set by sysctl(2). */
}

Prompted by deraadt@. Basic idea for ENODEV from kettenis@. Discussed at
length with kettenis@. Additional testing by tb@.

No complaints from hackers@ after a week.

ok kettenis@, "I think you should commit [now]" deraadt@


# 1.92 22-Sep-2018 millert

Use user_from_uid() and uid_from_user() directly. The wrappers
in username.c are now so simple there is no longer a good reason
to use them. OK deraadt@


# 1.91 13-Sep-2018 millert

Fix warnings caused by user_from_uid() and group_from_gid() now
returning const char *.


# 1.90 14-May-2018 mpi

Stopping counting and reporting CPU time spent spinning on a lock as
system time.

Introduce a new CP_SPIN "scheduler state" and modify userland tools
to display the % of timer a CPU spents spinning.

Based on a diff from jmatthew@, ok pirofti@, bluhm@, visa@, deraadt@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.89 30-May-2017 tedu

avoid some shadow warnings. from Brian Callahan


Revision tags: OPENBSD_6_1_BASE
# 1.88 15-Mar-2017 deraadt

annoying whitespace die die die


# 1.87 28-Jul-2016 tedu

rework realloc loop. there's no need to shrink the allocation between
calls. if we need a big space once, we'll likely need a big space again.


Revision tags: OPENBSD_6_0_BASE
# 1.86 11-May-2016 edd

Allow top(1) to filter process args if they are being displayed.

OK tedu@, deraadt@, and with input from Michal Mazurek.

Thanks


Revision tags: OPENBSD_5_9_BASE
# 1.85 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_8_BASE
# 1.84 06-May-2015 mpi

Display thread IDs instead of the name of the process's owner when "-H"
is used.

The rationnal is that when you're looking at threads you're generally
already filtereing by PID and this allow you to see which thread is a
pig.

Written some time ago with mikeb@

ok sthen@, krw@, guenther@


Revision tags: OPENBSD_5_7_BASE
# 1.83 19-Jan-2015 millert

Kill loadavg.h and just use the fscale value directly.
There's no need for crazy casts of p_pctcpu in ORDERKEY_PCTCPU
since kinfo_proc exports it as uint32_t. OK deraadt@


# 1.82 19-Jan-2015 deraadt

like in ps(1), fetch the FSCALE value using sysctl rather than using
the header version
ok guenther


# 1.81 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.80 17-Sep-2014 dlg

if there are more than 8 cpus, default to combined cpu stats (like
you passed -1 on the command line).

ok kettenis@ tedu@


# 1.79 15-Sep-2014 miod

Remove non-standard <sys/dkstat.h> header. It has not contained anything
related to disk stastics for almost 17 years, and the remaining
userland-visible defines duplicate those found in <sys/sched.h>.

Move the remaining _KERNEL defines to <sys/tty.h> where they belong, and
update all users to cope with this.

ok kettenis@


Revision tags: OPENBSD_5_6_BASE
# 1.78 04-Jul-2014 guenther

Track whether a process is a zombie or not yet fully built via flags
PS_{ZOMBIE,EMBRYO} on the process instead of peeking into the process's
thread data. This eliminates the need for the thread-level SDEAD state.

Change kvm_getprocs() (both the sysctl() and kvm backends) to report the
"most active" scheduler state for the process's threads.

tweaks kettenis@
feedback and ok matthew@


# 1.77 08-Apr-2014 mpi

Use VM_UVMEXP instead of VM_METER for memory usages and directly
include <sys/vmmeter.h> where it is needed instead of relying on
it being included by <uvm/uvm_extern.h>.

miod@ likes it, ok guenther@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.76 23-Mar-2013 tedu

wchan is no longer exposed, just check if wmesg is not empty.
(wmesg hasn't itself been a pointer since conversion to kinfo_proc.)
noticed by sthen.
ok deraadt jsing millert sthen


Revision tags: OPENBSD_5_3_BASE
# 1.75 18-Dec-2012 millert

We no longer use struct eproc for kinfo_proc in sysctl.h so there
is no direct need for sys/proc.h or sys/resource.h. Some consumers
of kinfo_proc need these for the proc flags and rlimit defines like
RLIM_INF so add the appropriate includes to them.
OK deraadt@ sthen@


Revision tags: OPENBSD_5_2_BASE
# 1.74 09-Jul-2012 deraadt

According to miod, KERNBASE used as a userland symbol should die
in a fire. Start by removing the definitions, before we clean the
headers later.


# 1.73 05-Jun-2012 brynet

Add support for hiding a user's processes in top.

feedback & ok lum@


# 1.72 21-Apr-2012 guenther

Remove a check made superfluous by the KERN_PROC_SHOW_THREADS flag.
This lets new binaries work (at least w/o the -H option) with old kernels.

ok pirofti@ deraadt@


# 1.71 17-Apr-2012 pirofti

Make it optional for kvm_getprocs() (and related sysctl) to return
thread information.

Add a KERN_PROC_SHOW_THREADS flag that has to be set in order to get the
thread info and make it off by default. This creates backwards compatibility
for applications that relied on a given size/number of items to be returned.

Modify ps(1) and top(1) accordingly.

Okay guenther@.


# 1.70 12-Apr-2012 pirofti

Add per thread accounting, mainly for usage & friends.

This expands the already bloated FILL_KPROC macro to take an extra
parameter that indicates if the callee is a thread or a process.

The userland bits are adjusted accordingly and ps(1) and top(1) now
display per thread usage times when -H is used.

Also pkill(1) had to be adjusted so that duplicates don't pop up.

libkvm does basically the same thing as the kernel bits.

Okay guenther@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.69 12-Jul-2011 tedu

print out the amount of memory used for cache. ok otto


# 1.68 10-Apr-2011 guenther

Switch back from KERN_PROC2/kinfo_proc2 to KERN_PROC/kinfo_proc now
that we've got name we want for the API we want

"ZAP!" deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.67 26-Apr-2010 deraadt

more portable code


# 1.66 26-Mar-2010 lum

Make the source of process TIME for displaying and sorting the same.
top(1) and ps(1) now display the same info.
ok tedu@ millert@ otto@ and tested on sparc64 by landry@


Revision tags: OPENBSD_4_7_BASE
# 1.65 29-Jan-2010 tedu

Allow sorting by command and pid. Also, allow partial matches with strncmp.
ok jmc otto


Revision tags: OPENBSD_4_6_BASE
# 1.64 28-Apr-2009 deraadt

extend STATE column to fit > 9 cpus; Aivar Jaakson; PR6131


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.63 01-Nov-2007 otto

bump arbitrary limit on arg length shown by -C; requested by and ok deraadt@


Revision tags: OPENBSD_4_2_BASE
# 1.62 27-Jul-2007 deraadt

replace a few more malloc(n*m) idioms with calloc(n,m) for safety;
inspired by zinovik@cs.karelia.ru


# 1.61 29-May-2007 otto

Instead of using hand-crafted redraws minimizing screen updates, use curses.
Enables nice things like process highlighting without hurting the brain.
ok deraadt@


# 1.60 16-Apr-2007 otto

don't truncate wchan, ok miod@


# 1.59 13-Apr-2007 otto

include cleanup and fix in comment; from Mark Lumsden


# 1.58 04-Apr-2007 otto

missing prototypes; from Mark Lumsden with a twist by me.


Revision tags: OPENBSD_4_1_BASE
# 1.57 04-Feb-2007 otto

long and long long is not the same on 32 bits archs


# 1.56 03-Jan-2007 otto

Add a 'g' command to only show processes having a string in their
command name. ok deraadt@


# 1.55 02-Jan-2007 otto

top(1) doesn't use any math.h function, so do not include math.h and
do not link with libm. ok ray@


# 1.54 29-Nov-2006 miod

Do not test for processes being swapped out since this can't happen anymore.


# 1.53 20-Sep-2006 ray

Recommit memory leak fix.


Revision tags: OPENBSD_4_0_BASE
# 1.52 29-Apr-2006 otto

use calloc(3) instead of malloc(3) to allocate cpu state structs; it's
better style here; plus it prevents bogus data to show up on first display.
report + ok pedro@


Revision tags: OPENBSD_3_9_BASE
# 1.51 21-Dec-2005 millert

Userland programs should include <errno.h> not <sys/errno.h>
OK deraadt@


# 1.50 04-Dec-2005 tedu

support for toggling thread viewing.
ok brad, hints from jmc


Revision tags: OPENBSD_3_8_BASE
# 1.49 17-Jun-2005 markus

add a 'C' command ) that toggles the display of the full cmdline;
with Jared Yanovich; ok deraadt


# 1.48 08-Jun-2005 millert

Multi-cpu support for top. Each cpu now gets its own stats line.
Based on work by todd@ at the hackathon.


Revision tags: OPENBSD_3_7_BASE
# 1.47 06-Dec-2004 markus

missing space; bluhm at genua.de


# 1.46 22-Nov-2004 pat

simplify loop iteration

ok otto millert


# 1.45 17-Nov-2004 markus

off-by-one in process_states[], fixes display of memory; ok deraadt, aaron


# 1.44 07-Oct-2004 otto

introduce -p option and p command to only show a single process.
From Patrick Latifi. ok deraadt@ millert@


Revision tags: OPENBSD_3_6_BASE
# 1.43 13-Jun-2004 otto

Typos in comments and a redundant prototype. From Brian Poole via jmc@
ok jmc@


# 1.42 11-Jun-2004 deraadt

handle the KI_NOCPU case, pointed out by markus


# 1.41 11-Jun-2004 deraadt

only print /# if > 1 cpu on a machine


# 1.40 11-Jun-2004 deraadt

export cpuid via kproc2, and make ps & top aware... from niklas


# 1.39 11-Jun-2004 deraadt

hide top breakage, until the next commit which will do it right


# 1.38 09-May-2004 deraadt

spaces


Revision tags: OPENBSD_3_5_BASE
# 1.37 08-Jan-2004 millert

Convert to kinfo_proc2; ok deraadt@


Revision tags: OPENBSD_3_4_BASE
# 1.36 07-Jul-2003 deraadt

protos


# 1.35 18-Jun-2003 deraadt

remove unused variables


# 1.34 17-Jun-2003 jfb

properly typecast to uid_t the return value of proc_owner() and report
ESRCH instead of EACCES if it returns -1

ok millert@


# 1.33 15-Jun-2003 millert

use uid_t and fix some sign compare warnings; OK krw@ and deraadt@


# 1.32 14-Jun-2003 avsm

use correct length instead of sizeof(char *) for some strl* bound values
millert@ ok


# 1.31 13-Jun-2003 deraadt

readable code


# 1.30 12-Jun-2003 deraadt

knf


# 1.29 12-Jun-2003 pvalchev

cleanup; ok deraadt


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.28 02-Jul-2002 tholo

Add missing copyright


Revision tags: OPENBSD_3_1_BASE
# 1.27 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.26 05-Dec-2001 art

Get maxslp with sysctl. Not the constant.


Revision tags: OPENBSD_3_0_BASE
# 1.25 12-Jul-2001 deraadt

first pass at a -Wall cleanup


# 1.24 03-Jun-2001 angelos

Use KERN_NPROCS to get the number of processes on the system, to
determine the amount of space we'll need to store the
information. The alternative, calling sysctl() with a NULL argument
for data, meant the kernel had to go through the process table.


Revision tags: OPENBSD_2_9_BASE
# 1.23 22-Feb-2001 deraadt

plug mem leak i introduced; spotted by millert


# 1.22 17-Feb-2001 deraadt

bye bye setgid kvm


# 1.21 17-Feb-2001 deraadt

do not use nlist anymore, kvm gets the bullet next


# 1.20 17-Feb-2001 deraadt

partial KNF, because i cannot read the diffs i am trying to debug


Revision tags: OPENBSD_2_8_BASE
# 1.19 18-Jun-2000 niklas

Mark kernel threads as system processes. Make ps hide them by default,
show them with -k. Do not try to show RSS based values for them as they
mess up column alignment. vmstat -f now shows kernel threads separately
from rforks too.


Revision tags: OPENBSD_2_7_BASE
# 1.18 14-Nov-1999 deraadt

stathz instead of hz, plus improvements by me; camield@inet.unisource.nl


# 1.17 29-Oct-1999 todd

writen


Revision tags: OPENBSD_2_6_BASE
# 1.16 21-Jun-1999 art

stop casting the third argument to kvm_read to char *. it is a void *.


# 1.15 22-May-1999 weingart

Update for new vmswap code.


Revision tags: OPENBSD_2_5_BASE
# 1.14 28-Nov-1998 kstailey

do not count swap spaces that were not swapon(2)ed


Revision tags: OPENBSD_2_4_BASE
# 1.13 20-Sep-1998 niklas

cp_time is long, deal with it.


# 1.12 20-Sep-1998 niklas

widen unsigned entities to signed ones before subtracting, fixes sorting on alpha


# 1.11 21-Aug-1998 kstailey

support "-o field" and the interactive "o" command


# 1.10 08-Jul-1998 deraadt

whack kmem gid after kvm_openfiles()


# 1.9 25-Jun-1998 deraadt

handle missing swap


Revision tags: OPENBSD_2_3_BASE
# 1.8 08-Nov-1997 millert

Sort idle processes by total CPU usage, not cpticks since cpticks
get zeroed every second; dan@dan.emsphone.com


Revision tags: OPENBSD_2_2_BASE
# 1.7 09-Sep-1997 millert

"last pid" stuff is useless with random pids.


# 1.6 09-Sep-1997 millert

Use kvm_openfiles() not the sunos compat kvm_open().
Use warn/warnx where it makes sense and check some more ret vals.


# 1.5 24-Aug-1997 millert

Fix compiler warnings.


# 1.4 22-Aug-1997 downsj

Two more alpha warnings.


# 1.3 22-Aug-1997 downsj

First sweep. Prototype, type fixes, long fixes, mostly compiles with
-Wall.


# 1.2 17-Aug-1997 kstailey

print "idle" for idle processes


# 1.1 14-Aug-1997 downsj

top 3.4, with a few changes. Still needs more work.


# 1.93 26-Sep-2018 cheloha

KERN_CPTIME2: set ENODEV if the CPU is offline.

This lets userspace distinguish between idle CPUs and those that are
not schedulable because hw.smt=0.

A subsequent commit probably needs to add documentation for this
to sysctl.2 (and perhaps elsewhere) after the dust settles.

Also included here are changes to systat(1) and top(1) that account
for the ENODEV case and adjust behavior accordingly:

- systat(1)'s cpu view prints placeholder marks ('-') instead of
percentages for each state if the given CPU is offline.

- systat(1)'s vmstat view checks for offline CPUs when computing the
machine state total and excludes them, so the CPU usage graph
only represents the states for online CPUs.

- top(1) does not draw CPU rows for offline CPUs when the view is
redrawn. If CPUs "go offline", percentages for each state are
replaced by placeholder marks ('-'); the view will need to be
redrawn to remove these rows. If CPUs "go online" the view will
need to be redrawn to show these new CPUs. In "combined CPU" mode,
the count and the state totals only represent online CPUs.

Ports using KERN_CPTIME2 will need to be updated. The changes
described above to make systat(1) and top(1) aware of the ENODEV
case *and* gracefully handle a changing HW_NCPUONLINE while the
application is running are not necessarily appropriate for each
and every port.

The changes described above are so extensive in part to demonstrate
one way a program *might* be made robust to changing CPU availability.
In particular, changing hw.smt after boot is an extremely rare event,
and this needs to be weighed when updating ports.

The logic needed to account for the KERN_CPTIME2 ENODEV case is
very roughly:

if (sysctl(...) == -1) {
if (errno != ENODEV) {
/* Actual error occurred. */
} else {
/* CPU is offline. */
}
} else {
/* CPU is online and CPU states were set by sysctl(2). */
}

Prompted by deraadt@. Basic idea for ENODEV from kettenis@. Discussed at
length with kettenis@. Additional testing by tb@.

No complaints from hackers@ after a week.

ok kettenis@, "I think you should commit [now]" deraadt@


# 1.92 22-Sep-2018 millert

Use user_from_uid() and uid_from_user() directly. The wrappers
in username.c are now so simple there is no longer a good reason
to use them. OK deraadt@


# 1.91 13-Sep-2018 millert

Fix warnings caused by user_from_uid() and group_from_gid() now
returning const char *.


# 1.90 14-May-2018 mpi

Stopping counting and reporting CPU time spent spinning on a lock as
system time.

Introduce a new CP_SPIN "scheduler state" and modify userland tools
to display the % of timer a CPU spents spinning.

Based on a diff from jmatthew@, ok pirofti@, bluhm@, visa@, deraadt@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.89 30-May-2017 tedu

avoid some shadow warnings. from Brian Callahan


Revision tags: OPENBSD_6_1_BASE
# 1.88 15-Mar-2017 deraadt

annoying whitespace die die die


# 1.87 28-Jul-2016 tedu

rework realloc loop. there's no need to shrink the allocation between
calls. if we need a big space once, we'll likely need a big space again.


Revision tags: OPENBSD_6_0_BASE
# 1.86 11-May-2016 edd

Allow top(1) to filter process args if they are being displayed.

OK tedu@, deraadt@, and with input from Michal Mazurek.

Thanks


Revision tags: OPENBSD_5_9_BASE
# 1.85 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_8_BASE
# 1.84 06-May-2015 mpi

Display thread IDs instead of the name of the process's owner when "-H"
is used.

The rationnal is that when you're looking at threads you're generally
already filtereing by PID and this allow you to see which thread is a
pig.

Written some time ago with mikeb@

ok sthen@, krw@, guenther@


Revision tags: OPENBSD_5_7_BASE
# 1.83 19-Jan-2015 millert

Kill loadavg.h and just use the fscale value directly.
There's no need for crazy casts of p_pctcpu in ORDERKEY_PCTCPU
since kinfo_proc exports it as uint32_t. OK deraadt@


# 1.82 19-Jan-2015 deraadt

like in ps(1), fetch the FSCALE value using sysctl rather than using
the header version
ok guenther


# 1.81 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.80 17-Sep-2014 dlg

if there are more than 8 cpus, default to combined cpu stats (like
you passed -1 on the command line).

ok kettenis@ tedu@


# 1.79 15-Sep-2014 miod

Remove non-standard <sys/dkstat.h> header. It has not contained anything
related to disk stastics for almost 17 years, and the remaining
userland-visible defines duplicate those found in <sys/sched.h>.

Move the remaining _KERNEL defines to <sys/tty.h> where they belong, and
update all users to cope with this.

ok kettenis@


Revision tags: OPENBSD_5_6_BASE
# 1.78 04-Jul-2014 guenther

Track whether a process is a zombie or not yet fully built via flags
PS_{ZOMBIE,EMBRYO} on the process instead of peeking into the process's
thread data. This eliminates the need for the thread-level SDEAD state.

Change kvm_getprocs() (both the sysctl() and kvm backends) to report the
"most active" scheduler state for the process's threads.

tweaks kettenis@
feedback and ok matthew@


# 1.77 08-Apr-2014 mpi

Use VM_UVMEXP instead of VM_METER for memory usages and directly
include <sys/vmmeter.h> where it is needed instead of relying on
it being included by <uvm/uvm_extern.h>.

miod@ likes it, ok guenther@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.76 23-Mar-2013 tedu

wchan is no longer exposed, just check if wmesg is not empty.
(wmesg hasn't itself been a pointer since conversion to kinfo_proc.)
noticed by sthen.
ok deraadt jsing millert sthen


Revision tags: OPENBSD_5_3_BASE
# 1.75 18-Dec-2012 millert

We no longer use struct eproc for kinfo_proc in sysctl.h so there
is no direct need for sys/proc.h or sys/resource.h. Some consumers
of kinfo_proc need these for the proc flags and rlimit defines like
RLIM_INF so add the appropriate includes to them.
OK deraadt@ sthen@


Revision tags: OPENBSD_5_2_BASE
# 1.74 09-Jul-2012 deraadt

According to miod, KERNBASE used as a userland symbol should die
in a fire. Start by removing the definitions, before we clean the
headers later.


# 1.73 05-Jun-2012 brynet

Add support for hiding a user's processes in top.

feedback & ok lum@


# 1.72 21-Apr-2012 guenther

Remove a check made superfluous by the KERN_PROC_SHOW_THREADS flag.
This lets new binaries work (at least w/o the -H option) with old kernels.

ok pirofti@ deraadt@


# 1.71 17-Apr-2012 pirofti

Make it optional for kvm_getprocs() (and related sysctl) to return
thread information.

Add a KERN_PROC_SHOW_THREADS flag that has to be set in order to get the
thread info and make it off by default. This creates backwards compatibility
for applications that relied on a given size/number of items to be returned.

Modify ps(1) and top(1) accordingly.

Okay guenther@.


# 1.70 12-Apr-2012 pirofti

Add per thread accounting, mainly for usage & friends.

This expands the already bloated FILL_KPROC macro to take an extra
parameter that indicates if the callee is a thread or a process.

The userland bits are adjusted accordingly and ps(1) and top(1) now
display per thread usage times when -H is used.

Also pkill(1) had to be adjusted so that duplicates don't pop up.

libkvm does basically the same thing as the kernel bits.

Okay guenther@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.69 12-Jul-2011 tedu

print out the amount of memory used for cache. ok otto


# 1.68 10-Apr-2011 guenther

Switch back from KERN_PROC2/kinfo_proc2 to KERN_PROC/kinfo_proc now
that we've got name we want for the API we want

"ZAP!" deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.67 26-Apr-2010 deraadt

more portable code


# 1.66 26-Mar-2010 lum

Make the source of process TIME for displaying and sorting the same.
top(1) and ps(1) now display the same info.
ok tedu@ millert@ otto@ and tested on sparc64 by landry@


Revision tags: OPENBSD_4_7_BASE
# 1.65 29-Jan-2010 tedu

Allow sorting by command and pid. Also, allow partial matches with strncmp.
ok jmc otto


Revision tags: OPENBSD_4_6_BASE
# 1.64 28-Apr-2009 deraadt

extend STATE column to fit > 9 cpus; Aivar Jaakson; PR6131


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.63 01-Nov-2007 otto

bump arbitrary limit on arg length shown by -C; requested by and ok deraadt@


Revision tags: OPENBSD_4_2_BASE
# 1.62 27-Jul-2007 deraadt

replace a few more malloc(n*m) idioms with calloc(n,m) for safety;
inspired by zinovik@cs.karelia.ru


# 1.61 29-May-2007 otto

Instead of using hand-crafted redraws minimizing screen updates, use curses.
Enables nice things like process highlighting without hurting the brain.
ok deraadt@


# 1.60 16-Apr-2007 otto

don't truncate wchan, ok miod@


# 1.59 13-Apr-2007 otto

include cleanup and fix in comment; from Mark Lumsden


# 1.58 04-Apr-2007 otto

missing prototypes; from Mark Lumsden with a twist by me.


Revision tags: OPENBSD_4_1_BASE
# 1.57 04-Feb-2007 otto

long and long long is not the same on 32 bits archs


# 1.56 03-Jan-2007 otto

Add a 'g' command to only show processes having a string in their
command name. ok deraadt@


# 1.55 02-Jan-2007 otto

top(1) doesn't use any math.h function, so do not include math.h and
do not link with libm. ok ray@


# 1.54 29-Nov-2006 miod

Do not test for processes being swapped out since this can't happen anymore.


# 1.53 20-Sep-2006 ray

Recommit memory leak fix.


Revision tags: OPENBSD_4_0_BASE
# 1.52 29-Apr-2006 otto

use calloc(3) instead of malloc(3) to allocate cpu state structs; it's
better style here; plus it prevents bogus data to show up on first display.
report + ok pedro@


Revision tags: OPENBSD_3_9_BASE
# 1.51 21-Dec-2005 millert

Userland programs should include <errno.h> not <sys/errno.h>
OK deraadt@


# 1.50 04-Dec-2005 tedu

support for toggling thread viewing.
ok brad, hints from jmc


Revision tags: OPENBSD_3_8_BASE
# 1.49 17-Jun-2005 markus

add a 'C' command ) that toggles the display of the full cmdline;
with Jared Yanovich; ok deraadt


# 1.48 08-Jun-2005 millert

Multi-cpu support for top. Each cpu now gets its own stats line.
Based on work by todd@ at the hackathon.


Revision tags: OPENBSD_3_7_BASE
# 1.47 06-Dec-2004 markus

missing space; bluhm at genua.de


# 1.46 22-Nov-2004 pat

simplify loop iteration

ok otto millert


# 1.45 17-Nov-2004 markus

off-by-one in process_states[], fixes display of memory; ok deraadt, aaron


# 1.44 07-Oct-2004 otto

introduce -p option and p command to only show a single process.
From Patrick Latifi. ok deraadt@ millert@


Revision tags: OPENBSD_3_6_BASE
# 1.43 13-Jun-2004 otto

Typos in comments and a redundant prototype. From Brian Poole via jmc@
ok jmc@


# 1.42 11-Jun-2004 deraadt

handle the KI_NOCPU case, pointed out by markus


# 1.41 11-Jun-2004 deraadt

only print /# if > 1 cpu on a machine


# 1.40 11-Jun-2004 deraadt

export cpuid via kproc2, and make ps & top aware... from niklas


# 1.39 11-Jun-2004 deraadt

hide top breakage, until the next commit which will do it right


# 1.38 09-May-2004 deraadt

spaces


Revision tags: OPENBSD_3_5_BASE
# 1.37 08-Jan-2004 millert

Convert to kinfo_proc2; ok deraadt@


Revision tags: OPENBSD_3_4_BASE
# 1.36 07-Jul-2003 deraadt

protos


# 1.35 18-Jun-2003 deraadt

remove unused variables


# 1.34 17-Jun-2003 jfb

properly typecast to uid_t the return value of proc_owner() and report
ESRCH instead of EACCES if it returns -1

ok millert@


# 1.33 15-Jun-2003 millert

use uid_t and fix some sign compare warnings; OK krw@ and deraadt@


# 1.32 14-Jun-2003 avsm

use correct length instead of sizeof(char *) for some strl* bound values
millert@ ok


# 1.31 13-Jun-2003 deraadt

readable code


# 1.30 12-Jun-2003 deraadt

knf


# 1.29 12-Jun-2003 pvalchev

cleanup; ok deraadt


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.28 02-Jul-2002 tholo

Add missing copyright


Revision tags: OPENBSD_3_1_BASE
# 1.27 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.26 05-Dec-2001 art

Get maxslp with sysctl. Not the constant.


Revision tags: OPENBSD_3_0_BASE
# 1.25 12-Jul-2001 deraadt

first pass at a -Wall cleanup


# 1.24 03-Jun-2001 angelos

Use KERN_NPROCS to get the number of processes on the system, to
determine the amount of space we'll need to store the
information. The alternative, calling sysctl() with a NULL argument
for data, meant the kernel had to go through the process table.


Revision tags: OPENBSD_2_9_BASE
# 1.23 22-Feb-2001 deraadt

plug mem leak i introduced; spotted by millert


# 1.22 17-Feb-2001 deraadt

bye bye setgid kvm


# 1.21 17-Feb-2001 deraadt

do not use nlist anymore, kvm gets the bullet next


# 1.20 17-Feb-2001 deraadt

partial KNF, because i cannot read the diffs i am trying to debug


Revision tags: OPENBSD_2_8_BASE
# 1.19 18-Jun-2000 niklas

Mark kernel threads as system processes. Make ps hide them by default,
show them with -k. Do not try to show RSS based values for them as they
mess up column alignment. vmstat -f now shows kernel threads separately
from rforks too.


Revision tags: OPENBSD_2_7_BASE
# 1.18 14-Nov-1999 deraadt

stathz instead of hz, plus improvements by me; camield@inet.unisource.nl


# 1.17 29-Oct-1999 todd

writen


Revision tags: OPENBSD_2_6_BASE
# 1.16 21-Jun-1999 art

stop casting the third argument to kvm_read to char *. it is a void *.


# 1.15 22-May-1999 weingart

Update for new vmswap code.


Revision tags: OPENBSD_2_5_BASE
# 1.14 28-Nov-1998 kstailey

do not count swap spaces that were not swapon(2)ed


Revision tags: OPENBSD_2_4_BASE
# 1.13 20-Sep-1998 niklas

cp_time is long, deal with it.


# 1.12 20-Sep-1998 niklas

widen unsigned entities to signed ones before subtracting, fixes sorting on alpha


# 1.11 21-Aug-1998 kstailey

support "-o field" and the interactive "o" command


# 1.10 08-Jul-1998 deraadt

whack kmem gid after kvm_openfiles()


# 1.9 25-Jun-1998 deraadt

handle missing swap


Revision tags: OPENBSD_2_3_BASE
# 1.8 08-Nov-1997 millert

Sort idle processes by total CPU usage, not cpticks since cpticks
get zeroed every second; dan@dan.emsphone.com


Revision tags: OPENBSD_2_2_BASE
# 1.7 09-Sep-1997 millert

"last pid" stuff is useless with random pids.


# 1.6 09-Sep-1997 millert

Use kvm_openfiles() not the sunos compat kvm_open().
Use warn/warnx where it makes sense and check some more ret vals.


# 1.5 24-Aug-1997 millert

Fix compiler warnings.


# 1.4 22-Aug-1997 downsj

Two more alpha warnings.


# 1.3 22-Aug-1997 downsj

First sweep. Prototype, type fixes, long fixes, mostly compiles with
-Wall.


# 1.2 17-Aug-1997 kstailey

print "idle" for idle processes


# 1.1 14-Aug-1997 downsj

top 3.4, with a few changes. Still needs more work.


# 1.90 14-May-2018 mpi

Stopping counting and reporting CPU time spent spinning on a lock as
system time.

Introduce a new CP_SPIN "scheduler state" and modify userland tools
to display the % of timer a CPU spents spinning.

Based on a diff from jmatthew@, ok pirofti@, bluhm@, visa@, deraadt@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.89 30-May-2017 tedu

avoid some shadow warnings. from Brian Callahan


Revision tags: OPENBSD_6_1_BASE
# 1.88 15-Mar-2017 deraadt

annoying whitespace die die die


# 1.87 28-Jul-2016 tedu

rework realloc loop. there's no need to shrink the allocation between
calls. if we need a big space once, we'll likely need a big space again.


Revision tags: OPENBSD_6_0_BASE
# 1.86 11-May-2016 edd

Allow top(1) to filter process args if they are being displayed.

OK tedu@, deraadt@, and with input from Michal Mazurek.

Thanks


Revision tags: OPENBSD_5_9_BASE
# 1.85 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_8_BASE
# 1.84 06-May-2015 mpi

Display thread IDs instead of the name of the process's owner when "-H"
is used.

The rationnal is that when you're looking at threads you're generally
already filtereing by PID and this allow you to see which thread is a
pig.

Written some time ago with mikeb@

ok sthen@, krw@, guenther@


Revision tags: OPENBSD_5_7_BASE
# 1.83 19-Jan-2015 millert

Kill loadavg.h and just use the fscale value directly.
There's no need for crazy casts of p_pctcpu in ORDERKEY_PCTCPU
since kinfo_proc exports it as uint32_t. OK deraadt@


# 1.82 19-Jan-2015 deraadt

like in ps(1), fetch the FSCALE value using sysctl rather than using
the header version
ok guenther


# 1.81 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.80 17-Sep-2014 dlg

if there are more than 8 cpus, default to combined cpu stats (like
you passed -1 on the command line).

ok kettenis@ tedu@


# 1.79 15-Sep-2014 miod

Remove non-standard <sys/dkstat.h> header. It has not contained anything
related to disk stastics for almost 17 years, and the remaining
userland-visible defines duplicate those found in <sys/sched.h>.

Move the remaining _KERNEL defines to <sys/tty.h> where they belong, and
update all users to cope with this.

ok kettenis@


Revision tags: OPENBSD_5_6_BASE
# 1.78 04-Jul-2014 guenther

Track whether a process is a zombie or not yet fully built via flags
PS_{ZOMBIE,EMBRYO} on the process instead of peeking into the process's
thread data. This eliminates the need for the thread-level SDEAD state.

Change kvm_getprocs() (both the sysctl() and kvm backends) to report the
"most active" scheduler state for the process's threads.

tweaks kettenis@
feedback and ok matthew@


# 1.77 08-Apr-2014 mpi

Use VM_UVMEXP instead of VM_METER for memory usages and directly
include <sys/vmmeter.h> where it is needed instead of relying on
it being included by <uvm/uvm_extern.h>.

miod@ likes it, ok guenther@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.76 23-Mar-2013 tedu

wchan is no longer exposed, just check if wmesg is not empty.
(wmesg hasn't itself been a pointer since conversion to kinfo_proc.)
noticed by sthen.
ok deraadt jsing millert sthen


Revision tags: OPENBSD_5_3_BASE
# 1.75 18-Dec-2012 millert

We no longer use struct eproc for kinfo_proc in sysctl.h so there
is no direct need for sys/proc.h or sys/resource.h. Some consumers
of kinfo_proc need these for the proc flags and rlimit defines like
RLIM_INF so add the appropriate includes to them.
OK deraadt@ sthen@


Revision tags: OPENBSD_5_2_BASE
# 1.74 09-Jul-2012 deraadt

According to miod, KERNBASE used as a userland symbol should die
in a fire. Start by removing the definitions, before we clean the
headers later.


# 1.73 05-Jun-2012 brynet

Add support for hiding a user's processes in top.

feedback & ok lum@


# 1.72 21-Apr-2012 guenther

Remove a check made superfluous by the KERN_PROC_SHOW_THREADS flag.
This lets new binaries work (at least w/o the -H option) with old kernels.

ok pirofti@ deraadt@


# 1.71 17-Apr-2012 pirofti

Make it optional for kvm_getprocs() (and related sysctl) to return
thread information.

Add a KERN_PROC_SHOW_THREADS flag that has to be set in order to get the
thread info and make it off by default. This creates backwards compatibility
for applications that relied on a given size/number of items to be returned.

Modify ps(1) and top(1) accordingly.

Okay guenther@.


# 1.70 12-Apr-2012 pirofti

Add per thread accounting, mainly for usage & friends.

This expands the already bloated FILL_KPROC macro to take an extra
parameter that indicates if the callee is a thread or a process.

The userland bits are adjusted accordingly and ps(1) and top(1) now
display per thread usage times when -H is used.

Also pkill(1) had to be adjusted so that duplicates don't pop up.

libkvm does basically the same thing as the kernel bits.

Okay guenther@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.69 12-Jul-2011 tedu

print out the amount of memory used for cache. ok otto


# 1.68 10-Apr-2011 guenther

Switch back from KERN_PROC2/kinfo_proc2 to KERN_PROC/kinfo_proc now
that we've got name we want for the API we want

"ZAP!" deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.67 26-Apr-2010 deraadt

more portable code


# 1.66 26-Mar-2010 lum

Make the source of process TIME for displaying and sorting the same.
top(1) and ps(1) now display the same info.
ok tedu@ millert@ otto@ and tested on sparc64 by landry@


Revision tags: OPENBSD_4_7_BASE
# 1.65 29-Jan-2010 tedu

Allow sorting by command and pid. Also, allow partial matches with strncmp.
ok jmc otto


Revision tags: OPENBSD_4_6_BASE
# 1.64 28-Apr-2009 deraadt

extend STATE column to fit > 9 cpus; Aivar Jaakson; PR6131


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.63 01-Nov-2007 otto

bump arbitrary limit on arg length shown by -C; requested by and ok deraadt@


Revision tags: OPENBSD_4_2_BASE
# 1.62 27-Jul-2007 deraadt

replace a few more malloc(n*m) idioms with calloc(n,m) for safety;
inspired by zinovik@cs.karelia.ru


# 1.61 29-May-2007 otto

Instead of using hand-crafted redraws minimizing screen updates, use curses.
Enables nice things like process highlighting without hurting the brain.
ok deraadt@


# 1.60 16-Apr-2007 otto

don't truncate wchan, ok miod@


# 1.59 13-Apr-2007 otto

include cleanup and fix in comment; from Mark Lumsden


# 1.58 04-Apr-2007 otto

missing prototypes; from Mark Lumsden with a twist by me.


Revision tags: OPENBSD_4_1_BASE
# 1.57 04-Feb-2007 otto

long and long long is not the same on 32 bits archs


# 1.56 03-Jan-2007 otto

Add a 'g' command to only show processes having a string in their
command name. ok deraadt@


# 1.55 02-Jan-2007 otto

top(1) doesn't use any math.h function, so do not include math.h and
do not link with libm. ok ray@


# 1.54 29-Nov-2006 miod

Do not test for processes being swapped out since this can't happen anymore.


# 1.53 20-Sep-2006 ray

Recommit memory leak fix.


Revision tags: OPENBSD_4_0_BASE
# 1.52 29-Apr-2006 otto

use calloc(3) instead of malloc(3) to allocate cpu state structs; it's
better style here; plus it prevents bogus data to show up on first display.
report + ok pedro@


Revision tags: OPENBSD_3_9_BASE
# 1.51 21-Dec-2005 millert

Userland programs should include <errno.h> not <sys/errno.h>
OK deraadt@


# 1.50 04-Dec-2005 tedu

support for toggling thread viewing.
ok brad, hints from jmc


Revision tags: OPENBSD_3_8_BASE
# 1.49 17-Jun-2005 markus

add a 'C' command ) that toggles the display of the full cmdline;
with Jared Yanovich; ok deraadt


# 1.48 08-Jun-2005 millert

Multi-cpu support for top. Each cpu now gets its own stats line.
Based on work by todd@ at the hackathon.


Revision tags: OPENBSD_3_7_BASE
# 1.47 06-Dec-2004 markus

missing space; bluhm at genua.de


# 1.46 22-Nov-2004 pat

simplify loop iteration

ok otto millert


# 1.45 17-Nov-2004 markus

off-by-one in process_states[], fixes display of memory; ok deraadt, aaron


# 1.44 07-Oct-2004 otto

introduce -p option and p command to only show a single process.
From Patrick Latifi. ok deraadt@ millert@


Revision tags: OPENBSD_3_6_BASE
# 1.43 13-Jun-2004 otto

Typos in comments and a redundant prototype. From Brian Poole via jmc@
ok jmc@


# 1.42 11-Jun-2004 deraadt

handle the KI_NOCPU case, pointed out by markus


# 1.41 11-Jun-2004 deraadt

only print /# if > 1 cpu on a machine


# 1.40 11-Jun-2004 deraadt

export cpuid via kproc2, and make ps & top aware... from niklas


# 1.39 11-Jun-2004 deraadt

hide top breakage, until the next commit which will do it right


# 1.38 09-May-2004 deraadt

spaces


Revision tags: OPENBSD_3_5_BASE
# 1.37 08-Jan-2004 millert

Convert to kinfo_proc2; ok deraadt@


Revision tags: OPENBSD_3_4_BASE
# 1.36 07-Jul-2003 deraadt

protos


# 1.35 18-Jun-2003 deraadt

remove unused variables


# 1.34 17-Jun-2003 jfb

properly typecast to uid_t the return value of proc_owner() and report
ESRCH instead of EACCES if it returns -1

ok millert@


# 1.33 15-Jun-2003 millert

use uid_t and fix some sign compare warnings; OK krw@ and deraadt@


# 1.32 14-Jun-2003 avsm

use correct length instead of sizeof(char *) for some strl* bound values
millert@ ok


# 1.31 13-Jun-2003 deraadt

readable code


# 1.30 12-Jun-2003 deraadt

knf


# 1.29 12-Jun-2003 pvalchev

cleanup; ok deraadt


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.28 02-Jul-2002 tholo

Add missing copyright


Revision tags: OPENBSD_3_1_BASE
# 1.27 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.26 05-Dec-2001 art

Get maxslp with sysctl. Not the constant.


Revision tags: OPENBSD_3_0_BASE
# 1.25 12-Jul-2001 deraadt

first pass at a -Wall cleanup


# 1.24 03-Jun-2001 angelos

Use KERN_NPROCS to get the number of processes on the system, to
determine the amount of space we'll need to store the
information. The alternative, calling sysctl() with a NULL argument
for data, meant the kernel had to go through the process table.


Revision tags: OPENBSD_2_9_BASE
# 1.23 22-Feb-2001 deraadt

plug mem leak i introduced; spotted by millert


# 1.22 17-Feb-2001 deraadt

bye bye setgid kvm


# 1.21 17-Feb-2001 deraadt

do not use nlist anymore, kvm gets the bullet next


# 1.20 17-Feb-2001 deraadt

partial KNF, because i cannot read the diffs i am trying to debug


Revision tags: OPENBSD_2_8_BASE
# 1.19 18-Jun-2000 niklas

Mark kernel threads as system processes. Make ps hide them by default,
show them with -k. Do not try to show RSS based values for them as they
mess up column alignment. vmstat -f now shows kernel threads separately
from rforks too.


Revision tags: OPENBSD_2_7_BASE
# 1.18 14-Nov-1999 deraadt

stathz instead of hz, plus improvements by me; camield@inet.unisource.nl


# 1.17 29-Oct-1999 todd

writen


Revision tags: OPENBSD_2_6_BASE
# 1.16 21-Jun-1999 art

stop casting the third argument to kvm_read to char *. it is a void *.


# 1.15 22-May-1999 weingart

Update for new vmswap code.


Revision tags: OPENBSD_2_5_BASE
# 1.14 28-Nov-1998 kstailey

do not count swap spaces that were not swapon(2)ed


Revision tags: OPENBSD_2_4_BASE
# 1.13 20-Sep-1998 niklas

cp_time is long, deal with it.


# 1.12 20-Sep-1998 niklas

widen unsigned entities to signed ones before subtracting, fixes sorting on alpha


# 1.11 21-Aug-1998 kstailey

support "-o field" and the interactive "o" command


# 1.10 08-Jul-1998 deraadt

whack kmem gid after kvm_openfiles()


# 1.9 25-Jun-1998 deraadt

handle missing swap


Revision tags: OPENBSD_2_3_BASE
# 1.8 08-Nov-1997 millert

Sort idle processes by total CPU usage, not cpticks since cpticks
get zeroed every second; dan@dan.emsphone.com


Revision tags: OPENBSD_2_2_BASE
# 1.7 09-Sep-1997 millert

"last pid" stuff is useless with random pids.


# 1.6 09-Sep-1997 millert

Use kvm_openfiles() not the sunos compat kvm_open().
Use warn/warnx where it makes sense and check some more ret vals.


# 1.5 24-Aug-1997 millert

Fix compiler warnings.


# 1.4 22-Aug-1997 downsj

Two more alpha warnings.


# 1.3 22-Aug-1997 downsj

First sweep. Prototype, type fixes, long fixes, mostly compiles with
-Wall.


# 1.2 17-Aug-1997 kstailey

print "idle" for idle processes


# 1.1 14-Aug-1997 downsj

top 3.4, with a few changes. Still needs more work.


Revision tags: OPENBSD_6_2_BASE
# 1.89 30-May-2017 tedu

avoid some shadow warnings. from Brian Callahan


Revision tags: OPENBSD_6_1_BASE
# 1.88 15-Mar-2017 deraadt

annoying whitespace die die die


# 1.87 28-Jul-2016 tedu

rework realloc loop. there's no need to shrink the allocation between
calls. if we need a big space once, we'll likely need a big space again.


Revision tags: OPENBSD_6_0_BASE
# 1.86 11-May-2016 edd

Allow top(1) to filter process args if they are being displayed.

OK tedu@, deraadt@, and with input from Michal Mazurek.

Thanks


Revision tags: OPENBSD_5_9_BASE
# 1.85 20-Aug-2015 deraadt

Do not cast result of malloc/calloc/realloc* if stdlib.h is in scope
ok krw millert


Revision tags: OPENBSD_5_8_BASE
# 1.84 06-May-2015 mpi

Display thread IDs instead of the name of the process's owner when "-H"
is used.

The rationnal is that when you're looking at threads you're generally
already filtereing by PID and this allow you to see which thread is a
pig.

Written some time ago with mikeb@

ok sthen@, krw@, guenther@


Revision tags: OPENBSD_5_7_BASE
# 1.83 19-Jan-2015 millert

Kill loadavg.h and just use the fscale value directly.
There's no need for crazy casts of p_pctcpu in ORDERKEY_PCTCPU
since kinfo_proc exports it as uint32_t. OK deraadt@


# 1.82 19-Jan-2015 deraadt

like in ps(1), fetch the FSCALE value using sysctl rather than using
the header version
ok guenther


# 1.81 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


# 1.80 17-Sep-2014 dlg

if there are more than 8 cpus, default to combined cpu stats (like
you passed -1 on the command line).

ok kettenis@ tedu@


# 1.79 15-Sep-2014 miod

Remove non-standard <sys/dkstat.h> header. It has not contained anything
related to disk stastics for almost 17 years, and the remaining
userland-visible defines duplicate those found in <sys/sched.h>.

Move the remaining _KERNEL defines to <sys/tty.h> where they belong, and
update all users to cope with this.

ok kettenis@


Revision tags: OPENBSD_5_6_BASE
# 1.78 04-Jul-2014 guenther

Track whether a process is a zombie or not yet fully built via flags
PS_{ZOMBIE,EMBRYO} on the process instead of peeking into the process's
thread data. This eliminates the need for the thread-level SDEAD state.

Change kvm_getprocs() (both the sysctl() and kvm backends) to report the
"most active" scheduler state for the process's threads.

tweaks kettenis@
feedback and ok matthew@


# 1.77 08-Apr-2014 mpi

Use VM_UVMEXP instead of VM_METER for memory usages and directly
include <sys/vmmeter.h> where it is needed instead of relying on
it being included by <uvm/uvm_extern.h>.

miod@ likes it, ok guenther@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.76 23-Mar-2013 tedu

wchan is no longer exposed, just check if wmesg is not empty.
(wmesg hasn't itself been a pointer since conversion to kinfo_proc.)
noticed by sthen.
ok deraadt jsing millert sthen


Revision tags: OPENBSD_5_3_BASE
# 1.75 18-Dec-2012 millert

We no longer use struct eproc for kinfo_proc in sysctl.h so there
is no direct need for sys/proc.h or sys/resource.h. Some consumers
of kinfo_proc need these for the proc flags and rlimit defines like
RLIM_INF so add the appropriate includes to them.
OK deraadt@ sthen@


Revision tags: OPENBSD_5_2_BASE
# 1.74 09-Jul-2012 deraadt

According to miod, KERNBASE used as a userland symbol should die
in a fire. Start by removing the definitions, before we clean the
headers later.


# 1.73 05-Jun-2012 brynet

Add support for hiding a user's processes in top.

feedback & ok lum@


# 1.72 21-Apr-2012 guenther

Remove a check made superfluous by the KERN_PROC_SHOW_THREADS flag.
This lets new binaries work (at least w/o the -H option) with old kernels.

ok pirofti@ deraadt@


# 1.71 17-Apr-2012 pirofti

Make it optional for kvm_getprocs() (and related sysctl) to return
thread information.

Add a KERN_PROC_SHOW_THREADS flag that has to be set in order to get the
thread info and make it off by default. This creates backwards compatibility
for applications that relied on a given size/number of items to be returned.

Modify ps(1) and top(1) accordingly.

Okay guenther@.


# 1.70 12-Apr-2012 pirofti

Add per thread accounting, mainly for usage & friends.

This expands the already bloated FILL_KPROC macro to take an extra
parameter that indicates if the callee is a thread or a process.

The userland bits are adjusted accordingly and ps(1) and top(1) now
display per thread usage times when -H is used.

Also pkill(1) had to be adjusted so that duplicates don't pop up.

libkvm does basically the same thing as the kernel bits.

Okay guenther@.


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE
# 1.69 12-Jul-2011 tedu

print out the amount of memory used for cache. ok otto


# 1.68 10-Apr-2011 guenther

Switch back from KERN_PROC2/kinfo_proc2 to KERN_PROC/kinfo_proc now
that we've got name we want for the API we want

"ZAP!" deraadt@


Revision tags: OPENBSD_4_8_BASE OPENBSD_4_9_BASE
# 1.67 26-Apr-2010 deraadt

more portable code


# 1.66 26-Mar-2010 lum

Make the source of process TIME for displaying and sorting the same.
top(1) and ps(1) now display the same info.
ok tedu@ millert@ otto@ and tested on sparc64 by landry@


Revision tags: OPENBSD_4_7_BASE
# 1.65 29-Jan-2010 tedu

Allow sorting by command and pid. Also, allow partial matches with strncmp.
ok jmc otto


Revision tags: OPENBSD_4_6_BASE
# 1.64 28-Apr-2009 deraadt

extend STATE column to fit > 9 cpus; Aivar Jaakson; PR6131


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE
# 1.63 01-Nov-2007 otto

bump arbitrary limit on arg length shown by -C; requested by and ok deraadt@


Revision tags: OPENBSD_4_2_BASE
# 1.62 27-Jul-2007 deraadt

replace a few more malloc(n*m) idioms with calloc(n,m) for safety;
inspired by zinovik@cs.karelia.ru


# 1.61 29-May-2007 otto

Instead of using hand-crafted redraws minimizing screen updates, use curses.
Enables nice things like process highlighting without hurting the brain.
ok deraadt@


# 1.60 16-Apr-2007 otto

don't truncate wchan, ok miod@


# 1.59 13-Apr-2007 otto

include cleanup and fix in comment; from Mark Lumsden


# 1.58 04-Apr-2007 otto

missing prototypes; from Mark Lumsden with a twist by me.


Revision tags: OPENBSD_4_1_BASE
# 1.57 04-Feb-2007 otto

long and long long is not the same on 32 bits archs


# 1.56 03-Jan-2007 otto

Add a 'g' command to only show processes having a string in their
command name. ok deraadt@


# 1.55 02-Jan-2007 otto

top(1) doesn't use any math.h function, so do not include math.h and
do not link with libm. ok ray@


# 1.54 29-Nov-2006 miod

Do not test for processes being swapped out since this can't happen anymore.


# 1.53 20-Sep-2006 ray

Recommit memory leak fix.


Revision tags: OPENBSD_4_0_BASE
# 1.52 29-Apr-2006 otto

use calloc(3) instead of malloc(3) to allocate cpu state structs; it's
better style here; plus it prevents bogus data to show up on first display.
report + ok pedro@


Revision tags: OPENBSD_3_9_BASE
# 1.51 21-Dec-2005 millert

Userland programs should include <errno.h> not <sys/errno.h>
OK deraadt@


# 1.50 04-Dec-2005 tedu

support for toggling thread viewing.
ok brad, hints from jmc


Revision tags: OPENBSD_3_8_BASE
# 1.49 17-Jun-2005 markus

add a 'C' command ) that toggles the display of the full cmdline;
with Jared Yanovich; ok deraadt


# 1.48 08-Jun-2005 millert

Multi-cpu support for top. Each cpu now gets its own stats line.
Based on work by todd@ at the hackathon.


Revision tags: OPENBSD_3_7_BASE
# 1.47 06-Dec-2004 markus

missing space; bluhm at genua.de


# 1.46 22-Nov-2004 pat

simplify loop iteration

ok otto millert


# 1.45 17-Nov-2004 markus

off-by-one in process_states[], fixes display of memory; ok deraadt, aaron


# 1.44 07-Oct-2004 otto

introduce -p option and p command to only show a single process.
From Patrick Latifi. ok deraadt@ millert@


Revision tags: OPENBSD_3_6_BASE
# 1.43 13-Jun-2004 otto

Typos in comments and a redundant prototype. From Brian Poole via jmc@
ok jmc@


# 1.42 11-Jun-2004 deraadt

handle the KI_NOCPU case, pointed out by markus


# 1.41 11-Jun-2004 deraadt

only print /# if > 1 cpu on a machine


# 1.40 11-Jun-2004 deraadt

export cpuid via kproc2, and make ps & top aware... from niklas


# 1.39 11-Jun-2004 deraadt

hide top breakage, until the next commit which will do it right


# 1.38 09-May-2004 deraadt

spaces


Revision tags: OPENBSD_3_5_BASE
# 1.37 08-Jan-2004 millert

Convert to kinfo_proc2; ok deraadt@


Revision tags: OPENBSD_3_4_BASE
# 1.36 07-Jul-2003 deraadt

protos


# 1.35 18-Jun-2003 deraadt

remove unused variables


# 1.34 17-Jun-2003 jfb

properly typecast to uid_t the return value of proc_owner() and report
ESRCH instead of EACCES if it returns -1

ok millert@


# 1.33 15-Jun-2003 millert

use uid_t and fix some sign compare warnings; OK krw@ and deraadt@


# 1.32 14-Jun-2003 avsm

use correct length instead of sizeof(char *) for some strl* bound values
millert@ ok


# 1.31 13-Jun-2003 deraadt

readable code


# 1.30 12-Jun-2003 deraadt

knf


# 1.29 12-Jun-2003 pvalchev

cleanup; ok deraadt


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.28 02-Jul-2002 tholo

Add missing copyright


Revision tags: OPENBSD_3_1_BASE
# 1.27 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.26 05-Dec-2001 art

Get maxslp with sysctl. Not the constant.


Revision tags: OPENBSD_3_0_BASE
# 1.25 12-Jul-2001 deraadt

first pass at a -Wall cleanup


# 1.24 03-Jun-2001 angelos

Use KERN_NPROCS to get the number of processes on the system, to
determine the amount of space we'll need to store the
information. The alternative, calling sysctl() with a NULL argument
for data, meant the kernel had to go through the process table.


Revision tags: OPENBSD_2_9_BASE
# 1.23 22-Feb-2001 deraadt

plug mem leak i introduced; spotted by millert


# 1.22 17-Feb-2001 deraadt

bye bye setgid kvm


# 1.21 17-Feb-2001 deraadt

do not use nlist anymore, kvm gets the bullet next


# 1.20 17-Feb-2001 deraadt

partial KNF, because i cannot read the diffs i am trying to debug


Revision tags: OPENBSD_2_8_BASE
# 1.19 18-Jun-2000 niklas

Mark kernel threads as system processes. Make ps hide them by default,
show them with -k. Do not try to show RSS based values for them as they
mess up column alignment. vmstat -f now shows kernel threads separately
from rforks too.


Revision tags: OPENBSD_2_7_BASE
# 1.18 14-Nov-1999 deraadt

stathz instead of hz, plus improvements by me; camield@inet.unisource.nl


# 1.17 29-Oct-1999 todd

writen


Revision tags: OPENBSD_2_6_BASE
# 1.16 21-Jun-1999 art

stop casting the third argument to kvm_read to char *. it is a void *.


# 1.15 22-May-1999 weingart

Update for new vmswap code.


Revision tags: OPENBSD_2_5_BASE
# 1.14 28-Nov-1998 kstailey

do not count swap spaces that were not swapon(2)ed


Revision tags: OPENBSD_2_4_BASE
# 1.13 20-Sep-1998 niklas

cp_time is long, deal with it.


# 1.12 20-Sep-1998 niklas

widen unsigned entities to signed ones before subtracting, fixes sorting on alpha


# 1.11 21-Aug-1998 kstailey

support "-o field" and the interactive "o" command


# 1.10 08-Jul-1998 deraadt

whack kmem gid after kvm_openfiles()


# 1.9 25-Jun-1998 deraadt

handle missing swap


Revision tags: OPENBSD_2_3_BASE
# 1.8 08-Nov-1997 millert

Sort idle processes by total CPU usage, not cpticks since cpticks
get zeroed every second; dan@dan.emsphone.com


Revision tags: OPENBSD_2_2_BASE
# 1.7 09-Sep-1997 millert

"last pid" stuff is useless with random pids.


# 1.6 09-Sep-1997 millert

Use kvm_openfiles() not the sunos compat kvm_open().
Use warn/warnx where it makes sense and check some more ret vals.


# 1.5 24-Aug-1997 millert

Fix compiler warnings.


# 1.4 22-Aug-1997 downsj

Two more alpha warnings.


# 1.3 22-Aug-1997 downsj

First sweep. Prototype, type fixes, long fixes, mostly compiles with
-Wall.


# 1.2 17-Aug-1997 kstailey

print "idle" for idle processes


# 1.1 14-Aug-1997 downsj

top 3.4, with a few changes. Still needs more work.