Getting per-thread CPU time on *BSD

Harald Eilertsen harald+jdklists at volse.no
Mon Jun 16 17:53:54 UTC 2025


Hi,

On Mon, Jun 16, 2025 at 06:04:14PM +0200, Thomas Schatzl wrote:
> On 16.06.25 16:21, Harald Eilertsen wrote:
> > Hi again,
> >
> > I think it should be possible to get what you need via the
> > clock_gettime() call with the clock id returned by
> > pthread_getcpuclockid() for the target thread.
> >
> > https://urldefense.com/v3/__https://man.freebsd.org/cgi/man.cgi?query=clock_gettime__;!!ACWV5N9M2RV99hQ!Igm6XiYuRHoGdLiINNmnGjMmnpJb1YT9uiq9qJr4MmKjsNFtl7OTvu9ogF6zQHLj4oR_fedP1I3oOQDabcPm4dNT7XdIry9c$
> > https://urldefense.com/v3/__https://man.freebsd.org/cgi/man.cgi?query=pthread_getcpuclockid__;!!ACWV5N9M2RV99hQ!Igm6XiYuRHoGdLiINNmnGjMmnpJb1YT9uiq9qJr4MmKjsNFtl7OTvu9ogF6zQHLj4oR_fedP1I3oOQDabcPm4dNT7dKHsMci$
> >
> >  From what I can see these calls are also supported on OpenBSD and
> > NetBSD.
> >
>
> So this is somewhat similar to what linux does if both user and sys time are
> requested, getting a clock and then using that method?

Yes, it seems we could in practice use mostly the same code. It is
tempting to try to move it to os_posix.cpp, except that AIX does it in a
completely different way :)

> When getting user time only, Linux seems to parse
> /proc/self/task/<pid>/stats or so file, not sure if that is possible on BSD
> too.

It requires that procfs is mounted, which it may not be. (Currently the
BSD port relies on procfs for some other stuff as well, but we're hoping
to get away from it.)

> > If that's ok, I'm happy to try to implement it. Is there a test that can
> > be used to check the implementation?
> >
> jcmd <pid> Thread.print on a java process should print lines like:
>
> "G1 Refine#3" os_prio=0 cpu=22.28ms elapsed=90.86s tid=0x0000f56fbc21f000
> nid=0x75f4 runnable

Thanks, I'll look into it.

Take care!
Harald


More information about the bsd-port-dev mailing list