/* * Copyright (C) 2008, 2009 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "config.h" #include "SamplingTool.h" #include "CodeBlock.h" #include "Interpreter.h" #include "Opcode.h" #if !OS(WINDOWS) #include #endif namespace JSC { #if ENABLE(SAMPLING_FLAGS) void SamplingFlags::sample() { uint32_t mask = static_cast(1 >= 1; } s_flagCounts[32 - index]++; } void SamplingFlags::start() { for (unsigned i = 0; i instructions().size(); m_samples = static_cast(calloc(m_size, sizeof(int))); m_codeBlock = codeBlock; } ++m_sampleCount; unsigned offest = vPC - codeBlock->instructions().begin(); // Since we don't read and write codeBlock and vPC atomically, this check // can fail if we sample mid op_call / op_ret. if (offest getOpcodeID(sample.vPC()[0].u.opcode); ++m_opcodeSampleCount; ++m_opcodeSamples[opcodeID]; if (sample.inCTIFunction()) m_opcodeSamplesInCTIFunctions[opcodeID]++; } #if ENABLE(CODEBLOCK_SAMPLING) if (CodeBlock* codeBlock = sample.codeBlock()) { MutexLocker locker(m_scriptSampleMapMutex); ScriptSampleRecord* record = m_scopeSampleMap->get(codeBlock->ownerExecutable()); ASSERT(record); record->sample(codeBlock, sample.vPC()); } #endif } void SamplingTool::sample() { s_samplingTool->doRun(); } void SamplingTool::notifyOfScope(ScriptExecutable* script) { #if ENABLE(CODEBLOCK_SAMPLING) MutexLocker locker(m_scriptSampleMapMutex); m_scopeSampleMap->set(script, new ScriptSampleRecord(script)); #else UNUSED_PARAM(script); #endif } void SamplingTool::setup() { s_samplingTool = this; } #if ENABLE(OPCODE_SAMPLING) struct OpcodeSampleInfo { OpcodeID opcode; long long count; long long countInCTIFunctions; }; struct LineCountInfo { unsigned line; unsigned count; }; static int compareOpcodeIndicesSampling(const void* left, const void* right) { const OpcodeSampleInfo* leftSampleInfo = reinterpret_cast(left); const OpcodeSampleInfo* rightSampleInfo = reinterpret_cast(right); return (leftSampleInfo->count count) ? 1 : (leftSampleInfo->count > rightSampleInfo->count) ? -1 : 0; } #if ENABLE(CODEBLOCK_SAMPLING) static int compareLineCountInfoSampling(const void* left, const void* right) { const LineCountInfo* leftLineCount = reinterpret_cast(left); const LineCountInfo* rightLineCount = reinterpret_cast(right); return (leftLineCount->line > rightLineCount->line) ? 1 : (leftLineCount->line line) ? -1 : 0; } static int compareScriptSampleRecords(const void* left, const void* right) { const ScriptSampleRecord* const leftValue = *static_cast(left); const ScriptSampleRecord* const rightValue = *static_cast(right); return (leftValue->m_sampleCount m_sampleCount) ? 1 : (leftValue->m_sampleCount > rightValue->m_sampleCount) ? -1 : 0; } #endif void SamplingTool::dump(ExecState* exec) { // Tidies up SunSpider output by removing short scripts - such a small number of samples would likely not be useful anyhow. if (m_sampleCount (i); opcodeSampleInfo[i].count = m_opcodeSamples[i]; opcodeSampleInfo[i].countInCTIFunctions = m_opcodeSamplesInCTIFunctions[i]; } qsort(opcodeSampleInfo, numOpcodeIDs, sizeof(OpcodeSampleInfo), compareOpcodeIndicesSampling); // (2) Print Opcode sampling results. printf("\nBytecode samples [*]\n"); printf(" sample %% of %% of | cti cti %%\n"); printf("opcode count VM total | count of self\n"); printf("------------------------------------------------------- | ----------------\n"); for (int i = 0; i (count) * 100) / m_opcodeSampleCount; double percentOfTotal = (static_cast(count) * 100) / m_sampleCount; long long countInCTIFunctions = opcodeSampleInfo[i].countInCTIFunctions; double percentInCTIFunctions = (static_cast(countInCTIFunctions) * 100) / count; fprintf(stdout, "%s:%s%-6lld %.3f%%\t%.3f%%\t | %-6lld %.3f%%\n", opcodeName, opcodePadding, count, percentOfVM, percentOfTotal, countInCTIFunctions, percentInCTIFunctions); } printf("\n[*] Samples inside host code are not charged to any Bytecode.\n\n"); printf("\tSamples inside VM:\t\t%lld / %lld (%.3f%%)\n", m_opcodeSampleCount, m_sampleCount, (static_cast(m_opcodeSampleCount) * 100) / m_sampleCount); printf("\tSamples inside host code:\t%lld / %lld (%.3f%%)\n\n", m_sampleCount - m_opcodeSampleCount, m_sampleCount, (static_cast(m_sampleCount - m_opcodeSampleCount) * 100) / m_sampleCount); printf("\tsample count:\tsamples inside this opcode\n"); printf("\t%% of VM:\tsample count / all opcode samples\n"); printf("\t%% of total:\tsample count / all samples\n"); printf("\t--------------\n"); printf("\tcti count:\tsamples inside a CTI function called by this opcode\n"); printf("\tcti %% of self:\tcti count / sample count\n"); #if ENABLE(CODEBLOCK_SAMPLING) // (3) Build and sort 'codeBlockSamples' array. int scopeCount = m_scopeSampleMap->size(); Vector codeBlockSamples(scopeCount); ScriptSampleRecordMap::iterator iter = m_scopeSampleMap->begin(); for (int i = 0; i second; qsort(codeBlockSamples.begin(), scopeCount, sizeof(ScriptSampleRecord*), compareScriptSampleRecords); // (4) Print data from 'codeBlockSamples' array. printf("\nCodeBlock samples\n\n"); for (int i = 0; i m_codeBlock; double blockPercent = (record->m_sampleCount * 100.0) / m_sampleCount; if (blockPercent >= 1) { //Instruction* code = codeBlock->instructions().begin(); printf("#%d: %s:%d: %d / %lld (%.3f%%)\n", i + 1, record->m_executable->sourceURL().ascii(), codeBlock->lineNumberForBytecodeOffset(exec, 0), record->m_sampleCount, m_sampleCount, blockPercent); if (i lineCounts; codeBlock->dump(exec); printf(" Opcode and line number samples [*]\n\n"); for (unsigned op = 0; op m_size; ++op) { int count = record->m_samples[op]; if (count) { printf(" [% 4d] has sample count: % 4d\n", op, count); unsigned line = codeBlock->lineNumberForBytecodeOffset(exec, op); lineCounts.set(line, (lineCounts.contains(line) ? lineCounts.get(line) : 0) + count); } } printf("\n"); int linesCount = lineCounts.size(); Vector lineCountInfo(linesCount); int lineno = 0; for (HashMap::iterator iter = lineCounts.begin(); iter != lineCounts.end(); ++iter, ++lineno) { lineCountInfo[lineno].line = iter->first; lineCountInfo[lineno].count = iter->second; } qsort(lineCountInfo.begin(), linesCount, sizeof(LineCountInfo), compareLineCountInfoSampling); for (lineno = 0; lineno m_sampleCount - record->m_opcodeSampleCount, record->m_sampleCount, (static_cast(record->m_sampleCount - record->m_opcodeSampleCount) * 100) / record->m_sampleCount); } } } #else UNUSED_PARAM(exec); #endif } #else void SamplingTool::dump(ExecState*) { } #endif void AbstractSamplingCounter::dump() { #if ENABLE(SAMPLING_COUNTERS) if (s_abstractSamplingCounterChain != &s_abstractSamplingCounterChainEnd) { printf("\nSampling Counter Values:\n"); for (AbstractSamplingCounter* currCounter = s_abstractSamplingCounterChain; (currCounter != &s_abstractSamplingCounterChainEnd); currCounter = currCounter->m_next) printf("\t%s\t: %lld\n", currCounter->m_name, currCounter->m_counter); printf("\n\n"); } s_completed = true; #endif } AbstractSamplingCounter AbstractSamplingCounter::s_abstractSamplingCounterChainEnd; AbstractSamplingCounter* AbstractSamplingCounter::s_abstractSamplingCounterChain = &s_abstractSamplingCounterChainEnd; bool AbstractSamplingCounter::s_completed = false; } // namespace JSC