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 || MOZ_SYSTEM_ICU
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 /* Initialize the resources associated with PRMJ_Now. */
41 #if defined(XP_WIN)
42 extern void PRMJ_NowInit();
43 #else
PRMJ_NowInit()44 inline void PRMJ_NowInit() {}
45 #endif
46 
47 /* Release the resources associated with PRMJ_Now; don't call PRMJ_Now again */
48 #ifdef XP_WIN
49 extern void PRMJ_NowShutdown();
50 #else
PRMJ_NowShutdown()51 inline void PRMJ_NowShutdown() {}
52 #endif
53 
54 #if !JS_HAS_INTL_API || MOZ_SYSTEM_ICU
55 /* Format a time value into a buffer. Same semantics as strftime() */
56 extern size_t PRMJ_FormatTime(char* buf, size_t buflen, const char* fmt,
57                               const PRMJTime* tm, int timeZoneYear,
58                               int offsetInSeconds);
59 #endif
60 
61 /**
62  * Requesting the number of cycles from the CPU.
63  *
64  * `rdtsc`, or Read TimeStamp Cycle, is an instruction provided by
65  * x86-compatible CPUs that lets processes request the number of
66  * cycles spent by the CPU executing instructions since the CPU was
67  * started. It may be used for performance monitoring, but you should
68  * be aware of the following limitations.
69  *
70  *
71  * 1. The value is *not* monotonic.
72  *
73  * The value is reset to 0 whenever a CPU is turned off (e.g. computer
74  * in full hibernation, single CPU going turned off). Moreover, on
75  * multi-core/multi-CPU architectures, the cycles of each core/CPU are
76  * generally not synchronized.  Therefore, is a process or thread is
77  * rescheduled to another core/CPU, the result of `rdtsc` may decrease
78  * arbitrarily.
79  *
80  * The only way to prevent this is to pin your thread to a particular
81  * CPU, which is generally not a good idea.
82  *
83  *
84  *
85  * 2. The value increases independently.
86  *
87  * The value may increase whenever the CPU executes an instruction,
88  * regardless of the process that has issued this
89  * instruction. Moreover, if a process or thread is rescheduled to
90  * another core/CPU, the result of `rdtsc` may increase arbitrarily.
91  *
92  * The only way to prevent this is to ensure that your thread is the
93  * sole owner of the CPU. See [1] for an example. This is also
94  * generally not a good idea.
95  *
96  *
97  *
98  * 3. The value does not measure time.
99  *
100  * On older architectures (pre-Pentium 4), there was no constant mapping
101  * between rdtsc and CPU time.
102  *
103  *
104  * 4. Instructions may be reordered.
105  *
106  * The CPU can reorder instructions. Also, rdtsc does not necessarily
107  * wait until all previous instructions have finished executing before
108  * reading the counter. Similarly, subsequent instructions may begin
109  * execution before the read operation is performed. If you use rdtsc
110  * for micro-benchmarking, you may end up measuring something else
111  * than what you expect. See [1] for a study of countermeasures.
112  *
113  *
114  * ** Performance
115  *
116  * According to unchecked sources on the web, the overhead of rdtsc is
117  * expected to be 150-200 cycles on old architectures, 6-50 on newer
118  * architectures. Agner's instruction tables [2] seem to confirm the latter
119  * results.
120  *
121  *
122  * [1]
123  * http://www.intel.com/content/dam/www/public/us/en/documents/white-papers/ia-32-ia-64-benchmark-code-execution-paper.pdf
124  * [2] http://www.agner.org/optimize/instruction_tables.pdf
125  */
126 
127 #define MOZ_HAVE_RDTSC 1
128 
129 #if defined(_WIN32) && (defined(_M_IX86) || defined(_M_AMD64))
130 
131 #  include <intrin.h>
ReadTimestampCounter(void)132 static __inline uint64_t ReadTimestampCounter(void) { return __rdtsc(); }
133 
134 #elif defined(__i386__)
135 
ReadTimestampCounter(void)136 static __inline__ uint64_t ReadTimestampCounter(void) {
137   uint64_t x;
138   __asm__ volatile(".byte 0x0f, 0x31" : "=A"(x));
139   return x;
140 }
141 
142 #elif defined(__x86_64__)
143 
ReadTimestampCounter(void)144 static __inline__ uint64_t ReadTimestampCounter(void) {
145   unsigned hi, lo;
146   __asm__ __volatile__("rdtsc" : "=a"(lo), "=d"(hi));
147   return ((uint64_t)lo) | (((uint64_t)hi) << 32);
148 }
149 
150 #else
151 
152 #  undef MOZ_HAVE_RDTSC
153 
154 #endif
155 
156 namespace js {
157 
ReallyNow()158 static inline mozilla::TimeStamp ReallyNow() {
159   return mozilla::TimeStamp::NowUnfuzzed();
160 }
161 
162 class MOZ_RAII AutoIncrementalTimer {
163   mozilla::TimeStamp startTime;
164   mozilla::TimeDuration& output;
165 
166  public:
167   AutoIncrementalTimer(const AutoIncrementalTimer&) = delete;
168   AutoIncrementalTimer& operator=(const AutoIncrementalTimer&) = delete;
169 
AutoIncrementalTimer(mozilla::TimeDuration & output_)170   explicit AutoIncrementalTimer(mozilla::TimeDuration& output_)
171       : output(output_) {
172     startTime = ReallyNow();
173   }
174 
~AutoIncrementalTimer()175   ~AutoIncrementalTimer() { output += ReallyNow() - startTime; }
176 };
177 
178 }  // namespace js
179 
180 #endif /* vm_Time_h */
181