r-lib / ps

R package to query, list, manipulate system processes
https://ps.r-lib.org/
Other
78 stars 20 forks source link

macos status is not specific #31

Closed gaborcsardi closed 2 months ago

gaborcsardi commented 6 years ago

It is only running or stopped or zombie. To get a sleeping status, we would need to iterate over the threads and check their status.

gaborcsardi commented 6 years ago

This is how htop does it: https://github.com/hishamhm/htop/commit/5ee6715843902da1aa40782a9d00f41ca855a2ee

gaborcsardi commented 2 months ago

Nowadays this does not work because macOS does not let you read the status of most processes, unless you are the root user.

An alternative would be to call out to /bin/ps, which is SUID root:

❯ pp <- ps_pids()
❯ system.time(st <- system2(stdout = TRUE, stderr = FALSE, "/bin/ps", c("ax", "-o", "pid,stat", "-p", paste(pp, collapse = ","))))
   user  system elapsed 
  0.004   0.018   0.064 

❯ writeLines(head(st))
  PID STAT
    1 Ss  
 1125 Ss  
 1127 Ss  
 1129 Ss  
 1130 Ss  

We might do that in the future if there is a need for it, but for now I am going to close this issue.

gaborcsardi commented 2 months ago

Slightly faster for a single pid:

❯ system.time(system2(stdout = TRUE, stderr = FALSE, "/bin/ps", c("-o", "pid,stat", "-p", Sys.getpid())))
   user  system elapsed 
  0.001   0.003   0.021