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 uint64_t time_inc = 0;
81 typedef uint64_t (*TimeFunc)(void);
83 static TimeFunc time_func;
88 #if defined(__APPLE__)
89 uint64_t time = mach_absolute_time ();
96 struct timespec tspec;
97 clock_gettime (CLOCK_MONOTONIC, &tspec);
98 return ((uint64_t)tspec.tv_sec * TICKS_PER_SEC + tspec.tv_nsec);
102 /* must be power of two */
106 fast_current_time (void)
109 if (tls->timer_count++ & (TIME_ADJ - 1)) {
110 tls->last_time += time_inc;
111 return tls->last_time;
113 tls->last_time = clock_time ();
114 return tls->last_time;
119 #define rdtsc(low,high) \
120 __asm__ __volatile__("rdtsc" : "=a" (low), "=d" (high))
123 safe_rdtsc (int *cpu)
125 unsigned int low, high;
126 int c1 = sched_getcpu ();
129 c2 = sched_getcpu ();
135 return (((uint64_t) high) << 32) + (uint64_t)low;
138 static double cpu_freq;
147 int cpu = sched_getcpu ();
152 if (!(cpuinfo = fopen ("/proc/cpuinfo", "r")))
154 while (fgets (buf, sizeof(buf), cpuinfo)) {
155 if (sscanf (buf, "cpu MHz : %f", &val) == 1) {
156 /*printf ("got mh: %f\n", val);*/
158 cpu_freq = val * 1000000;
160 if (strncmp (buf, "flags :", 5) == 0) {
161 if (strstr (buf, "constant_tsc")) {
163 /*printf ("have tsc\n");*/
168 return have_flag? have_freq: 0;
172 rdtsc_current_time (void)
175 if (tls->timer_count++ & (TIME_ADJ*8 - 1)) {
177 uint64_t tsc = safe_rdtsc (&cpu);
178 if (cpu != -1 && cpu == tls->last_cpu) {
179 int64_t diff = tsc - tls->last_rdtsc;
182 nsecs = (double)diff/cpu_freq;
183 //printf ("%llu cycles: %llu nsecs\n", diff, nsecs);
184 return tls->last_time + nsecs;
186 printf ("tsc went backwards\n");
189 //printf ("wrong cpu: %d\n", cpu);
192 tls->last_time = clock_time ();
193 tls->last_rdtsc = safe_rdtsc (&tls->last_cpu);
194 return tls->last_time;
197 #define have_rdtsc() 0
198 #define rdtsc_current_time fast_current_time
204 static uint64_t timer = 0;
209 utils_init (int fast_time)
213 InitializeCriticalSection (&log_lock);
215 #if defined (__APPLE__)
216 mach_timebase_info (&timebase_info);
220 time_func = null_time;
221 } else if (fast_time) {
225 timea = clock_time ();
226 timeb = clock_time ();
227 time_inc = (timeb - timea) / TIME_ADJ;
228 /*printf ("time inc: %llu, timea: %llu, timeb: %llu, diff: %llu\n", time_inc, timea, timeb, timec-timeb);*/
230 time_func = rdtsc_current_time;
232 time_func = fast_current_time;
234 time_func = clock_time;
245 alloc_buffer (int size)
249 ptr = VirtualAlloc (NULL, size, MEM_COMMIT, PAGE_READWRITE);
252 ptr = mmap (NULL, size, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
253 if (ptr == (void*)-1)
260 free_buffer (void *buf, int size)
263 VirtualFree (buf, 0, MEM_RELEASE);
273 EnterCriticalSection (&log_lock);
275 pthread_mutex_lock (&log_lock);
283 LeaveCriticalSection (&log_lock);
285 pthread_mutex_unlock (&log_lock);
290 encode_uleb128 (uint64_t value, uint8_t *buf, uint8_t **endbuf)
295 uint8_t b = value & 0x7f;
297 if (value != 0) /* more bytes to come */
306 encode_sleb128 (intptr_t value, uint8_t *buf, uint8_t **endbuf)
309 int negative = (value < 0);
310 unsigned int size = sizeof (intptr_t) * 8;
317 /* the following is unnecessary if the
318 * implementation of >>= uses an arithmetic rather
319 * than logical shift for a signed left operand
323 value |= - ((intptr_t)1 <<(size - 7));
324 /* sign bit of byte is second high order bit (0x40) */
325 if ((value == 0 && !(byte & 0x40)) ||
326 (value == -1 && (byte & 0x40)))
337 decode_uleb128 (uint8_t *buf, uint8_t **endbuf)
345 res |= (((uint64_t)(b & 0x7f)) << shift);
357 decode_sleb128 (uint8_t *buf, uint8_t **endbuf)
367 res = res | (((intptr_t)(b & 0x7f)) << shift);
370 if (shift < sizeof (intptr_t) * 8 && (b & 0x40))
371 res |= - ((intptr_t)1 << shift);
385 return (uintptr_t)GetCurrentThreadId ();
387 return (uintptr_t)pthread_self ();