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

Kotlin/Native runBlocking & delay cause high CPU usage #1225

Closed
thowimmer opened this issue May 27, 2019 · 2 comments
Closed

Kotlin/Native runBlocking & delay cause high CPU usage #1225

thowimmer opened this issue May 27, 2019 · 2 comments
Assignees

Comments

@thowimmer
Copy link

thowimmer commented May 27, 2019

Hi all,

I just started a very simple hello world console application with Kotlin/Native for a linuxX64 target.
I want to increment a counter and print it every two seconds to the command line:

fun main() = runBlocking {
    var i = 0
    while(isActive){
        println("Hello Kotlin/Native COROUTINE #${++i}")
        delay(2000)
    }
}

When I run the binary on a the target host I encounter that CPU usage increases by 25% (one core being totally occupied). So it seems this is blocking the thread totally.

When I replace the delay(...) suspend function with a platform.posix.sleep(2) there is no noticeable CPU usage increase (0%).

here my build.gradle.kts:

plugins {
    kotlin("multiplatform") version "1.3.30"
}

repositories {
    mavenCentral()
}


kotlin {
    linuxX64("native") {
        binaries {
            executable()
        }
    }

    sourceSets {
        val nativeMain by getting {
            dependencies {
                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-native:1.2.1")
            }
        }

    }
}

tasks.withType<Wrapper> {
  gradleVersion = "5.3.1"
  distributionType = Wrapper.DistributionType.ALL
}

Is this a known issue ? or is just me being new to Kotlin/Native and coroutines... (bear with me if that's the case)

Many thanks for your support and the great efforts you put into bringing Kotlin coroutines to every platform !

Best regards,
Thomas

@qwwdfsad
Copy link
Collaborator

It's a known issue waiting for someone to report it :)

@thowimmer
Copy link
Author

thowimmer commented May 27, 2019

Good to hear that 😃. Looking forward to this issue being fixed.

For those who are looking for a temporary workaround for Koltin/Native & Multiplatform projects targeting POSIX compliant targets:

expected declaration in common/platform independent code

expect suspend fun delayOnPlatform(timeMillis: Long)

actual declaration in POSIX compliant module

actual suspend fun delayOnPlatform(timeMillis: Long) {
    memScoped {
        val timespec = alloc<timespec>()
        timespec.tv_sec = timeMillis / 1000
        timespec.tv_nsec = ((timeMillis % 1000L) * 1000000L).convert()
        nanosleep(timespec.ptr, null)
    }
}

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

No branches or pull requests

2 participants