CPU consumption in Unix/Linux operating systems are studied using 8 different metrics: User CPU time, System CPU time, nice CPU time, Idle CPU time, Waiting CPU time, Hardware Interrupt CPU time, Software Interrupt CPU time, Stolen CPU time. In this article let us study about ‘User CPU time’.

What is ‘user’ CPU time?
In order to understand ‘User CPU Time’, one should understand ‘System CPU time’ as well, since they go hand in hand. User CPU time is the amount of time the processor spends in running your application code. System CPU Time is the amount of time the processor spends in running the operating system(i.e., kernal) functions connected to your application. Let’s say your application is manipulating the elements in an array; then, it will be accounted as ‘User’ CPU time. Let’s say your application is making network calls to external applications. To make network calls, it has to read/write data into socket buffers which is part of the operating system code. This will be accounted as ‘System CPU’ time.

How to find ‘user’ CPU time?
User CPU time can be found from the following sources:

a. You can use web-based root cause analysis tools like yCrash to report ‘user’ CPU time. Tool is capable of generating alerts if ‘user’ CPU time goes beyond the threshold.

b. ‘user’ CPU time is also reported in the Unix/Linux command line tool ‘top’ in the field ‘us’ as highlighted in the below image.



Fig: ‘user’ time in top

How to simulate high ‘user’ CPU time?
To simulate high ‘user’ CPU reporting let’s use BuggyApp. BuggyApp is an open source java project which can simulate various sort of performance problems. When you launch BuggyApp with following arguments, it will cause the ‘user’ CPU consumption to spike up on the host.

java -jar buggyApp.jar PROBLEM_CPU



Fig: You can see the user CPU time spike up to 99%

Now let’s see the source code in the BuggyApp which is causing the ‘user’ CPU time to spike up.
public class CPUSpikeDemo {
 
	public static void start() {
 
		new CPUSpikerThread().start();
		new CPUSpikerThread().start();
		new CPUSpikerThread().start();
		new CPUSpikerThread().start();
		new CPUSpikerThread().start();
		new CPUSpikerThread().start();
		System.out.println("6 threads launched!");
	}
}
 
 
public class CPUSpikerThread extends Thread {
 
	@Override
	public void run() {
 
		while (true) {
 
			doSomething();
		}
	}
 
	public static void doSomething() {
		// Does nothing
	}	
}

Here you can see that BuggyApp is launching 6 ‘CPUSpikerThread’ in the ‘CPUSpikeDemo’ class. You can notice that ‘CPUSpikerThread’ is going on an infinite while loop and invoking the ‘doSomething()’ function again and again. This doSomething() method doesn’t do anything.

Thus If threads keep looping infinitely on the application-level code then ‘user’ CPU time will start to spike up.

How to resolve high ‘user’ CPU time?

1. Short-term/Tactical solution is to restart the high CPU consuming process.
2. Strategic/Permanent solution is to identify lines of code in the application that is causing the CPU to spike up using root cause analysis tools like yCrash and fix it.
3. If CPU consumption is consistently (i.e., 24 x 7) high, then it could be indicative that you are running short of compute capacity. In such circumstances, you can do couple of things:
a. Upgrade the capacity of your device. May be move to better EC2 instance (if you are in cloud environment).

b. Reduce the number of processes running on that device.

c. Try adding more devices (i.e. EC2 instances) to your application pool, so that your work load can get distributed.