Tag Archive for 'linux'

The Linux OOM Killer

Most Linux distributions allow processes to request more memory that what is available in the system. The logic behind the approval is that generally the allocated memory is not used up immediately. Also it has been observed that processes over their lifetime do not utilize all of the memory they had initially requested. Thus over-committing allows the system to fully utilize it’s memory at the risk of out-of-memory (OOM) situations.

The purpose of OOM Killer is to find the best process to kill in case of severe memory shortage. The process is selected on the basis of badness score. The value of badness score is determined by the following properties:

  • original memory size of the process – more memory a process uses higher is its score
  • it’s CPU time
  • the run time – the longer a process is alive lower is its score
  • oom_adj value – The /proc/<pid>/oom_adj can be set to a value between -17 and +15. Higher the value, more likely is it to be selected as the sacrificial lamb. Setting this value to -17 instructs the OOM Killer to never kill the process.
  • Half of each child’s memory size is added to parent’s score.
  • If the task has a nice value above zero, the score doubles
  • Superuser or direct hardware access tasks have their values divided by 4
  • Depending on oom_adj the value is adjusted as:
    • if oom_adj > 0, score <<= oom_adj
    • if oom_adj < 0, score >>= -(oom_adj)

The principle on which the OOM Killer operates is :

System should lose the minimum amount of work done, recovers a large amount of memory, doesn’t kill innocent processes eating tons of memory and kills the minimum number of processes (limit to 1 if possible).

The task with the highest badness score is selected and all it’s children are killed. If the process does not have any child then the process itself will be killed.

Adding more info from OOM_KILLER.

The function which does the above mentioned badness score computation is called badness(). It gets called by the following chain:

_alloc_pages -> out_of_memory() -> select_bad_process() -> badness()

The badness() accumulates points for each process and returns them to select_bad_process(). The scoring of a process starts with the size of it’s resident memory:

        /*
* The memory size of the process is the basis for the badness.
*/
        points = p->mm->total_vm;
view raw badness_1.c This Gist brought to you by GitHub.

The memory size of any child is added to the process:

        /*
* Processes which fork a lot of child processes are likely
* a good choice. We add the vmsize of the childs if they
* have an own mm. This prevents forking servers to flood the
* machine with an endless amount of childs
*/
          ...
                  if (chld->mm != p->mm && chld->mm)
                        points += chld->mm->total_vm;
view raw badness_2.c This Gist brought to you by GitHub.

Process with nice value above zero have their score increased and long running processes have theirs decreased:

        s = int_sqrt(cpu_time);
        if (s)
                points /= s;
        s = int_sqrt(int_sqrt(run_time));
        if (s)
                points /= s;

        /*
* Niced processes are most likely less important, so double
* their badness points.
*/
        if (task_nice(p) > 0)
                points *= 2;
view raw badness_3.c This Gist brought to you by GitHub.

Superuser processes and direct hardware access tasks have their scores reduced:

        /*
* Superuser processes are usually more important, so we make it
* less likely that we kill those.
*/
        if (cap_t(p->cap_effective) & CAP_TO_MASK(CAP_SYS_ADMIN) ||
                                p->uid == 0 || p->euid == 0)
                points /= 4;

        /*
* We don't want to kill a process with direct hardware access.
* Not only could that mess up the hardware, but usually users
* tend to only have this flag set on applications they think
* of as important.
*/
        if (cap_t(p->cap_effective) & CAP_TO_MASK(CAP_SYS_RAWIO))
                points /= 4;

view raw badness_4.c This Gist brought to you by GitHub.

Finally, honour the oom_adj setting:

        /*
* Adjust the score by oomkilladj.
*/
        if (p->oomkilladj) {
                if (p->oomkilladj > 0)
                        points <<= p->oomkilladj;
                else
                        points >>= -(p->oomkilladj);
        }
view raw badness_5.c This Gist brought to you by GitHub.

Thus the ideal candidate will be:

One that was recently started, is a non-privileged process which together with its children uses a lot of memory, has been nice’d and does no I/O. Something like a nohup’d parallel kernel build (which is not a bad choice since all results are saved to disk and very little work is lost when a make is terminated).

From the SDE Tip – Amazon

Popularity: 2% [?]

What’s in /proc?

Quicktip:

/proc has subdirectory for each running process as well as some subdirectories for various aspects of your hardware. You can learn lots of details about your cpu, your memory, processes running on your computer, and more by digging around in /proc.

A few things you’ll find in /proc:

  • /proc/cpuinfo: Info about your cpu
  • /proc/meminfo: Info about your RAM
  • In /proc/<pid>/:
    • cmdline – the command line used to start the process
    • cwd – a link to the current working directory of the process
    • environ – the environment variable for the process separated by nulls. Try “cat /proc/<pid>/environ | xargs -n 1 -o”
    • fd – directory containing links to file descriptors opened by the process, including files that have been deleted since the fd was created!

What it’s good for:

  • Recover a deleted file if some process is still writing to it (tail -n 999999999 -f /proc/<pid>/fd/<file handle> > ~/recovered-file; kill <pid>)
  • Check if your cpu is 32 or 64 bit
  • Look into the environment variables your process was started with.
  • See what directory a process is running in.
  • Quickly see the full command like of a process.
  • Much more – look around!

 

From The SDE Tip – Amazon

Popularity: 1% [?]

Find Files Of A Given Name Instantly

Usually when I have to find a file, I use a find and a grep command together – because I still haven’t learnt to use find properly. This is what I do:

find -L . | grep <name>

But this process is usually slow as it traverses the whole directory structure recursively following the symlinks to find all the files and then greps for the name that I need.

Recently I learnt about a faster way of doing the same – use locate to find files of a given name quickly. The command is:

locate <name>

It creates an index of all the files on the system and searches off it when called for. The index is rebuilt (by default) once a day by a cron’d find so results maybe stale by 24 hours.

If locate is not already present on your system, you could install it by:

sudo yum install mlocate

From the SDE Tip – Amazon

Popularity: 1% [?]

Save File With Sudo Permissions In VIM

A lot of time I open a file, make changes and when the time comes to save it realize that I hadn’t sudo’ed it. Learnt that the following command helps:

:w ! sudo tee %

Here’s the reason:

:w tells the vi to save modification of the file

! sudo tee executes the tee command with sudo permissions taking it’s input from :w

% here means the current file name

From the SDE Tip – Amazon

Popularity: 1% [?]

Mistake of the Day : rm -rf ~

Kids these days huh! They never exercise caution before executing the rm -rf command. It’s gotten ingrained into their habit to use it just as commonly as ls. Careless people. And that is exactly what I did! Damn it! So here’s what happened.

I use tmux in which ` is set as the control character. So it’s pretty common for me to reach out to the ` keystroke. Now I was editing a file in vi. I wanted to save the file and move to the previous tmux screen.

:wq followed by “

Now : requires the SHIFT key be pressed. So usually you get used to having a finger over the SHIFT key. I entered the :wq command. But then somehow my fingers also pressed the SPACE and ~ keystrokes. So I ended up doing

:wq ~

Pff. Vi created a new file named ~. Bah. And then by habit, even before my brain could process anything, my fingers typed in

rm -rf ~

:D . In their defense their intention was just to delete the ~ file. But – Disaster! :’(

At this point I must mention that I was in the root directory /. Luckily enough I did not create a file named *. :D .

Popularity: 1% [?]

Ubuntu Lucid: Upgrading To Lynx In Virtualbox

I have Virtualbox 3.1.4 r57640 installed on Windows XP as the host, Ubuntu 9.10 as the guest. And since the ubuntu is not being used as an active development currently, I decided to upgrade it to Ubuntu 10.04 Beta 1. Having read a lot of positive reviews for Lucid Lynx, I wanted to try it out as soon as possible.

The upgrade took more than a couple of hours. It downloaded a GB of package data from repositories, and then took 90 minutes to setup the new system. Usually my ubuntu upgrades have been quite a painful process, having to manually configure a few things and set another few right. But then these things are to be expected if you always jump the ship in the Beta mode.

Immediately upon restart I was faced with my first problem. The xserver failed to load and instead threw a dialog box saying it was switching to low graphics mode for the session. I let it do so. Logging in into the system, as usual, my VirtualBox addons needed re-installation. But even that did not solve the problem. The same error popped up again upon restart. Also, my mouse integration did not work.

The first problem was occurring due to the new kernal 2.6.32-16-generic. Looking up in the forums, I found that virtualbox will incorporated the fix in their next maintenance release. However, one could apply the patch manually. It required modifying the file:

/usr/src/vboxvideo-3.1.4/vboxvideo_drm.c

The fix is r27248. You can read up about the ticket here.

To get the cursor integration working, we need to create xorg.conf. Ubuntu has done away with xorg and are using hal now. I could not clearly understand the reason behind the problem, but re-creating the xorg.conf file does the trick. Here is the forum where this issue has been discussed:

Cursor Control in Ubuntu 10.04

I do not seem to be having any more issues. I’ll keep consolidating any issues I run into and how to fix them so that I don’t have to google them up every time.

Popularity: 9% [?]

Courage And Failure: Read Data Protector

Ever since I joined FICO and received my laptop, I had been wondering if I could install Ubuntu on it. Now officially I might not be allowed to, but what if I take a chance and do it myself. However, there was a small glitch.

The laptop that we use comes with Data Protector installed. The Data Armor software keeps the hard disk encrypted at all times. Even while windows is running, the Data Armor is operational silently in the background. This was a variable in the equation of which I had no clue.

Specifically, my questions were whether Wubi would be allowed to modify the boot entry to make the presence of Ubuntu known? If yes, would it be able to boot into Ubuntu? And if true, would the usage of Ubuntu without Data Armor break my system in any way?

I tried googling around without any success. I was left alone, with no explanations or warnings. And messing around with your official computer’s boot record was too big a risk to take in the starting months of job,

So I waited. I thought of later buying a personal laptop and installing Ubuntu in it. But the impatient man that I am, I was at times tempted to just experiment.

Today was one of those days when the urge to experiment and know overcame my restrictive self. I decided to try it out. In the worse case I would go to the technical department and say sorry for the damage.

I downloaded Wubi. It said it would need to download 700 mb’s of Ubuntu unless a Desktop CD was provided. Yay ! I remembered I had the Desktop CD. A few of franctic search, and I found my CD hiding under a pile of rubbish junk. Excitedly I removed the CD from its cover, slid it into the drive and started the Wubi application.

It recognized my CD and started unpacking the contents. After copying all the files onto the disk, it asked for a reboot. I did so immediately.

Now was the time I would find out if the attempt was successful or was it thwarted by my foe named Data Armor. I kept my fingers crossed.

The boot screen came on for a few seconds and went off. This was normal. The screen was blank for a few seconds which stretched into infinity. I was worried now. If the Data Armor screen failed to show up, I would know that I have messed up things big time and would have to be prepared for some serious lectures the following Monday.

But while all these thoughts were engulfing me, the Data Armor screen came up. Yipee. I was too glad. I entered my account id and password. Upon checking the credentials, the computer restarted as it always does. Now the question was whether the dual boot option would show up. If the installer was successful in modifying the Master Boot Record, I should be getting an option to select either of the operating systems. And get, I did those.

My happiness was beyond measure. I selected Ubuntu and pressed the Enter button. I was jumping up and down, congratulating myself for having the courage to try out Wubi and get rewarded. The Ubuntu boot screen flashed on before me. Woohoo !!

But a few joys are short lived. Immediately a blank screen replaced the bar. Now this was wrong. Then a line mentioning BusyBox came up. The next line was a prompt saying

initramfs >

Wait there, this is not done. At this point of time, my Ubuntu should have been booting up and starting the automatic install procedure. I hard booted my computer and logged into Windows.

Cursed ! I was this close to escaping the fuss of Windows and enjoy the pleasures of Ubuntu. I googled up immediately to know if my setup had gone wrong or was it plain impossible to install Ubuntu in the encrypted hard disk. And this is when I stumbled across the FAQ of Wubi which has mentioned that Wubi fails if the hard disk is encrypted. Damn you Data Protector.

But at least I am happy that I tried. I can now rest peacefully knowing that its not possible as long as the disk is encrypted. The suffering of not knowing is cured.

So long as I do not find another way to get Ubuntu running on my machine, I shall tolerate Windows.

Popularity: 3% [?]

The Agony Of Having Data Protector

The company that I work for has provided me a Dell D630 Latitude. And since this is the only laptop I have, I use it for my personal purpose too. The laptop came with windows XP installed. Since I have never been a fan of Windows, I wanted to get back to ubuntu on my laptop.

This is where the problems started. Now the company is paranoid when it comes to security. They have installed an application called the Data Armor. This application keeps the hard disk encrypted at all times. In fact, when the laptop starts, the whole disk is encrypted and a password dialog box appears from Data Armor. At this point, if you do not know the password, you cannot decrypt the hard drive, thus ensuring the safety of the contents inside it.

When you log into Data Armor, the drive is decrypted, and the laptop boots again. This time though, the normal windows welcome screen appears. All the time that windows is operating, the Data Armor works in the background.

Now, I would like to install ubuntu using wubi on this laptop, but am not sure about a few issues. The biggest concern is that will installing ubuntu break or conflict with the Data Armor? I suppose the initial boot phase should be fine. After all, the hard disk is encrypted and Data Armor has no information whether the computer runs a windows or a linux. But then, once the reboot is done, will logging into ubuntu disrupt the functioning of Data Armor? Will it fail to maintain the encrypted state of the disk, thereby leading to a failure in the next boot up? Or will it gracefully allow the operation of ubuntu go on, and yet start up when logging into windows the next time?

So many questions to which I have no answers. I have tried googling around a bit but to no avail. I have only met with failure every time. In fact, I also tried querying the technical support guys at the company regarding this, but never got a satisfactory reply. A few question my need for ubuntu, reiterating that since my work does not require me to work on ubuntu, my official laptop should not contain the linux. A few nod to my possible explanations, but always remarking that even they are not sure of it. And then there are the few who don’t even know what wubi is!

So as it stands, I am yet to install ubuntu in my laptop. I will have to refine my search to get better results. Hopefully I succeed.

Popularity: 3% [?]

Installing Amarok 1.4 in Ubuntu Jaunty

Don’t like Amarok 2? Despair not, because there is a way to get back your Amarok 1.4 in Ubuntu Jaunty. Basically, you need to find a mirror of Amarok 1.4 and just install from there. One of those is mentioned below. This is all you have to do :

Add the repositories for Amarok 1.4 to your sources.list file:

deb http://ppa.launchpad.net/bogdanb/ppa/ubuntu jaunty main
deb-src http://ppa.launchpad.net/bogdanb/ppa/ubuntu jaunty main

Add the key:

sudo apt-key adv –recv-keys –keyserver keyserver.ubuntu.com \
0x1d7e9dd033e89ba781e32a24b9f1c432ae74ae63

And then finally, update your sources, remove Amarok 2 and install 1.4

sudo apt-get update
sudo apt-get remove amarok
sudo apt-get install amarok14

That’s it. You are done. You have successfully installed Amarok 1.4 in Jaunty. Have fun.

Popularity: 2% [?]

Amarok 2.1 : Managing Collections in Mounted Partition

I have a huge collection of songs stored in my 500 GB USB drive. Now this partition is accessible from /media/Songs/audio [I mounted the partition with label Songs]. The problem I was facing was that Amarok 2.1 failed to remember the location of audio folder. As a result, whenever I would close and restart Amarok, I would lose all the collection information. And if I build the collection again, the information such as number of times played and etc., would not be remembered.

However, Amarok auto-detected ~/Music/ as containing all the media files. This is the default location where Amarok would look for the collection of songs. This gave me an idea to solve my problem.

I created a link for my audio folder in the Music folder. To create a link in ubuntu, grab the folder, press and hold CTRL+SHIFT, and drag the folder to the ~/Music/ location. And we are done.

Now all my songs are remembered and all the folders watched for collection.

So, as I understand from the bug report filed, Amarok fails to remember the collections if they are either not in $HOME or not on the same hard drive. Well, this should help. Thanks Mark Kretschmann.

Popularity: 2% [?]