DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (777e60ca8853)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * vim: set ts=8 sts=4 et sw=4 tw=99:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "jit/PerfSpewer.h"

#if defined(__linux__)
# include <unistd.h>
#endif

#ifdef JS_ION_PERF
# include "jit/IonSpewer.h"
# include "jit/LinearScan.h"
# include "jit/LIR.h"
# include "jit/MIR.h"
# include "jit/MIRGraph.h"
#endif

// perf expects its data to be in a file /tmp/perf-PID.map, but for Android
// and B2G the map files are written to /data/local/tmp/perf-PID.map
//
// Except that Android 4.3 no longer allows the browser to write to /data/local/tmp/
// so also try /sdcard/.

#ifndef PERF_SPEW_DIR
# if defined(__ANDROID__)
#  define PERF_SPEW_DIR "/data/local/tmp/"
#  define PERF_SPEW_DIR_2 "/sdcard/"
# else
#  define PERF_SPEW_DIR "/tmp/"
# endif
#endif

using namespace js;
using namespace js::jit;

#define PERF_MODE_NONE  1
#define PERF_MODE_FUNC  2
#define PERF_MODE_BLOCK 3

#ifdef JS_ION_PERF

static uint32_t PerfMode = 0;

static bool PerfChecked = false;

static FILE* PerfFilePtr = nullptr;

#ifdef JS_THREADSAFE
# include "jslock.h"
static PRLock* PerfMutex;
#endif

static bool
openPerfMap(const char* dir)
{
    const ssize_t bufferSize = 256;
    char filenameBuffer[bufferSize];

    if (snprintf(filenameBuffer, bufferSize, "%sperf-%d.map", dir, getpid()) >= bufferSize)
        return false;

    JS_ASSERT(!PerfFilePtr);
    PerfFilePtr = fopen(filenameBuffer, "a");

    if (!PerfFilePtr)
        return false;

    return true;
}

void
js::jit::CheckPerf() {
    if (!PerfChecked) {
        const char* env = getenv("IONPERF");
        if (env == nullptr) {
            PerfMode = PERF_MODE_NONE;
            fprintf(stderr, "Warning: JIT perf reporting requires IONPERF set to \"block\" or \"func\". ");
            fprintf(stderr, "Perf mapping will be deactivated.\n");
        } else if (!strcmp(env, "none")) {
            PerfMode = PERF_MODE_NONE;
        } else if (!strcmp(env, "block")) {
            PerfMode = PERF_MODE_BLOCK;
        } else if (!strcmp(env, "func")) {
            PerfMode = PERF_MODE_FUNC;
        } else {
            fprintf(stderr, "Use IONPERF=func to record at function granularity\n");
            fprintf(stderr, "Use IONPERF=block to record at basic block granularity\n");
            fprintf(stderr, "\n");
            fprintf(stderr, "Be advised that using IONPERF will cause all scripts\n");
            fprintf(stderr, "to be leaked.\n");
            exit(0);
        }

        if (PerfMode != PERF_MODE_NONE) {
#ifdef JS_THREADSAFE
            PerfMutex = PR_NewLock();
            if (!PerfMutex)
                MOZ_CRASH();
#endif

            if (openPerfMap(PERF_SPEW_DIR)) {
                PerfChecked = true;
                return;
            }

#if defined(__ANDROID__)
            if (openPerfMap(PERF_SPEW_DIR_2)) {
                PerfChecked = true;
                return;
            }
#endif
            fprintf(stderr, "Failed to open perf map file.  Disabling IONPERF.\n");
            PerfMode = PERF_MODE_NONE;
        }
        PerfChecked = true;
    }
}

bool
js::jit::PerfBlockEnabled() {
    JS_ASSERT(PerfMode);
    return PerfMode == PERF_MODE_BLOCK;
}

bool
js::jit::PerfFuncEnabled() {
    JS_ASSERT(PerfMode);
    return PerfMode == PERF_MODE_FUNC;
}

static bool
lockPerfMap(void)
{
    if (!PerfEnabled())
        return false;

#ifdef JS_THREADSAFE
    PR_Lock(PerfMutex);
#endif

    JS_ASSERT(PerfFilePtr);
    return true;
}

static void
unlockPerfMap()
{
    JS_ASSERT(PerfFilePtr);
    fflush(PerfFilePtr);
#ifdef JS_THREADSAFE
    PR_Unlock(PerfMutex);
#endif
}

uint32_t PerfSpewer::nextFunctionIndex = 0;

bool
PerfSpewer::startBasicBlock(MBasicBlock* blk,
                            MacroAssembler& masm)
{
    if (!PerfBlockEnabled())
        return true;

    const char* filename = blk->info().script()->filename();
    unsigned lineNumber, columnNumber;
    if (blk->pc()) {
        lineNumber = PCToLineNumber(blk->info().script(),
                                    blk->pc(),
                                    &columnNumber);
    } else {
        lineNumber = 0;
        columnNumber = 0;
    }
    Record r(filename, lineNumber, columnNumber, blk->id());
    masm.bind(&r.start);
    return basicBlocks_.append(r);
}

bool
PerfSpewer::endBasicBlock(MacroAssembler& masm)
{
    if (!PerfBlockEnabled())
        return true;

    masm.bind(&basicBlocks_.back().end);
    return true;
}

bool
PerfSpewer::noteEndInlineCode(MacroAssembler& masm)
{
    if (!PerfBlockEnabled())
        return true;

    masm.bind(&endInlineCode);
    return true;
}

void
PerfSpewer::writeProfile(JSScript* script,
                         JitCode* code,
                         MacroAssembler& masm)
{
    if (PerfFuncEnabled()) {
        if (!lockPerfMap())
            return;

        uint32_t thisFunctionIndex = nextFunctionIndex++;

        size_t size = code->instructionsSize();
        if (size > 0) {
            fprintf(PerfFilePtr, "%zx %zx %s:%d: Func%02d\n",
                    reinterpret_cast<uintptr_t>(code->raw()),
                    size,
                    script->filename(),
                    script->lineno(),
                    thisFunctionIndex);
        }
        unlockPerfMap();
        return;
    }

    if (PerfBlockEnabled() && basicBlocks_.length() > 0) {
        if (!lockPerfMap())
            return;

        uint32_t thisFunctionIndex = nextFunctionIndex++;
        uintptr_t funcStart = uintptr_t(code->raw());
        uintptr_t funcEndInlineCode = funcStart + masm.actualOffset(endInlineCode.offset());
        uintptr_t funcEnd = funcStart + code->instructionsSize();

        // function begins with the prologue, which is located before the first basic block
        size_t prologueSize = masm.actualOffset(basicBlocks_[0].start.offset());

        if (prologueSize > 0) {
            fprintf(PerfFilePtr, "%zx %zx %s:%d: Func%02d-Prologue\n",
                    funcStart, prologueSize, script->filename(), script->lineno(), thisFunctionIndex);
        }

        uintptr_t cur = funcStart + prologueSize;
        for (uint32_t i = 0; i < basicBlocks_.length(); i++) {
            Record& r = basicBlocks_[i];

            uintptr_t blockStart = funcStart + masm.actualOffset(r.start.offset());
            uintptr_t blockEnd = funcStart + masm.actualOffset(r.end.offset());

            JS_ASSERT(cur <= blockStart);
            if (cur < blockStart) {
                fprintf(PerfFilePtr, "%zx %zx %s:%d: Func%02d-Block?\n",
                        static_cast<uintptr_t>(cur),
                        static_cast<uintptr_t>(blockStart - cur),
                        script->filename(), script->lineno(),
                        thisFunctionIndex);
            }
            cur = blockEnd;

            size_t size = blockEnd - blockStart;

            if (size > 0) {
                fprintf(PerfFilePtr, "%zx %zx %s:%d:%d: Func%02d-Block%d\n",
                        static_cast<uintptr_t>(blockStart), size,
                        r.filename, r.lineNumber, r.columnNumber,
                        thisFunctionIndex, r.id);
            }
        }

        JS_ASSERT(cur <= funcEndInlineCode);
        if (cur < funcEndInlineCode) {
            fprintf(PerfFilePtr, "%zx %zx %s:%d: Func%02d-Epilogue\n",
                    cur, funcEndInlineCode - cur,
                    script->filename(), script->lineno(),
                    thisFunctionIndex);
        }

        JS_ASSERT(funcEndInlineCode <= funcEnd);
        if (funcEndInlineCode < funcEnd) {
            fprintf(PerfFilePtr, "%zx %zx %s:%d: Func%02d-OOL\n",
                    funcEndInlineCode, funcEnd - funcEndInlineCode,
                    script->filename(), script->lineno(),
                    thisFunctionIndex);
        }

        unlockPerfMap();
        return;
    }
}

void
js::jit::writePerfSpewerBaselineProfile(JSScript* script, JitCode* code)
{
    if (!PerfEnabled())
        return;

    if (!lockPerfMap())
        return;

    size_t size = code->instructionsSize();
    if (size > 0) {
        fprintf(PerfFilePtr, "%zx %zx %s:%d: Baseline\n",
                reinterpret_cast<uintptr_t>(code->raw()),
                size, script->filename(), script->lineno());
    }

    unlockPerfMap();
}

void
js::jit::writePerfSpewerJitCodeProfile(JitCode* code, const char* msg)
{
    if (!code || !PerfEnabled())
        return;

    if (!lockPerfMap())
        return;

    size_t size = code->instructionsSize();
    if (size > 0) {
        fprintf(PerfFilePtr, "%zx %zx %s (%p 0x%zx)\n",
                reinterpret_cast<uintptr_t>(code->raw()),
                size, msg, code->raw(), size);
    }

    unlockPerfMap();
}

void
js::jit::writePerfSpewerAsmJSFunctionMap(uintptr_t base, uintptr_t size,
                                         const char* filename, unsigned lineno, unsigned colIndex,
                                         const char* funcName)
{
    if (!PerfFuncEnabled() || size == 0U)
        return;

    if (!lockPerfMap())
        return;

    fprintf(PerfFilePtr, "%zx %zx %s:%d:%d: Function %s\n", base, size, filename, lineno, colIndex, funcName);

    unlockPerfMap();
}

bool
AsmJSPerfSpewer::startBasicBlock(MBasicBlock* blk, MacroAssembler& masm)
{
    if (!PerfBlockEnabled())
        return true;

    Record r("", blk->lineno(), blk->columnIndex(), blk->id()); // filename is retrieved later
    masm.bind(&r.start);
    return basicBlocks_.append(r);
}

void
AsmJSPerfSpewer::noteBlocksOffsets()
{
    if (!PerfBlockEnabled())
        return;

    for (uint32_t i = 0; i < basicBlocks_.length(); i++) {
        Record& r = basicBlocks_[i];
        r.startOffset = r.start.offset();
        r.endOffset = r.end.offset();
    }
}

void
js::jit::writePerfSpewerAsmJSBlocksMap(uintptr_t baseAddress, size_t funcStartOffset,
                                       size_t funcEndInlineOffset, size_t funcSize,
                                       const char* filename, const char* funcName,
                                       const js::jit::BasicBlocksVector& basicBlocks)
{
    if (!PerfBlockEnabled() || basicBlocks.empty())
        return;

    if (!lockPerfMap())
        return;

    // function begins with the prologue, which is located before the first basic block
    size_t prologueSize = basicBlocks[0].startOffset - funcStartOffset;
    size_t cur = baseAddress + funcStartOffset + prologueSize;
    size_t funcEndInlineCode = baseAddress + funcEndInlineOffset;
    size_t funcEnd = baseAddress + funcStartOffset + funcSize;

    if (prologueSize > 0) {
        fprintf(PerfFilePtr, "%zx %zx %s: Function %s - Prologue\n",
                baseAddress + funcStartOffset, prologueSize, filename, funcName);
    }

    for (uint32_t i = 0; i < basicBlocks.length(); i++) {
        const Record& r = basicBlocks[i];

        size_t blockStart = baseAddress + r.startOffset;
        size_t blockEnd = baseAddress + r.endOffset;

        JS_ASSERT(cur <= blockStart);
        if (cur < blockStart) {
            fprintf(PerfFilePtr, "%zx %zx %s: Function %s - unknown block\n",
                    cur, blockStart - cur,
                    filename,
                    funcName);
        }
        cur = blockEnd;

        size_t size = blockEnd - blockStart;
        if (size > 0) {
            fprintf(PerfFilePtr, "%zx %zx %s:%d:%d: Function %s - Block %d\n",
                    blockStart, size,
                    filename, r.lineNumber, r.columnNumber,
                    funcName, r.id);
        }
    }

    JS_ASSERT(cur <= funcEndInlineCode);
    if (cur < funcEndInlineCode)
        fprintf(PerfFilePtr, "%zx %zx %s: Function %s - Epilogue\n",
                cur, funcEndInlineCode - cur, filename, funcName);

    JS_ASSERT(funcEndInlineCode <= funcEnd);
    if (funcEndInlineCode < funcEnd) {
        fprintf(PerfFilePtr, "%zx %zx %s: Function %s - OOL\n",
                funcEndInlineCode, funcEnd - funcEndInlineCode, filename, funcName);
    }

    unlockPerfMap();
}

void
js::jit::writePerfSpewerAsmJSEntriesAndExits(uintptr_t base, size_t size)
{
    if (size == 0)
        return;

    if (!lockPerfMap())
        return;

    fprintf(PerfFilePtr, "%zx %zx AsmJS Entries and Exits (0x%zx 0x%zx)\n", base, size, base, size);

    unlockPerfMap();
}

#endif // defined (JS_ION_PERF)