I know that in Java one can use ThreadMXBean to get CPU utilization of any thread.
However, as I understand an actor can be scheduled to execute on any thread from a thread pool. So subsequent calls to getCurrentThreadCpuTime
from the same actor may end up giving totally unrelated numbers.
Is the above true? If yes, is there any way to find the CPU utilization by the actor itself?
Your thinking is correct.
Akka, by default assigns a free thread from a pool of threads as needed, no promise is made about which thread from the pool will be used for each message sent to an actor (although batching will tend to mean that messages sent to the same actor close together will tend towards being on the same thread). Akka does allow configuration of the thread pools, and that could include configuring a single thread per actor. However sharing threads across multiple actors avoids some of the weaknesses of the SEDA architecture (where it can experience increased latency on multi-socket setups).
CPU utilisation is tricky in Java at the best of times. Java delegates thread scheduling to the OS, and does not expose much in the way of querying that information from the OS. Add Akka's use of thread pools, and separating Akka scheduling from actor runtime and that leads us to saying that the answer is no; there is no supported way to get an accurate CPU utilisation on a per actor basis.
As an alternative, you could measure wall clock time within each Actor. This would be simplest done by adding your own wrapper to each actor's receive method. TypeSafe used to have a monitoring tool for Akka, which they have discontinued and have instead started partnering with thirdparties to provide enterprise monitoring.
Though! this post & answer is quite old.i would like to update the answer
i was in a similar situation when comparing CPU utilization of java threads and actors.
i used Kamon.io partnering with https://www.datadoghq.com to get the better view of CPU, memory and network usage of my actors.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With