8366035: Simplify CPUTimeCounters::publish_gc_total_cpu_time

Reviewed-by: ayang, kbarrett
This commit is contained in:
Guanqiang Han 2025-08-26 08:35:52 +00:00 committed by Albert Mingkun Yang
parent 68abf76e90
commit 2ae3ea2ad9

View File

@ -75,15 +75,9 @@ void CPUTimeCounters::inc_gc_total_cpu_time(jlong diff) {
void CPUTimeCounters::publish_gc_total_cpu_time() {
CPUTimeCounters* instance = CPUTimeCounters::get_instance();
// Ensure that we are only incrementing atomically by using Atomic::cmpxchg
// to set the value to zero after we obtain the new CPU time difference.
jlong old_value;
jlong fetched_value = Atomic::load(&(instance->_gc_total_cpu_time_diff));
// Atomically fetch the current _gc_total_cpu_time_diff and reset it to zero.
jlong new_value = 0;
do {
old_value = fetched_value;
fetched_value = Atomic::cmpxchg(&(instance->_gc_total_cpu_time_diff), old_value, new_value);
} while (old_value != fetched_value);
jlong fetched_value = Atomic::xchg(&(instance->_gc_total_cpu_time_diff), new_value);
get_counter(CPUTimeGroups::CPUTimeType::gc_total)->inc(fetched_value);
}