ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 1 | // Copyright 2006-2009 the V8 project authors. All rights reserved. |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +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. |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 27 | // |
| 28 | // Tests of logging functions from log.h |
| 29 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 30 | #ifdef __linux__ |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 31 | #include <math.h> |
| 32 | #include <pthread.h> |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 33 | #include <signal.h> |
| 34 | #include <unistd.h> |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 35 | #endif // __linux__ |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 36 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 37 | #include "v8.h" |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 38 | #include "log.h" |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 39 | #include "cpu-profiler.h" |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 40 | #include "natives.h" |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 41 | #include "v8threads.h" |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 42 | #include "v8utils.h" |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 43 | #include "cctest.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 44 | #include "vm-state-inl.h" |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 45 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 46 | using v8::internal::Address; |
| 47 | using v8::internal::EmbeddedVector; |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 48 | using v8::internal::Logger; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 49 | using v8::internal::StrLength; |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 50 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 51 | namespace { |
| 52 | |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 53 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 54 | class ScopedLoggerInitializer { |
| 55 | public: |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 56 | explicit ScopedLoggerInitializer(bool prof_lazy) |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 57 | : saved_log_(i::FLAG_log), |
| 58 | saved_prof_lazy_(i::FLAG_prof_lazy), |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 59 | saved_prof_(i::FLAG_prof), |
| 60 | saved_prof_auto_(i::FLAG_prof_auto), |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 61 | temp_file_(NULL), |
| 62 | // Need to run this prior to creating the scope. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 63 | trick_to_run_init_flags_(init_flags_(prof_lazy)), |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 64 | scope_(v8::Isolate::GetCurrent()), |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 65 | env_(v8::Context::New()), |
| 66 | logger_(i::Isolate::Current()->logger()) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 67 | env_->Enter(); |
| 68 | } |
| 69 | |
| 70 | ~ScopedLoggerInitializer() { |
| 71 | env_->Exit(); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 72 | logger_->TearDown(); |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 73 | if (temp_file_ != NULL) fclose(temp_file_); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 74 | i::FLAG_prof_lazy = saved_prof_lazy_; |
| 75 | i::FLAG_prof = saved_prof_; |
| 76 | i::FLAG_prof_auto = saved_prof_auto_; |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 77 | i::FLAG_log = saved_log_; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | v8::Handle<v8::Context>& env() { return env_; } |
| 81 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 82 | Logger* logger() { return logger_; } |
| 83 | |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 84 | FILE* StopLoggingGetTempFile() { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 85 | temp_file_ = logger_->TearDown(); |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 86 | CHECK_NE(NULL, temp_file_); |
| 87 | fflush(temp_file_); |
| 88 | rewind(temp_file_); |
| 89 | return temp_file_; |
| 90 | } |
| 91 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 92 | private: |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 93 | static bool init_flags_(bool prof_lazy) { |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 94 | i::FLAG_log = true; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 95 | i::FLAG_prof = true; |
| 96 | i::FLAG_prof_lazy = prof_lazy; |
| 97 | i::FLAG_prof_auto = false; |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 98 | i::FLAG_logfile = i::Log::kLogToTemporaryFile; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 99 | return prof_lazy; |
| 100 | } |
| 101 | |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 102 | const bool saved_log_; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 103 | const bool saved_prof_lazy_; |
| 104 | const bool saved_prof_; |
| 105 | const bool saved_prof_auto_; |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 106 | FILE* temp_file_; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 107 | const bool trick_to_run_init_flags_; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 108 | v8::HandleScope scope_; |
| 109 | v8::Handle<v8::Context> env_; |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 110 | Logger* logger_; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 111 | |
| 112 | DISALLOW_COPY_AND_ASSIGN(ScopedLoggerInitializer); |
| 113 | }; |
| 114 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 115 | } // namespace |
| 116 | |
| 117 | |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 118 | static const char* StrNStr(const char* s1, const char* s2, int n) { |
| 119 | if (s1[n] == '\0') return strstr(s1, s2); |
| 120 | i::ScopedVector<char> str(n + 1); |
| 121 | i::OS::StrNCpy(str, s1, static_cast<size_t>(n)); |
| 122 | str[n] = '\0'; |
| 123 | char* found = strstr(str.start(), s2); |
| 124 | return found != NULL ? s1 + (found - str.start()) : NULL; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | |
| 128 | TEST(ProfLazyMode) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 129 | ScopedLoggerInitializer initialize_logger(true); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 130 | Logger* logger = initialize_logger.logger(); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 131 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 132 | if (!i::V8::UseCrankshaft()) return; |
| 133 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 134 | logger->StringEvent("test-start", ""); |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 135 | CompileRun("var a = (function(x) { return x + 1; })(10);"); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 136 | logger->StringEvent("test-profiler-start", ""); |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 137 | v8::V8::ResumeProfiler(); |
| 138 | CompileRun( |
| 139 | "var b = (function(x) { return x + 2; })(10);\n" |
| 140 | "var c = (function(x) { return x + 3; })(10);\n" |
| 141 | "var d = (function(x) { return x + 4; })(10);\n" |
| 142 | "var e = (function(x) { return x + 5; })(10);"); |
| 143 | v8::V8::PauseProfiler(); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 144 | logger->StringEvent("test-profiler-stop", ""); |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 145 | CompileRun("var f = (function(x) { return x + 6; })(10);"); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 146 | // Check that profiling can be resumed again. |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 147 | logger->StringEvent("test-profiler-start-2", ""); |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 148 | v8::V8::ResumeProfiler(); |
| 149 | CompileRun( |
| 150 | "var g = (function(x) { return x + 7; })(10);\n" |
| 151 | "var h = (function(x) { return x + 8; })(10);\n" |
| 152 | "var i = (function(x) { return x + 9; })(10);\n" |
| 153 | "var j = (function(x) { return x + 10; })(10);"); |
| 154 | v8::V8::PauseProfiler(); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 155 | logger->StringEvent("test-profiler-stop-2", ""); |
| 156 | logger->StringEvent("test-stop", ""); |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 157 | |
| 158 | bool exists = false; |
| 159 | i::Vector<const char> log( |
| 160 | i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true)); |
| 161 | CHECK(exists); |
| 162 | |
| 163 | const char* test_start_position = |
| 164 | StrNStr(log.start(), "test-start,", log.length()); |
| 165 | CHECK_NE(NULL, test_start_position); |
| 166 | const char* test_profiler_start_position = |
| 167 | StrNStr(log.start(), "test-profiler-start,", log.length()); |
| 168 | CHECK_NE(NULL, test_profiler_start_position); |
| 169 | CHECK_GT(test_profiler_start_position, test_start_position); |
| 170 | const char* test_profiler_stop_position = |
| 171 | StrNStr(log.start(), "test-profiler-stop,", log.length()); |
| 172 | CHECK_NE(NULL, test_profiler_stop_position); |
| 173 | CHECK_GT(test_profiler_stop_position, test_profiler_start_position); |
| 174 | const char* test_profiler_start_2_position = |
| 175 | StrNStr(log.start(), "test-profiler-start-2,", log.length()); |
| 176 | CHECK_NE(NULL, test_profiler_start_2_position); |
| 177 | CHECK_GT(test_profiler_start_2_position, test_profiler_stop_position); |
| 178 | |
| 179 | // Nothing must be logged until profiling is resumed. |
| 180 | CHECK_EQ(NULL, StrNStr(test_start_position, |
| 181 | "code-creation,", |
| 182 | static_cast<int>(test_profiler_start_position - |
| 183 | test_start_position))); |
| 184 | // Nothing must be logged while profiling is suspended. |
| 185 | CHECK_EQ(NULL, StrNStr(test_profiler_stop_position, |
| 186 | "code-creation,", |
| 187 | static_cast<int>(test_profiler_start_2_position - |
| 188 | test_profiler_stop_position))); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 192 | // BUG(913). Need to implement support for profiling multiple VM threads. |
| 193 | #if 0 |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 194 | |
| 195 | namespace { |
| 196 | |
| 197 | class LoopingThread : public v8::internal::Thread { |
| 198 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 199 | explicit LoopingThread(v8::internal::Isolate* isolate) |
| 200 | : v8::internal::Thread(isolate), |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 201 | semaphore_(v8::internal::OS::CreateSemaphore(0)), |
| 202 | run_(true) { |
| 203 | } |
| 204 | |
| 205 | virtual ~LoopingThread() { delete semaphore_; } |
| 206 | |
| 207 | void Run() { |
| 208 | self_ = pthread_self(); |
| 209 | RunLoop(); |
| 210 | } |
| 211 | |
| 212 | void SendSigProf() { pthread_kill(self_, SIGPROF); } |
| 213 | |
| 214 | void Stop() { run_ = false; } |
| 215 | |
| 216 | bool WaitForRunning() { return semaphore_->Wait(1000000); } |
| 217 | |
| 218 | protected: |
| 219 | bool IsRunning() { return run_; } |
| 220 | |
| 221 | virtual void RunLoop() = 0; |
| 222 | |
| 223 | void SetV8ThreadId() { |
| 224 | v8_thread_id_ = v8::V8::GetCurrentThreadId(); |
| 225 | } |
| 226 | |
| 227 | void SignalRunning() { semaphore_->Signal(); } |
| 228 | |
| 229 | private: |
| 230 | v8::internal::Semaphore* semaphore_; |
| 231 | bool run_; |
| 232 | pthread_t self_; |
| 233 | int v8_thread_id_; |
| 234 | }; |
| 235 | |
| 236 | |
| 237 | class LoopingJsThread : public LoopingThread { |
| 238 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 239 | explicit LoopingJsThread(v8::internal::Isolate* isolate) |
| 240 | : LoopingThread(isolate) { } |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 241 | void RunLoop() { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 242 | v8::Locker locker; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 243 | CHECK(i::Isolate::Current() != NULL); |
| 244 | CHECK_GT(i::Isolate::Current()->thread_manager()->CurrentId(), 0); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 245 | SetV8ThreadId(); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 246 | while (IsRunning()) { |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 247 | v8::HandleScope scope; |
| 248 | v8::Persistent<v8::Context> context = v8::Context::New(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 249 | CHECK(!context.IsEmpty()); |
| 250 | { |
| 251 | v8::Context::Scope context_scope(context); |
| 252 | SignalRunning(); |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 253 | CompileRun( |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 254 | "var j; for (var i=0; i<10000; ++i) { j = Math.sin(i); }"); |
| 255 | } |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 256 | context.Dispose(); |
| 257 | i::OS::Sleep(1); |
| 258 | } |
| 259 | } |
| 260 | }; |
| 261 | |
| 262 | |
| 263 | class LoopingNonJsThread : public LoopingThread { |
| 264 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 265 | explicit LoopingNonJsThread(v8::internal::Isolate* isolate) |
| 266 | : LoopingThread(isolate) { } |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 267 | void RunLoop() { |
| 268 | v8::Locker locker; |
| 269 | v8::Unlocker unlocker; |
| 270 | // Now thread has V8's id, but will not run VM code. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 271 | CHECK(i::Isolate::Current() != NULL); |
| 272 | CHECK_GT(i::Isolate::Current()->thread_manager()->CurrentId(), 0); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 273 | double i = 10; |
| 274 | SignalRunning(); |
| 275 | while (IsRunning()) { |
| 276 | i = sin(i); |
| 277 | i::OS::Sleep(1); |
| 278 | } |
| 279 | } |
| 280 | }; |
| 281 | |
| 282 | |
| 283 | class TestSampler : public v8::internal::Sampler { |
| 284 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 285 | explicit TestSampler(v8::internal::Isolate* isolate) |
| 286 | : Sampler(isolate, 0, true, true), |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 287 | semaphore_(v8::internal::OS::CreateSemaphore(0)), |
| 288 | was_sample_stack_called_(false) { |
| 289 | } |
| 290 | |
| 291 | ~TestSampler() { delete semaphore_; } |
| 292 | |
| 293 | void SampleStack(v8::internal::TickSample*) { |
| 294 | was_sample_stack_called_ = true; |
| 295 | } |
| 296 | |
| 297 | void Tick(v8::internal::TickSample*) { semaphore_->Signal(); } |
| 298 | |
| 299 | bool WaitForTick() { return semaphore_->Wait(1000000); } |
| 300 | |
| 301 | void Reset() { was_sample_stack_called_ = false; } |
| 302 | |
| 303 | bool WasSampleStackCalled() { return was_sample_stack_called_; } |
| 304 | |
| 305 | private: |
| 306 | v8::internal::Semaphore* semaphore_; |
| 307 | bool was_sample_stack_called_; |
| 308 | }; |
| 309 | |
| 310 | |
| 311 | } // namespace |
| 312 | |
| 313 | TEST(ProfMultipleThreads) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 314 | TestSampler* sampler = NULL; |
| 315 | { |
| 316 | v8::Locker locker; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 317 | sampler = new TestSampler(v8::internal::Isolate::Current()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 318 | sampler->Start(); |
| 319 | CHECK(sampler->IsActive()); |
| 320 | } |
| 321 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 322 | LoopingJsThread jsThread(v8::internal::Isolate::Current()); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 323 | jsThread.Start(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 324 | LoopingNonJsThread nonJsThread(v8::internal::Isolate::Current()); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 325 | nonJsThread.Start(); |
| 326 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 327 | CHECK(!sampler->WasSampleStackCalled()); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 328 | jsThread.WaitForRunning(); |
| 329 | jsThread.SendSigProf(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 330 | CHECK(sampler->WaitForTick()); |
| 331 | CHECK(sampler->WasSampleStackCalled()); |
| 332 | sampler->Reset(); |
| 333 | CHECK(!sampler->WasSampleStackCalled()); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 334 | nonJsThread.WaitForRunning(); |
| 335 | nonJsThread.SendSigProf(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 336 | CHECK(!sampler->WaitForTick()); |
| 337 | CHECK(!sampler->WasSampleStackCalled()); |
| 338 | sampler->Stop(); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 339 | |
| 340 | jsThread.Stop(); |
| 341 | nonJsThread.Stop(); |
| 342 | jsThread.Join(); |
| 343 | nonJsThread.Join(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 344 | |
| 345 | delete sampler; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | #endif // __linux__ |
| 349 | |
| 350 | |
sgjesse@chromium.org | 152a0b0 | 2009-10-07 13:50:16 +0000 | [diff] [blame] | 351 | // Test for issue http://crbug.com/23768 in Chromium. |
| 352 | // Heap can contain scripts with already disposed external sources. |
| 353 | // We need to verify that LogCompiledFunctions doesn't crash on them. |
| 354 | namespace { |
| 355 | |
| 356 | class SimpleExternalString : public v8::String::ExternalStringResource { |
| 357 | public: |
| 358 | explicit SimpleExternalString(const char* source) |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 359 | : utf_source_(StrLength(source)) { |
sgjesse@chromium.org | 152a0b0 | 2009-10-07 13:50:16 +0000 | [diff] [blame] | 360 | for (int i = 0; i < utf_source_.length(); ++i) |
| 361 | utf_source_[i] = source[i]; |
| 362 | } |
| 363 | virtual ~SimpleExternalString() {} |
| 364 | virtual size_t length() const { return utf_source_.length(); } |
| 365 | virtual const uint16_t* data() const { return utf_source_.start(); } |
| 366 | private: |
| 367 | i::ScopedVector<uint16_t> utf_source_; |
| 368 | }; |
| 369 | |
| 370 | } // namespace |
| 371 | |
| 372 | TEST(Issue23768) { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 373 | v8::HandleScope scope(v8::Isolate::GetCurrent()); |
sgjesse@chromium.org | 152a0b0 | 2009-10-07 13:50:16 +0000 | [diff] [blame] | 374 | v8::Handle<v8::Context> env = v8::Context::New(); |
| 375 | env->Enter(); |
| 376 | |
| 377 | SimpleExternalString source_ext_str("(function ext() {})();"); |
| 378 | v8::Local<v8::String> source = v8::String::NewExternal(&source_ext_str); |
| 379 | // Script needs to have a name in order to trigger InitLineEnds execution. |
| 380 | v8::Handle<v8::String> origin = v8::String::New("issue-23768-test"); |
| 381 | v8::Handle<v8::Script> evil_script = v8::Script::Compile(source, origin); |
| 382 | CHECK(!evil_script.IsEmpty()); |
| 383 | CHECK(!evil_script->Run().IsEmpty()); |
| 384 | i::Handle<i::ExternalTwoByteString> i_source( |
| 385 | i::ExternalTwoByteString::cast(*v8::Utils::OpenHandle(*source))); |
| 386 | // This situation can happen if source was an external string disposed |
| 387 | // by its owner. |
| 388 | i_source->set_resource(NULL); |
| 389 | |
| 390 | // Must not crash. |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 391 | i::Isolate::Current()->logger()->LogCompiledFunctions(); |
sgjesse@chromium.org | 152a0b0 | 2009-10-07 13:50:16 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | |
ager@chromium.org | 01beca7 | 2009-11-24 14:29:16 +0000 | [diff] [blame] | 395 | static v8::Handle<v8::Value> ObjMethod1(const v8::Arguments& args) { |
| 396 | return v8::Handle<v8::Value>(); |
| 397 | } |
| 398 | |
| 399 | TEST(LogCallbacks) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 400 | ScopedLoggerInitializer initialize_logger(false); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 401 | Logger* logger = initialize_logger.logger(); |
ager@chromium.org | 01beca7 | 2009-11-24 14:29:16 +0000 | [diff] [blame] | 402 | |
| 403 | v8::Persistent<v8::FunctionTemplate> obj = |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 404 | v8::Persistent<v8::FunctionTemplate>::New(v8::Isolate::GetCurrent(), |
| 405 | v8::FunctionTemplate::New()); |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 406 | obj->SetClassName(v8_str("Obj")); |
ager@chromium.org | 01beca7 | 2009-11-24 14:29:16 +0000 | [diff] [blame] | 407 | v8::Handle<v8::ObjectTemplate> proto = obj->PrototypeTemplate(); |
| 408 | v8::Local<v8::Signature> signature = v8::Signature::New(obj); |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 409 | proto->Set(v8_str("method1"), |
ager@chromium.org | 01beca7 | 2009-11-24 14:29:16 +0000 | [diff] [blame] | 410 | v8::FunctionTemplate::New(ObjMethod1, |
| 411 | v8::Handle<v8::Value>(), |
| 412 | signature), |
| 413 | static_cast<v8::PropertyAttribute>(v8::DontDelete)); |
| 414 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 415 | initialize_logger.env()->Global()->Set(v8_str("Obj"), obj->GetFunction()); |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 416 | CompileRun("Obj.prototype.method1.toString();"); |
ager@chromium.org | 01beca7 | 2009-11-24 14:29:16 +0000 | [diff] [blame] | 417 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 418 | logger->LogCompiledFunctions(); |
ager@chromium.org | 01beca7 | 2009-11-24 14:29:16 +0000 | [diff] [blame] | 419 | |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 420 | bool exists = false; |
| 421 | i::Vector<const char> log( |
| 422 | i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true)); |
| 423 | CHECK(exists); |
| 424 | |
| 425 | i::EmbeddedVector<char, 100> ref_data; |
ager@chromium.org | 01beca7 | 2009-11-24 14:29:16 +0000 | [diff] [blame] | 426 | i::OS::SNPrintF(ref_data, |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 427 | "code-creation,Callback,-3,0x%" V8PRIxPTR ",1,\"method1\"\0", |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 428 | ObjMethod1); |
| 429 | |
| 430 | CHECK_NE(NULL, StrNStr(log.start(), ref_data.start(), log.length())); |
ager@chromium.org | 01beca7 | 2009-11-24 14:29:16 +0000 | [diff] [blame] | 431 | |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 432 | obj.Dispose(v8::Isolate::GetCurrent()); |
ager@chromium.org | 01beca7 | 2009-11-24 14:29:16 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 436 | static v8::Handle<v8::Value> Prop1Getter(v8::Local<v8::String> property, |
| 437 | const v8::AccessorInfo& info) { |
| 438 | return v8::Handle<v8::Value>(); |
| 439 | } |
| 440 | |
| 441 | static void Prop1Setter(v8::Local<v8::String> property, |
| 442 | v8::Local<v8::Value> value, |
| 443 | const v8::AccessorInfo& info) { |
| 444 | } |
| 445 | |
| 446 | static v8::Handle<v8::Value> Prop2Getter(v8::Local<v8::String> property, |
| 447 | const v8::AccessorInfo& info) { |
| 448 | return v8::Handle<v8::Value>(); |
| 449 | } |
| 450 | |
| 451 | TEST(LogAccessorCallbacks) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 452 | ScopedLoggerInitializer initialize_logger(false); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 453 | Logger* logger = initialize_logger.logger(); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 454 | |
| 455 | v8::Persistent<v8::FunctionTemplate> obj = |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 456 | v8::Persistent<v8::FunctionTemplate>::New(v8::Isolate::GetCurrent(), |
| 457 | v8::FunctionTemplate::New()); |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 458 | obj->SetClassName(v8_str("Obj")); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 459 | v8::Handle<v8::ObjectTemplate> inst = obj->InstanceTemplate(); |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 460 | inst->SetAccessor(v8_str("prop1"), Prop1Getter, Prop1Setter); |
| 461 | inst->SetAccessor(v8_str("prop2"), Prop2Getter); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 462 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 463 | logger->LogAccessorCallbacks(); |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 464 | |
| 465 | bool exists = false; |
| 466 | i::Vector<const char> log( |
| 467 | i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true)); |
| 468 | CHECK(exists); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 469 | |
| 470 | EmbeddedVector<char, 100> prop1_getter_record; |
| 471 | i::OS::SNPrintF(prop1_getter_record, |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 472 | "code-creation,Callback,-3,0x%" V8PRIxPTR ",1,\"get prop1\"", |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 473 | Prop1Getter); |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 474 | CHECK_NE(NULL, |
| 475 | StrNStr(log.start(), prop1_getter_record.start(), log.length())); |
| 476 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 477 | EmbeddedVector<char, 100> prop1_setter_record; |
| 478 | i::OS::SNPrintF(prop1_setter_record, |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 479 | "code-creation,Callback,-3,0x%" V8PRIxPTR ",1,\"set prop1\"", |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 480 | Prop1Setter); |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 481 | CHECK_NE(NULL, |
| 482 | StrNStr(log.start(), prop1_setter_record.start(), log.length())); |
| 483 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 484 | EmbeddedVector<char, 100> prop2_getter_record; |
| 485 | i::OS::SNPrintF(prop2_getter_record, |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 486 | "code-creation,Callback,-3,0x%" V8PRIxPTR ",1,\"get prop2\"", |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 487 | Prop2Getter); |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 488 | CHECK_NE(NULL, |
| 489 | StrNStr(log.start(), prop2_getter_record.start(), log.length())); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 490 | |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 491 | obj.Dispose(v8::Isolate::GetCurrent()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 492 | } |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 493 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 494 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 495 | TEST(IsLoggingPreserved) { |
| 496 | ScopedLoggerInitializer initialize_logger(false); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 497 | Logger* logger = initialize_logger.logger(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 498 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 499 | CHECK(logger->is_logging()); |
| 500 | logger->ResumeProfiler(); |
| 501 | CHECK(logger->is_logging()); |
| 502 | logger->PauseProfiler(); |
| 503 | CHECK(logger->is_logging()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 507 | typedef i::NativesCollection<i::TEST> TestSources; |
| 508 | |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 509 | |
| 510 | // Test that logging of code create / move events is equivalent to traversal of |
| 511 | // a resulting heap. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 512 | TEST(EquivalenceOfLoggingAndTraversal) { |
| 513 | // This test needs to be run on a "clean" V8 to ensure that snapshot log |
| 514 | // is loaded. This is always true when running using tools/test.py because |
| 515 | // it launches a new cctest instance for every test. To be sure that launching |
| 516 | // cctest manually also works, please be sure that no tests below |
| 517 | // are using V8. |
| 518 | // |
| 519 | // P.S. No, V8 can't be re-initialized after disposal, see include/v8.h. |
| 520 | CHECK(!i::V8::IsRunning()); |
| 521 | |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 522 | // Start with profiling to capture all code events from the beginning. |
| 523 | ScopedLoggerInitializer initialize_logger(false); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 524 | Logger* logger = initialize_logger.logger(); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 525 | |
| 526 | // Compile and run a function that creates other functions. |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 527 | CompileRun( |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 528 | "(function f(obj) {\n" |
| 529 | " obj.test =\n" |
| 530 | " (function a(j) { return function b() { return j; } })(100);\n" |
| 531 | "})(this);"); |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 532 | v8::V8::PauseProfiler(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 533 | HEAP->CollectAllGarbage(i::Heap::kMakeHeapIterableMask); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 534 | logger->StringEvent("test-logging-done", ""); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 535 | |
| 536 | // Iterate heap to find compiled functions, will write to log. |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 537 | logger->LogCompiledFunctions(); |
| 538 | logger->StringEvent("test-traversal-done", ""); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 539 | |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 540 | bool exists = false; |
| 541 | i::Vector<const char> log( |
| 542 | i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true)); |
| 543 | CHECK(exists); |
| 544 | v8::Handle<v8::String> log_str = v8::String::New(log.start(), log.length()); |
| 545 | initialize_logger.env()->Global()->Set(v8_str("_log"), log_str); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 546 | |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 547 | i::Vector<const unsigned char> source = TestSources::GetScriptsSource(); |
| 548 | v8::Handle<v8::String> source_str = v8::String::New( |
| 549 | reinterpret_cast<const char*>(source.start()), source.length()); |
| 550 | v8::TryCatch try_catch; |
| 551 | v8::Handle<v8::Script> script = v8::Script::Compile(source_str, v8_str("")); |
| 552 | if (script.IsEmpty()) { |
| 553 | v8::String::Utf8Value exception(try_catch.Exception()); |
| 554 | printf("compile: %s\n", *exception); |
| 555 | CHECK(false); |
| 556 | } |
| 557 | v8::Handle<v8::Value> result = script->Run(); |
| 558 | if (result.IsEmpty()) { |
| 559 | v8::String::Utf8Value exception(try_catch.Exception()); |
| 560 | printf("run: %s\n", *exception); |
| 561 | CHECK(false); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 562 | } |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 563 | // The result either be a "true" literal or problem description. |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 564 | if (!result->IsTrue()) { |
| 565 | v8::Local<v8::String> s = result->ToString(); |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 566 | i::ScopedVector<char> data(s->Length() + 1); |
| 567 | CHECK_NE(NULL, data.start()); |
| 568 | s->WriteAscii(data.start()); |
| 569 | printf("%s\n", data.start()); |
| 570 | // Make sure that our output is written prior crash due to CHECK failure. |
| 571 | fflush(stdout); |
| 572 | CHECK(false); |
| 573 | } |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 574 | } |