			 How Big Must a SWAPFILE Be?
			       by R. O. Weber
			     Cyclotron Institute
			    Texas A&M University
			College Station, Texas  77843

	When the swapper decides to outswap a process, it writes the pages
currently in the process's working set into the file [SYSEXE]SWAPFILE.SYS.
The Installation Guide states that the number of blocks in SWAPFILE must be
equal to the product of the two system parameters MAXPROCESSCNT and WSMAX.
Other DEC spokespersons will say that if SWAPFILE is smaller than MAXPROCESSCNT
times WSMAX, the number of processes allowed on the system will be reduced
accordingly.  The number MAXPROCESSCNT times WSMAX seems needlessly large
for the SWAPFILE size especially since MAXPROCESSCNT includes two processes
- NULL and SWAPPER - which cannot be swapped.  For this reason, I have studied
the code VMS uses to determine how many processes a given SWAPFILE can support
and have written a short FORTRAN program which uses that algorithm to determine
the exact size a SWAPFILE must be in order to support a given number of
processes.  The code is listed below and my best attempt at explaining it
follows.
	During system boot, the SWAPFILE is divided into slots.  Each slot
is large enough to accomodate all the pages in the largest possible working
set, WSMAX, plus the maximum number of non-page-table pages in a process header.
The pages used for the page tables need not be counted because they are treated
as part of the working set and will be saved in the same manner as other pages
in the working set.  Each time a process is created a test is made to insure
that a SWAPFILE slot is available for that process.  The test is done by keeping
a count of the number of created processes in the system and never allowing that
number to exceed the number of slots in SWAPFILE.  The NULL and SWAPPER
processes are not created in the normal sense and thus do not enter into the
count of created processed in the system.  As would seem reasonable, SWAPFILE
slots are not reserved for the unswappable NULL and SWAPPER processes.  The size
of the SWAPFILE can thus be loosely expressed as:
	(WSMAX + NONPT_PHD_PAGES) * (MAXPROCESSCNT - 2)
The influence of this statement can be seen in lines 64 and 66 of the
program.  Lines 46 thru 62 are devoted to computing the number of non-page-
table pages in a process header.  Lines 48 thru 60 compute and sum the sizes
of the major process header constituents; the fixed portion, the working set
list, and the process section table.  Lines 55 thru 60 add the space required
by several arrays containing information about the pages occupied by the
process header itself.
	The program requires as input four VMS system parameters and three
data structure size constants, as does the VMS code which it emulates.
The VMS system parameters are: MAXPROCESSCNT, used to determine the number
of processes SWAPFILE must support; PROCSECTCNT, used to determine the size
of the process section table portion of the process header; WSMAX, used
in determining the size of a SWAPFILE slot and the size of the working
set list portion of the process header; and VIRTUALPAGECNT, used to determine
the size of the process page tables.  WSL$C_LENGTH gives the size of one entry
in the working set list and can be determined by expanding the macro $WSLDEF
found in SYS$LIBRARY:LIB.MLB.  PHD$C_LENGTH gives the size of the fixed portion
of the process header and can be determined by expanding the macro $PHDDEF.
SEC$C_LENGTH gives the size of one entry in the process section table and can be
determined by expanding the macro $SECDEF.  The values of these constants,
under VMS V1.6, are given in the comments in the program.
	Checking the size of and number of SWAPFILE slots can be accomplished
by using SDA on the currently running system.  The command "EXamine
SWP$GL_SFTBAS;40" will display the first two entries in the swap file table.
Each entry is of the form shown below.  The first entry is for the shell
process, which is used as the source for an inswap each time a new process is
created.  The second entry is for SWAPFILE.  The EXamine command above assumes
that the size of a swap file table entry is 20(hex), which is the size under VMS
V1.6.  If the size changes for V2.0 or later, the size field of the shell
process entry, which is in a fixed position relative to SWP$GL_SFTBAS, can be
used to determine the size of an entry.  The slot count and slot size fields of
the SWAPFILE entry give the number of swap slots and the size of each swap slot,
respectively.

			  SWAP FILE TABLE ENTRY
  +----------------+----------------+----------------+----------------+
  !	       slot size	    ! max. I/O size  !	 slot count   !
  +---------------------------------+----------------+----------------+
  !				  spare				      !
  +----------------+----------------+---------------------------------+
  !pg. fault clust.!	  type	    !		   size		      !
  +----------------+----------------+---------------------------------+
  !	      pointer to window control block for swap file	      !
  +-------------------------------------------------------------------+
  !		 base virtual block window of first slot	      !
  +-------------------------------------------------------------------+
  !				 bitmap				      !
  +----------------------------           ----------------------------+
  !				(64 bits)			      !
  +-------------------------------------------------------------------+
  !				  spare				      !
  +----------------+----------------+----------------+----------------+

	Within the program, the names SGN$GW_MAXPRCCNT, SGN$GW_MAXPSTCT,
SGN$GL_MAXWSCNT, SGN$GL_MAXVPGCT, SGN$GL_PTPAGCNT and SGN$GL_PHDPAGCT are
spelled the same as actual names in the system image which contain real copies
of the values they represent.  Other names having the standard DEC name
format are used in a way similar to the way in which names defined by
a $???DEF macro might be used.

01		PROGRAM SWAP_FILE_SIZE
02	
03	!  This program calculates an exactly correct swap file size given
04	!  the values of the following:
05	!	MAXPROCESSCNT (system parameter - SYSGEN)
06	!	PROCSECTCNT (system parameter - SYSGEN)
07	!	WSMAX (system parameter - SYSGEN)
08	!	VIRTUALPAGECNT (system parameter - SYSGEN)
09	!	WSL$C_LENGTH (from $WSLDEF, 4 in V1.6)
10	!	PHD$C_LENGTH (from $PHDDEF, '118'X in V1.6)
11	!	SEC$C_LENGTH (from $SECDEF, '20'X in V1.6)
12	!  All required values are prompted for.  This program represents
13	!  my way of remembering the way VMS calculates the number of pages
14	!  in a process header (in module SYSBOOT) and the size of a slot in
15	!  the swap file (in module SYSINIT).
16	!		Ralph O. Weber 9-JUN-1980
17	
18		IMPLICIT INTEGER*4 (A-Z)
19	
20	1	FORMAT ('$'A)
21	2	FORMAT (I)
22	3	FORMAT (Z)
23	
24		TYPE 1, 'MAXPROCESSCNT (in decimal):'
25		ACCEPT 2, SGN$GW_MAXPRCCT
26	
27		TYPE 1, 'PROCSECTCNT (in decimal):'
28		ACCEPT 2, SGN$GW_MAXPSTCT
29	
30		TYPE 1, 'WSMAX (in decimal):'
31		ACCEPT 2, SGN$GL_MAXWSCNT
32	
33		TYPE 1, 'VIRTUALPAGECNT (in decimal):'
34		ACCEPT 2, SGN$GL_MAXVPGCT
35	
36		TYPE 1, 'WSL$C_LENGTH (in hexadecimal):'
37		ACCEPT 3, WSL$C_LENGTH
38	
39		TYPE 1, 'PHD$C_LENGTH (in hexadecimal):'
40		ACCEPT 3, PHD$C_LENGTH
41	
42		TYPE 1, 'SEC$C_LENGTH (in hexadecimal):'
43		ACCEPT 3, SEC$C_LENGTH
44	
45	
46		SGN$GL_PTPAGCNT = (SGN$GL_MAXVPGCT*4 + 511) / 512
47
48		FIXED_PHD_BYTES = PHD$C_LENGTH + 511
49		WSL_BYTES = SGN$GL_MAXWSCNT * WSL$C_LENGTH
50		PRC_SECT_BYTES = SGN$GW_MAXPSTCT * SEC$C_LENGTH
51		BASIC_PHD_PAGES = (FIXED_PHD_BYTES + WSL_BYTES + PRC_SECT_BYTES)
52		1		  / 512
53		BASIC_PHD_BYTES = (BASIC_PHD_PAGES * 512) + 511
54	
55		PHD_PGS_ARY_BTS = (BASIC_PHD_PAGES + SGN$GL_PTPAGCNT) * 8
56	
57		NONPT_PHD_BYTES = BASIC_PHD_BYTES
58	10	NONPT_PHD_BYTES = NONPT_PHD_BYTES + PHD_PGS_ARY_BTS
59		    PHD_PGS_ARY_BTS = (PHD_PGS_ARY_BTS/512) * 8
60		    IF (PHD_PGS_ARY_BTS.NE.0)  GO TO 10
61	
62		SGN$GL_PHDPAGCT = NONPT_PHD_BYTES / 512
63	
64		SFT$W_SLOTSZ = SGN$GL_MAXWSCNT + SGN$GL_PHDPAGCT
65	
66		SWAP_FILE_SZ = (SGN$GW_MAXPRCCT - 2) * SFT$W_SLOTSZ
67				!!!!!!!!!!!!!!!!!!!
68		!NB. I can use MAXPROCESSCNT-2 because two processes, NULL and
69		!    SWAPPER, are mystically created by INIT without aid of
70		!    SYS$CREPRC.  SYS$CREPRC maintains the swap slot counters
71		!    and it is unaware of NULL and SWAPPER.  NULL and SWAPPER
72		!    also cannot be swapped under any circumstances.
73		!    MAXPROCESSCNT, on the other hand, controls the number of
74		!    PCB slots and NULL and SWAPPER do require PCB slots.
75	
76		TYPE *, 'Swap file size must be at least', SWAP_FILE_SZ,
77		1	'blocks.'
78	
79		STOP
80		END

	The following is a table showing the swap file size computed by
the program and using MAXPROCESSCNT times WSMAX for several cases of
interest to me.

MAXPROCESSCNT  PROCSECTCNT  WSMAX  VIRTUALPAGECNT     swap file size
						    program	standard
     20		    20       250	4096	      4590	  5000
     24		    20	     250	4096	      5610	  6000
     24		    20	     250	8192	      5632	  6000
     24		    20	     256	8192	      5764	  6144
     24		    20	     512	8192	     11440	 12288

The standard method of computing swap file size appears to produce sizes
which are too large by at least WSMAX.
