CPULeash: A tool for capping per-process and process group CPU utilisation from userspace

It’s now mid October, there are 0 posts in 2017. Therefore I think it’s time to loosen up posting constraints and try make a bit more casual posts. For a start I will dig up one of the unfinished works. I will start with a quick overview of an old piece of tool (unmaintained) I wrote quite a few years ago. The tool caps the maximum CPU utilisation of a process from userspace without requiring root access. One instance of the process can cap multiple processes individually, in a group or it can also cap an entire process sub-tree. I intended to post about it in great detail, but this time I will keep it brief, and dig deeper in the next opportunity. Let’s get into it.

Motivation (from agony)

A few years ago, I was used to run different CPU intensive experiments on my old laptop, which would sometime run weeks. The CPU temperature used to go up to 95 Deg C. Sometimes it went upto 99 Deg C and then it used to throttle down the cores to get the temperature down. This is definitely not a healthy temperature, therefore I feared of hardware damage. The laptop had a metal case, which became so hot that it left red marks on my thighs. One time an entire chocolate bar melted into a pool of liquid chocolate (someone left it near the exhaust fan). Therefore it was time that I figure something out, such that I can run the experiments, but keep the laptop cool (relatively).

The obvious idea was to cap the CPU usage for the processes. An easy solution is to install a virtual machine and run the code inside the virtual environment. Note that, in my case nice does not work, as it does not help control how much of the CPU a processes utilises when it acquires it. I could have used cgroups, but I didn’t want to (I don’t know why). At that point of time I was feeling the need for Solaris Zones.

Essentially what I wanted is something portable in any *nix system, works completely from userspace and also does not need root privilege. Therefore I made a small tool which does in. It was written in C and POSIX compliant libraries.

Also note that, there is a similiar kind of tool called cpulimit, which I didn’t know existed when I wrote this. This tool works well too.

Given this situation, it was time to write something in-house … drum rollCPULeash.

Continue reading “CPULeash: A tool for capping per-process and process group CPU utilisation from userspace”

Advertisements

Simple script to restart services automatically when stopped in Fedora/Redhat

fedora_logoWhen occasionally the services like Apache or MySQL or other rc.d script crashes and stops working, then instead of restarting them manually, it may be more desirable to restart them automatically. A friend of mine asked about such an issue and here is the quick fix bash script fix.

Continue reading “Simple script to restart services automatically when stopped in Fedora/Redhat”

Bash Script: Reading ID3v1 Tags

mp3 files contain track name, artist name, etc meta data about the music file itself. The meta data format used in mp3 files are called the ID3 tags. There are different versions of ID3 tags available. The objective is to read this meta data information from an mp3 file having an ID3v1 tag.

A detailed overview of the ID3v1 (as well as ID3v2 tags) are done here in this post: What are ID3 Tags all about?. A small ID3v1 library is also implemented (reading and writing tags) in C language here: An ID3v1 Tag Parsing Library. TagLib is available to read and write ID3v1 and ID3v2 tags and is free to use.
Continue reading “Bash Script: Reading ID3v1 Tags”

Bash Script: Generating Primes within Range

Objective

Generating prime numbers within a range using bash script. The technique used in this implementation to generate the prime numbers is still the good old divide and check method, but with some tweaks attempting to make the execution a bit better.

Approach

The core method is, we check if an integer p is prime by:

  1. Dividing with integers which are not multiples of 2’s and 3’s
  2. Dividing with integers upto floor ( sqrt ( p ) )

The integer p itself is needed to be not divisible by 2 or 3 or both.
Read more to get the bash script

awk to the rescue

Some days back I got a collection of 100 Greatest Piano Works cd collection. The first thing I did was to rip the CD in flac and preserve a lossless copy of the disks. Then I converted those to ogg files (with oggenc ) so that I could take it in my iPod and listen at any time. The problem occurred at the final moment when I realized that the file names were really long and which would be very difficult to scan through my iPod (with Rockbox). The file name was in this format : %n – %a – %t.ogg  for example : For example :
“17 – Robert Schumann (1810-1856) – THE DAVIDSBÜNDLER DANCES, 18 CHARACTERISTIC PIECES, OP. 6: XIV. Zart und singend – Dolce e cantando.ogg” . The western classical musics this combination is very long most almost all the tracks, and these files with huge file names and lot of files having ‘:’ in the name being refused to copied directly. So I needed to truncate or rename the 100 files in a way so that they are identifiable and also truncated to short names.
I decided to keep only the track name and composers names (%n – %a fields), by renaming the files with some batch file renaming process. Continue reading “awk to the rescue”