ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 1 | // Copyright 2009 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +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 | #include <stdarg.h> |
| 29 | |
| 30 | #include "v8.h" |
| 31 | |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 32 | #include "bootstrapper.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 33 | #include "log.h" |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 34 | #include "macro-assembler.h" |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 35 | #include "serialize.h" |
| 36 | #include "string-stream.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 37 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 38 | namespace v8 { |
| 39 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 40 | |
| 41 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 42 | |
| 43 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 44 | // Sliding state window. Updates counters to keep track of the last |
| 45 | // window of kBufferSize states. This is useful to track where we |
| 46 | // spent our time. |
| 47 | // |
| 48 | class SlidingStateWindow { |
| 49 | public: |
| 50 | SlidingStateWindow(); |
| 51 | ~SlidingStateWindow(); |
| 52 | void AddState(StateTag state); |
| 53 | |
| 54 | private: |
| 55 | static const int kBufferSize = 256; |
| 56 | int current_index_; |
| 57 | bool is_full_; |
| 58 | byte buffer_[kBufferSize]; |
| 59 | |
| 60 | |
| 61 | void IncrementStateCounter(StateTag state) { |
| 62 | Counters::state_counters[state].Increment(); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | void DecrementStateCounter(StateTag state) { |
| 67 | Counters::state_counters[state].Decrement(); |
| 68 | } |
| 69 | }; |
| 70 | |
| 71 | |
| 72 | // |
| 73 | // The Profiler samples pc and sp values for the main thread. |
| 74 | // Each sample is appended to a circular buffer. |
| 75 | // An independent thread removes data and writes it to the log. |
| 76 | // This design minimizes the time spent in the sampler. |
| 77 | // |
| 78 | class Profiler: public Thread { |
| 79 | public: |
| 80 | Profiler(); |
| 81 | void Engage(); |
| 82 | void Disengage(); |
| 83 | |
| 84 | // Inserts collected profiling data into buffer. |
| 85 | void Insert(TickSample* sample) { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 86 | if (paused_) |
| 87 | return; |
| 88 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 89 | if (Succ(head_) == tail_) { |
| 90 | overflow_ = true; |
| 91 | } else { |
| 92 | buffer_[head_] = *sample; |
| 93 | head_ = Succ(head_); |
| 94 | buffer_semaphore_->Signal(); // Tell we have an element. |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // Waits for a signal and removes profiling data. |
| 99 | bool Remove(TickSample* sample) { |
| 100 | buffer_semaphore_->Wait(); // Wait for an element. |
| 101 | *sample = buffer_[tail_]; |
| 102 | bool result = overflow_; |
| 103 | tail_ = Succ(tail_); |
| 104 | overflow_ = false; |
| 105 | return result; |
| 106 | } |
| 107 | |
| 108 | void Run(); |
| 109 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 110 | // Pause and Resume TickSample data collection. |
| 111 | static bool paused() { return paused_; } |
| 112 | static void pause() { paused_ = true; } |
| 113 | static void resume() { paused_ = false; } |
| 114 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 115 | private: |
| 116 | // Returns the next index in the cyclic buffer. |
| 117 | int Succ(int index) { return (index + 1) % kBufferSize; } |
| 118 | |
| 119 | // Cyclic buffer for communicating profiling samples |
| 120 | // between the signal handler and the worker thread. |
| 121 | static const int kBufferSize = 128; |
| 122 | TickSample buffer_[kBufferSize]; // Buffer storage. |
| 123 | int head_; // Index to the buffer head. |
| 124 | int tail_; // Index to the buffer tail. |
| 125 | bool overflow_; // Tell whether a buffer overflow has occurred. |
| 126 | Semaphore* buffer_semaphore_; // Sempahore used for buffer synchronization. |
| 127 | |
| 128 | // Tells whether worker thread should continue running. |
| 129 | bool running_; |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 130 | |
| 131 | // Tells whether we are currently recording tick samples. |
| 132 | static bool paused_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 133 | }; |
| 134 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 135 | bool Profiler::paused_ = false; |
| 136 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 137 | |
| 138 | // |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 139 | // StackTracer implementation |
| 140 | // |
| 141 | void StackTracer::Trace(TickSample* sample) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 142 | if (sample->state == GC) { |
| 143 | sample->frames_count = 0; |
| 144 | return; |
| 145 | } |
| 146 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 147 | const Address js_entry_sp = Top::js_entry_sp(Top::GetCurrentThread()); |
| 148 | if (js_entry_sp == 0) { |
| 149 | // Not executing JS now. |
| 150 | sample->frames_count = 0; |
| 151 | return; |
| 152 | } |
| 153 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 154 | SafeStackTraceFrameIterator it( |
| 155 | reinterpret_cast<Address>(sample->fp), |
| 156 | reinterpret_cast<Address>(sample->sp), |
| 157 | reinterpret_cast<Address>(sample->sp), |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 158 | js_entry_sp); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 159 | int i = 0; |
| 160 | while (!it.done() && i < TickSample::kMaxFramesCount) { |
| 161 | sample->stack[i++] = it.frame()->pc(); |
| 162 | it.Advance(); |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 163 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 164 | sample->frames_count = i; |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | |
| 168 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 169 | // Ticker used to provide ticks to the profiler and the sliding state |
| 170 | // window. |
| 171 | // |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 172 | class Ticker: public Sampler { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 173 | public: |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 174 | explicit Ticker(int interval): |
| 175 | Sampler(interval, FLAG_prof), window_(NULL), profiler_(NULL) {} |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 176 | |
| 177 | ~Ticker() { if (IsActive()) Stop(); } |
| 178 | |
| 179 | void Tick(TickSample* sample) { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 180 | if (IsProfiling()) StackTracer::Trace(sample); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 181 | if (profiler_) profiler_->Insert(sample); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 182 | if (window_) window_->AddState(sample->state); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | void SetWindow(SlidingStateWindow* window) { |
| 186 | window_ = window; |
| 187 | if (!IsActive()) Start(); |
| 188 | } |
| 189 | |
| 190 | void ClearWindow() { |
| 191 | window_ = NULL; |
| 192 | if (!profiler_ && IsActive()) Stop(); |
| 193 | } |
| 194 | |
| 195 | void SetProfiler(Profiler* profiler) { |
| 196 | profiler_ = profiler; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 197 | if (!FLAG_prof_lazy && !IsActive()) Start(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | void ClearProfiler() { |
| 201 | profiler_ = NULL; |
| 202 | if (!window_ && IsActive()) Stop(); |
| 203 | } |
| 204 | |
| 205 | private: |
| 206 | SlidingStateWindow* window_; |
| 207 | Profiler* profiler_; |
| 208 | }; |
| 209 | |
| 210 | |
| 211 | // |
| 212 | // SlidingStateWindow implementation. |
| 213 | // |
| 214 | SlidingStateWindow::SlidingStateWindow(): current_index_(0), is_full_(false) { |
| 215 | for (int i = 0; i < kBufferSize; i++) { |
| 216 | buffer_[i] = static_cast<byte>(OTHER); |
| 217 | } |
| 218 | Logger::ticker_->SetWindow(this); |
| 219 | } |
| 220 | |
| 221 | |
| 222 | SlidingStateWindow::~SlidingStateWindow() { |
| 223 | Logger::ticker_->ClearWindow(); |
| 224 | } |
| 225 | |
| 226 | |
| 227 | void SlidingStateWindow::AddState(StateTag state) { |
| 228 | if (is_full_) { |
| 229 | DecrementStateCounter(static_cast<StateTag>(buffer_[current_index_])); |
| 230 | } else if (current_index_ == kBufferSize - 1) { |
| 231 | is_full_ = true; |
| 232 | } |
| 233 | buffer_[current_index_] = static_cast<byte>(state); |
| 234 | IncrementStateCounter(state); |
| 235 | ASSERT(IsPowerOf2(kBufferSize)); |
| 236 | current_index_ = (current_index_ + 1) & (kBufferSize - 1); |
| 237 | } |
| 238 | |
| 239 | |
| 240 | // |
| 241 | // Profiler implementation. |
| 242 | // |
| 243 | Profiler::Profiler() { |
| 244 | buffer_semaphore_ = OS::CreateSemaphore(0); |
| 245 | head_ = 0; |
| 246 | tail_ = 0; |
| 247 | overflow_ = false; |
| 248 | running_ = false; |
| 249 | } |
| 250 | |
| 251 | |
| 252 | void Profiler::Engage() { |
| 253 | OS::LogSharedLibraryAddresses(); |
| 254 | |
| 255 | // Start thread processing the profiler buffer. |
| 256 | running_ = true; |
| 257 | Start(); |
| 258 | |
| 259 | // Register to get ticks. |
| 260 | Logger::ticker_->SetProfiler(this); |
| 261 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 262 | Logger::ProfilerBeginEvent(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 263 | Logger::LogAliases(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | |
| 267 | void Profiler::Disengage() { |
| 268 | // Stop receiving ticks. |
| 269 | Logger::ticker_->ClearProfiler(); |
| 270 | |
| 271 | // Terminate the worker thread by setting running_ to false, |
| 272 | // inserting a fake element in the queue and then wait for |
| 273 | // the thread to terminate. |
| 274 | running_ = false; |
| 275 | TickSample sample; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 276 | // Reset 'paused_' flag, otherwise semaphore may not be signalled. |
| 277 | resume(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 278 | Insert(&sample); |
| 279 | Join(); |
| 280 | |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 281 | LOG(UncheckedStringEvent("profiler", "end")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | |
| 285 | void Profiler::Run() { |
| 286 | TickSample sample; |
| 287 | bool overflow = Logger::profiler_->Remove(&sample); |
| 288 | while (running_) { |
| 289 | LOG(TickEvent(&sample, overflow)); |
| 290 | overflow = Logger::profiler_->Remove(&sample); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | |
| 295 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 296 | // Logger class implementation. |
| 297 | // |
| 298 | Ticker* Logger::ticker_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 299 | Profiler* Logger::profiler_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 300 | VMState* Logger::current_state_ = NULL; |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 301 | VMState Logger::bottom_state_(EXTERNAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 302 | SlidingStateWindow* Logger::sliding_state_window_ = NULL; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 303 | const char** Logger::log_events_ = NULL; |
| 304 | CompressionHelper* Logger::compression_helper_ = NULL; |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame^] | 305 | bool Logger::is_logging_ = false; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 306 | |
| 307 | #define DECLARE_LONG_EVENT(ignore1, long_name, ignore2) long_name, |
| 308 | const char* kLongLogEventsNames[Logger::NUMBER_OF_LOG_EVENTS] = { |
| 309 | LOG_EVENTS_AND_TAGS_LIST(DECLARE_LONG_EVENT) |
| 310 | }; |
| 311 | #undef DECLARE_LONG_EVENT |
| 312 | |
| 313 | #define DECLARE_SHORT_EVENT(ignore1, ignore2, short_name) short_name, |
| 314 | const char* kCompressedLogEventsNames[Logger::NUMBER_OF_LOG_EVENTS] = { |
| 315 | LOG_EVENTS_AND_TAGS_LIST(DECLARE_SHORT_EVENT) |
| 316 | }; |
| 317 | #undef DECLARE_SHORT_EVENT |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 318 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 319 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 320 | void Logger::ProfilerBeginEvent() { |
| 321 | if (!Log::IsEnabled()) return; |
| 322 | LogMessageBuilder msg; |
| 323 | msg.Append("profiler,\"begin\",%d\n", kSamplingIntervalMs); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 324 | if (FLAG_compress_log) { |
| 325 | msg.Append("profiler,\"compression\",%d\n", kCompressionWindowSize); |
| 326 | } |
| 327 | msg.WriteToLogFile(); |
| 328 | } |
| 329 | |
| 330 | |
| 331 | void Logger::LogAliases() { |
| 332 | if (!Log::IsEnabled() || !FLAG_compress_log) return; |
| 333 | LogMessageBuilder msg; |
| 334 | for (int i = 0; i < NUMBER_OF_LOG_EVENTS; ++i) { |
| 335 | msg.Append("alias,%s,%s\n", |
| 336 | kCompressedLogEventsNames[i], kLongLogEventsNames[i]); |
| 337 | } |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 338 | msg.WriteToLogFile(); |
| 339 | } |
| 340 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 341 | #endif // ENABLE_LOGGING_AND_PROFILING |
| 342 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 343 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 344 | void Logger::Preamble(const char* content) { |
| 345 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 346 | if (!Log::IsEnabled() || !FLAG_log_code) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 347 | LogMessageBuilder msg; |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 348 | msg.WriteCStringToLogFile(content); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 349 | #endif |
| 350 | } |
| 351 | |
| 352 | |
| 353 | void Logger::StringEvent(const char* name, const char* value) { |
| 354 | #ifdef ENABLE_LOGGING_AND_PROFILING |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 355 | if (FLAG_log) UncheckedStringEvent(name, value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 356 | #endif |
| 357 | } |
| 358 | |
| 359 | |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 360 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 361 | void Logger::UncheckedStringEvent(const char* name, const char* value) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 362 | if (!Log::IsEnabled()) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 363 | LogMessageBuilder msg; |
| 364 | msg.Append("%s,\"%s\"\n", name, value); |
| 365 | msg.WriteToLogFile(); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 366 | } |
| 367 | #endif |
| 368 | |
| 369 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 370 | void Logger::IntEvent(const char* name, int value) { |
| 371 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 372 | if (!Log::IsEnabled() || !FLAG_log) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 373 | LogMessageBuilder msg; |
| 374 | msg.Append("%s,%d\n", name, value); |
| 375 | msg.WriteToLogFile(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 376 | #endif |
| 377 | } |
| 378 | |
| 379 | |
| 380 | void Logger::HandleEvent(const char* name, Object** location) { |
| 381 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 382 | if (!Log::IsEnabled() || !FLAG_log_handles) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 383 | LogMessageBuilder msg; |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 384 | msg.Append("%s,0x%" V8PRIxPTR "\n", name, location); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 385 | msg.WriteToLogFile(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 386 | #endif |
| 387 | } |
| 388 | |
| 389 | |
| 390 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 391 | // ApiEvent is private so all the calls come from the Logger class. It is the |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 392 | // caller's responsibility to ensure that log is enabled and that |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 393 | // FLAG_log_api is true. |
| 394 | void Logger::ApiEvent(const char* format, ...) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 395 | ASSERT(Log::IsEnabled() && FLAG_log_api); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 396 | LogMessageBuilder msg; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 397 | va_list ap; |
| 398 | va_start(ap, format); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 399 | msg.AppendVA(format, ap); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 400 | va_end(ap); |
| 401 | msg.WriteToLogFile(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 402 | } |
| 403 | #endif |
| 404 | |
| 405 | |
| 406 | void Logger::ApiNamedSecurityCheck(Object* key) { |
| 407 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 408 | if (!Log::IsEnabled() || !FLAG_log_api) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 409 | if (key->IsString()) { |
| 410 | SmartPointer<char> str = |
| 411 | String::cast(key)->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL); |
| 412 | ApiEvent("api,check-security,\"%s\"\n", *str); |
| 413 | } else if (key->IsUndefined()) { |
| 414 | ApiEvent("api,check-security,undefined\n"); |
| 415 | } else { |
| 416 | ApiEvent("api,check-security,['no-name']\n"); |
| 417 | } |
| 418 | #endif |
| 419 | } |
| 420 | |
| 421 | |
| 422 | void Logger::SharedLibraryEvent(const char* library_path, |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 423 | uintptr_t start, |
| 424 | uintptr_t end) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 425 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 426 | if (!Log::IsEnabled() || !FLAG_prof) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 427 | LogMessageBuilder msg; |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 428 | msg.Append("shared-library,\"%s\",0x%08" V8PRIxPTR ",0x%08" V8PRIxPTR "\n", |
| 429 | library_path, |
| 430 | start, |
| 431 | end); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 432 | msg.WriteToLogFile(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 433 | #endif |
| 434 | } |
| 435 | |
| 436 | |
| 437 | void Logger::SharedLibraryEvent(const wchar_t* library_path, |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 438 | uintptr_t start, |
| 439 | uintptr_t end) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 440 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 441 | if (!Log::IsEnabled() || !FLAG_prof) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 442 | LogMessageBuilder msg; |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 443 | msg.Append("shared-library,\"%ls\",0x%08" V8PRIxPTR ",0x%08" V8PRIxPTR "\n", |
| 444 | library_path, |
| 445 | start, |
| 446 | end); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 447 | msg.WriteToLogFile(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 448 | #endif |
| 449 | } |
| 450 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 451 | |
| 452 | #ifdef ENABLE_LOGGING_AND_PROFILING |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 453 | void Logger::LogRegExpSource(Handle<JSRegExp> regexp) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 454 | // Prints "/" + re.source + "/" + |
| 455 | // (re.global?"g":"") + (re.ignorecase?"i":"") + (re.multiline?"m":"") |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 456 | LogMessageBuilder msg; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 457 | |
| 458 | Handle<Object> source = GetProperty(regexp, "source"); |
| 459 | if (!source->IsString()) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 460 | msg.Append("no source"); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 461 | return; |
| 462 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 463 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 464 | switch (regexp->TypeTag()) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 465 | case JSRegExp::ATOM: |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 466 | msg.Append('a'); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 467 | break; |
| 468 | default: |
| 469 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 470 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 471 | msg.Append('/'); |
| 472 | msg.AppendDetailed(*Handle<String>::cast(source), false); |
| 473 | msg.Append('/'); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 474 | |
| 475 | // global flag |
| 476 | Handle<Object> global = GetProperty(regexp, "global"); |
| 477 | if (global->IsTrue()) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 478 | msg.Append('g'); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 479 | } |
| 480 | // ignorecase flag |
| 481 | Handle<Object> ignorecase = GetProperty(regexp, "ignoreCase"); |
| 482 | if (ignorecase->IsTrue()) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 483 | msg.Append('i'); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 484 | } |
| 485 | // multiline flag |
| 486 | Handle<Object> multiline = GetProperty(regexp, "multiline"); |
| 487 | if (multiline->IsTrue()) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 488 | msg.Append('m'); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 489 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 490 | |
| 491 | msg.WriteToLogFile(); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 492 | } |
| 493 | #endif // ENABLE_LOGGING_AND_PROFILING |
| 494 | |
| 495 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 496 | void Logger::RegExpCompileEvent(Handle<JSRegExp> regexp, bool in_cache) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 497 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 498 | if (!Log::IsEnabled() || !FLAG_log_regexp) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 499 | LogMessageBuilder msg; |
| 500 | msg.Append("regexp-compile,"); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 501 | LogRegExpSource(regexp); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 502 | msg.Append(in_cache ? ",hit\n" : ",miss\n"); |
| 503 | msg.WriteToLogFile(); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 504 | #endif |
| 505 | } |
| 506 | |
| 507 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 508 | void Logger::LogRuntime(Vector<const char> format, JSArray* args) { |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 509 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 510 | if (!Log::IsEnabled() || !FLAG_log_runtime) return; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 511 | HandleScope scope; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 512 | LogMessageBuilder msg; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 513 | for (int i = 0; i < format.length(); i++) { |
| 514 | char c = format[i]; |
| 515 | if (c == '%' && i <= format.length() - 2) { |
| 516 | i++; |
| 517 | ASSERT('0' <= format[i] && format[i] <= '9'); |
| 518 | Object* obj = args->GetElement(format[i] - '0'); |
| 519 | i++; |
| 520 | switch (format[i]) { |
| 521 | case 's': |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 522 | msg.AppendDetailed(String::cast(obj), false); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 523 | break; |
| 524 | case 'S': |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 525 | msg.AppendDetailed(String::cast(obj), true); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 526 | break; |
| 527 | case 'r': |
| 528 | Logger::LogRegExpSource(Handle<JSRegExp>(JSRegExp::cast(obj))); |
| 529 | break; |
| 530 | case 'x': |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 531 | msg.Append("0x%x", Smi::cast(obj)->value()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 532 | break; |
| 533 | case 'i': |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 534 | msg.Append("%i", Smi::cast(obj)->value()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 535 | break; |
| 536 | default: |
| 537 | UNREACHABLE(); |
| 538 | } |
| 539 | } else { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 540 | msg.Append(c); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 541 | } |
| 542 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 543 | msg.Append('\n'); |
| 544 | msg.WriteToLogFile(); |
christian.plesner.hansen@gmail.com | aca4968 | 2009-01-07 14:29:04 +0000 | [diff] [blame] | 545 | #endif |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 549 | void Logger::ApiIndexedSecurityCheck(uint32_t index) { |
| 550 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 551 | if (!Log::IsEnabled() || !FLAG_log_api) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 552 | ApiEvent("api,check-security,%u\n", index); |
| 553 | #endif |
| 554 | } |
| 555 | |
| 556 | |
| 557 | void Logger::ApiNamedPropertyAccess(const char* tag, |
| 558 | JSObject* holder, |
| 559 | Object* name) { |
| 560 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 561 | ASSERT(name->IsString()); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 562 | if (!Log::IsEnabled() || !FLAG_log_api) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 563 | String* class_name_obj = holder->class_name(); |
| 564 | SmartPointer<char> class_name = |
| 565 | class_name_obj->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL); |
| 566 | SmartPointer<char> property_name = |
| 567 | String::cast(name)->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL); |
| 568 | Logger::ApiEvent("api,%s,\"%s\",\"%s\"\n", tag, *class_name, *property_name); |
| 569 | #endif |
| 570 | } |
| 571 | |
| 572 | void Logger::ApiIndexedPropertyAccess(const char* tag, |
| 573 | JSObject* holder, |
| 574 | uint32_t index) { |
| 575 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 576 | if (!Log::IsEnabled() || !FLAG_log_api) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 577 | String* class_name_obj = holder->class_name(); |
| 578 | SmartPointer<char> class_name = |
| 579 | class_name_obj->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL); |
| 580 | Logger::ApiEvent("api,%s,\"%s\",%u\n", tag, *class_name, index); |
| 581 | #endif |
| 582 | } |
| 583 | |
| 584 | void Logger::ApiObjectAccess(const char* tag, JSObject* object) { |
| 585 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 586 | if (!Log::IsEnabled() || !FLAG_log_api) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 587 | String* class_name_obj = object->class_name(); |
| 588 | SmartPointer<char> class_name = |
| 589 | class_name_obj->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL); |
| 590 | Logger::ApiEvent("api,%s,\"%s\"\n", tag, *class_name); |
| 591 | #endif |
| 592 | } |
| 593 | |
| 594 | |
| 595 | void Logger::ApiEntryCall(const char* name) { |
| 596 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 597 | if (!Log::IsEnabled() || !FLAG_log_api) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 598 | Logger::ApiEvent("api,%s\n", name); |
| 599 | #endif |
| 600 | } |
| 601 | |
| 602 | |
| 603 | void Logger::NewEvent(const char* name, void* object, size_t size) { |
| 604 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 605 | if (!Log::IsEnabled() || !FLAG_log) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 606 | LogMessageBuilder msg; |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 607 | msg.Append("new,%s,0x%" V8PRIxPTR ",%u\n", name, object, |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 608 | static_cast<unsigned int>(size)); |
| 609 | msg.WriteToLogFile(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 610 | #endif |
| 611 | } |
| 612 | |
| 613 | |
| 614 | void Logger::DeleteEvent(const char* name, void* object) { |
| 615 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 616 | if (!Log::IsEnabled() || !FLAG_log) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 617 | LogMessageBuilder msg; |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 618 | msg.Append("delete,%s,0x%" V8PRIxPTR "\n", name, object); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 619 | msg.WriteToLogFile(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 620 | #endif |
| 621 | } |
| 622 | |
| 623 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 624 | #ifdef ENABLE_LOGGING_AND_PROFILING |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 625 | |
| 626 | // A class that contains all common code dealing with record compression. |
| 627 | class CompressionHelper { |
| 628 | public: |
| 629 | explicit CompressionHelper(int window_size) |
| 630 | : compressor_(window_size), repeat_count_(0) { } |
| 631 | |
| 632 | // Handles storing message in compressor, retrieving the previous one and |
| 633 | // prefixing it with repeat count, if needed. |
| 634 | // Returns true if message needs to be written to log. |
| 635 | bool HandleMessage(LogMessageBuilder* msg) { |
| 636 | if (!msg->StoreInCompressor(&compressor_)) { |
| 637 | // Current message repeats the previous one, don't write it. |
| 638 | ++repeat_count_; |
| 639 | return false; |
| 640 | } |
| 641 | if (repeat_count_ == 0) { |
| 642 | return msg->RetrieveCompressedPrevious(&compressor_); |
| 643 | } |
| 644 | OS::SNPrintF(prefix_, "%s,%d,", |
| 645 | Logger::log_events_[Logger::REPEAT_META_EVENT], |
| 646 | repeat_count_ + 1); |
| 647 | repeat_count_ = 0; |
| 648 | return msg->RetrieveCompressedPrevious(&compressor_, prefix_.start()); |
| 649 | } |
| 650 | |
| 651 | private: |
| 652 | LogRecordCompressor compressor_; |
| 653 | int repeat_count_; |
| 654 | EmbeddedVector<char, 20> prefix_; |
| 655 | }; |
| 656 | |
| 657 | #endif // ENABLE_LOGGING_AND_PROFILING |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 658 | |
| 659 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 660 | void Logger::CodeCreateEvent(LogEventsAndTags tag, |
| 661 | Code* code, |
| 662 | const char* comment) { |
| 663 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 664 | if (!Log::IsEnabled() || !FLAG_log_code) return; |
| 665 | LogMessageBuilder msg; |
| 666 | msg.Append("%s,%s,", log_events_[CODE_CREATION_EVENT], log_events_[tag]); |
| 667 | msg.AppendAddress(code->address()); |
| 668 | msg.Append(",%d,\"", code->ExecutableSize()); |
| 669 | for (const char* p = comment; *p != '\0'; p++) { |
| 670 | if (*p == '"') { |
| 671 | msg.Append('\\'); |
| 672 | } |
| 673 | msg.Append(*p); |
| 674 | } |
| 675 | msg.Append('"'); |
| 676 | if (FLAG_compress_log) { |
| 677 | ASSERT(compression_helper_ != NULL); |
| 678 | if (!compression_helper_->HandleMessage(&msg)) return; |
| 679 | } |
| 680 | msg.Append('\n'); |
| 681 | msg.WriteToLogFile(); |
| 682 | #endif |
| 683 | } |
| 684 | |
| 685 | |
| 686 | void Logger::CodeCreateEvent(LogEventsAndTags tag, Code* code, String* name) { |
| 687 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 688 | if (!Log::IsEnabled() || !FLAG_log_code) return; |
| 689 | LogMessageBuilder msg; |
| 690 | SmartPointer<char> str = |
| 691 | name->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL); |
| 692 | msg.Append("%s,%s,", log_events_[CODE_CREATION_EVENT], log_events_[tag]); |
| 693 | msg.AppendAddress(code->address()); |
| 694 | msg.Append(",%d,\"%s\"", code->ExecutableSize(), *str); |
| 695 | if (FLAG_compress_log) { |
| 696 | ASSERT(compression_helper_ != NULL); |
| 697 | if (!compression_helper_->HandleMessage(&msg)) return; |
| 698 | } |
| 699 | msg.Append('\n'); |
| 700 | msg.WriteToLogFile(); |
| 701 | #endif |
| 702 | } |
| 703 | |
| 704 | |
| 705 | void Logger::CodeCreateEvent(LogEventsAndTags tag, |
| 706 | Code* code, String* name, |
| 707 | String* source, int line) { |
| 708 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 709 | if (!Log::IsEnabled() || !FLAG_log_code) return; |
| 710 | LogMessageBuilder msg; |
| 711 | SmartPointer<char> str = |
| 712 | name->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL); |
| 713 | SmartPointer<char> sourcestr = |
| 714 | source->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL); |
| 715 | msg.Append("%s,%s,", log_events_[CODE_CREATION_EVENT], log_events_[tag]); |
| 716 | msg.AppendAddress(code->address()); |
| 717 | msg.Append(",%d,\"%s %s:%d\"", |
| 718 | code->ExecutableSize(), *str, *sourcestr, line); |
| 719 | if (FLAG_compress_log) { |
| 720 | ASSERT(compression_helper_ != NULL); |
| 721 | if (!compression_helper_->HandleMessage(&msg)) return; |
| 722 | } |
| 723 | msg.Append('\n'); |
| 724 | msg.WriteToLogFile(); |
| 725 | #endif |
| 726 | } |
| 727 | |
| 728 | |
| 729 | void Logger::CodeCreateEvent(LogEventsAndTags tag, Code* code, int args_count) { |
| 730 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 731 | if (!Log::IsEnabled() || !FLAG_log_code) return; |
| 732 | LogMessageBuilder msg; |
| 733 | msg.Append("%s,%s,", log_events_[CODE_CREATION_EVENT], log_events_[tag]); |
| 734 | msg.AppendAddress(code->address()); |
| 735 | msg.Append(",%d,\"args_count: %d\"", code->ExecutableSize(), args_count); |
| 736 | if (FLAG_compress_log) { |
| 737 | ASSERT(compression_helper_ != NULL); |
| 738 | if (!compression_helper_->HandleMessage(&msg)) return; |
| 739 | } |
| 740 | msg.Append('\n'); |
| 741 | msg.WriteToLogFile(); |
| 742 | #endif |
| 743 | } |
| 744 | |
| 745 | |
| 746 | void Logger::RegExpCodeCreateEvent(Code* code, String* source) { |
| 747 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 748 | if (!Log::IsEnabled() || !FLAG_log_code) return; |
| 749 | LogMessageBuilder msg; |
| 750 | msg.Append("%s,%s,", |
| 751 | log_events_[CODE_CREATION_EVENT], log_events_[REG_EXP_TAG]); |
| 752 | msg.AppendAddress(code->address()); |
| 753 | msg.Append(",%d,\"", code->ExecutableSize()); |
| 754 | msg.AppendDetailed(source, false); |
| 755 | msg.Append('\"'); |
| 756 | if (FLAG_compress_log) { |
| 757 | ASSERT(compression_helper_ != NULL); |
| 758 | if (!compression_helper_->HandleMessage(&msg)) return; |
| 759 | } |
| 760 | msg.Append('\n'); |
| 761 | msg.WriteToLogFile(); |
| 762 | #endif |
| 763 | } |
| 764 | |
| 765 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 766 | void Logger::CodeMoveEvent(Address from, Address to) { |
| 767 | #ifdef ENABLE_LOGGING_AND_PROFILING |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 768 | static Address prev_to_ = NULL; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 769 | if (!Log::IsEnabled() || !FLAG_log_code) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 770 | LogMessageBuilder msg; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 771 | msg.Append("%s,", log_events_[CODE_MOVE_EVENT]); |
| 772 | msg.AppendAddress(from); |
| 773 | msg.Append(','); |
| 774 | msg.AppendAddress(to, prev_to_); |
| 775 | prev_to_ = to; |
| 776 | if (FLAG_compress_log) { |
| 777 | ASSERT(compression_helper_ != NULL); |
| 778 | if (!compression_helper_->HandleMessage(&msg)) return; |
| 779 | } |
| 780 | msg.Append('\n'); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 781 | msg.WriteToLogFile(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 782 | #endif |
| 783 | } |
| 784 | |
| 785 | |
| 786 | void Logger::CodeDeleteEvent(Address from) { |
| 787 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 788 | if (!Log::IsEnabled() || !FLAG_log_code) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 789 | LogMessageBuilder msg; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 790 | msg.Append("%s,", log_events_[CODE_DELETE_EVENT]); |
| 791 | msg.AppendAddress(from); |
| 792 | if (FLAG_compress_log) { |
| 793 | ASSERT(compression_helper_ != NULL); |
| 794 | if (!compression_helper_->HandleMessage(&msg)) return; |
| 795 | } |
| 796 | msg.Append('\n'); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 797 | msg.WriteToLogFile(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 798 | #endif |
| 799 | } |
| 800 | |
| 801 | |
| 802 | void Logger::ResourceEvent(const char* name, const char* tag) { |
| 803 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 804 | if (!Log::IsEnabled() || !FLAG_log) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 805 | LogMessageBuilder msg; |
| 806 | msg.Append("%s,%s,", name, tag); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 807 | |
| 808 | uint32_t sec, usec; |
| 809 | if (OS::GetUserTime(&sec, &usec) != -1) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 810 | msg.Append("%d,%d,", sec, usec); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 811 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 812 | msg.Append("%.0f", OS::TimeCurrentMillis()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 813 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 814 | msg.Append('\n'); |
| 815 | msg.WriteToLogFile(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 816 | #endif |
| 817 | } |
| 818 | |
| 819 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 820 | void Logger::SuspectReadEvent(String* name, Object* obj) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 821 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 822 | if (!Log::IsEnabled() || !FLAG_log_suspect) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 823 | LogMessageBuilder msg; |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 824 | String* class_name = obj->IsJSObject() |
| 825 | ? JSObject::cast(obj)->class_name() |
| 826 | : Heap::empty_string(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 827 | msg.Append("suspect-read,"); |
| 828 | msg.Append(class_name); |
| 829 | msg.Append(','); |
| 830 | msg.Append('"'); |
| 831 | msg.Append(name); |
| 832 | msg.Append('"'); |
| 833 | msg.Append('\n'); |
| 834 | msg.WriteToLogFile(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 835 | #endif |
| 836 | } |
| 837 | |
| 838 | |
| 839 | void Logger::HeapSampleBeginEvent(const char* space, const char* kind) { |
| 840 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 841 | if (!Log::IsEnabled() || !FLAG_log_gc) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 842 | LogMessageBuilder msg; |
| 843 | msg.Append("heap-sample-begin,\"%s\",\"%s\"\n", space, kind); |
| 844 | msg.WriteToLogFile(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 845 | #endif |
| 846 | } |
| 847 | |
| 848 | |
| 849 | void Logger::HeapSampleEndEvent(const char* space, const char* kind) { |
| 850 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 851 | if (!Log::IsEnabled() || !FLAG_log_gc) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 852 | LogMessageBuilder msg; |
| 853 | msg.Append("heap-sample-end,\"%s\",\"%s\"\n", space, kind); |
| 854 | msg.WriteToLogFile(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 855 | #endif |
| 856 | } |
| 857 | |
| 858 | |
| 859 | void Logger::HeapSampleItemEvent(const char* type, int number, int bytes) { |
| 860 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 861 | if (!Log::IsEnabled() || !FLAG_log_gc) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 862 | LogMessageBuilder msg; |
| 863 | msg.Append("heap-sample-item,%s,%d,%d\n", type, number, bytes); |
| 864 | msg.WriteToLogFile(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 865 | #endif |
| 866 | } |
| 867 | |
| 868 | |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 869 | void Logger::DebugTag(const char* call_site_tag) { |
| 870 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 871 | if (!Log::IsEnabled() || !FLAG_log) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 872 | LogMessageBuilder msg; |
| 873 | msg.Append("debug-tag,%s\n", call_site_tag); |
| 874 | msg.WriteToLogFile(); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 875 | #endif |
| 876 | } |
| 877 | |
| 878 | |
| 879 | void Logger::DebugEvent(const char* event_type, Vector<uint16_t> parameter) { |
| 880 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 881 | if (!Log::IsEnabled() || !FLAG_log) return; |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 882 | StringBuilder s(parameter.length() + 1); |
| 883 | for (int i = 0; i < parameter.length(); ++i) { |
| 884 | s.AddCharacter(static_cast<char>(parameter[i])); |
| 885 | } |
| 886 | char* parameter_string = s.Finalize(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 887 | LogMessageBuilder msg; |
| 888 | msg.Append("debug-queue-event,%s,%15.3f,%s\n", |
| 889 | event_type, |
| 890 | OS::TimeCurrentMillis(), |
| 891 | parameter_string); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 892 | DeleteArray(parameter_string); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 893 | msg.WriteToLogFile(); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 894 | #endif |
| 895 | } |
| 896 | |
| 897 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 898 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 899 | void Logger::TickEvent(TickSample* sample, bool overflow) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 900 | if (!Log::IsEnabled() || !FLAG_prof) return; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 901 | static Address prev_sp = NULL; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 902 | LogMessageBuilder msg; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 903 | msg.Append("%s,", log_events_[TICK_EVENT]); |
| 904 | Address prev_addr = reinterpret_cast<Address>(sample->pc); |
| 905 | msg.AppendAddress(prev_addr); |
| 906 | msg.Append(','); |
| 907 | msg.AppendAddress(reinterpret_cast<Address>(sample->sp), prev_sp); |
| 908 | prev_sp = reinterpret_cast<Address>(sample->sp); |
| 909 | msg.Append(",%d", static_cast<int>(sample->state)); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 910 | if (overflow) { |
| 911 | msg.Append(",overflow"); |
| 912 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 913 | for (int i = 0; i < sample->frames_count; ++i) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 914 | msg.Append(','); |
| 915 | msg.AppendAddress(sample->stack[i], prev_addr); |
| 916 | prev_addr = sample->stack[i]; |
| 917 | } |
| 918 | if (FLAG_compress_log) { |
| 919 | ASSERT(compression_helper_ != NULL); |
| 920 | if (!compression_helper_->HandleMessage(&msg)) return; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 921 | } |
| 922 | msg.Append('\n'); |
| 923 | msg.WriteToLogFile(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 924 | } |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 925 | |
| 926 | |
| 927 | bool Logger::IsProfilerPaused() { |
| 928 | return profiler_->paused(); |
| 929 | } |
| 930 | |
| 931 | |
| 932 | void Logger::PauseProfiler() { |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 933 | if (profiler_->paused()) { |
| 934 | return; |
| 935 | } |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 936 | profiler_->pause(); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 937 | if (FLAG_prof_lazy) { |
| 938 | if (!FLAG_sliding_state_window) ticker_->Stop(); |
| 939 | FLAG_log_code = false; |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 940 | // Must be the same message as Log::kDynamicBufferSeal. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 941 | LOG(UncheckedStringEvent("profiler", "pause")); |
| 942 | } |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame^] | 943 | is_logging_ = false; |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | |
| 947 | void Logger::ResumeProfiler() { |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 948 | if (!profiler_->paused() || !Log::IsEnabled()) { |
| 949 | return; |
| 950 | } |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame^] | 951 | is_logging_ = true; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 952 | if (FLAG_prof_lazy) { |
| 953 | LOG(UncheckedStringEvent("profiler", "resume")); |
| 954 | FLAG_log_code = true; |
| 955 | LogCompiledFunctions(); |
| 956 | if (!FLAG_sliding_state_window) ticker_->Start(); |
| 957 | } |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 958 | profiler_->resume(); |
| 959 | } |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 960 | |
| 961 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 962 | // This function can be called when Log's mutex is acquired, |
| 963 | // either from main or Profiler's thread. |
| 964 | void Logger::StopLoggingAndProfiling() { |
| 965 | Log::stop(); |
| 966 | PauseProfiler(); |
| 967 | } |
| 968 | |
| 969 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 970 | bool Logger::IsProfilerSamplerActive() { |
| 971 | return ticker_->IsActive(); |
| 972 | } |
| 973 | |
| 974 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 975 | int Logger::GetLogLines(int from_pos, char* dest_buf, int max_size) { |
| 976 | return Log::GetLogLines(from_pos, dest_buf, max_size); |
| 977 | } |
| 978 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 979 | |
| 980 | void Logger::LogCompiledFunctions() { |
| 981 | HandleScope scope; |
| 982 | Handle<SharedFunctionInfo>* sfis = NULL; |
| 983 | int compiled_funcs_count = 0; |
| 984 | |
| 985 | { |
| 986 | AssertNoAllocation no_alloc; |
| 987 | |
| 988 | HeapIterator iterator; |
| 989 | while (iterator.has_next()) { |
| 990 | HeapObject* obj = iterator.next(); |
| 991 | ASSERT(obj != NULL); |
| 992 | if (obj->IsSharedFunctionInfo() |
| 993 | && SharedFunctionInfo::cast(obj)->is_compiled()) { |
| 994 | ++compiled_funcs_count; |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | sfis = NewArray< Handle<SharedFunctionInfo> >(compiled_funcs_count); |
| 999 | iterator.reset(); |
| 1000 | |
| 1001 | int i = 0; |
| 1002 | while (iterator.has_next()) { |
| 1003 | HeapObject* obj = iterator.next(); |
| 1004 | ASSERT(obj != NULL); |
| 1005 | if (obj->IsSharedFunctionInfo() |
| 1006 | && SharedFunctionInfo::cast(obj)->is_compiled()) { |
| 1007 | sfis[i++] = Handle<SharedFunctionInfo>(SharedFunctionInfo::cast(obj)); |
| 1008 | } |
| 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | // During iteration, there can be heap allocation due to |
| 1013 | // GetScriptLineNumber call. |
| 1014 | for (int i = 0; i < compiled_funcs_count; ++i) { |
| 1015 | Handle<SharedFunctionInfo> shared = sfis[i]; |
| 1016 | Handle<String> name(String::cast(shared->name())); |
| 1017 | Handle<String> func_name(name->length() > 0 ? |
| 1018 | *name : shared->inferred_name()); |
| 1019 | if (shared->script()->IsScript()) { |
| 1020 | Handle<Script> script(Script::cast(shared->script())); |
| 1021 | if (script->name()->IsString()) { |
| 1022 | Handle<String> script_name(String::cast(script->name())); |
| 1023 | int line_num = GetScriptLineNumber(script, shared->start_position()); |
| 1024 | if (line_num > 0) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 1025 | LOG(CodeCreateEvent(Logger::LAZY_COMPILE_TAG, |
| 1026 | shared->code(), *func_name, |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 1027 | *script_name, line_num + 1)); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1028 | } else { |
| 1029 | // Can't distinguish enum and script here, so always use Script. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 1030 | LOG(CodeCreateEvent(Logger::SCRIPT_TAG, |
| 1031 | shared->code(), *script_name)); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1032 | } |
| 1033 | continue; |
| 1034 | } |
| 1035 | } |
| 1036 | // If no script or script has no name. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 1037 | LOG(CodeCreateEvent(Logger::LAZY_COMPILE_TAG, shared->code(), *func_name)); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | DeleteArray(sfis); |
| 1041 | } |
| 1042 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1043 | #endif |
| 1044 | |
| 1045 | |
| 1046 | bool Logger::Setup() { |
| 1047 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 1048 | // --log-all enables all the log flags. |
| 1049 | if (FLAG_log_all) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1050 | FLAG_log_runtime = true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1051 | FLAG_log_api = true; |
| 1052 | FLAG_log_code = true; |
| 1053 | FLAG_log_gc = true; |
| 1054 | FLAG_log_suspect = true; |
| 1055 | FLAG_log_handles = true; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1056 | FLAG_log_regexp = true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | // --prof implies --log-code. |
| 1060 | if (FLAG_prof) FLAG_log_code = true; |
| 1061 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1062 | // --prof_lazy controls --log-code, implies --noprof_auto. |
| 1063 | if (FLAG_prof_lazy) { |
| 1064 | FLAG_log_code = false; |
| 1065 | FLAG_prof_auto = false; |
| 1066 | } |
| 1067 | |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame^] | 1068 | bool start_logging = FLAG_log || FLAG_log_runtime || FLAG_log_api |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1069 | || FLAG_log_code || FLAG_log_gc || FLAG_log_handles || FLAG_log_suspect |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame^] | 1070 | || FLAG_log_regexp || FLAG_log_state_changes; |
| 1071 | |
| 1072 | bool open_log_file = start_logging || FLAG_prof_lazy; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1073 | |
| 1074 | // If we're logging anything, we need to open the log file. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1075 | if (open_log_file) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1076 | if (strcmp(FLAG_logfile, "-") == 0) { |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 1077 | Log::OpenStdout(); |
| 1078 | } else if (strcmp(FLAG_logfile, "*") == 0) { |
| 1079 | Log::OpenMemoryBuffer(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1080 | } else if (strchr(FLAG_logfile, '%') != NULL) { |
| 1081 | // If there's a '%' in the log file name we have to expand |
| 1082 | // placeholders. |
| 1083 | HeapStringAllocator allocator; |
| 1084 | StringStream stream(&allocator); |
| 1085 | for (const char* p = FLAG_logfile; *p; p++) { |
| 1086 | if (*p == '%') { |
| 1087 | p++; |
| 1088 | switch (*p) { |
| 1089 | case '\0': |
| 1090 | // If there's a % at the end of the string we back up |
| 1091 | // one character so we can escape the loop properly. |
| 1092 | p--; |
| 1093 | break; |
| 1094 | case 't': { |
| 1095 | // %t expands to the current time in milliseconds. |
| 1096 | uint32_t time = static_cast<uint32_t>(OS::TimeCurrentMillis()); |
| 1097 | stream.Add("%u", time); |
| 1098 | break; |
| 1099 | } |
| 1100 | case '%': |
| 1101 | // %% expands (contracts really) to %. |
| 1102 | stream.Put('%'); |
| 1103 | break; |
| 1104 | default: |
| 1105 | // All other %'s expand to themselves. |
| 1106 | stream.Put('%'); |
| 1107 | stream.Put(*p); |
| 1108 | break; |
| 1109 | } |
| 1110 | } else { |
| 1111 | stream.Put(*p); |
| 1112 | } |
| 1113 | } |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1114 | SmartPointer<const char> expanded = stream.ToCString(); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 1115 | Log::OpenFile(*expanded); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1116 | } else { |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 1117 | Log::OpenFile(FLAG_logfile); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1118 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 1121 | current_state_ = &bottom_state_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1122 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1123 | ticker_ = new Ticker(kSamplingIntervalMs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1124 | |
| 1125 | if (FLAG_sliding_state_window && sliding_state_window_ == NULL) { |
| 1126 | sliding_state_window_ = new SlidingStateWindow(); |
| 1127 | } |
| 1128 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 1129 | log_events_ = FLAG_compress_log ? |
| 1130 | kCompressedLogEventsNames : kLongLogEventsNames; |
| 1131 | if (FLAG_compress_log) { |
| 1132 | compression_helper_ = new CompressionHelper(kCompressionWindowSize); |
| 1133 | } |
| 1134 | |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame^] | 1135 | is_logging_ = start_logging; |
| 1136 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1137 | if (FLAG_prof) { |
| 1138 | profiler_ = new Profiler(); |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame^] | 1139 | if (!FLAG_prof_auto) { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1140 | profiler_->pause(); |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame^] | 1141 | } else { |
| 1142 | is_logging_ = true; |
| 1143 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1144 | profiler_->Engage(); |
| 1145 | } |
| 1146 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 1147 | LogMessageBuilder::set_write_failure_handler(StopLoggingAndProfiling); |
| 1148 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1149 | return true; |
| 1150 | |
| 1151 | #else |
| 1152 | return false; |
| 1153 | #endif |
| 1154 | } |
| 1155 | |
| 1156 | |
| 1157 | void Logger::TearDown() { |
| 1158 | #ifdef ENABLE_LOGGING_AND_PROFILING |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 1159 | LogMessageBuilder::set_write_failure_handler(NULL); |
| 1160 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1161 | // Stop the profiler before closing the file. |
| 1162 | if (profiler_ != NULL) { |
| 1163 | profiler_->Disengage(); |
| 1164 | delete profiler_; |
| 1165 | profiler_ = NULL; |
| 1166 | } |
| 1167 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 1168 | delete compression_helper_; |
| 1169 | compression_helper_ = NULL; |
| 1170 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1171 | delete sliding_state_window_; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1172 | sliding_state_window_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1173 | |
| 1174 | delete ticker_; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1175 | ticker_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1176 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 1177 | Log::Close(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1178 | #endif |
| 1179 | } |
| 1180 | |
| 1181 | |
| 1182 | void Logger::EnableSlidingStateWindow() { |
| 1183 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 1184 | // If the ticker is NULL, Logger::Setup has not been called yet. In |
| 1185 | // that case, we set the sliding_state_window flag so that the |
| 1186 | // sliding window computation will be started when Logger::Setup is |
| 1187 | // called. |
| 1188 | if (ticker_ == NULL) { |
| 1189 | FLAG_sliding_state_window = true; |
| 1190 | return; |
| 1191 | } |
| 1192 | // Otherwise, if the sliding state window computation has not been |
| 1193 | // started we do it now. |
| 1194 | if (sliding_state_window_ == NULL) { |
| 1195 | sliding_state_window_ = new SlidingStateWindow(); |
| 1196 | } |
| 1197 | #endif |
| 1198 | } |
| 1199 | |
| 1200 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1201 | } } // namespace v8::internal |