31 Aralık 2010 Cuma

Experiments and fun with the Linux disk cache

Hopefully you are now convinced that Linux didn't just eat your ram. Here are some interesting things you can do to learn how the disk cache works.

Effects of disk cache on application memory allocation

Since I've already promised that disk cache doesn't prevent applications from getting the memory they want, let's start with that. Here is a C app (munch.c) that gobbles up as much memory as it can, or to a specified limit:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char** argv) {
int max = -1;
int mb = 0;
char* buffer;

if(argc > 1)
max = atoi(argv[1]);

while((buffer=malloc(1024*1024)) != NULL && mb != max) {
memset(buffer, 0, 1024*1024);
mb++;
printf("Allocated %d MB\n", mb);
}

return 0;
}
Running out of memory isn't fun, but the OOM killer should end just this process and hopefully the rest will be unperturbed. We'll definitely want to disable swap for this, or the app will gobble up that as well.
$ sudo swapoff -a

$ free -m
total used free shared buffers cached
Mem: 1504 1490 14 0 24 809
-/+ buffers/cache: 656 848
Swap: 0 0 0

$ gcc munch.c -o munch

$ ./munch
Allocated 1 MB
Allocated 2 MB
(...)
Allocated 877 MB
Allocated 878 MB
Allocated 879 MB
Killed

$ free -m
total used free shared buffers cached
Mem: 1504 650 854 0 1 67
-/+ buffers/cache: 581 923
Swap: 0 0 0

$
Even though it said 14MB "free", that didn't stop the application from grabbing 879MB. Afterwards, the cache is pretty empty, but it will gradually fill up again as files are read and written. Give it a try.

Effects of disk cache on swapping

I also said that disk cache won't cause applications to use swap. Let's try that as well, with the same 'munch' app as in the last experiment. This time we'll run it with swap on, and limit it to a few hundred megabytes:
$ free -m
total used free shared buffers cached
Mem: 1504 1490 14 0 10 874
-/+ buffers/cache: 605 899
Swap: 2047 6 2041

$ ./munch 400
Allocated 1 MB
Allocated 2 MB
(...)
Allocated 399 MB
Allocated 400 MB

$ free -m
total used free shared buffers cached
Mem: 1504 1090 414 0 5 485
-/+ buffers/cache: 598 906
Swap: 2047 6 2041
munch ate 400MB of ram, which was taken from the disk cache without resorting to swap. Likewise, we can fill the disk cache again and it will not start eating swap either. If you run watch free -m in one terminal, and find . -type f -exec cat {} + > /dev/null in another, you can see that "cached" will rise while "free" falls. After a while, it tapers off but swap is never touched1

Clearing the disk cache

For experimentation, it's very convenient to be able to drop the disk cache. For this, we can use the special file /proc/sys/vm/drop_caches. By writing 3 to it, we can clear most of the disk cache:
$ free -m
total used free shared buffers cached
Mem: 1504 1471 33 0 36 801
-/+ buffers/cache: 633 871
Swap: 2047 6 2041

$ echo 3 | sudo tee /proc/sys/vm/drop_caches
3

$ free -m
total used free shared buffers cached
Mem: 1504 763 741 0 0 134
-/+ buffers/cache: 629 875
Swap: 2047 6 2041
Notice how "buffers" and "cached" went down, free mem went up, and free+buffers/cache stayed the same.

Effects of disk cache on load times

Let's make two test programs, one in Python and one in Java. Python and Java both come with pretty big runtimes, which have to be loaded in order to run the application. This is a perfect scenario for disk cache to work its magic.
$ cat hello.py
print "Hello World! Love, Python"

$ cat Hello.java
class Hello {
public static void main(String[] args) throws Exception {
System.out.println("Hello World! Regards, Java");
}
}

$ javac Hello.java

$ python hello.py
Hello World! Love, Python

$ java Hello
Hello World! Regards, Java

$
Our hello world apps work. Now let's drop the disk cache, and see how long it takes to run them.
$ echo 3 | sudo tee /proc/sys/vm/drop_caches
3

$ time python hello.py
Hello World! Love, Python

real 0m1.026s
user 0m0.020s
sys 0m0.020s

$ time java Hello
Hello World! Regards, Java

real 0m2.174s
user 0m0.100s
sys 0m0.056s

$
Wow. 1 second for Python, and 2 seconds for Java? That's a lot just to say hello. However, now all the file required to run them will be in the disk cache so they can be fetched straight from memory. Let's try again:
$ time python hello.py
Hello World! Love, Python

real 0m0.022s
user 0m0.016s
sys 0m0.008s

$ time java Hello
Hello World! Regards, Java

real 0m0.139s
user 0m0.060s
sys 0m0.028s

$
Yay! Python now runs in just 22 milliseconds, while java uses 139ms. That's a 95% improvement! This works the same for every application!

Effects of disk cache on file reading

Let's make a big file and see how disk cache affects how fast we can read it. I'm making a 200mb file, but if you have less free ram, you can adjust it.
$ echo 3 | sudo tee /proc/sys/vm/drop_caches
3

$ free -m
total used free shared buffers cached
Mem: 1504 546 958 0 0 85
-/+ buffers/cache: 461 1043
Swap: 2047 6 2041

$ dd if=/dev/zero of=bigfile bs=1M count=200
200+0 records in
200+0 records out
209715200 bytes (210 MB) copied, 6.66191 s, 31.5 MB/s

$ ls -lh bigfile
-rw-r--r-- 1 vidar vidar 200M 2009-04-25 12:30 bigfile

$ free -m
total used free shared buffers cached
Mem: 1504 753 750 0 0 285
-/+ buffers/cache: 468 1036
Swap: 2047 6 2041

$
Since the file was just written, it will go in the disk cache. The 200MB file caused a 200MB bump in "cached". Let's read it, clear the cache, and read it again to see how fast it is:
$ time cat bigfile > /dev/null

real 0m0.139s
user 0m0.008s
sys 0m0.128s

$ echo 3 | sudo tee /proc/sys/vm/drop_caches
3

$ time cat bigfile > /dev/null

real 0m8.688s
user 0m0.020s
sys 0m0.336s

$
That's more than fifty times faster!

Conclusions

The Linux disk cache is very unobtrusive. It uses spare memory to greatly increase disk access speeds, and without taking any memory away from applications. A fully used store of ram on Linux is efficient hardware use, not a warning sign.
LinuxAteMyRam.com was presented by VidarHolen.net

1. This is somewhat oversimplified. While newly allocated memory will always be taken from the disk cache instead of swap, Linux can be configured to preemptively swap out other unused applications in the background to free up memory for cache. The is tunable through the 'swappiness' setting, accessible through /proc/sys/vm/swappiness.

A server might want to swap out unused apps to speed up disk access of running ones (making the system faster), while a desktop system might want to keep apps in memory to prevent lag when the user finally uses them (making the system more responsive). This is the subject of much debate.

linux ate my RAM

What's going on?

Linux is borrowing unused memory for disk caching. This makes it looks like you are low on memory, but you are not! Everything is fine!

Why is it doing this?

Disk caching makes the system much faster! There are no downsides, except for confusing newbies. It does not take memory away from applications in any way, ever!



What if I want to run more applications?

If your applications want more memory, they just take back a chunk that the disk cache borrowed. Disk cache can always be given back to applications immediately! You are not low on ram!

Do I need more swap?

No, disk caching only borrows the ram that applications don't currently want. It will not use swap. If applications want more memory, they just take it back from the disk cache. They will not start swapping.

How do I stop Linux from doing this?

You can't disable disk caching. The only reason anyone ever wants to disable disk caching is because they think it takes memory away from their applications, which it doesn't! Disk cache makes applications load faster and run smoother, but it NEVER EVER takes memory away from them! Therefore, there's absolutely no reason to disable it!

Why does top and free say all my ram is used if it isn't?

This is just a misunderstanding of terms. Both you and Linux agrees that memory taken by applications is "used", while memory that isn't used for anything is "free". But what do you call memory that is both used for something and available for applications?

You would call that "free", but Linux calls it "used".






Memory that isYou'd call itLinux calls it
taken by applications Used Used
available for applications, and used for something Free Used
not used for anything Free Free
This "something" is what top and free calls "buffers" and "cached". Since your and Linux's terminology differs, you think you are low on ram when you're not.

How do I see how much free ram I really have?

Too see how much ram is free to use for your applications, run free -m and look at the row that says "-/+ buffers/cache" in the column that says "free". That is your answer in megabytes:

$ free -m
total used free shared buffers cached
Mem: 1504 1491 13 0 91 764
-/+ buffers/cache: 635 869
Swap: 2047 6 2041
$
If you don't know how to read the numbers, you'll think the ram is 99% full when it's really just 42%.

How can I verify these things?

See this page for more details and how you can experiment with disk cache.

30 Aralık 2010 Perşembe

How To Use Apple’s AirTunes in Ubuntu

airtunesI’m a Ubuntu guy at heart, but the team over at bicycle-based IT company iSupportU, my home away from MakeUseOf, includes a couple of real Mac types. As such, a recent company memo stated that music can now reach our stereo over something called “AirTunes.”

First, I grumbled about the typical Apple naming scheme, then I wondered if it was possible to use such a thing from Ubuntu, then I heard my fellow Linux user ask the same question out loud.



Then, and only then, did I begin Googling for more information. What I found was useful, and with typical, super-helpful Ubuntu-forumites to boot, but I think there’s a gap in the Internet here that needs to be filled. Let’s fill it then, shall we? This guide is written using Ubuntu 10.04, but may apply to other Linux distributions as well.



Step 1: Install Necessary Software

To begin, you need to install two packages: “pulseaudio-module-raop paprefs” and “paprefs”. The first package allows Ubuntu to use the remote audio output protocol (raop); the second, configure Ubuntu to enable it. To install the packages, simply open the command line and type this command:

sudo apt-get install pulseaudio-module-raop paprefs
If the command line scares you simply click here to install the packages.

Non-Ubuntu people, your mileage may vary here in terms of the package names. Sorry about that!

Step 2: Enable AirTunes Detection

Now that you’ve installed what needs installing, you need to open the program “paprefs”. Do this from the command line, or click “Alt” and “F2″ and type “paprefs” there. Don’t worry; you’re most of the way, and everything will be GUI-full from here on out. Here’s what the window should look like:

airtunes
Simply click the “Make Discoverable Apple AirTunes sound devices available locally” checkbox and you’ve enabled it.

Step 3: Switch to AirTunes

Now that AirTunes detection is enabled all you need to do is switch your speakers from your local computer to your remote one. Click the “Volume” icon in your tray, then click “Preferences”, as seen here:

airtunes
Click the “Output” tab and you should see your Apple AirTunes device listed here, like this:

apple airtunes
If you don’t see such an option, don’t panic. Simply restart Pulseaudio (if you know how) or reboot your computer. It should show up for sure now.

Quirks

Of course, this entire process being a little bit of a hack, there are a few quirks to be noted. For example, I noticed a six-second delay. This isn’t a big deal if you’re listening to music, but don’t plan on using this from watching movies.

The second problem I’ve noted is that, while switching back to my computer’s speakers is as easy as re-opening the output selection, this apparently doesn’t log me off on the server-side. This means that, if one of my Apple-using collegues decides to reconnect to AirTunes after I’m done using it, they see a message about someone already being logged onto the device. This is frustrating, but nothing that a reboot of the Airport Express can’t fix.

If I find a fix to either of these problems you can expect to find it in the comments below, but please also feel free to share if you have any of your own.

Conclusion

I was happy to find a way to stream my music to the AirTunes device at work, and hope this guide is useful to at least a few other people. If it does, please leave me a note in the comments below. Also leave me a note if you’ve found a way to decrease lag or log off without restarting the AirPort.

Finally, as I cannot use every Linux distribution simultaniously, please let me know how these steps work on other systems such as Fedora or SUSE. It’s always good to know!

PIX-3-201002: Too many connections on xlate

Our sever Intermittently loses its internet connection. All I see in the syslog for the Pix 10000 is message "PIX-3-201002: Too many connections on xlate." Could this be a cause why connections are dropping? If so, how can I correct it?
Average Rating: 0 (0 Votes)
Outline View
dro
  dro
Currently Being Moderated
1. Jun 6, 2003 8:43 AM in response to: wongn
Re: PIX-3-201002: Too many connections on xlate
This error is normally caused by having a limit set in place on the static maps.



Do a "show static" and look for the server's entry. The last two numbers represent the maximum amount of embryonic and actual connections a static map is allowed to have.





This is 0 by default, so it must have been configured with a limit on your PIX.



Regards,

-Joshua
Average Rating: 0 (0 Votes)
Average Rating: 0 (0 Votes)
dro
  dro
Currently Being Moderated
3. Jun 6, 2003 10:23 AM in response to: wongn
Re: PIX-3-201002: Too many connections on xlate
Sorry, in that case it's most like the last two numbers listed in the "show nat" command. If these are both set to 0, then there are no limits set.



The second last entry is a reference to the maximum number of TCP connections allowed and the last entry is a reference to the number of embryonic connections which are allowed.



for example:



pixfirewall# show nat

nat (inside) 1 192.168.1.0 255.255.255.0 50 30



This shows that 50 TCP connections are allowed at once and 30 embryonic connections are allowed.



Regards,

-Joshua
Average Rating: 0 (0 Votes)
Employee kthanuva
Currently Being Moderated
4. Jun 6, 2003 6:44 PM in response to: wongn
Re: PIX-3-201002: Too many connections on xlate
Hi,

     please find the details f this error message....



1. %PIX-3-201002: Too many connections on static|xlate gaddr! econns nconns

This is a connection-related message. This message is logged when the maximum number of connections to the specified static address has been exceeded. The econns variable is the maximum number of embryonic connections and nconn s is the maximum number of connections permitted for the static or xlate.



Recommended Action: Use the show static command to check the limit imposed on connections to a static address. The limit is configurable.







we can configure the connection limit thro static command



Regards

kiruba

 

Install Sun Java Packages



Ubuntu uses OpenJDK by default, but I note that some web services such as ezyZip.com might need the Sun Java Runtime Environment (JRE) to be installed in the system for running the services properly. If you would like to get the proprietary Sun Java packages for your system, you can download and install them from the Canonical Partner Repository with the steps below:

  1. Go to Applications (or Main Menu) > Accessories > Terminal.

  2. Enter sudo add-apt-repository "deb http://archive.canonical.com/ lucid partner" to add the partner repository.

  3. Enter sudo apt-get update to update the source list.

  4. Enter sudo apt-get install sun-java6-jre sun-java6-plugin sun-java6-fonts to download and install the Sun Java packages. (If asked to accept the Distributor License for Java (DLJ) terms, use the left/right key to navigate and select Yes, then press the Enter key for installation).

  5. Enter sudo java -version to check the version of the Java used in the system.

  6. Enter sudo update-alternatives --config java to choose the default Java for use in the system when necessary.

Change Default Boot Options



After full installation, Ubuntu is set to be the default operating system to boot up if no key is pressed within a few seconds on a multi-boot system. You might want to set your preferred operating system to boot up by default. This can be done easily with StartUp-Manager.

  1. StartUp-ManagerGo to Applications (or Main Menu) > Accessories > Terminal.

  2. Enter sudo apt-get install startupmanager (OR copy the highlighted code and, in the Terminal, press Ctrl-Shift-V to paste it).

  3. Enter password used upon installation of Ubuntu.

  4. Go to System > Administration > StartUp-Manager

  5. Enter the same password to perform pre-configuration tasks, which include searching bootloaders to operating systems.

  6. Select the default operating system from the pull-down menu, click "Close" to perform post-configuration tasks.

With StartUp-Manager, you can also do others such as manage Usplash themes, adjust bootloader menu resolution or set timeout in seconds. Avoid changing timeout to 0 seconds if you need to select a system to boot up from a multi-boot menu.

Create an Advanced File Manager

In the Ubuntu file system, you can use Nautilus file manager to browse most files but can only write files in your home folder /home/your_name and its sub-folders such as Desktop and Documents. If you have to rename a folder or write files outside of your home folder using the file manager, you won't be able to but you can create an advanced file manager for this purpose.

  1. Advanced File ManagerGo to System > Preferences > Main Menu

  2. Select "Accessories" in the left panel and click "New Item" in the right panel.

  3. Enter a name such as Advanced Nautilus in the "Name" box.

  4. Enter gksu nautilus in the "Command" box.

  5. Click the "OK" button and the "Close" button.

Now you can go to Applications (or Main Menu) > Accessories and see that the Advanced Nautilus is ready for use. But be careful since you can use it to delete or change any files on your system.