Why does select() consume so much CPU time in my program?

大憨熊 提交于 2019-11-28 06:48:48
Denis Bazhenov

Unfortunately, this is wrong interpretation of the numbers.

I've faced this situation many times (and ask a question on stackoverflow too).

The main reason, is VisualVM doesn't show correct CPU time. It showing percentage of the thread time in RUNNING state. But from documentation on Thread.State:

Thread state for a runnable thread. A thread in the runnable state is executing in the Java virtual machine but it may be waiting for other resources from the operating system such as processor.

This is exactly what's going on. Actually, thread is blocked inside OS epoll_wait() call. On Linux box there are several ways you can confirm it's the case.

strace'ing thread

$ strace -tttT -f -p [thread-id]

Thread id could be obtained from jstack output:

$ jstack [java-pid]
[...]
"Netty Builtin Server 1" #17 prio=5 os_prio=31 tid=0x00000001013dd800 nid=0xe12f runnable [0x0000700001fe4000]
  java.lang.Thread.State: RUNNABLE
  at sun.nio.ch.KQueueArrayWrapper.kevent0(Native Method)
  at sun.nio.ch.KQueueArrayWrapper.poll(KQueueArrayWrapper.java:198)
[...]

in this case thread id is 0xe12f (should be converted to decimal). You will see the most of the time thread will be in epoll_wait() call.

pidstating thread

$ pidstat -tu -p [java-pid] | grep [thread pid]

you will see low system as well as user CPU time by this thread, meaning it doesn't consume CPU.

polling thread state using ps

$ ps -eL -o pid,tid,state | grep [thread-id]

you will see most of the time thread in state S or Sl (interruptible sleep) instead of R (runnable).

In the end you should not worrying about that if there are no operational issues with the service.

One app is polling 10,000 connections, using very little CPU per connection, but all together it could add up to a good fraction of CPU time. All priority does is let some other job get in line first.

The other app that has fewer connections but does more crunch per connection could also show the higher percent, but it should show a lower fraction of time waiting, and a higher fraction CPU-ing.

jzd

Like the answer from the linked question referred to, the common problem is older JDK bugs. Since you are now on an updated version, I think the this might actually be a hardware bottleneck.

Here is a link to a glassfish issue describing where they discuss the possibility of hardware (network and servers) being the source of the problem.

https://www.java.net//forum/topic/glassfish/glassfish/glassfish-31-deadlock-epollarraywrapperepollwait-how-handle

Also, here is another similar question with no answer yet: SelectorImpl is BLOCKED

Firstly, it's good that both applications have the same issue; it probably indicates that the issue is with either the JVM or the OS and not your application :-)

As jzd mentioned, nio.select() has had issues. The multiplication of {various versions of Java} x {various platforms, kernel versions} makes it seem like an all-over-the-place issue. I hope one of these works for you :

  • If you are on Linux, try the 2.6 kernel just in case you are on 2.4

    , assuming the bug is akin to : http://bugs.sun.com/view_bug.do?bug_id=6670302

  • Use an older version of the JRE / JDK, not the latest version!

    , i.e., go back to JRE 6 / JDK 6 instead of 7.

Try

  • {older version of JRE (6), older version of Linux kernel} or
  • {newer version of JRE (7), newer version of Linux kernel}

instead of mixing them up as in {older, newer} or {newer, older}.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!