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: // michael@0: // Here are a few examples of using the value-profiling utility: michael@0: // michael@0: // _vprof (e); michael@0: // at the end of program execution, you'll get a dump of the source location of this probe, michael@0: // its min, max, average, the total sum of all instances of e, and the total number of times this probe was called. michael@0: // michael@0: // _vprof (x > 0); michael@0: // shows how many times and what percentage of the cases x was > 0, michael@0: // that is the probablitiy that x > 0. michael@0: // michael@0: // _vprof (n % 2 == 0); michael@0: // shows how many times n was an even number michael@0: // as well as th probablitiy of n being an even number. michael@0: // michael@0: // _hprof (n, 4, 1000, 5000, 5001, 10000); michael@0: // gives you the histogram of n over the given 4 bucket boundaries: michael@0: // # cases < 1000 michael@0: // # cases >= 1000 and < 5000 michael@0: // # cases >= 5000 and < 5001 michael@0: // # cases >= 5001 and < 10000 michael@0: // # cases >= 10000 michael@0: // michael@0: // _nvprof ("event name", value); michael@0: // all instances with the same name are merged michael@0: // so, you can call _vprof with the same event name at difference places michael@0: // michael@0: // _vprof (e, myProbe); michael@0: // value profile e and call myProbe (void* vprofID) at the profiling point. michael@0: // inside the probe, the client has the predefined variables: michael@0: // _VAL, _COUNT, _SUM, _MIN, _MAX, and the general purpose registers michael@0: // _IVAR1, ..., IVAR4 general integer registrs michael@0: // _I64VAR1, ..., I64VAR4 general integer64 registrs michael@0: // _DVAR1, ..., _DVAR4 general double registers michael@0: // _GENPTR a generic pointer that can be used by the client michael@0: // the number of registers can be changed in vprof.h michael@0: // michael@0: michael@0: #ifndef devtools_vprof_vprof_h michael@0: #define devtools_vprof_vprof_h michael@0: // michael@0: // If the application for which you want to use vprof is threaded, THREADED must be defined as 1, otherwise define it as 0 michael@0: // michael@0: // If your application is not threaded, define THREAD_SAFE 0, michael@0: // otherwise, you have the option of setting THREAD_SAFE to 1 which results in exact counts or to 0 which results in a much more efficient but non-exact counts michael@0: // michael@0: #define THREADED 0 michael@0: #define THREAD_SAFE 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: // portable align macro michael@0: #if defined(_MSC_VER) michael@0: #define vprof_align8(t) __declspec(align(8)) t michael@0: #elif defined(__GNUC__) michael@0: #define vprof_align8(t) t __attribute__ ((aligned (8))) michael@0: #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC) michael@0: #define vprof_align8(t) t __attribute__ ((aligned (8))) michael@0: #elif defined(VMCFG_SYMBIAN) michael@0: #define vprof_align8(t) t __attribute__ ((aligned (8))) michael@0: #endif michael@0: michael@0: #ifdef __cplusplus michael@0: extern "C" { michael@0: #endif michael@0: michael@0: int initValueProfile(void** id, char* file, int line, ...); michael@0: int profileValue(void* id, int64_t value); michael@0: int initHistProfile(void** id, char* file, int line, int nbins, ...); michael@0: int histValue(void* id, int64_t value); michael@0: uint64_t readTimestampCounter(); michael@0: michael@0: #ifdef __cplusplus michael@0: } michael@0: #endif michael@0: michael@0: //#define DOPROF michael@0: michael@0: #ifndef DOPROF michael@0: #define _nvprof(e,v) michael@0: #ifndef VMCFG_SYMBIAN michael@0: #define _vprof(v,...) michael@0: #define _hprof(v,n,...) michael@0: #define _nhprof(e,v,n,...) michael@0: #define _ntprof_begin(e) michael@0: #define _ntprof_end(e) michael@0: #define _jvprof_init(id,...) michael@0: #define _jnvprof_init(id,e,...) michael@0: #define _jhprof_init(id,n,...) michael@0: #define _jnhprof_init(id,e,n,...) michael@0: #define _jvprof(id,v) michael@0: #define _jhprof(id,v) michael@0: #endif // ! VMCFG_SYMBIAN michael@0: #else michael@0: michael@0: // Historical/compatibility note: michael@0: // The macros below were originally written using conditional expressions, not if/else. The original author michael@0: // said that this was done to allow _vprof and _nvprof to be used in an expression context, but the old code michael@0: // had already wrapped the macro bodies in { }, so it is not clear how this could have worked. At present, michael@0: // the profiling macros must appear in a statement context only. michael@0: michael@0: #define _vprof(v,...) \ michael@0: do { \ michael@0: static void* id = 0; \ michael@0: if (id == 0) \ michael@0: initValueProfile(&id, __FILE__, __LINE__, ##__VA_ARGS__, NULL); \ michael@0: profileValue(id, (int64_t) (v)); \ michael@0: } while (0) michael@0: michael@0: #define _nvprof(e,v) \ michael@0: do { \ michael@0: static void* id = 0; \ michael@0: if (id == 0) \ michael@0: initValueProfile(&id, (char*) (e), -1, NULL); \ michael@0: profileValue(id, (int64_t) (v)); \ michael@0: } while (0) michael@0: michael@0: #define _hprof(v,n,...) \ michael@0: do { \ michael@0: static void* id = 0; \ michael@0: if (id == 0) \ michael@0: initHistProfile(&id, __FILE__, __LINE__, (int) (n), ##__VA_ARGS__); \ michael@0: histValue(id, (int64_t) (v)); \ michael@0: } while (0) michael@0: michael@0: #define _nhprof(e,v,n,...) \ michael@0: do { \ michael@0: static void* id = 0; \ michael@0: if (id == 0) \ michael@0: initHistProfile(&id, (char*) (e), -1, (int) (n), ##__VA_ARGS__); \ michael@0: histValue(id, (int64_t) (v)); \ michael@0: } while (0) michael@0: michael@0: // Profile execution time between _ntprof_begin(e) and _ntprof_end(e). michael@0: // The tag 'e' must match at the beginning and end of the region to michael@0: // be timed. Regions may be nested or overlap arbitrarily, as it is michael@0: // the tag alone that defines the begin/end correspondence. michael@0: michael@0: #define _ntprof_begin(e) \ michael@0: do { \ michael@0: static void* id = 0; \ michael@0: if (id == 0) \ michael@0: initValueProfile(&id, (char*)(e), -1, NULL); \ michael@0: ((entry_t)id)->i64var[0] = readTimestampCounter(); \ michael@0: } while (0) michael@0: michael@0: // Assume 2.6 Ghz CPU michael@0: #define TICKS_PER_USEC 2600 michael@0: michael@0: #define _ntprof_end(e) \ michael@0: do { \ michael@0: static void* id = 0; \ michael@0: uint64_t stop = readTimestampCounter(); \ michael@0: if (id == 0) \ michael@0: initValueProfile(&id, (char*)(e), -1, NULL); \ michael@0: uint64_t start = ((entry_t)id)->i64var[0]; \ michael@0: uint64_t usecs = (stop - start) / TICKS_PER_USEC; \ michael@0: profileValue(id, usecs); \ michael@0: } while (0) michael@0: michael@0: // These macros separate the creation of a profile record from its later usage. michael@0: // They are intended for profiling JIT-generated code. Once created, the JIT can michael@0: // bind a pointer to the profile record into the generated code, which can then michael@0: // record profile events during execution. michael@0: michael@0: #define _jvprof_init(id,...) \ michael@0: if (*(id) == 0) \ michael@0: initValueProfile((id), __FILE__, __LINE__, ##__VA_ARGS__, NULL) michael@0: michael@0: #define _jnvprof_init(id,e,...) \ michael@0: if (*(id) == 0) \ michael@0: initValueProfile((id), (char*) (e), -1, ##__VA_ARGS__, NULL) michael@0: michael@0: #define _jhprof_init(id,n,...) \ michael@0: if (*(id) == 0) \ michael@0: initHistProfile((id), __FILE__, __LINE__, (int) (n), ##__VA_ARGS__) michael@0: michael@0: #define _jnhprof_init(id,e,n,...) \ michael@0: if (*(id) == 0) \ michael@0: initHistProfile((id), (char*) (e), -1, (int) (n), ##__VA_ARGS__) michael@0: michael@0: // Calls to the _jvprof and _jhprof macros must be wrapped in a non-inline michael@0: // function in order to be invoked from JIT-compiled code. michael@0: michael@0: #define _jvprof(id,v) \ michael@0: profileValue((id), (int64_t) (v)) michael@0: michael@0: #define _jhprof(id,v) \ michael@0: histValue((id), (int64_t) (v)) michael@0: michael@0: #endif michael@0: michael@0: #define NUM_EVARS 4 michael@0: michael@0: enum { michael@0: LOCK_IS_FREE = 0, michael@0: LOCK_IS_TAKEN = 1 michael@0: }; michael@0: michael@0: extern michael@0: #ifdef __cplusplus michael@0: "C" michael@0: #endif michael@0: long _InterlockedCompareExchange ( michael@0: long volatile * Destination, michael@0: long Exchange, michael@0: long Comperand michael@0: ); michael@0: michael@0: typedef struct hist hist; michael@0: michael@0: typedef struct hist { michael@0: int nbins; michael@0: int64_t* lb; michael@0: int64_t* count; michael@0: } *hist_t; michael@0: michael@0: typedef struct entry entry; michael@0: michael@0: typedef struct entry { michael@0: long lock; michael@0: char* file; michael@0: int line; michael@0: int64_t value; michael@0: int64_t count; michael@0: int64_t sum; michael@0: int64_t min; michael@0: int64_t max; michael@0: void (*func)(void*); michael@0: hist* h; michael@0: michael@0: entry* next; michael@0: michael@0: // exposed to the clients michael@0: void* genptr; michael@0: int ivar[NUM_EVARS]; michael@0: vprof_align8(int64_t) i64var[NUM_EVARS]; michael@0: vprof_align8(double) dvar[NUM_EVARS]; michael@0: // michael@0: michael@0: char pad[128]; // avoid false sharing michael@0: } *entry_t; michael@0: michael@0: #define _VAL ((entry_t)vprofID)->value michael@0: #define _COUNT ((entry_t)vprofID)->count michael@0: #define _SUM ((entry_t)vprofID)->sum michael@0: #define _MIN ((entry_t)vprofID)->min michael@0: #define _MAX ((entry_t)vprofID)->max michael@0: michael@0: #define _GENPTR ((entry_t)vprofID)->genptr michael@0: michael@0: #define _IVAR0 ((entry_t)vprofID)->ivar[0] michael@0: #define _IVAR1 ((entry_t)vprofID)->ivar[1] michael@0: #define _IVAR2 ((entry_t)vprofID)->ivar[2] michael@0: #define _IVAR3 ((entry_t)vprofID)->ivar[3] michael@0: michael@0: #define _I64VAR0 ((entry_t)vprofID)->i64var[0] michael@0: #define _I64VAR1 ((entry_t)vprofID)->i64var[1] michael@0: #define _I64VAR2 ((entry_t)vprofID)->i64var[2] michael@0: #define _I64VAR3 ((entry_t)vprofID)->i64var[3] michael@0: michael@0: #define _DVAR0 ((entry_t)vprofID)->dvar[0] michael@0: #define _DVAR1 ((entry_t)vprofID)->dvar[1] michael@0: #define _DVAR2 ((entry_t)vprofID)->dvar[2] michael@0: #define _DVAR3 ((entry_t)vprofID)->dvar[3] michael@0: michael@0: #endif /* devtools_vprof_vprof_h */