LCOV - app.info - js/src/jsutil.cpp

LCOV - code coverage report
| Current view: | directory - js/src - jsutil.cpp (source / functions) |  | Found | Hit | Coverage |
| Test: | app.info | Lines: | 86 | 0 | 0.0 %
| Date: | 2012-04-07 | Functions: | 8 | 0 | 0.0 %

           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                 : /* Various JS utility functions. */
          42                 :
          43                 : #include "mozilla/Attributes.h"
          44                 :
          45                 : #include <stdio.h>
          46                 : #include <stdlib.h>
          47                 : #include "jstypes.h"
          48                 : #include "jsutil.h"
          49                 :
          50                 : #ifdef WIN32
          51                 : #    include "jswin.h"
          52                 : #else
          53                 : #    include <signal.h>
          54                 : #endif
          55                 :
          56                 : #include "js/TemplateLib.h"
          57                 :
          58                 : using namespace js;
          59                 :
          60                 : #ifdef DEBUG
          61                 : /* For JS_OOM_POSSIBLY_FAIL in jsutil.h. */
          62                 : JS_PUBLIC_DATA(uint32_t) OOM_maxAllocations = UINT32_MAX;
          63                 : JS_PUBLIC_DATA(uint32_t) OOM_counter = 0;
          64                 : #endif
          65                 :
          66                 : /*
          67                 :  * Checks the assumption that JS_FUNC_TO_DATA_PTR and JS_DATA_TO_FUNC_PTR
          68                 :  * macros uses to implement casts between function and data pointers.
          69                 :  */
          70                 : JS_STATIC_ASSERT(sizeof(void *) == sizeof(void (*)()));
          71                 :
          72                 : static JS_NEVER_INLINE void
          73               0 : CrashInJS()
          74                 : {
          75                 :     /*
          76                 :      * We write 123 here so that the machine code for this function is
          77                 :      * unique. Otherwise the linker, trying to be smart, might use the
          78                 :      * same code for CrashInJS and for some other function. That
          79                 :      * messes up the signature in minidumps.
          80                 :      */
          81                 :
          82                 : #if defined(WIN32)
          83                 :     /*
          84                 :      * We used to call DebugBreak() on Windows, but amazingly, it causes
          85                 :      * the MSVS 2010 debugger not to be able to recover a call stack.
          86                 :      */
          87                 :     *((volatile int *) NULL) = 123;
          88                 :     exit(3);
          89                 : #elif defined(__APPLE__)
          90                 :     /*
          91                 :      * On Mac OS X, Breakpad ignores signals. Only real Mach exceptions are
          92                 :      * trapped.
          93                 :      */
          94                 :     *((volatile int *) NULL) = 123;  /* To continue from here in GDB: "return" then "continue". */
          95                 :     raise(SIGABRT);  /* In case above statement gets nixed by the optimizer. */
          96                 : #else
          97               0 :     raise(SIGABRT);  /* To continue from here in GDB: "signal 0". */
          98                 : #endif
          99               0 : }
         100                 :
         101                 : JS_PUBLIC_API(void)
         102               0 : JS_Assert(const char *s, const char *file, int ln)
         103                 : {
         104               0 :     fprintf(stderr, "Assertion failure: %s, at %s:%d\n", s, file, ln);
         105               0 :     fflush(stderr);
         106               0 :     CrashInJS();
         107               0 : }
         108                 :
         109                 : #ifdef JS_BASIC_STATS
         110                 :
         111                 : #include <math.h>
         112                 : #include <string.h>
         113                 :
         114                 : /*
         115                 :  * Histogram bins count occurrences of values <= the bin label, as follows:
         116                 :  *
         117                 :  *   linear:  0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10 or more
         118                 :  *     2**x:  0,   1,   2,   4,   8,  16,  32,  64, 128, 256, 512 or more
         119                 :  *    10**x:  0,   1,  10, 100, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9 or more
         120                 :  *
         121                 :  * We wish to count occurrences of 0 and 1 values separately, always.
         122                 :  */
         123                 : static uint32_t
         124               0 : BinToVal(unsigned logscale, unsigned bin)
         125                 : {
         126               0 :     JS_ASSERT(bin <= 10);
         127               0 :     if (bin <= 1 || logscale == 0)
         128               0 :         return bin;
         129               0 :     --bin;
         130               0 :     if (logscale == 2)
         131               0 :         return JS_BIT(bin);
         132               0 :     JS_ASSERT(logscale == 10);
         133               0 :     return uint32_t(pow(10.0, (double) bin));
         134                 : }
         135                 :
         136                 : static unsigned
         137               0 : ValToBin(unsigned logscale, uint32_t val)
         138                 : {
         139                 :     unsigned bin;
         140                 :
         141               0 :     if (val <= 1)
         142               0 :         return val;
         143                 :     bin = (logscale == 10)
         144               0 :           ? (unsigned) ceil(log10((double) val))
         145                 :           : (logscale == 2)
         146               0 :           ? (unsigned) JS_CEILING_LOG2W(val)
         147               0 :           : val;
         148               0 :     return JS_MIN(bin, 10);
         149                 : }
         150                 :
         151                 : void
         152               0 : JS_BasicStatsAccum(JSBasicStats *bs, uint32_t val)
         153                 : {
         154                 :     unsigned oldscale, newscale, bin;
         155                 :     double mean;
         156                 :
         157               0 :     ++bs->num;
         158               0 :     if (bs->max < val)
         159               0 :         bs->max = val;
         160               0 :     bs->sum += val;
         161               0 :     bs->sqsum += (double)val * val;
         162                 :
         163               0 :     oldscale = bs->logscale;
         164               0 :     if (oldscale != 10) {
         165               0 :         mean = bs->sum / bs->num;
         166               0 :         if (bs->max > 16 && mean > 8) {
         167               0 :             newscale = (bs->max > 1e6 && mean > 1000) ? 10 : 2;
         168               0 :             if (newscale != oldscale) {
         169                 :                 uint32_t newhist[11], newbin;
         170                 :
         171               0 :                 PodArrayZero(newhist);
         172               0 :                 for (bin = 0; bin <= 10; bin++) {
         173               0 :                     newbin = ValToBin(newscale, BinToVal(oldscale, bin));
         174               0 :                     newhist[newbin] += bs->hist[bin];
         175                 :                 }
         176               0 :                 js_memcpy(bs->hist, newhist, sizeof bs->hist);
         177               0 :                 bs->logscale = newscale;
         178                 :             }
         179                 :         }
         180                 :     }
         181                 :
         182               0 :     bin = ValToBin(bs->logscale, val);
         183               0 :     ++bs->hist[bin];
         184               0 : }
         185                 :
         186                 : double
         187               0 : JS_MeanAndStdDev(uint32_t num, double sum, double sqsum, double *sigma)
         188                 : {
         189                 :     double var;
         190                 :
         191               0 :     if (num == 0 || sum == 0) {
         192               0 :         *sigma = 0;
         193               0 :         return 0;
         194                 :     }
         195                 :
         196               0 :     var = num * sqsum - sum * sum;
         197               0 :     if (var < 0 || num == 1)
         198               0 :         var = 0;
         199                 :     else
         200               0 :         var /= (double)num * (num - 1);
         201                 :
         202                 :     /* Windows says sqrt(0.0) is "-1.#J" (?!) so we must test. */
         203               0 :     *sigma = (var != 0) ? sqrt(var) : 0;
         204               0 :     return sum / num;
         205                 : }
         206                 :
         207                 : void
         208               0 : JS_DumpBasicStats(JSBasicStats *bs, const char *title, FILE *fp)
         209                 : {
         210                 :     double mean, sigma;
         211                 :
         212               0 :     mean = JS_MeanAndStdDevBS(bs, &sigma);
         213                 :     fprintf(fp, "\nmean %s %g, std. deviation %g, max %lu\n",
         214               0 :             title, mean, sigma, (unsigned long) bs->max);
         215               0 :     JS_DumpHistogram(bs, fp);
         216               0 : }
         217                 :
         218                 : void
         219               0 : JS_DumpHistogram(JSBasicStats *bs, FILE *fp)
         220                 : {
         221                 :     unsigned bin;
         222                 :     uint32_t cnt, max;
         223                 :     double sum, mean;
         224                 :
         225               0 :     for (bin = 0, max = 0, sum = 0; bin <= 10; bin++) {
         226               0 :         cnt = bs->hist[bin];
         227               0 :         if (max < cnt)
         228               0 :             max = cnt;
         229               0 :         sum += cnt;
         230                 :     }
         231               0 :     mean = sum / cnt;
         232               0 :     for (bin = 0; bin <= 10; bin++) {
         233               0 :         unsigned val = BinToVal(bs->logscale, bin);
         234               0 :         unsigned end = (bin == 10) ? 0 : BinToVal(bs->logscale, bin + 1);
         235               0 :         cnt = bs->hist[bin];
         236               0 :         if (val + 1 == end)
         237               0 :             fprintf(fp, "        [%6u]", val);
         238               0 :         else if (end != 0)
         239               0 :             fprintf(fp, "[%6u, %6u]", val, end - 1);
         240                 :         else
         241               0 :             fprintf(fp, "[%6u,   +inf]", val);
         242               0 :         fprintf(fp, ": %8u ", cnt);
         243               0 :         if (cnt != 0) {
         244               0 :             if (max > 1e6 && mean > 1e3)
         245               0 :                 cnt = uint32_t(ceil(log10((double) cnt)));
         246               0 :             else if (max > 16 && mean > 8)
         247               0 :                 cnt = JS_CEILING_LOG2W(cnt);
         248               0 :             for (unsigned i = 0; i < cnt; i++)
         249               0 :                 putc('*', fp);
         250                 :         }
         251               0 :         putc('\n', fp);
         252                 :     }
         253               0 : }
         254                 :
         255                 : #endif /* JS_BASIC_STATS */

Generated by: LCOV version 1.7