Changeset 35305 in webkit for trunk/JavaScriptCore/VM/Opcode.h


Ignore:
Timestamp:
Jul 23, 2008, 3:36:39 PM (17 years ago)
Author:
[email protected]
Message:

2008-07-23 Gavin Barraclough <[email protected]>

Reviewed by Geoff Garen.

Sampling tool to analyze cost of instruction execution and identify hot regions of JS code.
Enable Switches by setting SAMPLING_TOOL_ENABLED in Opcode.h.

  • JavaScriptCore.exp: Export symbols for Shell.cpp.
  • VM/Machine.cpp: Added sampling hooks.
  • VM/Machine.h: Machine contains a pointer to a sampler, when sampling.
  • VM/Opcode.cpp: Tool implementation.
  • VM/Opcode.h: Tool declaration.
  • kjs/Shell.cpp: Initialize the sampler, if enabled.
  • kjs/nodes.cpp: Added sampling hooks.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/VM/Opcode.h

    r34883 r35305  
    3232
    3333#include <wtf/Assertions.h>
     34#include <wtf/HashMap.h>
    3435
    3536namespace KJS {
    3637
     38#define SAMPLING_TOOL_ENABLED 0
    3739#define DUMP_OPCODE_STATS 0
    3840
     
    144146#endif
    145147
     148    class ExecState;
     149    class ScopeNode;
     150    class CodeBlock;
     151    struct Instruction;
     152
     153    struct ScopeSampleRecord
     154    {
     155        RefPtr<ScopeNode> m_scope;
     156        CodeBlock* m_codeBlock;
     157        int m_totalCount;
     158        int* m_vpcCounts;
     159        unsigned m_size;
     160       
     161        ScopeSampleRecord(ScopeNode* scope)
     162            : m_scope(scope)
     163            , m_codeBlock(0)
     164            , m_totalCount(0)
     165            , m_vpcCounts(0)
     166            , m_size(0)
     167        {
     168        }
     169       
     170        ~ScopeSampleRecord()
     171        {
     172            if (m_vpcCounts)
     173                free(m_vpcCounts);
     174        }
     175       
     176        void sample(CodeBlock* codeBlock, Instruction* vPC);
     177    };
     178
     179    typedef WTF::HashMap<ScopeNode*, ScopeSampleRecord*> ScopeSampleRecordMap;
     180
     181    class SamplingTool
     182    {
     183    public:
     184        SamplingTool()
     185            : m_running(false)
     186            , m_recordedCodeBlock(0)
     187            , m_recordedVPC(0)
     188            , m_totalSamples(0)
     189            , m_scopeSampleMap(new ScopeSampleRecordMap())
     190        {
     191        }
     192
     193        ~SamplingTool()
     194        {
     195            for (ScopeSampleRecordMap::iterator iter = m_scopeSampleMap->begin(); iter != m_scopeSampleMap->end(); ++iter)
     196                delete iter->second;
     197            delete m_scopeSampleMap;
     198        }
     199
     200        void start(unsigned hertz=1000);
     201        void stop();
     202        void dump(ExecState*);
     203
     204        void notifyOfScope(ScopeNode* scope);
     205
     206        void sample(CodeBlock* recordedCodeBlock, Instruction* recordedVPC)
     207        {
     208            m_recordedCodeBlock = recordedCodeBlock;
     209            m_recordedVPC = recordedVPC;
     210        }
     211
     212        void privateExecuteReturned()
     213        {
     214            m_recordedCodeBlock = 0;
     215            m_recordedVPC = 0;
     216        }
     217       
     218        void callingNativeFunction()
     219        {
     220            m_recordedCodeBlock = 0;
     221            m_recordedVPC = 0;
     222        }
     223       
     224    private:
     225        static void* threadStartFunc(void*);
     226        void run();
     227       
     228        // Sampling thread state.
     229        bool m_running;
     230        unsigned m_hertz;
     231        pthread_t m_samplingThread;
     232
     233        // State tracked by the main thread, used by the sampling thread.
     234        CodeBlock* m_recordedCodeBlock;
     235        Instruction* m_recordedVPC;
     236
     237        // Gathered sample data.
     238        long long m_totalSamples;
     239        ScopeSampleRecordMap* m_scopeSampleMap;
     240    };
     241
     242// SCOPENODE_ / MACHINE_ macros for use from within member methods on ScopeNode / Machine respectively.
     243#if SAMPLING_TOOL_ENABLED
     244#define SCOPENODE_SAMPLING_notifyOfScope(sampler) sampler->notifyOfScope(this)
     245#define MACHINE_SAMPLING_sample(codeBlock, vPC) m_sampler->sample(codeBlock, vPC)
     246#define MACHINE_SAMPLING_privateExecuteReturned() m_sampler->privateExecuteReturned()
     247#define MACHINE_SAMPLING_callingNativeFunction() m_sampler->callingNativeFunction()
     248#else
     249#define SCOPENODE_SAMPLING_notifyOfScope(sampler)
     250#define MACHINE_SAMPLING_sample(codeBlock, vPC)
     251#define MACHINE_SAMPLING_privateExecuteReturned()
     252#define MACHINE_SAMPLING_callingNativeFunction()
     253#endif
     254
     255
    146256#if DUMP_OPCODE_STATS
    147257
Note: See TracChangeset for help on using the changeset viewer.