PageRenderTime 45ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/js/lib/Socket.IO-node/support/expresso/deps/jscoverage/js/jsutil.cpp

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
C++ | 345 lines | 234 code | 41 blank | 70 comment | 57 complexity | fe0a4e5c0b316432de4179e40f489b99 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2. *
  3. * ***** BEGIN LICENSE BLOCK *****
  4. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5. *
  6. * The contents of this file are subject to the Mozilla Public License Version
  7. * 1.1 (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. * http://www.mozilla.org/MPL/
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. *
  16. * The Original Code is Mozilla Communicator client code, released
  17. * March 31, 1998.
  18. *
  19. * The Initial Developer of the Original Code is
  20. * Netscape Communications Corporation.
  21. * Portions created by the Initial Developer are Copyright (C) 1998
  22. * the Initial Developer. All Rights Reserved.
  23. *
  24. * Contributor(s):
  25. * IBM Corp.
  26. *
  27. * Alternatively, the contents of this file may be used under the terms of
  28. * either of the GNU General Public License Version 2 or later (the "GPL"),
  29. * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  30. * in which case the provisions of the GPL or the LGPL are applicable instead
  31. * of those above. If you wish to allow use of your version of this file only
  32. * under the terms of either the GPL or the LGPL, and not to allow others to
  33. * use your version of this file under the terms of the MPL, indicate your
  34. * decision by deleting the provisions above and replace them with the notice
  35. * and other provisions required by the GPL or the LGPL. If you do not delete
  36. * the provisions above, a recipient may use your version of this file under
  37. * the terms of any one of the MPL, the GPL or the LGPL.
  38. *
  39. * ***** END LICENSE BLOCK ***** */
  40. /*
  41. * PR assertion checker.
  42. */
  43. #include "jsstddef.h"
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include "jstypes.h"
  47. #include "jsutil.h"
  48. #ifdef WIN32
  49. # include <windows.h>
  50. #endif
  51. JS_PUBLIC_API(void) JS_Assert(const char *s, const char *file, JSIntn ln)
  52. {
  53. fprintf(stderr, "Assertion failure: %s, at %s:%d\n", s, file, ln);
  54. #if defined(WIN32)
  55. DebugBreak();
  56. exit(3);
  57. #elif defined(XP_OS2) || (defined(__GNUC__) && defined(__i386))
  58. asm("int $3");
  59. #endif
  60. abort();
  61. }
  62. #ifdef JS_BASIC_STATS
  63. #include <math.h>
  64. #include <string.h>
  65. #include "jscompat.h"
  66. #include "jsbit.h"
  67. /*
  68. * Histogram bins count occurrences of values <= the bin label, as follows:
  69. *
  70. * linear: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 or more
  71. * 2**x: 0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512 or more
  72. * 10**x: 0, 1, 10, 100, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9 or more
  73. *
  74. * We wish to count occurrences of 0 and 1 values separately, always.
  75. */
  76. static uint32
  77. BinToVal(uintN logscale, uintN bin)
  78. {
  79. JS_ASSERT(bin <= 10);
  80. if (bin <= 1 || logscale == 0)
  81. return bin;
  82. --bin;
  83. if (logscale == 2)
  84. return JS_BIT(bin);
  85. JS_ASSERT(logscale == 10);
  86. return (uint32) pow(10.0, (double) bin);
  87. }
  88. static uintN
  89. ValToBin(uintN logscale, uint32 val)
  90. {
  91. uintN bin;
  92. if (val <= 1)
  93. return val;
  94. bin = (logscale == 10)
  95. ? (uintN) ceil(log10((double) val))
  96. : (logscale == 2)
  97. ? (uintN) JS_CeilingLog2(val)
  98. : val;
  99. return JS_MIN(bin, 10);
  100. }
  101. void
  102. JS_BasicStatsAccum(JSBasicStats *bs, uint32 val)
  103. {
  104. uintN oldscale, newscale, bin;
  105. double mean;
  106. ++bs->num;
  107. if (bs->max < val)
  108. bs->max = val;
  109. bs->sum += val;
  110. bs->sqsum += (double)val * val;
  111. oldscale = bs->logscale;
  112. if (oldscale != 10) {
  113. mean = bs->sum / bs->num;
  114. if (bs->max > 16 && mean > 8) {
  115. newscale = (bs->max > 1e6 && mean > 1000) ? 10 : 2;
  116. if (newscale != oldscale) {
  117. uint32 newhist[11], newbin;
  118. memset(newhist, 0, sizeof newhist);
  119. for (bin = 0; bin <= 10; bin++) {
  120. newbin = ValToBin(newscale, BinToVal(oldscale, bin));
  121. newhist[newbin] += bs->hist[bin];
  122. }
  123. memcpy(bs->hist, newhist, sizeof bs->hist);
  124. bs->logscale = newscale;
  125. }
  126. }
  127. }
  128. bin = ValToBin(bs->logscale, val);
  129. ++bs->hist[bin];
  130. }
  131. double
  132. JS_MeanAndStdDev(uint32 num, double sum, double sqsum, double *sigma)
  133. {
  134. double var;
  135. if (num == 0 || sum == 0) {
  136. *sigma = 0;
  137. return 0;
  138. }
  139. var = num * sqsum - sum * sum;
  140. if (var < 0 || num == 1)
  141. var = 0;
  142. else
  143. var /= (double)num * (num - 1);
  144. /* Windows says sqrt(0.0) is "-1.#J" (?!) so we must test. */
  145. *sigma = (var != 0) ? sqrt(var) : 0;
  146. return sum / num;
  147. }
  148. void
  149. JS_DumpBasicStats(JSBasicStats *bs, const char *title, FILE *fp)
  150. {
  151. double mean, sigma;
  152. mean = JS_MeanAndStdDevBS(bs, &sigma);
  153. fprintf(fp, "\nmean %s %g, std. deviation %g, max %lu\n",
  154. title, mean, sigma, (unsigned long) bs->max);
  155. JS_DumpHistogram(bs, fp);
  156. }
  157. void
  158. JS_DumpHistogram(JSBasicStats *bs, FILE *fp)
  159. {
  160. uintN bin;
  161. uint32 cnt, max, prev, val, i;
  162. double sum, mean;
  163. for (bin = 0, max = 0, sum = 0; bin <= 10; bin++) {
  164. cnt = bs->hist[bin];
  165. if (max < cnt)
  166. max = cnt;
  167. sum += cnt;
  168. }
  169. mean = sum / cnt;
  170. for (bin = 0, prev = 0; bin <= 10; bin++, prev = val) {
  171. val = BinToVal(bs->logscale, bin);
  172. cnt = bs->hist[bin];
  173. if (prev + 1 >= val)
  174. fprintf(fp, " [%6u]", val);
  175. else
  176. fprintf(fp, "[%6u, %6u]", prev + 1, val);
  177. fprintf(fp, "%s %8u ", (bin == 10) ? "+" : ":", cnt);
  178. if (cnt != 0) {
  179. if (max > 1e6 && mean > 1e3)
  180. cnt = (uint32) ceil(log10((double) cnt));
  181. else if (max > 16 && mean > 8)
  182. cnt = JS_CeilingLog2(cnt);
  183. for (i = 0; i < cnt; i++)
  184. putc('*', fp);
  185. }
  186. putc('\n', fp);
  187. }
  188. }
  189. #endif /* JS_BASIC_STATS */
  190. #if defined DEBUG_notme && defined XP_UNIX
  191. #define __USE_GNU 1
  192. #include <dlfcn.h>
  193. #include <string.h>
  194. #include "jshash.h"
  195. #include "jsprf.h"
  196. JSCallsite js_calltree_root = {0, NULL, NULL, 0, NULL, NULL, NULL, NULL};
  197. static JSCallsite *
  198. CallTree(void **bp)
  199. {
  200. void **bpup, **bpdown, *pc;
  201. JSCallsite *parent, *site, **csp;
  202. Dl_info info;
  203. int ok, offset;
  204. const char *symbol;
  205. char *method;
  206. /* Reverse the stack frame list to avoid recursion. */
  207. bpup = NULL;
  208. for (;;) {
  209. bpdown = (void**) bp[0];
  210. bp[0] = (void*) bpup;
  211. if ((void**) bpdown[0] < bpdown)
  212. break;
  213. bpup = bp;
  214. bp = bpdown;
  215. }
  216. /* Reverse the stack again, finding and building a path in the tree. */
  217. parent = &js_calltree_root;
  218. do {
  219. bpup = (void**) bp[0];
  220. bp[0] = (void*) bpdown;
  221. pc = bp[1];
  222. csp = &parent->kids;
  223. while ((site = *csp) != NULL) {
  224. if (site->pc == (uint32)pc) {
  225. /* Put the most recently used site at the front of siblings. */
  226. *csp = site->siblings;
  227. site->siblings = parent->kids;
  228. parent->kids = site;
  229. /* Site already built -- go up the stack. */
  230. goto upward;
  231. }
  232. csp = &site->siblings;
  233. }
  234. /* Check for recursion: see if pc is on our ancestor line. */
  235. for (site = parent; site; site = site->parent) {
  236. if (site->pc == (uint32)pc)
  237. goto upward;
  238. }
  239. /*
  240. * Not in tree at all: let's find our symbolic callsite info.
  241. * XXX static syms are masked by nearest lower global
  242. */
  243. info.dli_fname = info.dli_sname = NULL;
  244. ok = dladdr(pc, &info);
  245. if (ok < 0) {
  246. fprintf(stderr, "dladdr failed!\n");
  247. return NULL;
  248. }
  249. /* XXXbe sub 0x08040000? or something, see dbaron bug with tenthumbs comment */
  250. symbol = info.dli_sname;
  251. offset = (char*)pc - (char*)info.dli_fbase;
  252. method = symbol
  253. ? strdup(symbol)
  254. : JS_smprintf("%s+%X",
  255. info.dli_fname ? info.dli_fname : "main",
  256. offset);
  257. if (!method)
  258. return NULL;
  259. /* Create a new callsite record. */
  260. site = (JSCallsite *) malloc(sizeof(JSCallsite));
  261. if (!site)
  262. return NULL;
  263. /* Insert the new site into the tree. */
  264. site->pc = (uint32)pc;
  265. site->name = method;
  266. site->library = info.dli_fname;
  267. site->offset = offset;
  268. site->parent = parent;
  269. site->siblings = parent->kids;
  270. parent->kids = site;
  271. site->kids = NULL;
  272. upward:
  273. parent = site;
  274. bpdown = bp;
  275. bp = bpup;
  276. } while (bp);
  277. return site;
  278. }
  279. JSCallsite *
  280. JS_Backtrace(int skip)
  281. {
  282. void **bp, **bpdown;
  283. /* Stack walking code adapted from Kipp's "leaky". */
  284. #if defined(__i386)
  285. __asm__( "movl %%ebp, %0" : "=g"(bp));
  286. #elif defined(__x86_64__)
  287. __asm__( "movq %%rbp, %0" : "=g"(bp));
  288. #else
  289. /*
  290. * It would be nice if this worked uniformly, but at least on i386 and
  291. * x86_64, it stopped working with gcc 4.1, because it points to the
  292. * end of the saved registers instead of the start.
  293. */
  294. bp = (void**) __builtin_frame_address(0);
  295. #endif
  296. while (--skip >= 0) {
  297. bpdown = (void**) *bp++;
  298. if (bpdown < bp)
  299. break;
  300. bp = bpdown;
  301. }
  302. return CallTree(bp);
  303. }
  304. #endif /* DEBUG_notme && XP_UNIX */