Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Even if you do nothing after calling into_event_stream, it will still consume CPU. #159

@oldshensheep

Description

@oldshensheep

Since into_event_stream is just a call to AsyncFd::new, I think it might be a Tokio issue, not sure.

If tokio is working as intended, I will close this issue(which is pretty bad news, I will switch to blocking mode with a channel);if not, I will report it upstream.

This is an issue for me, I want to optimize CPU usage as much as possible, and ideally pause event fetching when it's unnecessary.

Step to reproduce

  1. Compile the following code and run it with env time -v ./target/debug/examples/async-evdev
use evdev::Device;
use std::time::Duration;
use tokio::task;
use tokio::time::sleep;

#[tokio::main]
async fn main() {
    let path = "/dev/input/by-id/<your-mouse>";
    let device = Device::open(path).unwrap();
    let event_stream = device.into_event_stream();
    sleep(Duration::from_secs(1000)).await;
}
  1. move your mouse very fast about 3s.
  2. Ctrl-C terminal the program.

Expected result

CPU usage is zero.

Actual result

2% CPU usage

❯ env time -v ./target/debug/examples/async-evdev
^CCommand terminated by signal 2
        Command being timed: "./target/debug/examples/async-evdev"
        User time (seconds): 0.20
        System time (seconds): 0.18
        Percent of CPU this job got: 2%

Addition context

The CPU usage is even higher than manually fetching. The following code consumes 0~1% CPU

use evdev::Device;

fn main() {
    let path = "/dev/input/by-id/<your-mouse>";
    let mut device = Device::open(path).unwrap();
    loop {
        let result = device.fetch_events();
    }
}

Profile from async code.
Tokio is polling in background.

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions