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

List threads per process #943

Open
itamarst opened this issue Feb 26, 2023 · 6 comments
Open

List threads per process #943

itamarst opened this issue Feb 26, 2023 · 6 comments

Comments

@itamarst
Copy link

itamarst commented Feb 26, 2023

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
Copy link
Author

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

@GuillaumeGomez
Copy link
Owner

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. :)

@itamarst
Copy link
Author

It's already provided on Linux, the tasks field of the Process struct:

pub tasks: HashMap<Pid, Process>,

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
Copy link
Author

(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...)

@GuillaumeGomez
Copy link
Owner

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 GuillaumeGomez changed the title List threads per process on macOS List threads per process Jul 21, 2023
@leofidus
Copy link

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)

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

No branches or pull requests

3 participants