Merge mozilla-central and tracemonkey. (a=blockers)
[mozilla-central.git] / js / src / jsutil.cpp
blobdc66ab6dbc2a9dd27c2a5f8c05226e279f44513a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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.
16 * The Original Code is Mozilla Communicator client code, released
17 * March 31, 1998.
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.
24 * Contributor(s):
25 * IBM Corp.
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.
39 * ***** END LICENSE BLOCK ***** */
42 * PR assertion checker.
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include "jstypes.h"
47 #include "jsstdint.h"
48 #include "jsutil.h"
50 #ifdef WIN32
51 # include "jswin.h"
52 #else
53 # include <signal.h>
54 #endif
56 using namespace js;
59 * Checks the assumption that JS_FUNC_TO_DATA_PTR and JS_DATA_TO_FUNC_PTR
60 * macros uses to implement casts between function and data pointers.
62 JS_STATIC_ASSERT(sizeof(void *) == sizeof(void (*)()));
64 JS_PUBLIC_API(void) JS_Assert(const char *s, const char *file, JSIntn ln)
66 fprintf(stderr, "Assertion failure: %s, at %s:%d\n", s, file, ln);
67 fflush(stderr);
68 #if defined(WIN32)
70 * We used to call DebugBreak() on Windows, but amazingly, it causes
71 * the MSVS 2010 debugger not to be able to recover a call stack.
73 *((int *) NULL) = 0;
74 exit(3);
75 #elif defined(__APPLE__)
77 * On Mac OS X, Breakpad ignores signals. Only real Mach exceptions are
78 * trapped.
80 *((int *) NULL) = 0; /* To continue from here in GDB: "return" then "continue". */
81 raise(SIGABRT); /* In case above statement gets nixed by the optimizer. */
82 #else
83 raise(SIGABRT); /* To continue from here in GDB: "signal 0". */
84 #endif
87 #ifdef JS_BASIC_STATS
89 #include <math.h>
90 #include <string.h>
91 #include "jscompat.h"
92 #include "jsbit.h"
95 * Histogram bins count occurrences of values <= the bin label, as follows:
97 * linear: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 or more
98 * 2**x: 0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512 or more
99 * 10**x: 0, 1, 10, 100, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9 or more
101 * We wish to count occurrences of 0 and 1 values separately, always.
103 static uint32
104 BinToVal(uintN logscale, uintN bin)
106 JS_ASSERT(bin <= 10);
107 if (bin <= 1 || logscale == 0)
108 return bin;
109 --bin;
110 if (logscale == 2)
111 return JS_BIT(bin);
112 JS_ASSERT(logscale == 10);
113 return (uint32) pow(10.0, (double) bin);
116 static uintN
117 ValToBin(uintN logscale, uint32 val)
119 uintN bin;
121 if (val <= 1)
122 return val;
123 bin = (logscale == 10)
124 ? (uintN) ceil(log10((double) val))
125 : (logscale == 2)
126 ? (uintN) JS_CeilingLog2(val)
127 : val;
128 return JS_MIN(bin, 10);
131 void
132 JS_BasicStatsAccum(JSBasicStats *bs, uint32 val)
134 uintN oldscale, newscale, bin;
135 double mean;
137 ++bs->num;
138 if (bs->max < val)
139 bs->max = val;
140 bs->sum += val;
141 bs->sqsum += (double)val * val;
143 oldscale = bs->logscale;
144 if (oldscale != 10) {
145 mean = bs->sum / bs->num;
146 if (bs->max > 16 && mean > 8) {
147 newscale = (bs->max > 1e6 && mean > 1000) ? 10 : 2;
148 if (newscale != oldscale) {
149 uint32 newhist[11], newbin;
151 PodArrayZero(newhist);
152 for (bin = 0; bin <= 10; bin++) {
153 newbin = ValToBin(newscale, BinToVal(oldscale, bin));
154 newhist[newbin] += bs->hist[bin];
156 memcpy(bs->hist, newhist, sizeof bs->hist);
157 bs->logscale = newscale;
162 bin = ValToBin(bs->logscale, val);
163 ++bs->hist[bin];
166 double
167 JS_MeanAndStdDev(uint32 num, double sum, double sqsum, double *sigma)
169 double var;
171 if (num == 0 || sum == 0) {
172 *sigma = 0;
173 return 0;
176 var = num * sqsum - sum * sum;
177 if (var < 0 || num == 1)
178 var = 0;
179 else
180 var /= (double)num * (num - 1);
182 /* Windows says sqrt(0.0) is "-1.#J" (?!) so we must test. */
183 *sigma = (var != 0) ? sqrt(var) : 0;
184 return sum / num;
187 void
188 JS_DumpBasicStats(JSBasicStats *bs, const char *title, FILE *fp)
190 double mean, sigma;
192 mean = JS_MeanAndStdDevBS(bs, &sigma);
193 fprintf(fp, "\nmean %s %g, std. deviation %g, max %lu\n",
194 title, mean, sigma, (unsigned long) bs->max);
195 JS_DumpHistogram(bs, fp);
198 void
199 JS_DumpHistogram(JSBasicStats *bs, FILE *fp)
201 uintN bin;
202 uint32 cnt, max;
203 double sum, mean;
205 for (bin = 0, max = 0, sum = 0; bin <= 10; bin++) {
206 cnt = bs->hist[bin];
207 if (max < cnt)
208 max = cnt;
209 sum += cnt;
211 mean = sum / cnt;
212 for (bin = 0; bin <= 10; bin++) {
213 uintN val = BinToVal(bs->logscale, bin);
214 uintN end = (bin == 10) ? 0 : BinToVal(bs->logscale, bin + 1);
215 cnt = bs->hist[bin];
216 if (val + 1 == end)
217 fprintf(fp, " [%6u]", val);
218 else if (end != 0)
219 fprintf(fp, "[%6u, %6u]", val, end - 1);
220 else
221 fprintf(fp, "[%6u, +inf]", val);
222 fprintf(fp, ": %8u ", cnt);
223 if (cnt != 0) {
224 if (max > 1e6 && mean > 1e3)
225 cnt = (uint32) ceil(log10((double) cnt));
226 else if (max > 16 && mean > 8)
227 cnt = JS_CeilingLog2(cnt);
228 for (uintN i = 0; i < cnt; i++)
229 putc('*', fp);
231 putc('\n', fp);
235 #endif /* JS_BASIC_STATS */
237 #if defined(DEBUG_notme) && defined(XP_UNIX)
239 #define __USE_GNU 1
240 #include <dlfcn.h>
241 #include <string.h>
242 #include "jshash.h"
243 #include "jsprf.h"
245 JSCallsite js_calltree_root = {0, NULL, NULL, 0, NULL, NULL, NULL, NULL};
247 static JSCallsite *
248 CallTree(void **bp)
250 void **bpup, **bpdown, *pc;
251 JSCallsite *parent, *site, **csp;
252 Dl_info info;
253 int ok, offset;
254 const char *symbol;
255 char *method;
257 /* Reverse the stack frame list to avoid recursion. */
258 bpup = NULL;
259 for (;;) {
260 bpdown = (void**) bp[0];
261 bp[0] = (void*) bpup;
262 if ((void**) bpdown[0] < bpdown)
263 break;
264 bpup = bp;
265 bp = bpdown;
268 /* Reverse the stack again, finding and building a path in the tree. */
269 parent = &js_calltree_root;
270 do {
271 bpup = (void**) bp[0];
272 bp[0] = (void*) bpdown;
273 pc = bp[1];
275 csp = &parent->kids;
276 while ((site = *csp) != NULL) {
277 if (site->pc == (uint32)pc) {
278 /* Put the most recently used site at the front of siblings. */
279 *csp = site->siblings;
280 site->siblings = parent->kids;
281 parent->kids = site;
283 /* Site already built -- go up the stack. */
284 goto upward;
286 csp = &site->siblings;
289 /* Check for recursion: see if pc is on our ancestor line. */
290 for (site = parent; site; site = site->parent) {
291 if (site->pc == (uint32)pc)
292 goto upward;
296 * Not in tree at all: let's find our symbolic callsite info.
297 * XXX static syms are masked by nearest lower global
299 info.dli_fname = info.dli_sname = NULL;
300 ok = dladdr(pc, &info);
301 if (ok < 0) {
302 fprintf(stderr, "dladdr failed!\n");
303 return NULL;
306 /* XXXbe sub 0x08040000? or something, see dbaron bug with tenthumbs comment */
307 symbol = info.dli_sname;
308 offset = (char*)pc - (char*)info.dli_fbase;
309 method = symbol
310 ? strdup(symbol)
311 : JS_smprintf("%s+%X",
312 info.dli_fname ? info.dli_fname : "main",
313 offset);
314 if (!method)
315 return NULL;
317 /* Create a new callsite record. */
318 site = (JSCallsite *) js_malloc(sizeof(JSCallsite));
319 if (!site)
320 return NULL;
322 /* Insert the new site into the tree. */
323 site->pc = (uint32)pc;
324 site->name = method;
325 site->library = info.dli_fname;
326 site->offset = offset;
327 site->parent = parent;
328 site->siblings = parent->kids;
329 parent->kids = site;
330 site->kids = NULL;
332 upward:
333 parent = site;
334 bpdown = bp;
335 bp = bpup;
336 } while (bp);
338 return site;
341 JS_FRIEND_API(JSCallsite *)
342 JS_Backtrace(int skip)
344 void **bp, **bpdown;
346 /* Stack walking code adapted from Kipp's "leaky". */
347 #if defined(__i386)
348 __asm__( "movl %%ebp, %0" : "=g"(bp));
349 #elif defined(__x86_64__)
350 __asm__( "movq %%rbp, %0" : "=g"(bp));
351 #else
353 * It would be nice if this worked uniformly, but at least on i386 and
354 * x86_64, it stopped working with gcc 4.1, because it points to the
355 * end of the saved registers instead of the start.
357 bp = (void**) __builtin_frame_address(0);
358 #endif
359 while (--skip >= 0) {
360 bpdown = (void**) *bp++;
361 if (bpdown < bp)
362 break;
363 bp = bpdown;
366 return CallTree(bp);
369 JS_FRIEND_API(void)
370 JS_DumpBacktrace(JSCallsite *trace)
372 while (trace) {
373 fprintf(stdout, "%s [%s +0x%X]\n", trace->name, trace->library,
374 trace->offset);
375 trace = trace->parent;
379 #endif /* defined(DEBUG_notme) && defined(XP_UNIX) */