X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/ba379fdc102753d6be2c4d937058fe40257329fe..ef99ff287df9046eb88937225e0554eabb00e33c:/bytecode/SamplingTool.cpp diff --git a/bytecode/SamplingTool.cpp b/bytecode/SamplingTool.cpp index 8651723..12dbf24 100644 --- a/bytecode/SamplingTool.cpp +++ b/bytecode/SamplingTool.cpp @@ -10,7 +10,7 @@ * 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 + * 3. Neither the name of Apple 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. * @@ -32,8 +32,9 @@ #include "CodeBlock.h" #include "Interpreter.h" #include "Opcode.h" +#include "JSCInlines.h" -#if !PLATFORM(WIN_OS) +#if !OS(WINDOWS) #include #endif @@ -43,7 +44,7 @@ namespace JSC { void SamplingFlags::sample() { - uint32_t mask = 1 << 31; + uint32_t mask = static_cast(1 << 31); unsigned index; for (index = 0; index < 32; ++index) { @@ -67,14 +68,14 @@ void SamplingFlags::stop() total += s_flagCounts[i]; if (total) { - printf("\nSamplingFlags: sample counts with flags set: (%lld total)\n", total); + dataLogF("\nSamplingFlags: sample counts with flags set: (%lld total)\n", total); for (unsigned i = 0; i <= 32; ++i) { if (s_flagCounts[i]) - printf(" [ %02d ] : %lld\t\t(%03.2f%%)\n", i, s_flagCounts[i], (100.0 * s_flagCounts[i]) / total); + dataLogF(" [ %02d ] : %lld\t\t(%03.2f%%)\n", i, s_flagCounts[i], (100.0 * s_flagCounts[i]) / total); } - printf("\n"); + dataLogF("\n"); } else - printf("\nSamplingFlags: no samples.\n\n"); + dataLogF("\nSamplingFlags: no samples.\n\n"); } uint64_t SamplingFlags::s_flagCounts[33]; @@ -83,6 +84,93 @@ void SamplingFlags::start() {} void SamplingFlags::stop() {} #endif +#if ENABLE(SAMPLING_REGIONS) +volatile uintptr_t SamplingRegion::s_currentOrReserved; +Spectrum* SamplingRegion::s_spectrum; +unsigned long SamplingRegion::s_noneOfTheAbove; +unsigned SamplingRegion::s_numberOfSamplesSinceDump; + +SamplingRegion::Locker::Locker() +{ + uintptr_t previous; + while (true) { + previous = s_currentOrReserved; + if (previous & 1) { +#if OS(UNIX) + sched_yield(); +#endif + continue; + } + if (WTF::weakCompareAndSwapUIntPtr(&s_currentOrReserved, previous, previous | 1)) + break; + } +} + +SamplingRegion::Locker::~Locker() +{ + // We don't need the CAS, but we do it out of an + // abundance of caution (and because it gives us a memory fence, which is + // never bad). + uintptr_t previous; + do { + previous = s_currentOrReserved; + } while (!WTF::weakCompareAndSwapUIntPtr(&s_currentOrReserved, previous, previous & ~1)); +} + +void SamplingRegion::sample() +{ + // Make sure we lock s_current. + Locker locker; + + // Create a spectrum if we don't have one already. + if (!s_spectrum) + s_spectrum = new Spectrum(); + + ASSERT(s_currentOrReserved & 1); + + // Walk the region stack, and record each region we see. + SamplingRegion* region = bitwise_cast(s_currentOrReserved & ~1); + if (region) { + for (; region; region = region->m_previous) + s_spectrum->add(region->m_name); + } else + s_noneOfTheAbove++; + + if (s_numberOfSamplesSinceDump++ == SamplingThread::s_hertz) { + s_numberOfSamplesSinceDump = 0; + dumpInternal(); + } +} + +void SamplingRegion::dump() +{ + Locker locker; + + dumpInternal(); +} + +void SamplingRegion::dumpInternal() +{ + if (!s_spectrum) { + dataLogF("\nSamplingRegion: was never sampled.\n\n"); + return; + } + + Vector::KeyAndCount> list = s_spectrum->buildList(); + + unsigned long total = s_noneOfTheAbove; + for (unsigned i = list.size(); i--;) + total += list[i].count; + + dataLogF("\nSamplingRegion: sample counts for regions: (%lu samples)\n", total); + + for (unsigned i = list.size(); i--;) + dataLogF(" %3.2lf%% %s\n", (100.0 * list[i].count) / total, list[i].key); +} +#else // ENABLE(SAMPLING_REGIONS) +void SamplingRegion::dump() { } +#endif // ENABLE(SAMPLING_REGIONS) + /* Start with flag 16 set. By doing this the monitoring of lower valued flags will be masked out @@ -91,7 +179,7 @@ void SamplingFlags::stop() {} uint32_t SamplingFlags::s_flags = 1 << 15; -#if PLATFORM(WIN_OS) +#if OS(WINDOWS) static void sleepForMicroseconds(unsigned us) { @@ -123,7 +211,7 @@ bool SamplingThread::s_running = false; unsigned SamplingThread::s_hertz = 10000; ThreadIdentifier SamplingThread::s_samplingThread; -void* SamplingThread::threadStartFunc(void*) +void SamplingThread::threadStartFunc(void*) { while (s_running) { sleepForMicroseconds(hertz2us(s_hertz)); @@ -131,12 +219,13 @@ void* SamplingThread::threadStartFunc(void*) #if ENABLE(SAMPLING_FLAGS) SamplingFlags::sample(); #endif +#if ENABLE(SAMPLING_REGIONS) + SamplingRegion::sample(); +#endif #if ENABLE(OPCODE_SAMPLING) SamplingTool::sample(); #endif } - - return 0; } @@ -153,11 +242,11 @@ void SamplingThread::stop() { ASSERT(s_running); s_running = false; - waitForThreadCompletion(s_samplingThread, 0); + waitForThreadCompletion(s_samplingThread); } -void ScopeSampleRecord::sample(CodeBlock* codeBlock, Instruction* vPC) +void ScriptSampleRecord::sample(CodeBlock* codeBlock, Instruction* vPC) { if (!m_samples) { m_size = codeBlock->instructions().size(); @@ -196,8 +285,8 @@ void SamplingTool::doRun() #if ENABLE(CODEBLOCK_SAMPLING) if (CodeBlock* codeBlock = sample.codeBlock()) { - MutexLocker locker(m_scopeSampleMapMutex); - ScopeSampleRecord* record = m_scopeSampleMap->get(codeBlock->ownerNode()); + MutexLocker locker(m_scriptSampleMapMutex); + ScriptSampleRecord* record = m_scopeSampleMap->get(codeBlock->ownerExecutable()); ASSERT(record); record->sample(codeBlock, sample.vPC()); } @@ -209,13 +298,14 @@ void SamplingTool::sample() s_samplingTool->doRun(); } -void SamplingTool::notifyOfScope(ScopeNode* scope) +void SamplingTool::notifyOfScope(VM& vm, ScriptExecutable* script) { #if ENABLE(CODEBLOCK_SAMPLING) - MutexLocker locker(m_scopeSampleMapMutex); - m_scopeSampleMap->set(scope, new ScopeSampleRecord(scope)); + MutexLocker locker(m_scriptSampleMapMutex); + m_scopeSampleMap->set(script, adoptPtr(new ScriptSampleRecord(vm, script))); #else - UNUSED_PARAM(scope); + UNUSED_PARAM(vm); + UNUSED_PARAM(script); #endif } @@ -254,10 +344,10 @@ static int compareLineCountInfoSampling(const void* left, const void* right) return (leftLineCount->line > rightLineCount->line) ? 1 : (leftLineCount->line < rightLineCount->line) ? -1 : 0; } -static int compareScopeSampleRecords(const void* left, const void* right) +static int compareScriptSampleRecords(const void* left, const void* right) { - const ScopeSampleRecord* const leftValue = *static_cast(left); - const ScopeSampleRecord* const rightValue = *static_cast(right); + const ScriptSampleRecord* const leftValue = *static_cast(left); + const ScriptSampleRecord* const rightValue = *static_cast(right); return (leftValue->m_sampleCount < rightValue->m_sampleCount) ? 1 : (leftValue->m_sampleCount > rightValue->m_sampleCount) ? -1 : 0; } @@ -282,10 +372,10 @@ void SamplingTool::dump(ExecState* exec) // (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"); + dataLogF("\nBytecode samples [*]\n"); + dataLogF(" sample %% of %% of | cti cti %%\n"); + dataLogF("opcode count VM total | count of self\n"); + dataLogF("------------------------------------------------------- | ----------------\n"); for (int i = 0; i < numOpcodeIDs; ++i) { long long count = opcodeSampleInfo[i].count; @@ -300,76 +390,76 @@ void SamplingTool::dump(ExecState* exec) 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); + debugDebugPrintf("%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"); + dataLogF("\n[*] Samples inside host code are not charged to any Bytecode.\n\n"); + dataLogF("\tSamples inside VM:\t\t%lld / %lld (%.3f%%)\n", m_opcodeSampleCount, m_sampleCount, (static_cast(m_opcodeSampleCount) * 100) / m_sampleCount); + dataLogF("\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); + dataLogF("\tsample count:\tsamples inside this opcode\n"); + dataLogF("\t%% of VM:\tsample count / all opcode samples\n"); + dataLogF("\t%% of total:\tsample count / all samples\n"); + dataLogF("\t--------------\n"); + dataLogF("\tcti count:\tsamples inside a CTI function called by this opcode\n"); + dataLogF("\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); - ScopeSampleRecordMap::iterator iter = m_scopeSampleMap->begin(); + Vector codeBlockSamples(scopeCount); + ScriptSampleRecordMap::iterator iter = m_scopeSampleMap->begin(); for (int i = 0; i < scopeCount; ++i, ++iter) - codeBlockSamples[i] = iter->second; + codeBlockSamples[i] = iter->value.get(); - qsort(codeBlockSamples.begin(), scopeCount, sizeof(ScopeSampleRecord*), compareScopeSampleRecords); + qsort(codeBlockSamples.begin(), scopeCount, sizeof(ScriptSampleRecord*), compareScriptSampleRecords); // (4) Print data from 'codeBlockSamples' array. - printf("\nCodeBlock samples\n\n"); + dataLogF("\nCodeBlock samples\n\n"); for (int i = 0; i < scopeCount; ++i) { - ScopeSampleRecord* record = codeBlockSamples[i]; + ScriptSampleRecord* record = codeBlockSamples[i]; CodeBlock* codeBlock = record->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_scope->sourceURL().UTF8String().c_str(), codeBlock->lineNumberForBytecodeOffset(exec, 0), record->m_sampleCount, m_sampleCount, blockPercent); + dataLogF("#%d: %s:%d: %d / %lld (%.3f%%)\n", i + 1, record->m_executable->sourceURL().utf8().data(), codeBlock->lineNumberForBytecodeOffset(0), record->m_sampleCount, m_sampleCount, blockPercent); if (i < 10) { HashMap lineCounts; codeBlock->dump(exec); - printf(" Opcode and line number samples [*]\n\n"); + dataLogF(" Opcode and line number samples [*]\n\n"); for (unsigned op = 0; op < record->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); + dataLogF(" [% 4d] has sample count: % 4d\n", op, count); + unsigned line = codeBlock->lineNumberForBytecodeOffset(op); lineCounts.set(line, (lineCounts.contains(line) ? lineCounts.get(line) : 0) + count); } } - printf("\n"); + dataLogF("\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; + lineCountInfo[lineno].line = iter->key; + lineCountInfo[lineno].count = iter->value; } qsort(lineCountInfo.begin(), linesCount, sizeof(LineCountInfo), compareLineCountInfoSampling); for (lineno = 0; lineno < linesCount; ++lineno) { - printf(" Line #%d has sample count %d.\n", lineCountInfo[lineno].line, lineCountInfo[lineno].count); + dataLogF(" Line #%d has sample count %d.\n", lineCountInfo[lineno].line, lineCountInfo[lineno].count); } - printf("\n"); - printf(" [*] Samples inside host code are charged to the calling Bytecode.\n"); - printf(" Samples on a call / return boundary are not charged to a specific opcode or line.\n\n"); - printf(" Samples on a call / return boundary: %d / %d (%.3f%%)\n\n", record->m_sampleCount - record->m_opcodeSampleCount, record->m_sampleCount, (static_cast(record->m_sampleCount - record->m_opcodeSampleCount) * 100) / record->m_sampleCount); + dataLogF("\n"); + dataLogF(" [*] Samples inside host code are charged to the calling Bytecode.\n"); + dataLogF(" Samples on a call / return boundary are not charged to a specific opcode or line.\n\n"); + dataLogF(" Samples on a call / return boundary: %d / %d (%.3f%%)\n\n", record->m_sampleCount - record->m_opcodeSampleCount, record->m_sampleCount, (static_cast(record->m_sampleCount - record->m_opcodeSampleCount) * 100) / record->m_sampleCount); } } } @@ -386,21 +476,4 @@ 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