| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#ifndef vm_Time_h |
| 8 |
#define vm_Time_h |
| 9 |
|
| 10 |
#include "mozilla/TimeStamp.h" |
| 11 |
|
| 12 |
#include <stddef.h> |
| 13 |
#include <stdint.h> |
| 14 |
|
| 15 |
#if !JS_HAS_INTL_API |
| 16 |
/* |
| 17 |
* Broken down form of 64 bit time value. |
| 18 |
*/ |
| 19 |
struct PRMJTime { |
| 20 |
int32_t tm_usec; /* microseconds of second (0-999999) */ |
| 21 |
int8_t tm_sec; /* seconds of minute (0-59) */ |
| 22 |
int8_t tm_min; /* minutes of hour (0-59) */ |
| 23 |
int8_t tm_hour; /* hour of day (0-23) */ |
| 24 |
int8_t tm_mday; /* day of month (1-31) */ |
| 25 |
int8_t tm_mon; /* month of year (0-11) */ |
| 26 |
int8_t tm_wday; /* 0=sunday, 1=monday, ... */ |
| 27 |
int32_t tm_year; /* absolute year, AD */ |
| 28 |
int16_t tm_yday; /* day of year (0 to 365) */ |
| 29 |
int8_t tm_isdst; /* non-zero if DST in effect */ |
| 30 |
}; |
| 31 |
#endif |
| 32 |
|
| 33 |
/* Some handy constants */ |
| 34 |
#define PRMJ_USEC_PER_SEC 1000000L |
| 35 |
#define PRMJ_USEC_PER_MSEC 1000L |
| 36 |
|
| 37 |
/* Return the current local time in micro-seconds */ |
| 38 |
extern int64_t PRMJ_Now(); |
| 39 |
|
| 40 |
#if !JS_HAS_INTL_API |
| 41 |
/* Format a time value into a buffer. Same semantics as strftime() */ |
| 42 |
extern size_t PRMJ_FormatTime(char* buf, size_t buflen, const char* fmt, |
| 43 |
const PRMJTime* tm, int timeZoneYear, |
| 44 |
int offsetInSeconds); |
| 45 |
#endif |
| 46 |
|
| 47 |
/** |
| 48 |
* Requesting the number of cycles from the CPU. |
| 49 |
* |
| 50 |
* `rdtsc`, or Read TimeStamp Cycle, is an instruction provided by |
| 51 |
* x86-compatible CPUs that lets processes request the number of |
| 52 |
* cycles spent by the CPU executing instructions since the CPU was |
| 53 |
* started. It may be used for performance monitoring, but you should |
| 54 |
* be aware of the following limitations. |
| 55 |
* |
| 56 |
* |
| 57 |
* 1. The value is *not* monotonic. |
| 58 |
* |
| 59 |
* The value is reset to 0 whenever a CPU is turned off (e.g. computer |
| 60 |
* in full hibernation, single CPU going turned off). Moreover, on |
| 61 |
* multi-core/multi-CPU architectures, the cycles of each core/CPU are |
| 62 |
* generally not synchronized. Therefore, is a process or thread is |
| 63 |
* rescheduled to another core/CPU, the result of `rdtsc` may decrease |
| 64 |
* arbitrarily. |
| 65 |
* |
| 66 |
* The only way to prevent this is to pin your thread to a particular |
| 67 |
* CPU, which is generally not a good idea. |
| 68 |
* |
| 69 |
* |
| 70 |
* |
| 71 |
* 2. The value increases independently. |
| 72 |
* |
| 73 |
* The value may increase whenever the CPU executes an instruction, |
| 74 |
* regardless of the process that has issued this |
| 75 |
* instruction. Moreover, if a process or thread is rescheduled to |
| 76 |
* another core/CPU, the result of `rdtsc` may increase arbitrarily. |
| 77 |
* |
| 78 |
* The only way to prevent this is to ensure that your thread is the |
| 79 |
* sole owner of the CPU. See [1] for an example. This is also |
| 80 |
* generally not a good idea. |
| 81 |
* |
| 82 |
* |
| 83 |
* |
| 84 |
* 3. The value does not measure time. |
| 85 |
* |
| 86 |
* On older architectures (pre-Pentium 4), there was no constant mapping |
| 87 |
* between rdtsc and CPU time. |
| 88 |
* |
| 89 |
* |
| 90 |
* 4. Instructions may be reordered. |
| 91 |
* |
| 92 |
* The CPU can reorder instructions. Also, rdtsc does not necessarily |
| 93 |
* wait until all previous instructions have finished executing before |
| 94 |
* reading the counter. Similarly, subsequent instructions may begin |
| 95 |
* execution before the read operation is performed. If you use rdtsc |
| 96 |
* for micro-benchmarking, you may end up measuring something else |
| 97 |
* than what you expect. See [1] for a study of countermeasures. |
| 98 |
* |
| 99 |
* |
| 100 |
* ** Performance |
| 101 |
* |
| 102 |
* According to unchecked sources on the web, the overhead of rdtsc is |
| 103 |
* expected to be 150-200 cycles on old architectures, 6-50 on newer |
| 104 |
* architectures. Agner's instruction tables [2] seem to confirm the latter |
| 105 |
* results. |
| 106 |
* |
| 107 |
* |
| 108 |
* [1] |
| 109 |
* http://www.intel.com/content/dam/www/public/us/en/documents/white-papers/ia-32-ia-64-benchmark-code-execution-paper.pdf |
| 110 |
* [2] http://www.agner.org/optimize/instruction_tables.pdf |
| 111 |
*/ |
| 112 |
|
| 113 |
#define MOZ_HAVE_RDTSC 1 |
| 114 |
|
| 115 |
#if defined(_WIN32) && (defined(_M_IX86) || defined(_M_AMD64)) |
| 116 |
|
| 117 |
# include <intrin.h> |
| 118 |
static __inline uint64_t ReadTimestampCounter(void) { return __rdtsc(); } |
| 119 |
|
| 120 |
#elif defined(__i386__) |
| 121 |
|
| 122 |
static __inline__ uint64_t ReadTimestampCounter(void) { |
| 123 |
uint64_t x; |
| 124 |
__asm__ volatile(".byte 0x0f, 0x31" : "=A"(x)); |
| 125 |
return x; |
| 126 |
} |
| 127 |
|
| 128 |
#elif defined(__x86_64__) |
| 129 |
|
| 130 |
static __inline__ uint64_t ReadTimestampCounter(void) { |
| 131 |
unsigned hi, lo; |
| 132 |
__asm__ __volatile__("rdtsc" : "=a"(lo), "=d"(hi)); |
| 133 |
return ((uint64_t)lo) | (((uint64_t)hi) << 32); |
| 134 |
} |
| 135 |
|
| 136 |
#else |
| 137 |
|
| 138 |
# undef MOZ_HAVE_RDTSC |
| 139 |
|
| 140 |
#endif |
| 141 |
|
| 142 |
namespace js { |
| 143 |
|
| 144 |
class MOZ_RAII AutoIncrementalTimer { |
| 145 |
mozilla::TimeStamp startTime; |
| 146 |
mozilla::TimeDuration& output; |
| 147 |
|
| 148 |
public: |
| 149 |
AutoIncrementalTimer(const AutoIncrementalTimer&) = delete; |
| 150 |
AutoIncrementalTimer& operator=(const AutoIncrementalTimer&) = delete; |
| 151 |
|
| 152 |
explicit AutoIncrementalTimer(mozilla::TimeDuration& output_) |
| 153 |
: output(output_) { |
| 154 |
startTime = mozilla::TimeStamp::Now(); |
| 155 |
} |
| 156 |
|
| 157 |
~AutoIncrementalTimer() { output += mozilla::TimeStamp::Now() - startTime; } |
| 158 |
}; |
| 159 |
|
| 160 |
} // namespace js |
| 161 |
|
| 162 |
#endif /* vm_Time_h */ |