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”

An overview of the PC Real Time Clock (RTC)

Introduction

Have you ever thought how the computer is able to display the correct time after you power on the system? There is a component called the RTC in the computer which stores this date and time information when the computer is turned off.

I will talk about how to read/write the date and time and use other features of the RTC using command-line tools, Linux RTC driver and also a low level direct access to the RTC internal registers.
Continue reading “An overview of the PC Real Time Clock (RTC)”

Generate the process tree of a Linux system

This is a quick post on how to generate a process tree Linux (and *nix) operating systems.

The idea is the same, as in the previous posts: Finding overall and per core CPU utilization and Find process IDs of a running process by name. Read the information present in the /proc/ directory. To get which processes are running we can read the directories with numbers as their names in the /proc/ directory. To generate a process tree we need to establish a process child relationship within the running processes. Each process has a parent (the first generated process is an exception), and it is stored in the process table entry of that process. We need to fetch the parent process id for each running process inorder to establish the tree. Here’s the plan. Continue reading “Generate the process tree of a Linux system”

Find process IDs of a running process by name

In this post I will talk about a procedure to find the process IDs of a running process by name, which can then be used to send signals or do other stuffs. For example if you have multiple instances of bash opened, this should be able to get you the list of process IDs (PIDs) of the bash instances.

Firstly, a shell utility is already available called pidof which is a part of the sysvinit-tools package. There are a whole bunch of tools in this package which lets you query PID based on different requirements, send signals to set of processes, etc. Just check out the stuff.

I will only mention the outline of how this is done and post the sourcecodes to do it. After that this can be extended to have many features just like the tools of sysvinit-tools package or more.
Continue reading “Find process IDs of a running process by name”

Finding overall and per core CPU utilization

Today I will post about how to monitor CPU usage by processor in Linux. As you might have expected this will simply access the CPU time information from /proc pseudo-filesystem and report the results.

The proc filesystem or the Procfs is a special filesystem which gives you a view into the kernel data. No files in procfs exists actually in the disk. There is no disk inodes and thus storage related to the files. Instead of going into procfs I will redirect you to wikipedia: http://en.wikipedia.org/wiki/Procfs. Let’s get in.
Continue reading “Finding overall and per core CPU utilization”