1 // Copyright 2016 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef V8_CODE_EVENTS_H_ 6 #define V8_CODE_EVENTS_H_ 7 8 #include <unordered_set> 9 10 #include "src/base/platform/mutex.h" 11 #include "src/globals.h" 12 13 namespace v8 { 14 namespace internal { 15 16 class AbstractCode; 17 class Name; 18 class SharedFunctionInfo; 19 class String; 20 21 #define LOG_EVENTS_AND_TAGS_LIST(V) \ 22 V(CODE_CREATION_EVENT, "code-creation") \ 23 V(CODE_DISABLE_OPT_EVENT, "code-disable-optimization") \ 24 V(CODE_MOVE_EVENT, "code-move") \ 25 V(CODE_DELETE_EVENT, "code-delete") \ 26 V(CODE_MOVING_GC, "code-moving-gc") \ 27 V(SHARED_FUNC_MOVE_EVENT, "sfi-move") \ 28 V(SNAPSHOT_CODE_NAME_EVENT, "snapshot-code-name") \ 29 V(TICK_EVENT, "tick") \ 30 V(REPEAT_META_EVENT, "repeat") \ 31 V(BUILTIN_TAG, "Builtin") \ 32 V(CALL_DEBUG_BREAK_TAG, "CallDebugBreak") \ 33 V(CALL_DEBUG_PREPARE_STEP_IN_TAG, "CallDebugPrepareStepIn") \ 34 V(CALL_INITIALIZE_TAG, "CallInitialize") \ 35 V(CALL_MEGAMORPHIC_TAG, "CallMegamorphic") \ 36 V(CALL_MISS_TAG, "CallMiss") \ 37 V(CALL_NORMAL_TAG, "CallNormal") \ 38 V(LOAD_INITIALIZE_TAG, "LoadInitialize") \ 39 V(LOAD_MEGAMORPHIC_TAG, "LoadMegamorphic") \ 40 V(STORE_INITIALIZE_TAG, "StoreInitialize") \ 41 V(STORE_GENERIC_TAG, "StoreGeneric") \ 42 V(STORE_MEGAMORPHIC_TAG, "StoreMegamorphic") \ 43 V(KEYED_CALL_DEBUG_BREAK_TAG, "KeyedCallDebugBreak") \ 44 V(KEYED_CALL_DEBUG_PREPARE_STEP_IN_TAG, "KeyedCallDebugPrepareStepIn") \ 45 V(KEYED_CALL_INITIALIZE_TAG, "KeyedCallInitialize") \ 46 V(KEYED_CALL_MEGAMORPHIC_TAG, "KeyedCallMegamorphic") \ 47 V(KEYED_CALL_MISS_TAG, "KeyedCallMiss") \ 48 V(KEYED_CALL_NORMAL_TAG, "KeyedCallNormal") \ 49 V(CALLBACK_TAG, "Callback") \ 50 V(EVAL_TAG, "Eval") \ 51 V(FUNCTION_TAG, "Function") \ 52 V(HANDLER_TAG, "Handler") \ 53 V(BYTECODE_HANDLER_TAG, "BytecodeHandler") \ 54 V(KEYED_LOAD_IC_TAG, "KeyedLoadIC") \ 55 V(KEYED_LOAD_POLYMORPHIC_IC_TAG, "KeyedLoadPolymorphicIC") \ 56 V(KEYED_EXTERNAL_ARRAY_LOAD_IC_TAG, "KeyedExternalArrayLoadIC") \ 57 V(KEYED_STORE_IC_TAG, "KeyedStoreIC") \ 58 V(KEYED_STORE_POLYMORPHIC_IC_TAG, "KeyedStorePolymorphicIC") \ 59 V(KEYED_EXTERNAL_ARRAY_STORE_IC_TAG, "KeyedExternalArrayStoreIC") \ 60 V(LAZY_COMPILE_TAG, "LazyCompile") \ 61 V(CALL_IC_TAG, "CallIC") \ 62 V(LOAD_IC_TAG, "LoadIC") \ 63 V(LOAD_GLOBAL_IC_TAG, "LoadGlobalIC") \ 64 V(LOAD_POLYMORPHIC_IC_TAG, "LoadPolymorphicIC") \ 65 V(REG_EXP_TAG, "RegExp") \ 66 V(SCRIPT_TAG, "Script") \ 67 V(STORE_IC_TAG, "StoreIC") \ 68 V(STORE_POLYMORPHIC_IC_TAG, "StorePolymorphicIC") \ 69 V(STUB_TAG, "Stub") \ 70 V(NATIVE_FUNCTION_TAG, "Function") \ 71 V(NATIVE_LAZY_COMPILE_TAG, "LazyCompile") \ 72 V(NATIVE_SCRIPT_TAG, "Script") 73 // Note that 'NATIVE_' cases for functions and scripts are mapped onto 74 // original tags when writing to the log. 75 76 #define PROFILE(the_isolate, Call) (the_isolate)->code_event_dispatcher()->Call; 77 78 class CodeEventListener { 79 public: 80 #define DECLARE_ENUM(enum_item, _) enum_item, 81 enum LogEventsAndTags { 82 LOG_EVENTS_AND_TAGS_LIST(DECLARE_ENUM) NUMBER_OF_LOG_EVENTS 83 }; 84 #undef DECLARE_ENUM 85 ~CodeEventListener()86 virtual ~CodeEventListener() {} 87 88 virtual void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code, 89 const char* comment) = 0; 90 virtual void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code, 91 Name* name) = 0; 92 virtual void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code, 93 SharedFunctionInfo* shared, Name* source) = 0; 94 virtual void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code, 95 SharedFunctionInfo* shared, Name* source, 96 int line, int column) = 0; 97 virtual void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code, 98 int args_count) = 0; 99 virtual void CallbackEvent(Name* name, Address entry_point) = 0; 100 virtual void GetterCallbackEvent(Name* name, Address entry_point) = 0; 101 virtual void SetterCallbackEvent(Name* name, Address entry_point) = 0; 102 virtual void RegExpCodeCreateEvent(AbstractCode* code, String* source) = 0; 103 virtual void CodeMoveEvent(AbstractCode* from, Address to) = 0; 104 virtual void SharedFunctionInfoMoveEvent(Address from, Address to) = 0; 105 virtual void CodeMovingGCEvent() = 0; 106 virtual void CodeDisableOptEvent(AbstractCode* code, 107 SharedFunctionInfo* shared) = 0; 108 virtual void CodeDeoptEvent(Code* code, Address pc, int fp_to_sp_delta) = 0; 109 }; 110 111 class CodeEventDispatcher { 112 public: 113 using LogEventsAndTags = CodeEventListener::LogEventsAndTags; 114 CodeEventDispatcher()115 CodeEventDispatcher() {} 116 AddListener(CodeEventListener * listener)117 bool AddListener(CodeEventListener* listener) { 118 base::LockGuard<base::Mutex> guard(&mutex_); 119 return listeners_.insert(listener).second; 120 } RemoveListener(CodeEventListener * listener)121 void RemoveListener(CodeEventListener* listener) { 122 base::LockGuard<base::Mutex> guard(&mutex_); 123 listeners_.erase(listener); 124 } 125 126 #define CODE_EVENT_DISPATCH(code) \ 127 base::LockGuard<base::Mutex> guard(&mutex_); \ 128 for (auto it = listeners_.begin(); it != listeners_.end(); ++it) (*it)->code 129 CodeCreateEvent(LogEventsAndTags tag,AbstractCode * code,const char * comment)130 void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code, 131 const char* comment) { 132 CODE_EVENT_DISPATCH(CodeCreateEvent(tag, code, comment)); 133 } CodeCreateEvent(LogEventsAndTags tag,AbstractCode * code,Name * name)134 void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code, Name* name) { 135 CODE_EVENT_DISPATCH(CodeCreateEvent(tag, code, name)); 136 } CodeCreateEvent(LogEventsAndTags tag,AbstractCode * code,SharedFunctionInfo * shared,Name * name)137 void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code, 138 SharedFunctionInfo* shared, Name* name) { 139 CODE_EVENT_DISPATCH(CodeCreateEvent(tag, code, shared, name)); 140 } CodeCreateEvent(LogEventsAndTags tag,AbstractCode * code,SharedFunctionInfo * shared,Name * source,int line,int column)141 void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code, 142 SharedFunctionInfo* shared, Name* source, int line, 143 int column) { 144 CODE_EVENT_DISPATCH( 145 CodeCreateEvent(tag, code, shared, source, line, column)); 146 } CodeCreateEvent(LogEventsAndTags tag,AbstractCode * code,int args_count)147 void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code, 148 int args_count) { 149 CODE_EVENT_DISPATCH(CodeCreateEvent(tag, code, args_count)); 150 } CallbackEvent(Name * name,Address entry_point)151 void CallbackEvent(Name* name, Address entry_point) { 152 CODE_EVENT_DISPATCH(CallbackEvent(name, entry_point)); 153 } GetterCallbackEvent(Name * name,Address entry_point)154 void GetterCallbackEvent(Name* name, Address entry_point) { 155 CODE_EVENT_DISPATCH(GetterCallbackEvent(name, entry_point)); 156 } SetterCallbackEvent(Name * name,Address entry_point)157 void SetterCallbackEvent(Name* name, Address entry_point) { 158 CODE_EVENT_DISPATCH(SetterCallbackEvent(name, entry_point)); 159 } RegExpCodeCreateEvent(AbstractCode * code,String * source)160 void RegExpCodeCreateEvent(AbstractCode* code, String* source) { 161 CODE_EVENT_DISPATCH(RegExpCodeCreateEvent(code, source)); 162 } CodeMoveEvent(AbstractCode * from,Address to)163 void CodeMoveEvent(AbstractCode* from, Address to) { 164 CODE_EVENT_DISPATCH(CodeMoveEvent(from, to)); 165 } SharedFunctionInfoMoveEvent(Address from,Address to)166 void SharedFunctionInfoMoveEvent(Address from, Address to) { 167 CODE_EVENT_DISPATCH(SharedFunctionInfoMoveEvent(from, to)); 168 } CodeMovingGCEvent()169 void CodeMovingGCEvent() { CODE_EVENT_DISPATCH(CodeMovingGCEvent()); } CodeDisableOptEvent(AbstractCode * code,SharedFunctionInfo * shared)170 void CodeDisableOptEvent(AbstractCode* code, SharedFunctionInfo* shared) { 171 CODE_EVENT_DISPATCH(CodeDisableOptEvent(code, shared)); 172 } CodeDeoptEvent(Code * code,Address pc,int fp_to_sp_delta)173 void CodeDeoptEvent(Code* code, Address pc, int fp_to_sp_delta) { 174 CODE_EVENT_DISPATCH(CodeDeoptEvent(code, pc, fp_to_sp_delta)); 175 } 176 #undef CODE_EVENT_DISPATCH 177 178 private: 179 std::unordered_set<CodeEventListener*> listeners_; 180 base::Mutex mutex_; 181 182 DISALLOW_COPY_AND_ASSIGN(CodeEventDispatcher); 183 }; 184 185 } // namespace internal 186 } // namespace v8 187 188 #endif // V8_CODE_EVENTS_H_ 189