Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #ifndef V8_LOG_H_ |
| 29 | #define V8_LOG_H_ |
| 30 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 31 | #include "allocation.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 32 | #include "platform.h" |
| 33 | #include "log-utils.h" |
| 34 | |
| 35 | namespace v8 { |
| 36 | namespace internal { |
| 37 | |
| 38 | // Logger is used for collecting logging information from V8 during |
| 39 | // execution. The result is dumped to a file. |
| 40 | // |
| 41 | // Available command line flags: |
| 42 | // |
| 43 | // --log |
| 44 | // Minimal logging (no API, code, or GC sample events), default is off. |
| 45 | // |
| 46 | // --log-all |
| 47 | // Log all events to the file, default is off. This is the same as combining |
| 48 | // --log-api, --log-code, --log-gc, and --log-regexp. |
| 49 | // |
| 50 | // --log-api |
| 51 | // Log API events to the logfile, default is off. --log-api implies --log. |
| 52 | // |
| 53 | // --log-code |
| 54 | // Log code (create, move, and delete) events to the logfile, default is off. |
| 55 | // --log-code implies --log. |
| 56 | // |
| 57 | // --log-gc |
| 58 | // Log GC heap samples after each GC that can be processed by hp2ps, default |
| 59 | // is off. --log-gc implies --log. |
| 60 | // |
| 61 | // --log-regexp |
| 62 | // Log creation and use of regular expressions, Default is off. |
| 63 | // --log-regexp implies --log. |
| 64 | // |
| 65 | // --logfile <filename> |
| 66 | // Specify the name of the logfile, default is "v8.log". |
| 67 | // |
| 68 | // --prof |
| 69 | // Collect statistical profiling information (ticks), default is off. The |
| 70 | // tick profiler requires code events, so --prof implies --log-code. |
| 71 | |
| 72 | // Forward declarations. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 73 | class HashMap; |
| 74 | class LogMessageBuilder; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 75 | class Profiler; |
| 76 | class Semaphore; |
| 77 | class SlidingStateWindow; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 78 | class Ticker; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 79 | |
| 80 | #undef LOG |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 81 | #define LOG(isolate, Call) \ |
| 82 | do { \ |
| 83 | v8::internal::Logger* logger = \ |
| 84 | (isolate)->logger(); \ |
| 85 | if (logger->is_logging()) \ |
| 86 | logger->Call; \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 87 | } while (false) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 88 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 89 | #define LOG_EVENTS_AND_TAGS_LIST(V) \ |
| 90 | V(CODE_CREATION_EVENT, "code-creation") \ |
| 91 | V(CODE_MOVE_EVENT, "code-move") \ |
| 92 | V(CODE_DELETE_EVENT, "code-delete") \ |
| 93 | V(CODE_MOVING_GC, "code-moving-gc") \ |
| 94 | V(SHARED_FUNC_MOVE_EVENT, "sfi-move") \ |
| 95 | V(SNAPSHOT_POSITION_EVENT, "snapshot-pos") \ |
| 96 | V(SNAPSHOT_CODE_NAME_EVENT, "snapshot-code-name") \ |
| 97 | V(TICK_EVENT, "tick") \ |
| 98 | V(REPEAT_META_EVENT, "repeat") \ |
| 99 | V(BUILTIN_TAG, "Builtin") \ |
| 100 | V(CALL_DEBUG_BREAK_TAG, "CallDebugBreak") \ |
| 101 | V(CALL_DEBUG_PREPARE_STEP_IN_TAG, "CallDebugPrepareStepIn") \ |
| 102 | V(CALL_IC_TAG, "CallIC") \ |
| 103 | V(CALL_INITIALIZE_TAG, "CallInitialize") \ |
| 104 | V(CALL_MEGAMORPHIC_TAG, "CallMegamorphic") \ |
| 105 | V(CALL_MISS_TAG, "CallMiss") \ |
| 106 | V(CALL_NORMAL_TAG, "CallNormal") \ |
| 107 | V(CALL_PRE_MONOMORPHIC_TAG, "CallPreMonomorphic") \ |
| 108 | V(KEYED_CALL_DEBUG_BREAK_TAG, "KeyedCallDebugBreak") \ |
| 109 | V(KEYED_CALL_DEBUG_PREPARE_STEP_IN_TAG, \ |
| 110 | "KeyedCallDebugPrepareStepIn") \ |
| 111 | V(KEYED_CALL_IC_TAG, "KeyedCallIC") \ |
| 112 | V(KEYED_CALL_INITIALIZE_TAG, "KeyedCallInitialize") \ |
| 113 | V(KEYED_CALL_MEGAMORPHIC_TAG, "KeyedCallMegamorphic") \ |
| 114 | V(KEYED_CALL_MISS_TAG, "KeyedCallMiss") \ |
| 115 | V(KEYED_CALL_NORMAL_TAG, "KeyedCallNormal") \ |
| 116 | V(KEYED_CALL_PRE_MONOMORPHIC_TAG, "KeyedCallPreMonomorphic") \ |
| 117 | V(CALLBACK_TAG, "Callback") \ |
| 118 | V(EVAL_TAG, "Eval") \ |
| 119 | V(FUNCTION_TAG, "Function") \ |
| 120 | V(KEYED_LOAD_IC_TAG, "KeyedLoadIC") \ |
| 121 | V(KEYED_LOAD_MEGAMORPHIC_IC_TAG, "KeyedLoadMegamorphicIC") \ |
| 122 | V(KEYED_EXTERNAL_ARRAY_LOAD_IC_TAG, "KeyedExternalArrayLoadIC") \ |
| 123 | V(KEYED_STORE_IC_TAG, "KeyedStoreIC") \ |
| 124 | V(KEYED_STORE_MEGAMORPHIC_IC_TAG, "KeyedStoreMegamorphicIC") \ |
| 125 | V(KEYED_EXTERNAL_ARRAY_STORE_IC_TAG, "KeyedExternalArrayStoreIC") \ |
| 126 | V(LAZY_COMPILE_TAG, "LazyCompile") \ |
| 127 | V(LOAD_IC_TAG, "LoadIC") \ |
| 128 | V(REG_EXP_TAG, "RegExp") \ |
| 129 | V(SCRIPT_TAG, "Script") \ |
| 130 | V(STORE_IC_TAG, "StoreIC") \ |
| 131 | V(STUB_TAG, "Stub") \ |
| 132 | V(NATIVE_FUNCTION_TAG, "Function") \ |
| 133 | V(NATIVE_LAZY_COMPILE_TAG, "LazyCompile") \ |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 134 | V(NATIVE_SCRIPT_TAG, "Script") |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 135 | // Note that 'NATIVE_' cases for functions and scripts are mapped onto |
| 136 | // original tags when writing to the log. |
| 137 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 138 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 139 | class Sampler; |
| 140 | |
| 141 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 142 | class Logger { |
| 143 | public: |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 144 | #define DECLARE_ENUM(enum_item, ignore) enum_item, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 145 | enum LogEventsAndTags { |
| 146 | LOG_EVENTS_AND_TAGS_LIST(DECLARE_ENUM) |
| 147 | NUMBER_OF_LOG_EVENTS |
| 148 | }; |
| 149 | #undef DECLARE_ENUM |
| 150 | |
| 151 | // Acquires resources for logging if the right flags are set. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 152 | bool Setup(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 153 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 154 | void EnsureTickerStarted(); |
| 155 | void EnsureTickerStopped(); |
| 156 | |
| 157 | Sampler* sampler(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 158 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 159 | // Frees resources acquired in Setup. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 160 | // When a temporary file is used for the log, returns its stream descriptor, |
| 161 | // leaving the file open. |
| 162 | FILE* TearDown(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 163 | |
| 164 | // Enable the computation of a sliding window of states. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 165 | void EnableSlidingStateWindow(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 166 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 167 | // Emits an event with a string value -> (name, value). |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 168 | void StringEvent(const char* name, const char* value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 169 | |
| 170 | // Emits an event with an int value -> (name, value). |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 171 | void IntEvent(const char* name, int value); |
| 172 | void IntPtrTEvent(const char* name, intptr_t value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 173 | |
| 174 | // Emits an event with an handle value -> (name, location). |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 175 | void HandleEvent(const char* name, Object** location); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 176 | |
| 177 | // Emits memory management events for C allocated structures. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 178 | void NewEvent(const char* name, void* object, size_t size); |
| 179 | void DeleteEvent(const char* name, void* object); |
| 180 | |
| 181 | // Static versions of the above, operate on current isolate's logger. |
| 182 | // Used in TRACK_MEMORY(TypeName) defined in globals.h |
| 183 | static void NewEventStatic(const char* name, void* object, size_t size); |
| 184 | static void DeleteEventStatic(const char* name, void* object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 185 | |
| 186 | // Emits an event with a tag, and some resource usage information. |
| 187 | // -> (name, tag, <rusage information>). |
| 188 | // Currently, the resource usage information is a process time stamp |
| 189 | // and a real time timestamp. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 190 | void ResourceEvent(const char* name, const char* tag); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 191 | |
| 192 | // Emits an event that an undefined property was read from an |
| 193 | // object. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 194 | void SuspectReadEvent(String* name, Object* obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 195 | |
| 196 | // Emits an event when a message is put on or read from a debugging queue. |
| 197 | // DebugTag lets us put a call-site specific label on the event. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 198 | void DebugTag(const char* call_site_tag); |
| 199 | void DebugEvent(const char* event_type, Vector<uint16_t> parameter); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 200 | |
| 201 | |
| 202 | // ==== Events logged by --log-api. ==== |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 203 | void ApiNamedSecurityCheck(Object* key); |
| 204 | void ApiIndexedSecurityCheck(uint32_t index); |
| 205 | void ApiNamedPropertyAccess(const char* tag, JSObject* holder, Object* name); |
| 206 | void ApiIndexedPropertyAccess(const char* tag, |
| 207 | JSObject* holder, |
| 208 | uint32_t index); |
| 209 | void ApiObjectAccess(const char* tag, JSObject* obj); |
| 210 | void ApiEntryCall(const char* name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 211 | |
| 212 | |
| 213 | // ==== Events logged by --log-code. ==== |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 214 | // Emits a code event for a callback function. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 215 | void CallbackEvent(String* name, Address entry_point); |
| 216 | void GetterCallbackEvent(String* name, Address entry_point); |
| 217 | void SetterCallbackEvent(String* name, Address entry_point); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 218 | // Emits a code create event. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 219 | void CodeCreateEvent(LogEventsAndTags tag, |
| 220 | Code* code, const char* source); |
| 221 | void CodeCreateEvent(LogEventsAndTags tag, |
| 222 | Code* code, String* name); |
| 223 | void CodeCreateEvent(LogEventsAndTags tag, |
| 224 | Code* code, |
| 225 | SharedFunctionInfo* shared, |
| 226 | String* name); |
| 227 | void CodeCreateEvent(LogEventsAndTags tag, |
| 228 | Code* code, |
| 229 | SharedFunctionInfo* shared, |
| 230 | String* source, int line); |
| 231 | void CodeCreateEvent(LogEventsAndTags tag, Code* code, int args_count); |
| 232 | void CodeMovingGCEvent(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 233 | // Emits a code create event for a RegExp. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 234 | void RegExpCodeCreateEvent(Code* code, String* source); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 235 | // Emits a code move event. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 236 | void CodeMoveEvent(Address from, Address to); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 237 | // Emits a code delete event. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 238 | void CodeDeleteEvent(Address from); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 239 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 240 | void SharedFunctionInfoMoveEvent(Address from, Address to); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 241 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 242 | void SnapshotPositionEvent(Address addr, int pos); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 243 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 244 | // ==== Events logged by --log-gc. ==== |
| 245 | // Heap sampling events: start, end, and individual types. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 246 | void HeapSampleBeginEvent(const char* space, const char* kind); |
| 247 | void HeapSampleEndEvent(const char* space, const char* kind); |
| 248 | void HeapSampleItemEvent(const char* type, int number, int bytes); |
| 249 | void HeapSampleJSConstructorEvent(const char* constructor, |
| 250 | int number, int bytes); |
| 251 | void HeapSampleJSRetainersEvent(const char* constructor, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 252 | const char* event); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 253 | void HeapSampleJSProducerEvent(const char* constructor, |
| 254 | Address* stack); |
| 255 | void HeapSampleStats(const char* space, const char* kind, |
| 256 | intptr_t capacity, intptr_t used); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 257 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 258 | void SharedLibraryEvent(const char* library_path, |
| 259 | uintptr_t start, |
| 260 | uintptr_t end); |
| 261 | void SharedLibraryEvent(const wchar_t* library_path, |
| 262 | uintptr_t start, |
| 263 | uintptr_t end); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 264 | |
| 265 | // ==== Events logged by --log-regexp ==== |
| 266 | // Regexp compilation and execution events. |
| 267 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 268 | void RegExpCompileEvent(Handle<JSRegExp> regexp, bool in_cache); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 269 | |
| 270 | // Log an event reported from generated code |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 271 | void LogRuntime(Vector<const char> format, JSArray* args); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 272 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 273 | bool is_logging() { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 274 | return logging_nesting_ > 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | // Pause/Resume collection of profiling data. |
| 278 | // When data collection is paused, CPU Tick events are discarded until |
| 279 | // data collection is Resumed. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 280 | void PauseProfiler(); |
| 281 | void ResumeProfiler(); |
| 282 | bool IsProfilerPaused(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 283 | |
| 284 | // Logs all compiled functions found in the heap. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 285 | void LogCompiledFunctions(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 286 | // Logs all accessor callbacks found in the heap. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 287 | void LogAccessorCallbacks(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 288 | // Used for logging stubs found in the snapshot. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 289 | void LogCodeObjects(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 290 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 291 | // Converts tag to a corresponding NATIVE_... if the script is native. |
| 292 | INLINE(static LogEventsAndTags ToNativeByScript(LogEventsAndTags, Script*)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 293 | |
| 294 | // Profiler's sampling interval (in milliseconds). |
Ben Murdoch | 7549248 | 2011-12-01 11:18:12 +0000 | [diff] [blame] | 295 | #if defined(ANDROID) |
| 296 | // Phones and tablets have processors that are much slower than desktop |
| 297 | // and laptop computers for which current heuristics are tuned. |
| 298 | static const int kSamplingIntervalMs = 5; |
| 299 | #else |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 300 | static const int kSamplingIntervalMs = 1; |
Ben Murdoch | 7549248 | 2011-12-01 11:18:12 +0000 | [diff] [blame] | 301 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 302 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 303 | // Callback from Log, stops profiling in case of insufficient resources. |
| 304 | void LogFailure(); |
| 305 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 306 | private: |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 307 | class NameBuffer; |
| 308 | class NameMap; |
| 309 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 310 | Logger(); |
| 311 | ~Logger(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 312 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 313 | // Emits the profiler's first message. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 314 | void ProfilerBeginEvent(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 315 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 316 | // Emits callback event messages. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 317 | void CallbackEventInternal(const char* prefix, |
| 318 | const char* name, |
| 319 | Address entry_point); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 320 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 321 | // Internal configurable move event. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 322 | void MoveEventInternal(LogEventsAndTags event, Address from, Address to); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 323 | |
| 324 | // Internal configurable move event. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 325 | void DeleteEventInternal(LogEventsAndTags event, Address from); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 326 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 327 | // Emits the source code of a regexp. Used by regexp events. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 328 | void LogRegExpSource(Handle<JSRegExp> regexp); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 329 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 330 | // Used for logging stubs found in the snapshot. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 331 | void LogCodeObject(Object* code_object); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 332 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 333 | // Emits general information about generated code. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 334 | void LogCodeInfo(); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 335 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 336 | void RegisterSnapshotCodeName(Code* code, const char* name, int name_size); |
| 337 | |
| 338 | // Low-level logging support. |
| 339 | |
| 340 | void LowLevelCodeCreateEvent(Code* code, const char* name, int name_size); |
| 341 | |
| 342 | void LowLevelCodeMoveEvent(Address from, Address to); |
| 343 | |
| 344 | void LowLevelCodeDeleteEvent(Address from); |
| 345 | |
| 346 | void LowLevelSnapshotPositionEvent(Address addr, int pos); |
| 347 | |
| 348 | void LowLevelLogWriteBytes(const char* bytes, int size); |
| 349 | |
| 350 | template <typename T> |
| 351 | void LowLevelLogWriteStruct(const T& s) { |
| 352 | char tag = T::kTag; |
| 353 | LowLevelLogWriteBytes(reinterpret_cast<const char*>(&tag), sizeof(tag)); |
| 354 | LowLevelLogWriteBytes(reinterpret_cast<const char*>(&s), sizeof(s)); |
| 355 | } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 356 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 357 | // Emits a profiler tick event. Used by the profiler thread. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 358 | void TickEvent(TickSample* sample, bool overflow); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 359 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 360 | void ApiEvent(const char* name, ...); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 361 | |
| 362 | // Logs a StringEvent regardless of whether FLAG_log is true. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 363 | void UncheckedStringEvent(const char* name, const char* value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 364 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 365 | // Logs an IntEvent regardless of whether FLAG_log is true. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 366 | void UncheckedIntEvent(const char* name, int value); |
| 367 | void UncheckedIntPtrTEvent(const char* name, intptr_t value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 368 | |
| 369 | // Returns whether profiler's sampler is active. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 370 | bool IsProfilerSamplerActive(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 371 | |
| 372 | // The sampler used by the profiler and the sliding state window. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 373 | Ticker* ticker_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 374 | |
| 375 | // When the statistical profile is active, profiler_ |
| 376 | // points to a Profiler, that handles collection |
| 377 | // of samples. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 378 | Profiler* profiler_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 379 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 380 | // SlidingStateWindow instance keeping a sliding window of the most |
| 381 | // recent VM states. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 382 | SlidingStateWindow* sliding_state_window_; |
| 383 | |
| 384 | // An array of log events names. |
| 385 | const char* const* log_events_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 386 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 387 | // Internal implementation classes with access to |
| 388 | // private members. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 389 | friend class EventLog; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 390 | friend class Isolate; |
| 391 | friend class LogMessageBuilder; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 392 | friend class TimeLog; |
| 393 | friend class Profiler; |
| 394 | friend class SlidingStateWindow; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 395 | friend class StackTracer; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 396 | friend class VMState; |
| 397 | |
| 398 | friend class LoggerTestHelper; |
| 399 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 400 | |
| 401 | int logging_nesting_; |
| 402 | int cpu_profiler_nesting_; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 403 | |
| 404 | Log* log_; |
| 405 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 406 | NameBuffer* name_buffer_; |
| 407 | |
| 408 | NameMap* address_to_name_map_; |
| 409 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 410 | // Guards against multiple calls to TearDown() that can happen in some tests. |
| 411 | // 'true' between Setup() and TearDown(). |
| 412 | bool is_initialized_; |
| 413 | |
| 414 | // Support for 'incremental addresses' in compressed logs: |
| 415 | // LogMessageBuilder::AppendAddress(Address addr) |
| 416 | Address last_address_; |
| 417 | // Logger::TickEvent(...) |
| 418 | Address prev_sp_; |
| 419 | Address prev_function_; |
| 420 | // Logger::MoveEventInternal(...) |
| 421 | Address prev_to_; |
| 422 | // Logger::FunctionCreateEvent(...) |
| 423 | Address prev_code_; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 424 | |
| 425 | friend class CpuProfiler; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 426 | }; |
| 427 | |
| 428 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 429 | // Process wide registry of samplers. |
| 430 | class SamplerRegistry : public AllStatic { |
| 431 | public: |
| 432 | enum State { |
| 433 | HAS_NO_SAMPLERS, |
| 434 | HAS_SAMPLERS, |
| 435 | HAS_CPU_PROFILING_SAMPLERS |
| 436 | }; |
| 437 | |
| 438 | typedef void (*VisitSampler)(Sampler*, void*); |
| 439 | |
| 440 | static State GetState(); |
| 441 | |
| 442 | // Iterates over all active samplers keeping the internal lock held. |
| 443 | // Returns whether there are any active samplers. |
| 444 | static bool IterateActiveSamplers(VisitSampler func, void* param); |
| 445 | |
| 446 | // Adds/Removes an active sampler. |
| 447 | static void AddActiveSampler(Sampler* sampler); |
| 448 | static void RemoveActiveSampler(Sampler* sampler); |
| 449 | |
| 450 | private: |
| 451 | static bool ActiveSamplersExist() { |
| 452 | return active_samplers_ != NULL && !active_samplers_->is_empty(); |
| 453 | } |
| 454 | |
| 455 | static Mutex* mutex_; // Protects the state below. |
| 456 | static List<Sampler*>* active_samplers_; |
| 457 | |
| 458 | DISALLOW_IMPLICIT_CONSTRUCTORS(SamplerRegistry); |
| 459 | }; |
| 460 | |
| 461 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 462 | // Class that extracts stack trace, used for profiling. |
| 463 | class StackTracer : public AllStatic { |
| 464 | public: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 465 | static void Trace(Isolate* isolate, TickSample* sample); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 466 | }; |
| 467 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 468 | } } // namespace v8::internal |
| 469 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 470 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 471 | #endif // V8_LOG_H_ |