vprof.cpp (8323B)
1 /* -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: t; tab-width: 4 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 #include "VMPI.h" 7 8 // Note, this is not supported in configurations with more than one AvmCore 9 // running in the same process. 10 11 #ifdef WIN32 12 # include "util/WindowsWrapper.h" 13 #else 14 # define __cdecl 15 # include <stdarg.h> 16 # include <string.h> 17 #endif 18 19 #include "vprof.h" 20 21 #ifndef MIN 22 # define MIN(x, y) ((x) <= (y) ? x : y) 23 #endif 24 #ifndef MAX 25 # define MAX(x, y) ((x) >= (y) ? x : y) 26 #endif 27 28 #ifndef MAXINT 29 # define MAXINT int(unsigned(-1) >> 1) 30 #endif 31 32 #ifndef MAXINT64 33 # define MAXINT64 int64_t(uint64_t(-1) >> 1) 34 #endif 35 36 #ifndef __STDC_WANT_SECURE_LIB__ 37 # define sprintf_s(b, size, fmt, ...) sprintf((b), (fmt), __VA_ARGS__) 38 #endif 39 40 #if THREADED 41 # define DO_LOCK(lock) \ 42 Lock(lock); \ 43 { 44 # define DO_UNLOCK(lock) \ 45 } \ 46 ; \ 47 Unlock(lock) 48 #else 49 # define DO_LOCK(lock) \ 50 { \ 51 (void)(lock); 52 # define DO_UNLOCK(lock) } 53 #endif 54 55 #if THREAD_SAFE 56 # define LOCK(lock) DO_LOCK(lock) 57 # define UNLOCK(lock) DO_UNLOCK(lock) 58 #else 59 # define LOCK(lock) \ 60 { \ 61 (void)(lock); 62 # define UNLOCK(lock) } 63 #endif 64 65 static entry* entries = nullptr; 66 static bool notInitialized = true; 67 static long glock = LOCK_IS_FREE; 68 69 #define Lock(lock) \ 70 while (_InterlockedCompareExchange(lock, LOCK_IS_TAKEN, LOCK_IS_FREE) == \ 71 LOCK_IS_TAKEN) { \ 72 }; 73 #define Unlock(lock) \ 74 _InterlockedCompareExchange(lock, LOCK_IS_FREE, LOCK_IS_TAKEN); 75 76 #if defined(WIN32) 77 static void vprof_printf(const char* format, ...) { 78 va_list args; 79 va_start(args, format); 80 81 char buf[1024]; 82 vsnprintf(buf, sizeof(buf), format, args); 83 84 va_end(args); 85 86 printf(buf); 87 ::OutputDebugStringA(buf); 88 } 89 #else 90 # define vprof_printf printf 91 #endif 92 93 static inline entry* reverse(entry* s) { 94 entry_t e, n, p; 95 96 p = nullptr; 97 for (e = s; e; e = n) { 98 n = e->next; 99 e->next = p; 100 p = e; 101 } 102 103 return p; 104 } 105 106 static char* f(double d) { 107 static char s[80]; 108 char* p; 109 sprintf_s(s, sizeof(s), "%lf", d); 110 p = s + VMPI_strlen(s) - 1; 111 while (*p == '0') { 112 *p = '\0'; 113 p--; 114 if (p == s) break; 115 } 116 if (*p == '.') *p = '\0'; 117 return s; 118 } 119 120 static void dumpProfile(void) { 121 entry_t e; 122 123 entries = reverse(entries); 124 vprof_printf("event avg [min : max] total count\n"); 125 for (e = entries; e; e = e->next) { 126 if (e->count == 0) continue; // ignore entries with zero count. 127 vprof_printf("%s", e->file); 128 if (e->line >= 0) { 129 vprof_printf(":%d", e->line); 130 } 131 vprof_printf(" %s [%lld : %lld] %lld %lld ", 132 f(((double)e->sum) / ((double)e->count)), 133 (long long int)e->min, (long long int)e->max, 134 (long long int)e->sum, (long long int)e->count); 135 if (e->h) { 136 int j = MAXINT; 137 for (j = 0; j < e->h->nbins; j++) { 138 vprof_printf("(%lld < %lld) ", (long long int)e->h->count[j], 139 (long long int)e->h->lb[j]); 140 } 141 vprof_printf("(%lld >= %lld) ", (long long int)e->h->count[e->h->nbins], 142 (long long int)e->h->lb[e->h->nbins - 1]); 143 } 144 if (e->func) { 145 int j; 146 for (j = 0; j < NUM_EVARS; j++) { 147 if (e->ivar[j] != 0) { 148 vprof_printf("IVAR%d %d ", j, e->ivar[j]); 149 } 150 } 151 for (j = 0; j < NUM_EVARS; j++) { 152 if (e->i64var[j] != 0) { 153 vprof_printf("I64VAR%d %lld ", j, (long long int)e->i64var[j]); 154 } 155 } 156 for (j = 0; j < NUM_EVARS; j++) { 157 if (e->dvar[j] != 0) { 158 vprof_printf("DVAR%d %lf ", j, e->dvar[j]); 159 } 160 } 161 } 162 vprof_printf("\n"); 163 } 164 entries = reverse(entries); 165 } 166 167 static inline entry_t findEntry(char* file, int line) { 168 for (entry_t e = entries; e; e = e->next) { 169 if ((e->line == line) && (VMPI_strcmp(e->file, file) == 0)) { 170 return e; 171 } 172 } 173 return nullptr; 174 } 175 176 // Initialize the location pointed to by 'id' to a new value profile entry 177 // associated with 'file' and 'line', or do nothing if already initialized. 178 // An optional final argument provides a user-defined probe function. 179 180 int initValueProfile(void** id, char* file, int line, ...) { 181 DO_LOCK(&glock); 182 entry_t e = (entry_t)*id; 183 if (notInitialized) { 184 atexit(dumpProfile); 185 notInitialized = false; 186 } 187 188 if (e == nullptr) { 189 e = findEntry(file, line); 190 if (e) { 191 *id = e; 192 } 193 } 194 195 if (e == nullptr) { 196 va_list va; 197 e = (entry_t)malloc(sizeof(entry)); 198 e->lock = LOCK_IS_FREE; 199 e->file = file; 200 e->line = line; 201 e->value = 0; 202 e->sum = 0; 203 e->count = 0; 204 e->min = 0; 205 e->max = 0; 206 // optional probe function argument 207 va_start(va, line); 208 e->func = (void(__cdecl*)(void*))va_arg(va, void*); 209 va_end(va); 210 e->h = nullptr; 211 e->genptr = nullptr; 212 VMPI_memset(&e->ivar, 0, sizeof(e->ivar)); 213 VMPI_memset(&e->i64var, 0, sizeof(e->i64var)); 214 VMPI_memset(&e->dvar, 0, sizeof(e->dvar)); 215 e->next = entries; 216 entries = e; 217 *id = e; 218 } 219 DO_UNLOCK(&glock); 220 221 return 0; 222 } 223 224 // Record a value profile event. 225 226 int profileValue(void* id, int64_t value) { 227 entry_t e = (entry_t)id; 228 long* lock = &(e->lock); 229 LOCK(lock); 230 e->value = value; 231 if (e->count == 0) { 232 e->sum = value; 233 e->count = 1; 234 e->min = value; 235 e->max = value; 236 } else { 237 e->sum += value; 238 e->count++; 239 e->min = MIN(e->min, value); 240 e->max = MAX(e->max, value); 241 } 242 if (e->func) e->func(e); 243 UNLOCK(lock); 244 245 return 0; 246 } 247 248 // Initialize the location pointed to by 'id' to a new histogram profile entry 249 // associated with 'file' and 'line', or do nothing if already initialized. 250 251 int initHistProfile(void** id, char* file, int line, int nbins, ...) { 252 DO_LOCK(&glock); 253 entry_t e = (entry_t)*id; 254 if (notInitialized) { 255 atexit(dumpProfile); 256 notInitialized = false; 257 } 258 259 if (e == nullptr) { 260 e = findEntry(file, line); 261 if (e) { 262 *id = e; 263 } 264 } 265 266 if (e == nullptr) { 267 va_list va; 268 hist_t h; 269 int b, n, s; 270 int64_t* lb; 271 272 e = (entry_t)malloc(sizeof(entry)); 273 e->lock = LOCK_IS_FREE; 274 e->file = file; 275 e->line = line; 276 e->value = 0; 277 e->sum = 0; 278 e->count = 0; 279 e->min = 0; 280 e->max = 0; 281 e->func = nullptr; 282 e->h = h = (hist_t)malloc(sizeof(hist)); 283 n = 1 + MAX(nbins, 0); 284 h->nbins = n - 1; 285 s = n * sizeof(int64_t); 286 lb = (int64_t*)malloc(s); 287 h->lb = lb; 288 VMPI_memset(h->lb, 0, s); 289 h->count = (int64_t*)malloc(s); 290 VMPI_memset(h->count, 0, s); 291 292 va_start(va, nbins); 293 for (b = 0; b < nbins; b++) { 294 // lb[b] = va_arg (va, int64_t); 295 lb[b] = va_arg(va, int); 296 } 297 lb[b] = MAXINT64; 298 va_end(va); 299 300 e->genptr = nullptr; 301 VMPI_memset(&e->ivar, 0, sizeof(e->ivar)); 302 VMPI_memset(&e->i64var, 0, sizeof(e->i64var)); 303 VMPI_memset(&e->dvar, 0, sizeof(e->dvar)); 304 e->next = entries; 305 entries = e; 306 *id = e; 307 } 308 DO_UNLOCK(&glock); 309 310 return 0; 311 } 312 313 // Record a histogram profile event. 314 315 int histValue(void* id, int64_t value) { 316 entry_t e = (entry_t)id; 317 long* lock = &(e->lock); 318 hist_t h = e->h; 319 int nbins = h->nbins; 320 int64_t* lb = h->lb; 321 int b; 322 323 LOCK(lock); 324 e->value = value; 325 if (e->count == 0) { 326 e->sum = value; 327 e->count = 1; 328 e->min = value; 329 e->max = value; 330 } else { 331 e->sum += value; 332 e->count++; 333 e->min = MIN(e->min, value); 334 e->max = MAX(e->max, value); 335 } 336 for (b = 0; b < nbins; b++) { 337 if (value < lb[b]) break; 338 } 339 h->count[b]++; 340 UNLOCK(lock); 341 342 return 0; 343 } 344 345 #if defined(_MSC_VER) && defined(_M_IX86) 346 uint64_t readTimestampCounter() { 347 // read the cpu cycle counter. 1 tick = 1 cycle on IA32 348 _asm rdtsc; 349 } 350 #elif defined(__GNUC__) && (__i386__ || __x86_64__) 351 uint64_t readTimestampCounter() { 352 uint32_t lo, hi; 353 __asm__ __volatile__("rdtsc" : "=a"(lo), "=d"(hi)); 354 return (uint64_t(hi) << 32) | lo; 355 } 356 #else 357 // add stub for platforms without it, so fat builds don't fail 358 uint64_t readTimestampCounter() { return 0; } 359 #endif