Blame js/src/vm/Time.h

Packit f0b94e
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
Packit f0b94e
 * vim: set ts=8 sts=4 et sw=4 tw=99:
Packit f0b94e
 * This Source Code Form is subject to the terms of the Mozilla Public
Packit f0b94e
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit f0b94e
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Packit f0b94e
Packit f0b94e
#ifndef vm_Time_h
Packit f0b94e
#define vm_Time_h
Packit f0b94e
Packit f0b94e
#include <stddef.h>
Packit f0b94e
#include <stdint.h>
Packit f0b94e
Packit f0b94e
/*
Packit f0b94e
 * Broken down form of 64 bit time value.
Packit f0b94e
 */
Packit f0b94e
struct PRMJTime {
Packit f0b94e
  int32_t tm_usec; /* microseconds of second (0-999999) */
Packit f0b94e
  int8_t tm_sec;   /* seconds of minute (0-59) */
Packit f0b94e
  int8_t tm_min;   /* minutes of hour (0-59) */
Packit f0b94e
  int8_t tm_hour;  /* hour of day (0-23) */
Packit f0b94e
  int8_t tm_mday;  /* day of month (1-31) */
Packit f0b94e
  int8_t tm_mon;   /* month of year (0-11) */
Packit f0b94e
  int8_t tm_wday;  /* 0=sunday, 1=monday, ... */
Packit f0b94e
  int32_t tm_year; /* absolute year, AD */
Packit f0b94e
  int16_t tm_yday; /* day of year (0 to 365) */
Packit f0b94e
  int8_t tm_isdst; /* non-zero if DST in effect */
Packit f0b94e
};
Packit f0b94e
Packit f0b94e
/* Some handy constants */
Packit f0b94e
#define PRMJ_USEC_PER_SEC 1000000L
Packit f0b94e
#define PRMJ_USEC_PER_MSEC 1000L
Packit f0b94e
Packit f0b94e
/* Return the current local time in micro-seconds */
Packit f0b94e
extern int64_t PRMJ_Now();
Packit f0b94e
Packit f0b94e
/* Initialize the resources associated with PRMJ_Now. */
Packit f0b94e
#if defined(XP_WIN)
Packit f0b94e
extern void PRMJ_NowInit();
Packit f0b94e
#else
Packit f0b94e
inline void PRMJ_NowInit() {}
Packit f0b94e
#endif
Packit f0b94e
Packit f0b94e
/* Release the resources associated with PRMJ_Now; don't call PRMJ_Now again */
Packit f0b94e
#ifdef XP_WIN
Packit f0b94e
extern void PRMJ_NowShutdown();
Packit f0b94e
#else
Packit f0b94e
inline void PRMJ_NowShutdown() {}
Packit f0b94e
#endif
Packit f0b94e
Packit f0b94e
/* Format a time value into a buffer. Same semantics as strftime() */
Packit f0b94e
extern size_t PRMJ_FormatTime(char* buf, int buflen, const char* fmt,
Packit 487334
                              const PRMJTime* tm, int timeZoneYear,
Packit f0b94e
                              int offsetInSeconds);
Packit f0b94e
Packit f0b94e
/**
Packit f0b94e
 * Requesting the number of cycles from the CPU.
Packit f0b94e
 *
Packit f0b94e
 * `rdtsc`, or Read TimeStamp Cycle, is an instruction provided by
Packit f0b94e
 * x86-compatible CPUs that lets processes request the number of
Packit f0b94e
 * cycles spent by the CPU executing instructions since the CPU was
Packit f0b94e
 * started. It may be used for performance monitoring, but you should
Packit f0b94e
 * be aware of the following limitations.
Packit f0b94e
 *
Packit f0b94e
 *
Packit f0b94e
 * 1. The value is *not* monotonic.
Packit f0b94e
 *
Packit f0b94e
 * The value is reset to 0 whenever a CPU is turned off (e.g. computer
Packit f0b94e
 * in full hibernation, single CPU going turned off). Moreover, on
Packit f0b94e
 * multi-core/multi-CPU architectures, the cycles of each core/CPU are
Packit f0b94e
 * generally not synchronized.  Therefore, is a process or thread is
Packit f0b94e
 * rescheduled to another core/CPU, the result of `rdtsc` may decrease
Packit f0b94e
 * arbitrarily.
Packit f0b94e
 *
Packit f0b94e
 * The only way to prevent this is to pin your thread to a particular
Packit f0b94e
 * CPU, which is generally not a good idea.
Packit f0b94e
 *
Packit f0b94e
 *
Packit f0b94e
 *
Packit f0b94e
 * 2. The value increases independently.
Packit f0b94e
 *
Packit f0b94e
 * The value may increase whenever the CPU executes an instruction,
Packit f0b94e
 * regardless of the process that has issued this
Packit f0b94e
 * instruction. Moreover, if a process or thread is rescheduled to
Packit f0b94e
 * another core/CPU, the result of `rdtsc` may increase arbitrarily.
Packit f0b94e
 *
Packit f0b94e
 * The only way to prevent this is to ensure that your thread is the
Packit f0b94e
 * sole owner of the CPU. See [1] for an example. This is also
Packit f0b94e
 * generally not a good idea.
Packit f0b94e
 *
Packit f0b94e
 *
Packit f0b94e
 *
Packit f0b94e
 * 3. The value does not measure time.
Packit f0b94e
 *
Packit f0b94e
 * On older architectures (pre-Pentium 4), there was no constant mapping
Packit f0b94e
 * between rdtsc and CPU time.
Packit f0b94e
 *
Packit f0b94e
 *
Packit f0b94e
 * 4. Instructions may be reordered.
Packit f0b94e
 *
Packit f0b94e
 * The CPU can reorder instructions. Also, rdtsc does not necessarily
Packit f0b94e
 * wait until all previous instructions have finished executing before
Packit f0b94e
 * reading the counter. Similarly, subsequent instructions may begin
Packit f0b94e
 * execution before the read operation is performed. If you use rdtsc
Packit f0b94e
 * for micro-benchmarking, you may end up measuring something else
Packit f0b94e
 * than what you expect. See [1] for a study of countermeasures.
Packit f0b94e
 *
Packit f0b94e
 *
Packit f0b94e
 * ** Performance
Packit f0b94e
 *
Packit f0b94e
 * According to unchecked sources on the web, the overhead of rdtsc is
Packit f0b94e
 * expected to be 150-200 cycles on old architectures, 6-50 on newer
Packit f0b94e
 * architectures. Agner's instruction tables [2] seem to confirm the latter
Packit f0b94e
 * results.
Packit f0b94e
 *
Packit f0b94e
 *
Packit f0b94e
 * [1]
Packit f0b94e
 * http://www.intel.com/content/dam/www/public/us/en/documents/white-papers/ia-32-ia-64-benchmark-code-execution-paper.pdf
Packit f0b94e
 * [2] http://www.agner.org/optimize/instruction_tables.pdf
Packit f0b94e
 */
Packit f0b94e
Packit f0b94e
#define MOZ_HAVE_RDTSC 1
Packit f0b94e
Packit f0b94e
#if defined(_WIN32)
Packit f0b94e
Packit f0b94e
#include <intrin.h>
Packit f0b94e
static __inline uint64_t ReadTimestampCounter(void) { return __rdtsc(); }
Packit f0b94e
Packit f0b94e
#elif defined(__i386__)
Packit f0b94e
Packit f0b94e
static __inline__ uint64_t ReadTimestampCounter(void) {
Packit f0b94e
  uint64_t x;
Packit f0b94e
  __asm__ volatile(".byte 0x0f, 0x31" : "=A"(x));
Packit f0b94e
  return x;
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
#elif defined(__x86_64__)
Packit f0b94e
Packit f0b94e
static __inline__ uint64_t ReadTimestampCounter(void) {
Packit f0b94e
  unsigned hi, lo;
Packit f0b94e
  __asm__ __volatile__("rdtsc" : "=a"(lo), "=d"(hi));
Packit f0b94e
  return ((uint64_t)lo) | (((uint64_t)hi) << 32);
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
#else
Packit f0b94e
Packit f0b94e
#undef MOZ_HAVE_RDTSC
Packit f0b94e
Packit f0b94e
#endif
Packit f0b94e
Packit f0b94e
#endif /* vm_Time_h */