Archive for the 'Tech' Category

Perforce: Can’t Clobber Writable File Error

When getting the latest version of a package from a perforce repository I was getting the following error:

Can’t clobber writable file

And of course the file was not being updated. Remember that all the files in perforce are marked as read-only unless opened by the ‘p4 edit’ command. I did a ‘p4 opened’ to check if the file had been opened for edit. It was not. Then I checked the permissions and found that the file was marked as writable.

Simply removing the writable field from the permissions of the file did the trick.

chmod -w <filename>

Now when I did a ‘p4 sync’ the file got updated. Happy.

Popularity: 1% [?]

Java: Fix Unchecked Warnings In Java Collections

If you use Collections.EMPTY_LIST, eclipse shows up an unchecked conversion warning. This can be easily avoided by using Collections.<FooBar> emptyList() instead.

Popularity: 1% [?]

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% [?]

JVM : Out Of Memory PermGen

Interesting thing I did not know about Java – PermGen. Apparently other than just the stack (local variables and methods) and the heap (everything else), java uses this extra storage which can also cause OOMs.

PermGen is where a few very long-lived types of objects are persisted in Java, such as class definitions and ‘intern’ed Strings.  It uses separate storage from the heap and the stack.  If you run out of PermGen you will get out of memory errors.  If you run very low you will get a JVM that spends all its time garbage collecting.

Few things I came about when I googled around:

Luckily I’ve never had to deal with PermGen OOM before. Apparently it’s hell-of-a-job debugging this issue.

From The SDE Tip – Amazon

Popularity: 1% [?]

Mac : Create Password Protected Folder

The Disk Utility app of Mac allows one to create a password protected folder. An encrypted disk image is created. To mount the disk one has to enter the password set at the time of creation. The steps to create one is very easy.

Open up the Disk Utility app. Goto File -> New -> Blank Disk Image. Enter a name for the image. Choose the location. Disk name will be the name of the mounted image. Set a size that you want. Importantly enable encryption from the drop down. And for the image format drop down, select the Sparse Disk Image option. Click on create.

A new window comes up to set a password. Remember to uncheck the “Remember password in Keychain” option. Set the password and you are done.

Double clicking on the image file will bring up a window to enter the password. Authenticate yourself and you’ll see that the image has been mounted. Add files, remove files, etc and do whatever you want to. Remember to eject the image after your work is done.

There you go. A password protected image is all setup.

Popularity: 1% [?]

Impossible Null Pointer Exceptions

I must admit this had never occurred to me before but after reading, the explanation seems so obvious!

You can get NullPointerException from lines in Java which appear to have no possibility of throwing them, such as;

this.setCount(num)

The null pointer exception can come about when num is of type Integer and setCount takes an int parameter. Java’s auto-boxing will automatically call num.intValue(), and if num is null you get an exception.

Of course, the fix is to check for null-ness and treat it however the semantics of your operation requires.

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% [?]

Kill A Process Running On A Particular Port In Windows

This has become a very common scenario for me these days. I have my grails application running on localhost:8080 and at times I need to manually kill the process. I am writing down the steps so that I know where to look it up quickly.

  • List the processes running on ports
    • netstat -a -o -n
  • Find the one you need. I search for 0.0.0.0:8080 and get the PID.
    • taskkill /F /PID <pid>

Task complete!

Popularity: 2% [?]