2012-02-07 08:00:36 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2008-08-22 13:33:59 +00:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2011-12-01 12:32:38 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
|
|
|
|
2008-08-22 13:33:59 +00:00
|
|
|
#include "v8.h"
|
|
|
|
|
|
|
|
#include "global-handles.h"
|
|
|
|
#include "snapshot.h"
|
|
|
|
#include "cctest.h"
|
|
|
|
|
|
|
|
using namespace v8::internal;
|
|
|
|
|
|
|
|
static v8::Persistent<v8::Context> env;
|
|
|
|
|
|
|
|
static void InitializeVM() {
|
|
|
|
if (env.IsEmpty()) env = v8::Context::New();
|
|
|
|
v8::HandleScope scope;
|
|
|
|
env->Enter();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-19 18:36:47 +00:00
|
|
|
TEST(MarkingDeque) {
|
2008-08-22 13:33:59 +00:00
|
|
|
int mem_size = 20 * kPointerSize;
|
|
|
|
byte* mem = NewArray<byte>(20*kPointerSize);
|
|
|
|
Address low = reinterpret_cast<Address>(mem);
|
|
|
|
Address high = low + mem_size;
|
2011-09-19 18:36:47 +00:00
|
|
|
MarkingDeque s;
|
2008-08-22 13:33:59 +00:00
|
|
|
s.Initialize(low, high);
|
|
|
|
|
|
|
|
Address address = NULL;
|
2011-09-19 18:36:47 +00:00
|
|
|
while (!s.IsFull()) {
|
|
|
|
s.PushBlack(HeapObject::FromAddress(address));
|
2008-08-22 13:33:59 +00:00
|
|
|
address += kPointerSize;
|
|
|
|
}
|
|
|
|
|
2011-09-19 18:36:47 +00:00
|
|
|
while (!s.IsEmpty()) {
|
2008-08-22 13:33:59 +00:00
|
|
|
Address value = s.Pop()->address();
|
|
|
|
address -= kPointerSize;
|
|
|
|
CHECK_EQ(address, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK_EQ(NULL, address);
|
|
|
|
DeleteArray(mem);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(Promotion) {
|
2010-12-07 11:31:57 +00:00
|
|
|
// This test requires compaction. If compaction is turned off, we
|
|
|
|
// skip the entire test.
|
|
|
|
if (FLAG_never_compact) return;
|
|
|
|
|
2008-08-22 13:33:59 +00:00
|
|
|
// Ensure that we get a compacting collection so that objects are promoted
|
|
|
|
// from new space.
|
|
|
|
FLAG_gc_global = true;
|
|
|
|
FLAG_always_compact = true;
|
2011-09-19 18:36:47 +00:00
|
|
|
HEAP->ConfigureHeap(2*256*KB, 8*MB, 8*MB);
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
InitializeVM();
|
|
|
|
|
|
|
|
v8::HandleScope sc;
|
|
|
|
|
|
|
|
// Allocate a fixed array in the new space.
|
2009-07-17 12:12:24 +00:00
|
|
|
int array_size =
|
2012-02-23 12:11:24 +00:00
|
|
|
(Page::kMaxNonCodeHeapObjectSize - FixedArray::kHeaderSize) /
|
2009-06-18 14:06:36 +00:00
|
|
|
(kPointerSize * 4);
|
2011-03-18 20:35:07 +00:00
|
|
|
Object* obj = HEAP->AllocateFixedArray(array_size)->ToObjectChecked();
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
Handle<FixedArray> array(FixedArray::cast(obj));
|
|
|
|
|
|
|
|
// Array should be in the new space.
|
2011-03-18 20:35:07 +00:00
|
|
|
CHECK(HEAP->InSpace(*array, NEW_SPACE));
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// Call the m-c collector, so array becomes an old object.
|
2011-03-18 20:35:07 +00:00
|
|
|
HEAP->CollectGarbage(OLD_POINTER_SPACE);
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// Array now sits in the old space
|
2011-03-18 20:35:07 +00:00
|
|
|
CHECK(HEAP->InSpace(*array, OLD_POINTER_SPACE));
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(NoPromotion) {
|
2011-09-19 18:36:47 +00:00
|
|
|
HEAP->ConfigureHeap(2*256*KB, 8*MB, 8*MB);
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// Test the situation that some objects in new space are promoted to
|
|
|
|
// the old space
|
|
|
|
InitializeVM();
|
|
|
|
|
|
|
|
v8::HandleScope sc;
|
|
|
|
|
|
|
|
// Do a mark compact GC to shrink the heap.
|
2011-03-18 20:35:07 +00:00
|
|
|
HEAP->CollectGarbage(OLD_POINTER_SPACE);
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// Allocate a big Fixed array in the new space.
|
2011-09-19 18:36:47 +00:00
|
|
|
int max_size =
|
2012-02-23 12:11:24 +00:00
|
|
|
Min(Page::kMaxNonCodeHeapObjectSize, HEAP->MaxObjectSizeInNewSpace());
|
2011-09-19 18:36:47 +00:00
|
|
|
|
|
|
|
int length = (max_size - FixedArray::kHeaderSize) / (2*kPointerSize);
|
|
|
|
Object* obj = i::Isolate::Current()->heap()->AllocateFixedArray(length)->
|
|
|
|
ToObjectChecked();
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
Handle<FixedArray> array(FixedArray::cast(obj));
|
|
|
|
|
|
|
|
// Array still stays in the new space.
|
2011-03-18 20:35:07 +00:00
|
|
|
CHECK(HEAP->InSpace(*array, NEW_SPACE));
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// Allocate objects in the old space until out of memory.
|
|
|
|
FixedArray* host = *array;
|
|
|
|
while (true) {
|
2010-10-25 15:22:03 +00:00
|
|
|
Object* obj;
|
2011-03-18 20:35:07 +00:00
|
|
|
{ MaybeObject* maybe_obj = HEAP->AllocateFixedArray(100, TENURED);
|
2010-10-25 15:22:03 +00:00
|
|
|
if (!maybe_obj->ToObject(&obj)) break;
|
|
|
|
}
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
host->set(0, obj);
|
|
|
|
host = FixedArray::cast(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call mark compact GC, and it should pass.
|
2011-03-18 20:35:07 +00:00
|
|
|
HEAP->CollectGarbage(OLD_POINTER_SPACE);
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(MarkCompactCollector) {
|
|
|
|
InitializeVM();
|
|
|
|
|
|
|
|
v8::HandleScope sc;
|
|
|
|
// call mark-compact when heap is empty
|
2011-03-18 20:35:07 +00:00
|
|
|
HEAP->CollectGarbage(OLD_POINTER_SPACE);
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// keep allocating garbage in new space until it fails
|
|
|
|
const int ARRAY_SIZE = 100;
|
|
|
|
Object* array;
|
2010-10-25 15:22:03 +00:00
|
|
|
MaybeObject* maybe_array;
|
2008-08-22 13:33:59 +00:00
|
|
|
do {
|
2011-03-18 20:35:07 +00:00
|
|
|
maybe_array = HEAP->AllocateFixedArray(ARRAY_SIZE);
|
2010-10-25 15:22:03 +00:00
|
|
|
} while (maybe_array->ToObject(&array));
|
2011-03-18 20:35:07 +00:00
|
|
|
HEAP->CollectGarbage(NEW_SPACE);
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
array = HEAP->AllocateFixedArray(ARRAY_SIZE)->ToObjectChecked();
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// keep allocating maps until it fails
|
|
|
|
Object* mapp;
|
2010-10-25 15:22:03 +00:00
|
|
|
MaybeObject* maybe_mapp;
|
2008-08-22 13:33:59 +00:00
|
|
|
do {
|
2011-03-18 20:35:07 +00:00
|
|
|
maybe_mapp = HEAP->AllocateMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
|
2010-10-25 15:22:03 +00:00
|
|
|
} while (maybe_mapp->ToObject(&mapp));
|
2011-03-18 20:35:07 +00:00
|
|
|
HEAP->CollectGarbage(MAP_SPACE);
|
|
|
|
mapp = HEAP->AllocateMap(JS_OBJECT_TYPE,
|
2010-10-25 15:22:03 +00:00
|
|
|
JSObject::kHeaderSize)->ToObjectChecked();
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// allocate a garbage
|
2010-10-25 15:22:03 +00:00
|
|
|
String* func_name =
|
2011-03-18 20:35:07 +00:00
|
|
|
String::cast(HEAP->LookupAsciiSymbol("theFunction")->ToObjectChecked());
|
2010-10-25 15:22:03 +00:00
|
|
|
SharedFunctionInfo* function_share = SharedFunctionInfo::cast(
|
2011-03-18 20:35:07 +00:00
|
|
|
HEAP->AllocateSharedFunctionInfo(func_name)->ToObjectChecked());
|
2010-10-25 15:22:03 +00:00
|
|
|
JSFunction* function = JSFunction::cast(
|
2011-03-18 20:35:07 +00:00
|
|
|
HEAP->AllocateFunction(*Isolate::Current()->function_map(),
|
2010-10-25 15:22:03 +00:00
|
|
|
function_share,
|
2011-03-18 20:35:07 +00:00
|
|
|
HEAP->undefined_value())->ToObjectChecked());
|
2008-08-22 13:33:59 +00:00
|
|
|
Map* initial_map =
|
2011-03-18 20:35:07 +00:00
|
|
|
Map::cast(HEAP->AllocateMap(JS_OBJECT_TYPE,
|
2010-10-25 15:22:03 +00:00
|
|
|
JSObject::kHeaderSize)->ToObjectChecked());
|
2008-08-22 13:33:59 +00:00
|
|
|
function->set_initial_map(initial_map);
|
2012-08-17 12:59:00 +00:00
|
|
|
Isolate::Current()->context()->global_object()->SetProperty(
|
2011-03-18 20:35:07 +00:00
|
|
|
func_name, function, NONE, kNonStrictMode)->ToObjectChecked();
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
JSObject* obj = JSObject::cast(
|
|
|
|
HEAP->AllocateJSObject(function)->ToObjectChecked());
|
|
|
|
HEAP->CollectGarbage(OLD_POINTER_SPACE);
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2010-10-25 15:22:03 +00:00
|
|
|
func_name =
|
2011-03-18 20:35:07 +00:00
|
|
|
String::cast(HEAP->LookupAsciiSymbol("theFunction")->ToObjectChecked());
|
2012-08-17 12:59:00 +00:00
|
|
|
CHECK(Isolate::Current()->context()->global_object()->
|
|
|
|
HasLocalProperty(func_name));
|
|
|
|
Object* func_value = Isolate::Current()->context()->global_object()->
|
2011-03-18 20:35:07 +00:00
|
|
|
GetProperty(func_name)->ToObjectChecked();
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK(func_value->IsJSFunction());
|
|
|
|
function = JSFunction::cast(func_value);
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
obj = JSObject::cast(HEAP->AllocateJSObject(function)->ToObjectChecked());
|
2010-10-25 15:22:03 +00:00
|
|
|
String* obj_name =
|
2011-03-18 20:35:07 +00:00
|
|
|
String::cast(HEAP->LookupAsciiSymbol("theObject")->ToObjectChecked());
|
2012-08-17 12:59:00 +00:00
|
|
|
Isolate::Current()->context()->global_object()->SetProperty(
|
2011-03-18 20:35:07 +00:00
|
|
|
obj_name, obj, NONE, kNonStrictMode)->ToObjectChecked();
|
2010-10-25 15:22:03 +00:00
|
|
|
String* prop_name =
|
2011-03-18 20:35:07 +00:00
|
|
|
String::cast(HEAP->LookupAsciiSymbol("theSlot")->ToObjectChecked());
|
2011-03-02 04:53:43 +00:00
|
|
|
obj->SetProperty(prop_name,
|
|
|
|
Smi::FromInt(23),
|
|
|
|
NONE,
|
|
|
|
kNonStrictMode)->ToObjectChecked();
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
HEAP->CollectGarbage(OLD_POINTER_SPACE);
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2010-10-25 15:22:03 +00:00
|
|
|
obj_name =
|
2011-03-18 20:35:07 +00:00
|
|
|
String::cast(HEAP->LookupAsciiSymbol("theObject")->ToObjectChecked());
|
2012-08-17 12:59:00 +00:00
|
|
|
CHECK(Isolate::Current()->context()->global_object()->
|
|
|
|
HasLocalProperty(obj_name));
|
|
|
|
CHECK(Isolate::Current()->context()->global_object()->
|
2011-03-18 20:35:07 +00:00
|
|
|
GetProperty(obj_name)->ToObjectChecked()->IsJSObject());
|
2012-08-17 12:59:00 +00:00
|
|
|
obj = JSObject::cast(Isolate::Current()->context()->global_object()->
|
2011-03-18 20:35:07 +00:00
|
|
|
GetProperty(obj_name)->ToObjectChecked());
|
2010-10-25 15:22:03 +00:00
|
|
|
prop_name =
|
2011-03-18 20:35:07 +00:00
|
|
|
String::cast(HEAP->LookupAsciiSymbol("theSlot")->ToObjectChecked());
|
|
|
|
CHECK(obj->GetProperty(prop_name) == Smi::FromInt(23));
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-19 18:36:47 +00:00
|
|
|
// TODO(1600): compaction of map space is temporary removed from GC.
|
|
|
|
#if 0
|
2010-01-19 16:34:37 +00:00
|
|
|
static Handle<Map> CreateMap() {
|
2011-03-18 20:35:07 +00:00
|
|
|
return FACTORY->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
|
2010-01-19 16:34:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(MapCompact) {
|
|
|
|
FLAG_max_map_space_pages = 16;
|
|
|
|
InitializeVM();
|
|
|
|
|
|
|
|
{
|
|
|
|
v8::HandleScope sc;
|
|
|
|
// keep allocating maps while pointers are still encodable and thus
|
|
|
|
// mark compact is permitted.
|
2011-03-18 20:35:07 +00:00
|
|
|
Handle<JSObject> root = FACTORY->NewJSObjectFromMap(CreateMap());
|
2010-01-19 16:34:37 +00:00
|
|
|
do {
|
|
|
|
Handle<Map> map = CreateMap();
|
|
|
|
map->set_prototype(*root);
|
2011-03-18 20:35:07 +00:00
|
|
|
root = FACTORY->NewJSObjectFromMap(map);
|
|
|
|
} while (HEAP->map_space()->MapPointersEncodable());
|
2010-01-19 16:34:37 +00:00
|
|
|
}
|
|
|
|
// Now, as we don't have any handles to just allocated maps, we should
|
|
|
|
// be able to trigger map compaction.
|
|
|
|
// To give an additional chance to fail, try to force compaction which
|
|
|
|
// should be impossible right now.
|
2011-09-19 18:36:47 +00:00
|
|
|
HEAP->CollectAllGarbage(Heap::kForceCompactionMask);
|
2010-01-19 16:34:37 +00:00
|
|
|
// And now map pointers should be encodable again.
|
2011-03-18 20:35:07 +00:00
|
|
|
CHECK(HEAP->map_space()->MapPointersEncodable());
|
2010-01-19 16:34:37 +00:00
|
|
|
}
|
2011-09-19 18:36:47 +00:00
|
|
|
#endif
|
2010-01-19 16:34:37 +00:00
|
|
|
|
2008-08-22 13:33:59 +00:00
|
|
|
static int gc_starts = 0;
|
|
|
|
static int gc_ends = 0;
|
|
|
|
|
|
|
|
static void GCPrologueCallbackFunc() {
|
|
|
|
CHECK(gc_starts == gc_ends);
|
|
|
|
gc_starts++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void GCEpilogueCallbackFunc() {
|
|
|
|
CHECK(gc_starts == gc_ends + 1);
|
|
|
|
gc_ends++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(GCCallback) {
|
|
|
|
InitializeVM();
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
HEAP->SetGlobalGCPrologueCallback(&GCPrologueCallbackFunc);
|
|
|
|
HEAP->SetGlobalGCEpilogueCallback(&GCEpilogueCallbackFunc);
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// Scavenge does not call GC callback functions.
|
2011-03-18 20:35:07 +00:00
|
|
|
HEAP->PerformScavenge();
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
CHECK_EQ(0, gc_starts);
|
|
|
|
CHECK_EQ(gc_ends, gc_starts);
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
HEAP->CollectGarbage(OLD_POINTER_SPACE);
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK_EQ(1, gc_starts);
|
|
|
|
CHECK_EQ(gc_ends, gc_starts);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int NumberOfWeakCalls = 0;
|
2008-10-23 08:25:23 +00:00
|
|
|
static void WeakPointerCallback(v8::Persistent<v8::Value> handle, void* id) {
|
2011-03-16 12:02:28 +00:00
|
|
|
ASSERT(id == reinterpret_cast<void*>(1234));
|
2008-08-22 13:33:59 +00:00
|
|
|
NumberOfWeakCalls++;
|
2010-07-19 13:26:25 +00:00
|
|
|
handle.Dispose();
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ObjectGroups) {
|
2012-11-27 15:30:12 +00:00
|
|
|
FLAG_incremental_marking = false;
|
2008-08-22 13:33:59 +00:00
|
|
|
InitializeVM();
|
2011-04-06 19:17:54 +00:00
|
|
|
GlobalHandles* global_handles = Isolate::Current()->global_handles();
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
NumberOfWeakCalls = 0;
|
|
|
|
v8::HandleScope handle_scope;
|
|
|
|
|
|
|
|
Handle<Object> g1s1 =
|
2011-03-18 20:35:07 +00:00
|
|
|
global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked());
|
2008-08-22 13:33:59 +00:00
|
|
|
Handle<Object> g1s2 =
|
2011-04-06 19:17:54 +00:00
|
|
|
global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked());
|
2011-03-16 12:02:28 +00:00
|
|
|
Handle<Object> g1c1 =
|
2011-04-06 19:17:54 +00:00
|
|
|
global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked());
|
2011-03-18 20:35:07 +00:00
|
|
|
global_handles->MakeWeak(g1s1.location(),
|
|
|
|
reinterpret_cast<void*>(1234),
|
|
|
|
&WeakPointerCallback);
|
|
|
|
global_handles->MakeWeak(g1s2.location(),
|
|
|
|
reinterpret_cast<void*>(1234),
|
|
|
|
&WeakPointerCallback);
|
|
|
|
global_handles->MakeWeak(g1c1.location(),
|
|
|
|
reinterpret_cast<void*>(1234),
|
|
|
|
&WeakPointerCallback);
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
Handle<Object> g2s1 =
|
2011-03-18 20:35:07 +00:00
|
|
|
global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked());
|
2008-08-22 13:33:59 +00:00
|
|
|
Handle<Object> g2s2 =
|
2011-03-18 20:35:07 +00:00
|
|
|
global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked());
|
2011-03-16 12:02:28 +00:00
|
|
|
Handle<Object> g2c1 =
|
2011-03-18 20:35:07 +00:00
|
|
|
global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked());
|
|
|
|
global_handles->MakeWeak(g2s1.location(),
|
|
|
|
reinterpret_cast<void*>(1234),
|
|
|
|
&WeakPointerCallback);
|
|
|
|
global_handles->MakeWeak(g2s2.location(),
|
|
|
|
reinterpret_cast<void*>(1234),
|
|
|
|
&WeakPointerCallback);
|
|
|
|
global_handles->MakeWeak(g2c1.location(),
|
|
|
|
reinterpret_cast<void*>(1234),
|
|
|
|
&WeakPointerCallback);
|
|
|
|
|
|
|
|
Handle<Object> root = global_handles->Create(*g1s1); // make a root.
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// Connect group 1 and 2, make a cycle.
|
|
|
|
Handle<FixedArray>::cast(g1s2)->set(0, *g2s2);
|
|
|
|
Handle<FixedArray>::cast(g2s1)->set(0, *g1s1);
|
|
|
|
|
2008-12-11 11:20:04 +00:00
|
|
|
{
|
|
|
|
Object** g1_objects[] = { g1s1.location(), g1s2.location() };
|
2011-03-16 12:02:28 +00:00
|
|
|
Object** g1_children[] = { g1c1.location() };
|
2008-12-11 11:20:04 +00:00
|
|
|
Object** g2_objects[] = { g2s1.location(), g2s2.location() };
|
2011-03-16 12:02:28 +00:00
|
|
|
Object** g2_children[] = { g2c1.location() };
|
2011-03-18 20:35:07 +00:00
|
|
|
global_handles->AddObjectGroup(g1_objects, 2, NULL);
|
2011-04-06 19:17:54 +00:00
|
|
|
global_handles->AddImplicitReferences(
|
|
|
|
Handle<HeapObject>::cast(g1s1).location(), g1_children, 1);
|
2011-03-18 20:35:07 +00:00
|
|
|
global_handles->AddObjectGroup(g2_objects, 2, NULL);
|
2011-04-06 19:17:54 +00:00
|
|
|
global_handles->AddImplicitReferences(
|
|
|
|
Handle<HeapObject>::cast(g2s2).location(), g2_children, 1);
|
2008-12-11 11:20:04 +00:00
|
|
|
}
|
2008-08-22 13:33:59 +00:00
|
|
|
// Do a full GC
|
2011-03-18 20:35:07 +00:00
|
|
|
HEAP->CollectGarbage(OLD_POINTER_SPACE);
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// All object should be alive.
|
|
|
|
CHECK_EQ(0, NumberOfWeakCalls);
|
|
|
|
|
|
|
|
// Weaken the root.
|
2011-03-18 20:35:07 +00:00
|
|
|
global_handles->MakeWeak(root.location(),
|
|
|
|
reinterpret_cast<void*>(1234),
|
|
|
|
&WeakPointerCallback);
|
2011-03-16 12:02:28 +00:00
|
|
|
// But make children strong roots---all the objects (except for children)
|
|
|
|
// should be collectable now.
|
2011-03-18 20:35:07 +00:00
|
|
|
global_handles->ClearWeakness(g1c1.location());
|
|
|
|
global_handles->ClearWeakness(g2c1.location());
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// Groups are deleted, rebuild groups.
|
2008-12-11 11:20:04 +00:00
|
|
|
{
|
|
|
|
Object** g1_objects[] = { g1s1.location(), g1s2.location() };
|
2011-03-16 12:02:28 +00:00
|
|
|
Object** g1_children[] = { g1c1.location() };
|
2008-12-11 11:20:04 +00:00
|
|
|
Object** g2_objects[] = { g2s1.location(), g2s2.location() };
|
2011-03-16 12:02:28 +00:00
|
|
|
Object** g2_children[] = { g2c1.location() };
|
2011-03-18 20:35:07 +00:00
|
|
|
global_handles->AddObjectGroup(g1_objects, 2, NULL);
|
2011-04-06 19:17:54 +00:00
|
|
|
global_handles->AddImplicitReferences(
|
|
|
|
Handle<HeapObject>::cast(g1s1).location(), g1_children, 1);
|
2011-03-18 20:35:07 +00:00
|
|
|
global_handles->AddObjectGroup(g2_objects, 2, NULL);
|
2011-04-06 19:17:54 +00:00
|
|
|
global_handles->AddImplicitReferences(
|
|
|
|
Handle<HeapObject>::cast(g2s2).location(), g2_children, 1);
|
2008-12-11 11:20:04 +00:00
|
|
|
}
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
HEAP->CollectGarbage(OLD_POINTER_SPACE);
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// All objects should be gone. 5 global handles in total.
|
|
|
|
CHECK_EQ(5, NumberOfWeakCalls);
|
2011-03-16 12:02:28 +00:00
|
|
|
|
|
|
|
// And now make children weak again and collect them.
|
2011-03-18 20:35:07 +00:00
|
|
|
global_handles->MakeWeak(g1c1.location(),
|
|
|
|
reinterpret_cast<void*>(1234),
|
|
|
|
&WeakPointerCallback);
|
|
|
|
global_handles->MakeWeak(g2c1.location(),
|
|
|
|
reinterpret_cast<void*>(1234),
|
|
|
|
&WeakPointerCallback);
|
|
|
|
|
|
|
|
HEAP->CollectGarbage(OLD_POINTER_SPACE);
|
2011-03-16 12:02:28 +00:00
|
|
|
CHECK_EQ(7, NumberOfWeakCalls);
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
2011-04-06 19:17:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestRetainedObjectInfo : public v8::RetainedObjectInfo {
|
|
|
|
public:
|
|
|
|
TestRetainedObjectInfo() : has_been_disposed_(false) {}
|
|
|
|
|
|
|
|
bool has_been_disposed() { return has_been_disposed_; }
|
|
|
|
|
|
|
|
virtual void Dispose() {
|
|
|
|
ASSERT(!has_been_disposed_);
|
|
|
|
has_been_disposed_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool IsEquivalent(v8::RetainedObjectInfo* other) {
|
|
|
|
return other == this;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual intptr_t GetHash() { return 0; }
|
|
|
|
|
|
|
|
virtual const char* GetLabel() { return "whatever"; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool has_been_disposed_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
TEST(EmptyObjectGroups) {
|
|
|
|
InitializeVM();
|
|
|
|
GlobalHandles* global_handles = Isolate::Current()->global_handles();
|
|
|
|
|
|
|
|
v8::HandleScope handle_scope;
|
|
|
|
|
|
|
|
Handle<Object> object =
|
|
|
|
global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked());
|
|
|
|
|
|
|
|
TestRetainedObjectInfo info;
|
|
|
|
global_handles->AddObjectGroup(NULL, 0, &info);
|
|
|
|
ASSERT(info.has_been_disposed());
|
|
|
|
|
|
|
|
global_handles->AddImplicitReferences(
|
|
|
|
Handle<HeapObject>::cast(object).location(), NULL, 0);
|
|
|
|
}
|
2011-12-01 12:32:38 +00:00
|
|
|
|
|
|
|
|
2011-12-06 12:09:11 +00:00
|
|
|
// Here is a memory use test that uses /proc, and is therefore Linux-only. We
|
|
|
|
// do not care how much memory the simulator uses, since it is only there for
|
|
|
|
// debugging purposes.
|
|
|
|
#if defined(__linux__) && !defined(USE_SIMULATOR)
|
2011-12-01 12:32:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
static uintptr_t ReadLong(char* buffer, intptr_t* position, int base) {
|
|
|
|
char* end_address = buffer + *position;
|
|
|
|
uintptr_t result = strtoul(buffer + *position, &end_address, base);
|
|
|
|
CHECK(result != ULONG_MAX || errno != ERANGE);
|
|
|
|
CHECK(end_address > buffer + *position);
|
|
|
|
*position = end_address - buffer;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static intptr_t MemoryInUse() {
|
|
|
|
intptr_t memory_use = 0;
|
|
|
|
|
|
|
|
int fd = open("/proc/self/maps", O_RDONLY);
|
|
|
|
if (fd < 0) return -1;
|
|
|
|
|
|
|
|
const int kBufSize = 10000;
|
|
|
|
char buffer[kBufSize];
|
|
|
|
int length = read(fd, buffer, kBufSize);
|
|
|
|
intptr_t line_start = 0;
|
|
|
|
CHECK_LT(length, kBufSize); // Make the buffer bigger.
|
|
|
|
CHECK_GT(length, 0); // We have to find some data in the file.
|
|
|
|
while (line_start < length) {
|
|
|
|
if (buffer[line_start] == '\n') {
|
|
|
|
line_start++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
intptr_t position = line_start;
|
|
|
|
uintptr_t start = ReadLong(buffer, &position, 16);
|
|
|
|
CHECK_EQ(buffer[position++], '-');
|
|
|
|
uintptr_t end = ReadLong(buffer, &position, 16);
|
|
|
|
CHECK_EQ(buffer[position++], ' ');
|
|
|
|
CHECK(buffer[position] == '-' || buffer[position] == 'r');
|
|
|
|
bool read_permission = (buffer[position++] == 'r');
|
|
|
|
CHECK(buffer[position] == '-' || buffer[position] == 'w');
|
|
|
|
bool write_permission = (buffer[position++] == 'w');
|
|
|
|
CHECK(buffer[position] == '-' || buffer[position] == 'x');
|
|
|
|
bool execute_permission = (buffer[position++] == 'x');
|
|
|
|
CHECK(buffer[position] == '-' || buffer[position] == 'p');
|
|
|
|
bool private_mapping = (buffer[position++] == 'p');
|
|
|
|
CHECK_EQ(buffer[position++], ' ');
|
|
|
|
uintptr_t offset = ReadLong(buffer, &position, 16);
|
|
|
|
USE(offset);
|
|
|
|
CHECK_EQ(buffer[position++], ' ');
|
|
|
|
uintptr_t major = ReadLong(buffer, &position, 16);
|
|
|
|
USE(major);
|
|
|
|
CHECK_EQ(buffer[position++], ':');
|
|
|
|
uintptr_t minor = ReadLong(buffer, &position, 16);
|
|
|
|
USE(minor);
|
|
|
|
CHECK_EQ(buffer[position++], ' ');
|
|
|
|
uintptr_t inode = ReadLong(buffer, &position, 10);
|
|
|
|
while (position < length && buffer[position] != '\n') position++;
|
|
|
|
if ((read_permission || write_permission || execute_permission) &&
|
|
|
|
private_mapping && inode == 0) {
|
|
|
|
memory_use += (end - start);
|
|
|
|
}
|
|
|
|
|
|
|
|
line_start = position;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return memory_use;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(BootUpMemoryUse) {
|
|
|
|
intptr_t initial_memory = MemoryInUse();
|
2012-07-19 18:58:23 +00:00
|
|
|
// Avoid flakiness.
|
|
|
|
FLAG_crankshaft = false;
|
|
|
|
FLAG_parallel_recompilation = false;
|
|
|
|
|
2011-12-01 12:32:38 +00:00
|
|
|
// Only Linux has the proc filesystem and only if it is mapped. If it's not
|
|
|
|
// there we just skip the test.
|
|
|
|
if (initial_memory >= 0) {
|
|
|
|
InitializeVM();
|
2012-05-24 10:40:24 +00:00
|
|
|
intptr_t delta = MemoryInUse() - initial_memory;
|
2012-01-24 15:48:16 +00:00
|
|
|
if (sizeof(initial_memory) == 8) {
|
|
|
|
if (v8::internal::Snapshot::IsEnabled()) {
|
2012-05-24 10:40:24 +00:00
|
|
|
CHECK_LE(delta, 3600 * 1024); // 3396.
|
2012-01-24 15:48:16 +00:00
|
|
|
} else {
|
2012-05-24 10:40:24 +00:00
|
|
|
CHECK_LE(delta, 4000 * 1024); // 3948.
|
2012-01-24 15:48:16 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (v8::internal::Snapshot::IsEnabled()) {
|
2012-12-05 11:04:10 +00:00
|
|
|
CHECK_LE(delta, 2600 * 1024); // 2400.
|
2012-01-24 15:48:16 +00:00
|
|
|
} else {
|
2012-12-05 11:04:10 +00:00
|
|
|
CHECK_LE(delta, 3000 * 1024); // 2920.
|
2012-01-24 15:48:16 +00:00
|
|
|
}
|
|
|
|
}
|
2011-12-01 12:32:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-06 12:09:11 +00:00
|
|
|
#endif // __linux__ and !USE_SIMULATOR
|