15 //#ifdef HAVE_SYS_TIME_H
22 #if defined(__APPLE__)
23 #include <mach/mach_time.h>
26 static mach_timebase_info_data_t timebase_info;
30 #define MAP_ANONYMOUS MAP_ANON
33 #define TICKS_PER_SEC 1000000000LL
35 #if (defined(TARGET_X86) || defined(TARGET_AMD64)) && defined(__linux__)
40 unsigned int timer_count;
48 #define DECL_TLS_DATA TlsData *tls; tls = (TlsData *) TlsGetValue (tls_data); if (tls == NULL) { tls = (TlsData *) calloc (sizeof (TlsData), 1); TlsSetValue (tls_data, tls); }
49 #define TLS_INIT(x) x = TlsAlloc()
51 static __thread TlsData tls_data;
52 #define DECL_TLS_DATA TlsData *tls = &tls_data
55 static pthread_key_t tls_data;
56 #define DECL_TLS_DATA TlsData *tls; tls = (TlsData *) pthread_getspecific (tls_data); if (tls == NULL) { tls = (TlsData *) calloc (sizeof (TlsData), 1); pthread_setspecific (tls_data, tls); }
57 #define TLS_INIT(x) pthread_key_create(&x, NULL)
61 static CRITICAL_SECTION log_lock;
63 static pthread_mutex_t log_lock = PTHREAD_MUTEX_INITIALIZER;
66 static uint64_t time_inc = 0;
67 typedef uint64_t (*TimeFunc)(void);
69 static TimeFunc time_func;
74 #if defined(__APPLE__)
75 uint64_t time = mach_absolute_time ();
82 struct timespec tspec;
83 clock_gettime (CLOCK_MONOTONIC, &tspec);
84 return ((uint64_t)tspec.tv_sec * TICKS_PER_SEC + tspec.tv_nsec);
88 /* must be power of two */
92 fast_current_time (void)
95 if (tls->timer_count++ & (TIME_ADJ - 1)) {
96 tls->last_time += time_inc;
97 return tls->last_time;
99 tls->last_time = clock_time ();
100 return tls->last_time;
105 #define rdtsc(low,high) \
106 __asm__ __volatile__("rdtsc" : "=a" (low), "=d" (high))
109 safe_rdtsc (int *cpu)
111 unsigned int low, high;
112 int c1 = sched_getcpu ();
115 c2 = sched_getcpu ();
121 return (((uint64_t) high) << 32) + (uint64_t)low;
124 static double cpu_freq;
133 int cpu = sched_getcpu ();
138 if (!(cpuinfo = fopen ("/proc/cpuinfo", "r")))
140 while (fgets (buf, sizeof(buf), cpuinfo)) {
141 if (sscanf (buf, "cpu MHz : %f", &val) == 1) {
142 /*printf ("got mh: %f\n", val);*/
144 cpu_freq = val * 1000000;
146 if (strncmp (buf, "flags :", 5) == 0) {
147 if (strstr (buf, "constant_tsc")) {
149 /*printf ("have tsc\n");*/
154 return have_flag? have_freq: 0;
158 rdtsc_current_time (void)
161 if (tls->timer_count++ & (TIME_ADJ*8 - 1)) {
163 uint64_t tsc = safe_rdtsc (&cpu);
164 if (cpu != -1 && cpu == tls->last_cpu) {
165 int64_t diff = tsc - tls->last_rdtsc;
168 nsecs = (double)diff/cpu_freq;
169 //printf ("%llu cycles: %llu nsecs\n", diff, nsecs);
170 return tls->last_time + nsecs;
172 printf ("tsc went backwards\n");
175 //printf ("wrong cpu: %d\n", cpu);
178 tls->last_time = clock_time ();
179 tls->last_rdtsc = safe_rdtsc (&tls->last_cpu);
180 return tls->last_time;
183 #define have_rdtsc() 0
184 #define rdtsc_current_time fast_current_time
190 static uint64_t timer = 0;
195 utils_init (int fast_time)
199 InitializeCriticalSection (&log_lock);
201 #if defined (__APPLE__)
202 mach_timebase_info (&timebase_info);
206 time_func = null_time;
207 } else if (fast_time) {
211 timea = clock_time ();
212 timeb = clock_time ();
213 time_inc = (timeb - timea) / TIME_ADJ;
214 /*printf ("time inc: %llu, timea: %llu, timeb: %llu, diff: %llu\n", time_inc, timea, timeb, timec-timeb);*/
216 time_func = rdtsc_current_time;
218 time_func = fast_current_time;
220 time_func = clock_time;
231 alloc_buffer (int size)
235 ptr = VirtualAlloc (NULL, size, MEM_COMMIT, PAGE_READWRITE);
238 ptr = mmap (NULL, size, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
239 if (ptr == (void*)-1)
246 free_buffer (void *buf, int size)
249 VirtualFree (buf, 0, MEM_RELEASE);
259 EnterCriticalSection (&log_lock);
261 pthread_mutex_lock (&log_lock);
269 LeaveCriticalSection (&log_lock);
271 pthread_mutex_unlock (&log_lock);
276 encode_uleb128 (uint64_t value, uint8_t *buf, uint8_t **endbuf)
281 uint8_t b = value & 0x7f;
283 if (value != 0) /* more bytes to come */
292 encode_sleb128 (intptr_t value, uint8_t *buf, uint8_t **endbuf)
295 int negative = (value < 0);
296 unsigned int size = sizeof (intptr_t) * 8;
303 /* the following is unnecessary if the
304 * implementation of >>= uses an arithmetic rather
305 * than logical shift for a signed left operand
309 value |= - ((intptr_t)1 <<(size - 7));
310 /* sign bit of byte is second high order bit (0x40) */
311 if ((value == 0 && !(byte & 0x40)) ||
312 (value == -1 && (byte & 0x40)))
323 decode_uleb128 (uint8_t *buf, uint8_t **endbuf)
331 res |= (((uint64_t)(b & 0x7f)) << shift);
343 decode_sleb128 (uint8_t *buf, uint8_t **endbuf)
353 res = res | (((intptr_t)(b & 0x7f)) << shift);
356 if (shift < sizeof (intptr_t) * 8 && (b & 0x40))
357 res |= - ((intptr_t)1 << shift);
371 return (uintptr_t)GetCurrentThreadId ();
373 return (uintptr_t)pthread_self ();