[comp.unix.xenix] Performance monitor

jct@jct.UUCP (jct) (06/02/89)

John C. Tompkins
4616 NW 59th St.
Oklahoma City, OK 73122
(405) 728-1208

I have noticed recently a lot of interest in programs that look into the 
computer system and measure various performace parameters. I can make 
available a system utility I developed for monitoring computer usage which 
everything these do and a WHOLE ot more. I would prefer not releasing the 
source code because it is 1) quite large (almost 450K), and 2) it uses a lot 
of personal library functions. I can be persuaded of course. The binary itself
is almost 100K (medium model) and also uses 30K of help files. Its big but its
nice!

NOTE: Since the program reads kernal data structures, it is s VERY dependent 
on your operating system version. It has only been tested, in this binary 
form, on SCO XENIX 286 2.2.1. It should run, but has not been tested, on other 
XENIX 286 2.x releases. It will probably NOT run, in this binary form, on 
XENIX 386.  It will NOT run, in this binary form, on Microport UNIX, VENIX or 
anything else. It will run on these, and other systems, but not in this binary 
format. For example, it also currently runs on a Convergent MiniFrame CTIX 
system, in another binary format. I someone else wants to adapt to other XENIX 
versions (such as 386) or other UNIXs I can make the source available.

The monitor program is a full screen, menu driven performance monitor for
XENIX and UNIX. It will show all kinds of information about what your
computer is doing in real time in nice graphs and numerically. It does most 
everything ps, pstat ... do and a lot more, in a lot better format. It will, 
for example, show free RAM memory space, per process signal handling, per 
process open files, top CPU usage processes, system call frequency, CPU idle 
time, number of free process slots, kill running processes (if you have
privelege) and so on. I find it to be VERY useful in program development and 
system administration. If its not the ultimate (whatever is?) its a heck
of a start.

Monitor requires a "glass" tty, line printer terminals will not work.  It 
contains context sensitive help on all screens and, I hope, is easy to use. 
All menus have 2 second type-ahead, if you make a valid entry in 2 seconds 
the menu is not shown. Entrys are made by using the up and down arrows and 
pressing ENTER or by pressing the key corresponding to the menu entry. Help 
may be called by function key 1, escape-h or '?'. If you are in a menu and 
have an entry highlighted, help is given on that entry, if no entry is 
highlighted, general help. After an entry is selected, help gives help for 
that entry. Help may be moved through by the down arrow for the next page or 
the up arrow for the previous page.

Monitor will typically consume 1 to 2.5 percent of your CPU time for a 5 second
sample interval. If you flip among screens or call help the CPU time will of
course go up. Under static displays (which still update at the sample interval)
1 to 2.5 percent on the console of an 8 mHZ IBM-AT is typical. It depends
somewhat on what you are currently monitoring. Output to remote serial
terminals, more so if monochrome, will be lower consumers. Faster machines will
have lower consumption of course while a shorter sample interval will increase
CPU usage. 

Five files are included-> monitor, monitor.hlp, help.hlp, screen.hlp and hindex.

Installation requires a few more steps than most programs. Because it must
read operating system and user data structures (just like ps), it requires
read access to the following files :

	/xenix
        /dev/mem
        /dev/kmem
        /dev/swap

It only needs read access, write access is not required. This can be done
by the following. /xenix is readable by everyone. On XENIX the 3 /dev files
are owned by 'sysinfo', the group is also 'sysinfo', they have read/write 
access for the owner and no access for group or others. Setting the monitor 
program to set-userid and making the owner of monitor to be sysinfo gives 
read/write access to the monitor program. However, write access, as mentioned,
is not needed. Since you have no reason to trust me, I wouldn't either if 
someone could write to my kernel, you should instead change the group on 
monitor to sysinfo and make it set-groupid and remove the set-userid. 
The owner can still be sysinfo, it doesn't matter. Also, add group read 
privilege to the files /dev/mem, /dev/kmem and /dev/swap. This gives read 
only access to these files. After program startup, monitor will set its 
effective uid and gid back to the real uid and gid. Monitor has no facilities 
for creating subproceses.

The program needs to be able to read the namelist from /xenix, so read
access is required. By default XENIX allows that. The program will start up
faster, without reading the namelist each time, if you allow it to write a
data file named 'monitor.dat' in the directory /usr/lib/ps. Normally this
directory is owned by sysinfo but the group is bin. Change the group to
sysinfo and add group write access to the directory. The created file,
'monitor.dat' will be set to mode 0644 on creation. If the /xenix
modification time is newer than the modification time on monitor.dat,
monitor will update monitor.dat when it is started.

The program uses an enhanced termcap entry. The following are special key
entries (along with default keystrokes) :

KI (ESCi) - Insert toggle key
KT (ESCt) - Top or Home key
KB (ESCb) - Bottom or End key
KF (ESCd) - Forward or Page forward (down) key
KR (ESCu) - Reverse or Page back (up) key
KH (ESCh) - Help key
KD (^C)   - Delete character under cursor key
KX (ESCx) - Clear entire input key
KC (ESCc) - Cancel input and return key
KP (ESCp) - Print screen key
KQ (ESCq) - Quit key
KK (ESC ) - Backup key

The program uses screen routines, like curses, but allows for color and
use of a terminals line drawing set. These features make it look much
nicer but are not required. If color is not defined for a terminal, no
color is used and if the line drawing set is not defined, a '-' is used
for horizontal lines, '|' for vertical lines and '+' for corners and ticks.
Having reverse video (standout) defined also helps but is not required.
An example using the console terminal will help explain. The console is
listed in /etc/termcap as 'ansi' and the following additions are used :

        GS=\E12m:GE=\E10m:GB=D3EZB?4YA@C:

This defines the line drawing set, using single lines. GS is the sequence
to start 'graphic' mode. GE ends 'graphic' mode and GB is the 11 character
set to draw, horizontal, vertical, center cross, upper left corner, down
tick (like a T), upper right corner, left tick (-|), lower right corner,
up tick (inverted T), lower left corner and right tick (|-).

Colors are defined as follows, Although this scheme may be too simple for
many color capable terminals it does work on the console and Tektronix 41xx :

        RC:SC=\E[2;%f;%bm\E[3m%c0,4,2,14,1,5,3,7,8,9,10,11,12,13,6,15:

RC is a boolean that means, if color is defined and reverse video (standout)
is requested, Reverse Color instead. SC is the Set Color string, %f is the
location in the string where Foreground color is set, %b is where Background
color is set. %c is the end of the set color string and the start of the
color definition. 16 colors must be specified even if some or all repeat
the same color. In order they are: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA,
CYAN, WHITE, COLOR_8, COLOR_9, COLOR_10, COLOR_11, COLOR_12, COLOR_13,
COLOR_14, COLOR_15. Each color entry is delimited by a comma and is a string
of one or more characters. This example just happens to use strings that
look like numbers. COLOR_8 thru COLOR_15 are not used in the program but
must be given a value.

Now, to specify colors, a file /etc/monitorc is used, if a '.monitorc'
is in a users HOME directory it is used instead. It is quite simple and
contains lines like :

FG BLUE
BG WHITE
HELP_FG WHITE
HELP_BG RED

Which means, if the terminal can do color, make the foreground blue and
the background white. Also, the default help foreground is white and the
default help background is red. Specifications in the help file itself may
be used to override the default. The program only uses 2 colors but I have
tried many more, I think 2 works best. The named colors may be any of those
given above, upper or lower case makes no difference.

Lastly, the help files 'monitor.hlp', 'help.hlp' and 'screen.hlp' should be
put in the directory /usr/lib/help. If you make any changes to the help
information, issue the command 'hindex monitor.hlp', otherwise the indexes in
the help file are not up to date and you get funny looking help.

The help file may also contain various formatting information such as colors,
pop-up window size and location, subhelp menus and many others we don't want
to get into here. The formatting takes the form of "dot commands", for
example .XXC GREEN BLACK starting in the first column says set the
foreground color, for this help screen only, to green and the background
to black. .XXW 12 50 7 15 make this help screen a pop-up window 12 lines
by 50 columns starting at the 7th line and 15th column. .XXT sets the "title"
for a subhelp menu and .XXS sets the available subhelp "selections".
There are a lot of options in the help files that I don't cover completely
here. Please look at the included help files and I think you can figure out
most of it given this start.

Note that the screen will update its data every 5 seconds, by default. Use the 
command line option -i # or the runtime menu entry 'W' to tell how often to
update the screen. If you have a pop-up help window, the data under the window 
will still update every time interval seconds.

You may set environment variable SCR_PRINTER or SCR_FILE to specify
destinations for copying screen contents when the ESC-p sequence is pressed.
For example :

SCR_FILE=+monitor.scr
SCR_PRINTER=dumb

Says to append the screen dump to the file monitor.scr, without the leading
'+' the file will be overwritten. Note that in this format the line drawing
characters will always be dumped as '-', '|' and '+'. The printer that is
dumped to is any valid lp printer name.

Valid command line options are :

        -i number       - set update interval to "number" of seconds
                          instead  of default 5.
        -u user_name    - set initial user process monitor name to
                          "user_name". The default is the invoking user
                          name unless you are root which sets it to "All".
        -a              - set initial user process monitor name to "All".

Known problems :

For some reason the initial login shell process start time will often (but
not always) be wrong -> it will show several hours or days before the actual
start. The normal utilities such as 'ps' also show the same incorrect time. 
So, I figure its not really my problem, its in XENIX itself.

Process name are sometimes messed up if a lot of swapping is going on. Names
are stored in the swap area and if swapping occurs while reading ... Its no
disaster, just mangled data.

Under user process monitor, the second screen, you will only see the first
10 or 11 (depending on the number of lines on your screen) non-default signal
statuses. There is no way to scroll down. Likewise, only the first 18 or 19
open files are shown. I just haven't gotten around to fixing these.

Open files are only listed as i-node numbers, there is no efficient way
I know of to get a pathname from an i-number. Everything, such as ncheck,
takes way too much CPU time. Again, its not my fault, its just the way 
UNIX/XENIX stores open files in the kernel to save data space.