[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: Efficient Create Swap File?



On 27Mar2009 14:01, Mike McCarty <Mike McCarty sbcglobal net> wrote:
> I've seen various recommendations for adding swap files after
> system creation, and it occurs to me that the "standard" technique
> may not be the most efficient. I realize that one rarely creates
> swap files, but nonetheless on occasion one needs to "precreate"
> some file or other, then do something to it, like mkfs etc.
[...]
> The standard technique is to do something like
> $ dd if=/dev/zero of=/path/to/new/file bs=1024 count=524288
> $ mkswap /path/to/new/file
> to create a 512MB file. The second command may be different,
> depending upon the circumstances, but the technique remains
> the same. In effect, the new file gets written twice.

No. mkswap just marks up the control areas of th file for use, like mkfs
marks up the control areas of a filesystem partition for use. Neither
writes the _whole_ file, just a small section of it.

So you're only writing the file once (with dd), and marking up a small
area of it for use (with mkswap).

> It occurs to me that one could, instead, do
> $ dd if=/dev/zero of=/path/to/new/file bs=1M seek=511 count=1
> $ mkswap /path/to/new/file
>
> and have the same results, requiring only writing the file
> once.

No. It only looks the same from the outside.

What you have done here is make a _sparse_ file. A UNIX file only
allocates data blocks for areas that have been written to. By writing a
single chunk of data at the end, you have not allocated all the data
blocks. A swap area _must_ have all the blocks allocated. Indeed,
"man mkswap" even says this:

       To  setup  a swap file, it is necessary to create that file before
       initializing it with mkswap , e.g. using a command like

              # dd if=/dev/zero of=swapfile bs=1024 count=65536

       Note that a swap file must not contain any holes (so,  using cp(1) 
       to create the file is not acceptable).

A sparse file is a file with holes.

> My guess is that when the swap file with a "hole" first gets used, there
> will be a long(ish?) pause while some part or parts of the sparse file
> get filled in. This is not so good for a swap file, but when one
> is actually going to rewrite most of the file anyway, and is only
> using the file itself as more or less an indicator of the size, then
> it might make sense.
>
> Comments?

Leaving aside the possibilty that the swap system will simply refuse
to work on a file with holes, which it may, you also get much more
contiguous file if you preallocate. Data blocks allocated later must
come from where the free space is at that time. Might be anywhere.

Cheers,
-- 
Cameron Simpson <cs zip com au> DoD#743
http://www.cskk.ezoshosting.com/cs/

Good judgement comes from experience.  Experience comes from bad judgement.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]