Skip to content
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

CoroutineScheduler: oversubscription in Dispatchers.Default when switching to IO with CPU tasks in local queue #3418

Closed
qwwdfsad opened this issue Aug 22, 2022 · 0 comments
Labels

Comments

@qwwdfsad
Copy link
Contributor

package kotlinx.coroutines.scheduling

import kotlinx.coroutines.*
import kotlinx.coroutines.scheduling.CoroutineScheduler.Companion.MAX_SUPPORTED_POOL_SIZE
import org.junit.*
import java.util.concurrent.*
import java.util.concurrent.atomic.AtomicInteger

class CoroutineSchedulerOversubscriptionStressTest : TestBase() {

    private val poolSize = Runtime.getRuntime().availableProcessors()
    private val inDefault = AtomicInteger(0)

    fun doCheck() {
        println("In default dispatcher, blocking thread #${inDefault.incrementAndGet()}, max allowed threads: $poolSize")
    }

    @Test
    fun testOverSubscription() = runTest {
        val barrier = CountDownLatch(1)

        // All threads but one
        repeat(CORE_POOL_SIZE - 1) {
            launch(Dispatchers.Default) {
                doCheck()
                barrier.await()
            }
        }
        Thread.sleep(200)
        withContext(Dispatchers.Default) {
            // Put a task in a local queue, it will be stolen
            launch(Dispatchers.Default) {
                doCheck()
                barrier.await()
            }
            // Put one more task to trick the local queue check
            launch(Dispatchers.Default) {
                doCheck()
                barrier.await()
            }

            withContext(Dispatchers.IO) {
                Thread.sleep(1000) // Wait for stealing
            }
        }
    }

}

Will print In default dispatcher, blocking thread #17, max allowed threads: 16

@qwwdfsad qwwdfsad added the bug label Aug 22, 2022
@qwwdfsad qwwdfsad changed the title CoroutineScheduler: oversubscription in Dispatchers.Default CoroutineScheduler: oversubscription in Dispatchers.Default when switching to IO with CPU tasks in local queue Aug 22, 2022
qwwdfsad added a commit that referenced this issue Nov 22, 2022
Previously, a worker thread unconditionally processed tasks from its own local queue, even if tasks were CPU-intensive, but CPU token was not acquired.

Fixes #3418
qwwdfsad added a commit that referenced this issue Nov 22, 2022
Previously, a worker thread unconditionally processed tasks from its own local queue, even if tasks were CPU-intensive, but CPU token was not acquired.

Fixes #3418
qwwdfsad added a commit that referenced this issue Nov 22, 2022
Previously, a worker thread unconditionally processed tasks from its own local queue, even if tasks were CPU-intensive, but CPU token was not acquired.

Fixes #3418
qwwdfsad added a commit that referenced this issue Dec 28, 2022
Previously, a worker thread unconditionally processed tasks from its own local queue, even if tasks were CPU-intensive, but CPU token was not acquired.

Fixes #3418
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant