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

Tasks aren't refreshed in System #1452

Open
stevenxxiu opened this issue Jan 11, 2025 · 5 comments
Open

Tasks aren't refreshed in System #1452

stevenxxiu opened this issue Jan 11, 2025 · 5 comments
Labels

Comments

@stevenxxiu
Copy link

Describe the bug

I'm using .tasks() to count the # of processes on Linux.

Tasks aren't refreshed in an existing instance of System. To get the correct value, I have to create a new instance of System every time.

  • OS: Arch Linux
  • sysinfo version: v0.33.1

To Reproduce

In the following code, what's printed should match, but they don't:

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

use sysinfo::{Pid, ProcessRefreshKind, ProcessesToUpdate, RefreshKind, System };

fn get_tasks(system: &System) -> HashSet<Pid> {
  let pid_to_process = system.processes();
  let mut task_pids: HashSet<Pid> = HashSet::new();
  for process in pid_to_process.values() {
    if let Some(tasks) = process.tasks() {
      task_pids.extend(tasks)
    }
  }
  task_pids
}

pub fn main() {
  let scheduler = thread::spawn(|| {
    let mut system = System::new_with_specifics(RefreshKind::nothing());

    loop {
      system.refresh_processes_specifics(ProcessesToUpdate::All, true, ProcessRefreshKind::nothing());

      let mut system_new = System::new_with_specifics(RefreshKind::nothing());
      system_new.refresh_processes_specifics(ProcessesToUpdate::All, true, ProcessRefreshKind::nothing());

      println!("{} {}", get_tasks(&system).len(), get_tasks(&system_new).len());
      thread::sleep(Duration::from_secs(1));
    }
  });
  scheduler.join().expect("Scheduler panicked");
}
@stevenxxiu stevenxxiu added the bug label Jan 11, 2025
@provrb
Copy link
Contributor

provrb commented Jan 11, 2025

The above code snippet produces the correct result on Kali WSL.

Perhaps try using ProcessRefreshKind::everything(). Does this produce a different result? If not, can you attach the output you're getting?

Below is the output I'm getting using your code snippet, where both numbers are matching.

Terminal Output (Kali Linux WSL):

19 19
19 19
19 19
19 19
19 19
19 19
19 19
19 19
19 19
19 19
19 19
19 19
19 19
19 19
^C

@GuillaumeGomez
Copy link
Owner

When you say "tasks aren't refreshed", do you mean you get the same count every time or do you mean the existing tasks information are not updated?

@stevenxxiu
Copy link
Author

The counts will change as programs are opened or closed. The 2 counts should match but they don't.

Using data from system_new in my code, I can get a total process count to match that of top, but not with system. This is what I mean when I say that it seems like "tasks aren't refreshed". The value for system is incorrect.

@GuillaumeGomez
Copy link
Owner

That doesn't help at all trying to understand you. ^^'

So the process list is not refreshed correctly, right?

@stevenxxiu
Copy link
Author

The list of PIDs are correct, as in this always returns true:

println!("{}", system.processes().keys().copied().collect::<HashSet<Pid>>() == system_new.processes().keys().copied().collect::<HashSet<Pid>>());

However .tasks() isn't being refreshed correctly, as shown in the first post. This gives different values, with system_new giving the correct values and system incorrect ones. All I need is this test case to pass. My first post uses .len() but this is a more proper test:

println!("{}", get_tasks(&system) == get_tasks(&system_new));

This will print true a few times then stay false.

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

3 participants