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

System::refresh_processes() doesn't update CPU #1351

Open
M1loseph opened this issue Sep 16, 2024 · 2 comments
Open

System::refresh_processes() doesn't update CPU #1351

M1loseph opened this issue Sep 16, 2024 · 2 comments

Comments

@M1loseph
Copy link

M1loseph commented Sep 16, 2024

My setup

OS: WSL, Ubuntu 24.04 LTS
Architecture: x86_64
Rust: 1.81.0
Sysinfo: 0.31.4

Issue

When I call System::refresh_processes_specifics(ProcessesToUpdate::Some(&[pid], ProcessRefreshKind::new().with_cpu().with_memory()) the cpu usage of provided process is not updated. Calling Process::cpu_usage() always returns the same value.

Expected behavious

CPU usage is updated and can be read from Process struct.

Actuall result

Only memory gets updated, CPU value always stays the same.

What is interesting, when I replaced ProcessesToUpdate::Some(...) with ProcessesToUpdate::All, all of a sudden it is updating correctly.

Code to reproduce issue

use std::{thread, time::Duration};

use sysinfo::{get_current_pid, ProcessRefreshKind, System};


fn main() {
    let mut system = System::new_all();
    loop {
        thread::sleep(Duration::from_secs(1));
        let pid = get_current_pid().unwrap();
        system.refresh_processes_specifics(
            sysinfo::ProcessesToUpdate::Some(&[pid]),
            ProcessRefreshKind::everything()
        );
        let process = system.process(pid).unwrap();

        println!("cpu={}, mem={}", process.cpu_usage(), process.memory());
    }
}

image

top -p <pid> returns CPU usage from 0.0 to 0.3, to the value should fluctuate a bit, but it always remains zero.

@M1loseph M1loseph added the bug label Sep 16, 2024
@M1loseph
Copy link
Author

M1loseph commented Sep 17, 2024

I even added some shitty fibbonaci function to make sure that CPU is heavily loaded - even when top returns 90% CPU utilization, I still get 0 printed to the console :(

use std::{thread, time::Duration};

use sysinfo::{get_current_pid, ProcessRefreshKind, System};


fn fib(n: u64) -> u64 {
    if n == 0 || n == 1 {
        return 1;
    }
    return fib(n - 1) + fib(n - 2);
}

fn main() {
    thread::spawn(|| {
        let calculation = fib(100);
        println!("{calculation}");
    });
    let mut system = System::new_all();
    loop {
        thread::sleep(Duration::from_secs(1));
        let pid = get_current_pid().unwrap();
        system.refresh_processes_specifics(
            sysinfo::ProcessesToUpdate::Some(&[pid]),
            ProcessRefreshKind::everything()
        );
        let process = system.process(pid).unwrap();

        println!("cpu={}, mem={}", process.cpu_usage(), process.memory());
    }
}

@GuillaumeGomez
Copy link
Owner

I'll try to take a look in the next days. Could be interesting if you checked what path the code is entering into on your side as I don't have your setup.

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