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

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

Issue 2762323002: Reimplemented zone memory tracking to avoid race conditions that were causing crashes in the previo… (Closed)
Patch Set: Final change Created 3 years, 9 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/zone.cc ('k') | no next file » | 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 "platform/assert.h" 5 #include "platform/assert.h"
6 #include "vm/dart.h" 6 #include "vm/dart.h"
7 #include "vm/isolate.h" 7 #include "vm/isolate.h"
8 #include "vm/unit_test.h" 8 #include "vm/unit_test.h"
9 #include "vm/zone.h" 9 #include "vm/zone.h"
10 10
11 namespace dart { 11 namespace dart {
12 12
13 VM_UNIT_TEST_CASE(AllocateZone) { 13 VM_UNIT_TEST_CASE(AllocateZone) {
14 #if defined(DEBUG) 14 #if defined(DEBUG)
15 FLAG_trace_zones = true; 15 FLAG_trace_zones = true;
16 #endif 16 #endif
17 Dart_CreateIsolate(NULL, NULL, bin::core_isolate_snapshot_data, 17 Dart_CreateIsolate(NULL, NULL, bin::core_isolate_snapshot_data,
18 bin::core_isolate_snapshot_instructions, NULL, NULL, NULL); 18 bin::core_isolate_snapshot_instructions, NULL, NULL, NULL);
19 Thread* thread = Thread::Current(); 19 Thread* thread = Thread::Current();
20 EXPECT(thread->zone() == NULL); 20 EXPECT(thread->zone() == NULL);
21 { 21 {
22 StackZone stack_zone(thread); 22 StackZone stack_zone(thread);
23 EXPECT(thread->zone() != NULL); 23 EXPECT(thread->zone() != NULL);
24 Zone* zone = stack_zone.GetZone(); 24 Zone* zone = stack_zone.GetZone();
25 intptr_t allocated_size = 0; 25 uintptr_t allocated_size = 0;
26 26
27 // The loop is to make sure we overflow one segment and go on 27 // The loop is to make sure we overflow one segment and go on
28 // to the next segment. 28 // to the next segment.
29 for (int i = 0; i < 1000; i++) { 29 for (int i = 0; i < 1000; i++) {
30 uword first = zone->AllocUnsafe(2 * kWordSize); 30 uword first = zone->AllocUnsafe(2 * kWordSize);
31 uword second = zone->AllocUnsafe(3 * kWordSize); 31 uword second = zone->AllocUnsafe(3 * kWordSize);
32 EXPECT(first != second); 32 EXPECT(first != second);
33 allocated_size = ((2 + 3) * kWordSize); 33 allocated_size = ((2 + 3) * kWordSize);
34 } 34 }
35 EXPECT_LE(allocated_size, zone->SizeInBytes()); 35 EXPECT_LE(allocated_size, zone->SizeInBytes());
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 #if defined(DEBUG) 76 #if defined(DEBUG)
77 FLAG_trace_zones = true; 77 FLAG_trace_zones = true;
78 #endif 78 #endif
79 Dart_CreateIsolate(NULL, NULL, bin::core_isolate_snapshot_data, 79 Dart_CreateIsolate(NULL, NULL, bin::core_isolate_snapshot_data,
80 bin::core_isolate_snapshot_instructions, NULL, NULL, NULL); 80 bin::core_isolate_snapshot_instructions, NULL, NULL, NULL);
81 Thread* thread = Thread::Current(); 81 Thread* thread = Thread::Current();
82 EXPECT(thread->zone() == NULL); 82 EXPECT(thread->zone() == NULL);
83 { 83 {
84 StackZone zone(thread); 84 StackZone zone(thread);
85 EXPECT(thread->zone() != NULL); 85 EXPECT(thread->zone() != NULL);
86 intptr_t allocated_size = 0; 86 uintptr_t allocated_size = 0;
87 87
88 const intptr_t kNumElements = 1000; 88 const intptr_t kNumElements = 1000;
89 zone.GetZone()->Alloc<uint32_t>(kNumElements); 89 zone.GetZone()->Alloc<uint32_t>(kNumElements);
90 allocated_size += sizeof(uint32_t) * kNumElements; 90 allocated_size += sizeof(uint32_t) * kNumElements;
91 EXPECT_LE(allocated_size, zone.SizeInBytes()); 91 EXPECT_LE(allocated_size, zone.SizeInBytes());
92 } 92 }
93 EXPECT(thread->zone() == NULL); 93 EXPECT(thread->zone() == NULL);
94 Dart_ShutdownIsolate(); 94 Dart_ShutdownIsolate();
95 } 95 }
96 96
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 virtual int GetSlot() { return slot; } 132 virtual int GetSlot() { return slot; }
133 int slot; 133 int slot;
134 }; 134 };
135 135
136 // Reset the marker. 136 // Reset the marker.
137 marker = 0; 137 marker = 0;
138 138
139 // Create a few zone allocated objects. 139 // Create a few zone allocated objects.
140 { 140 {
141 StackZone zone(thread); 141 StackZone zone(thread);
142 EXPECT_EQ(0, zone.SizeInBytes()); 142 EXPECT_EQ(0UL, zone.SizeInBytes());
143 SimpleZoneObject* first = new SimpleZoneObject(); 143 SimpleZoneObject* first = new SimpleZoneObject();
144 EXPECT(first != NULL); 144 EXPECT(first != NULL);
145 SimpleZoneObject* second = new SimpleZoneObject(); 145 SimpleZoneObject* second = new SimpleZoneObject();
146 EXPECT(second != NULL); 146 EXPECT(second != NULL);
147 EXPECT(first != second); 147 EXPECT(first != second);
148 intptr_t expected_size = (2 * sizeof(SimpleZoneObject)); 148 uintptr_t expected_size = (2 * sizeof(SimpleZoneObject));
149 EXPECT_LE(expected_size, zone.SizeInBytes()); 149 EXPECT_LE(expected_size, zone.SizeInBytes());
150 150
151 // Make sure the constructors were invoked. 151 // Make sure the constructors were invoked.
152 EXPECT_EQ(0, first->slot); 152 EXPECT_EQ(0, first->slot);
153 EXPECT_EQ(1, second->slot); 153 EXPECT_EQ(1, second->slot);
154 154
155 // Make sure we can write to the members of the zone objects. 155 // Make sure we can write to the members of the zone objects.
156 first->slot = 42; 156 first->slot = 42;
157 second->slot = 87; 157 second->slot = 87;
158 EXPECT_EQ(42, first->slot); 158 EXPECT_EQ(42, first->slot);
159 EXPECT_EQ(87, second->slot); 159 EXPECT_EQ(87, second->slot);
160 } 160 }
161 EXPECT(thread->zone() == NULL); 161 EXPECT(thread->zone() == NULL);
162 Dart_ShutdownIsolate(); 162 Dart_ShutdownIsolate();
163 } 163 }
164 164
165 165
166 TEST_CASE(PrintToString) { 166 TEST_CASE(PrintToString) {
167 StackZone zone(Thread::Current()); 167 StackZone zone(Thread::Current());
168 const char* result = zone.GetZone()->PrintToString("Hello %s!", "World"); 168 const char* result = zone.GetZone()->PrintToString("Hello %s!", "World");
169 EXPECT_STREQ("Hello World!", result); 169 EXPECT_STREQ("Hello World!", result);
170 } 170 }
171 171
172 172
173 #ifndef PRODUCT
174 VM_UNIT_TEST_CASE(PrintZoneMemoryInfoToJSON) {
175 #if defined(DEBUG)
176 FLAG_trace_zones = true;
177 #endif
178 Dart_CreateIsolate(NULL, NULL, bin::core_isolate_snapshot_data,
179 bin::core_isolate_snapshot_instructions, NULL, NULL, NULL);
180 Thread* thread = Thread::Current();
181 EXPECT(thread->zone() == NULL);
182 {
183 StackZone zone(thread);
184 StackZone string_stack_zone(thread);
185 EXPECT(thread->zone() != NULL);
186
187 intptr_t allocated_size = 0;
188 const intptr_t kNumElements = 1000;
189
190 zone.GetZone()->Alloc<uint32_t>(kNumElements);
191 allocated_size += sizeof(uint32_t) * kNumElements;
192
193 EXPECT_LE(allocated_size, zone.SizeInBytes());
194 {
195 JSONStream stream;
196 // Get the JSON formated zone information.
197 zone.GetZone()->PrintJSON(&stream);
198 const char* json = stream.ToCString();
199 // Ensure that matches actual values.
200 char* size_buf =
201 OS::SCreate(string_stack_zone.GetZone(), "\"capacity\":%" Pd
202 ","
203 "\"used\":%" Pd "",
204 zone.CapacityInBytes(), zone.SizeInBytes());
205 EXPECT_LE(zone.SizeInBytes(), zone.CapacityInBytes());
206 EXPECT_SUBSTRING(size_buf, json);
207 }
208
209 // Expand the zone to ensure that JSON is updated accordingly.
210 zone.GetZone()->Alloc<uint32_t>(kNumElements);
211 allocated_size += sizeof(uint32_t) * kNumElements;
212 EXPECT_LE(allocated_size, zone.SizeInBytes());
213 {
214 JSONStream stream;
215 zone.GetZone()->PrintJSON(&stream);
216 const char* json = stream.ToCString();
217 char* size_buf =
218 OS::SCreate(string_stack_zone.GetZone(), "\"capacity\":%" Pd
219 ","
220 "\"used\":%" Pd "",
221 zone.CapacityInBytes(), zone.SizeInBytes());
222 EXPECT_LE(zone.SizeInBytes(), zone.CapacityInBytes());
223 EXPECT_SUBSTRING(size_buf, json);
224 }
225 }
226 EXPECT(thread->zone() == NULL);
227 Dart_ShutdownIsolate();
228 }
229 #endif
230
231
232 VM_UNIT_TEST_CASE(NativeScopeZoneAllocation) { 173 VM_UNIT_TEST_CASE(NativeScopeZoneAllocation) {
233 ASSERT(ApiNativeScope::Current() == NULL); 174 ASSERT(ApiNativeScope::Current() == NULL);
234 ASSERT(Thread::Current() == NULL); 175 ASSERT(Thread::Current() == NULL);
235 EXPECT_EQ(0, ApiNativeScope::current_memory_usage()); 176 EXPECT_EQ(0UL, ApiNativeScope::current_memory_usage());
236 { 177 {
237 ApiNativeScope scope; 178 ApiNativeScope scope;
238 EXPECT_EQ(scope.zone()->CapacityInBytes(), 179 EXPECT_EQ(scope.zone()->CapacityInBytes(),
239 ApiNativeScope::current_memory_usage()); 180 ApiNativeScope::current_memory_usage());
240 (void)Dart_ScopeAllocate(2048); 181 (void)Dart_ScopeAllocate(2048);
241 EXPECT_EQ(scope.zone()->CapacityInBytes(), 182 EXPECT_EQ(scope.zone()->CapacityInBytes(),
242 ApiNativeScope::current_memory_usage()); 183 ApiNativeScope::current_memory_usage());
243 } 184 }
244 EXPECT_EQ(0, ApiNativeScope::current_memory_usage()); 185 EXPECT_EQ(0UL, ApiNativeScope::current_memory_usage());
245 } 186 }
246 187
247 } // namespace dart 188 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/zone.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698