New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
High CPU usate in SharedFlowImpl #4030
Comments
Hi @qwwdfsad
|
Thanks! I'm on vacation right now, so expect a bit of radio silence from me; I've got what you sent and will return to it later |
Thank you @qwwdfsad
This time, the profiler points to the Happy holiday |
I tried to create a reproducer. The code is: fun main() {
runBlocking {
val consumerCount = 1_000
val messageCount = 1000
repeat(50) {
val mutableStateFlow = MutableSharedFlow<Int>()
val sharedFlow = mutableStateFlow.asSharedFlow()
val nanos: Long
coroutineScope {
repeat(consumerCount) {
launch(start = CoroutineStart.UNDISPATCHED) {
val channel = sharedFlow.produceIn(this)
repeat(messageCount) { channel.receive() }
cancel()
}
}
delay(1.seconds)
nanos = System.nanoTime()
launch(Dispatchers.Default) {
repeat(messageCount) { mutableStateFlow.emit(it) }
}
}
val delta = System.nanoTime() - nanos
println(NumberFormat.getIntegerInstance().format(delta / consumerCount / messageCount))
}
}
} I did some benchmark with different counts
I attach the JFR Maybe a large number of subscribers can cause this behavior, so these measures can be normal. |
I confirm that our issue was caused by a code similar to my reproducer, we updated our code to reduce subscribers count. If the above benchmark are OK for you, feel free to close this issue. |
Thanks for the self-contained reproducer and all the profiles, it made my investigation so much easier 🙇 You hit the weakest spot of the I have a draft idea of how to fix it -- for each unique update (value/index/version) we can fallback to concurrent helping for the linear part (which still might be quadratic if you are unlucky enough and all collectors get OS-scheduled at the same time), but should be much better and eliminate the issue for a single-threaded usages. |
We detected high CPU usage on
kotlinx.coroutines.flow.SharedFlowImpl
using Java Flight Recorder on a 2 CPU machine.The consumed CPU was two order of magnitude than others, neither other code looks causing this CPU usage.
JFR's thread dump:
a bit later:
Unfornutately I am not able to provide a reproducer, we don't have idea how can cause this issue in our code. Secondary, this version of our server is running from Jan 8, 2024 without issue.
Our code use lesser 1% of CPU (
SharedFlowImpl.emit
), should we check some API usage that can cause this issue?The text was updated successfully, but these errors were encountered: