nice CPU time – ‘ni’ time in top

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 ‘nice CPU time’.

What is ‘nice’ CPU time?

To understand ‘nice’ CPU time, one must first understand ‘nice’. When there is a CPU contention (i.e., multiple processes contend for CPU cycles), the process with high priority are given more chances to run. In Unix/Linux operating systems, processes are launched with a priority 0 (default). However, using the ‘nice’ command, super users (like ‘root’) can set the priority for the processes as shown below:

nice -n nice_val [command]

Example:

nice -n 15 java -jar buggApp.jar

Priority can range from -20 to 19. -20 is the highest priority with which a process can be launched. 19 is the lowest priority. For more options on how to launch process with nice priority or change the priority level using renice command refer to this article.

‘nice CPU time’ is the amount of time CPU spends in running the low priority processes (whose nice values are greater than 0). 

How to find ‘nice’ CPU time?

 nice CPU time can be found from the following sources:

a. You can use web-based root cause analysis tools like yCrash to report ‘nice’ CPU time. Tool is capable of generating alerts if ‘nice’ CPU time goes beyond the threshold. It will also generate alerts if your application process is running with low priority.

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

Fig: ni time in top

How to simulate high ‘nice’ CPU time?

To simulate high ‘nice’ 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 CPU consumption to spike up on the host. 

 java -jar buggyApp.jar PROBLEM_CPU

Now let’s launch BuggyApp with 3 different priorities and study the ‘nice’ CPU time reporting in the ‘top’. 

1. Default priority

Now let’s launch BuggyApp with default priority using the below command:

java -jar buggyApp.jar PROBLEM_CPU

 Below is the screenshot of ‘top’ reporting on the host:

Fig: Normal priority process CPU consumption reported under ‘user’ in top

You can notice that BuggyApp’s CPU utilization is reported under ‘user’ CPU utilization. You can see the ‘user’ CPU time is at 99.3%, whereas ‘nice’ CPU utilization is at 0.0%.

Note: ‘user’ CPU utilization is the amount of time your application spends in executing application level code. To learn more about ‘user’ CPU time, refer to this article.

2. Low priority

 Now let’s launch the same BuggyApp with a low priority nice value ’10’. (Note: As values goes higher than 0, priority becomes lower). You can launch the BuggyApp with low priority 10 using below ‘nice’ command:

nice -n 10 java -jar buggyApp.jar PROBLEM_CPU

 Below is the screenshot of ‘top’ reporting on the host:

Fig: Low priority process CPU consumption reported under ‘nice’ in top

You can notice that now BuggyApp’s CPU utilization is reported under ‘nice’ CPU utilization. You can see the ‘nice’ CPU time is at 99.3%, whereas ‘user’ CPU utilization is at 0.3%. Note: Reporting is totally opposite of what we saw when the process was launched with default priority.

3. High priority

 Now let’s launch the same BuggyApp with a high priority nice value ‘-10’. (Note: As values goes lower than 0, priority becomes higher). You can launch the BuggyApp with high priority -10 using below ‘nice’ command:

nice -n -10 java -jar buggyApp.jar PROBLEM_CPU

 Below is the screenshot of ‘top’ reporting on the host:

Fig: High priority process CPU consumption reported under ‘user’ in top

You can notice that now BuggyApp’s CPU utilization is reported under ‘user’ CPU utilization. You can see the ‘user’ CPU time is at 99.3%, whereas ‘user’ CPU utilization is at 0.0%. Note: Reporting is totally opposite of what we saw when the process was launched with lower priority.

What to do if ‘nice’ CPU time is high?

1. If your production application (and it’s dependent processes) are running with low priority and it is causing the nice CPU time to go high then increase the priority. Production applications should always run with default or high priority. 

2. If ‘nice’ CPU consumption is intermittently high, then identify the process which is consuming high CPU time using tools like top, yCrash. Once identified, tactical/short-term solution is to restart the offending process. Strategic/Permanent solution is to optimize the application performance using tools like yCrash.

3. If ‘nice’ 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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

A WordPress.com Website.

Up ↑

%d bloggers like this: