应用程序空闲时,Akka线程使用100%CPU [英] akka threads use 100% CPU while application is idle

查看:209
本文介绍了应用程序空闲时,Akka线程使用100%CPU的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行AMD四核处理器的Ubuntu 12.04系统上使用akka/scala 2.10.3进行实验.

I am experimenting with akka / scala 2.10.3 on an Ubuntu 12.04 system running an AMD Quad-Core processor.

我编写了一个服务器,该服务器在收到请求时应执行一些计算.客户端将使用actor消息发送一些数据(字符串列表).当服务器上的actor收到列表时,它将其分成四个列表,并由四个子actor对列表进行排序.

I have written a server that should perform some computation when it receives a request. A client will send some data (list of strings) using an actor message. When the actor on the server receives the list, it splits it into four lists and has four child actors sort the list.

到目前为止,效果很好:该程序正常运行,我可以验证客户端是否收到正确排序的列表.

So far so good: the program works and I could verify that the client receives a correctly sorted list as a result.

但是,服务器的行为中有两点我无法理解:

There are however two things I do not understand in the behaviour of the server:

  1. 启动服务器后,即使没有参与者进行任何处理(他们都在等待消息),我处理器的所有四个内核的利用率几乎都达到了100%.这怎么可能?不应该仅在安排一些参与者处理某些消息时才使用内核吗?
  2. 我曾期望由四个参与者完成工作会自动暗示将以四倍的速度加快排序:每个参与者在单独的线程中运行,而每个线程在不同的内核中运行.但是,事实并非如此:没有观察到加速,甚至基于actor的排序都比普通的单线程排序慢得多.原因可能是我的调度程序配置,即默认配置不会自动利用多个内核吗?

修改 按照丹·西蒙(Dan Simon)的建议,我使用visualvm调查了问题1.

Edit Following Dan Simon's suggestions, I have looked into problem 1 using visualvm.

visualvm报告大多数时间都在等待的几个调度程序线程(因此它们似乎并没有占用太多的CPU时间). visualvm还显示了其他几个始终在运行的线程,即使应用程序未执行任何操作;至少,我的代码都没有被执行.这些 busy 线程被命名为New I/O worker #1, #2, #4, #5New I/O boss #3New I/O server bossSignal DispatcherRMI TCP Connection(2)-127.0.0.1Attach ListenerRMI TCP Accept-0.

visualvm reports several dispatcher threads that are waiting most of the time (so they do not seem to use much CPU time). visualvm also shows several other threads that are running all the time, even though the application is not doing anything; at least, none of my code is being executed. These busy threads are named New I/O worker #1, #2, #4, #5, New I/O boss #3, New I/O server boss, Signal Dispatcher, RMI TCP Connection(2)-127.0.0.1, Attach Listener, and RMI TCP Accept-0.

我有一些akka编程经验,但是几乎没有配置它的经验,因此我对上述信息没有多大意义.

I have some experience programming akka but almost no experience configuring it, so I cannot make much sense out the above information.

推荐答案

他们进入了仅等待传入消息的状态"

"They move into a state in which they just wait for incoming messages"

当消息通过回调方法到达时,行为者会收到通知吗?还是行为者本身正在轮询某些队列以查看是否有新消息?

Are the actors notified when a messages arrives via a callback method or are the actors themselves polling some queue to see if there are new messages?

如果您正在轮询,后者可能导致100%的CPU使用率,却找不到工作,然后立即返回以再次轮询(本质上是忙于旋转).解决方法是在再次轮询之前先等待(Thread.yield(),LockSupport.parkNanos(1)等).

The latter can cause 100% cpu usage if you are polling, finding no work and then going right back to poll again (essentially busy spinning). The fix is wait (Thread.yield(), LockSupport.parkNanos(1) etc) for a bit before polling again.

这篇关于应用程序空闲时,Akka线程使用100%CPU的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆