To get user CPU time on Linux for a thread we read /proc. Reading proc is slow (hence why this procedure is put under the method slow_thread_cpu_time(...)) and may lead noticeable spikes in case of contention for kernel resources.
Linux internally accounts for system and user time separately for threads/processes. Each clock id supports two modes: retrieve total CPU time (sys+user) or just user time. That way we can to two calls to clock_gettime and get total and user. From that we can get system (total - user).
I did some benchmarking and it is clear that the cost is minimal with two calls to clock_gettime vs reading proc (see attached plot). Moreover, and more importantly, the code will be substantially simplified.
Note: This is a feature that is only specific to Linux and not part of the POSIX standard, but used by glibc/musl (both of which OpenJDK supports) and is a Linux ABI. Additionally, the current implementation of slow_thread_cpu_time already have a Linux specific implementation it fits well to replace the call to read proc.
Linux internally accounts for system and user time separately for threads/processes. Each clock id supports two modes: retrieve total CPU time (sys+user) or just user time. That way we can to two calls to clock_gettime and get total and user. From that we can get system (total - user).
I did some benchmarking and it is clear that the cost is minimal with two calls to clock_gettime vs reading proc (see attached plot). Moreover, and more importantly, the code will be substantially simplified.
Note: This is a feature that is only specific to Linux and not part of the POSIX standard, but used by glibc/musl (both of which OpenJDK supports) and is a Linux ABI. Additionally, the current implementation of slow_thread_cpu_time already have a Linux specific implementation it fits well to replace the call to read proc.
- duplicates
-
JDK-8210452 getCurrentThreadUserTime is 30x-400x times slower then getCurrentThreadCpuTime
-
- Closed
-
- is blocked by
-
JDK-8372625 [Linux] Remove unnecessary logic for supports_fast_thread_cpu_time
-
- Resolved
-
- links to
-
Review(master)
openjdk/jdk/28556