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
16 * The Original Code is Mozilla Communicator client code, released
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.
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.
54 JS_PUBLIC_API(void) JS_Assert(const char *s
, const char *file
, JSIntn ln
)
56 fprintf(stderr
, "Assertion failure: %s, at %s:%d\n", s
, file
, ln
);
60 #elif defined(XP_OS2) || (defined(__GNUC__) && defined(__i386))
74 * Histogram bins count occurrences of values <= the bin label, as follows:
76 * linear: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 or more
77 * 2**x: 0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512 or more
78 * 10**x: 0, 1, 10, 100, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9 or more
80 * We wish to count occurrences of 0 and 1 values separately, always.
83 BinToVal(uintN logscale
, uintN bin
)
86 if (bin
<= 1 || logscale
== 0)
91 JS_ASSERT(logscale
== 10);
92 return (uint32
) pow(10.0, (double) bin
);
96 ValToBin(uintN logscale
, uint32 val
)
102 bin
= (logscale
== 10)
103 ? (uintN
) ceil(log10((double) val
))
105 ? (uintN
) JS_CeilingLog2(val
)
107 return JS_MIN(bin
, 10);
111 JS_BasicStatsAccum(JSBasicStats
*bs
, uint32 val
)
113 uintN oldscale
, newscale
, bin
;
120 bs
->sqsum
+= (double)val
* val
;
122 oldscale
= bs
->logscale
;
123 if (oldscale
!= 10) {
124 mean
= bs
->sum
/ bs
->num
;
125 if (bs
->max
> 16 && mean
> 8) {
126 newscale
= (bs
->max
> 1e6
&& mean
> 1000) ? 10 : 2;
127 if (newscale
!= oldscale
) {
128 uint32 newhist
[11], newbin
;
130 memset(newhist
, 0, sizeof newhist
);
131 for (bin
= 0; bin
<= 10; bin
++) {
132 newbin
= ValToBin(newscale
, BinToVal(oldscale
, bin
));
133 newhist
[newbin
] += bs
->hist
[bin
];
135 memcpy(bs
->hist
, newhist
, sizeof bs
->hist
);
136 bs
->logscale
= newscale
;
141 bin
= ValToBin(bs
->logscale
, val
);
146 JS_MeanAndStdDev(uint32 num
, double sum
, double sqsum
, double *sigma
)
150 if (num
== 0 || sum
== 0) {
155 var
= num
* sqsum
- sum
* sum
;
156 if (var
< 0 || num
== 1)
159 var
/= (double)num
* (num
- 1);
161 /* Windows says sqrt(0.0) is "-1.#J" (?!) so we must test. */
162 *sigma
= (var
!= 0) ? sqrt(var
) : 0;
167 JS_DumpBasicStats(JSBasicStats
*bs
, const char *title
, FILE *fp
)
171 mean
= JS_MeanAndStdDevBS(bs
, &sigma
);
172 fprintf(fp
, "\nmean %s %g, std. deviation %g, max %lu\n",
173 title
, mean
, sigma
, (unsigned long) bs
->max
);
174 JS_DumpHistogram(bs
, fp
);
178 JS_DumpHistogram(JSBasicStats
*bs
, FILE *fp
)
181 uint32 cnt
, max
, prev
, val
, i
;
184 for (bin
= 0, max
= 0, sum
= 0; bin
<= 10; bin
++) {
191 for (bin
= 0, prev
= 0; bin
<= 10; bin
++, prev
= val
) {
192 val
= BinToVal(bs
->logscale
, bin
);
195 fprintf(fp
, " [%6u]", val
);
197 fprintf(fp
, "[%6u, %6u]", prev
+ 1, val
);
198 fprintf(fp
, "%s %8u ", (bin
== 10) ? "+" : ":", cnt
);
200 if (max
> 1e6
&& mean
> 1e3
)
201 cnt
= (uint32
) ceil(log10((double) cnt
));
202 else if (max
> 16 && mean
> 8)
203 cnt
= JS_CeilingLog2(cnt
);
204 for (i
= 0; i
< cnt
; i
++)
211 #endif /* JS_BASIC_STATS */
213 #if defined DEBUG_notme && defined XP_UNIX
221 JSCallsite js_calltree_root
= {0, NULL
, NULL
, 0, NULL
, NULL
, NULL
, NULL
};
226 void **bpup
, **bpdown
, *pc
;
227 JSCallsite
*parent
, *site
, **csp
;
233 /* Reverse the stack frame list to avoid recursion. */
236 bpdown
= (void**) bp
[0];
237 bp
[0] = (void*) bpup
;
238 if ((void**) bpdown
[0] < bpdown
)
244 /* Reverse the stack again, finding and building a path in the tree. */
245 parent
= &js_calltree_root
;
247 bpup
= (void**) bp
[0];
248 bp
[0] = (void*) bpdown
;
252 while ((site
= *csp
) != NULL
) {
253 if (site
->pc
== (uint32
)pc
) {
254 /* Put the most recently used site at the front of siblings. */
255 *csp
= site
->siblings
;
256 site
->siblings
= parent
->kids
;
259 /* Site already built -- go up the stack. */
262 csp
= &site
->siblings
;
265 /* Check for recursion: see if pc is on our ancestor line. */
266 for (site
= parent
; site
; site
= site
->parent
) {
267 if (site
->pc
== (uint32
)pc
)
272 * Not in tree at all: let's find our symbolic callsite info.
273 * XXX static syms are masked by nearest lower global
275 info
.dli_fname
= info
.dli_sname
= NULL
;
276 ok
= dladdr(pc
, &info
);
278 fprintf(stderr
, "dladdr failed!\n");
282 /* XXXbe sub 0x08040000? or something, see dbaron bug with tenthumbs comment */
283 symbol
= info
.dli_sname
;
284 offset
= (char*)pc
- (char*)info
.dli_fbase
;
287 : JS_smprintf("%s+%X",
288 info
.dli_fname
? info
.dli_fname
: "main",
293 /* Create a new callsite record. */
294 site
= (JSCallsite
*) malloc(sizeof(JSCallsite
));
298 /* Insert the new site into the tree. */
299 site
->pc
= (uint32
)pc
;
301 site
->library
= info
.dli_fname
;
302 site
->offset
= offset
;
303 site
->parent
= parent
;
304 site
->siblings
= parent
->kids
;
318 JS_Backtrace(int skip
)
322 /* Stack walking code adapted from Kipp's "leaky". */
324 __asm__( "movl %%ebp, %0" : "=g"(bp
));
325 #elif defined(__x86_64__)
326 __asm__( "movq %%rbp, %0" : "=g"(bp
));
329 * It would be nice if this worked uniformly, but at least on i386 and
330 * x86_64, it stopped working with gcc 4.1, because it points to the
331 * end of the saved registers instead of the start.
333 bp
= (void**) __builtin_frame_address(0);
335 while (--skip
>= 0) {
336 bpdown
= (void**) *bp
++;
345 #endif /* DEBUG_notme && XP_UNIX */