0

On my Ubuntu 16.04, I am running a program that is relatively expensive in terms of CPU usage. It takes quite much time to finish, and other tasks (like browsing the web) get slower as a consequence. I was looking for a way to improve the computing power, if possible and at the same time to know better what is happening in my laptop.

By this, I found that, if I ask top, the program I am running shows a CPU usage of 100%, while when I open the System monitor the CPU usage is only at ~25% in the Processes tab. In addition, the Resources tab in System monitor shows 4 CPUs, whose usage changes from ~5 to ~100% for each one:

CPUs usage

The outcome of lscpu is:

@C:~$ lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                4
On-line CPU(s) list:   0-3
Thread(s) per core:    2
Core(s) per socket:    2
Socket(s):             1
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 142
Model name:            Intel(R) Core(TM) i5-7200U CPU @2.50GHz
Stepping:              9
CPU MHz:               3099.937
CPU max MHz:           3100,0000
CPU min MHz:           400,0000
BogoMIPS:              5423.81
Virtualization:        VT-x
L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              3072K
NUMA node0 CPU(s):     0-3

Is there anything I can do to improve the situation, such as dilute the work among the 4 CPUs? Usinge nice does not seem a solution to my situation.

I know this may be a general question that depends on the type of program I am running (let's say it is a Python script), but I am trying here to get general knowledge.

Py-ser
  • 697

2 Answers2

1

Is there anything I can do to improve the situation, such as dilute the work among the 4 CPUs? Usinge nice does not seem a solution to my situation.

In short: no. The program is single threaded.

Top can show cpu use in two ways: saturation per CPU, or share of total. If you have a four core system, top may show Irix mode:

Also for multi-processor environments, if Irix mode is Off, top will operate in Solaris mode where a task's cpu usage will be divided by the total number of CPUs. You toggle Irix/Solaris modes with the `I' interactive command.

You can toggle this by pressing I in top. As you have four CPU cores, the use is divided by four, and you will show 25%. If you turn on Irix mode, it will show 100%.

Your application is single threaded. It executes a sequential thread, on one CPU core. If you have the source code, you can change this. It's usually not a trivial task however, and depending on task it may not be possible.

Linux has a scheduler which will distribute running threads to different CPUs/cores.

vidarlo
  • 22,691
  • Thanks a lot. Can you tell how you know that the program is single threaded? Also, what is the "scheduler" you are mentioning at the end of your answer? – Py-ser Oct 14 '18 at 07:31
  • You can know in three ways: read the source, read the documentation, or observe it. The fact that it fully utilize one core is a solid indication. Regarding scheduler, this is the system responsible for distributing threads to available CPUs. – vidarlo Oct 14 '18 at 09:22
  • Great. So the scheduler is not like an app that the user can manage, but rather an automatic process which I can't (or hardly can, if I was a pro) access. – Py-ser Oct 14 '18 at 09:33
  • You can manage it to a certain extent. The Linux kernel has different schedulers, but they won't change how a single threaded program work. They may for instance favor response time over throughput. This generally depends on the workload the computer is running.Wikipedia has an entry on Schedulers. – vidarlo Oct 14 '18 at 10:47
1

100% vs 25% CPU usage

According to lscpu, you have four cores in your CPU:

On-line CPU(s) list:   0-3

top uses 100% per core, so if you have a four-core processor, that's 400%. You're using one core up completely, so that's 100% out of the 400%. The system monitor uses 100% for everything (or perhaps per actual CPU, I'm not sure). One core is a quarter of the four cores available, so it shows up as 25% usage in the system monitor.

Significant slowdown with one core used

If the system is slowing down a bit, it's probably just the CPU usage, and you can't do much about that other than raising the niceness value of the workload's processes.

If it's slowing down to a lot less than three-quarters speed, it's probably using a lot of I/O so that other applications can't do I/O very quickly, either using files or the network or swapping if there isn't enough RAM in the system, which are all shown in the system monitor application. File usage and swapping are also shown with sudo iotop.

Changing the task's priority

You can use nice with a high value like 15 to make the task give way to other tasks that need the CPU:

nice -n 15 the-program its-arguments

Slowing the task down

You can try both niceing the program to a high value like 15 and using the cpulimit program (in the cpulimit APT package) to reduce the CPU usage while it's allowed to run.

An example slowing the the-program its-arguments down to 60% CPU usage and using a niceness value of 15:

cpulimit -q -z -l 60 -- nice -n 15 the-program its-arguments

Using multiple cores

There are a few ways to proceed as far as splitting up the workload to use more cores. This is probably not what you want to do if you're trying to do other things on the computer, since it can use up even more processing power to do the tasks faster.

One is to run the process four times each with one quarter of the workload, if there's some way to specify the workload when running it. One possibility useful in some circumstances is using xargs -n ## -P 4 command to process ## items at a time on each core using 4 cores.

Another is to rewrite the application to use four or so threads instead of just one. If there's a lot of I/O waiting in your application or something like that, you can potentially increase it far above four threads.

There are other possibilities as well.

Combining using multiple cores with slowing the task down and setting the niceness value

You can combine using multiple cores with slowing the task down or changing the task's priority. This will allow you to have the task finish more quickly but to get out of the way of other processes.

Chai T. Rex
  • 5,193