In-depth V8 Engine-Time Module Introduction

Keywords: C++ Javascript Windows

Accumulate your steps and travel thousands of miles. Start with the simplest.

This article introduces the time module in V8. Unlike libuv's rough update_loop_time method, V8 has a set of independent and complete classes responsible for managing time.

Located in src/base/platform/time.h, this class is an auxiliary module. First, let's look at the inheritance tree.

The inheritance relationship of the whole module is relatively simple. The Time and TimeTicks classes are commonly used. Let's introduce them one by one.

 

TimeConstants

This class is very direct, just defining some constants, such as 7 days a week, 24 hours a day, 60 minutes an hour and so on.

class TimeConstants {
 public:
  static constexpr int64_t kHoursPerDay = 24;
  static constexpr int64_t kMillisecondsPerSecond = 1000;
  static constexpr int64_t kMillisecondsPerDay =
      kMillisecondsPerSecond * 60 * 60 * kHoursPerDay;
  // ...
};

 

TimeDelta

This class provides a way to convert various units of time into microseconds.

class V8_BASE_EXPORT TimeDelta final {
 public:
  constexpr TimeDelta() : delta_(0) {}

  // Converts units of time to TimeDeltas.
  static constexpr TimeDelta FromDays(int days) {
    return TimeDelta(days * TimeConstants::kMicrosecondsPerDay);
  }
  // ...
}

The constant definition here comes from the TimeConstants class above.

 

TimeBase

There's nothing to say about this class. What's special about this class is that it's a template class that provides time series functionality for a given type.

template <class TimeClass>
class TimeBase : public TimeConstants {
 public:
  // ...

  int64_t ToInternalValue() const { return us_; }

  // ...

  static TimeClass FromInternalValue(int64_t us) { return TimeClass(us); }
 protected:
  explicit constexpr TimeBase(int64_t us) : us_(us) {}

  // Time value in a microsecond timebase.
  int64_t us_;
};

 

Time

The Time class is responsible for managing the timestamps generated by Date.now in JavaScript. It uses a lot of time stamps, so it's not explained here.

// -----------------------------------------------------------------------------
// Time
//
// This class represents an absolute point in time, internally represented as
// microseconds (s/1,000,000) since 00:00:00 UTC, January 1, 1970.

class V8_BASE_EXPORT Time final : public time_internal::TimeBase<Time> {
  // ...
};

The introduction of classes is very clear in the notes. It should be noted that the performance of these methods varies greatly from one operating system to another, and even some methods can only take effect in the specified operating system.

 

TimeTicks

// -----------------------------------------------------------------------------
// TimeTicks
//
// This class represents an abstract time that is most of the time incrementing
// for use in measuring time durations. It is internally represented in
// microseconds.  It can not be converted to a human-readable time, but is
// guaranteed not to decrease (if the user changes the computer clock,
// Time::Now() may actually decrease or jump).  But note that TimeTicks may
// "stand still", for example if the computer suspended.

class V8_BASE_EXPORT TimeTicks final : public time_internal::TimeBase<TimeTicks> {
  // ...
};

The annotations are quite elaborate.

The TimeTicks class is responsible for another timestamp. Brief Analysis of libuv Source-Acquisition of Precise Time Some of them have been introduced. For example, in windows, there are two kinds of counting API s, which return the time spent by the system "heartbeat" once and the number of "heartbeat" times respectively. Because the frequency is always fixed, the time can be timed according to the number of times returned each time.

The advantage of this event stamp over Time is that it guarantees that the number is always increasing and that it is not affected by external factors. So whether libuv sets polling start time or handles timer tasks, or V8 compiles and times JS code, this is used.

The last ThreadTicks will be ignored for the time being and will be used to explain.

This article first gives a brief introduction, and then goes into details about the implementation of two kinds of timestamps under different operating systems.

Posted by ghgarcia on Thu, 23 May 2019 14:33:25 -0700