Index: test/cctest/test-sampler-api.cc |
diff --git a/test/cctest/test-sampler-api.cc b/test/cctest/test-sampler-api.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..8b0e216e0c4261c23f5d9362ca777015a1b8f371 |
--- /dev/null |
+++ b/test/cctest/test-sampler-api.cc |
@@ -0,0 +1,143 @@ |
+// Copyright 2014 the V8 project authors. All rights reserved. |
Benedikt Meurer
2014/08/27 04:17:23
Nit: Use new short copyright header.
gholap
2014/08/27 21:48:09
Done.
|
+// Redistribution and use in source and binary forms, with or without |
+// modification, are permitted provided that the following conditions are |
+// met: |
+// |
+// * Redistributions of source code must retain the above copyright |
+// notice, this list of conditions and the following disclaimer. |
+// * Redistributions in binary form must reproduce the above |
+// copyright notice, this list of conditions and the following |
+// disclaimer in the documentation and/or other materials provided |
+// with the distribution. |
+// * Neither the name of Google Inc. nor the names of its |
+// contributors may be used to endorse or promote products derived |
+// from this software without specific prior written permission. |
+// |
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
+// |
+// Tests the public sampling API in include/v8-sampler.h |
+ |
+#include "include/v8.h" |
+#include "include/v8-sampler.h" |
+ |
+#include "src/v8.h" |
+ |
+#include "src/base/platform/platform.h" |
+ |
+#include "test/cctest/cctest.h" |
+ |
+using v8::Local; |
+using v8::Script; |
+ |
+using v8::internal::Isolate; |
+ |
+namespace SamplerTester { |
Benedikt Meurer
2014/08/27 04:17:23
Use an anonymous namespace instead.
gholap
2014/08/27 21:48:09
Done.
|
+ // sampler thread waits on this semaphore. |
+ v8::base::Semaphore* SamplerSemaphore = new v8::base::Semaphore(0); |
+ |
+ // V8 thread (the JavaScript code) waits on this semaphore. |
+ v8::base::Semaphore* V8Semaphore = new v8::base::Semaphore(0); |
+ |
+ // The JavaScript calls this function when an full stack depth. |
+ void SignalAndWaitForSampler( |
+ const v8::FunctionCallbackInfo<v8::Value>& args) { |
+ // Tell the sampler that it can take a sample now. |
+ SamplerSemaphore->Signal(); |
+ |
+ // Wait for the sampler to finish collecting a sample. |
+ V8Semaphore->Wait(); |
+ } |
+} |
+ |
+ |
+// A thread which collects samples from v8. |
+class SamplerThread : public v8::base::Thread { |
Benedikt Meurer
2014/08/27 04:17:23
Nit: Mark as V8_FINAL.
gholap
2014/08/27 21:48:09
Done.
|
+ public: |
+ explicit SamplerThread(v8::Isolate* isolate); |
+ virtual ~SamplerThread() {} |
+ int sampleStackSize; |
+ |
+ virtual void Run(); |
+ void Stop(); |
+ |
+ private: |
+ v8::Isolate* isolate_; |
+}; |
+ |
+static const int kSamplerThreadStackSize = 64 * 1024; |
Benedikt Meurer
2014/08/27 04:17:24
There should be no need to manually override speci
gholap
2014/08/27 21:48:09
Done.
|
+ |
+SamplerThread::SamplerThread(v8::Isolate* isolate) |
Benedikt Meurer
2014/08/27 04:17:23
Nit: Inline method definition into declaration abo
gholap
2014/08/27 21:48:09
Done.
|
+ : Thread(v8::base::Thread::Options("sampler-api-tester", |
+ kSamplerThreadStackSize)), |
+ sampleStackSize(0), |
+ isolate_(isolate) { |
+} |
+ |
+ |
+void SamplerThread::Stop() { |
Benedikt Meurer
2014/08/27 04:17:23
Nit: Remove this method, there's no advantage over
gholap
2014/08/27 21:48:09
Done.
|
+ Join(); |
+} |
+ |
+ |
+void SamplerThread::Run() { |
Benedikt Meurer
2014/08/27 04:17:23
Nit: Inline method definition into declaration abo
gholap
2014/08/27 21:48:09
Done.
|
+ v8::Sample sample; |
+ |
+ // Wait for JS to reach full stack depth. |
+ SamplerTester::SamplerSemaphore->Wait(); |
+ |
+ v8::Sample* sample_ptr = &sample; |
Benedikt Meurer
2014/08/27 04:17:23
Replace with something like:
v8::Sample sample;
C
gholap
2014/08/27 21:48:09
Changed the API so that sample.frames_count is zer
|
+ sample_ptr = v8::Sampler::GetSample(isolate_, sample_ptr); |
+ CHECK_NE(NULL, sample_ptr); |
+ sampleStackSize = sample_ptr->frames_count; |
+ |
+ // Tell JS that sample collection is done. |
+ SamplerTester::V8Semaphore->Signal(); |
+} |
+ |
+ |
+// A JavaScript program with stack depth 8. |
+// When at the bottom of the recursion, |
+// The JavaScript code calls into C++ test code, |
+// waiting for the sampler to take a sample. |
+static const char* sampler_api_test_source = "function func(count) {\n" |
+" if (count == 1) {\n" |
+" SignalAndWaitForSampler();\n" |
+" }\n" |
+" else return func(count - 1);\n" |
+"}\n" |
+"func(7);\n"; |
+ |
+ |
+// TODO(gholap): Right now, we are just checking whether GetSample |
+// gets samples and whether the stack depth is reasonable. |
+// After implementing code event listener API, add tests |
+// to further verify the correctness of collected samples. |
+TEST(StackDepthIsConsistent) { |
+ v8::Isolate* isolate = CcTest::isolate(); |
+ v8::HandleScope scope(isolate); |
+ v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate); |
+ global->Set( |
+ v8::String::NewFromUtf8(isolate, "SignalAndWaitForSampler"), |
+ v8::FunctionTemplate::New(isolate, SamplerTester::SignalAndWaitForSampler)); |
+ |
+ LocalContext env(isolate, NULL, global); |
+ Local<v8::Script> script = v8::Script::Compile( |
+ v8::String::NewFromUtf8(isolate, sampler_api_test_source)); |
+ SamplerThread* sampler = new SamplerThread(isolate); |
Benedikt Meurer
2014/08/27 04:17:23
You leak the SamplerThread in this method. Just al
gholap
2014/08/27 21:48:09
Oops. Damn :(
Done.
|
+ |
+ sampler->Start(); |
+ script->Run(); |
+ sampler->Stop(); |
+ |
+ CHECK_EQ(8, sampler->sampleStackSize); |
Benedikt Meurer
2014/08/27 04:17:23
Remove this CHECK_EQ here.
gholap
2014/08/27 21:48:09
Yes, removed all CHECK_* logic from the Run method
|
+} |