michael@0: /* -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "VMPI.h" michael@0: michael@0: // Note, this is not supported in configurations with more than one AvmCore running michael@0: // in the same process. michael@0: michael@0: #ifdef WIN32 michael@0: #include "windows.h" michael@0: #else michael@0: #define __cdecl michael@0: #include michael@0: #include michael@0: #endif michael@0: michael@0: #include "vprof.h" michael@0: michael@0: #ifndef MIN michael@0: #define MIN(x,y) ((x) <= (y) ? x : y) michael@0: #endif michael@0: #ifndef MAX michael@0: #define MAX(x,y) ((x) >= (y) ? x : y) michael@0: #endif michael@0: michael@0: #ifndef MAXINT michael@0: #define MAXINT int(unsigned(-1)>>1) michael@0: #endif michael@0: michael@0: #ifndef MAXINT64 michael@0: #define MAXINT64 int64_t(uint64_t(-1)>>1) michael@0: #endif michael@0: michael@0: #ifndef __STDC_WANT_SECURE_LIB__ michael@0: #define sprintf_s(b,size,fmt,...) sprintf((b),(fmt),__VA_ARGS__) michael@0: #endif michael@0: michael@0: #if THREADED michael@0: #define DO_LOCK(lock) Lock(lock); { michael@0: #define DO_UNLOCK(lock) }; Unlock(lock) michael@0: #else michael@0: #define DO_LOCK(lock) { (void)(lock); michael@0: #define DO_UNLOCK(lock) } michael@0: #endif michael@0: michael@0: #if THREAD_SAFE michael@0: #define LOCK(lock) DO_LOCK(lock) michael@0: #define UNLOCK(lock) DO_UNLOCK(lock) michael@0: #else michael@0: #define LOCK(lock) { (void)(lock); michael@0: #define UNLOCK(lock) } michael@0: #endif michael@0: michael@0: static entry* entries = nullptr; michael@0: static bool notInitialized = true; michael@0: static long glock = LOCK_IS_FREE; michael@0: michael@0: #define Lock(lock) while (_InterlockedCompareExchange(lock, LOCK_IS_TAKEN, LOCK_IS_FREE) == LOCK_IS_TAKEN){}; michael@0: #define Unlock(lock) _InterlockedCompareExchange(lock, LOCK_IS_FREE, LOCK_IS_TAKEN); michael@0: michael@0: #if defined(WIN32) michael@0: static void vprof_printf(const char* format, ...) michael@0: { michael@0: va_list args; michael@0: va_start(args, format); michael@0: michael@0: char buf[1024]; michael@0: vsnprintf(buf, sizeof(buf), format, args); michael@0: michael@0: va_end(args); michael@0: michael@0: printf(buf); michael@0: ::OutputDebugStringA(buf); michael@0: } michael@0: #else michael@0: #define vprof_printf printf michael@0: #endif michael@0: michael@0: static inline entry* reverse (entry* s) michael@0: { michael@0: entry_t e, n, p; michael@0: michael@0: p = nullptr; michael@0: for (e = s; e; e = n) { michael@0: n = e->next; michael@0: e->next = p; michael@0: p = e; michael@0: } michael@0: michael@0: return p; michael@0: } michael@0: michael@0: static char* f (double d) michael@0: { michael@0: static char s[80]; michael@0: char* p; michael@0: sprintf_s (s, sizeof(s), "%lf", d); michael@0: p = s+VMPI_strlen(s)-1; michael@0: while (*p == '0') { michael@0: *p = '\0'; michael@0: p--; michael@0: if (p == s) break; michael@0: } michael@0: if (*p == '.') *p = '\0'; michael@0: return s; michael@0: } michael@0: michael@0: static void dumpProfile (void) michael@0: { michael@0: entry_t e; michael@0: michael@0: entries = reverse(entries); michael@0: vprof_printf ("event avg [min : max] total count\n"); michael@0: for (e = entries; e; e = e->next) { michael@0: if (e->count == 0) continue; // ignore entries with zero count. michael@0: vprof_printf ("%s", e->file); michael@0: if (e->line >= 0) { michael@0: vprof_printf (":%d", e->line); michael@0: } michael@0: vprof_printf (" %s [%lld : %lld] %lld %lld ", michael@0: f(((double)e->sum)/((double)e->count)), (long long int)e->min, (long long int)e->max, (long long int)e->sum, (long long int)e->count); michael@0: if (e->h) { michael@0: int j = MAXINT; michael@0: for (j = 0; j < e->h->nbins; j ++) { michael@0: vprof_printf ("(%lld < %lld) ", (long long int)e->h->count[j], (long long int)e->h->lb[j]); michael@0: } michael@0: vprof_printf ("(%lld >= %lld) ", (long long int)e->h->count[e->h->nbins], (long long int)e->h->lb[e->h->nbins-1]); michael@0: } michael@0: if (e->func) { michael@0: int j; michael@0: for (j = 0; j < NUM_EVARS; j++) { michael@0: if (e->ivar[j] != 0) { michael@0: vprof_printf ("IVAR%d %d ", j, e->ivar[j]); michael@0: } michael@0: } michael@0: for (j = 0; j < NUM_EVARS; j++) { michael@0: if (e->i64var[j] != 0) { michael@0: vprof_printf ("I64VAR%d %lld ", j, (long long int)e->i64var[j]); michael@0: } michael@0: } michael@0: for (j = 0; j < NUM_EVARS; j++) { michael@0: if (e->dvar[j] != 0) { michael@0: vprof_printf ("DVAR%d %lf ", j, e->dvar[j]); michael@0: } michael@0: } michael@0: } michael@0: vprof_printf ("\n"); michael@0: } michael@0: entries = reverse(entries); michael@0: } michael@0: michael@0: static inline entry_t findEntry (char* file, int line) michael@0: { michael@0: for (entry_t e = entries; e; e = e->next) { michael@0: if ((e->line == line) && (VMPI_strcmp (e->file, file) == 0)) { michael@0: return e; michael@0: } michael@0: } michael@0: return nullptr; michael@0: } michael@0: michael@0: // Initialize the location pointed to by 'id' to a new value profile entry michael@0: // associated with 'file' and 'line', or do nothing if already initialized. michael@0: // An optional final argument provides a user-defined probe function. michael@0: michael@0: int initValueProfile(void** id, char* file, int line, ...) michael@0: { michael@0: DO_LOCK (&glock); michael@0: entry_t e = (entry_t) *id; michael@0: if (notInitialized) { michael@0: atexit (dumpProfile); michael@0: notInitialized = false; michael@0: } michael@0: michael@0: if (e == nullptr) { michael@0: e = findEntry (file, line); michael@0: if (e) { michael@0: *id = e; michael@0: } michael@0: } michael@0: michael@0: if (e == nullptr) { michael@0: va_list va; michael@0: e = (entry_t) malloc (sizeof(entry)); michael@0: e->lock = LOCK_IS_FREE; michael@0: e->file = file; michael@0: e->line = line; michael@0: e->value = 0; michael@0: e->sum = 0; michael@0: e->count = 0; michael@0: e->min = 0; michael@0: e->max = 0; michael@0: // optional probe function argument michael@0: va_start (va, line); michael@0: e->func = (void (__cdecl*)(void*)) va_arg (va, void*); michael@0: va_end (va); michael@0: e->h = nullptr; michael@0: e->genptr = nullptr; michael@0: VMPI_memset (&e->ivar, 0, sizeof(e->ivar)); michael@0: VMPI_memset (&e->i64var, 0, sizeof(e->i64var)); michael@0: VMPI_memset (&e->dvar, 0, sizeof(e->dvar)); michael@0: e->next = entries; michael@0: entries = e; michael@0: *id = e; michael@0: } michael@0: DO_UNLOCK (&glock); michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: // Record a value profile event. michael@0: michael@0: int profileValue(void* id, int64_t value) michael@0: { michael@0: entry_t e = (entry_t) id; michael@0: long* lock = &(e->lock); michael@0: LOCK (lock); michael@0: e->value = value; michael@0: if (e->count == 0) { michael@0: e->sum = value; michael@0: e->count = 1; michael@0: e->min = value; michael@0: e->max = value; michael@0: } else { michael@0: e->sum += value; michael@0: e->count ++; michael@0: e->min = MIN (e->min, value); michael@0: e->max = MAX (e->max, value); michael@0: } michael@0: if (e->func) e->func (e); michael@0: UNLOCK (lock); michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: // Initialize the location pointed to by 'id' to a new histogram profile entry michael@0: // associated with 'file' and 'line', or do nothing if already initialized. michael@0: michael@0: int initHistProfile(void** id, char* file, int line, int nbins, ...) michael@0: { michael@0: DO_LOCK (&glock); michael@0: entry_t e = (entry_t) *id; michael@0: if (notInitialized) { michael@0: atexit (dumpProfile); michael@0: notInitialized = false; michael@0: } michael@0: michael@0: if (e == nullptr) { michael@0: e = findEntry (file, line); michael@0: if (e) { michael@0: *id = e; michael@0: } michael@0: } michael@0: michael@0: if (e == nullptr) { michael@0: va_list va; michael@0: hist_t h; michael@0: int b, n, s; michael@0: int64_t* lb; michael@0: michael@0: e = (entry_t) malloc (sizeof(entry)); michael@0: e->lock = LOCK_IS_FREE; michael@0: e->file = file; michael@0: e->line = line; michael@0: e->value = 0; michael@0: e->sum = 0; michael@0: e->count = 0; michael@0: e->min = 0; michael@0: e->max = 0; michael@0: e->func = nullptr; michael@0: e->h = h = (hist_t) malloc (sizeof(hist)); michael@0: n = 1+MAX(nbins,0); michael@0: h->nbins = n-1; michael@0: s = n*sizeof(int64_t); michael@0: lb = (int64_t*) malloc (s); michael@0: h->lb = lb; michael@0: VMPI_memset (h->lb, 0, s); michael@0: h->count = (int64_t*) malloc (s); michael@0: VMPI_memset (h->count, 0, s); michael@0: michael@0: va_start (va, nbins); michael@0: for (b = 0; b < nbins; b++) { michael@0: //lb[b] = va_arg (va, int64_t); michael@0: lb[b] = va_arg (va, int); michael@0: } michael@0: lb[b] = MAXINT64; michael@0: va_end (va); michael@0: michael@0: e->genptr = nullptr; michael@0: VMPI_memset (&e->ivar, 0, sizeof(e->ivar)); michael@0: VMPI_memset (&e->i64var, 0, sizeof(e->i64var)); michael@0: VMPI_memset (&e->dvar, 0, sizeof(e->dvar)); michael@0: e->next = entries; michael@0: entries = e; michael@0: *id = e; michael@0: } michael@0: DO_UNLOCK (&glock); michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: // Record a histogram profile event. michael@0: michael@0: int histValue(void* id, int64_t value) michael@0: { michael@0: entry_t e = (entry_t) id; michael@0: long* lock = &(e->lock); michael@0: hist_t h = e->h; michael@0: int nbins = h->nbins; michael@0: int64_t* lb = h->lb; michael@0: int b; michael@0: michael@0: LOCK (lock); michael@0: e->value = value; michael@0: if (e->count == 0) { michael@0: e->sum = value; michael@0: e->count = 1; michael@0: e->min = value; michael@0: e->max = value; michael@0: } else { michael@0: e->sum += value; michael@0: e->count ++; michael@0: e->min = MIN (e->min, value); michael@0: e->max = MAX (e->max, value); michael@0: } michael@0: for (b = 0; b < nbins; b ++) { michael@0: if (value < lb[b]) break; michael@0: } michael@0: h->count[b] ++; michael@0: UNLOCK (lock); michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: #if defined(_MSC_VER) && defined(_M_IX86) michael@0: uint64_t readTimestampCounter() michael@0: { michael@0: // read the cpu cycle counter. 1 tick = 1 cycle on IA32 michael@0: _asm rdtsc; michael@0: } michael@0: #elif defined(__GNUC__) && (__i386__ || __x86_64__) michael@0: uint64_t readTimestampCounter() michael@0: { michael@0: uint32_t lo, hi; michael@0: __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi)); michael@0: return (uint64_t(hi) << 32) | lo; michael@0: } michael@0: #else michael@0: // add stub for platforms without it, so fat builds don't fail michael@0: uint64_t readTimestampCounter() { return 0; } michael@0: #endif michael@0: