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)
30 #ifdef HAVE_SYS_TIME_H
37 #if defined(__APPLE__)
38 #include <mach/mach_time.h>
41 static mach_timebase_info_data_t timebase_info;
45 #define MAP_ANONYMOUS MAP_ANON
48 #define TICKS_PER_SEC 1000000000LL
50 #if (defined(TARGET_X86) || defined(TARGET_AMD64)) && defined(__linux__)
55 unsigned int timer_count;
63 #define DECL_TLS_DATA TlsData *tls; tls = (TlsData *) TlsGetValue (tls_data); if (tls == NULL) { tls = (TlsData *) calloc (sizeof (TlsData), 1); TlsSetValue (tls_data, tls); }
64 #define TLS_INIT(x) x = TlsAlloc()
66 static __thread TlsData tls_data;
67 #define DECL_TLS_DATA TlsData *tls = &tls_data
70 static pthread_key_t tls_data;
71 #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); }
72 #define TLS_INIT(x) pthread_key_create(&x, NULL)
76 static CRITICAL_SECTION log_lock;
78 static pthread_mutex_t log_lock = PTHREAD_MUTEX_INITIALIZER;
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)
214 InitializeCriticalSection (&log_lock);
216 #if defined (__APPLE__)
217 mach_timebase_info (&timebase_info);
221 time_func = null_time;
222 } else if (fast_time) {
226 timea = clock_time ();
227 timeb = clock_time ();
228 time_inc = (timeb - timea) / TIME_ADJ;
229 /*printf ("time inc: %llu, timea: %llu, timeb: %llu, diff: %llu\n", time_inc, timea, timeb, timec-timeb);*/
231 time_func = rdtsc_current_time;
233 time_func = fast_current_time;
235 time_func = clock_time;
246 alloc_buffer (int size)
250 ptr = VirtualAlloc (NULL, size, MEM_COMMIT, PAGE_READWRITE);
253 ptr = mmap (NULL, size, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
254 if (ptr == (void*)-1)
261 free_buffer (void *buf, int size)
264 VirtualFree (buf, 0, MEM_RELEASE);
274 EnterCriticalSection (&log_lock);
276 pthread_mutex_lock (&log_lock);
284 LeaveCriticalSection (&log_lock);
286 pthread_mutex_unlock (&log_lock);
291 encode_uleb128 (uint64_t value, uint8_t *buf, uint8_t **endbuf)
296 uint8_t b = value & 0x7f;
298 if (value != 0) /* more bytes to come */
307 encode_sleb128 (intptr_t value, uint8_t *buf, uint8_t **endbuf)
310 int negative = (value < 0);
311 unsigned int size = sizeof (intptr_t) * 8;
318 /* the following is unnecessary if the
319 * implementation of >>= uses an arithmetic rather
320 * than logical shift for a signed left operand
324 value |= - ((intptr_t)1 <<(size - 7));
325 /* sign bit of byte is second high order bit (0x40) */
326 if ((value == 0 && !(byte & 0x40)) ||
327 (value == -1 && (byte & 0x40)))
338 decode_uleb128 (uint8_t *buf, uint8_t **endbuf)
346 res |= (((uint64_t)(b & 0x7f)) << shift);
358 decode_sleb128 (uint8_t *buf, uint8_t **endbuf)
368 res = res | (((intptr_t)(b & 0x7f)) << shift);
371 if (shift < sizeof (intptr_t) * 8 && (b & 0x40))
372 res |= - ((intptr_t)1 << shift);
386 return (uintptr_t)GetCurrentThreadId ();
388 return (uintptr_t)pthread_self ();