#include "Interpreter.h"
#include "Opcode.h"
-#if !PLATFORM(WIN_OS)
+#if !OS(WINDOWS)
#include <unistd.h>
#endif
namespace JSC {
-void ScopeSampleRecord::sample(CodeBlock* codeBlock, Instruction* vPC)
+#if ENABLE(SAMPLING_FLAGS)
+
+void SamplingFlags::sample()
{
- if (!m_samples) {
- m_size = codeBlock->instructions().size();
- m_samples = static_cast<int*>(calloc(m_size, sizeof(int)));
- m_codeBlock = codeBlock;
+ uint32_t mask = static_cast<uint32_t>(1 << 31);
+ unsigned index;
+
+ for (index = 0; index < 32; ++index) {
+ if (mask & s_flags)
+ break;
+ mask >>= 1;
}
- ++m_sampleCount;
+ s_flagCounts[32 - index]++;
+}
- 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 < m_size) {
- m_samples[offest]++;
- m_opcodeSampleCount++;
+void SamplingFlags::start()
+{
+ for (unsigned i = 0; i <= 32; ++i)
+ s_flagCounts[i] = 0;
+}
+void SamplingFlags::stop()
+{
+ uint64_t total = 0;
+ for (unsigned i = 0; i <= 32; ++i)
+ total += s_flagCounts[i];
+
+ if (total) {
+ dataLogF("\nSamplingFlags: sample counts with flags set: (%lld total)\n", total);
+ for (unsigned i = 0; i <= 32; ++i) {
+ if (s_flagCounts[i])
+ dataLogF(" [ %02d ] : %lld\t\t(%03.2f%%)\n", i, s_flagCounts[i], (100.0 * s_flagCounts[i]) / total);
+ }
+ dataLogF("\n");
+ } else
+ dataLogF("\nSamplingFlags: no samples.\n\n");
+}
+uint64_t SamplingFlags::s_flagCounts[33];
+
+#else
+void SamplingFlags::start() {}
+void SamplingFlags::stop() {}
+#endif
+
+#if ENABLE(SAMPLING_REGIONS)
+volatile uintptr_t SamplingRegion::s_currentOrReserved;
+Spectrum<const char*>* 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<const char*>();
+
+ ASSERT(s_currentOrReserved & 1);
+
+ // Walk the region stack, and record each region we see.
+ SamplingRegion* region = bitwise_cast<SamplingRegion*>(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<Spectrum<const char*>::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
+ until flag 16 is explictly cleared.
+*/
+uint32_t SamplingFlags::s_flags = 1 << 15;
-#if PLATFORM(WIN_OS)
+
+#if OS(WINDOWS)
static void sleepForMicroseconds(unsigned us)
{
return 1000000 / hertz;
}
-void SamplingTool::run()
-{
- while (m_running) {
- sleepForMicroseconds(hertz2us(m_hertz));
-
- Sample sample(m_sample, m_codeBlock);
- ++m_sampleCount;
- if (sample.isNull())
- continue;
+SamplingTool* SamplingTool::s_samplingTool = 0;
- if (!sample.inHostFunction()) {
- unsigned opcodeID = m_interpreter->getOpcodeID(sample.vPC()[0].u.opcode);
- ++m_opcodeSampleCount;
- ++m_opcodeSamples[opcodeID];
+bool SamplingThread::s_running = false;
+unsigned SamplingThread::s_hertz = 10000;
+ThreadIdentifier SamplingThread::s_samplingThread;
- if (sample.inCTIFunction())
- m_opcodeSamplesInCTIFunctions[opcodeID]++;
- }
+void SamplingThread::threadStartFunc(void*)
+{
+ while (s_running) {
+ sleepForMicroseconds(hertz2us(s_hertz));
-#if ENABLE(CODEBLOCK_SAMPLING)
- MutexLocker locker(m_scopeSampleMapMutex);
- ScopeSampleRecord* record = m_scopeSampleMap->get(sample.codeBlock()->ownerNode());
- ASSERT(record);
- record->sample(sample.codeBlock(), sample.vPC());
+#if ENABLE(SAMPLING_FLAGS)
+ SamplingFlags::sample();
+#endif
+#if ENABLE(SAMPLING_REGIONS)
+ SamplingRegion::sample();
+#endif
+#if ENABLE(OPCODE_SAMPLING)
+ SamplingTool::sample();
#endif
}
}
-void* SamplingTool::threadStartFunc(void* samplingTool)
+
+void SamplingThread::start(unsigned hertz)
+{
+ ASSERT(!s_running);
+ s_running = true;
+ s_hertz = hertz;
+
+ s_samplingThread = createThread(threadStartFunc, 0, "JavaScriptCore::Sampler");
+}
+
+void SamplingThread::stop()
+{
+ ASSERT(s_running);
+ s_running = false;
+ waitForThreadCompletion(s_samplingThread);
+}
+
+
+void ScriptSampleRecord::sample(CodeBlock* codeBlock, Instruction* vPC)
{
- reinterpret_cast<SamplingTool*>(samplingTool)->run();
- return 0;
+ if (!m_samples) {
+ m_size = codeBlock->instructions().size();
+ m_samples = static_cast<int*>(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 < m_size) {
+ m_samples[offest]++;
+ m_opcodeSampleCount++;
+ }
}
-void SamplingTool::notifyOfScope(ScopeNode* scope)
+void SamplingTool::doRun()
{
- MutexLocker locker(m_scopeSampleMapMutex);
- m_scopeSampleMap->set(scope, new ScopeSampleRecord(scope));
+ Sample sample(m_sample, m_codeBlock);
+ ++m_sampleCount;
+
+ if (sample.isNull())
+ return;
+
+ if (!sample.inHostFunction()) {
+ unsigned opcodeID = m_interpreter->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::start(unsigned hertz)
+void SamplingTool::sample()
{
- ASSERT(!m_running);
- m_running = true;
- m_hertz = hertz;
+ s_samplingTool->doRun();
+}
- m_samplingThread = createThread(threadStartFunc, this, "JavaScriptCore::Sampler");
+void SamplingTool::notifyOfScope(VM& vm, ScriptExecutable* script)
+{
+#if ENABLE(CODEBLOCK_SAMPLING)
+ MutexLocker locker(m_scriptSampleMapMutex);
+ m_scopeSampleMap->set(script, adoptPtr(new ScriptSampleRecord(vm, script)));
+#else
+ UNUSED_PARAM(vm);
+ UNUSED_PARAM(script);
+#endif
}
-void SamplingTool::stop()
+void SamplingTool::setup()
{
- ASSERT(m_running);
- m_running = false;
- waitForThreadCompletion(m_samplingThread, 0);
+ s_samplingTool = this;
}
#if ENABLE(OPCODE_SAMPLING)
unsigned count;
};
-static int compareLineCountInfoSampling(const void* left, const void* right)
-{
- const LineCountInfo* leftLineCount = reinterpret_cast<const LineCountInfo*>(left);
- const LineCountInfo* rightLineCount = reinterpret_cast<const LineCountInfo*>(right);
-
- return (leftLineCount->line > rightLineCount->line) ? 1 : (leftLineCount->line < rightLineCount->line) ? -1 : 0;
-}
-
static int compareOpcodeIndicesSampling(const void* left, const void* right)
{
const OpcodeSampleInfo* leftSampleInfo = reinterpret_cast<const OpcodeSampleInfo*>(left);
return (leftSampleInfo->count < rightSampleInfo->count) ? 1 : (leftSampleInfo->count > rightSampleInfo->count) ? -1 : 0;
}
-static int compareScopeSampleRecords(const void* left, const void* right)
+#if ENABLE(CODEBLOCK_SAMPLING)
+static int compareLineCountInfoSampling(const void* left, const void* right)
+{
+ const LineCountInfo* leftLineCount = reinterpret_cast<const LineCountInfo*>(left);
+ const LineCountInfo* rightLineCount = reinterpret_cast<const LineCountInfo*>(right);
+
+ return (leftLineCount->line > rightLineCount->line) ? 1 : (leftLineCount->line < rightLineCount->line) ? -1 : 0;
+}
+
+static int compareScriptSampleRecords(const void* left, const void* right)
{
- const ScopeSampleRecord* const leftValue = *static_cast<const ScopeSampleRecord* const *>(left);
- const ScopeSampleRecord* const rightValue = *static_cast<const ScopeSampleRecord* const *>(right);
+ const ScriptSampleRecord* const leftValue = *static_cast<const ScriptSampleRecord* const *>(left);
+ const ScriptSampleRecord* const rightValue = *static_cast<const ScriptSampleRecord* const *>(right);
return (leftValue->m_sampleCount < rightValue->m_sampleCount) ? 1 : (leftValue->m_sampleCount > rightValue->m_sampleCount) ? -1 : 0;
}
+#endif
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;
double percentOfTotal = (static_cast<double>(count) * 100) / m_sampleCount;
long long countInCTIFunctions = opcodeSampleInfo[i].countInCTIFunctions;
double percentInCTIFunctions = (static_cast<double>(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<double>(m_opcodeSampleCount) * 100) / m_sampleCount);
- printf("\tSamples inside host code:\t%lld / %lld (%.3f%%)\n\n", m_sampleCount - m_opcodeSampleCount, m_sampleCount, (static_cast<double>(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<double>(m_opcodeSampleCount) * 100) / m_sampleCount);
+ dataLogF("\tSamples inside host code:\t%lld / %lld (%.3f%%)\n\n", m_sampleCount - m_opcodeSampleCount, m_sampleCount, (static_cast<double>(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<ScopeSampleRecord*> codeBlockSamples(scopeCount);
- ScopeSampleRecordMap::iterator iter = m_scopeSampleMap->begin();
+ Vector<ScriptSampleRecord*> 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<unsigned,unsigned> 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> lineCountInfo(linesCount);
int lineno = 0;
for (HashMap<unsigned,unsigned>::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<double>(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<double>(record->m_sampleCount - record->m_opcodeSampleCount) * 100) / record->m_sampleCount);
}
}
}
+#else
+ UNUSED_PARAM(exec);
+#endif
}
#else