Line data Source code
1 : // Copyright 2017 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include "testing/gtest/include/gtest/gtest.h"
6 :
7 : #include "include/libplatform/libplatform.h"
8 : #include "include/v8-platform.h"
9 : #include "include/v8.h"
10 : #include "src/base/macros.h"
11 : #include "src/base/platform/semaphore.h"
12 : #include "src/execution.h"
13 : #include "src/isolate.h"
14 : #include "src/v8.h"
15 : #include "test/unittests/test-utils.h"
16 :
17 : namespace v8 {
18 :
19 : typedef TestWithIsolate IsolateTest;
20 :
21 : namespace {
22 :
23 : class MemoryPressureTask : public v8::Task {
24 : public:
25 : MemoryPressureTask(Isolate* isolate, base::Semaphore* semaphore)
26 1 : : isolate_(isolate), semaphore_(semaphore) {}
27 2 : ~MemoryPressureTask() override = default;
28 :
29 : // v8::Task implementation.
30 1 : void Run() override {
31 1 : isolate_->MemoryPressureNotification(MemoryPressureLevel::kCritical);
32 1 : semaphore_->Signal();
33 1 : }
34 :
35 : private:
36 : Isolate* isolate_;
37 : base::Semaphore* semaphore_;
38 :
39 : DISALLOW_COPY_AND_ASSIGN(MemoryPressureTask);
40 : };
41 :
42 : } // namespace
43 :
44 : // Check that triggering a memory pressure notification on the isolate thread
45 : // doesn't request a GC interrupt.
46 13159 : TEST_F(IsolateTest, MemoryPressureNotificationForeground) {
47 : internal::Isolate* i_isolate =
48 : reinterpret_cast<internal::Isolate*>(isolate());
49 :
50 3 : ASSERT_FALSE(i_isolate->stack_guard()->CheckGC());
51 1 : isolate()->MemoryPressureNotification(MemoryPressureLevel::kCritical);
52 2 : ASSERT_FALSE(i_isolate->stack_guard()->CheckGC());
53 : }
54 :
55 : // Check that triggering a memory pressure notification on an background thread
56 : // requests a GC interrupt.
57 13159 : TEST_F(IsolateTest, MemoryPressureNotificationBackground) {
58 : internal::Isolate* i_isolate =
59 : reinterpret_cast<internal::Isolate*>(isolate());
60 :
61 1 : base::Semaphore semaphore(0);
62 :
63 1 : internal::V8::GetCurrentPlatform()->CallOnBackgroundThread(
64 : new MemoryPressureTask(isolate(), &semaphore),
65 3 : v8::Platform::kShortRunningTask);
66 :
67 1 : semaphore.Wait();
68 :
69 3 : ASSERT_TRUE(i_isolate->stack_guard()->CheckGC());
70 1 : v8::platform::PumpMessageLoop(internal::V8::GetCurrentPlatform(), isolate());
71 : }
72 :
73 7893 : } // namespace v8
|