top

dynamically show processes, memory usage

Many implementations exist and all have different options, this is for ??
-c event counting : accumulative , delta. (relative to previous sample), e Absolute
non-event (default). CPU usage since previous sample.
-l nsamples logging mode in raw form.
-L non-logging i.e. interactive
-n nprocs
-o key sort on key in descending order. + prefixed ascending, - descending
-O skey secondary sort key

    cpu CPU usage.
    pid Process ID (default).
    prt Number of Mach ports.
    reg Number of memory regions.
    rprvt Resident private address space size.
    rshrd Resident shared address space size.
    rsize Resident memory size.
    th Number of threads.
    time Execution time.
    uid User ID.
    username
    vprvt Private address space size.
    vsize Total memory size.
-r recursively traverse and report the memory object map for each process (default).
-R not recursive . This substantially reduces the amount of processor time top consumes.
-s secs speed of updates
-t Translate uid numbers to usernames.
-T Do not " "
-U user Only display processes owned by user.
username or uid
-F omitt frameworks(i.e. shared libraries) substantially reduces processor time top consumes.
-f include shared libraries(default).

Command line options are processed from left to right.
If conflicting options are specified, later specifications overrides .
making it viable to create a shell alias for top with preferred defaults , then override those preferred defaults as desired on the command line.

The first several lines show the global state.

CPU Percentage of processor usage, broken into user, system, and idle components.
Disks reads and writes.
LoadAvg over 1, 5, and 15 minutes.
MemRegions Number and total size of memory regions, and total size of memory regions broken into private (broken into non-library and library) and shared components.
Networks packets.
PhysMem wired, active, inactive, used, and free components.
Procs Total number of processes and number of processes in each process state.
SharedLibs Number of shared libraries, resident sizes of code and data segments, and link editor memory usage.
Threads .
Time in accumulative event counting mode, the time since top started is printed in parentheses
VirtMem Total virtual memory, virtual memory consumed by shared libraries, and number of pageins and pageouts.

COW_FAULTS Copy On Write, faults causing a page to be copied.
%CPU
CSWITCH context switches.
MSGS_RCVD Mach messages received.
MSGS_SENT Mach sent.
PRT(delta) Mach ports.
RPRVT(delta) Resident private memory size.
RSHRD(delta) Resident shared
RSIZE(delta) Total resident , including shared pages.
BSYSCALL BSD system calls
TH Number of threads.
TIME Absolute processor time consumed.
VPRVT(delta) Private address space size.
VSIZE(delta) Total address space allocated, including shared pages.

Interaction

When interactive, control the output as well as sendinf signals to processes.
Each command is one character, followed by up to 2 arguments.
Commands that take arguments prompt for them the default value is shown in square brackets.
The default value can be selected by leaving the input field blank and pressing enter.
^G escapes the interactive argument prompt and is the same as leaving the input field blank and pressing enter.

other Keypresses cause a sample update.

^L Redraw the screen.
cmode accumulative , delta, event, non-event mode.
sdelay
n Only display processes. 0 =all
okey sort on key in descending order. + ascending -descending
Oskey skey secondary key

command
cpu
pid
prt Number of Mach ports.
reg Number of memory regions.
rprvt Resident private address space size.
rshrd Resident shared
vprvt Private
rsize Resident memory size.
th Number of threads.
time Execution time.
vsize Total memory size.
uid
username
 
toggles
f frameworks
r recursive (traversal)of the memory object map for each process.
t translation of uid numbers to usernames.
U Only display processes owned by . Either the username or uid number can be specified. To display all processes, press enter without entering a username or uid number.
w wide/narrow delta
x deprecated/normal
S signal pid a number or as a name (for example, INT, HUP). Default TERM. Each time a signal is successfully sent, the default signal is updated to be that signal.
q
?
deprecated-
-a use -ca.
-d use -cd.
-e use -ce.
-k memory object reporting for process 0 (kernel_task), default.
-u use -ocpu -Otime.

EXAMPLES

top -ocpu -O+rsize -s 5 -n 20
Sort the processes according to CPU usage (descending) and
sOresident memory size (ascending),
update every 5 seconds
and limit to 20 processes.
top -ce # Run top in event counter mode.
top -tl 10
Translate uid numbers to usernames and run in logging mode, taking 10 samples at 2 second intervals.

See htop, kill, vm_stat, signal

from BSD Apple darwin 7.9.0 Each version of *nix has their own implementaition of top.
This document reflects Apple Mac OS X El Captian.

top [-a | -d | -e | -c mode] [-F | -f] [-h] [-i interval] [-l samples] [-ncols columns] [-o key] [-O ]
      [-R | -r] [-S] [-s delay] [-n nprocs] [-stats keys] [-pid processid] [-user username] [-U username] [-u]

Periodically displays a list of system processes. The default sorting key is pid

Options are processed from left to right. For repeated options: later specifications override earlier ones.
This makes it viable to create a shell alias for top with preferred defaults then override those preferred defaults as desired on the command line.

-c mode
-d
-cd
Delta mode. Since previous sample. Default.
-a
-ca
Accumulative mode. Count events, CPU usage and time, since the launch of top.
Disables the memory object map reporting, re-enabled with -r or the interactive r command.
-e
-ce
Absolute mode. (i.e. since startup)
n Non-event mode (default). Calculate CPU usage since the previous sample.
-d
-a
-e
Delta
Accumulative
Absolute

-f Calculate statistics on shared libraries, also known as frameworks (default).
-F Do not "
-h help
-i samples Update framework info every samples ; see the PERFORMANCE vs. ACCURACY section below for more details.
-l samples logging mode and display samples samples
Rather than redisplaying, output is periodically output in raw form.
The first sample will have an invalid %CPU for each process, as it is calculated using the delta between samples.
-ncols columns Display columns in logging mode.
-n nprocs Only display nprocs processes.
-o key Order the process display by sorting on key in descending order.
A + or - can be prefixed to the key name to specify ascending or descending
-O skey Use skey as a secondary key
command
cpu       
time       Execution time.  
#th        threads (total/running).
threads
wq     
#wq workqueue total/running.
ports  
prt    Mach ports.  
mem    Internal memory  (0B zero bytes)
PURG   purgable
CMPR   compressed
pgrp   Process group id
ppid   Parent process id
state  running | sleeping | stuck
boosts 
 held by the process transitions 
  from unboosted to boosted 

An asterisk indicates 
  the  process was  able to send boosts 
  at some point since the previous update. 
  See  xpc_transaction_begin(3). 

cpu_me CPU time charged to me 
        by other processes.
cpu_others CPU time charged to 
        other processes by me.  
uid    User ID.  
faults    page faults.  
cow       copy-on-write faults.  

msgsent   mach messages sent
msgrecv   mach messages received
sysbsd    BSD syscalls  
sysmach   Mach syscalls
csw       context switches
pageins   pageins
idle
power

mregion
mreg 
reg    memory regions.

rprvt  Resident private address space
purg   Purgeable
vprvt  Private address space 
kprvt  kernel Private
kshrd  kernel Shared
vsize  Total 

pstate Process state.  
user alias: username Username.
-u -o cpu -O time.
-r Traverse and report the memory object map for each process (default).
-R Do not ""
-S Display the global statistics for swap and purgeable memory.
-s delay Set the delay between updates to delay seconds. Default: 1 second.
-stats keys Only display the comma separated statistics.
-pid processid Only display processid in top.
-user user
-U
Only display processes owned by user.

DISPLAY


prio / nice is not displayed! (see ps -o nice -o pri
The first several lines of show global state.
Procs Total number of processes and number of processes in each process state.
Threads Number of threads.
Time in logging mode YYYY/MM/DD HH:MM:SS format by default. overridden with accumulative mode.
When running in accumulative event counting mode, the Time is in HH:MM:SS since the beginning of the top process.
 
LoadAvg Load average over 1, 5, and 15 minutes. i.e. average number of jobs in the run queue.
CPU Percentage of processor usage, by user, system, and idle components.
The time period for which these percentages are calculated depends on the event counting mode.
SharedLibs Resident sizes of code and data segments, and link editor memory usage.
MemRegions Number and total size of memory regions, and total size of memory regions by private (by non-library and library) and shared components.
 
PhysMem Physical memory usage, broken into wired, active, inactive, used, and free components.
VirtMem Total virtual memory, virtual memory consumed by shared libraries, and number of pageins and pageouts.
Networks Number and total size of input and output network packets.
Disks Number and total size of disk reads and writes.
Swap Swap usage: total size of swap areas, amount of swap space in use and amount of swap space available.
Purgeable Number of pages purged and number of pages currently purgeable.

Below the global state fields, a list of processes is displayed.
pid displays the following for the architecture:
+ for 64-bit native
- for 32-bit native
* for a non-native .

Interaction

Commands are one character, arguments are prompted for, (default shown in brackets), selected by pressing enter.
c mode
o key sorting on key prefix + ascending or - descending.
O skey skey is secondary key
r Toggle traversal and reporting of the memory object map for each process.
S signal pid Send sig to pid
   default starts out as TERM.
   Each time a signal is sent, the default signal is updated to be that signal.
sdelay between updates
Uuser Only display processes owned by user.
  Either the username or uid number
? Display the help screen.
^L Redraw the screen.
q Quit.
To display all processes, press enter

PERFORMANCE vs. ACCURACY

Calculating detailed memory statistics is resource-intensive. To minimize the cpu usage stats are updated every 10 samples. -i 1 will result in the most accurate display, at the expense of system resources.

N/A Not Available: Caused by the memory object map reporting being disabled. (default in delta mode)
, but may be enabled via -r or the interactive r command after any -c

EXAMPLES

Sort the processes according to
  • CPU usage (descending)
  • resident memory size (ascending)
  • sample and update the display at 5 second intervals and
  • limit the display to 20 processes.
top -o cpu    -O +rsize    -s 5    -n 20
Run top in delta mode.
top -c d
Display only the specified statistics, regardless of width chage of the terminal. If too narrow, only the statistics that fit will be displayed.
top -stats pid,command,cpu,th,pstate,time
smac14 .zsh_sessions % brew install htop
Running `brew update --auto-update`...

==> Auto-updated Homebrew!
==> Fetching dependencies for htop: ncurses
==> Fetching ncurses
==> Downloading https://ghcr.io/v2/homebrew/core/ncurses/manifests/6.3
####################### 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/ncurses/blobs/sha256:
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:
####################### 100.0%
==> Fetching htop
==> Downloading https://ghcr.io/v2/homebrew/core/htop/manifests/3.2.1
smac14 .zsh_sessions % brew install htop
Running `brew update --auto-update`...

==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).

==> Fetching dependencies for htop: ncurses
==> Fetching ncurses
==> Downloading https://ghcr.io/v2/homebrew/core/ncurses/manifests/6.3
==> Downloading https://ghcr.io/v2/homebrew/core/ncurses/blobs/sha256:xx
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:xx
==> Fetching htop
==> Downloading https://ghcr.io/v2/homebrew/core/htop/manifests/3.2.1
==> Downloading https://ghcr.io/v2/homebrew/core/htop/blobs/sha256:xx
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:xx
==> Installing dependencies for htop: ncurses
==> Installing htop dependency: ncurses
==> Pouring ncurses--6.3.arm64_ventura.bottle.tar.gz
  /opt/homebrew/Cellar/ncurses/6.3: 3,968 files, 9.6MB
==> Installing htop
==> Pouring htop--3.2.1.arm64_ventura.bottle.tar.gz
==> Caveats
htop requires root privileges to correctly display all running processes,
 % sudo htop

    0[||||||||                                                 10.5%]   4[                                                           0.0%]
    1[||||||                                                    8.6%]   5[                                                           0.0%]
    2[||||                                                      4.7%]   6[                                                           0.0%]
    3[||                                                        2.0%]   7[                                                           0.0%]
  Mem[||||||||||||||||||||||||||||||                     8.13G/32.0G] Tasks: 579, 2074 thr, 0 kthr; 1 running
  Swp[                                                         0K/0K] Load average: 1.46 1.41 1.45 
                                                                      Uptime: 2 days, 10:32:24

  [Setup]
Categories       Header Layout
Display options  [x]    2 columns - 50/50 (default)
Header layout    [ ]    2 columns - 33/67
Meters           [ ]    2 columns - 67/33
Screens          [ ]    3 columns - 33/34/33
Colors           [ ]    3 columns - 25/25/50
                 [ ]    3 columns - 25/50/25
                 [ ]    3 columns - 50/25/25
                 [ ]    3 columns - 40/20/40
                 [ ]    4 columns - 25/25/25/25

F1      F2      F3      F4      F5      F6      F7      F8      F9      F10Done