/js/src/vprof/vprof.h

http://github.com/zpao/v8monkey · C Header · 308 lines · 162 code · 44 blank · 102 comment · 27 complexity · 7414f0d2afd1d4c47ce2a58ea27c5dfb MD5 · raw file

  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
  4. *
  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/
  9. *
  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
  13. * License.
  14. *
  15. * The Original Code is Value-Profiling Utility.
  16. *
  17. * The Initial Developer of the Original Code is
  18. * Intel Corporation.
  19. * Portions created by the Initial Developer are Copyright (C) 2008
  20. * the Initial Developer. All Rights Reserved.
  21. *
  22. * Contributor(s):
  23. * Mohammad R. Haghighat [mohammad.r.haghighat@intel.com]
  24. *
  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.
  36. *
  37. * ***** END LICENSE BLOCK ***** */
  38. //
  39. // Here are a few examples of using the value-profiling utility:
  40. //
  41. // _vprof (e);
  42. // at the end of program execution, you'll get a dump of the source location of this probe,
  43. // its min, max, average, the total sum of all instances of e, and the total number of times this probe was called.
  44. //
  45. // _vprof (x > 0);
  46. // shows how many times and what percentage of the cases x was > 0,
  47. // that is the probablitiy that x > 0.
  48. //
  49. // _vprof (n % 2 == 0);
  50. // shows how many times n was an even number
  51. // as well as th probablitiy of n being an even number.
  52. //
  53. // _hprof (n, 4, 1000, 5000, 5001, 10000);
  54. // gives you the histogram of n over the given 4 bucket boundaries:
  55. // # cases < 1000
  56. // # cases >= 1000 and < 5000
  57. // # cases >= 5000 and < 5001
  58. // # cases >= 5001 and < 10000
  59. // # cases >= 10000
  60. //
  61. // _nvprof ("event name", value);
  62. // all instances with the same name are merged
  63. // so, you can call _vprof with the same event name at difference places
  64. //
  65. // _vprof (e, myProbe);
  66. // value profile e and call myProbe (void* vprofID) at the profiling point.
  67. // inside the probe, the client has the predefined variables:
  68. // _VAL, _COUNT, _SUM, _MIN, _MAX, and the general purpose registers
  69. // _IVAR1, ..., IVAR4 general integer registrs
  70. // _I64VAR1, ..., I64VAR4 general integer64 registrs
  71. // _DVAR1, ..., _DVAR4 general double registers
  72. // _GENPTR a generic pointer that can be used by the client
  73. // the number of registers can be changed in vprof.h
  74. //
  75. #ifndef __VPROF__
  76. #define __VPROF__
  77. //
  78. // If the application for which you want to use vprof is threaded, THREADED must be defined as 1, otherwise define it as 0
  79. //
  80. // If your application is not threaded, define THREAD_SAFE 0,
  81. // 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
  82. //
  83. #define THREADED 0
  84. #define THREAD_SAFE 0
  85. #include "VMPI.h"
  86. // Note, this is not supported in configurations with more than one AvmCore running
  87. // in the same process.
  88. // portable align macro
  89. #if defined(_MSC_VER)
  90. #define vprof_align8(t) __declspec(align(8)) t
  91. #elif defined(__GNUC__)
  92. #define vprof_align8(t) t __attribute__ ((aligned (8)))
  93. #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
  94. #define vprof_align8(t) t __attribute__ ((aligned (8)))
  95. #elif defined(VMCFG_SYMBIAN)
  96. #define vprof_align8(t) t __attribute__ ((aligned (8)))
  97. #endif
  98. #ifdef __cplusplus
  99. extern "C" {
  100. #endif
  101. int initValueProfile(void** id, char* file, int line, ...);
  102. int profileValue(void* id, int64_t value);
  103. int initHistProfile(void** id, char* file, int line, int nbins, ...);
  104. int histValue(void* id, int64_t value);
  105. uint64_t readTimestampCounter();
  106. #ifdef __cplusplus
  107. }
  108. #endif
  109. //#define DOPROF
  110. #ifndef DOPROF
  111. #define _nvprof(e,v)
  112. #ifndef VMCFG_SYMBIAN
  113. #define _vprof(v,...)
  114. #define _hprof(v,n,...)
  115. #define _nhprof(e,v,n,...)
  116. #define _ntprof_begin(e)
  117. #define _ntprof_end(e)
  118. #define _jvprof_init(id,...)
  119. #define _jnvprof_init(id,e,...)
  120. #define _jhprof_init(id,n,...)
  121. #define _jnhprof_init(id,e,n,...)
  122. #define _jvprof(id,v)
  123. #define _jhprof(id,v)
  124. #endif // ! VMCFG_SYMBIAN
  125. #else
  126. // Historical/compatibility note:
  127. // The macros below were originally written using conditional expressions, not if/else. The original author
  128. // said that this was done to allow _vprof and _nvprof to be used in an expression context, but the old code
  129. // had already wrapped the macro bodies in { }, so it is not clear how this could have worked. At present,
  130. // the profiling macros must appear in a statement context only.
  131. #define _vprof(v,...) \
  132. do { \
  133. static void* id = 0; \
  134. if (id == 0) \
  135. initValueProfile(&id, __FILE__, __LINE__, ##__VA_ARGS__, NULL); \
  136. profileValue(id, (int64_t) (v)); \
  137. } while (0)
  138. #define _nvprof(e,v) \
  139. do { \
  140. static void* id = 0; \
  141. if (id == 0) \
  142. initValueProfile(&id, (char*) (e), -1, NULL); \
  143. profileValue(id, (int64_t) (v)); \
  144. } while (0)
  145. #define _hprof(v,n,...) \
  146. do { \
  147. static void* id = 0; \
  148. if (id == 0) \
  149. initHistProfile(&id, __FILE__, __LINE__, (int) (n), ##__VA_ARGS__); \
  150. histValue(id, (int64_t) (v)); \
  151. } while (0)
  152. #define _nhprof(e,v,n,...) \
  153. do { \
  154. static void* id = 0; \
  155. if (id == 0) \
  156. initHistProfile(&id, (char*) (e), -1, (int) (n), ##__VA_ARGS__); \
  157. histValue(id, (int64_t) (v)); \
  158. } while (0)
  159. // Profile execution time between _ntprof_begin(e) and _ntprof_end(e).
  160. // The tag 'e' must match at the beginning and end of the region to
  161. // be timed. Regions may be nested or overlap arbitrarily, as it is
  162. // the tag alone that defines the begin/end correspondence.
  163. #define _ntprof_begin(e) \
  164. do { \
  165. static void* id = 0; \
  166. if (id == 0) \
  167. initValueProfile(&id, (char*)(e), -1, NULL); \
  168. ((entry_t)id)->i64var[0] = readTimestampCounter(); \
  169. } while (0)
  170. // Assume 2.6 Ghz CPU
  171. #define TICKS_PER_USEC 2600
  172. #define _ntprof_end(e) \
  173. do { \
  174. static void* id = 0; \
  175. uint64_t stop = readTimestampCounter(); \
  176. if (id == 0) \
  177. initValueProfile(&id, (char*)(e), -1, NULL); \
  178. uint64_t start = ((entry_t)id)->i64var[0]; \
  179. uint64_t usecs = (stop - start) / TICKS_PER_USEC; \
  180. profileValue(id, usecs); \
  181. } while (0)
  182. // These macros separate the creation of a profile record from its later usage.
  183. // They are intended for profiling JIT-generated code. Once created, the JIT can
  184. // bind a pointer to the profile record into the generated code, which can then
  185. // record profile events during execution.
  186. #define _jvprof_init(id,...) \
  187. if (*(id) == 0) \
  188. initValueProfile((id), __FILE__, __LINE__, ##__VA_ARGS__, NULL)
  189. #define _jnvprof_init(id,e,...) \
  190. if (*(id) == 0) \
  191. initValueProfile((id), (char*) (e), -1, ##__VA_ARGS__, NULL)
  192. #define _jhprof_init(id,n,...) \
  193. if (*(id) == 0) \
  194. initHistProfile((id), __FILE__, __LINE__, (int) (n), ##__VA_ARGS__)
  195. #define _jnhprof_init(id,e,n,...) \
  196. if (*(id) == 0) \
  197. initHistProfile((id), (char*) (e), -1, (int) (n), ##__VA_ARGS__)
  198. // Calls to the _jvprof and _jhprof macros must be wrapped in a non-inline
  199. // function in order to be invoked from JIT-compiled code.
  200. #define _jvprof(id,v) \
  201. profileValue((id), (int64_t) (v))
  202. #define _jhprof(id,v) \
  203. histValue((id), (int64_t) (v))
  204. #endif
  205. #define NUM_EVARS 4
  206. enum {
  207. LOCK_IS_FREE = 0,
  208. LOCK_IS_TAKEN = 1
  209. };
  210. extern
  211. #ifdef __cplusplus
  212. "C"
  213. #endif
  214. long _InterlockedCompareExchange (
  215. long volatile * Destination,
  216. long Exchange,
  217. long Comperand
  218. );
  219. typedef struct hist hist;
  220. typedef struct hist {
  221. int nbins;
  222. int64_t* lb;
  223. int64_t* count;
  224. } *hist_t;
  225. typedef struct entry entry;
  226. typedef struct entry {
  227. long lock;
  228. char* file;
  229. int line;
  230. int64_t value;
  231. int64_t count;
  232. int64_t sum;
  233. int64_t min;
  234. int64_t max;
  235. void (*func)(void*);
  236. hist* h;
  237. entry* next;
  238. // exposed to the clients
  239. void* genptr;
  240. int ivar[NUM_EVARS];
  241. vprof_align8(int64_t) i64var[NUM_EVARS];
  242. vprof_align8(double) dvar[NUM_EVARS];
  243. //
  244. char pad[128]; // avoid false sharing
  245. } *entry_t;
  246. #define _VAL ((entry_t)vprofID)->value
  247. #define _COUNT ((entry_t)vprofID)->count
  248. #define _SUM ((entry_t)vprofID)->sum
  249. #define _MIN ((entry_t)vprofID)->min
  250. #define _MAX ((entry_t)vprofID)->max
  251. #define _GENPTR ((entry_t)vprofID)->genptr
  252. #define _IVAR0 ((entry_t)vprofID)->ivar[0]
  253. #define _IVAR1 ((entry_t)vprofID)->ivar[1]
  254. #define _IVAR2 ((entry_t)vprofID)->ivar[2]
  255. #define _IVAR3 ((entry_t)vprofID)->ivar[3]
  256. #define _I64VAR0 ((entry_t)vprofID)->i64var[0]
  257. #define _I64VAR1 ((entry_t)vprofID)->i64var[1]
  258. #define _I64VAR2 ((entry_t)vprofID)->i64var[2]
  259. #define _I64VAR3 ((entry_t)vprofID)->i64var[3]
  260. #define _DVAR0 ((entry_t)vprofID)->dvar[0]
  261. #define _DVAR1 ((entry_t)vprofID)->dvar[1]
  262. #define _DVAR2 ((entry_t)vprofID)->dvar[2]
  263. #define _DVAR3 ((entry_t)vprofID)->dvar[3]
  264. #endif /* __VPROF__ */