[JAVA] Is 100% CPU usage bad?

2019/9/29 Partial revision

==========================================================================

0. Introduction

Thank you so much for seeing previous post. It's been a long time since the last time, but this time I would like to introduce a topic that was very intriguing to me from the following books I read recently (as of September-October 2017).

Translated by Scott Oaks, translated by Acroquest Technology Co., Ltd., translated by Kao Terada, translated by Satoshi Makino "Java Performance" published by O'Reilly Japan https://www.oreilly.co.jp/books/9784873117188/

1. Meaning of "100% CPU usage"

How do you think of a state where the CPU usage is 100%? I don't think you have a good image, "Isn't it adversely affecting performance?" The book states that this is not always the case.

I will quote the text, but first of all, the author of this book

Suppose a program takes 10 minutes to run and the CPU usage is 50% during that time. If you could tune it to 100%, it would double the performance and run in 5 minutes. If we could double the performance here, the CPU usage would remain at 100% and the execution time would be two and a half minutes.

After giving an example, "CPU utilization indicates how efficiently the program is using the CPU", and concludes that "the larger this value is, the more desirable the state". ..

Of course. When the program is executed, the program module is expanded from the disk to the memory, and the information stored in the memory is sequentially read by the CPU and executed. It is desirable from the viewpoint of performance if the program can be executed without letting the CPU rest.

2. Cases that are not "100% CPU usage"

On the contrary, when the program is executed, if the CPU is resting, in other words, if it is not 100% used up, there is some reason why the CPU does not run, and analysis and sometimes tuning are required for it. is.

The author of this book cites the following three causes as cases where the CPU is not 100% used.

-(Omitted) The application is blocked and execution is suspended until the lock is released. --The application is waiting for something (for example, a response from the database). --There is nothing for the application to do.

For the first and second causes above, "If you can reduce the contention for locks and tune to make the response from the database faster, (...) program execution will be faster and CPU usage will be increased. Will go up. " On the other hand, as for the third cause, for example, "a server application that waits for a request from a client" is supposed to "process all HTTP requests waiting to be processed and wait until the next request". In this case, the CPU is resting, indicating that it is still ready to accept HTTP requests.

3. Think about the meaning of CPU usage

By the way, CPU utilization generally refers to the percentage of how much CPU has been running for a given amount of time. A CPU usage of 45% means running 450 milliseconds for a given time of 1 second. From a different point of view, 450 milliseconds of the second was in full operation, so if you quote from this book,

In other words, 450 ms of 1 second is 100% CPU usage and the remaining 550 ms is 0%. We refer to this situation and express that the CPU usage is 45%.

That's why. Looking at the CPU usage of 45% (the CPU wasn't running for 550ms per second), is that reasonable? Where should I fix it if it's not valid? You need to think about that.

Four. At the end

The ultimate goal when running a program is to use the full CPU and finish it in a short time. In other words, maximize performance with 100% CPU usage. Therefore, if there is time for the CPU to rest, it will be tuned as needed as described above. Don't get me wrong here, 100% CPU utilization is a way to improve performance, not an end. If you try to execute a program and the CPU usage is 100% for a long time, it will not only finish in a short time, but it will prevent other programs from executing, resulting in bad results. .. On the contrary, even if the CPU usage is 70%, if the performance that meets the requirements is obtained, it will be considered that there is no problem. "100% CPU usage" is not bad. However, it should be said on the premise that good performance is being obtained.

5. Digression

When you think of Java books, you probably think of introductory books that start with "What is Java?", Books that explain while creating Web applications, and books that introduce design patterns. This book focuses on Java performance. The know-how and analysis method for improving Java performance, the mechanism of garbage collection in Java VM options, etc. are explained. Although it is a translated book, I think it is the only book in Japan that focuses on this point of view. If you are interested, please pick it up and take a look.

Thank you for reading this far.

Recommended Posts

Is 100% CPU usage bad?
Why null is said to be bad