A low overhead timer for x86 and AArch64

    • Type: New Feature
    • Resolution: Unresolved
    • Priority: Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • Server Programmability
    • None
    • 3
    • TBD
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      Today, the cheapest primitive to measure durations is the FastClockSource, which still requires running hundreds of instructions. We can introduce a new timing primitive that utilizes CPU timestamps to measure elapsed time. The following, for example, is how that can be done on x86 and AArch64:

      MONGO_COMPILER_ALWAYS_INLINE uint64_t getCPUTimestamp {
      #if defined(__x86_64__)
          unsigned int lo, hi;
          __asm__ __volatile__("rdtsc" : "=a"(lo), "=d"(hi));
          __asm__ __volatile__("lfence" ::: "memory");
          return ((uint64_t)hi << 32) | lo;
      #else
          asm volatile("isb" : : : "memory");
          uint64_t tsc;
          asm volatile("mrs %0, cntvct_el0" : "=r"(tsc));
          return tsc;
      #endif
          return 0;
      }
      

      On AArch64, there is also an instruction to read the CPU frequency. Considering there is no dynamic frequency scaling on Graviton processors, we can use that to convert the timestamps/cycles into durations (e.g. nanoseconds).

            Assignee:
            Mathias Stearn
            Reporter:
            Amirsaman Memaripour
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated: