Skip to content

Tracking Child Processes (tree of child processes) #2568

Open

Description

@CMCDragonkai

I've noticed that when using subprocess.run on my dask tasks, the workers still report low CPU usage, when the child process is in fact maxing out my CPU usage. Does Dask track the forked/child process CPU usage under the worker CPU usage? If not, I think it should since child processes can occur when interfacing dask workers against programs written in non-python.

Activity

  1. CMCDragonkai commented on Mar 18, 2019

    @CMCDragonkai
    Author

    The task stream isn't able to keep track of the child process either, so it just looks like the task is blocked and there's no progress report.

  2. mrocklin commented on Mar 18, 2019

    @mrocklin
    Member

    Does Dask track the forked/child process CPU usage under the worker CPU usage?

    No, it doesn't

    If not, I think it should since child processes can occur when interfacing dask workers against programs written in non-python.

    Do you have recommendations on how to do this well? Currently we use the psutil module. If there is something else we should be doing then suggestions would be welcome.

    The relevant code is here:

    cpu = self.proc.cpu_percent()

  3. CMCDragonkai commented on Mar 19, 2019

    @CMCDragonkai
    Author

    It looks like you would need to do something where you find the pid tree of the worker process and then acquire stats for each pid. This is transitive as child processes that produce more child processes would all come under the pid tree.

  4. changed the title [-]CPU Usage Child Processes[/-] [+]Tracking Child Processes (tree of child processes)[/+] on Mar 19, 2019
  5. CMCDragonkai commented on Mar 19, 2019

    @CMCDragonkai
    Author
  6. CMCDragonkai commented on Mar 19, 2019

    @CMCDragonkai
    Author

    This is natively supported in the psutil package: https://unix.stackexchange.com/a/339071/56970

    pid=2235; python3 -c "import psutil
    for c in psutil.Process($pid).children(True):
      print(c.pid)"
    

    The True ensures that it gets it recursively.

    https://psutil.readthedocs.io/en/latest/#psutil.Process.children

    Another way is to realise that process group on Linux will always encapsulate the entire process tree when launched by a shell. See: https://en.wikipedia.org/wiki/Process_group This means that on Linux, if you start each worker on its own process group. Then you can just query the PGID and get all processes part of that group. This is also useful for distribution of signals. However this may not be portable to Windows or other operating systems, whereas process tree via direct descendants may be more standard among all OS.

    I wrote a gist about this process groups: https://gist.github.com/CMCDragonkai/f58afb7e39fcc422097849b853caa140

  7. mrocklin commented on Mar 19, 2019

    @mrocklin
    Member
  8. CMCDragonkai commented on Mar 19, 2019

    @CMCDragonkai
    Author

    It should only affect performance for workers who are creating a tree of subprocesses. Otherwise it would just be checking one process. I think in this case it should be linear to the number total subprocesses that exist. I haven't checked psutil internals if it has to iterate over the entire process tree each time that is called, if it does, that would not be ideal. Then the ideal should be to use process groups. Windows actually does have process groups.

  9. mrocklin commented on Mar 19, 2019

    @mrocklin
    Member
  10. mrocklin commented on Mar 29, 2019

    @mrocklin
    Member

    @CMCDragonkai do you have any interest in implementing and testing this?

  11. CMCDragonkai commented on Apr 1, 2019

    @CMCDragonkai
    Author

    Yes, however it's in the queue of things to do. I don't mind if somebody else takes the lead on this.

  12. danpf commented on Sep 13, 2019

    @danpf
    Contributor

    #2390
    if you can find a way to make this use less cpu then I'm sure the developers would add it.

    currently it takes about 3-10% of the cpu to run these checks, so your options would be to only poll for cpu/memory a subset of the time, or to use a different module and figure it out in some other fashion.

    the problem lies in the fact that you have to hold onto the children and ask them for resource usage more than once. a single time will give you 0 usage.

  13. CMCDragonkai commented on Jan 6, 2020

    @CMCDragonkai
    Author

    If it is worth it for correctness, allow the user of the system to decide whether the overhead is worth it.

  14. fbriol commented on Nov 19, 2020

    @fbriol

    I tried to find a solution to follow the workers' child processes.

    To do this, the class handling the system monitoring must track the children; otherwise, the CPU usage is always 0 for all children (no previous time exists to calculate the usage). Initially, the children's set is empty. Then we have to add new children and remove the old ones. In short, it adds a little bit of calculation. In my test case, I didn't see any problem with the monitoring performance.

    --- 8,14 ----
      class SystemMonitor:
          def __init__(self, n=10000):
              self.proc = psutil.Process()
    +         self.children = set()
      
              self.time = deque(maxlen=n)
              self.cpu = deque(maxlen=n)
    *************** class SystemMonitor:
    *** 45,50 ****
    --- 46,60 ----
              with self.proc.oneshot():
                  cpu = self.proc.cpu_percent()
                  memory = self.proc.memory_info().rss
    +             children = set(self.proc.children(True)) - self.children
    +             if children:
    +                 self.children.update(children)
    +             self.children = set(item for item in self.children
    +                                 if item.is_running())
    +             for item in self.children:
    +                 with item.oneshot():
    +                     cpu += item.cpu_percent()
    +                     memory += item.memory_info().rss
              now = time()
      
              self.cpu.append(cpu)

    I can open a PR to insert this change.

  15. zbarr commented on Jul 26, 2023

    @zbarr

    Any traction on this? I'd pay the extra cpu penalty to be able to get these stats or even more granular stats.

  16. rasmus98 commented on Apr 2, 2024

    @rasmus98

    Ditto here, has any switch been added to allow this? I use dask primarily to taskfarm a fortran program where each execution takes minutes or more, so a few percent extra overhead paid per task would be no problem for me. On the other hand, knowing the CPU and memory headspace per node would be invaluable for optimizing my use of the nodes

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions