Measuring thread cpu time (using bound threads)

Harendra Kumar harendra.kumar at gmail.com
Tue Dec 28 14:57:31 UTC 2021


Hi GHC devs,

I want to measure the CPU time spent in a particular Haskell thread across
thread yield points. I am assuming Haskell threads do not keep track of per
thread cpu time. Therefore, to measure it I have to use the thread cpu
clock (CLOCK_THREAD_CPUTIME_ID) provided by the OS (Linux). But Haskell
threads can keep jumping across multiple OS threads so the OS thread's CPU
time cannot be used reliably.

To solve that issue I thought I could use bound threads. From the
documentation it sounded like bound threads are exclusively bound to a
particular OS thread so we can get thread cpu time from the OS and use it.

However, if I print the thread id for a bound thread using "gettid" (OS
thread id on Linux) or using "pthread_self" (pthread id), both these thread
ids are changing for the same Haskell thread which is a bound thread. Is
that expected, or am I doing something wrong?

My questions:

* Are bound threads really bound to a particular OS thread or they can use
multiple OS threads for executing Haskell code, and only use a fixed OS
thread for FFI code?
* Is there a way to make this work? Can we obtain the thread cpu time
reliably across thread yield points? Is there a way to really bind Haskell
threads to OS threads?
* Is there a better or alternative way to get the thread CPU time?

Thanks,
Harendra
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-devs/attachments/20211228/4a2bfb21/attachment.html>


More information about the ghc-devs mailing list