Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(52)

Side by Side Diff: runtime/vm/isolate_test.cc

Issue 1277473004: Safe and efficient stack-limit based interrupt checking in C++. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Explicitly signal task ready. Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « runtime/vm/isolate.h ('k') | runtime/vm/vm_sources.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "include/dart_api.h" 5 #include "include/dart_api.h"
6 #include "platform/assert.h" 6 #include "platform/assert.h"
7 #include "vm/globals.h" 7 #include "vm/globals.h"
8 #include "vm/isolate.h" 8 #include "vm/isolate.h"
9 #include "vm/lockers.h"
10 #include "vm/thread_pool.h"
9 #include "vm/unit_test.h" 11 #include "vm/unit_test.h"
10 12
11 namespace dart { 13 namespace dart {
12 14
13 UNIT_TEST_CASE(IsolateCurrent) { 15 UNIT_TEST_CASE(IsolateCurrent) {
14 Dart_Isolate isolate = Dart_CreateIsolate( 16 Dart_Isolate isolate = Dart_CreateIsolate(
15 NULL, NULL, bin::isolate_snapshot_buffer, NULL, NULL, NULL); 17 NULL, NULL, bin::isolate_snapshot_buffer, NULL, NULL, NULL);
16 EXPECT_EQ(isolate, Dart_CurrentIsolate()); 18 EXPECT_EQ(isolate, Dart_CurrentIsolate());
17 Dart_ShutdownIsolate(); 19 Dart_ShutdownIsolate();
18 EXPECT_EQ(reinterpret_cast<Dart_Isolate>(NULL), Dart_CurrentIsolate()); 20 EXPECT_EQ(reinterpret_cast<Dart_Isolate>(NULL), Dart_CurrentIsolate());
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 result = Dart_Invoke(test_lib, NewString("testMain"), 0, NULL); 75 result = Dart_Invoke(test_lib, NewString("testMain"), 0, NULL);
74 EXPECT(!Dart_IsError(result)); 76 EXPECT(!Dart_IsError(result));
75 // Run until all ports to isolate are closed. 77 // Run until all ports to isolate are closed.
76 result = Dart_RunLoop(); 78 result = Dart_RunLoop();
77 EXPECT_ERROR(result, "Null callback specified for isolate creation"); 79 EXPECT_ERROR(result, "Null callback specified for isolate creation");
78 EXPECT(Dart_ErrorHasException(result)); 80 EXPECT(Dart_ErrorHasException(result));
79 Dart_Handle exception_result = Dart_ErrorGetException(result); 81 Dart_Handle exception_result = Dart_ErrorGetException(result);
80 EXPECT_VALID(exception_result); 82 EXPECT_VALID(exception_result);
81 } 83 }
82 84
85
86 class InterruptChecker : public ThreadPool::Task {
87 public:
88 static const intptr_t kTaskCount;
89 static const intptr_t kIterations;
90
91 InterruptChecker(Isolate* isolate,
92 Monitor* awake_monitor,
93 bool* awake,
94 Monitor* round_monitor,
95 const intptr_t* round)
96 : isolate_(isolate),
97 awake_monitor_(awake_monitor),
98 awake_(awake),
99 round_monitor_(round_monitor),
100 round_(round) {
101 }
102
103 virtual void Run() {
104 Thread::EnterIsolateAsHelper(isolate_);
105 // Tell main thread that we are ready.
106 {
107 MonitorLocker ml(awake_monitor_);
108 ASSERT(!*awake_);
109 *awake_ = true;
110 ml.Notify();
111 }
112 for (intptr_t i = 0; i < kIterations; ++i) {
113 // Busy wait for interrupts.
114 while (!isolate_->HasInterruptsScheduled(Isolate::kVMInterrupt)) {
115 // Do nothing.
116 }
117 // Tell main thread that we observed the interrupt.
118 {
119 MonitorLocker ml(awake_monitor_);
120 ASSERT(!*awake_);
121 *awake_ = true;
122 ml.Notify();
123 }
124 // Wait for main thread to let us resume, i.e., until all tasks are here.
125 {
126 MonitorLocker ml(round_monitor_);
127 EXPECT(*round_ == i || *round_ == (i + 1));
128 while (*round_ == i) {
129 ml.Wait();
130 }
131 EXPECT(*round_ == i + 1);
132 }
133 }
134 Thread::ExitIsolateAsHelper();
135 // Use awake also to signal exit.
136 {
137 MonitorLocker ml(awake_monitor_);
138 *awake_ = true;
139 ml.Notify();
140 }
141 }
142
143 private:
144 Isolate* isolate_;
145 Monitor* awake_monitor_;
146 bool* awake_;
147 Monitor* round_monitor_;
148 const intptr_t* round_;
149 };
150
151
152 const intptr_t InterruptChecker::kTaskCount = 50;
153 const intptr_t InterruptChecker::kIterations = 1000;
154
155
156 // Waits for all tasks to set their individual flag, then clears them all.
157 static void WaitForAllTasks(bool* flags, Monitor* monitor) {
158 MonitorLocker ml(monitor);
159 while (true) {
160 intptr_t count = 0;
161 for (intptr_t task = 0; task < InterruptChecker::kTaskCount; ++task) {
162 if (flags[task]) {
163 ++count;
164 }
165 }
166 if (count == InterruptChecker::kTaskCount) {
167 memset(flags, 0, sizeof(*flags) * count);
168 break;
169 } else {
170 ml.Wait();
171 }
172 }
173 }
174
175 // Test and document usage of Isolate::HasInterruptsScheduled.
176 //
177 // Go through a number of rounds of scheduling interrupts and waiting until all
178 // unsynchronized busy-waiting tasks observe it (in the current implementation,
179 // the exact latency depends on cache coherence). Synchronization is then used
180 // to ensure that the response to the interrupt, i.e., starting a new round,
181 // happens *after* the interrupt is observed. Without this synchronization, the
182 // compiler and/or CPU could reorder operations to make the tasks observe the
183 // round update *before* the interrupt is set.
184 TEST_CASE(StackLimitInterrupts) {
185 Monitor awake_monitor; // Synchronizes the 'awake' flags.
186 bool awake[InterruptChecker::kTaskCount];
187 memset(awake, 0, sizeof(awake));
188 Monitor round_monitor; // Synchronizes the 'round' counter.
189 intptr_t round = 0;
190 Isolate* isolate = Thread::Current()->isolate();
191 // Start all tasks. They will busy-wait until interrupted in the first round.
192 for (intptr_t task = 0; task < InterruptChecker::kTaskCount; task++) {
193 Dart::thread_pool()->Run(new InterruptChecker(
194 isolate, &awake_monitor, &awake[task], &round_monitor, &round));
195 }
196 // Wait for all tasks to get ready for the first round.
197 WaitForAllTasks(awake, &awake_monitor);
198 for (intptr_t i = 0; i < InterruptChecker::kIterations; ++i) {
199 isolate->ScheduleInterrupts(Isolate::kVMInterrupt);
200 // Wait for all tasks to observe the interrupt.
201 WaitForAllTasks(awake, &awake_monitor);
202 // Continue with next round.
203 uword interrupts = isolate->GetAndClearInterrupts();
204 EXPECT((interrupts & Isolate::kVMInterrupt) != 0);
205 {
206 MonitorLocker ml(&round_monitor);
207 ++round;
208 ml.NotifyAll();
209 }
210 }
211 // Wait for tasks to exit cleanly.
212 WaitForAllTasks(awake, &awake_monitor);
213 }
214
83 } // namespace dart 215 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/isolate.h ('k') | runtime/vm/vm_sources.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698