2 * utils.c: log profiler and reporter utils
4 * We have here the minimal needed portability functions: we can't depend
5 * on the ones provided by the runtime, since they are internal and,
6 * especially mprof-report is an external program.
7 * Note also that we don't take a glib/eglib dependency here for mostly
8 * the same reason (but also because we need tight control in the profiler
9 * over memory allocation, which needs to work with the world stopped).
12 * Paolo Molaro (lupus@ximian.com)
14 * Copyright 2010 Novell, Inc (http://www.novell.com)
29 #ifdef HAVE_SYS_TIME_H
36 #if defined(__APPLE__)
37 #include <mach/mach_time.h>
40 static mach_timebase_info_data_t timebase_info;
44 #define MAP_ANONYMOUS MAP_ANON
47 #define TICKS_PER_SEC 1000000000LL
49 #if (defined(TARGET_X86) || defined(TARGET_AMD64)) && defined(__linux__)
54 unsigned int timer_count;
62 #define DECL_TLS_DATA TlsData *tls; tls = (TlsData *) TlsGetValue (tls_data); if (tls == NULL) { tls = (TlsData *) calloc (sizeof (TlsData), 1); TlsSetValue (tls_data, tls); }
63 #define TLS_INIT(x) x = TlsAlloc()
65 static __thread TlsData tls_data;
66 #define DECL_TLS_DATA TlsData *tls = &tls_data
69 static pthread_key_t tls_data;
70 #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); }
71 #define TLS_INIT(x) pthread_key_create(&x, NULL)
75 static CRITICAL_SECTION log_lock;
77 static pthread_mutex_t log_lock = PTHREAD_MUTEX_INITIALIZER;
80 static int timer_overhead = 0;
81 static uint64_t time_inc = 0;
82 typedef uint64_t (*TimeFunc)(void);
84 static TimeFunc time_func;
89 #if defined(__APPLE__)
90 uint64_t time = mach_absolute_time ();
97 struct timespec tspec;
98 clock_gettime (CLOCK_MONOTONIC, &tspec);
99 return ((uint64_t)tspec.tv_sec * TICKS_PER_SEC + tspec.tv_nsec);
103 /* must be power of two */
107 fast_current_time (void)
110 if (tls->timer_count++ & (TIME_ADJ - 1)) {
111 tls->last_time += time_inc;
112 return tls->last_time;
114 tls->last_time = clock_time ();
115 return tls->last_time;
120 #define rdtsc(low,high) \
121 __asm__ __volatile__("rdtsc" : "=a" (low), "=d" (high))
124 safe_rdtsc (int *cpu)
126 unsigned int low, high;
127 int c1 = sched_getcpu ();
130 c2 = sched_getcpu ();
136 return (((uint64_t) high) << 32) + (uint64_t)low;
139 static double cpu_freq;
148 int cpu = sched_getcpu ();
153 if (!(cpuinfo = fopen ("/proc/cpuinfo", "r")))
155 while (fgets (buf, sizeof(buf), cpuinfo)) {
156 if (sscanf (buf, "cpu MHz : %f", &val) == 1) {
157 /*printf ("got mh: %f\n", val);*/
159 cpu_freq = val * 1000000;
161 if (strncmp (buf, "flags :", 5) == 0) {
162 if (strstr (buf, "constant_tsc")) {
164 /*printf ("have tsc\n");*/
169 return have_flag? have_freq: 0;
173 rdtsc_current_time (void)
176 if (tls->timer_count++ & (TIME_ADJ*8 - 1)) {
178 uint64_t tsc = safe_rdtsc (&cpu);
179 if (cpu != -1 && cpu == tls->last_cpu) {
180 int64_t diff = tsc - tls->last_rdtsc;
183 nsecs = (double)diff/cpu_freq;
184 //printf ("%llu cycles: %llu nsecs\n", diff, nsecs);
185 return tls->last_time + nsecs;
187 printf ("tsc went backwards\n");
190 //printf ("wrong cpu: %d\n", cpu);
193 tls->last_time = clock_time ();
194 tls->last_rdtsc = safe_rdtsc (&tls->last_cpu);
195 return tls->last_time;
198 #define have_rdtsc() 0
199 #define rdtsc_current_time fast_current_time
205 static uint64_t timer = 0;
210 utils_init (int fast_time)
213 uint64_t time_start, time_end;
216 InitializeCriticalSection (&log_lock);
218 #if defined (__APPLE__)
219 mach_timebase_info (&timebase_info);
223 time_func = null_time;
224 } else if (fast_time) {
228 timea = clock_time ();
229 timeb = clock_time ();
230 time_inc = (timeb - timea) / TIME_ADJ;
231 /*printf ("time inc: %llu, timea: %llu, timeb: %llu, diff: %llu\n", time_inc, timea, timeb, timec-timeb);*/
233 time_func = rdtsc_current_time;
235 time_func = fast_current_time;
237 time_func = clock_time;
239 time_start = time_func ();
240 for (i = 0; i < 256; ++i)
242 time_end = time_func ();
243 timer_overhead = (time_end - time_start) / 256;
247 get_timer_overhead (void)
249 return timer_overhead;
259 alloc_buffer (int size)
263 ptr = VirtualAlloc (NULL, size, MEM_COMMIT, PAGE_READWRITE);
266 ptr = mmap (NULL, size, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
267 if (ptr == (void*)-1)
274 free_buffer (void *buf, int size)
277 VirtualFree (buf, 0, MEM_RELEASE);
287 EnterCriticalSection (&log_lock);
289 pthread_mutex_lock (&log_lock);
297 LeaveCriticalSection (&log_lock);
299 pthread_mutex_unlock (&log_lock);
304 encode_uleb128 (uint64_t value, uint8_t *buf, uint8_t **endbuf)
309 uint8_t b = value & 0x7f;
311 if (value != 0) /* more bytes to come */
320 encode_sleb128 (intptr_t value, uint8_t *buf, uint8_t **endbuf)
323 int negative = (value < 0);
324 unsigned int size = sizeof (intptr_t) * 8;
331 /* the following is unnecessary if the
332 * implementation of >>= uses an arithmetic rather
333 * than logical shift for a signed left operand
337 value |= - ((intptr_t)1 <<(size - 7));
338 /* sign bit of byte is second high order bit (0x40) */
339 if ((value == 0 && !(byte & 0x40)) ||
340 (value == -1 && (byte & 0x40)))
351 decode_uleb128 (uint8_t *buf, uint8_t **endbuf)
359 res |= (((uint64_t)(b & 0x7f)) << shift);
371 decode_sleb128 (uint8_t *buf, uint8_t **endbuf)
381 res = res | (((intptr_t)(b & 0x7f)) << shift);
384 if (shift < sizeof (intptr_t) * 8 && (b & 0x40))
385 res |= - ((intptr_t)1 << shift);
399 return (uintptr_t)GetCurrentThreadId ();
401 return (uintptr_t)pthread_self ();