question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Why is offerInternal and takeFirstReceiveOrPeekClosed using such high CPU time?

See original GitHub issue

I am experimenting with a project for using coroutines and channels under the hood for reactive-style data streams:

https://github.com/caleb-allen/Konko-Flow

The idea is that any (stateless) operator can be run concurrently. As an example:

val a = listOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Flow.from(a)
        .map { "$it says hi!" }
        .forEach { println(it) }

Each stage would have coroutine(s) processing the operator, and channel(s) to send the data downstream:

[fromOp] -------- channel -------- [mapOp] -------- channel -------- [forEachOp]

Each “stage” is an operator plus a channel. A stateless stage may have multiple coroutines processing the operation, and a channel for each coroutine to pass the results down. We only need a single stream to show the issue.

channels_offerinternal_visualvm

Here is a jvm snapshot using VisualVM. This is using 1 stream. I’ve been digging through the source to figure out what is hanging but am at a loss. As you can see, AbstractSendChannel.send() is taking significantly more time than the user code ChannelTest$collectTest$times$1$wordsCount$1.invoke(), where the operation is being applied. I am using unlimited channels with LinkedListChannels. Am I misunderstanding or misusing channels?

Best,

Caleb

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
qwwdfsadcommented, Mar 26, 2018

I’ve used async-profiler and disabled debug mode which is enabled in tests by default.

1reaction
qwwdfsadcommented, Mar 26, 2018

Yes, that’s what I needed, thanks.

I’ve ran your test in a loop for a while and profiled it:

  1. Your code (everything starting from ChannelTest$) takes ~30% of total execution time
  2. ~14% is spent for reading file
  3. ~50% is communication overhead

In pipeline Flow.from(file).partition().flatMap().collect() every source element is passed through 4 concurrent channels and up to 4 context switches, which is a performance killer. Currently our own channel implementation suffers from the same issue, https://github.com/Kotlin/kotlinx.coroutines/issues/285. As a solution, consider fusing all intermediate operations into one channel or build your library on top of kotlinx channel operators and wait until https://github.com/Kotlin/kotlinx.coroutines/issues/285 is fixed.

Am I misunderstanding or misusing channels?

No, in general your approach to build Konko-Flow pipelines is fine.

As an additional performance improvement we can provide specific Channel implementations such as array-based OneToOneChannel and OneToManyChannel, but this improvement is negligible in comparison with operator fusing

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Fix High CPU Usage - Intel
Find out all the reasons why your PC displays high CPU usage. Our step-by-step guide will show you how to fix your CPU...
Read more >
How to Lower CPU Usage: Common Causes & Tips - N-able
A longer count means the system is busy or overloaded. High physical memory usage is often a consequence of using too many demanding...
Read more >
What is CPU usage, and how to fix high CPU usage
If your PC has slowed down, high CPU usage may be the cause. In this guide, we will show you how to check...
Read more >
How to Lower CPU Usage - NinjaOne
In this article, we'll look at how to remedy high CPU usage on your machine or ... However, if your PC is always...
Read more >
Guidance for troubleshooting high CPU usage - Windows Server
Keep in mind that you can expect CPU usage to increase as a process or an application serves requests. However, if you consistently...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found