2011-05-03 08:23:58 +00:00
|
|
|
// Copyright 2011 the V8 project authors. All rights reserved.
|
2013-03-07 11:12:26 +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.
|
2009-09-16 13:41:24 +00:00
|
|
|
//
|
|
|
|
// Tests for heap profiler
|
|
|
|
|
2012-04-19 15:58:42 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
|
2012-04-19 16:31:01 +00:00
|
|
|
#include "v8.h"
|
|
|
|
|
2011-05-03 08:23:58 +00:00
|
|
|
#include "cctest.h"
|
2012-05-23 05:27:08 +00:00
|
|
|
#include "hashmap.h"
|
2009-09-16 13:41:24 +00:00
|
|
|
#include "heap-profiler.h"
|
2010-06-15 12:28:25 +00:00
|
|
|
#include "snapshot.h"
|
2012-04-17 15:57:35 +00:00
|
|
|
#include "debug.h"
|
2011-05-03 08:23:58 +00:00
|
|
|
#include "utils-inl.h"
|
2010-06-15 11:44:07 +00:00
|
|
|
#include "../include/v8-profiler.h"
|
2009-09-16 13:41:24 +00:00
|
|
|
|
2010-06-15 11:44:07 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class NamedEntriesDetector {
|
|
|
|
public:
|
|
|
|
NamedEntriesDetector()
|
2010-11-08 15:18:12 +00:00
|
|
|
: has_A2(false), has_B2(false), has_C2(false) {
|
2010-06-15 11:44:07 +00:00
|
|
|
}
|
|
|
|
|
2012-02-27 15:42:36 +00:00
|
|
|
void CheckEntry(i::HeapEntry* entry) {
|
|
|
|
if (strcmp(entry->name(), "A2") == 0) has_A2 = true;
|
|
|
|
if (strcmp(entry->name(), "B2") == 0) has_B2 = true;
|
|
|
|
if (strcmp(entry->name(), "C2") == 0) has_C2 = true;
|
2010-08-09 11:37:24 +00:00
|
|
|
}
|
|
|
|
|
2012-05-23 05:27:08 +00:00
|
|
|
static bool AddressesMatch(void* key1, void* key2) {
|
|
|
|
return key1 == key2;
|
|
|
|
}
|
|
|
|
|
2012-02-27 15:42:36 +00:00
|
|
|
void CheckAllReachables(i::HeapEntry* root) {
|
2012-05-23 05:27:08 +00:00
|
|
|
i::HashMap visited(AddressesMatch);
|
2012-02-27 15:42:36 +00:00
|
|
|
i::List<i::HeapEntry*> list(10);
|
|
|
|
list.Add(root);
|
|
|
|
CheckEntry(root);
|
|
|
|
while (!list.is_empty()) {
|
|
|
|
i::HeapEntry* entry = list.RemoveLast();
|
2012-05-09 14:34:13 +00:00
|
|
|
i::Vector<i::HeapGraphEdge*> children = entry->children();
|
2012-02-27 15:42:36 +00:00
|
|
|
for (int i = 0; i < children.length(); ++i) {
|
2012-05-09 14:34:13 +00:00
|
|
|
if (children[i]->type() == i::HeapGraphEdge::kShortcut) continue;
|
|
|
|
i::HeapEntry* child = children[i]->to();
|
2012-05-23 05:27:08 +00:00
|
|
|
i::HashMap::Entry* entry = visited.Lookup(
|
|
|
|
reinterpret_cast<void*>(child),
|
|
|
|
static_cast<uint32_t>(reinterpret_cast<uintptr_t>(child)),
|
|
|
|
true);
|
|
|
|
if (entry->value)
|
|
|
|
continue;
|
|
|
|
entry->value = reinterpret_cast<void*>(1);
|
|
|
|
list.Add(child);
|
|
|
|
CheckEntry(child);
|
2012-02-27 15:42:36 +00:00
|
|
|
}
|
|
|
|
}
|
2010-06-15 11:44:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool has_A2;
|
|
|
|
bool has_B2;
|
|
|
|
bool has_C2;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2010-06-17 12:56:55 +00:00
|
|
|
|
|
|
|
static const v8::HeapGraphNode* GetGlobalObject(
|
|
|
|
const v8::HeapSnapshot* snapshot) {
|
2010-11-18 10:38:25 +00:00
|
|
|
CHECK_EQ(2, snapshot->GetRoot()->GetChildrenCount());
|
|
|
|
const v8::HeapGraphNode* global_obj =
|
|
|
|
snapshot->GetRoot()->GetChild(0)->GetToNode();
|
2011-05-30 14:31:47 +00:00
|
|
|
CHECK_EQ(0, strncmp("Object", const_cast<i::HeapEntry*>(
|
|
|
|
reinterpret_cast<const i::HeapEntry*>(global_obj))->name(), 6));
|
2010-11-18 10:38:25 +00:00
|
|
|
return global_obj;
|
2010-06-17 12:56:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const v8::HeapGraphNode* GetProperty(const v8::HeapGraphNode* node,
|
|
|
|
v8::HeapGraphEdge::Type type,
|
|
|
|
const char* name) {
|
|
|
|
for (int i = 0, count = node->GetChildrenCount(); i < count; ++i) {
|
|
|
|
const v8::HeapGraphEdge* prop = node->GetChild(i);
|
|
|
|
v8::String::AsciiValue prop_name(prop->GetName());
|
|
|
|
if (prop->GetType() == type && strcmp(name, *prop_name) == 0)
|
|
|
|
return prop->GetToNode();
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool HasString(const v8::HeapGraphNode* node, const char* contents) {
|
|
|
|
for (int i = 0, count = node->GetChildrenCount(); i < count; ++i) {
|
|
|
|
const v8::HeapGraphEdge* prop = node->GetChild(i);
|
|
|
|
const v8::HeapGraphNode* node = prop->GetToNode();
|
2010-08-09 11:37:24 +00:00
|
|
|
if (node->GetType() == v8::HeapGraphNode::kString) {
|
2010-06-17 12:56:55 +00:00
|
|
|
v8::String::AsciiValue node_name(node->GetName());
|
|
|
|
if (strcmp(contents, *node_name) == 0) return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-15 11:44:07 +00:00
|
|
|
TEST(HeapSnapshot) {
|
2010-07-15 13:21:50 +00:00
|
|
|
LocalContext env2;
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(env2->GetIsolate());
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::HeapProfiler* heap_profiler = env2->GetIsolate()->GetHeapProfiler();
|
2010-06-15 11:44:07 +00:00
|
|
|
|
2010-10-18 09:15:38 +00:00
|
|
|
CompileRun(
|
2010-06-15 11:44:07 +00:00
|
|
|
"function A2() {}\n"
|
|
|
|
"function B2(x) { return function() { return typeof x; }; }\n"
|
|
|
|
"function C2(x) { this.x1 = x; this.x2 = x; this[1] = x; }\n"
|
|
|
|
"var a2 = new A2();\n"
|
|
|
|
"var b2_1 = new B2(a2), b2_2 = new B2(a2);\n"
|
|
|
|
"var c2 = new C2(a2);");
|
|
|
|
const v8::HeapSnapshot* snapshot_env2 =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("env2"));
|
2010-06-17 12:56:55 +00:00
|
|
|
const v8::HeapGraphNode* global_env2 = GetGlobalObject(snapshot_env2);
|
2010-06-15 11:44:07 +00:00
|
|
|
|
2010-11-08 15:18:12 +00:00
|
|
|
// Verify, that JS global object of env2 has '..2' properties.
|
2010-06-17 12:56:55 +00:00
|
|
|
const v8::HeapGraphNode* a2_node =
|
2012-04-16 14:31:13 +00:00
|
|
|
GetProperty(global_env2, v8::HeapGraphEdge::kProperty, "a2");
|
2010-06-17 12:56:55 +00:00
|
|
|
CHECK_NE(NULL, a2_node);
|
2010-08-09 11:37:24 +00:00
|
|
|
CHECK_NE(
|
2012-04-16 14:31:13 +00:00
|
|
|
NULL, GetProperty(global_env2, v8::HeapGraphEdge::kProperty, "b2_1"));
|
2010-08-09 11:37:24 +00:00
|
|
|
CHECK_NE(
|
2012-04-16 14:31:13 +00:00
|
|
|
NULL, GetProperty(global_env2, v8::HeapGraphEdge::kProperty, "b2_2"));
|
|
|
|
CHECK_NE(NULL, GetProperty(global_env2, v8::HeapGraphEdge::kProperty, "c2"));
|
2010-06-15 11:44:07 +00:00
|
|
|
|
|
|
|
NamedEntriesDetector det;
|
2012-02-27 15:42:36 +00:00
|
|
|
det.CheckAllReachables(const_cast<i::HeapEntry*>(
|
|
|
|
reinterpret_cast<const i::HeapEntry*>(global_env2)));
|
2010-06-15 11:44:07 +00:00
|
|
|
CHECK(det.has_A2);
|
|
|
|
CHECK(det.has_B2);
|
|
|
|
CHECK(det.has_C2);
|
|
|
|
}
|
|
|
|
|
2010-06-17 12:56:55 +00:00
|
|
|
|
2010-08-09 11:37:24 +00:00
|
|
|
TEST(HeapSnapshotObjectSizes) {
|
|
|
|
LocalContext env;
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
|
2010-08-09 11:37:24 +00:00
|
|
|
|
|
|
|
// -a-> X1 --a
|
|
|
|
// x -b-> X2 <-|
|
2010-10-18 09:15:38 +00:00
|
|
|
CompileRun(
|
2010-08-09 11:37:24 +00:00
|
|
|
"function X(a, b) { this.a = a; this.b = b; }\n"
|
|
|
|
"x = new X(new X(), new X());\n"
|
2012-04-16 14:31:13 +00:00
|
|
|
"dummy = new X();\n"
|
2010-11-18 10:38:25 +00:00
|
|
|
"(function() { x.a.a = x.b; })();");
|
2010-08-09 11:37:24 +00:00
|
|
|
const v8::HeapSnapshot* snapshot =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("sizes"));
|
2010-08-09 11:37:24 +00:00
|
|
|
const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
|
|
|
|
const v8::HeapGraphNode* x =
|
2012-04-16 14:31:13 +00:00
|
|
|
GetProperty(global, v8::HeapGraphEdge::kProperty, "x");
|
2010-08-09 11:37:24 +00:00
|
|
|
CHECK_NE(NULL, x);
|
|
|
|
const v8::HeapGraphNode* x1 =
|
|
|
|
GetProperty(x, v8::HeapGraphEdge::kProperty, "a");
|
|
|
|
CHECK_NE(NULL, x1);
|
|
|
|
const v8::HeapGraphNode* x2 =
|
|
|
|
GetProperty(x, v8::HeapGraphEdge::kProperty, "b");
|
|
|
|
CHECK_NE(NULL, x2);
|
2010-11-22 14:00:40 +00:00
|
|
|
|
2012-02-27 15:42:36 +00:00
|
|
|
// Test sizes.
|
2012-05-23 05:27:08 +00:00
|
|
|
CHECK_NE(0, x->GetSelfSize());
|
|
|
|
CHECK_NE(0, x1->GetSelfSize());
|
|
|
|
CHECK_NE(0, x2->GetSelfSize());
|
2010-08-09 11:37:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-10 13:54:20 +00:00
|
|
|
TEST(BoundFunctionInSnapshot) {
|
|
|
|
LocalContext env;
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
|
2012-02-10 13:54:20 +00:00
|
|
|
CompileRun(
|
|
|
|
"function myFunction(a, b) { this.a = a; this.b = b; }\n"
|
|
|
|
"function AAAAA() {}\n"
|
|
|
|
"boundFunction = myFunction.bind(new AAAAA(), 20, new Number(12)); \n");
|
|
|
|
const v8::HeapSnapshot* snapshot =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("sizes"));
|
2012-02-10 13:54:20 +00:00
|
|
|
const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
|
|
|
|
const v8::HeapGraphNode* f =
|
2012-04-16 14:31:13 +00:00
|
|
|
GetProperty(global, v8::HeapGraphEdge::kProperty, "boundFunction");
|
2012-02-10 13:54:20 +00:00
|
|
|
CHECK(f);
|
|
|
|
CHECK_EQ(v8::String::New("native_bind"), f->GetName());
|
|
|
|
const v8::HeapGraphNode* bindings =
|
|
|
|
GetProperty(f, v8::HeapGraphEdge::kInternal, "bindings");
|
|
|
|
CHECK_NE(NULL, bindings);
|
|
|
|
CHECK_EQ(v8::HeapGraphNode::kArray, bindings->GetType());
|
|
|
|
CHECK_EQ(4, bindings->GetChildrenCount());
|
|
|
|
|
|
|
|
const v8::HeapGraphNode* bound_this = GetProperty(
|
|
|
|
f, v8::HeapGraphEdge::kShortcut, "bound_this");
|
|
|
|
CHECK(bound_this);
|
|
|
|
CHECK_EQ(v8::HeapGraphNode::kObject, bound_this->GetType());
|
|
|
|
|
|
|
|
const v8::HeapGraphNode* bound_function = GetProperty(
|
|
|
|
f, v8::HeapGraphEdge::kShortcut, "bound_function");
|
|
|
|
CHECK(bound_function);
|
|
|
|
CHECK_EQ(v8::HeapGraphNode::kClosure, bound_function->GetType());
|
|
|
|
|
|
|
|
const v8::HeapGraphNode* bound_argument = GetProperty(
|
|
|
|
f, v8::HeapGraphEdge::kShortcut, "bound_argument_1");
|
|
|
|
CHECK(bound_argument);
|
|
|
|
CHECK_EQ(v8::HeapGraphNode::kObject, bound_argument->GetType());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-09 11:37:24 +00:00
|
|
|
TEST(HeapSnapshotEntryChildren) {
|
|
|
|
LocalContext env;
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
|
2010-08-09 11:37:24 +00:00
|
|
|
|
2010-10-18 09:15:38 +00:00
|
|
|
CompileRun(
|
2010-08-09 11:37:24 +00:00
|
|
|
"function A() { }\n"
|
|
|
|
"a = new A;");
|
|
|
|
const v8::HeapSnapshot* snapshot =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("children"));
|
2010-08-09 11:37:24 +00:00
|
|
|
const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
|
|
|
|
for (int i = 0, count = global->GetChildrenCount(); i < count; ++i) {
|
|
|
|
const v8::HeapGraphEdge* prop = global->GetChild(i);
|
|
|
|
CHECK_EQ(global, prop->GetFromNode());
|
|
|
|
}
|
|
|
|
const v8::HeapGraphNode* a =
|
|
|
|
GetProperty(global, v8::HeapGraphEdge::kProperty, "a");
|
|
|
|
CHECK_NE(NULL, a);
|
|
|
|
for (int i = 0, count = a->GetChildrenCount(); i < count; ++i) {
|
|
|
|
const v8::HeapGraphEdge* prop = a->GetChild(i);
|
|
|
|
CHECK_EQ(a, prop->GetFromNode());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-17 12:56:55 +00:00
|
|
|
TEST(HeapSnapshotCodeObjects) {
|
2010-07-15 13:21:50 +00:00
|
|
|
LocalContext env;
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
|
2010-06-17 12:56:55 +00:00
|
|
|
|
2010-10-18 09:15:38 +00:00
|
|
|
CompileRun(
|
2010-06-17 12:56:55 +00:00
|
|
|
"function lazy(x) { return x - 1; }\n"
|
|
|
|
"function compiled(x) { return x + 1; }\n"
|
2010-08-18 08:19:29 +00:00
|
|
|
"var anonymous = (function() { return function() { return 0; } })();\n"
|
2010-06-17 12:56:55 +00:00
|
|
|
"compiled(1)");
|
|
|
|
const v8::HeapSnapshot* snapshot =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("code"));
|
2010-06-17 12:56:55 +00:00
|
|
|
|
|
|
|
const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
|
|
|
|
const v8::HeapGraphNode* compiled =
|
2012-04-16 14:31:13 +00:00
|
|
|
GetProperty(global, v8::HeapGraphEdge::kProperty, "compiled");
|
2010-06-17 12:56:55 +00:00
|
|
|
CHECK_NE(NULL, compiled);
|
2010-08-09 11:37:24 +00:00
|
|
|
CHECK_EQ(v8::HeapGraphNode::kClosure, compiled->GetType());
|
2010-06-17 12:56:55 +00:00
|
|
|
const v8::HeapGraphNode* lazy =
|
2012-04-16 14:31:13 +00:00
|
|
|
GetProperty(global, v8::HeapGraphEdge::kProperty, "lazy");
|
2010-06-17 12:56:55 +00:00
|
|
|
CHECK_NE(NULL, lazy);
|
2010-08-09 11:37:24 +00:00
|
|
|
CHECK_EQ(v8::HeapGraphNode::kClosure, lazy->GetType());
|
2010-08-18 08:19:29 +00:00
|
|
|
const v8::HeapGraphNode* anonymous =
|
2012-04-16 14:31:13 +00:00
|
|
|
GetProperty(global, v8::HeapGraphEdge::kProperty, "anonymous");
|
2010-08-18 08:19:29 +00:00
|
|
|
CHECK_NE(NULL, anonymous);
|
|
|
|
CHECK_EQ(v8::HeapGraphNode::kClosure, anonymous->GetType());
|
|
|
|
v8::String::AsciiValue anonymous_name(anonymous->GetName());
|
2010-10-13 14:57:00 +00:00
|
|
|
CHECK_EQ("", *anonymous_name);
|
2010-06-17 12:56:55 +00:00
|
|
|
|
|
|
|
// Find references to code.
|
|
|
|
const v8::HeapGraphNode* compiled_code =
|
2011-03-30 14:04:50 +00:00
|
|
|
GetProperty(compiled, v8::HeapGraphEdge::kInternal, "shared");
|
2010-06-17 12:56:55 +00:00
|
|
|
CHECK_NE(NULL, compiled_code);
|
|
|
|
const v8::HeapGraphNode* lazy_code =
|
2011-03-30 14:04:50 +00:00
|
|
|
GetProperty(lazy, v8::HeapGraphEdge::kInternal, "shared");
|
2010-06-17 12:56:55 +00:00
|
|
|
CHECK_NE(NULL, lazy_code);
|
|
|
|
|
|
|
|
// Verify that non-compiled code doesn't contain references to "x"
|
2010-07-13 13:06:33 +00:00
|
|
|
// literal, while compiled code does. The scope info is stored in FixedArray
|
|
|
|
// objects attached to the SharedFunctionInfo.
|
2010-06-17 12:56:55 +00:00
|
|
|
bool compiled_references_x = false, lazy_references_x = false;
|
|
|
|
for (int i = 0, count = compiled_code->GetChildrenCount(); i < count; ++i) {
|
|
|
|
const v8::HeapGraphEdge* prop = compiled_code->GetChild(i);
|
|
|
|
const v8::HeapGraphNode* node = prop->GetToNode();
|
2010-08-09 11:37:24 +00:00
|
|
|
if (node->GetType() == v8::HeapGraphNode::kArray) {
|
2010-06-17 12:56:55 +00:00
|
|
|
if (HasString(node, "x")) {
|
|
|
|
compiled_references_x = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int i = 0, count = lazy_code->GetChildrenCount(); i < count; ++i) {
|
|
|
|
const v8::HeapGraphEdge* prop = lazy_code->GetChild(i);
|
|
|
|
const v8::HeapGraphNode* node = prop->GetToNode();
|
2010-08-09 11:37:24 +00:00
|
|
|
if (node->GetType() == v8::HeapGraphNode::kArray) {
|
2010-06-17 12:56:55 +00:00
|
|
|
if (HasString(node, "x")) {
|
|
|
|
lazy_references_x = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CHECK(compiled_references_x);
|
|
|
|
CHECK(!lazy_references_x);
|
|
|
|
}
|
|
|
|
|
2010-07-15 13:21:50 +00:00
|
|
|
|
2010-10-18 09:15:38 +00:00
|
|
|
TEST(HeapSnapshotHeapNumbers) {
|
|
|
|
LocalContext env;
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
|
2010-10-18 09:15:38 +00:00
|
|
|
CompileRun(
|
|
|
|
"a = 1; // a is Smi\n"
|
|
|
|
"b = 2.5; // b is HeapNumber");
|
|
|
|
const v8::HeapSnapshot* snapshot =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("numbers"));
|
2010-10-18 09:15:38 +00:00
|
|
|
const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
|
2012-04-16 14:31:13 +00:00
|
|
|
CHECK_EQ(NULL, GetProperty(global, v8::HeapGraphEdge::kProperty, "a"));
|
2010-10-18 09:15:38 +00:00
|
|
|
const v8::HeapGraphNode* b =
|
2012-04-16 14:31:13 +00:00
|
|
|
GetProperty(global, v8::HeapGraphEdge::kProperty, "b");
|
2010-10-18 09:15:38 +00:00
|
|
|
CHECK_NE(NULL, b);
|
|
|
|
CHECK_EQ(v8::HeapGraphNode::kHeapNumber, b->GetType());
|
|
|
|
}
|
|
|
|
|
2011-10-21 13:05:37 +00:00
|
|
|
TEST(HeapSnapshotSlicedString) {
|
|
|
|
LocalContext env;
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
|
2011-10-21 13:05:37 +00:00
|
|
|
CompileRun(
|
|
|
|
"parent_string = \"123456789.123456789.123456789.123456789.123456789."
|
|
|
|
"123456789.123456789.123456789.123456789.123456789."
|
|
|
|
"123456789.123456789.123456789.123456789.123456789."
|
|
|
|
"123456789.123456789.123456789.123456789.123456789.\";"
|
|
|
|
"child_string = parent_string.slice(100);");
|
|
|
|
const v8::HeapSnapshot* snapshot =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("strings"));
|
2011-10-21 13:05:37 +00:00
|
|
|
const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
|
|
|
|
const v8::HeapGraphNode* parent_string =
|
2012-04-16 14:31:13 +00:00
|
|
|
GetProperty(global, v8::HeapGraphEdge::kProperty, "parent_string");
|
2011-10-21 13:05:37 +00:00
|
|
|
CHECK_NE(NULL, parent_string);
|
|
|
|
const v8::HeapGraphNode* child_string =
|
2012-04-16 14:31:13 +00:00
|
|
|
GetProperty(global, v8::HeapGraphEdge::kProperty, "child_string");
|
2011-10-21 13:05:37 +00:00
|
|
|
CHECK_NE(NULL, child_string);
|
|
|
|
const v8::HeapGraphNode* parent =
|
|
|
|
GetProperty(child_string, v8::HeapGraphEdge::kInternal, "parent");
|
|
|
|
CHECK_EQ(parent_string, parent);
|
|
|
|
}
|
2010-10-18 09:15:38 +00:00
|
|
|
|
|
|
|
TEST(HeapSnapshotInternalReferences) {
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
|
|
|
v8::HandleScope scope(isolate);
|
2010-10-18 09:15:38 +00:00
|
|
|
v8::Local<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New();
|
|
|
|
global_template->SetInternalFieldCount(2);
|
|
|
|
LocalContext env(NULL, global_template);
|
|
|
|
v8::Handle<v8::Object> global_proxy = env->Global();
|
|
|
|
v8::Handle<v8::Object> global = global_proxy->GetPrototype().As<v8::Object>();
|
|
|
|
CHECK_EQ(2, global->InternalFieldCount());
|
|
|
|
v8::Local<v8::Object> obj = v8::Object::New();
|
|
|
|
global->SetInternalField(0, v8_num(17));
|
|
|
|
global->SetInternalField(1, obj);
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::HeapProfiler* heap_profiler = isolate->GetHeapProfiler();
|
2010-10-18 09:15:38 +00:00
|
|
|
const v8::HeapSnapshot* snapshot =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("internals"));
|
2010-10-18 09:15:38 +00:00
|
|
|
const v8::HeapGraphNode* global_node = GetGlobalObject(snapshot);
|
|
|
|
// The first reference will not present, because it's a Smi.
|
|
|
|
CHECK_EQ(NULL, GetProperty(global_node, v8::HeapGraphEdge::kInternal, "0"));
|
|
|
|
// The second reference is to an object.
|
|
|
|
CHECK_NE(NULL, GetProperty(global_node, v8::HeapGraphEdge::kInternal, "1"));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-26 13:47:37 +00:00
|
|
|
// Trying to introduce a check helper for uint32_t causes many
|
2010-07-15 14:28:52 +00:00
|
|
|
// overloading ambiguities, so it seems easier just to cast
|
|
|
|
// them to a signed type.
|
2012-03-26 13:47:37 +00:00
|
|
|
#define CHECK_EQ_SNAPSHOT_OBJECT_ID(a, b) \
|
|
|
|
CHECK_EQ(static_cast<int32_t>(a), static_cast<int32_t>(b))
|
|
|
|
#define CHECK_NE_SNAPSHOT_OBJECT_ID(a, b) \
|
2010-08-09 11:37:24 +00:00
|
|
|
CHECK((a) != (b)) // NOLINT
|
2010-07-15 14:28:52 +00:00
|
|
|
|
2013-02-21 13:16:17 +00:00
|
|
|
TEST(HeapSnapshotAddressReuse) {
|
|
|
|
LocalContext env;
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
|
2013-02-21 13:16:17 +00:00
|
|
|
|
|
|
|
CompileRun(
|
|
|
|
"function A() {}\n"
|
|
|
|
"var a = [];\n"
|
|
|
|
"for (var i = 0; i < 10000; ++i)\n"
|
|
|
|
" a[i] = new A();\n");
|
|
|
|
const v8::HeapSnapshot* snapshot1 =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("snapshot1"));
|
2013-02-21 13:16:17 +00:00
|
|
|
v8::SnapshotObjectId maxId1 = snapshot1->GetMaxSnapshotJSObjectId();
|
|
|
|
|
|
|
|
CompileRun(
|
|
|
|
"for (var i = 0; i < 10000; ++i)\n"
|
|
|
|
" a[i] = new A();\n");
|
|
|
|
HEAP->CollectAllGarbage(i::Heap::kNoGCFlags);
|
|
|
|
|
|
|
|
const v8::HeapSnapshot* snapshot2 =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("snapshot2"));
|
2013-02-21 13:16:17 +00:00
|
|
|
const v8::HeapGraphNode* global2 = GetGlobalObject(snapshot2);
|
|
|
|
|
|
|
|
const v8::HeapGraphNode* array_node =
|
|
|
|
GetProperty(global2, v8::HeapGraphEdge::kProperty, "a");
|
|
|
|
CHECK_NE(NULL, array_node);
|
|
|
|
int wrong_count = 0;
|
|
|
|
for (int i = 0, count = array_node->GetChildrenCount(); i < count; ++i) {
|
|
|
|
const v8::HeapGraphEdge* prop = array_node->GetChild(i);
|
|
|
|
if (prop->GetType() != v8::HeapGraphEdge::kElement)
|
|
|
|
continue;
|
|
|
|
v8::SnapshotObjectId id = prop->GetToNode()->GetId();
|
|
|
|
if (id < maxId1)
|
|
|
|
++wrong_count;
|
|
|
|
}
|
2013-02-25 16:09:57 +00:00
|
|
|
CHECK_EQ(0, wrong_count);
|
2013-02-21 13:16:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-20 14:35:05 +00:00
|
|
|
TEST(HeapEntryIdsAndArrayShift) {
|
|
|
|
LocalContext env;
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
|
2012-03-20 14:35:05 +00:00
|
|
|
|
|
|
|
CompileRun(
|
|
|
|
"function AnObject() {\n"
|
|
|
|
" this.first = 'first';\n"
|
|
|
|
" this.second = 'second';\n"
|
|
|
|
"}\n"
|
|
|
|
"var a = new Array();\n"
|
|
|
|
"for (var i = 0; i < 10; ++i)\n"
|
|
|
|
" a.push(new AnObject());\n");
|
|
|
|
const v8::HeapSnapshot* snapshot1 =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("s1"));
|
2012-03-20 14:35:05 +00:00
|
|
|
|
|
|
|
CompileRun(
|
|
|
|
"for (var i = 0; i < 1; ++i)\n"
|
|
|
|
" a.shift();\n");
|
|
|
|
|
|
|
|
HEAP->CollectAllGarbage(i::Heap::kNoGCFlags);
|
|
|
|
|
|
|
|
const v8::HeapSnapshot* snapshot2 =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("s2"));
|
2012-03-20 14:35:05 +00:00
|
|
|
|
|
|
|
const v8::HeapGraphNode* global1 = GetGlobalObject(snapshot1);
|
|
|
|
const v8::HeapGraphNode* global2 = GetGlobalObject(snapshot2);
|
2012-03-26 13:47:37 +00:00
|
|
|
CHECK_NE_SNAPSHOT_OBJECT_ID(0, global1->GetId());
|
|
|
|
CHECK_EQ_SNAPSHOT_OBJECT_ID(global1->GetId(), global2->GetId());
|
2012-03-20 14:35:05 +00:00
|
|
|
|
|
|
|
const v8::HeapGraphNode* a1 =
|
|
|
|
GetProperty(global1, v8::HeapGraphEdge::kProperty, "a");
|
|
|
|
CHECK_NE(NULL, a1);
|
|
|
|
const v8::HeapGraphNode* k1 =
|
2012-04-16 14:31:13 +00:00
|
|
|
GetProperty(a1, v8::HeapGraphEdge::kInternal, "elements");
|
2012-03-20 14:35:05 +00:00
|
|
|
CHECK_NE(NULL, k1);
|
|
|
|
const v8::HeapGraphNode* a2 =
|
|
|
|
GetProperty(global2, v8::HeapGraphEdge::kProperty, "a");
|
|
|
|
CHECK_NE(NULL, a2);
|
|
|
|
const v8::HeapGraphNode* k2 =
|
2012-04-16 14:31:13 +00:00
|
|
|
GetProperty(a2, v8::HeapGraphEdge::kInternal, "elements");
|
2012-03-20 14:35:05 +00:00
|
|
|
CHECK_NE(NULL, k2);
|
|
|
|
|
2012-03-26 13:47:37 +00:00
|
|
|
CHECK_EQ_SNAPSHOT_OBJECT_ID(a1->GetId(), a2->GetId());
|
|
|
|
CHECK_EQ_SNAPSHOT_OBJECT_ID(k1->GetId(), k2->GetId());
|
2012-03-20 14:35:05 +00:00
|
|
|
}
|
|
|
|
|
2010-07-15 13:21:50 +00:00
|
|
|
TEST(HeapEntryIdsAndGC) {
|
|
|
|
LocalContext env;
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
|
2010-07-15 13:21:50 +00:00
|
|
|
|
2010-10-18 09:15:38 +00:00
|
|
|
CompileRun(
|
2010-07-15 13:21:50 +00:00
|
|
|
"function A() {}\n"
|
|
|
|
"function B(x) { this.x = x; }\n"
|
|
|
|
"var a = new A();\n"
|
|
|
|
"var b = new B(a);");
|
2012-03-27 11:54:47 +00:00
|
|
|
v8::Local<v8::String> s1_str = v8_str("s1");
|
|
|
|
v8::Local<v8::String> s2_str = v8_str("s2");
|
2010-07-15 13:21:50 +00:00
|
|
|
const v8::HeapSnapshot* snapshot1 =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(s1_str);
|
2010-07-15 13:21:50 +00:00
|
|
|
|
2011-09-19 18:36:47 +00:00
|
|
|
HEAP->CollectAllGarbage(i::Heap::kNoGCFlags);
|
2010-07-15 13:21:50 +00:00
|
|
|
|
|
|
|
const v8::HeapSnapshot* snapshot2 =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(s2_str);
|
2012-03-27 11:54:47 +00:00
|
|
|
|
2012-03-29 14:50:26 +00:00
|
|
|
CHECK_GT(snapshot1->GetMaxSnapshotJSObjectId(), 7000);
|
2012-03-29 14:18:11 +00:00
|
|
|
CHECK(snapshot1->GetMaxSnapshotJSObjectId() <=
|
|
|
|
snapshot2->GetMaxSnapshotJSObjectId());
|
2010-07-15 13:21:50 +00:00
|
|
|
|
|
|
|
const v8::HeapGraphNode* global1 = GetGlobalObject(snapshot1);
|
|
|
|
const v8::HeapGraphNode* global2 = GetGlobalObject(snapshot2);
|
2012-03-26 13:47:37 +00:00
|
|
|
CHECK_NE_SNAPSHOT_OBJECT_ID(0, global1->GetId());
|
|
|
|
CHECK_EQ_SNAPSHOT_OBJECT_ID(global1->GetId(), global2->GetId());
|
2010-07-15 13:21:50 +00:00
|
|
|
const v8::HeapGraphNode* A1 =
|
2010-08-09 11:37:24 +00:00
|
|
|
GetProperty(global1, v8::HeapGraphEdge::kProperty, "A");
|
|
|
|
CHECK_NE(NULL, A1);
|
2010-07-15 13:21:50 +00:00
|
|
|
const v8::HeapGraphNode* A2 =
|
2010-08-09 11:37:24 +00:00
|
|
|
GetProperty(global2, v8::HeapGraphEdge::kProperty, "A");
|
|
|
|
CHECK_NE(NULL, A2);
|
2012-03-26 13:47:37 +00:00
|
|
|
CHECK_NE_SNAPSHOT_OBJECT_ID(0, A1->GetId());
|
|
|
|
CHECK_EQ_SNAPSHOT_OBJECT_ID(A1->GetId(), A2->GetId());
|
2010-07-15 13:21:50 +00:00
|
|
|
const v8::HeapGraphNode* B1 =
|
2010-08-09 11:37:24 +00:00
|
|
|
GetProperty(global1, v8::HeapGraphEdge::kProperty, "B");
|
|
|
|
CHECK_NE(NULL, B1);
|
2010-07-15 13:21:50 +00:00
|
|
|
const v8::HeapGraphNode* B2 =
|
2010-08-09 11:37:24 +00:00
|
|
|
GetProperty(global2, v8::HeapGraphEdge::kProperty, "B");
|
|
|
|
CHECK_NE(NULL, B2);
|
2012-03-26 13:47:37 +00:00
|
|
|
CHECK_NE_SNAPSHOT_OBJECT_ID(0, B1->GetId());
|
|
|
|
CHECK_EQ_SNAPSHOT_OBJECT_ID(B1->GetId(), B2->GetId());
|
2010-07-15 13:21:50 +00:00
|
|
|
const v8::HeapGraphNode* a1 =
|
2010-08-09 11:37:24 +00:00
|
|
|
GetProperty(global1, v8::HeapGraphEdge::kProperty, "a");
|
|
|
|
CHECK_NE(NULL, a1);
|
2010-07-15 13:21:50 +00:00
|
|
|
const v8::HeapGraphNode* a2 =
|
2010-08-09 11:37:24 +00:00
|
|
|
GetProperty(global2, v8::HeapGraphEdge::kProperty, "a");
|
|
|
|
CHECK_NE(NULL, a2);
|
2012-03-26 13:47:37 +00:00
|
|
|
CHECK_NE_SNAPSHOT_OBJECT_ID(0, a1->GetId());
|
|
|
|
CHECK_EQ_SNAPSHOT_OBJECT_ID(a1->GetId(), a2->GetId());
|
2010-07-15 13:21:50 +00:00
|
|
|
const v8::HeapGraphNode* b1 =
|
2010-08-09 11:37:24 +00:00
|
|
|
GetProperty(global1, v8::HeapGraphEdge::kProperty, "b");
|
|
|
|
CHECK_NE(NULL, b1);
|
2010-07-15 13:21:50 +00:00
|
|
|
const v8::HeapGraphNode* b2 =
|
2010-08-09 11:37:24 +00:00
|
|
|
GetProperty(global2, v8::HeapGraphEdge::kProperty, "b");
|
|
|
|
CHECK_NE(NULL, b2);
|
2012-03-26 13:47:37 +00:00
|
|
|
CHECK_NE_SNAPSHOT_OBJECT_ID(0, b1->GetId());
|
|
|
|
CHECK_EQ_SNAPSHOT_OBJECT_ID(b1->GetId(), b2->GetId());
|
2010-07-15 13:21:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-01 07:19:23 +00:00
|
|
|
TEST(HeapSnapshotRootPreservedAfterSorting) {
|
|
|
|
LocalContext env;
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
|
2010-10-01 07:19:23 +00:00
|
|
|
const v8::HeapSnapshot* snapshot =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("s"));
|
2010-10-01 07:19:23 +00:00
|
|
|
const v8::HeapGraphNode* root1 = snapshot->GetRoot();
|
|
|
|
const_cast<i::HeapSnapshot*>(reinterpret_cast<const i::HeapSnapshot*>(
|
|
|
|
snapshot))->GetSortedEntriesList();
|
|
|
|
const v8::HeapGraphNode* root2 = snapshot->GetRoot();
|
|
|
|
CHECK_EQ(root1, root2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-14 11:49:06 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class TestJSONStream : public v8::OutputStream {
|
|
|
|
public:
|
2010-09-14 15:11:24 +00:00
|
|
|
TestJSONStream() : eos_signaled_(0), abort_countdown_(-1) {}
|
|
|
|
explicit TestJSONStream(int abort_countdown)
|
|
|
|
: eos_signaled_(0), abort_countdown_(abort_countdown) {}
|
2010-09-14 11:49:06 +00:00
|
|
|
virtual ~TestJSONStream() {}
|
|
|
|
virtual void EndOfStream() { ++eos_signaled_; }
|
2010-09-14 15:11:24 +00:00
|
|
|
virtual WriteResult WriteAsciiChunk(char* buffer, int chars_written) {
|
|
|
|
if (abort_countdown_ > 0) --abort_countdown_;
|
|
|
|
if (abort_countdown_ == 0) return kAbort;
|
2010-09-14 11:49:06 +00:00
|
|
|
CHECK_GT(chars_written, 0);
|
|
|
|
i::Vector<char> chunk = buffer_.AddBlock(chars_written, '\0');
|
2013-04-16 12:30:51 +00:00
|
|
|
i::OS::MemCopy(chunk.start(), buffer, chars_written);
|
2010-09-14 15:11:24 +00:00
|
|
|
return kContinue;
|
2010-09-14 11:49:06 +00:00
|
|
|
}
|
2012-04-13 08:52:25 +00:00
|
|
|
virtual WriteResult WriteUint32Chunk(uint32_t* buffer, int chars_written) {
|
|
|
|
ASSERT(false);
|
|
|
|
return kAbort;
|
|
|
|
}
|
2010-09-14 11:49:06 +00:00
|
|
|
void WriteTo(i::Vector<char> dest) { buffer_.WriteTo(dest); }
|
|
|
|
int eos_signaled() { return eos_signaled_; }
|
|
|
|
int size() { return buffer_.size(); }
|
2012-04-13 08:52:25 +00:00
|
|
|
|
2010-09-14 11:49:06 +00:00
|
|
|
private:
|
|
|
|
i::Collector<char> buffer_;
|
|
|
|
int eos_signaled_;
|
2010-09-14 15:11:24 +00:00
|
|
|
int abort_countdown_;
|
2010-09-14 11:49:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class AsciiResource: public v8::String::ExternalAsciiStringResource {
|
|
|
|
public:
|
|
|
|
explicit AsciiResource(i::Vector<char> string): data_(string.start()) {
|
|
|
|
length_ = string.length();
|
|
|
|
}
|
|
|
|
virtual const char* data() const { return data_; }
|
|
|
|
virtual size_t length() const { return length_; }
|
|
|
|
private:
|
|
|
|
const char* data_;
|
|
|
|
size_t length_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST(HeapSnapshotJSONSerialization) {
|
|
|
|
LocalContext env;
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
|
2010-09-14 11:49:06 +00:00
|
|
|
|
|
|
|
#define STRING_LITERAL_FOR_TEST \
|
|
|
|
"\"String \\n\\r\\u0008\\u0081\\u0101\\u0801\\u8001\""
|
2010-10-18 09:15:38 +00:00
|
|
|
CompileRun(
|
2010-09-14 11:49:06 +00:00
|
|
|
"function A(s) { this.s = s; }\n"
|
|
|
|
"function B(x) { this.x = x; }\n"
|
|
|
|
"var a = new A(" STRING_LITERAL_FOR_TEST ");\n"
|
|
|
|
"var b = new B(a);");
|
|
|
|
const v8::HeapSnapshot* snapshot =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("json"));
|
2010-09-14 11:49:06 +00:00
|
|
|
TestJSONStream stream;
|
|
|
|
snapshot->Serialize(&stream, v8::HeapSnapshot::kJSON);
|
|
|
|
CHECK_GT(stream.size(), 0);
|
|
|
|
CHECK_EQ(1, stream.eos_signaled());
|
|
|
|
i::ScopedVector<char> json(stream.size());
|
|
|
|
stream.WriteTo(json);
|
|
|
|
|
|
|
|
// Verify that snapshot string is valid JSON.
|
|
|
|
AsciiResource json_res(json);
|
|
|
|
v8::Local<v8::String> json_string = v8::String::NewExternal(&json_res);
|
2011-09-26 14:38:29 +00:00
|
|
|
env->Global()->Set(v8_str("json_snapshot"), json_string);
|
2010-09-14 11:49:06 +00:00
|
|
|
v8::Local<v8::Value> snapshot_parse_result = CompileRun(
|
|
|
|
"var parsed = JSON.parse(json_snapshot); true;");
|
|
|
|
CHECK(!snapshot_parse_result.IsEmpty());
|
|
|
|
|
|
|
|
// Verify that snapshot object has required fields.
|
|
|
|
v8::Local<v8::Object> parsed_snapshot =
|
2011-09-26 14:38:29 +00:00
|
|
|
env->Global()->Get(v8_str("parsed"))->ToObject();
|
|
|
|
CHECK(parsed_snapshot->Has(v8_str("snapshot")));
|
|
|
|
CHECK(parsed_snapshot->Has(v8_str("nodes")));
|
2012-04-13 12:50:48 +00:00
|
|
|
CHECK(parsed_snapshot->Has(v8_str("edges")));
|
2011-09-26 14:38:29 +00:00
|
|
|
CHECK(parsed_snapshot->Has(v8_str("strings")));
|
2010-09-14 11:49:06 +00:00
|
|
|
|
|
|
|
// Get node and edge "member" offsets.
|
|
|
|
v8::Local<v8::Value> meta_analysis_result = CompileRun(
|
2012-04-13 12:50:48 +00:00
|
|
|
"var meta = parsed.snapshot.meta;\n"
|
2012-06-06 10:57:20 +00:00
|
|
|
"var edge_count_offset = meta.node_fields.indexOf('edge_count');\n"
|
2012-04-13 12:50:48 +00:00
|
|
|
"var node_fields_count = meta.node_fields.length;\n"
|
|
|
|
"var edge_fields_count = meta.edge_fields.length;\n"
|
|
|
|
"var edge_type_offset = meta.edge_fields.indexOf('type');\n"
|
|
|
|
"var edge_name_offset = meta.edge_fields.indexOf('name_or_index');\n"
|
|
|
|
"var edge_to_node_offset = meta.edge_fields.indexOf('to_node');\n"
|
2010-09-14 11:49:06 +00:00
|
|
|
"var property_type ="
|
2012-04-13 12:50:48 +00:00
|
|
|
" meta.edge_types[edge_type_offset].indexOf('property');\n"
|
2010-11-18 10:38:25 +00:00
|
|
|
"var shortcut_type ="
|
2012-04-13 12:50:48 +00:00
|
|
|
" meta.edge_types[edge_type_offset].indexOf('shortcut');\n"
|
2012-06-06 10:57:20 +00:00
|
|
|
"var node_count = parsed.nodes.length / node_fields_count;\n"
|
|
|
|
"var first_edge_indexes = parsed.first_edge_indexes = [];\n"
|
|
|
|
"for (var i = 0, first_edge_index = 0; i < node_count; ++i) {\n"
|
|
|
|
" first_edge_indexes[i] = first_edge_index;\n"
|
|
|
|
" first_edge_index += edge_fields_count *\n"
|
|
|
|
" parsed.nodes[i * node_fields_count + edge_count_offset];\n"
|
|
|
|
"}\n");
|
2010-09-14 11:49:06 +00:00
|
|
|
CHECK(!meta_analysis_result.IsEmpty());
|
|
|
|
|
|
|
|
// A helper function for processing encoded nodes.
|
|
|
|
CompileRun(
|
2010-11-18 10:38:25 +00:00
|
|
|
"function GetChildPosByProperty(pos, prop_name, prop_type) {\n"
|
2010-09-14 11:49:06 +00:00
|
|
|
" var nodes = parsed.nodes;\n"
|
2012-04-13 12:50:48 +00:00
|
|
|
" var edges = parsed.edges;\n"
|
2010-09-14 11:49:06 +00:00
|
|
|
" var strings = parsed.strings;\n"
|
2012-06-06 10:57:20 +00:00
|
|
|
" var node_ordinal = pos / node_fields_count;\n"
|
|
|
|
" for (var i = parsed.first_edge_indexes[node_ordinal],\n"
|
|
|
|
" count = parsed.first_edge_indexes[node_ordinal + 1];\n"
|
2012-04-13 12:50:48 +00:00
|
|
|
" i < count; i += edge_fields_count) {\n"
|
|
|
|
" if (edges[i + edge_type_offset] === prop_type\n"
|
|
|
|
" && strings[edges[i + edge_name_offset]] === prop_name)\n"
|
|
|
|
" return edges[i + edge_to_node_offset];\n"
|
2010-09-14 11:49:06 +00:00
|
|
|
" }\n"
|
|
|
|
" return null;\n"
|
|
|
|
"}\n");
|
|
|
|
// Get the string index using the path: <root> -> <global>.b.x.s
|
|
|
|
v8::Local<v8::Value> string_obj_pos_val = CompileRun(
|
|
|
|
"GetChildPosByProperty(\n"
|
|
|
|
" GetChildPosByProperty(\n"
|
|
|
|
" GetChildPosByProperty("
|
2012-06-06 10:57:20 +00:00
|
|
|
" parsed.edges[edge_to_node_offset],"
|
2012-04-16 14:31:13 +00:00
|
|
|
" \"b\", property_type),\n"
|
2010-11-18 10:38:25 +00:00
|
|
|
" \"x\", property_type),"
|
|
|
|
" \"s\", property_type)");
|
2010-09-14 11:49:06 +00:00
|
|
|
CHECK(!string_obj_pos_val.IsEmpty());
|
|
|
|
int string_obj_pos =
|
|
|
|
static_cast<int>(string_obj_pos_val->ToNumber()->Value());
|
|
|
|
v8::Local<v8::Object> nodes_array =
|
2011-09-26 14:38:29 +00:00
|
|
|
parsed_snapshot->Get(v8_str("nodes"))->ToObject();
|
2010-09-14 11:49:06 +00:00
|
|
|
int string_index = static_cast<int>(
|
|
|
|
nodes_array->Get(string_obj_pos + 1)->ToNumber()->Value());
|
|
|
|
CHECK_GT(string_index, 0);
|
|
|
|
v8::Local<v8::Object> strings_array =
|
2011-09-26 14:38:29 +00:00
|
|
|
parsed_snapshot->Get(v8_str("strings"))->ToObject();
|
2010-09-14 11:49:06 +00:00
|
|
|
v8::Local<v8::String> string = strings_array->Get(string_index)->ToString();
|
|
|
|
v8::Local<v8::String> ref_string =
|
|
|
|
CompileRun(STRING_LITERAL_FOR_TEST)->ToString();
|
|
|
|
#undef STRING_LITERAL_FOR_TEST
|
|
|
|
CHECK_EQ(*v8::String::Utf8Value(ref_string),
|
|
|
|
*v8::String::Utf8Value(string));
|
|
|
|
}
|
|
|
|
|
2010-09-14 15:11:24 +00:00
|
|
|
|
|
|
|
TEST(HeapSnapshotJSONSerializationAborting) {
|
|
|
|
LocalContext env;
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
|
2010-09-14 15:11:24 +00:00
|
|
|
const v8::HeapSnapshot* snapshot =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("abort"));
|
2010-09-14 15:11:24 +00:00
|
|
|
TestJSONStream stream(5);
|
|
|
|
snapshot->Serialize(&stream, v8::HeapSnapshot::kJSON);
|
|
|
|
CHECK_GT(stream.size(), 0);
|
|
|
|
CHECK_EQ(0, stream.eos_signaled());
|
|
|
|
}
|
|
|
|
|
2012-04-13 08:52:25 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class TestStatsStream : public v8::OutputStream {
|
|
|
|
public:
|
|
|
|
TestStatsStream()
|
|
|
|
: eos_signaled_(0),
|
2012-04-17 13:10:17 +00:00
|
|
|
updates_written_(0),
|
2012-04-13 08:52:25 +00:00
|
|
|
entries_count_(0),
|
2012-04-16 09:44:42 +00:00
|
|
|
entries_size_(0),
|
2012-04-13 08:52:25 +00:00
|
|
|
intervals_count_(0),
|
|
|
|
first_interval_index_(-1) { }
|
|
|
|
TestStatsStream(const TestStatsStream& stream)
|
2012-04-13 09:19:01 +00:00
|
|
|
: v8::OutputStream(stream),
|
|
|
|
eos_signaled_(stream.eos_signaled_),
|
2012-04-17 13:10:17 +00:00
|
|
|
updates_written_(stream.updates_written_),
|
2012-04-13 08:52:25 +00:00
|
|
|
entries_count_(stream.entries_count_),
|
2012-04-16 09:44:42 +00:00
|
|
|
entries_size_(stream.entries_size_),
|
2012-04-13 08:52:25 +00:00
|
|
|
intervals_count_(stream.intervals_count_),
|
|
|
|
first_interval_index_(stream.first_interval_index_) { }
|
|
|
|
virtual ~TestStatsStream() {}
|
|
|
|
virtual void EndOfStream() { ++eos_signaled_; }
|
|
|
|
virtual WriteResult WriteAsciiChunk(char* buffer, int chars_written) {
|
|
|
|
ASSERT(false);
|
|
|
|
return kAbort;
|
|
|
|
}
|
2012-04-17 13:10:17 +00:00
|
|
|
virtual WriteResult WriteHeapStatsChunk(v8::HeapStatsUpdate* buffer,
|
|
|
|
int updates_written) {
|
2012-04-13 08:52:25 +00:00
|
|
|
++intervals_count_;
|
2012-04-17 13:10:17 +00:00
|
|
|
ASSERT(updates_written);
|
|
|
|
updates_written_ += updates_written;
|
2012-04-13 08:52:25 +00:00
|
|
|
entries_count_ = 0;
|
2012-04-17 13:10:17 +00:00
|
|
|
if (first_interval_index_ == -1 && updates_written != 0)
|
|
|
|
first_interval_index_ = buffer[0].index;
|
|
|
|
for (int i = 0; i < updates_written; ++i) {
|
|
|
|
entries_count_ += buffer[i].count;
|
|
|
|
entries_size_ += buffer[i].size;
|
2012-04-16 09:44:42 +00:00
|
|
|
}
|
2012-04-13 08:52:25 +00:00
|
|
|
|
|
|
|
return kContinue;
|
|
|
|
}
|
|
|
|
int eos_signaled() { return eos_signaled_; }
|
2012-04-17 13:10:17 +00:00
|
|
|
int updates_written() { return updates_written_; }
|
2012-04-13 08:52:25 +00:00
|
|
|
uint32_t entries_count() const { return entries_count_; }
|
2012-04-16 09:44:42 +00:00
|
|
|
uint32_t entries_size() const { return entries_size_; }
|
2012-04-13 08:52:25 +00:00
|
|
|
int intervals_count() const { return intervals_count_; }
|
|
|
|
int first_interval_index() const { return first_interval_index_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
int eos_signaled_;
|
2012-04-17 13:10:17 +00:00
|
|
|
int updates_written_;
|
2012-04-13 08:52:25 +00:00
|
|
|
uint32_t entries_count_;
|
2012-04-16 09:44:42 +00:00
|
|
|
uint32_t entries_size_;
|
2012-04-13 08:52:25 +00:00
|
|
|
int intervals_count_;
|
|
|
|
int first_interval_index_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2012-06-01 16:10:52 +00:00
|
|
|
static TestStatsStream GetHeapStatsUpdate(
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::HeapProfiler* heap_profiler,
|
2012-06-01 16:10:52 +00:00
|
|
|
v8::SnapshotObjectId* object_id = NULL) {
|
2012-04-13 08:52:25 +00:00
|
|
|
TestStatsStream stream;
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::SnapshotObjectId last_seen_id = heap_profiler->GetHeapStats(&stream);
|
2012-06-01 16:10:52 +00:00
|
|
|
if (object_id)
|
|
|
|
*object_id = last_seen_id;
|
2012-04-13 08:52:25 +00:00
|
|
|
CHECK_EQ(1, stream.eos_signaled());
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(HeapSnapshotObjectsStats) {
|
|
|
|
LocalContext env;
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
|
2012-04-13 08:52:25 +00:00
|
|
|
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->StartTrackingHeapObjects();
|
2012-06-25 13:55:36 +00:00
|
|
|
// We have to call GC 6 times. In other case the garbage will be
|
2012-04-13 08:52:25 +00:00
|
|
|
// the reason of flakiness.
|
2012-06-25 13:55:36 +00:00
|
|
|
for (int i = 0; i < 6; ++i) {
|
2012-04-13 08:52:25 +00:00
|
|
|
HEAP->CollectAllGarbage(i::Heap::kNoGCFlags);
|
|
|
|
}
|
|
|
|
|
2012-06-01 16:10:52 +00:00
|
|
|
v8::SnapshotObjectId initial_id;
|
2012-04-13 08:52:25 +00:00
|
|
|
{
|
|
|
|
// Single chunk of data expected in update. Initial data.
|
2013-04-02 08:03:01 +00:00
|
|
|
TestStatsStream stats_update = GetHeapStatsUpdate(heap_profiler,
|
|
|
|
&initial_id);
|
2012-04-13 08:52:25 +00:00
|
|
|
CHECK_EQ(1, stats_update.intervals_count());
|
2012-04-17 13:10:17 +00:00
|
|
|
CHECK_EQ(1, stats_update.updates_written());
|
2012-04-16 09:44:42 +00:00
|
|
|
CHECK_LT(0, stats_update.entries_size());
|
2012-04-13 08:52:25 +00:00
|
|
|
CHECK_EQ(0, stats_update.first_interval_index());
|
|
|
|
}
|
|
|
|
|
|
|
|
// No data expected in update because nothing has happened.
|
2012-06-01 16:10:52 +00:00
|
|
|
v8::SnapshotObjectId same_id;
|
2013-04-02 08:03:01 +00:00
|
|
|
CHECK_EQ(0, GetHeapStatsUpdate(heap_profiler, &same_id).updates_written());
|
2012-06-01 16:10:52 +00:00
|
|
|
CHECK_EQ_SNAPSHOT_OBJECT_ID(initial_id, same_id);
|
|
|
|
|
2012-04-13 08:52:25 +00:00
|
|
|
{
|
2012-06-01 16:10:52 +00:00
|
|
|
v8::SnapshotObjectId additional_string_id;
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope inner_scope_1(env->GetIsolate());
|
2012-04-16 13:27:38 +00:00
|
|
|
v8_str("string1");
|
2012-04-13 08:52:25 +00:00
|
|
|
{
|
|
|
|
// Single chunk of data with one new entry expected in update.
|
2013-04-02 08:03:01 +00:00
|
|
|
TestStatsStream stats_update = GetHeapStatsUpdate(heap_profiler,
|
|
|
|
&additional_string_id);
|
2012-06-01 16:10:52 +00:00
|
|
|
CHECK_LT(same_id, additional_string_id);
|
2012-04-13 08:52:25 +00:00
|
|
|
CHECK_EQ(1, stats_update.intervals_count());
|
2012-04-17 13:10:17 +00:00
|
|
|
CHECK_EQ(1, stats_update.updates_written());
|
2012-04-16 09:44:42 +00:00
|
|
|
CHECK_LT(0, stats_update.entries_size());
|
2012-04-13 08:52:25 +00:00
|
|
|
CHECK_EQ(1, stats_update.entries_count());
|
|
|
|
CHECK_EQ(2, stats_update.first_interval_index());
|
|
|
|
}
|
|
|
|
|
|
|
|
// No data expected in update because nothing happened.
|
2012-06-01 16:10:52 +00:00
|
|
|
v8::SnapshotObjectId last_id;
|
2013-04-02 08:03:01 +00:00
|
|
|
CHECK_EQ(0, GetHeapStatsUpdate(heap_profiler, &last_id).updates_written());
|
2012-06-01 16:10:52 +00:00
|
|
|
CHECK_EQ_SNAPSHOT_OBJECT_ID(additional_string_id, last_id);
|
2012-04-13 08:52:25 +00:00
|
|
|
|
|
|
|
{
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope inner_scope_2(env->GetIsolate());
|
2012-04-16 13:27:38 +00:00
|
|
|
v8_str("string2");
|
2012-04-13 08:52:25 +00:00
|
|
|
|
2012-04-16 09:44:42 +00:00
|
|
|
uint32_t entries_size;
|
2012-04-13 08:52:25 +00:00
|
|
|
{
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope inner_scope_3(env->GetIsolate());
|
2012-04-16 13:27:38 +00:00
|
|
|
v8_str("string3");
|
|
|
|
v8_str("string4");
|
2012-04-13 08:52:25 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
// Single chunk of data with three new entries expected in update.
|
2013-04-02 08:03:01 +00:00
|
|
|
TestStatsStream stats_update = GetHeapStatsUpdate(heap_profiler);
|
2012-04-13 08:52:25 +00:00
|
|
|
CHECK_EQ(1, stats_update.intervals_count());
|
2012-04-17 13:10:17 +00:00
|
|
|
CHECK_EQ(1, stats_update.updates_written());
|
2012-04-16 09:44:42 +00:00
|
|
|
CHECK_LT(0, entries_size = stats_update.entries_size());
|
2012-04-13 08:52:25 +00:00
|
|
|
CHECK_EQ(3, stats_update.entries_count());
|
|
|
|
CHECK_EQ(4, stats_update.first_interval_index());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Single chunk of data with two left entries expected in update.
|
2013-04-02 08:03:01 +00:00
|
|
|
TestStatsStream stats_update = GetHeapStatsUpdate(heap_profiler);
|
2012-04-13 08:52:25 +00:00
|
|
|
CHECK_EQ(1, stats_update.intervals_count());
|
2012-04-17 13:10:17 +00:00
|
|
|
CHECK_EQ(1, stats_update.updates_written());
|
2012-04-16 09:44:42 +00:00
|
|
|
CHECK_GT(entries_size, stats_update.entries_size());
|
2012-04-13 08:52:25 +00:00
|
|
|
CHECK_EQ(1, stats_update.entries_count());
|
|
|
|
// Two strings from forth interval were released.
|
|
|
|
CHECK_EQ(4, stats_update.first_interval_index());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Single chunk of data with 0 left entries expected in update.
|
2013-04-02 08:03:01 +00:00
|
|
|
TestStatsStream stats_update = GetHeapStatsUpdate(heap_profiler);
|
2012-04-13 08:52:25 +00:00
|
|
|
CHECK_EQ(1, stats_update.intervals_count());
|
2012-04-17 13:10:17 +00:00
|
|
|
CHECK_EQ(1, stats_update.updates_written());
|
2012-04-16 09:44:42 +00:00
|
|
|
CHECK_EQ(0, stats_update.entries_size());
|
2012-04-13 08:52:25 +00:00
|
|
|
CHECK_EQ(0, stats_update.entries_count());
|
|
|
|
// The last string from forth interval was released.
|
|
|
|
CHECK_EQ(4, stats_update.first_interval_index());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
// Single chunk of data with 0 left entries expected in update.
|
2013-04-02 08:03:01 +00:00
|
|
|
TestStatsStream stats_update = GetHeapStatsUpdate(heap_profiler);
|
2012-04-13 08:52:25 +00:00
|
|
|
CHECK_EQ(1, stats_update.intervals_count());
|
2012-04-17 13:10:17 +00:00
|
|
|
CHECK_EQ(1, stats_update.updates_written());
|
2012-04-16 09:44:42 +00:00
|
|
|
CHECK_EQ(0, stats_update.entries_size());
|
2012-04-13 08:52:25 +00:00
|
|
|
CHECK_EQ(0, stats_update.entries_count());
|
|
|
|
// The only string from the second interval was released.
|
|
|
|
CHECK_EQ(2, stats_update.first_interval_index());
|
|
|
|
}
|
|
|
|
|
2012-04-16 09:44:42 +00:00
|
|
|
v8::Local<v8::Array> array = v8::Array::New();
|
|
|
|
CHECK_EQ(0, array->Length());
|
|
|
|
// Force array's buffer allocation.
|
|
|
|
array->Set(2, v8_num(7));
|
|
|
|
|
|
|
|
uint32_t entries_size;
|
|
|
|
{
|
|
|
|
// Single chunk of data with 2 entries expected in update.
|
2013-04-02 08:03:01 +00:00
|
|
|
TestStatsStream stats_update = GetHeapStatsUpdate(heap_profiler);
|
2012-04-16 09:44:42 +00:00
|
|
|
CHECK_EQ(1, stats_update.intervals_count());
|
2012-04-17 13:10:17 +00:00
|
|
|
CHECK_EQ(1, stats_update.updates_written());
|
2012-04-16 09:44:42 +00:00
|
|
|
CHECK_LT(0, entries_size = stats_update.entries_size());
|
|
|
|
// They are the array and its buffer.
|
|
|
|
CHECK_EQ(2, stats_update.entries_count());
|
|
|
|
CHECK_EQ(8, stats_update.first_interval_index());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < 100; ++i)
|
|
|
|
array->Set(i, v8_num(i));
|
|
|
|
|
|
|
|
{
|
|
|
|
// Single chunk of data with 1 entry expected in update.
|
2013-04-02 08:03:01 +00:00
|
|
|
TestStatsStream stats_update = GetHeapStatsUpdate(heap_profiler);
|
2012-04-16 09:44:42 +00:00
|
|
|
CHECK_EQ(1, stats_update.intervals_count());
|
|
|
|
// The first interval was changed because old buffer was collected.
|
|
|
|
// The second interval was changed because new buffer was allocated.
|
2012-04-17 13:10:17 +00:00
|
|
|
CHECK_EQ(2, stats_update.updates_written());
|
2012-04-16 09:44:42 +00:00
|
|
|
CHECK_LT(entries_size, stats_update.entries_size());
|
|
|
|
CHECK_EQ(2, stats_update.entries_count());
|
|
|
|
CHECK_EQ(8, stats_update.first_interval_index());
|
|
|
|
}
|
|
|
|
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->StopTrackingHeapObjects();
|
2012-04-13 08:52:25 +00:00
|
|
|
}
|
|
|
|
|
2010-11-24 10:47:18 +00:00
|
|
|
|
2011-12-06 17:41:47 +00:00
|
|
|
static void CheckChildrenIds(const v8::HeapSnapshot* snapshot,
|
|
|
|
const v8::HeapGraphNode* node,
|
|
|
|
int level, int max_level) {
|
|
|
|
if (level > max_level) return;
|
|
|
|
CHECK_EQ(node, snapshot->GetNodeById(node->GetId()));
|
|
|
|
for (int i = 0, count = node->GetChildrenCount(); i < count; ++i) {
|
|
|
|
const v8::HeapGraphEdge* prop = node->GetChild(i);
|
|
|
|
const v8::HeapGraphNode* child =
|
|
|
|
snapshot->GetNodeById(prop->GetToNode()->GetId());
|
2012-03-26 13:47:37 +00:00
|
|
|
CHECK_EQ_SNAPSHOT_OBJECT_ID(prop->GetToNode()->GetId(), child->GetId());
|
2011-12-06 17:41:47 +00:00
|
|
|
CHECK_EQ(prop->GetToNode(), child);
|
|
|
|
CheckChildrenIds(snapshot, child, level + 1, max_level);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-02 15:38:51 +00:00
|
|
|
TEST(HeapSnapshotGetNodeById) {
|
|
|
|
LocalContext env;
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
|
2010-12-02 15:38:51 +00:00
|
|
|
|
|
|
|
const v8::HeapSnapshot* snapshot =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("id"));
|
2010-12-02 15:38:51 +00:00
|
|
|
const v8::HeapGraphNode* root = snapshot->GetRoot();
|
2011-12-06 17:41:47 +00:00
|
|
|
CheckChildrenIds(snapshot, root, 0, 3);
|
2010-12-02 15:38:51 +00:00
|
|
|
// Check a big id, which should not exist yet.
|
|
|
|
CHECK_EQ(NULL, snapshot->GetNodeById(0x1000000UL));
|
|
|
|
}
|
|
|
|
|
2010-12-13 10:42:06 +00:00
|
|
|
|
2012-04-16 15:36:19 +00:00
|
|
|
TEST(HeapSnapshotGetSnapshotObjectId) {
|
|
|
|
LocalContext env;
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
|
2012-04-16 15:36:19 +00:00
|
|
|
CompileRun("globalObject = {};\n");
|
|
|
|
const v8::HeapSnapshot* snapshot =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("get_snapshot_object_id"));
|
2012-04-16 15:36:19 +00:00
|
|
|
const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
|
|
|
|
const v8::HeapGraphNode* global_object =
|
|
|
|
GetProperty(global, v8::HeapGraphEdge::kProperty, "globalObject");
|
|
|
|
CHECK(global_object);
|
|
|
|
|
|
|
|
v8::Local<v8::Value> globalObjectHandle =
|
|
|
|
env->Global()->Get(v8::String::New("globalObject"));
|
|
|
|
CHECK(!globalObjectHandle.IsEmpty());
|
|
|
|
CHECK(globalObjectHandle->IsObject());
|
|
|
|
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::SnapshotObjectId id = heap_profiler->GetObjectId(globalObjectHandle);
|
2012-04-16 15:36:19 +00:00
|
|
|
CHECK_NE(static_cast<int>(v8::HeapProfiler::kUnknownObjectId),
|
|
|
|
id);
|
|
|
|
CHECK_EQ(static_cast<int>(id), global_object->GetId());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(HeapSnapshotUnknownSnapshotObjectId) {
|
|
|
|
LocalContext env;
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
|
2012-04-16 15:36:19 +00:00
|
|
|
CompileRun("globalObject = {};\n");
|
|
|
|
const v8::HeapSnapshot* snapshot =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("unknown_object_id"));
|
2012-04-16 15:36:19 +00:00
|
|
|
const v8::HeapGraphNode* node =
|
|
|
|
snapshot->GetNodeById(v8::HeapProfiler::kUnknownObjectId);
|
|
|
|
CHECK_EQ(NULL, node);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-13 10:42:06 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class TestActivityControl : public v8::ActivityControl {
|
|
|
|
public:
|
|
|
|
explicit TestActivityControl(int abort_count)
|
|
|
|
: done_(0), total_(0), abort_count_(abort_count) {}
|
|
|
|
ControlOption ReportProgressValue(int done, int total) {
|
|
|
|
done_ = done;
|
|
|
|
total_ = total;
|
|
|
|
return --abort_count_ != 0 ? kContinue : kAbort;
|
|
|
|
}
|
|
|
|
int done() { return done_; }
|
|
|
|
int total() { return total_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
int done_;
|
|
|
|
int total_;
|
|
|
|
int abort_count_;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TakeHeapSnapshotAborting) {
|
|
|
|
LocalContext env;
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2010-12-13 10:42:06 +00:00
|
|
|
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
|
|
|
|
const int snapshots_count = heap_profiler->GetSnapshotCount();
|
2012-02-24 15:46:45 +00:00
|
|
|
TestActivityControl aborting_control(1);
|
2010-12-13 10:42:06 +00:00
|
|
|
const v8::HeapSnapshot* no_snapshot =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("abort"),
|
2010-12-13 10:42:06 +00:00
|
|
|
&aborting_control);
|
|
|
|
CHECK_EQ(NULL, no_snapshot);
|
2013-04-02 08:03:01 +00:00
|
|
|
CHECK_EQ(snapshots_count, heap_profiler->GetSnapshotCount());
|
2010-12-13 10:42:06 +00:00
|
|
|
CHECK_GT(aborting_control.total(), aborting_control.done());
|
|
|
|
|
|
|
|
TestActivityControl control(-1); // Don't abort.
|
|
|
|
const v8::HeapSnapshot* snapshot =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("full"),
|
2010-12-13 10:42:06 +00:00
|
|
|
&control);
|
|
|
|
CHECK_NE(NULL, snapshot);
|
2013-04-02 08:03:01 +00:00
|
|
|
CHECK_EQ(snapshots_count + 1, heap_profiler->GetSnapshotCount());
|
2010-12-13 10:42:06 +00:00
|
|
|
CHECK_EQ(control.total(), control.done());
|
|
|
|
CHECK_GT(control.total(), 0);
|
|
|
|
}
|
|
|
|
|
2011-03-10 12:05:31 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class TestRetainedObjectInfo : public v8::RetainedObjectInfo {
|
|
|
|
public:
|
|
|
|
TestRetainedObjectInfo(int hash,
|
2012-01-27 12:02:57 +00:00
|
|
|
const char* group_label,
|
2011-03-10 12:05:31 +00:00
|
|
|
const char* label,
|
|
|
|
intptr_t element_count = -1,
|
|
|
|
intptr_t size = -1)
|
|
|
|
: disposed_(false),
|
|
|
|
hash_(hash),
|
2012-01-27 12:02:57 +00:00
|
|
|
group_label_(group_label),
|
2011-03-10 12:05:31 +00:00
|
|
|
label_(label),
|
|
|
|
element_count_(element_count),
|
|
|
|
size_(size) {
|
|
|
|
instances.Add(this);
|
|
|
|
}
|
|
|
|
virtual ~TestRetainedObjectInfo() {}
|
|
|
|
virtual void Dispose() {
|
|
|
|
CHECK(!disposed_);
|
|
|
|
disposed_ = true;
|
|
|
|
}
|
|
|
|
virtual bool IsEquivalent(RetainedObjectInfo* other) {
|
|
|
|
return GetHash() == other->GetHash();
|
|
|
|
}
|
|
|
|
virtual intptr_t GetHash() { return hash_; }
|
2012-01-27 12:02:57 +00:00
|
|
|
virtual const char* GetGroupLabel() { return group_label_; }
|
2011-03-10 12:05:31 +00:00
|
|
|
virtual const char* GetLabel() { return label_; }
|
|
|
|
virtual intptr_t GetElementCount() { return element_count_; }
|
|
|
|
virtual intptr_t GetSizeInBytes() { return size_; }
|
|
|
|
bool disposed() { return disposed_; }
|
|
|
|
|
|
|
|
static v8::RetainedObjectInfo* WrapperInfoCallback(
|
|
|
|
uint16_t class_id, v8::Handle<v8::Value> wrapper) {
|
|
|
|
if (class_id == 1) {
|
|
|
|
if (wrapper->IsString()) {
|
|
|
|
v8::String::AsciiValue ascii(wrapper);
|
|
|
|
if (strcmp(*ascii, "AAA") == 0)
|
2012-01-27 12:02:57 +00:00
|
|
|
return new TestRetainedObjectInfo(1, "aaa-group", "aaa", 100);
|
2011-03-10 12:05:31 +00:00
|
|
|
else if (strcmp(*ascii, "BBB") == 0)
|
2012-01-27 12:02:57 +00:00
|
|
|
return new TestRetainedObjectInfo(1, "aaa-group", "aaa", 100);
|
2011-03-10 12:05:31 +00:00
|
|
|
}
|
|
|
|
} else if (class_id == 2) {
|
|
|
|
if (wrapper->IsString()) {
|
|
|
|
v8::String::AsciiValue ascii(wrapper);
|
|
|
|
if (strcmp(*ascii, "CCC") == 0)
|
2012-01-27 12:02:57 +00:00
|
|
|
return new TestRetainedObjectInfo(2, "ccc-group", "ccc");
|
2011-03-10 12:05:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
CHECK(false);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static i::List<TestRetainedObjectInfo*> instances;
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool disposed_;
|
|
|
|
int hash_;
|
2012-01-27 12:02:57 +00:00
|
|
|
const char* group_label_;
|
2011-03-10 12:05:31 +00:00
|
|
|
const char* label_;
|
|
|
|
intptr_t element_count_;
|
|
|
|
intptr_t size_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
i::List<TestRetainedObjectInfo*> TestRetainedObjectInfo::instances;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const v8::HeapGraphNode* GetNode(const v8::HeapGraphNode* parent,
|
|
|
|
v8::HeapGraphNode::Type type,
|
|
|
|
const char* name) {
|
|
|
|
for (int i = 0, count = parent->GetChildrenCount(); i < count; ++i) {
|
|
|
|
const v8::HeapGraphNode* node = parent->GetChild(i)->GetToNode();
|
|
|
|
if (node->GetType() == type && strcmp(name,
|
|
|
|
const_cast<i::HeapEntry*>(
|
|
|
|
reinterpret_cast<const i::HeapEntry*>(node))->name()) == 0) {
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(HeapSnapshotRetainedObjectInfo) {
|
|
|
|
LocalContext env;
|
2013-01-25 08:31:46 +00:00
|
|
|
v8::Isolate* isolate = env->GetIsolate();
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(isolate);
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::HeapProfiler* heap_profiler = isolate->GetHeapProfiler();
|
2011-03-10 12:05:31 +00:00
|
|
|
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->SetWrapperClassInfoProvider(
|
2011-03-10 12:05:31 +00:00
|
|
|
1, TestRetainedObjectInfo::WrapperInfoCallback);
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->SetWrapperClassInfoProvider(
|
2011-03-10 12:05:31 +00:00
|
|
|
2, TestRetainedObjectInfo::WrapperInfoCallback);
|
|
|
|
v8::Persistent<v8::String> p_AAA =
|
2013-01-25 08:31:46 +00:00
|
|
|
v8::Persistent<v8::String>::New(isolate, v8_str("AAA"));
|
|
|
|
p_AAA.SetWrapperClassId(isolate, 1);
|
2011-03-10 12:05:31 +00:00
|
|
|
v8::Persistent<v8::String> p_BBB =
|
2013-01-25 08:31:46 +00:00
|
|
|
v8::Persistent<v8::String>::New(isolate, v8_str("BBB"));
|
|
|
|
p_BBB.SetWrapperClassId(isolate, 1);
|
2011-03-10 12:05:31 +00:00
|
|
|
v8::Persistent<v8::String> p_CCC =
|
2013-01-25 08:31:46 +00:00
|
|
|
v8::Persistent<v8::String>::New(isolate, v8_str("CCC"));
|
|
|
|
p_CCC.SetWrapperClassId(isolate, 2);
|
2011-03-10 12:05:31 +00:00
|
|
|
CHECK_EQ(0, TestRetainedObjectInfo::instances.length());
|
|
|
|
const v8::HeapSnapshot* snapshot =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("retained"));
|
2011-03-10 12:05:31 +00:00
|
|
|
|
|
|
|
CHECK_EQ(3, TestRetainedObjectInfo::instances.length());
|
|
|
|
for (int i = 0; i < TestRetainedObjectInfo::instances.length(); ++i) {
|
|
|
|
CHECK(TestRetainedObjectInfo::instances[i]->disposed());
|
|
|
|
delete TestRetainedObjectInfo::instances[i];
|
|
|
|
}
|
|
|
|
|
2012-01-27 12:02:57 +00:00
|
|
|
const v8::HeapGraphNode* native_group_aaa = GetNode(
|
2012-02-07 05:21:00 +00:00
|
|
|
snapshot->GetRoot(), v8::HeapGraphNode::kSynthetic, "aaa-group");
|
2012-01-27 12:02:57 +00:00
|
|
|
CHECK_NE(NULL, native_group_aaa);
|
|
|
|
CHECK_EQ(1, native_group_aaa->GetChildrenCount());
|
2011-03-10 12:05:31 +00:00
|
|
|
const v8::HeapGraphNode* aaa = GetNode(
|
2012-01-27 12:02:57 +00:00
|
|
|
native_group_aaa, v8::HeapGraphNode::kNative, "aaa / 100 entries");
|
2011-03-10 12:05:31 +00:00
|
|
|
CHECK_NE(NULL, aaa);
|
2012-01-27 12:02:57 +00:00
|
|
|
CHECK_EQ(2, aaa->GetChildrenCount());
|
|
|
|
|
|
|
|
const v8::HeapGraphNode* native_group_ccc = GetNode(
|
2012-02-07 05:21:00 +00:00
|
|
|
snapshot->GetRoot(), v8::HeapGraphNode::kSynthetic, "ccc-group");
|
2011-03-10 12:05:31 +00:00
|
|
|
const v8::HeapGraphNode* ccc = GetNode(
|
2012-01-27 12:02:57 +00:00
|
|
|
native_group_ccc, v8::HeapGraphNode::kNative, "ccc");
|
2011-03-10 12:05:31 +00:00
|
|
|
CHECK_NE(NULL, ccc);
|
|
|
|
|
|
|
|
const v8::HeapGraphNode* n_AAA = GetNode(
|
|
|
|
aaa, v8::HeapGraphNode::kString, "AAA");
|
|
|
|
CHECK_NE(NULL, n_AAA);
|
|
|
|
const v8::HeapGraphNode* n_BBB = GetNode(
|
|
|
|
aaa, v8::HeapGraphNode::kString, "BBB");
|
|
|
|
CHECK_NE(NULL, n_BBB);
|
|
|
|
CHECK_EQ(1, ccc->GetChildrenCount());
|
|
|
|
const v8::HeapGraphNode* n_CCC = GetNode(
|
|
|
|
ccc, v8::HeapGraphNode::kString, "CCC");
|
|
|
|
CHECK_NE(NULL, n_CCC);
|
|
|
|
|
2011-03-30 14:04:50 +00:00
|
|
|
CHECK_EQ(aaa, GetProperty(n_AAA, v8::HeapGraphEdge::kInternal, "native"));
|
|
|
|
CHECK_EQ(aaa, GetProperty(n_BBB, v8::HeapGraphEdge::kInternal, "native"));
|
|
|
|
CHECK_EQ(ccc, GetProperty(n_CCC, v8::HeapGraphEdge::kInternal, "native"));
|
2011-03-10 12:05:31 +00:00
|
|
|
}
|
|
|
|
|
2011-03-22 16:10:01 +00:00
|
|
|
|
2012-02-03 14:51:53 +00:00
|
|
|
class GraphWithImplicitRefs {
|
|
|
|
public:
|
|
|
|
static const int kObjectsCount = 4;
|
|
|
|
explicit GraphWithImplicitRefs(LocalContext* env) {
|
|
|
|
CHECK_EQ(NULL, instance_);
|
|
|
|
instance_ = this;
|
2013-04-24 15:59:23 +00:00
|
|
|
isolate_ = (*env)->GetIsolate();
|
2012-02-03 14:51:53 +00:00
|
|
|
for (int i = 0; i < kObjectsCount; i++) {
|
2013-04-24 15:59:23 +00:00
|
|
|
objects_[i] =
|
|
|
|
v8::Persistent<v8::Object>::New(isolate_, v8::Object::New());
|
2012-02-03 14:51:53 +00:00
|
|
|
}
|
|
|
|
(*env)->Global()->Set(v8_str("root_object"), objects_[0]);
|
|
|
|
}
|
|
|
|
~GraphWithImplicitRefs() {
|
|
|
|
instance_ = NULL;
|
|
|
|
}
|
|
|
|
|
2013-01-24 07:54:40 +00:00
|
|
|
static void gcPrologue(v8::GCType type, v8::GCCallbackFlags flags) {
|
2012-02-03 14:51:53 +00:00
|
|
|
instance_->AddImplicitReferences();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void AddImplicitReferences() {
|
|
|
|
// 0 -> 1
|
2013-04-24 15:59:23 +00:00
|
|
|
isolate_->SetObjectGroupId(v8::Persistent<v8::Object>::Cast(objects_[0]),
|
|
|
|
v8::UniqueId(1));
|
|
|
|
isolate_->SetReferenceFromGroup(
|
|
|
|
v8::UniqueId(1), v8::Persistent<v8::Object>::Cast(objects_[1]));
|
|
|
|
// Adding two more references: 1 -> 2, 1 -> 3
|
|
|
|
isolate_->SetReference(v8::Persistent<v8::Object>::Cast(objects_[1]),
|
|
|
|
v8::Persistent<v8::Object>::Cast(objects_[2]));
|
|
|
|
isolate_->SetReference(v8::Persistent<v8::Object>::Cast(objects_[1]),
|
|
|
|
v8::Persistent<v8::Object>::Cast(objects_[3]));
|
2012-02-03 14:51:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
v8::Persistent<v8::Value> objects_[kObjectsCount];
|
|
|
|
static GraphWithImplicitRefs* instance_;
|
2013-04-24 15:59:23 +00:00
|
|
|
v8::Isolate* isolate_;
|
2012-02-03 14:51:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
GraphWithImplicitRefs* GraphWithImplicitRefs::instance_ = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
TEST(HeapSnapshotImplicitReferences) {
|
|
|
|
LocalContext env;
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
|
2012-02-03 14:51:53 +00:00
|
|
|
|
|
|
|
GraphWithImplicitRefs graph(&env);
|
2013-01-24 07:54:40 +00:00
|
|
|
v8::V8::AddGCPrologueCallback(&GraphWithImplicitRefs::gcPrologue);
|
2012-02-03 14:51:53 +00:00
|
|
|
|
|
|
|
const v8::HeapSnapshot* snapshot =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("implicit_refs"));
|
2012-02-03 14:51:53 +00:00
|
|
|
|
|
|
|
const v8::HeapGraphNode* global_object = GetGlobalObject(snapshot);
|
|
|
|
const v8::HeapGraphNode* obj0 = GetProperty(
|
2012-04-16 14:31:13 +00:00
|
|
|
global_object, v8::HeapGraphEdge::kProperty, "root_object");
|
2012-02-03 14:51:53 +00:00
|
|
|
CHECK(obj0);
|
|
|
|
CHECK_EQ(v8::HeapGraphNode::kObject, obj0->GetType());
|
|
|
|
const v8::HeapGraphNode* obj1 = GetProperty(
|
|
|
|
obj0, v8::HeapGraphEdge::kInternal, "native");
|
|
|
|
CHECK(obj1);
|
|
|
|
int implicit_targets_count = 0;
|
|
|
|
for (int i = 0, count = obj1->GetChildrenCount(); i < count; ++i) {
|
|
|
|
const v8::HeapGraphEdge* prop = obj1->GetChild(i);
|
|
|
|
v8::String::AsciiValue prop_name(prop->GetName());
|
|
|
|
if (prop->GetType() == v8::HeapGraphEdge::kInternal &&
|
|
|
|
strcmp("native", *prop_name) == 0) {
|
|
|
|
++implicit_targets_count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CHECK_EQ(2, implicit_targets_count);
|
2013-01-24 07:54:40 +00:00
|
|
|
v8::V8::RemoveGCPrologueCallback(&GraphWithImplicitRefs::gcPrologue);
|
2012-02-03 14:51:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-22 16:10:01 +00:00
|
|
|
TEST(DeleteAllHeapSnapshots) {
|
|
|
|
LocalContext env;
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
|
|
|
|
|
|
|
|
CHECK_EQ(0, heap_profiler->GetSnapshotCount());
|
|
|
|
heap_profiler->DeleteAllHeapSnapshots();
|
|
|
|
CHECK_EQ(0, heap_profiler->GetSnapshotCount());
|
|
|
|
CHECK_NE(NULL, heap_profiler->TakeHeapSnapshot(v8_str("1")));
|
|
|
|
CHECK_EQ(1, heap_profiler->GetSnapshotCount());
|
|
|
|
heap_profiler->DeleteAllHeapSnapshots();
|
|
|
|
CHECK_EQ(0, heap_profiler->GetSnapshotCount());
|
|
|
|
CHECK_NE(NULL, heap_profiler->TakeHeapSnapshot(v8_str("1")));
|
|
|
|
CHECK_NE(NULL, heap_profiler->TakeHeapSnapshot(v8_str("2")));
|
|
|
|
CHECK_EQ(2, heap_profiler->GetSnapshotCount());
|
|
|
|
heap_profiler->DeleteAllHeapSnapshots();
|
|
|
|
CHECK_EQ(0, heap_profiler->GetSnapshotCount());
|
2011-03-22 16:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(DeleteHeapSnapshot) {
|
|
|
|
LocalContext env;
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
|
2011-03-22 16:10:01 +00:00
|
|
|
|
2013-04-02 08:03:01 +00:00
|
|
|
CHECK_EQ(0, heap_profiler->GetSnapshotCount());
|
2011-03-22 16:10:01 +00:00
|
|
|
const v8::HeapSnapshot* s1 =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("1"));
|
2011-03-22 16:10:01 +00:00
|
|
|
CHECK_NE(NULL, s1);
|
2013-04-02 08:03:01 +00:00
|
|
|
CHECK_EQ(1, heap_profiler->GetSnapshotCount());
|
2011-03-22 16:10:01 +00:00
|
|
|
unsigned uid1 = s1->GetUid();
|
2013-04-02 08:03:01 +00:00
|
|
|
CHECK_EQ(s1, heap_profiler->FindHeapSnapshot(uid1));
|
2011-03-22 16:10:01 +00:00
|
|
|
const_cast<v8::HeapSnapshot*>(s1)->Delete();
|
2013-04-02 08:03:01 +00:00
|
|
|
CHECK_EQ(0, heap_profiler->GetSnapshotCount());
|
|
|
|
CHECK_EQ(NULL, heap_profiler->FindHeapSnapshot(uid1));
|
2011-03-22 16:10:01 +00:00
|
|
|
|
|
|
|
const v8::HeapSnapshot* s2 =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("2"));
|
2011-03-22 16:10:01 +00:00
|
|
|
CHECK_NE(NULL, s2);
|
2013-04-02 08:03:01 +00:00
|
|
|
CHECK_EQ(1, heap_profiler->GetSnapshotCount());
|
2011-03-22 16:10:01 +00:00
|
|
|
unsigned uid2 = s2->GetUid();
|
|
|
|
CHECK_NE(static_cast<int>(uid1), static_cast<int>(uid2));
|
2013-04-02 08:03:01 +00:00
|
|
|
CHECK_EQ(s2, heap_profiler->FindHeapSnapshot(uid2));
|
2011-03-22 16:10:01 +00:00
|
|
|
const v8::HeapSnapshot* s3 =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("3"));
|
2011-03-22 16:10:01 +00:00
|
|
|
CHECK_NE(NULL, s3);
|
2013-04-02 08:03:01 +00:00
|
|
|
CHECK_EQ(2, heap_profiler->GetSnapshotCount());
|
2011-03-22 16:10:01 +00:00
|
|
|
unsigned uid3 = s3->GetUid();
|
|
|
|
CHECK_NE(static_cast<int>(uid1), static_cast<int>(uid3));
|
2013-04-02 08:03:01 +00:00
|
|
|
CHECK_EQ(s3, heap_profiler->FindHeapSnapshot(uid3));
|
2011-03-22 16:10:01 +00:00
|
|
|
const_cast<v8::HeapSnapshot*>(s2)->Delete();
|
2013-04-02 08:03:01 +00:00
|
|
|
CHECK_EQ(1, heap_profiler->GetSnapshotCount());
|
|
|
|
CHECK_EQ(NULL, heap_profiler->FindHeapSnapshot(uid2));
|
|
|
|
CHECK_EQ(s3, heap_profiler->FindHeapSnapshot(uid3));
|
2011-03-22 16:10:01 +00:00
|
|
|
const_cast<v8::HeapSnapshot*>(s3)->Delete();
|
2013-04-02 08:03:01 +00:00
|
|
|
CHECK_EQ(0, heap_profiler->GetSnapshotCount());
|
|
|
|
CHECK_EQ(NULL, heap_profiler->FindHeapSnapshot(uid3));
|
2011-03-22 16:10:01 +00:00
|
|
|
}
|
|
|
|
|
2011-05-30 14:31:47 +00:00
|
|
|
|
2012-12-04 17:17:55 +00:00
|
|
|
class NameResolver : public v8::HeapProfiler::ObjectNameResolver {
|
|
|
|
public:
|
|
|
|
virtual const char* GetName(v8::Handle<v8::Object> object) {
|
|
|
|
return "Global object name";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(GlobalObjectName) {
|
|
|
|
LocalContext env;
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
|
2012-12-04 17:17:55 +00:00
|
|
|
|
|
|
|
CompileRun("document = { URL:\"abcdefgh\" };");
|
|
|
|
|
|
|
|
NameResolver name_resolver;
|
|
|
|
const v8::HeapSnapshot* snapshot =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("document"),
|
2012-12-04 17:17:55 +00:00
|
|
|
NULL,
|
|
|
|
&name_resolver);
|
|
|
|
const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
|
|
|
|
CHECK_NE(NULL, global);
|
|
|
|
CHECK_EQ("Object / Global object name" ,
|
|
|
|
const_cast<i::HeapEntry*>(
|
|
|
|
reinterpret_cast<const i::HeapEntry*>(global))->name());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-18 11:42:39 +00:00
|
|
|
TEST(NoHandleLeaks) {
|
|
|
|
LocalContext env;
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
|
2011-11-18 11:42:39 +00:00
|
|
|
|
|
|
|
CompileRun("document = { URL:\"abcdefgh\" };");
|
|
|
|
|
|
|
|
v8::Handle<v8::String> name(v8_str("leakz"));
|
2013-02-25 14:46:09 +00:00
|
|
|
i::Isolate* isolate = i::Isolate::Current();
|
|
|
|
int count_before = i::HandleScope::NumberOfHandles(isolate);
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(name);
|
2013-02-25 14:46:09 +00:00
|
|
|
int count_after = i::HandleScope::NumberOfHandles(isolate);
|
2011-11-18 11:42:39 +00:00
|
|
|
CHECK_EQ(count_before, count_after);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-21 08:02:34 +00:00
|
|
|
TEST(NodesIteration) {
|
|
|
|
LocalContext env;
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
|
2011-06-21 08:02:34 +00:00
|
|
|
const v8::HeapSnapshot* snapshot =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("iteration"));
|
2011-06-21 08:02:34 +00:00
|
|
|
const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
|
|
|
|
CHECK_NE(NULL, global);
|
|
|
|
// Verify that we can find this object by iteration.
|
|
|
|
const int nodes_count = snapshot->GetNodesCount();
|
|
|
|
int count = 0;
|
|
|
|
for (int i = 0; i < nodes_count; ++i) {
|
|
|
|
if (snapshot->GetNode(i) == global)
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
CHECK_EQ(1, count);
|
|
|
|
}
|
2011-08-23 12:24:54 +00:00
|
|
|
|
|
|
|
|
2011-09-26 14:38:29 +00:00
|
|
|
TEST(GetHeapValue) {
|
|
|
|
LocalContext env;
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
|
2011-09-26 14:38:29 +00:00
|
|
|
|
|
|
|
CompileRun("a = { s_prop: \'value\', n_prop: 0.1 };");
|
|
|
|
const v8::HeapSnapshot* snapshot =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("value"));
|
2011-09-26 14:38:29 +00:00
|
|
|
const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
|
|
|
|
CHECK(global->GetHeapValue()->IsObject());
|
|
|
|
v8::Local<v8::Object> js_global =
|
|
|
|
env->Global()->GetPrototype().As<v8::Object>();
|
|
|
|
CHECK(js_global == global->GetHeapValue());
|
|
|
|
const v8::HeapGraphNode* obj = GetProperty(
|
2012-04-16 14:31:13 +00:00
|
|
|
global, v8::HeapGraphEdge::kProperty, "a");
|
2011-09-26 14:38:29 +00:00
|
|
|
CHECK(obj->GetHeapValue()->IsObject());
|
|
|
|
v8::Local<v8::Object> js_obj = js_global->Get(v8_str("a")).As<v8::Object>();
|
|
|
|
CHECK(js_obj == obj->GetHeapValue());
|
|
|
|
const v8::HeapGraphNode* s_prop =
|
|
|
|
GetProperty(obj, v8::HeapGraphEdge::kProperty, "s_prop");
|
|
|
|
v8::Local<v8::String> js_s_prop =
|
|
|
|
js_obj->Get(v8_str("s_prop")).As<v8::String>();
|
|
|
|
CHECK(js_s_prop == s_prop->GetHeapValue());
|
|
|
|
const v8::HeapGraphNode* n_prop =
|
|
|
|
GetProperty(obj, v8::HeapGraphEdge::kProperty, "n_prop");
|
|
|
|
v8::Local<v8::Number> js_n_prop =
|
|
|
|
js_obj->Get(v8_str("n_prop")).As<v8::Number>();
|
|
|
|
CHECK(js_n_prop == n_prop->GetHeapValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(GetHeapValueForDeletedObject) {
|
|
|
|
LocalContext env;
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
|
2011-09-26 14:38:29 +00:00
|
|
|
|
|
|
|
// It is impossible to delete a global property, so we are about to delete a
|
|
|
|
// property of the "a" object. Also, the "p" object can't be an empty one
|
|
|
|
// because the empty object is static and isn't actually deleted.
|
|
|
|
CompileRun("a = { p: { r: {} } };");
|
|
|
|
const v8::HeapSnapshot* snapshot =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("snapshot"));
|
2011-09-26 14:38:29 +00:00
|
|
|
const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
|
|
|
|
const v8::HeapGraphNode* obj = GetProperty(
|
2012-04-16 14:31:13 +00:00
|
|
|
global, v8::HeapGraphEdge::kProperty, "a");
|
2011-09-26 14:38:29 +00:00
|
|
|
const v8::HeapGraphNode* prop = GetProperty(
|
|
|
|
obj, v8::HeapGraphEdge::kProperty, "p");
|
|
|
|
{
|
|
|
|
// Perform the check inside a nested local scope to avoid creating a
|
|
|
|
// reference to the object we are deleting.
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2011-09-26 14:38:29 +00:00
|
|
|
CHECK(prop->GetHeapValue()->IsObject());
|
|
|
|
}
|
|
|
|
CompileRun("delete a.p;");
|
|
|
|
CHECK(prop->GetHeapValue()->IsUndefined());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-23 12:24:54 +00:00
|
|
|
static int StringCmp(const char* ref, i::String* act) {
|
2011-09-09 22:39:47 +00:00
|
|
|
i::SmartArrayPointer<char> s_act = act->ToCString();
|
2011-08-23 12:24:54 +00:00
|
|
|
int result = strcmp(ref, *s_act);
|
|
|
|
if (result != 0)
|
|
|
|
fprintf(stderr, "Expected: \"%s\", Actual: \"%s\"\n", ref, *s_act);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(GetConstructorName) {
|
|
|
|
LocalContext env;
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2011-08-23 12:24:54 +00:00
|
|
|
|
|
|
|
CompileRun(
|
|
|
|
"function Constructor1() {};\n"
|
|
|
|
"var obj1 = new Constructor1();\n"
|
|
|
|
"var Constructor2 = function() {};\n"
|
|
|
|
"var obj2 = new Constructor2();\n"
|
|
|
|
"var obj3 = {};\n"
|
|
|
|
"obj3.constructor = function Constructor3() {};\n"
|
|
|
|
"var obj4 = {};\n"
|
|
|
|
"// Slow properties\n"
|
|
|
|
"for (var i=0; i<2000; ++i) obj4[\"p\" + i] = i;\n"
|
|
|
|
"obj4.constructor = function Constructor4() {};\n"
|
|
|
|
"var obj5 = {};\n"
|
|
|
|
"var obj6 = {};\n"
|
|
|
|
"obj6.constructor = 6;");
|
|
|
|
v8::Local<v8::Object> js_global =
|
|
|
|
env->Global()->GetPrototype().As<v8::Object>();
|
|
|
|
v8::Local<v8::Object> obj1 = js_global->Get(v8_str("obj1")).As<v8::Object>();
|
|
|
|
i::Handle<i::JSObject> js_obj1 = v8::Utils::OpenHandle(*obj1);
|
|
|
|
CHECK_EQ(0, StringCmp(
|
|
|
|
"Constructor1", i::V8HeapExplorer::GetConstructorName(*js_obj1)));
|
|
|
|
v8::Local<v8::Object> obj2 = js_global->Get(v8_str("obj2")).As<v8::Object>();
|
|
|
|
i::Handle<i::JSObject> js_obj2 = v8::Utils::OpenHandle(*obj2);
|
|
|
|
CHECK_EQ(0, StringCmp(
|
|
|
|
"Constructor2", i::V8HeapExplorer::GetConstructorName(*js_obj2)));
|
|
|
|
v8::Local<v8::Object> obj3 = js_global->Get(v8_str("obj3")).As<v8::Object>();
|
|
|
|
i::Handle<i::JSObject> js_obj3 = v8::Utils::OpenHandle(*obj3);
|
|
|
|
CHECK_EQ(0, StringCmp(
|
|
|
|
"Constructor3", i::V8HeapExplorer::GetConstructorName(*js_obj3)));
|
|
|
|
v8::Local<v8::Object> obj4 = js_global->Get(v8_str("obj4")).As<v8::Object>();
|
|
|
|
i::Handle<i::JSObject> js_obj4 = v8::Utils::OpenHandle(*obj4);
|
|
|
|
CHECK_EQ(0, StringCmp(
|
|
|
|
"Constructor4", i::V8HeapExplorer::GetConstructorName(*js_obj4)));
|
|
|
|
v8::Local<v8::Object> obj5 = js_global->Get(v8_str("obj5")).As<v8::Object>();
|
|
|
|
i::Handle<i::JSObject> js_obj5 = v8::Utils::OpenHandle(*obj5);
|
|
|
|
CHECK_EQ(0, StringCmp(
|
|
|
|
"Object", i::V8HeapExplorer::GetConstructorName(*js_obj5)));
|
|
|
|
v8::Local<v8::Object> obj6 = js_global->Get(v8_str("obj6")).As<v8::Object>();
|
|
|
|
i::Handle<i::JSObject> js_obj6 = v8::Utils::OpenHandle(*obj6);
|
|
|
|
CHECK_EQ(0, StringCmp(
|
|
|
|
"Object", i::V8HeapExplorer::GetConstructorName(*js_obj6)));
|
|
|
|
}
|
2011-11-14 11:13:29 +00:00
|
|
|
|
2011-11-18 11:42:39 +00:00
|
|
|
|
2011-11-14 11:13:29 +00:00
|
|
|
TEST(FastCaseGetter) {
|
|
|
|
LocalContext env;
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
|
2011-11-14 11:13:29 +00:00
|
|
|
|
|
|
|
CompileRun("var obj1 = {};\n"
|
|
|
|
"obj1.__defineGetter__('propWithGetter', function Y() {\n"
|
|
|
|
" return 42;\n"
|
|
|
|
"});\n"
|
|
|
|
"obj1.__defineSetter__('propWithSetter', function Z(value) {\n"
|
|
|
|
" return this.value_ = value;\n"
|
|
|
|
"});\n");
|
|
|
|
const v8::HeapSnapshot* snapshot =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("fastCaseGetter"));
|
2011-11-14 11:13:29 +00:00
|
|
|
|
|
|
|
const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
|
|
|
|
CHECK_NE(NULL, global);
|
|
|
|
const v8::HeapGraphNode* obj1 =
|
2012-04-16 14:31:13 +00:00
|
|
|
GetProperty(global, v8::HeapGraphEdge::kProperty, "obj1");
|
2011-11-14 11:13:29 +00:00
|
|
|
CHECK_NE(NULL, obj1);
|
|
|
|
const v8::HeapGraphNode* getterFunction =
|
|
|
|
GetProperty(obj1, v8::HeapGraphEdge::kProperty, "get-propWithGetter");
|
|
|
|
CHECK_NE(NULL, getterFunction);
|
|
|
|
const v8::HeapGraphNode* setterFunction =
|
|
|
|
GetProperty(obj1, v8::HeapGraphEdge::kProperty, "set-propWithSetter");
|
|
|
|
CHECK_NE(NULL, setterFunction);
|
|
|
|
}
|
2011-12-06 17:41:47 +00:00
|
|
|
|
2012-07-02 12:46:31 +00:00
|
|
|
TEST(HiddenPropertiesFastCase) {
|
|
|
|
LocalContext env;
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
|
2012-07-02 12:46:31 +00:00
|
|
|
|
|
|
|
CompileRun(
|
|
|
|
"function C(x) { this.a = this; this.b = x; }\n"
|
|
|
|
"c = new C(2012);\n");
|
|
|
|
const v8::HeapSnapshot* snapshot =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("HiddenPropertiesFastCase1"));
|
2012-07-02 12:46:31 +00:00
|
|
|
const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
|
|
|
|
const v8::HeapGraphNode* c =
|
|
|
|
GetProperty(global, v8::HeapGraphEdge::kProperty, "c");
|
|
|
|
CHECK_NE(NULL, c);
|
|
|
|
const v8::HeapGraphNode* hidden_props =
|
|
|
|
GetProperty(c, v8::HeapGraphEdge::kInternal, "hidden_properties");
|
|
|
|
CHECK_EQ(NULL, hidden_props);
|
|
|
|
|
|
|
|
v8::Handle<v8::Value> cHandle = env->Global()->Get(v8::String::New("c"));
|
|
|
|
CHECK(!cHandle.IsEmpty() && cHandle->IsObject());
|
2012-07-30 13:09:09 +00:00
|
|
|
cHandle->ToObject()->SetHiddenValue(v8_str("key"), v8_str("val"));
|
2012-07-02 12:46:31 +00:00
|
|
|
|
2013-04-02 08:03:01 +00:00
|
|
|
snapshot = heap_profiler->TakeHeapSnapshot(
|
2012-07-02 12:46:31 +00:00
|
|
|
v8_str("HiddenPropertiesFastCase2"));
|
|
|
|
global = GetGlobalObject(snapshot);
|
|
|
|
c = GetProperty(global, v8::HeapGraphEdge::kProperty, "c");
|
|
|
|
CHECK_NE(NULL, c);
|
|
|
|
hidden_props = GetProperty(c, v8::HeapGraphEdge::kInternal,
|
|
|
|
"hidden_properties");
|
|
|
|
CHECK_NE(NULL, hidden_props);
|
|
|
|
}
|
2011-12-06 17:41:47 +00:00
|
|
|
|
|
|
|
bool HasWeakEdge(const v8::HeapGraphNode* node) {
|
|
|
|
for (int i = 0; i < node->GetChildrenCount(); ++i) {
|
|
|
|
const v8::HeapGraphEdge* handle_edge = node->GetChild(i);
|
|
|
|
if (handle_edge->GetType() == v8::HeapGraphEdge::kWeak) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool HasWeakGlobalHandle() {
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
|
|
|
v8::HeapProfiler* heap_profiler = isolate->GetHeapProfiler();
|
2011-12-06 17:41:47 +00:00
|
|
|
const v8::HeapSnapshot* snapshot =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("weaks"));
|
2011-12-06 17:41:47 +00:00
|
|
|
const v8::HeapGraphNode* gc_roots = GetNode(
|
|
|
|
snapshot->GetRoot(), v8::HeapGraphNode::kObject, "(GC roots)");
|
|
|
|
CHECK_NE(NULL, gc_roots);
|
|
|
|
const v8::HeapGraphNode* global_handles = GetNode(
|
|
|
|
gc_roots, v8::HeapGraphNode::kObject, "(Global handles)");
|
|
|
|
CHECK_NE(NULL, global_handles);
|
|
|
|
return HasWeakEdge(global_handles);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-25 08:31:46 +00:00
|
|
|
static void PersistentHandleCallback(v8::Isolate* isolate,
|
|
|
|
v8::Persistent<v8::Value> handle,
|
|
|
|
void*) {
|
|
|
|
handle.Dispose(isolate);
|
2011-12-06 17:41:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(WeakGlobalHandle) {
|
|
|
|
LocalContext env;
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2011-12-06 17:41:47 +00:00
|
|
|
|
|
|
|
CHECK(!HasWeakGlobalHandle());
|
|
|
|
|
|
|
|
v8::Persistent<v8::Object> handle =
|
2013-01-25 08:31:46 +00:00
|
|
|
v8::Persistent<v8::Object>::New(env->GetIsolate(), v8::Object::New());
|
|
|
|
handle.MakeWeak(env->GetIsolate(), NULL, PersistentHandleCallback);
|
2011-12-06 17:41:47 +00:00
|
|
|
|
|
|
|
CHECK(HasWeakGlobalHandle());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-17 09:03:08 +00:00
|
|
|
TEST(WeakNativeContextRefs) {
|
2011-12-06 17:41:47 +00:00
|
|
|
LocalContext env;
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
|
2011-12-06 17:41:47 +00:00
|
|
|
|
|
|
|
const v8::HeapSnapshot* snapshot =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("weaks"));
|
2011-12-06 17:41:47 +00:00
|
|
|
const v8::HeapGraphNode* gc_roots = GetNode(
|
|
|
|
snapshot->GetRoot(), v8::HeapGraphNode::kObject, "(GC roots)");
|
|
|
|
CHECK_NE(NULL, gc_roots);
|
|
|
|
const v8::HeapGraphNode* global_handles = GetNode(
|
|
|
|
gc_roots, v8::HeapGraphNode::kObject, "(Global handles)");
|
|
|
|
CHECK_NE(NULL, global_handles);
|
2012-08-17 09:03:08 +00:00
|
|
|
const v8::HeapGraphNode* native_context = GetNode(
|
|
|
|
global_handles, v8::HeapGraphNode::kHidden, "system / NativeContext");
|
|
|
|
CHECK_NE(NULL, native_context);
|
|
|
|
CHECK(HasWeakEdge(native_context));
|
2011-12-06 17:41:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(SfiAndJsFunctionWeakRefs) {
|
|
|
|
LocalContext env;
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
|
2011-12-06 17:41:47 +00:00
|
|
|
|
|
|
|
CompileRun(
|
|
|
|
"fun = (function (x) { return function () { return x + 1; } })(1);");
|
|
|
|
const v8::HeapSnapshot* snapshot =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("fun"));
|
2011-12-06 17:41:47 +00:00
|
|
|
const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
|
|
|
|
CHECK_NE(NULL, global);
|
|
|
|
const v8::HeapGraphNode* fun =
|
2012-04-16 14:31:13 +00:00
|
|
|
GetProperty(global, v8::HeapGraphEdge::kProperty, "fun");
|
2011-12-06 17:41:47 +00:00
|
|
|
CHECK(HasWeakEdge(fun));
|
|
|
|
const v8::HeapGraphNode* shared =
|
|
|
|
GetProperty(fun, v8::HeapGraphEdge::kInternal, "shared");
|
|
|
|
CHECK(HasWeakEdge(shared));
|
|
|
|
}
|
2012-03-07 17:38:50 +00:00
|
|
|
|
|
|
|
|
2012-07-05 16:23:14 +00:00
|
|
|
#ifdef ENABLE_DEBUGGER_SUPPORT
|
2012-04-17 15:57:35 +00:00
|
|
|
TEST(NoDebugObjectInSnapshot) {
|
|
|
|
LocalContext env;
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
|
2012-04-17 15:57:35 +00:00
|
|
|
|
|
|
|
v8::internal::Isolate::Current()->debug()->Load();
|
|
|
|
CompileRun("foo = {};");
|
|
|
|
const v8::HeapSnapshot* snapshot =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("snapshot"));
|
2012-04-17 15:57:35 +00:00
|
|
|
const v8::HeapGraphNode* root = snapshot->GetRoot();
|
|
|
|
int globals_count = 0;
|
|
|
|
for (int i = 0; i < root->GetChildrenCount(); ++i) {
|
|
|
|
const v8::HeapGraphEdge* edge = root->GetChild(i);
|
|
|
|
if (edge->GetType() == v8::HeapGraphEdge::kShortcut) {
|
|
|
|
++globals_count;
|
|
|
|
const v8::HeapGraphNode* global = edge->GetToNode();
|
|
|
|
const v8::HeapGraphNode* foo =
|
|
|
|
GetProperty(global, v8::HeapGraphEdge::kProperty, "foo");
|
|
|
|
CHECK_NE(NULL, foo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CHECK_EQ(1, globals_count);
|
|
|
|
}
|
2012-07-05 16:23:14 +00:00
|
|
|
#endif // ENABLE_DEBUGGER_SUPPORT
|
2012-04-17 15:57:35 +00:00
|
|
|
|
|
|
|
|
2012-04-19 15:58:42 +00:00
|
|
|
TEST(AllStrongGcRootsHaveNames) {
|
|
|
|
LocalContext env;
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
|
2012-04-19 15:58:42 +00:00
|
|
|
|
|
|
|
CompileRun("foo = {};");
|
|
|
|
const v8::HeapSnapshot* snapshot =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("snapshot"));
|
2012-04-19 15:58:42 +00:00
|
|
|
const v8::HeapGraphNode* gc_roots = GetNode(
|
|
|
|
snapshot->GetRoot(), v8::HeapGraphNode::kObject, "(GC roots)");
|
|
|
|
CHECK_NE(NULL, gc_roots);
|
|
|
|
const v8::HeapGraphNode* strong_roots = GetNode(
|
|
|
|
gc_roots, v8::HeapGraphNode::kObject, "(Strong roots)");
|
|
|
|
CHECK_NE(NULL, strong_roots);
|
|
|
|
for (int i = 0; i < strong_roots->GetChildrenCount(); ++i) {
|
|
|
|
const v8::HeapGraphEdge* edge = strong_roots->GetChild(i);
|
|
|
|
CHECK_EQ(v8::HeapGraphEdge::kInternal, edge->GetType());
|
|
|
|
v8::String::AsciiValue name(edge->GetName());
|
|
|
|
CHECK(isalpha(**name));
|
|
|
|
}
|
|
|
|
}
|
2012-04-20 14:03:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
TEST(NoRefsToNonEssentialEntries) {
|
|
|
|
LocalContext env;
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
|
2012-04-20 14:03:06 +00:00
|
|
|
CompileRun("global_object = {};\n");
|
|
|
|
const v8::HeapSnapshot* snapshot =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("snapshot"));
|
2012-04-20 14:03:06 +00:00
|
|
|
const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
|
|
|
|
const v8::HeapGraphNode* global_object =
|
|
|
|
GetProperty(global, v8::HeapGraphEdge::kProperty, "global_object");
|
|
|
|
CHECK_NE(NULL, global_object);
|
|
|
|
const v8::HeapGraphNode* properties =
|
|
|
|
GetProperty(global_object, v8::HeapGraphEdge::kInternal, "properties");
|
|
|
|
CHECK_EQ(NULL, properties);
|
|
|
|
const v8::HeapGraphNode* elements =
|
|
|
|
GetProperty(global_object, v8::HeapGraphEdge::kInternal, "elements");
|
|
|
|
CHECK_EQ(NULL, elements);
|
|
|
|
}
|
2012-08-16 14:25:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
TEST(MapHasDescriptorsAndTransitions) {
|
|
|
|
LocalContext env;
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
|
2012-08-16 14:25:55 +00:00
|
|
|
CompileRun("obj = { a: 10 };\n");
|
|
|
|
const v8::HeapSnapshot* snapshot =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("snapshot"));
|
2012-08-16 14:25:55 +00:00
|
|
|
const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
|
|
|
|
const v8::HeapGraphNode* global_object =
|
|
|
|
GetProperty(global, v8::HeapGraphEdge::kProperty, "obj");
|
|
|
|
CHECK_NE(NULL, global_object);
|
Sharing of descriptor arrays.
This CL adds multiple things:
Transition arrays do not directly point at their descriptor array anymore, but rather do so via an indirect pointer (a JSGlobalPropertyCell).
An ownership bit is added to maps indicating whether it owns its own descriptor array or not.
Maps owning a descriptor array can pass on ownership if a transition from that map is generated; but only if the descriptor array stays exactly the same; or if a descriptor is added.
Maps that don't have ownership get ownership back if their direct child to which ownership was passed is cleared in ClearNonLiveTransitions.
To detect which descriptors in an array are valid, each map knows its own NumberOfOwnDescriptors. Since the descriptors are sorted in order of addition, if we search and find a descriptor with index bigger than this number, it is not valid for the given map.
We currently still build up an enumeration cache (although this may disappear). The enumeration cache is always built for the entire descriptor array, even if not all descriptors are owned by the map. Once a descriptor array has an enumeration cache for a given map; this invariant will always be true, even if the descriptor array was extended. The extended array will inherit the enumeration cache from the smaller descriptor array. If a map with more descriptors needs an enumeration cache, it's EnumLength will still be set to invalid, so it will have to recompute the enumeration cache. This new cache will also be valid for smaller maps since they have their own enumlength; and use this to loop over the cache. If the EnumLength is still invalid, but there is already a cache present that is big enough; we just initialize the EnumLength field for the map.
When we apply ClearNonLiveTransitions and descriptor ownership is passed back to a parent map, the descriptor array is trimmed in-place and resorted. At the same time, the enumeration cache is trimmed in-place.
Only transition arrays contain descriptor arrays. If we transition to a map and pass ownership of the descriptor array along, the child map will not store the descriptor array it owns. Rather its parent will keep the pointer. So for every leaf-map, we find the descriptor array by following the back pointer, reading out the transition array, and fetching the descriptor array from the JSGlobalPropertyCell. If a map has a transition array, we fetch it from there. If a map has undefined as its back-pointer and has no transition array; it is considered to have an empty descriptor array.
When we modify properties, we cannot share the descriptor array. To accommodate this, the child map will get its own transition array; even if there are not necessarily any transitions leaving from the child map. This is necessary since it's the only way to store its own descriptor array.
Review URL: https://chromiumcodereview.appspot.com/10909007
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@12492 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-09-12 16:43:57 +00:00
|
|
|
|
2012-08-16 14:25:55 +00:00
|
|
|
const v8::HeapGraphNode* map =
|
|
|
|
GetProperty(global_object, v8::HeapGraphEdge::kInternal, "map");
|
|
|
|
CHECK_NE(NULL, map);
|
Sharing of descriptor arrays.
This CL adds multiple things:
Transition arrays do not directly point at their descriptor array anymore, but rather do so via an indirect pointer (a JSGlobalPropertyCell).
An ownership bit is added to maps indicating whether it owns its own descriptor array or not.
Maps owning a descriptor array can pass on ownership if a transition from that map is generated; but only if the descriptor array stays exactly the same; or if a descriptor is added.
Maps that don't have ownership get ownership back if their direct child to which ownership was passed is cleared in ClearNonLiveTransitions.
To detect which descriptors in an array are valid, each map knows its own NumberOfOwnDescriptors. Since the descriptors are sorted in order of addition, if we search and find a descriptor with index bigger than this number, it is not valid for the given map.
We currently still build up an enumeration cache (although this may disappear). The enumeration cache is always built for the entire descriptor array, even if not all descriptors are owned by the map. Once a descriptor array has an enumeration cache for a given map; this invariant will always be true, even if the descriptor array was extended. The extended array will inherit the enumeration cache from the smaller descriptor array. If a map with more descriptors needs an enumeration cache, it's EnumLength will still be set to invalid, so it will have to recompute the enumeration cache. This new cache will also be valid for smaller maps since they have their own enumlength; and use this to loop over the cache. If the EnumLength is still invalid, but there is already a cache present that is big enough; we just initialize the EnumLength field for the map.
When we apply ClearNonLiveTransitions and descriptor ownership is passed back to a parent map, the descriptor array is trimmed in-place and resorted. At the same time, the enumeration cache is trimmed in-place.
Only transition arrays contain descriptor arrays. If we transition to a map and pass ownership of the descriptor array along, the child map will not store the descriptor array it owns. Rather its parent will keep the pointer. So for every leaf-map, we find the descriptor array by following the back pointer, reading out the transition array, and fetching the descriptor array from the JSGlobalPropertyCell. If a map has a transition array, we fetch it from there. If a map has undefined as its back-pointer and has no transition array; it is considered to have an empty descriptor array.
When we modify properties, we cannot share the descriptor array. To accommodate this, the child map will get its own transition array; even if there are not necessarily any transitions leaving from the child map. This is necessary since it's the only way to store its own descriptor array.
Review URL: https://chromiumcodereview.appspot.com/10909007
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@12492 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-09-12 16:43:57 +00:00
|
|
|
const v8::HeapGraphNode* own_descriptors = GetProperty(
|
|
|
|
map, v8::HeapGraphEdge::kInternal, "descriptors");
|
2012-10-17 13:04:49 +00:00
|
|
|
CHECK_NE(NULL, own_descriptors);
|
Sharing of descriptor arrays.
This CL adds multiple things:
Transition arrays do not directly point at their descriptor array anymore, but rather do so via an indirect pointer (a JSGlobalPropertyCell).
An ownership bit is added to maps indicating whether it owns its own descriptor array or not.
Maps owning a descriptor array can pass on ownership if a transition from that map is generated; but only if the descriptor array stays exactly the same; or if a descriptor is added.
Maps that don't have ownership get ownership back if their direct child to which ownership was passed is cleared in ClearNonLiveTransitions.
To detect which descriptors in an array are valid, each map knows its own NumberOfOwnDescriptors. Since the descriptors are sorted in order of addition, if we search and find a descriptor with index bigger than this number, it is not valid for the given map.
We currently still build up an enumeration cache (although this may disappear). The enumeration cache is always built for the entire descriptor array, even if not all descriptors are owned by the map. Once a descriptor array has an enumeration cache for a given map; this invariant will always be true, even if the descriptor array was extended. The extended array will inherit the enumeration cache from the smaller descriptor array. If a map with more descriptors needs an enumeration cache, it's EnumLength will still be set to invalid, so it will have to recompute the enumeration cache. This new cache will also be valid for smaller maps since they have their own enumlength; and use this to loop over the cache. If the EnumLength is still invalid, but there is already a cache present that is big enough; we just initialize the EnumLength field for the map.
When we apply ClearNonLiveTransitions and descriptor ownership is passed back to a parent map, the descriptor array is trimmed in-place and resorted. At the same time, the enumeration cache is trimmed in-place.
Only transition arrays contain descriptor arrays. If we transition to a map and pass ownership of the descriptor array along, the child map will not store the descriptor array it owns. Rather its parent will keep the pointer. So for every leaf-map, we find the descriptor array by following the back pointer, reading out the transition array, and fetching the descriptor array from the JSGlobalPropertyCell. If a map has a transition array, we fetch it from there. If a map has undefined as its back-pointer and has no transition array; it is considered to have an empty descriptor array.
When we modify properties, we cannot share the descriptor array. To accommodate this, the child map will get its own transition array; even if there are not necessarily any transitions leaving from the child map. This is necessary since it's the only way to store its own descriptor array.
Review URL: https://chromiumcodereview.appspot.com/10909007
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@12492 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-09-12 16:43:57 +00:00
|
|
|
const v8::HeapGraphNode* own_transitions = GetProperty(
|
|
|
|
map, v8::HeapGraphEdge::kInternal, "transitions");
|
|
|
|
CHECK_EQ(NULL, own_transitions);
|
2012-08-16 14:25:55 +00:00
|
|
|
}
|
2013-03-13 17:38:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
TEST(ManyLocalsInSharedContext) {
|
|
|
|
LocalContext env;
|
2013-04-02 08:03:01 +00:00
|
|
|
v8::HandleScope scope(env->GetIsolate());
|
|
|
|
v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
|
2013-03-14 14:42:00 +00:00
|
|
|
int num_objects = 6000;
|
2013-03-13 17:38:04 +00:00
|
|
|
CompileRun(
|
2013-03-14 14:42:00 +00:00
|
|
|
"var n = 6000;"
|
2013-03-13 17:38:04 +00:00
|
|
|
"var result = [];"
|
|
|
|
"result.push('(function outer() {');"
|
|
|
|
"for (var i = 0; i < n; i++) {"
|
|
|
|
" var f = 'function f_' + i + '() { ';"
|
|
|
|
" if (i > 0)"
|
|
|
|
" f += 'f_' + (i - 1) + '();';"
|
|
|
|
" f += ' }';"
|
|
|
|
" result.push(f);"
|
|
|
|
"}"
|
|
|
|
"result.push('return f_' + (n - 1) + ';');"
|
|
|
|
"result.push('})()');"
|
|
|
|
"var ok = eval(result.join('\\n'));");
|
|
|
|
const v8::HeapSnapshot* snapshot =
|
2013-04-02 08:03:01 +00:00
|
|
|
heap_profiler->TakeHeapSnapshot(v8_str("snapshot"));
|
2013-03-13 17:38:04 +00:00
|
|
|
const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
|
|
|
|
CHECK_NE(NULL, global);
|
|
|
|
const v8::HeapGraphNode* ok_object =
|
|
|
|
GetProperty(global, v8::HeapGraphEdge::kProperty, "ok");
|
|
|
|
CHECK_NE(NULL, ok_object);
|
|
|
|
const v8::HeapGraphNode* context_object =
|
|
|
|
GetProperty(ok_object, v8::HeapGraphEdge::kInternal, "context");
|
|
|
|
CHECK_NE(NULL, context_object);
|
|
|
|
// Check the objects are not duplicated in the context.
|
|
|
|
CHECK_EQ(v8::internal::Context::MIN_CONTEXT_SLOTS + num_objects - 1,
|
|
|
|
context_object->GetChildrenCount());
|
|
|
|
// Check all the objects have got their names.
|
|
|
|
// ... well check just every 8th because otherwise it's too slow in debug.
|
|
|
|
for (int i = 0; i < num_objects - 1; i += 8) {
|
2013-03-13 18:35:30 +00:00
|
|
|
i::EmbeddedVector<char, 100> var_name;
|
|
|
|
i::OS::SNPrintF(var_name, "f_%d", i);
|
2013-03-13 17:38:04 +00:00
|
|
|
const v8::HeapGraphNode* f_object = GetProperty(
|
2013-03-13 18:35:30 +00:00
|
|
|
context_object, v8::HeapGraphEdge::kContextVariable, var_name.start());
|
2013-03-13 17:38:04 +00:00
|
|
|
CHECK_NE(NULL, f_object);
|
|
|
|
}
|
|
|
|
}
|