Code Monkey home page Code Monkey logo

Comments (7)

GuillaumeGomez avatar GuillaumeGomez commented on May 29, 2024

Hum, seems weird but I'll check this evening. Thanks!

from sysinfo.

vandenoever avatar vandenoever commented on May 29, 2024

get_process_list() can be easier and more efficient by changing Process::task from HashMap<pid_t, Process> to Vec<pid_t>.

Also the field Process::pid is duplication, the value is already in the HashMap key.

from sysinfo.

GuillaumeGomez avatar GuillaumeGomez commented on May 29, 2024

A duplication of 4 bytes, so not a big deal. Also, it's way more efficient to search into a HashMap than into a Vec.

from sysinfo.

GuillaumeGomez avatar GuillaumeGomez commented on May 29, 2024

Also, in your current code, you're not really checking process hierarchy. The tasks are threads, not processes. In addition, in the first step you put None but all the processes ahve at least the process 1 as parent. Try with the following code:

fn check() {
    fn check_process_hierarchy(parent: Option<pid_t>, processes: &HashMap<pid_t,Process>) {
        for (pid, process) in processes {
            assert_eq!(process.pid, *pid);
            if !parent.is_none() {
                assert_eq!(process.parent, parent);
            }
            check_process_hierarchy(Some(*pid), &process.tasks);
        }
    }

    let mut sysinfo = System::new();
    sysinfo.refresh_processes();
    check_process_hierarchy(None, sysinfo.get_process_list());
}

from sysinfo.

vandenoever avatar vandenoever commented on May 29, 2024

it's way more efficient to search into a HashMap than into a Vec.

Yes, so if you want to know the parent of a process, you look up the process in the HashMap of all processes and tasks and get the parent property. If you want to know properties of the tasks, you take their pids from the tasks vector.

all the processes ahve at least the process 1 as parent.

Process 1 (init system, e.g. systemd) and 2 (kthreadd) have no parent. So Option<pid_t> for parent is fine. All other processes and threads have parents.

I expected that list_process would give me only the top processes (1 and 2), but now I understand that it's all processes and that tasks are threads. The fact that tasks have type Process is confusing.

from sysinfo.

vandenoever avatar vandenoever commented on May 29, 2024

The adapted check passes.

from sysinfo.

GuillaumeGomez avatar GuillaumeGomez commented on May 29, 2024

It's a trick, but since tasks are just like processes, I used the same struct. I should maybe state it more strongly in the docs...

from sysinfo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.