RFR: 8273239: Standardize Ticks APIs return type [v2]

Paul Hohensee phh at openjdk.java.net
Wed Sep 8 16:58:09 UTC 2021


On Tue, 7 Sep 2021 17:27:00 GMT, Albert Mingkun Yang <ayang at openjdk.org> wrote:

>> Simple change on return types of Ticks API.
>> 
>> The call of `milliseconds()` in `spinYield.cpp` seems a bug to me, because the unit in the message is `usecs`. Therefore, I changed it to `microseconds()`.
>> 
>> Test: tier1
>
> Albert Mingkun Yang has updated the pull request incrementally with one additional commit since the last revision:
> 
>   template

I'm confused about two things. First, why is ElapsedCounter::frequency() ignored in this patch? Second, why are/were we potentially losing precision by converting everything to double internally and then converting the result to the target type?

If nanoseconds are the source of truth, we could do arithmetic in the target type. One could replace frequency() by 

uint64_t nanospertick() {
  static const uint64_t npt = NANOSECS_PER_SEC / (uint64_t)os::elapsed_frequency();
  return npt;
}

define conversion() to convert ticks to nanos and then do arithmetic in the target type,

template <T, typename TimeSource, const int nanos_per_unit>
inline T conversion(typename TimeSource::Type& value) {
  return (T)(value * TimeSource::nanospertick()) / (T)nanos_per_unit;
}

and then define seconds(), millseconds(), and nanoseconds() as

template<typename T>
static T seconds(Type value) {
  return conversion<T, FastUnorderedElapsedCounterSource, NANOSECS_PER_SEC>(value);
}

template<typename T>
static T milliseconds(Type value) {
  return conversion<T, FastUnorderedElapsedCounterSource, NANOS_PER_MILLISEC>(value);
}

template<typename T>
static T microseconds(Type value) {
  return conversion<T, FastUnorderedElapsedCounterSource, NANOS_PER_MICROSEC>(value);
}

template<typename T>
static T nanoseconds(Type value) {
  return conversion<T, FastUnorderedElapsedCounterSource, 1>(value);
}

This approach can lose a result tick fraction for all T other than double, but that currently happens on the final target type cast anyway.

-------------

PR: https://git.openjdk.java.net/jdk/pull/5332


More information about the hotspot-dev mailing list