1 /* -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Value-Profiling Utility.
17 * The Initial Developer of the Original Code is
19 * Portions created by the Initial Developer are Copyright (C) 2008
20 * the Initial Developer. All Rights Reserved.
23 * Mohammad R. Haghighat [mohammad.r.haghighat@intel.com]
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
41 // Note, this is not supported in configurations with more than one AvmCore running
42 // in the same process.
54 #define MIN(x,y) ((x) <= (y) ? x : y)
55 #define MAX(x,y) ((x) >= (y) ? x : y)
58 #define MAXINT int(unsigned(-1)>>1)
62 #define MAXINT64 int64_t(uint64_t(-1)>>1)
65 #ifndef __STDC_WANT_SECURE_LIB__
66 #define sprintf_s(b,size,fmt,...) sprintf((b),(fmt),__VA_ARGS__)
70 #define DO_LOCK(lock) Lock(lock); {
71 #define DO_UNLOCK(lock) }; Unlock(lock)
73 #define DO_LOCK(lock) { (void)(lock);
74 #define DO_UNLOCK(lock) }
78 #define LOCK(lock) DO_LOCK(lock)
79 #define UNLOCK(lock) DO_UNLOCK(lock)
81 #define LOCK(lock) { (void)(lock);
82 #define UNLOCK(lock) }
85 static entry* entries = NULL;
86 static bool notInitialized = true;
87 static long glock = LOCK_IS_FREE;
89 #define Lock(lock) while (_InterlockedCompareExchange(lock, LOCK_IS_TAKEN, LOCK_IS_FREE) == LOCK_IS_TAKEN){};
90 #define Unlock(lock) _InterlockedCompareExchange(lock, LOCK_IS_FREE, LOCK_IS_TAKEN);
92 #if defined(WIN32) && !defined(UNDER_CE)
93 static void vprof_printf(const char* format, ...)
96 va_start(args, format);
99 vsnprintf(buf, sizeof(buf), format, args);
104 ::OutputDebugStringA(buf);
107 #define vprof_printf printf
110 inline static entry* reverse (entry* s)
115 for (e = s; e; e = n) {
124 static char* f (double d)
128 sprintf_s (s, sizeof(s), "%lf", d);
129 p = s+VMPI_strlen(s)-1;
135 if (*p == '.') *p = '\0';
139 static void dumpProfile (void)
143 entries = reverse(entries);
144 vprof_printf ("event avg [min : max] total count\n");
145 for (e = entries; e; e = e->next) {
146 if (e->count == 0) continue; // ignore entries with zero count.
147 vprof_printf ("%s", e->file);
149 vprof_printf (":%d", e->line);
151 vprof_printf (" %s [%lld : %lld] %lld %lld ",
152 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);
155 for (j = 0; j < e->h->nbins; j ++) {
156 vprof_printf ("(%lld < %lld) ", (long long int)e->h->count[j], (long long int)e->h->lb[j]);
158 vprof_printf ("(%lld >= %lld) ", (long long int)e->h->count[e->h->nbins], (long long int)e->h->lb[e->h->nbins-1]);
162 for (j = 0; j < NUM_EVARS; j++) {
163 if (e->ivar[j] != 0) {
164 vprof_printf ("IVAR%d %d ", j, e->ivar[j]);
167 for (j = 0; j < NUM_EVARS; j++) {
168 if (e->i64var[j] != 0) {
169 vprof_printf ("I64VAR%d %lld ", j, (long long int)e->i64var[j]);
172 for (j = 0; j < NUM_EVARS; j++) {
173 if (e->dvar[j] != 0) {
174 vprof_printf ("DVAR%d %lf ", j, e->dvar[j]);
180 entries = reverse(entries);
183 inline static entry_t findEntry (char* file, int line)
185 for (entry_t e = entries; e; e = e->next) {
186 if ((e->line == line) && (VMPI_strcmp (e->file, file) == 0)) {
193 // Initialize the location pointed to by 'id' to a new value profile entry
194 // associated with 'file' and 'line', or do nothing if already initialized.
195 // An optional final argument provides a user-defined probe function.
197 int initValueProfile(void** id, char* file, int line, ...)
200 entry_t e = (entry_t) *id;
201 if (notInitialized) {
202 atexit (dumpProfile);
203 notInitialized = false;
207 e = findEntry (file, line);
215 e = (entry_t) malloc (sizeof(entry));
216 e->lock = LOCK_IS_FREE;
224 // optional probe function argument
226 e->func = (void (__cdecl*)(void*)) va_arg (va, void*);
230 VMPI_memset (&e->ivar, 0, sizeof(e->ivar));
231 VMPI_memset (&e->i64var, 0, sizeof(e->i64var));
232 VMPI_memset (&e->dvar, 0, sizeof(e->dvar));
242 // Record a value profile event.
244 int profileValue(void* id, int64_t value)
246 entry_t e = (entry_t) id;
247 long* lock = &(e->lock);
258 e->min = MIN (e->min, value);
259 e->max = MAX (e->max, value);
261 if (e->func) e->func (e);
267 // Initialize the location pointed to by 'id' to a new histogram profile entry
268 // associated with 'file' and 'line', or do nothing if already initialized.
270 int initHistProfile(void** id, char* file, int line, int nbins, ...)
273 entry_t e = (entry_t) *id;
274 if (notInitialized) {
275 atexit (dumpProfile);
276 notInitialized = false;
280 e = findEntry (file, line);
292 e = (entry_t) malloc (sizeof(entry));
293 e->lock = LOCK_IS_FREE;
302 e->h = h = (hist_t) malloc (sizeof(hist));
305 s = n*sizeof(int64_t);
306 lb = (int64_t*) malloc (s);
308 VMPI_memset (h->lb, 0, s);
309 h->count = (int64_t*) malloc (s);
310 VMPI_memset (h->count, 0, s);
312 va_start (va, nbins);
313 for (b = 0; b < nbins; b++) {
314 //lb[b] = va_arg (va, int64_t);
315 lb[b] = va_arg (va, int);
321 VMPI_memset (&e->ivar, 0, sizeof(e->ivar));
322 VMPI_memset (&e->i64var, 0, sizeof(e->i64var));
323 VMPI_memset (&e->dvar, 0, sizeof(e->dvar));
333 // Record a histogram profile event.
335 int histValue(void* id, int64_t value)
337 entry_t e = (entry_t) id;
338 long* lock = &(e->lock);
340 int nbins = h->nbins;
354 e->min = MIN (e->min, value);
355 e->max = MAX (e->max, value);
357 for (b = 0; b < nbins; b ++) {
358 if (value < lb[b]) break;
366 #if defined(_MSC_VER) && defined(_M_IX86)
367 uint64_t readTimestampCounter()
369 // read the cpu cycle counter. 1 tick = 1 cycle on IA32
372 #elif defined(__GNUC__) && (__i386__ || __x86_64__)
373 uint64_t readTimestampCounter()
376 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
377 return (uint64_t(hi) << 32) | lo;
380 // add stub for platforms without it, so fat builds don't fail
381 uint64_t readTimestampCounter() { return 0; }