1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3 #ident "$Id$"
4 /*======
5 This file is part of PerconaFT.
6 
7 
8 Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9 
10     PerconaFT is free software: you can redistribute it and/or modify
11     it under the terms of the GNU General Public License, version 2,
12     as published by the Free Software Foundation.
13 
14     PerconaFT is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18 
19     You should have received a copy of the GNU General Public License
20     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
21 
22 ----------------------------------------
23 
24     PerconaFT is free software: you can redistribute it and/or modify
25     it under the terms of the GNU Affero General Public License, version 3,
26     as published by the Free Software Foundation.
27 
28     PerconaFT is distributed in the hope that it will be useful,
29     but WITHOUT ANY WARRANTY; without even the implied warranty of
30     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31     GNU Affero General Public License for more details.
32 
33     You should have received a copy of the GNU Affero General Public License
34     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
35 
36 ----------------------------------------
37 
38    Licensed under the Apache License, Version 2.0 (the "License");
39    you may not use this file except in compliance with the License.
40    You may obtain a copy of the License at
41 
42        http://www.apache.org/licenses/LICENSE-2.0
43 
44    Unless required by applicable law or agreed to in writing, software
45    distributed under the License is distributed on an "AS IS" BASIS,
46    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
47    See the License for the specific language governing permissions and
48    limitations under the License.
49 ======= */
50 
51 #ident \
52     "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
53 
54 #pragma once
55 
56 // PORT2: #include "toku_config.h"
57 
58 #include <stdint.h>
59 #include <sys/time.h>
60 #include <time.h>
61 #if defined(__powerpc__) && defined(__linux__)
62 #include <sys/platform/ppc.h>
63 #endif
64 
65 #if 0
66 static inline float toku_tdiff (struct timeval *a, struct timeval *b) {
67     return (float)((a->tv_sec - b->tv_sec) + 1e-6 * (a->tv_usec - b->tv_usec));
68 }
69 // PORT2: temporary:
70 #define HAVE_CLOCK_REALTIME
71 #if !defined(HAVE_CLOCK_REALTIME)
72 // OS X does not have clock_gettime, we fake clockid_t for the interface, and we'll implement it with clock_get_time.
73 typedef int clockid_t;
74 // just something bogus, it doesn't matter, we just want to make sure we're
75 // only supporting this mode because we're not sure we can support other modes
76 // without a real clock_gettime()
77 #define CLOCK_REALTIME 0x01867234
78 #endif
79 int toku_clock_gettime(clockid_t clk_id, struct timespec *ts) __attribute__((__visibility__("default")));
80 #endif
81 
82 // *************** Performance timers ************************
83 // What do you really want from a performance timer:
84 //  (1) Can determine actual time of day from the performance time.
85 //  (2) Time goes forward, never backward.
86 //  (3) Same time on different processors (or even different machines).
87 //  (4) Time goes forward at a constant rate (doesn't get faster and slower)
88 //  (5) Portable.
89 //  (6) Getting the time is cheap.
90 // Unfortuately it seems tough to get Properties 1-5.  So we go for Property 6,,
91 // but we abstract it. We offer a type tokutime_t which can hold the time. This
92 // type can be subtracted to get a time difference. We can get the present time
93 // cheaply. We can convert this type to seconds (but that can be expensive). The
94 // implementation is to use RDTSC (hence we lose property 3: not portable).
95 // Recent machines have constant_tsc in which case we get property (4).
96 // Recent OSs on recent machines (that have RDTSCP) fix the per-processor clock
97 // skew, so we get property (3). We get property 2 with RDTSC (as long as
98 // there's not any skew). We don't even try to get propety 1, since we don't
99 // need it. The decision here is that these times are really accurate only on
100 // modern machines with modern OSs.
101 typedef uint64_t tokutime_t;  // Time type used in by tokutek timers.
102 
103 #if 0
104 // The value of tokutime_t is not specified here.
105 // It might be microseconds since 1/1/1970 (if gettimeofday() is
106 // used), or clock cycles since boot (if rdtsc is used).  Or something
107 // else.
108 // Two tokutime_t values can be subtracted to get a time difference.
109 // Use tokutime_to_seconds to that convert difference  to seconds.
110 // We want get_tokutime() to be fast, but don't care so much about tokutime_to_seconds();
111 //
112 // For accurate time calculations do the subtraction in the right order:
113 //   Right:  tokutime_to_seconds(t1-t2);
114 //   Wrong   tokutime_to_seconds(t1)-toku_time_to_seconds(t2);
115 // Doing it the wrong way is likely to result in loss of precision.
116 // A double can hold numbers up to about 53 bits.  RDTSC which uses about 33 bits every second, so that leaves
117 // 2^20 seconds from booting (about 2 weeks) before the RDTSC value cannot be represented accurately as a double.
118 //
119 double tokutime_to_seconds(tokutime_t)  __attribute__((__visibility__("default"))); // Convert tokutime to seconds.
120 
121 #endif
122 
123 // Get the value of tokutime for right now.  We want this to be fast, so we
124 // expose the implementation as RDTSC.
toku_time_now(void)125 static inline tokutime_t toku_time_now(void) {
126 #if defined(__x86_64__) || defined(__i386__)
127   uint32_t lo, hi;
128   __asm__ __volatile__("rdtsc" : "=a"(lo), "=d"(hi));
129   return (uint64_t)hi << 32 | lo;
130 #elif defined(__aarch64__)
131   uint64_t result;
132   __asm __volatile__("mrs %[rt], cntvct_el0" : [ rt ] "=r"(result));
133   return result;
134 #elif defined(__powerpc__)
135 #ifdef __linux__
136   return __ppc_get_timebase();
137 #elif defined(__FreeBSD__)
138   int64_t tbr;
139   asm volatile("mfspr %0, 268" : "=r"(tbr));
140   return tbr;
141 #endif
142 #else
143 #error No timer implementation for this platform
144 #endif
145 }
146 
toku_current_time_microsec(void)147 static inline uint64_t toku_current_time_microsec(void) {
148   struct timeval t;
149   gettimeofday(&t, NULL);
150   return t.tv_sec * (1UL * 1000 * 1000) + t.tv_usec;
151 }
152 
153 #if 0
154 // sleep microseconds
155 static inline void toku_sleep_microsec(uint64_t ms) {
156     struct timeval  t;
157 
158     t.tv_sec = ms / 1000000;
159     t.tv_usec = ms % 1000000;
160 
161     select(0, NULL, NULL, NULL, &t);
162 }
163 #endif
164 
165 /*
166   PORT: Usage of this file:
167 
168   uint64_t toku_current_time_microsec()   // uses gettimeoday
169       is used to track how much time various operations took (for example, lock
170       escalation). (TODO: it is not clear why these operations are tracked with
171       microsecond precision while others use nanoseconds)
172 
173   tokutime_t toku_time_now() // uses rdtsc
174       seems to be used for a very similar purpose. This has greater precision
175 
176   RocksDB environment provides Env::Default()->NowMicros() and NowNanos() which
177   should be adequate substitutes.
178 */
179