1 |
-----BEGIN PGP SIGNED MESSAGE----- |
2 |
Hash: SHA1 |
3 |
|
4 |
Volker Armin Hemmann wrote: |
5 |
> |
6 |
> where is the difference between 'app syncs its files to disk' and 'kernel |
7 |
> swaps app to disk, than decides to swap it in, so app can decides which files |
8 |
> it wants to sync to disk, then swap it out again'? |
9 |
> |
10 |
> oh, more disk access... |
11 |
> |
12 |
|
13 |
Actually, you're not quite looking at it the right way. Suppose during |
14 |
the course of an emerge I need 150MB of temp files. I have two choices |
15 |
- - write to disk, or write to tmpfs. |
16 |
|
17 |
In the former case every write goes to disk buffer (ram), and then is |
18 |
synced to disk within about 5 seconds. In the latter case every write |
19 |
goes to tmpfs (ram), and if necessary gets written to swap at an |
20 |
opportune time (when the system needs more ram, the page is idle for a |
21 |
long time, the system is otherwise idle, etc) - and if this need doesn't |
22 |
come up before the file is deleted it never gets written to disk at all. |
23 |
|
24 |
In the worst case both get synced to disk, but in the former case the |
25 |
kernel does not get to choose when outside the forced setting of about 5 |
26 |
seconds. In the latter case the kernel can schedule disk access more |
27 |
opportunistically - because the kernel knows the data isn't that |
28 |
important and doesn't need to be written to disk quickly. |
29 |
|
30 |
Generally well-written apps don't decide what files it wants syned to |
31 |
disk - the kernel does that. If a file is written to a normal |
32 |
filesystem it gets synced according to the rules of the filesystem, and |
33 |
if it is written to tmpfs it gets written to disk if the system needs |
34 |
more ram and the file is more idle than most of the rest of RAM. |
35 |
|
36 |
I think you'd be surprised at how similarly files on disk and memory in |
37 |
ram get handled. Both get "swapped" in and out of memory (if the file |
38 |
is mmapped - which is often the case), and writes to files get stored in |
39 |
buffers/cache just like files in tmpfs. The big difference is that |
40 |
tmpfs files are not guaranteed to ever get written to disk, which means |
41 |
the kernel can handle them more flexibly. Normal files are ALWAYS |
42 |
written to disk (unless they're deleted in the few seconds before they |
43 |
are synced), and the kernel isn't allowed to just keep them in buffers |
44 |
for 30 minutes if the disk is busy. |
45 |
|
46 |
In the extreme of excessive RAM the tmpfs is going to be many orders of |
47 |
magnitude faster than writing to disk. In the extreme of a very |
48 |
ram-starved system it will probably be comparable. |
49 |
|
50 |
> |
51 |
> And honestly? From my 'feelings' swap is way slower than normal disk accesses. |
52 |
> |
53 |
|
54 |
What kinds of access are you comparing? If you're talking about |
55 |
swapping in and out a 50MB binary vs a 50kb text file then you're |
56 |
comparing apples to oranges. Ditto if you're comparing time to access |
57 |
just part of a file. And any time you run a program you're using disk |
58 |
swapping - even if you have no swap partition. Linux just mmaps the |
59 |
executable into memory and starts running it (with little to nothing in |
60 |
RAM) - any time the program accesses its own code it triggers a swap-in. |
61 |
This means that code that is unused might never get read off the disk, |
62 |
and the kernel will opportunistically read-ahead when it thinks it makes |
63 |
sense to do so - just as it does with any other kind of swapped memory, |
64 |
or a regular file. |
65 |
|
66 |
Swap and normal file access aren't handled completely identically, so |
67 |
there can potentially be differences. However, there is so much in |
68 |
common that I'd be surprised if there is as big a difference as you suggest. |
69 |
-----BEGIN PGP SIGNATURE----- |
70 |
Version: GnuPG v1.4.7 (GNU/Linux) |
71 |
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org |
72 |
|
73 |
iD8DBQFGs5eOG4/rWKZmVWkRAmRpAJ96U/jIjZQmtItisYKi88F+OoKpfQCgnpzB |
74 |
nQv1vfz65sXNroRWmKLsZTs= |
75 |
=8Rna |
76 |
-----END PGP SIGNATURE----- |