Getting a list or count of all running threads
Asked Answered
C

1

9

How can I do one of the following?

  • get a list of ThreadIDs all running forked threads (preferably with labels) from application code?
  • get a simple (maybe approximate; e.g. from the last major GC), count of running threads
  • get either of the above from gdb or similar

Things that don't work for me:

  • requiring a profiling build
  • maintaining some data structure to try to track this information myself

Things I thought might be promising:

Calcifuge answered 14/7, 2020 at 22:36 Comment(5)
May I ask why a tracking data structure is out of bounds? In principle, this seems like the most straightforward approach: replace calls to fork primitives with ones that update a global set or counter.Sabellian
@dfeuer: That’s what I’d do with code I controlled, but I don’t believe you can easily instrument code that you don’t control with your replacement forksAutoroute
Maybe the ThreadScope source code will be informative?Sabellian
I think it's a great question (clear, well-asked, clear effort to solve it yourself first, not a dupe of a thing we've seen a million times)... I just don't think there's a satisfying answer...Northeastwards
@Sabellian I'd like to be able to audit whether threads are getting created but not destroyed properly, or whether they are leaking from some other place (e.g. a bug in async or something). I suppose I could do the former with confidence if I just store ThreadIds in a set and have a thread that polls them periodically and only removes them when the status shows "terminated" or whateverCalcifuge
S
3

GHC 9.6 introduced listThreads :: IO [ThreadId]

From GHC User’s Guide:

GHC now provides a set of operations for introspecting on the threads of a program, GHC.Conc.listThreads, as well as operations for querying a thread’s label (GHC.Conc.threadLabel) and status (GHC.Conc.threadStatus).

Spelling answered 15/1, 2023 at 18:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.