OLD | NEW |
1 // Copyright 2007-2010 the V8 project authors. All rights reserved. | 1 // Copyright 2007-2010 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 21 matching lines...) Expand all Loading... |
32 | 32 |
33 #include "debug.h" | 33 #include "debug.h" |
34 #include "ic-inl.h" | 34 #include "ic-inl.h" |
35 #include "runtime.h" | 35 #include "runtime.h" |
36 #include "serialize.h" | 36 #include "serialize.h" |
37 #include "scopeinfo.h" | 37 #include "scopeinfo.h" |
38 #include "snapshot.h" | 38 #include "snapshot.h" |
39 #include "cctest.h" | 39 #include "cctest.h" |
40 #include "spaces.h" | 40 #include "spaces.h" |
41 #include "objects.h" | 41 #include "objects.h" |
| 42 #include "natives.h" |
| 43 #include "bootstrapper.h" |
42 | 44 |
43 using namespace v8::internal; | 45 using namespace v8::internal; |
44 | 46 |
45 static const unsigned kCounters = 256; | 47 static const unsigned kCounters = 256; |
46 static int local_counters[kCounters]; | 48 static int local_counters[kCounters]; |
47 static const char* local_counter_names[kCounters]; | 49 static const char* local_counter_names[kCounters]; |
48 | 50 |
49 | 51 |
50 static unsigned CounterHash(const char* s) { | 52 static unsigned CounterHash(const char* s) { |
51 unsigned hash = 0; | 53 unsigned hash = 0; |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 decoder.Decode(make_code(UNCLASSIFIED, 4))); | 164 decoder.Decode(make_code(UNCLASSIFIED, 4))); |
163 CHECK_EQ(ExternalReference::address_of_real_stack_limit().address(), | 165 CHECK_EQ(ExternalReference::address_of_real_stack_limit().address(), |
164 decoder.Decode(make_code(UNCLASSIFIED, 5))); | 166 decoder.Decode(make_code(UNCLASSIFIED, 5))); |
165 CHECK_EQ(ExternalReference::debug_break().address(), | 167 CHECK_EQ(ExternalReference::debug_break().address(), |
166 decoder.Decode(make_code(UNCLASSIFIED, 11))); | 168 decoder.Decode(make_code(UNCLASSIFIED, 11))); |
167 CHECK_EQ(ExternalReference::new_space_start().address(), | 169 CHECK_EQ(ExternalReference::new_space_start().address(), |
168 decoder.Decode(make_code(UNCLASSIFIED, 7))); | 170 decoder.Decode(make_code(UNCLASSIFIED, 7))); |
169 } | 171 } |
170 | 172 |
171 | 173 |
| 174 class FileByteSink : public SnapshotByteSink { |
| 175 public: |
| 176 explicit FileByteSink(const char* snapshot_file) { |
| 177 fp_ = OS::FOpen(snapshot_file, "wb"); |
| 178 file_name_ = snapshot_file; |
| 179 if (fp_ == NULL) { |
| 180 PrintF("Unable to write to snapshot file \"%s\"\n", snapshot_file); |
| 181 exit(1); |
| 182 } |
| 183 } |
| 184 virtual ~FileByteSink() { |
| 185 if (fp_ != NULL) { |
| 186 fclose(fp_); |
| 187 } |
| 188 } |
| 189 virtual void Put(int byte, const char* description) { |
| 190 if (fp_ != NULL) { |
| 191 fputc(byte, fp_); |
| 192 } |
| 193 } |
| 194 virtual int Position() { |
| 195 return ftell(fp_); |
| 196 } |
| 197 void WriteSpaceUsed( |
| 198 int new_space_used, |
| 199 int pointer_space_used, |
| 200 int data_space_used, |
| 201 int code_space_used, |
| 202 int map_space_used, |
| 203 int cell_space_used, |
| 204 int large_space_used); |
| 205 |
| 206 private: |
| 207 FILE* fp_; |
| 208 const char* file_name_; |
| 209 }; |
| 210 |
| 211 |
| 212 void FileByteSink::WriteSpaceUsed( |
| 213 int new_space_used, |
| 214 int pointer_space_used, |
| 215 int data_space_used, |
| 216 int code_space_used, |
| 217 int map_space_used, |
| 218 int cell_space_used, |
| 219 int large_space_used) { |
| 220 int file_name_length = strlen(file_name_) + 10; |
| 221 Vector<char> name = Vector<char>::New(file_name_length + 1); |
| 222 OS::SNPrintF(name, "%s.size", file_name_); |
| 223 FILE* fp = OS::FOpen(name.start(), "w"); |
| 224 fprintf(fp, "new %d\n", new_space_used); |
| 225 fprintf(fp, "pointer %d\n", pointer_space_used); |
| 226 fprintf(fp, "data %d\n", data_space_used); |
| 227 fprintf(fp, "code %d\n", code_space_used); |
| 228 fprintf(fp, "map %d\n", map_space_used); |
| 229 fprintf(fp, "cell %d\n", cell_space_used); |
| 230 fprintf(fp, "large %d\n", large_space_used); |
| 231 fclose(fp); |
| 232 } |
| 233 |
| 234 |
| 235 static bool WriteToFile(const char* snapshot_file) { |
| 236 FileByteSink file(snapshot_file); |
| 237 StartupSerializer ser(&file); |
| 238 ser.Serialize(); |
| 239 return true; |
| 240 } |
| 241 |
| 242 |
172 static void Serialize() { | 243 static void Serialize() { |
173 // We have to create one context. One reason for this is so that the builtins | 244 // We have to create one context. One reason for this is so that the builtins |
174 // can be loaded from v8natives.js and their addresses can be processed. This | 245 // can be loaded from v8natives.js and their addresses can be processed. This |
175 // will clear the pending fixups array, which would otherwise contain GC roots | 246 // will clear the pending fixups array, which would otherwise contain GC roots |
176 // that would confuse the serialization/deserialization process. | 247 // that would confuse the serialization/deserialization process. |
177 v8::Persistent<v8::Context> env = v8::Context::New(); | 248 v8::Persistent<v8::Context> env = v8::Context::New(); |
178 env.Dispose(); | 249 env.Dispose(); |
179 Snapshot::WriteToFile(FLAG_testing_serialization_file); | 250 WriteToFile(FLAG_testing_serialization_file); |
180 } | 251 } |
181 | 252 |
182 | 253 |
183 // Test that the whole heap can be serialized. | 254 // Test that the whole heap can be serialized. |
184 TEST(Serialize) { | 255 TEST(Serialize) { |
185 Serializer::Enable(); | 256 Serializer::Enable(); |
186 v8::V8::Initialize(); | 257 v8::V8::Initialize(); |
187 Serialize(); | 258 Serialize(); |
188 } | 259 } |
189 | 260 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 v8::Persistent<v8::Context> env = v8::Context::New(); | 337 v8::Persistent<v8::Context> env = v8::Context::New(); |
267 env->Enter(); | 338 env->Enter(); |
268 | 339 |
269 const char* c_source = "\"1234\".length"; | 340 const char* c_source = "\"1234\".length"; |
270 v8::Local<v8::String> source = v8::String::New(c_source); | 341 v8::Local<v8::String> source = v8::String::New(c_source); |
271 v8::Local<v8::Script> script = v8::Script::Compile(source); | 342 v8::Local<v8::Script> script = v8::Script::Compile(source); |
272 CHECK_EQ(4, script->Run()->Int32Value()); | 343 CHECK_EQ(4, script->Run()->Int32Value()); |
273 } | 344 } |
274 | 345 |
275 | 346 |
276 class FileByteSink : public SnapshotByteSink { | |
277 public: | |
278 explicit FileByteSink(const char* snapshot_file) { | |
279 fp_ = OS::FOpen(snapshot_file, "wb"); | |
280 file_name_ = snapshot_file; | |
281 if (fp_ == NULL) { | |
282 PrintF("Unable to write to snapshot file \"%s\"\n", snapshot_file); | |
283 exit(1); | |
284 } | |
285 } | |
286 virtual ~FileByteSink() { | |
287 if (fp_ != NULL) { | |
288 fclose(fp_); | |
289 } | |
290 } | |
291 virtual void Put(int byte, const char* description) { | |
292 if (fp_ != NULL) { | |
293 fputc(byte, fp_); | |
294 } | |
295 } | |
296 virtual int Position() { | |
297 return ftell(fp_); | |
298 } | |
299 void WriteSpaceUsed( | |
300 int new_space_used, | |
301 int pointer_space_used, | |
302 int data_space_used, | |
303 int code_space_used, | |
304 int map_space_used, | |
305 int cell_space_used, | |
306 int large_space_used); | |
307 | |
308 private: | |
309 FILE* fp_; | |
310 const char* file_name_; | |
311 }; | |
312 | |
313 | |
314 void FileByteSink::WriteSpaceUsed( | |
315 int new_space_used, | |
316 int pointer_space_used, | |
317 int data_space_used, | |
318 int code_space_used, | |
319 int map_space_used, | |
320 int cell_space_used, | |
321 int large_space_used) { | |
322 int file_name_length = strlen(file_name_) + 10; | |
323 Vector<char> name = Vector<char>::New(file_name_length + 1); | |
324 OS::SNPrintF(name, "%s.size", file_name_); | |
325 FILE* fp = OS::FOpen(name.start(), "w"); | |
326 fprintf(fp, "new %d\n", new_space_used); | |
327 fprintf(fp, "pointer %d\n", pointer_space_used); | |
328 fprintf(fp, "data %d\n", data_space_used); | |
329 fprintf(fp, "code %d\n", code_space_used); | |
330 fprintf(fp, "map %d\n", map_space_used); | |
331 fprintf(fp, "cell %d\n", cell_space_used); | |
332 fprintf(fp, "large %d\n", large_space_used); | |
333 fclose(fp); | |
334 } | |
335 | |
336 | |
337 TEST(PartialSerialization) { | 347 TEST(PartialSerialization) { |
338 Serializer::Enable(); | 348 Serializer::Enable(); |
339 v8::V8::Initialize(); | 349 v8::V8::Initialize(); |
| 350 |
340 v8::Persistent<v8::Context> env = v8::Context::New(); | 351 v8::Persistent<v8::Context> env = v8::Context::New(); |
| 352 ASSERT(!env.IsEmpty()); |
341 env->Enter(); | 353 env->Enter(); |
| 354 // Make sure all builtin scripts are cached. |
| 355 { HandleScope scope; |
| 356 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) { |
| 357 Bootstrapper::NativesSourceLookup(i); |
| 358 } |
| 359 } |
| 360 Heap::CollectAllGarbage(true); |
| 361 Heap::CollectAllGarbage(true); |
342 | 362 |
343 v8::HandleScope handle_scope; | 363 Object* raw_foo; |
344 v8::Local<v8::String> foo = v8::String::New("foo"); | 364 { |
| 365 v8::HandleScope handle_scope; |
| 366 v8::Local<v8::String> foo = v8::String::New("foo"); |
| 367 ASSERT(!foo.IsEmpty()); |
| 368 raw_foo = *(v8::Utils::OpenHandle(*foo)); |
| 369 } |
345 | 370 |
346 FileByteSink file(FLAG_testing_serialization_file); | 371 int file_name_length = strlen(FLAG_testing_serialization_file) + 10; |
347 Serializer ser(&file); | 372 Vector<char> startup_name = Vector<char>::New(file_name_length + 1); |
348 i::Handle<i::String> internal_foo = v8::Utils::OpenHandle(*foo); | 373 OS::SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file); |
349 Object* raw_foo = *internal_foo; | 374 |
350 ser.SerializePartial(&raw_foo); | 375 env->Exit(); |
351 file.WriteSpaceUsed(ser.CurrentAllocationAddress(NEW_SPACE), | 376 env.Dispose(); |
352 ser.CurrentAllocationAddress(OLD_POINTER_SPACE), | 377 |
353 ser.CurrentAllocationAddress(OLD_DATA_SPACE), | 378 FileByteSink startup_sink(startup_name.start()); |
354 ser.CurrentAllocationAddress(CODE_SPACE), | 379 StartupSerializer startup_serializer(&startup_sink); |
355 ser.CurrentAllocationAddress(MAP_SPACE), | 380 startup_serializer.SerializeStrongReferences(); |
356 ser.CurrentAllocationAddress(CELL_SPACE), | 381 |
357 ser.CurrentAllocationAddress(LO_SPACE)); | 382 FileByteSink partial_sink(FLAG_testing_serialization_file); |
| 383 PartialSerializer p_ser(&startup_serializer, &partial_sink); |
| 384 p_ser.Serialize(&raw_foo); |
| 385 startup_serializer.SerializeWeakReferences(); |
| 386 partial_sink.WriteSpaceUsed(p_ser.CurrentAllocationAddress(NEW_SPACE), |
| 387 p_ser.CurrentAllocationAddress(OLD_POINTER_SPACE), |
| 388 p_ser.CurrentAllocationAddress(OLD_DATA_SPACE), |
| 389 p_ser.CurrentAllocationAddress(CODE_SPACE), |
| 390 p_ser.CurrentAllocationAddress(MAP_SPACE), |
| 391 p_ser.CurrentAllocationAddress(CELL_SPACE), |
| 392 p_ser.CurrentAllocationAddress(LO_SPACE)); |
358 } | 393 } |
359 | 394 |
360 | 395 |
361 DEPENDENT_TEST(PartialDeserialization, PartialSerialization) { | 396 DEPENDENT_TEST(PartialDeserialization, PartialSerialization) { |
362 v8::V8::Initialize(); | 397 int file_name_length = strlen(FLAG_testing_serialization_file) + 10; |
| 398 Vector<char> startup_name = Vector<char>::New(file_name_length + 1); |
| 399 OS::SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file); |
| 400 |
| 401 CHECK(Snapshot::Initialize(startup_name.start())); |
| 402 |
363 const char* file_name = FLAG_testing_serialization_file; | 403 const char* file_name = FLAG_testing_serialization_file; |
364 int file_name_length = strlen(file_name) + 10; | |
365 Vector<char> name = Vector<char>::New(file_name_length + 1); | 404 Vector<char> name = Vector<char>::New(file_name_length + 1); |
366 OS::SNPrintF(name, "%s.size", file_name); | 405 OS::SNPrintF(name, "%s.size", file_name); |
367 FILE* fp = OS::FOpen(name.start(), "r"); | 406 FILE* fp = OS::FOpen(name.start(), "r"); |
368 int new_size, pointer_size, data_size, code_size, map_size, cell_size; | 407 int new_size, pointer_size, data_size, code_size, map_size, cell_size; |
369 int large_size; | 408 int large_size; |
370 #ifdef _MSC_VER | 409 #ifdef _MSC_VER |
371 // Avoid warning about unsafe fscanf from MSVC. | 410 // Avoid warning about unsafe fscanf from MSVC. |
372 // Please note that this is only fine if %c and %s are not being used. | 411 // Please note that this is only fine if %c and %s are not being used. |
373 #define fscanf fscanf_s | 412 #define fscanf fscanf_s |
374 #endif | 413 #endif |
(...skipping 10 matching lines...) Expand all Loading... |
385 fclose(fp); | 424 fclose(fp); |
386 Heap::ReserveSpace(new_size, | 425 Heap::ReserveSpace(new_size, |
387 pointer_size, | 426 pointer_size, |
388 data_size, | 427 data_size, |
389 code_size, | 428 code_size, |
390 map_size, | 429 map_size, |
391 cell_size, | 430 cell_size, |
392 large_size); | 431 large_size); |
393 int snapshot_size = 0; | 432 int snapshot_size = 0; |
394 byte* snapshot = ReadBytes(file_name, &snapshot_size); | 433 byte* snapshot = ReadBytes(file_name, &snapshot_size); |
395 SnapshotByteSource source(snapshot, snapshot_size); | 434 |
396 Deserializer deserializer(&source); | |
397 Object* root; | 435 Object* root; |
398 deserializer.DeserializePartial(&root); | 436 { |
399 CHECK(root->IsString()); | 437 SnapshotByteSource source(snapshot, snapshot_size); |
| 438 Deserializer deserializer(&source); |
| 439 deserializer.DeserializePartial(&root); |
| 440 CHECK(root->IsString()); |
| 441 } |
| 442 v8::HandleScope handle_scope; |
| 443 Handle<Object>root_handle(root); |
| 444 |
| 445 Object* root2; |
| 446 { |
| 447 SnapshotByteSource source(snapshot, snapshot_size); |
| 448 Deserializer deserializer(&source); |
| 449 deserializer.DeserializePartial(&root2); |
| 450 CHECK(root2->IsString()); |
| 451 CHECK(*root_handle == root2); |
| 452 } |
400 } | 453 } |
401 | 454 |
402 | 455 |
403 TEST(LinearAllocation) { | 456 TEST(LinearAllocation) { |
404 v8::V8::Initialize(); | 457 v8::V8::Initialize(); |
405 int new_space_max = 512 * KB; | 458 int new_space_max = 512 * KB; |
406 for (int size = 1000; size < 5 * MB; size += size >> 1) { | 459 for (int size = 1000; size < 5 * MB; size += size >> 1) { |
407 int new_space_size = (size < new_space_max) ? size : new_space_max; | 460 int new_space_size = (size < new_space_max) ? size : new_space_max; |
408 Heap::ReserveSpace( | 461 Heap::ReserveSpace( |
409 new_space_size, | 462 new_space_size, |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
508 TEST(TestThatAlwaysFails) { | 561 TEST(TestThatAlwaysFails) { |
509 bool ArtificialFailure = false; | 562 bool ArtificialFailure = false; |
510 CHECK(ArtificialFailure); | 563 CHECK(ArtificialFailure); |
511 } | 564 } |
512 | 565 |
513 | 566 |
514 DEPENDENT_TEST(DependentTestThatAlwaysFails, TestThatAlwaysSucceeds) { | 567 DEPENDENT_TEST(DependentTestThatAlwaysFails, TestThatAlwaysSucceeds) { |
515 bool ArtificialFailure2 = false; | 568 bool ArtificialFailure2 = false; |
516 CHECK(ArtificialFailure2); | 569 CHECK(ArtificialFailure2); |
517 } | 570 } |
OLD | NEW |