sysinfo icon indicating copy to clipboard operation
sysinfo copied to clipboard

List threads per process

Open itamarst opened this issue 3 years ago • 6 comments

Thanks for a great package! I am currently trying to add macOS support to Linux-only application using it, and discovered it's not possible to get the threads for a process on macOS.

This is possible to do, e.g. here's a Python psutil example:

>>> import psutil
>>> import threading
>>> import time
>>> threading.Thread(target=lambda: time.sleep(1000000)).start()
>>> p = psutil.Process()
>>> p.threads(
>>> p.threads() 
[pthread(id=1, user_time=0.098023, system_time=0.035946), pthread(id=2, user_time=0.000124, system_time=3.4e-05)]

The relevant code in psutil can be seen here: https://github.com/giampaolo/psutil/blob/bea3cf2d16899251b4b5f6b2609db9881645ea2d/psutil/_psutil_osx.c#L888

The Python psutil library does not include some info that I would like (thread scheduler status) but I suspect that is still obtainable. In particular, the thread_basic_info struct that it uses appears to have a run_state flag, based on some googling (https://developer.apple.com/documentation/kernel/thread_basic_info_t). So it seems like sysinfo could include much of the info it already includes for each thread on Linux.

itamarst avatar Feb 26 '23 22:02 itamarst

If this is something you'd be happy to accept, I can try to submit a PR.

itamarst avatar Feb 26 '23 22:02 itamarst

The big question is: is it possible to get this information on all sysinfo supported systems? If it's possible for mac, the code should be more or less the same for freebsd, and I assume linux shouldn't be too complicated either. But the big unknown is windows. :)

GuillaumeGomez avatar Feb 26 '23 22:02 GuillaumeGomez

It's already provided on Linux, the tasks field of the Process struct: https://github.com/GuillaumeGomez/sysinfo/blob/d9746a2280f42833f0ed984ede2d944f8bcd3aca/src/linux/process.rs#L85

psutil has code for FreeBSD (https://github.com/giampaolo/psutil/blob/f716afc81d4c9ded08b23d376612e1491d3ea5da/psutil/arch/freebsd/proc.c#L278) and Windows (https://github.com/giampaolo/psutil/blob/bea3cf2d16899251b4b5f6b2609db9881645ea2d/psutil/_psutil_windows.c#L689) so I assume it's possible there as well, but I personally only care about macOS this month.

itamarst avatar Feb 27 '23 00:02 itamarst

(Of course, the presumption that threads are the same as processes is kinda Linux-specific, so if you wanted a consistent thread information API it might look somewhat different. And e.g. scheduler status is definitely available on Linux and macOS, but not sure about FreeBSD and Windows...)

itamarst avatar Feb 27 '23 02:02 itamarst

If available on macOS, it'll be very likely available on freebsd as well. For windows, big unknown as usual.

And yes, if we provide it for all systems, at this point we can directly provide a unified API for it.

GuillaumeGomez avatar Feb 27 '23 09:02 GuillaumeGomez

Getting the threads of a specific process is easy in Windows. You just call CreateToolhelp32Snapshot to get a snapshot handle, then you can enumerate the threads with Thread32First and Thread32Next.

I have a working implementation in a library I never finished writing (the windows part works though, since that's what I actually needed)

leofidus avatar Dec 21 '23 00:12 leofidus