18

在分析本土网络应用程序时,我遇到了非常奇怪的(至少对我而言)观察。

几乎所有时间都花在类的socketRead0()方法上SocketInputStream。这并不奇怪,因为我的应用程序对每个请求都使用远程服务进行联网。奇怪的是,这种方法不仅挂钟时间使用率很高,而且CPU 时钟时间也非常高。我不明白为什么 CPU 时间很高,因为如果我的应用程序等待远程服务回复(实际上并没有那么快),那么应用程序本身就没有什么可做的了。所以 CPU 时间应该很短。

更多观察:

  • 采样模式下的 VisualVM 显示该方法SocketInputStream.socketRead0()占用了高达 95% 的时间(挂钟时间CPU 时间);
  • mpstat(我们使用 Linux 作为操作系统)显示约 90% 的用户时间和约 1-3% 的系统时间(其余为空闲时间);
  • 部署在专用服务器上的应用程序;
  • 远程服务也是 HTTP Web 应用程序。平均响应时间约为 100 毫秒。平均响应大小约为 2Kb。
  • 我的应用程序使用 springRestTemplate与远程服务进行交互,而不是SocketInputStream直接。

现在我只有一个想法——也许这是在 JVM 中调用本机方法的开销(SocketInputStream.socketRead0()是本机的)?

你怎么看?这还有其他原因吗?

4

2 回答 2

1

I am facing the same problem. My application has a very high qps and each request will make me send multiple thrift calls, which use this native api : socketRead0

So I decide to do an experiment. I make a mock server with an api sleep 30s before return and a client calls this api. My purpose is to test the thread status when the net io happening. Based on my thread dump, the thread status is RUNNABLE.

This explains to two things :

  1. application with high qps blocking io will face a high cpu load value

  2. your java thread is still running in jvm since the thread state is RUNNABLE which will contribute to high user space cpu utilization

both of these will make your cpu busy.

I noticed during the experiment, the system space cpu utilization is low. I think this something relates to the thread scheduling strategy difference between jvm and os. We know hotspot threading model is 1:1, meaning one jvm thread to one os thread. when a blocking io happened, such as socketRead0 the kernel thread will set to state S and will not blocking cpu, but user space thread is blocking(waiting). when this happened, I think we need to rethink the fundamental I/O model in our application.

于 2017-04-26T06:00:04.013 回答
0

VisualVM 不是将负载显示为绝对值,而是显示为相对值,因此它只是意味着您的应用程序没有更多的 CPU 消耗点。

我相信您应该将 VisualVM 配置为不深入挖掘,而是将此方法调用视为代码(或 spring)中方法的一部分。

我已经经历过这样的行为,但它看起来不需要任何优化。Web 应用程序只需从套接字读取数据(即 HTTP 请求、数据库、内部网络服务......),但没有任何帮助。

于 2013-08-01T02:27:09.687 回答