Usenet comp.sys.dec (1990-2013)
 help / color / mirror / Atom feed
* More questions on swap space reservation
@ 1999-05-26  4:00 dtmoore
  1999-05-26  4:00 ` Serguei Patchkovskii
  0 siblings, 1 reply; 4+ messages in thread
From: dtmoore @ 1999-05-26  4:00 UTC (permalink / raw)


Ok ... from eariler posts I know that there are two swap space
reservation modes:
   lazy -- reserve space only as needed ... can be risky

   aggressive -- reserve max. amt. of swap space at start of process and
                 keep it reserved until process terminates

Now, I have a FORTRAN77 program which contains lots of fairly
large statically allocated arrays.  When I run it under the agressive
swap mode it eats up about a third of my swap space (101 MB), but its
resident memory size is only ~6MB.  My question is:  Why are the
resident size and swap size so different?  As I understood it, the
aggressive mode reserved enough space so that the entire memory image of
a process could be swapped out.  Well, in this case, the memory image
appears to be 6.4 MB, yet it reserves about 16 times that in swap space.

What exactly is it that I am confused about here?  If I understand that,
maybe I can modify the program so that it isn't such a swap hog.

All comments/suggestions are appreciated.
--
                    Dave Moore -- Chemistry Dept. -- UNC Chapel Hill

Ford: "... eddys in the space-time continuum ..."
Arthur:  "So this is his sofa then, is it?"


--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: More questions on swap space reservation
  1999-05-26  4:00 More questions on swap space reservation dtmoore
@ 1999-05-26  4:00 ` Serguei Patchkovskii
  1999-05-27  4:00   ` dtmoore
  0 siblings, 1 reply; 4+ messages in thread
From: Serguei Patchkovskii @ 1999-05-26  4:00 UTC (permalink / raw)


dtmoore (sponk@my-deja.com) wrote:
: Now, I have a FORTRAN77 program which contains lots of fairly
: large statically allocated arrays.  When I run it under the agressive
: swap mode it eats up about a third of my swap space (101 MB), but its
: resident memory size is only ~6MB.  My question is:  Why are the
: resident size and swap size so different?  As I understood it, the
: aggressive mode reserved enough space so that the entire memory image of
: a process could be swapped out.  Well, in this case, the memory image
: appears to be 6.4 MB, yet it reserves about 16 times that in swap space.

Ok, I guess this is the time for a little lecture on basics of virtual
memory in Unix. Loosely speaking, each unix program consists of three
sections: code, initialized data, and uninitialized data (often called
bss). Code and data sections (plus some overhead) determine the size of
your program's disk image. For obvious reasons, bss does not take any
space in the disk image.

When program is started, neither one of the three sections is loaded
to memory. Instead, the operating system establishes a correspondence
(mapping) between the program image on disk and the virtual addess 
space of a running program:

Program code cannot be changed by the running program, and is therefore
mapped read-only, with only the parts (pages) which are being currently 
used copied to the main memory. If the O/S runs tight on memory, it can 
simply discard any in-memory copies of the code pages: it knows which
executable images they came from, and can re-load them from disk if 
they are needed again. If you run several copies of a program, all these
copies share code pages.

Initialized data is mapped with the copy-on-write attribute: until you
attempt to change it, it gets treated in exactly the same way as the
code pages would. However, if you change any variables, the system
will create a copy of the affected page private to the instance of the
running program which accessed it. If the system runs out of memory,
it will have to find some place to store the modified page until its
needed again - or until the program exits.

Finally, the part of the virtual address space corresponding to the
uninitialized data is mapped with the zero-on-access atribute. As long
and the program does not attempt to access it in any way, it simply
does not exist. On the first access (either read or write), the affected
page is allocated from the memory pool and filled with zeros. From
that point, it is treated just like a modified data page, and will have
to be stored somewhere if the memory it occupies is needed for other
purposes.

(Any memory you allocate dynamically can be treated as a part of bss
at this point, and has the same effect on the swap allocation)

Now, we can come to the difference between the immediate (or "aggressive")
and deferred (or "lazy") swap space allocation mode. In the immediate
mode, when a program starts the operating system allocates enough swap
space to store all pages which can *conceivably* have to be stored - that
is for all data pages as well as for all bss pages. This is almost always
an overkill, but *guarantees* that the program will not suddenly run out
of memory it think it owns.

The downside of the immediate mode is that in the cases like yours, there
a program declares huge chunks of bss which are not actually used in a 
typical run, the system still must reserve swap space for *all* of them,
requiring inordinate amount of swap space - which is not in fact used.
Fortran-77 programs tend to be the culprit in this situation.

In the deferred mode, the system does not try to allocate the swap space
until it actually needs to store a memory page somewhere. Since there 
usually are some, and sometimes quite a few, virtual memory pages which 
*could* need backing store (swap space), but do not need it right now, 
this usually results in more efficient memory/swap space utilization.

The downside is that a system running in deferred swapping mode may have
no recourse but to kill running applications more or less randomly: if
a program attempts to modify a data page which it had not changed before,
or accesses a new bss page, the system will have to find a new (private)
real memory page for that program. If no free pages are available, the
system will have to free up some of the occupied ones, either by discarding
pages it knows how to reload from disk or swap space ("clean" pages) or
by swapping out modified ("dirty") pages. If the system has no clean pages
it can discard, and no swap space to store the dirty pages, the only action
it can take is to kill the application which needs a new memory page in
the first place. Since this can be just about any program running in the
system, including the programs providing critical system services, this
*may* (but not necessarily *will*) result in the system becoming unusable.

In the case of Tru64/Digital Unix/OSF/1, the operating system attempts to
minize the potential negative impact of the deferred mode by reserving a
certaing fraction of the memory pages for the exclusive use of the privileged
programs. If the out-of-swap condition exists only for a short time, this
helps critical system programs to survive it, riding them over until one of 
the user processes is killed, freeing up some memory. It however does not 
*guarantee* that the system will remain usable - just improves the odds 
of that happening.

I tend to absolutely require immediate swap allocation mode on any systems 
which can be used by more than one person or for more than one task. For a 
"personal" system, it is probably Ok to enable the deferred mode - provided 
that the user of the system knows exactly what he or she is doing...

Regards,

/Serge.P

--
home page: http://www.cobalt.chem.ucalgary.ca/ps/



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: More questions on swap space reservation
  1999-05-26  4:00 ` Serguei Patchkovskii
@ 1999-05-27  4:00   ` dtmoore
  1999-05-27  4:00     ` Serguei Patchkovskii
  0 siblings, 1 reply; 4+ messages in thread
From: dtmoore @ 1999-05-27  4:00 UTC (permalink / raw)


Wow ... Thanks for all of the info.  I now feel that I understand the
situation much more clearly ... However I do have a follow-up question
based on this part of your explanation:

{snip}
> (Any memory you allocate dynamically can be treated as a part of bss
> at this point, and has the same effect on the swap allocation)
{snip}

I am not the sysop for my machine, and although I have asked him to
switch to the deferred swap mode, he is (understandably) reluctant to do
so.  So, my question is this ... if I re-write the array declarations
using F90 dynamic allocation, will it help to alleviate my problem, or
will it just delay the swap bloating until program reaches the
allocation statments?  Another way to ask this is ... under the
immediate swap mode, is the swap file size fixed when the process
starts, or will it be increased as more dynamic allocation occurs?

Thanks again for the info,
--
Dave Moore -- Chemistry Dept. -- UNC Chapel Hill

Ford: "... eddys in the space-time continuum ..."
Arthur:  "So this is his sofa then, is it?"


--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: More questions on swap space reservation
  1999-05-27  4:00   ` dtmoore
@ 1999-05-27  4:00     ` Serguei Patchkovskii
  0 siblings, 0 replies; 4+ messages in thread
From: Serguei Patchkovskii @ 1999-05-27  4:00 UTC (permalink / raw)


dtmoore (sponk@my-deja.com) wrote:
: so.  So, my question is this ... if I re-write the array declarations
: using F90 dynamic allocation, will it help to alleviate my problem, or
: will it just delay the swap bloating until program reaches the
: allocation statments?  Another way to ask this is ... under the

If you allocate only the amount of space you need (rather than the largest
size you might need), or if you deallocate arrays which are no longer
needed, you'll do fine. Likewise, if there are some parts of the program
which are never reached in a typical run, and you never get to the allocate
statements, the amount of swap space you need will decrease.

If neither is true, and all the memory you might conceivably need is 
allocated at the start of the program and lives through the whole run
you will need the same amount of the swap space as in the statically
dimensioned case - plus some overhead.

: immediate swap mode, is the swap file size fixed when the process
: starts, or will it be increased as more dynamic allocation occurs?

When you use ALLOCATE in your Fortran program, your request eventually
winds down to the sbrk() system routine[*], which simply attempts to
extend the bss segment of your program. (This is why I told that dynamic
memory is treated just like bss). In the immediate mode, this will also 
allocate however much swap pages is needed to swap out the new memory
locations. If this cannot be done, sbrk() will return an error, which
will (eventually) result in a non-zero status indication from ALLOCATE.
If you don't have the STAT= attribute in the ALLOCATE, it will simply 
kill your program - possibly several hours (or days, or weeks) into
the calculation. 

In this sense, a statically dimensioned Fortran-77 program running in
the immediate mode is more reliable, as it is *guaranteed* not to run 
out of memory at any future time. (Being able to determine the maximum
data size of an arbitrary program at the startup time was one of the
design requirements behind Fortran-77). Being able to allocate memory
dynamically is nice, but requires certain amount of discipline and a
fair bit of extra error handling.

Regards,

/Serge.P

[*] This is in fact a gross oversimplification, but it will do.

--
home page: http://www.cobalt.chem.ucalgary.ca/ps/



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1999-05-27  4:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1999-05-26  4:00 More questions on swap space reservation dtmoore
1999-05-26  4:00 ` Serguei Patchkovskii
1999-05-27  4:00   ` dtmoore
1999-05-27  4:00     ` Serguei Patchkovskii

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox