/[jscoverage]/trunk/js/jsutil.cpp
ViewVC logotype

Contents of /trunk/js/jsutil.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 507 - (show annotations)
Sun Jan 10 07:23:34 2010 UTC (9 years, 7 months ago) by siliconforks
File size: 10122 byte(s)
Update SpiderMonkey from Firefox 3.6rc1.

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 /*
42 * PR assertion checker.
43 */
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include "jstypes.h"
47 #include "jsstdint.h"
48 #include "jsutil.h"
49
50 #ifdef WIN32
51 # include <windows.h>
52 #endif
53
54 /*
55 * Checks the assumption that JS_FUNC_TO_DATA_PTR and JS_DATA_TO_FUNC_PTR
56 * macros uses to implement casts between function and data pointers.
57 */
58 JS_STATIC_ASSERT(sizeof(void *) == sizeof(void (*)()));
59
60 JS_PUBLIC_API(void) JS_Assert(const char *s, const char *file, JSIntn ln)
61 {
62 fprintf(stderr, "Assertion failure: %s, at %s:%d\n", s, file, ln);
63 #if defined(WIN32)
64 DebugBreak();
65 exit(3);
66 #elif defined(XP_OS2) || (defined(__GNUC__) && defined(__i386))
67 asm("int $3");
68 #endif
69 abort();
70 }
71
72 #ifdef JS_BASIC_STATS
73
74 #include <math.h>
75 #include <string.h>
76 #include "jscompat.h"
77 #include "jsbit.h"
78
79 /*
80 * Histogram bins count occurrences of values <= the bin label, as follows:
81 *
82 * linear: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 or more
83 * 2**x: 0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512 or more
84 * 10**x: 0, 1, 10, 100, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9 or more
85 *
86 * We wish to count occurrences of 0 and 1 values separately, always.
87 */
88 static uint32
89 BinToVal(uintN logscale, uintN bin)
90 {
91 JS_ASSERT(bin <= 10);
92 if (bin <= 1 || logscale == 0)
93 return bin;
94 --bin;
95 if (logscale == 2)
96 return JS_BIT(bin);
97 JS_ASSERT(logscale == 10);
98 return (uint32) pow(10.0, (double) bin);
99 }
100
101 static uintN
102 ValToBin(uintN logscale, uint32 val)
103 {
104 uintN bin;
105
106 if (val <= 1)
107 return val;
108 bin = (logscale == 10)
109 ? (uintN) ceil(log10((double) val))
110 : (logscale == 2)
111 ? (uintN) JS_CeilingLog2(val)
112 : val;
113 return JS_MIN(bin, 10);
114 }
115
116 void
117 JS_BasicStatsAccum(JSBasicStats *bs, uint32 val)
118 {
119 uintN oldscale, newscale, bin;
120 double mean;
121
122 ++bs->num;
123 if (bs->max < val)
124 bs->max = val;
125 bs->sum += val;
126 bs->sqsum += (double)val * val;
127
128 oldscale = bs->logscale;
129 if (oldscale != 10) {
130 mean = bs->sum / bs->num;
131 if (bs->max > 16 && mean > 8) {
132 newscale = (bs->max > 1e6 && mean > 1000) ? 10 : 2;
133 if (newscale != oldscale) {
134 uint32 newhist[11], newbin;
135
136 memset(newhist, 0, sizeof newhist);
137 for (bin = 0; bin <= 10; bin++) {
138 newbin = ValToBin(newscale, BinToVal(oldscale, bin));
139 newhist[newbin] += bs->hist[bin];
140 }
141 memcpy(bs->hist, newhist, sizeof bs->hist);
142 bs->logscale = newscale;
143 }
144 }
145 }
146
147 bin = ValToBin(bs->logscale, val);
148 ++bs->hist[bin];
149 }
150
151 double
152 JS_MeanAndStdDev(uint32 num, double sum, double sqsum, double *sigma)
153 {
154 double var;
155
156 if (num == 0 || sum == 0) {
157 *sigma = 0;
158 return 0;
159 }
160
161 var = num * sqsum - sum * sum;
162 if (var < 0 || num == 1)
163 var = 0;
164 else
165 var /= (double)num * (num - 1);
166
167 /* Windows says sqrt(0.0) is "-1.#J" (?!) so we must test. */
168 *sigma = (var != 0) ? sqrt(var) : 0;
169 return sum / num;
170 }
171
172 void
173 JS_DumpBasicStats(JSBasicStats *bs, const char *title, FILE *fp)
174 {
175 double mean, sigma;
176
177 mean = JS_MeanAndStdDevBS(bs, &sigma);
178 fprintf(fp, "\nmean %s %g, std. deviation %g, max %lu\n",
179 title, mean, sigma, (unsigned long) bs->max);
180 JS_DumpHistogram(bs, fp);
181 }
182
183 void
184 JS_DumpHistogram(JSBasicStats *bs, FILE *fp)
185 {
186 uintN bin;
187 uint32 cnt, max, prev, val, i;
188 double sum, mean;
189
190 for (bin = 0, max = 0, sum = 0; bin <= 10; bin++) {
191 cnt = bs->hist[bin];
192 if (max < cnt)
193 max = cnt;
194 sum += cnt;
195 }
196 mean = sum / cnt;
197 for (bin = 0, prev = 0; bin <= 10; bin++, prev = val) {
198 val = BinToVal(bs->logscale, bin);
199 cnt = bs->hist[bin];
200 if (prev + 1 >= val)
201 fprintf(fp, " [%6u]", val);
202 else
203 fprintf(fp, "[%6u, %6u]", prev + 1, val);
204 fprintf(fp, "%s %8u ", (bin == 10) ? "+" : ":", cnt);
205 if (cnt != 0) {
206 if (max > 1e6 && mean > 1e3)
207 cnt = (uint32) ceil(log10((double) cnt));
208 else if (max > 16 && mean > 8)
209 cnt = JS_CeilingLog2(cnt);
210 for (i = 0; i < cnt; i++)
211 putc('*', fp);
212 }
213 putc('\n', fp);
214 }
215 }
216
217 #endif /* JS_BASIC_STATS */
218
219 #if defined(DEBUG_notme) && defined(XP_UNIX)
220
221 #define __USE_GNU 1
222 #include <dlfcn.h>
223 #include <string.h>
224 #include "jshash.h"
225 #include "jsprf.h"
226
227 JSCallsite js_calltree_root = {0, NULL, NULL, 0, NULL, NULL, NULL, NULL};
228
229 static JSCallsite *
230 CallTree(void **bp)
231 {
232 void **bpup, **bpdown, *pc;
233 JSCallsite *parent, *site, **csp;
234 Dl_info info;
235 int ok, offset;
236 const char *symbol;
237 char *method;
238
239 /* Reverse the stack frame list to avoid recursion. */
240 bpup = NULL;
241 for (;;) {
242 bpdown = (void**) bp[0];
243 bp[0] = (void*) bpup;
244 if ((void**) bpdown[0] < bpdown)
245 break;
246 bpup = bp;
247 bp = bpdown;
248 }
249
250 /* Reverse the stack again, finding and building a path in the tree. */
251 parent = &js_calltree_root;
252 do {
253 bpup = (void**) bp[0];
254 bp[0] = (void*) bpdown;
255 pc = bp[1];
256
257 csp = &parent->kids;
258 while ((site = *csp) != NULL) {
259 if (site->pc == (uint32)pc) {
260 /* Put the most recently used site at the front of siblings. */
261 *csp = site->siblings;
262 site->siblings = parent->kids;
263 parent->kids = site;
264
265 /* Site already built -- go up the stack. */
266 goto upward;
267 }
268 csp = &site->siblings;
269 }
270
271 /* Check for recursion: see if pc is on our ancestor line. */
272 for (site = parent; site; site = site->parent) {
273 if (site->pc == (uint32)pc)
274 goto upward;
275 }
276
277 /*
278 * Not in tree at all: let's find our symbolic callsite info.
279 * XXX static syms are masked by nearest lower global
280 */
281 info.dli_fname = info.dli_sname = NULL;
282 ok = dladdr(pc, &info);
283 if (ok < 0) {
284 fprintf(stderr, "dladdr failed!\n");
285 return NULL;
286 }
287
288 /* XXXbe sub 0x08040000? or something, see dbaron bug with tenthumbs comment */
289 symbol = info.dli_sname;
290 offset = (char*)pc - (char*)info.dli_fbase;
291 method = symbol
292 ? strdup(symbol)
293 : JS_smprintf("%s+%X",
294 info.dli_fname ? info.dli_fname : "main",
295 offset);
296 if (!method)
297 return NULL;
298
299 /* Create a new callsite record. */
300 site = (JSCallsite *) js_malloc(sizeof(JSCallsite));
301 if (!site)
302 return NULL;
303
304 /* Insert the new site into the tree. */
305 site->pc = (uint32)pc;
306 site->name = method;
307 site->library = info.dli_fname;
308 site->offset = offset;
309 site->parent = parent;
310 site->siblings = parent->kids;
311 parent->kids = site;
312 site->kids = NULL;
313
314 upward:
315 parent = site;
316 bpdown = bp;
317 bp = bpup;
318 } while (bp);
319
320 return site;
321 }
322
323 JS_FRIEND_API(JSCallsite *)
324 JS_Backtrace(int skip)
325 {
326 void **bp, **bpdown;
327
328 /* Stack walking code adapted from Kipp's "leaky". */
329 #if defined(__i386)
330 __asm__( "movl %%ebp, %0" : "=g"(bp));
331 #elif defined(__x86_64__)
332 __asm__( "movq %%rbp, %0" : "=g"(bp));
333 #else
334 /*
335 * It would be nice if this worked uniformly, but at least on i386 and
336 * x86_64, it stopped working with gcc 4.1, because it points to the
337 * end of the saved registers instead of the start.
338 */
339 bp = (void**) __builtin_frame_address(0);
340 #endif
341 while (--skip >= 0) {
342 bpdown = (void**) *bp++;
343 if (bpdown < bp)
344 break;
345 bp = bpdown;
346 }
347
348 return CallTree(bp);
349 }
350
351 JS_FRIEND_API(void)
352 JS_DumpBacktrace(JSCallsite *trace)
353 {
354 while (trace) {
355 fprintf(stdout, "%s [%s +0x%X]\n", trace->name, trace->library,
356 trace->offset);
357 trace = trace->parent;
358 }
359 }
360
361 #endif /* defined(DEBUG_notme) && defined(XP_UNIX) */

  ViewVC Help
Powered by ViewVC 1.1.24