The system performance of the online server is not good. How can I break it with the top command?

content

  • How the online system works
  • How does the CPU run multiple threads concurrently?
  • What are the consequences of having too many threads?
  • What exactly is the often heard CPU usage and load?
  • How to view CPU usage and load through top command?
  • Summarize

Today I will share with you a knowledge point, that is, if the performance of the Java system deployed on our online server is found to be not good, how should we log in to the online server at this time, and then use the top command to analyze the performance of the online system? Next, we will show you step by step how to use the top command.

How does the online system work?

First of all, before explaining how to use the top command, we have to give you some basic knowledge, that is, what resources he will need to use when the online system runs on the server, or in other words, he will need to use What hardware to the server?

The first thing you need to understand is that the essence of the system running online is actually a linux process, that is, a JVM process. This JVM process will actually open multiple threads, and each thread will be handed over to the CPU to run. This CPU is a very critical piece of hardware for us. While running our thread, the CPU will also run the part that the thread is responsible for executing. code, as shown below.

How does the CPU run multiple threads concurrently?

But it's just the fact that the CPU runs multiple threads. Many people may not know that modern servers have multiple CPUs, or that a CPU has multiple cores. We have to figure out that a CPU has multiple cores. A CPU core running multiple threads concurrently is not the same as multiple CPU cores running multiple threads at the same time.

A CPU core runs multiple threads concurrently, which actually refers not to running multiple threads at the same time, but to quickly switching to run each thread, that is, a CPU core will run thread A first, and execute thread A for a while. code, then immediately switch to run thread B, run the code of B for a while and then quickly switch to run thread C. The CPU core is just switching very fast, giving you the illusion that a CPU core can run multiple threads at the same time, but it is not, as shown in the figure below.

What if there are multiple CPU cores? At this time, each CPU core can actually run a thread. If there are multiple CPU cores running multiple threads at the same time, it is true at the same time, because each CPU core can run the code of a thread independently, as shown in the following figure.

What are the consequences of having too many threads?

In many cases, the more threads, the better. Many small partners often make a mistake when developing online systems, that is, they think that if the system runs hundreds of thousands of threads at the same time, they can process requests with high concurrency. Not at all. If too many threads are opened, it will cause each CPU core to switch frequently to run n multi-threads, and each thread switch has a time overhead. Too many threads will cause each thread to run for only a short time and then be suspended for a period of time before it can be run again. Why? Because there are too many threads, the CPU core needs to run each thread, and the rain and dew will be soaked, as shown in the figure below.

The above picture is very obvious. You have a request and it is handed over to a thread for processing. This thread has just got the opportunity to run the CPU. After running for a while, the CPU will switch to execute other threads. , and you have to run many other threads, and finally you can switch back to run your thread. Does it take a long time for you to run a request?

Therefore, in general our experience is that when an online business system is connected to a MySQL database, a machine with 4 CPU cores often opens 200 business threads to process requests, which is about 1,000 threads per second. Because if you have more requests and more threads, 4 CPU cores can't be busy.

What exactly is the often heard CPU usage and load?

So here we introduce another concept, which is CPU usage and load. Everyone understands the CPU usage. The higher the CPU usage, the more the CPU is not idle and has been running desperately. The same is true for the load. The higher the load, the more busy the CPU is. If the CPU usage and load are too high, especially when the usage exceeds 90%, it often means that your system is under too much load, and the performance will drop at this time. .

When you send a request to the online system, because the busy CPU has to do a lot of things, it may leave you less chance to process the request, which will cause your request to wait for a while to be processed. System performance will inevitably decline, as shown below.

如何通过top命令来查看CPU使用率和负载?

因此如果线上服务器运行的系统性能不好,往往第一步就是登录到线上服务器使用top命令查看当前服务器的CPU使用率和负载是不是过高。如果过高了,那么说明当前系统压力过大,CPU过于繁忙,导致请求过来后处理比较慢,性能自然就下来了。

当然,如果公司有可视化监控系统,可以直接看到线上服务器的CPU负载和使用率,那也是没问题的。

接着一起来看看top命令吧,这里我用自己的MAC电脑执行了top命令,会得到如下结果。

现在来看一看这个top命令的输出结果中都有什么,东西很多,这里只挑选重要的东西来看就行了。

Processes是说当前机器上的进程情况,包括了运行中的进程数量、休眠中的进程数量以及进程开启的线程总数量。例如上图,我的电脑里有387个进程,就2个在运行,385个都是在休眠,一共有3834个线程。

Load Avg和CPU Usage是比较重要的,这里暂时重点看CPU Usage就可以了,它就是我们所说的CPU使用率。这个使用率一般最高是在80%左右,如果到90%以上,甚至超过100%,那就很危险了。例如我的电脑负载很低,使用率就1%~2%,空闲率在90%多。

PhysMem是指服务器的内存被使用了多少GB。

NetWorks是指通过网络输入和输出的数据量。

Disks是指对磁盘文件写入和读取的数据量。

最后还有当前运行的主要进程列表,每个进程对CPU等资源的消耗信息。

总结

好了,今天的知识点就到这里了,重点是给大家讲解了CPU对系统性能的影响,以及如何通过top命令查看服务器的CPU使用率和负载情况。

END

扫码 免费获取 600+页石杉老师原创精品文章汇总PDF

原创技术文章汇总

img

Guess you like

Origin juejin.im/post/7085145990214189063