From thomas.schatzl at oracle.com Fri Jun 13 13:06:23 2025 From: thomas.schatzl at oracle.com (Thomas Schatzl) Date: Fri, 13 Jun 2025 15:06:23 +0200 Subject: Getting per-thread CPU time on *BSD Message-ID: Hi all, I am currently looking into making os::get_thread_cpu_time() usable on *BSD because we in the gc team would like to use this functionality in the future. The idea is to get an accurate measurement of how much CPU time the GC threads take to do heap sizing. For this reason, for some time there is that os::get_thread_cpu_time(Thread*) that returns the user+sys time the given thread (id) took since creation API that is unimplemented on *BSD except OSX. Is there something on *BSD that would satisfy the requirements? I only could find getrusage(RUSAGE_THREAD), that only gets this information for the calling thread, which is not enough and too complicated/expensive to start the threads themselves. Also it only seems to be supported on OpenBSD and FreeBSD. The method seems also required for JVMTI, JFR, some management API and some collectors (Shenandoah uses it), so these are already not working optimally or not at all on *BSD. This would add G1, the default collector, not working as well (or not optimally). Thanks, Thomas (I'm aware that this mailing list might be dead, the last message was from 2019, but maybe....) From harald+jdklists at volse.no Mon Jun 16 07:51:00 2025 From: harald+jdklists at volse.no (Harald Eilertsen) Date: Mon, 16 Jun 2025 09:51:00 +0200 Subject: Getting per-thread CPU time on *BSD In-Reply-To: References: Message-ID: Hi Thomas, On Fri, Jun 13, 2025 at 03:06:23PM +0200, Thomas Schatzl wrote: > For this reason, for some time there is that > os::get_thread_cpu_time(Thread*) that returns the user+sys time the given > thread (id) took since creation API that is unimplemented on *BSD except > OSX. > > Is there something on *BSD that would satisfy the requirements? I'll have to research that a bit before giving you an answer, unless there's someone else slumbering on the list who can help. Take care! Harald From harald+jdklists at volse.no Mon Jun 16 14:21:15 2025 From: harald+jdklists at volse.no (Harald Eilertsen) Date: Mon, 16 Jun 2025 16:21:15 +0200 Subject: Getting per-thread CPU time on *BSD In-Reply-To: References: Message-ID: 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://man.freebsd.org/cgi/man.cgi?query=clock_gettime https://man.freebsd.org/cgi/man.cgi?query=pthread_getcpuclockid >From what I can see these calls are also supported on OpenBSD and NetBSD. One note, though: These calls return (afaict) the acumulated sys+user time of the thread in one timespec struct, not as separate sys+user times. If that's ok, I'm happy to try to implement it. Is there a test that can be used to check the implementation? Take Care! Harald From thomas.schatzl at oracle.com Mon Jun 16 16:04:14 2025 From: thomas.schatzl at oracle.com (Thomas Schatzl) Date: Mon, 16 Jun 2025 18:04:14 +0200 Subject: Getting per-thread CPU time on *BSD In-Reply-To: References: Message-ID: <97c3f636-5fed-4fd3-a104-afeda353ec22@oracle.com> Hi, thanks for looking into this. 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? (I did not look into the linux code in detail). When getting user time only, Linux seems to parse /proc/self/task//stats or so file, not sure if that is possible on BSD too. > One note, though: These calls return (afaict) the acumulated sys+user > time of the thread in one timespec struct, not as separate sys+user > times. > That's fine for many cases, at least for GC we want both. Some users (JFR) seem to make the distinction, but I guess just returning the same value is better than the current implementation (crashing). > 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 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 The value after "cpu=" is derived from os::thread_cpu_time() (see Thread::print_on in thread.cpp) which seems to be unimplemented (just fail the VM) on *BSD. I.e. os_bsd.cpp, search for "Unimplemented()" to find that above method. Would be great to have support for that on non-OSX too so that we can rely on that. Thanks, Thomas From harald+jdklists at volse.no Mon Jun 16 17:53:54 2025 From: harald+jdklists at volse.no (Harald Eilertsen) Date: Mon, 16 Jun 2025 19:53:54 +0200 Subject: Getting per-thread CPU time on *BSD In-Reply-To: <97c3f636-5fed-4fd3-a104-afeda353ec22@oracle.com> References: <97c3f636-5fed-4fd3-a104-afeda353ec22@oracle.com> Message-ID: 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//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 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 From harald+jdklists at volse.no Sat Jun 21 12:03:02 2025 From: harald+jdklists at volse.no (Harald Eilertsen) Date: Sat, 21 Jun 2025 14:03:02 +0200 Subject: Getting per-thread CPU time on *BSD In-Reply-To: References: <97c3f636-5fed-4fd3-a104-afeda353ec22@oracle.com> Message-ID: Hi, A quick update on my part. I have figured out how to get both user time and user+sys time on FreeBSD. https://git.anduin.net/haraldei/bsd-thread-time-example This is quite similar to the OpenBSD code by Kurt Miller that was already in the BSD port tree. I have a local patch based on that which replace the existing FreeBSD code with combined code for OpenBSD and FreeBSD. I will need help from someone to add support for NetBSD, though. I'm a bit unsure about what to do next however. Do you want me to submit the changes to the OpenJDK mainline repo? It won't in itself fix BSD support for OpenJDK, but could be one of the many steps on the way. In any case, it should be safe to go on with whatever needs you have for this information, we are able to provide it on the BSD's as well. H! From thomas.schatzl at oracle.com Mon Jun 23 07:46:49 2025 From: thomas.schatzl at oracle.com (Thomas Schatzl) Date: Mon, 23 Jun 2025 09:46:49 +0200 Subject: Getting per-thread CPU time on *BSD In-Reply-To: References: <97c3f636-5fed-4fd3-a104-afeda353ec22@oracle.com> Message-ID: Hi Harald, thanks for your effort! On 21.06.25 14:03, Harald Eilertsen wrote: > Hi, > > A quick update on my part. > > I have figured out how to get both user time and user+sys time on > FreeBSD. > > https://urldefense.com/v3/__https://git.anduin.net/haraldei/bsd-thread-time-example__;!!ACWV5N9M2RV99hQ!KBbaClb29ZBzy6EdTYhJCvZr14QJva2hOK77xk4iHj9UsZiM0q2WpJzM2-Yy0rIrpBA6ueo4LvMJJ90zr-K7IDN-PeWZ-p-s$ > > This is quite similar to the OpenBSD code by Kurt Miller that was > already in the BSD port tree. I have a local patch based on that I did not know somebody maintained the BSD port outside of mainline. That may make the issue more complicated. > which replace the existing FreeBSD code with combined code for OpenBSD > and FreeBSD. I will need help from someone to add support for NetBSD, > though. > > I'm a bit unsure about what to do next however. Do you want me to submit > the changes to the OpenJDK mainline repo? It won't in itself fix BSD > support for OpenJDK, but could be one of the many steps on the way. I would go that way if possible and try to keep mainline up to date. However if mainline does not build/work at all, then this effort would probably just contribute to more untested code. Unfortunately I do not know the exact state of the BSD ports. > > In any case, it should be safe to go on with whatever needs you have for > this information, we are able to provide it on the BSD's as well. > Thanks again. Thomas