2010-12-15 08:07:27 +00:00
|
|
|
// Copyright 2009-2010 the V8 project authors. All rights reserved.
|
2009-09-16 13:41:24 +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.
|
|
|
|
|
|
|
|
#ifndef V8_HEAP_PROFILER_H_
|
|
|
|
#define V8_HEAP_PROFILER_H_
|
|
|
|
|
2010-06-15 11:44:07 +00:00
|
|
|
#include "zone-inl.h"
|
2010-03-09 06:38:33 +00:00
|
|
|
|
2009-09-16 13:41:24 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
#ifdef ENABLE_LOGGING_AND_PROFILING
|
|
|
|
|
2010-06-15 11:44:07 +00:00
|
|
|
class HeapSnapshot;
|
|
|
|
class HeapSnapshotsCollection;
|
|
|
|
|
2010-07-15 13:21:50 +00:00
|
|
|
#define HEAP_PROFILE(Call) \
|
|
|
|
do { \
|
|
|
|
if (v8::internal::HeapProfiler::is_profiling()) { \
|
|
|
|
v8::internal::HeapProfiler::Call; \
|
|
|
|
} \
|
|
|
|
} while (false)
|
|
|
|
#else
|
|
|
|
#define HEAP_PROFILE(Call) ((void) 0)
|
|
|
|
#endif // ENABLE_LOGGING_AND_PROFILING
|
2010-06-16 19:53:24 +00:00
|
|
|
|
2009-09-16 13:41:24 +00:00
|
|
|
// The HeapProfiler writes data to the log files, which can be postprocessed
|
|
|
|
// to generate .hp files for use by the GHC/Valgrind tool hp2ps.
|
|
|
|
class HeapProfiler {
|
|
|
|
public:
|
2010-06-15 11:44:07 +00:00
|
|
|
static void Setup();
|
|
|
|
static void TearDown();
|
2010-06-16 19:53:24 +00:00
|
|
|
|
|
|
|
#ifdef ENABLE_LOGGING_AND_PROFILING
|
2010-12-13 10:42:06 +00:00
|
|
|
static HeapSnapshot* TakeSnapshot(const char* name,
|
|
|
|
int type,
|
|
|
|
v8::ActivityControl* control);
|
|
|
|
static HeapSnapshot* TakeSnapshot(String* name,
|
|
|
|
int type,
|
|
|
|
v8::ActivityControl* control);
|
2010-06-15 11:44:07 +00:00
|
|
|
static int GetSnapshotsCount();
|
|
|
|
static HeapSnapshot* GetSnapshot(int index);
|
|
|
|
static HeapSnapshot* FindSnapshot(unsigned uid);
|
|
|
|
|
2010-07-15 13:21:50 +00:00
|
|
|
static void ObjectMoveEvent(Address from, Address to);
|
|
|
|
|
|
|
|
static INLINE(bool is_profiling()) {
|
|
|
|
return singleton_ != NULL && singleton_->snapshots_->is_tracking_objects();
|
|
|
|
}
|
|
|
|
|
2010-06-15 11:44:07 +00:00
|
|
|
// Obsolete interface.
|
2009-09-16 13:41:24 +00:00
|
|
|
// Write a single heap sample to the log file.
|
|
|
|
static void WriteSample();
|
|
|
|
|
|
|
|
private:
|
2010-06-15 11:44:07 +00:00
|
|
|
HeapProfiler();
|
|
|
|
~HeapProfiler();
|
2010-12-13 10:42:06 +00:00
|
|
|
HeapSnapshot* TakeSnapshotImpl(const char* name,
|
|
|
|
int type,
|
|
|
|
v8::ActivityControl* control);
|
|
|
|
HeapSnapshot* TakeSnapshotImpl(String* name,
|
|
|
|
int type,
|
|
|
|
v8::ActivityControl* control);
|
2010-06-15 11:44:07 +00:00
|
|
|
|
|
|
|
HeapSnapshotsCollection* snapshots_;
|
|
|
|
unsigned next_snapshot_uid_;
|
|
|
|
|
|
|
|
static HeapProfiler* singleton_;
|
2010-06-16 19:53:24 +00:00
|
|
|
#endif // ENABLE_LOGGING_AND_PROFILING
|
2009-09-16 13:41:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-06-16 19:53:24 +00:00
|
|
|
#ifdef ENABLE_LOGGING_AND_PROFILING
|
|
|
|
|
2009-09-16 13:41:24 +00:00
|
|
|
// JSObjectsCluster describes a group of JS objects that are
|
2009-09-18 12:05:18 +00:00
|
|
|
// considered equivalent in terms of a particular profile.
|
2009-09-16 13:41:24 +00:00
|
|
|
class JSObjectsCluster BASE_EMBEDDED {
|
|
|
|
public:
|
2009-09-18 12:05:18 +00:00
|
|
|
// These special cases are used in retainer profile.
|
2009-09-16 13:41:24 +00:00
|
|
|
enum SpecialCase {
|
|
|
|
ROOTS = 1,
|
2009-09-28 09:05:06 +00:00
|
|
|
GLOBAL_PROPERTY = 2,
|
2009-10-26 10:51:30 +00:00
|
|
|
CODE = 3,
|
|
|
|
SELF = 100 // This case is used in ClustersCoarser only.
|
2009-09-16 13:41:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
JSObjectsCluster() : constructor_(NULL), instance_(NULL) {}
|
|
|
|
explicit JSObjectsCluster(String* constructor)
|
|
|
|
: constructor_(constructor), instance_(NULL) {}
|
|
|
|
explicit JSObjectsCluster(SpecialCase special)
|
|
|
|
: constructor_(FromSpecialCase(special)), instance_(NULL) {}
|
|
|
|
JSObjectsCluster(String* constructor, Object* instance)
|
|
|
|
: constructor_(constructor), instance_(instance) {}
|
|
|
|
|
2009-09-18 12:05:18 +00:00
|
|
|
static int CompareConstructors(const JSObjectsCluster& a,
|
|
|
|
const JSObjectsCluster& b) {
|
2009-09-16 13:41:24 +00:00
|
|
|
// Strings are unique, so it is sufficient to compare their pointers.
|
|
|
|
return a.constructor_ == b.constructor_ ? 0
|
|
|
|
: (a.constructor_ < b.constructor_ ? -1 : 1);
|
|
|
|
}
|
|
|
|
static int Compare(const JSObjectsCluster& a, const JSObjectsCluster& b) {
|
|
|
|
// Strings are unique, so it is sufficient to compare their pointers.
|
|
|
|
const int cons_cmp = CompareConstructors(a, b);
|
|
|
|
return cons_cmp == 0 ?
|
|
|
|
(a.instance_ == b.instance_ ? 0 : (a.instance_ < b.instance_ ? -1 : 1))
|
|
|
|
: cons_cmp;
|
|
|
|
}
|
2009-09-28 09:05:06 +00:00
|
|
|
static int Compare(const JSObjectsCluster* a, const JSObjectsCluster* b) {
|
|
|
|
return Compare(*a, *b);
|
|
|
|
}
|
2009-09-16 13:41:24 +00:00
|
|
|
|
|
|
|
bool is_null() const { return constructor_ == NULL; }
|
|
|
|
bool can_be_coarsed() const { return instance_ != NULL; }
|
2009-09-18 12:05:18 +00:00
|
|
|
String* constructor() const { return constructor_; }
|
2010-08-18 08:19:29 +00:00
|
|
|
Object* instance() const { return instance_; }
|
2009-09-16 13:41:24 +00:00
|
|
|
|
2010-08-18 08:19:29 +00:00
|
|
|
const char* GetSpecialCaseName() const;
|
2009-09-16 13:41:24 +00:00
|
|
|
void Print(StringStream* accumulator) const;
|
|
|
|
// Allows null clusters to be printed.
|
|
|
|
void DebugPrint(StringStream* accumulator) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
static String* FromSpecialCase(SpecialCase special) {
|
|
|
|
// We use symbols that are illegal JS identifiers to identify special cases.
|
|
|
|
// Their actual value is irrelevant for us.
|
|
|
|
switch (special) {
|
|
|
|
case ROOTS: return Heap::result_symbol();
|
|
|
|
case GLOBAL_PROPERTY: return Heap::code_symbol();
|
2009-10-26 10:51:30 +00:00
|
|
|
case CODE: return Heap::arguments_shadow_symbol();
|
2009-09-28 09:05:06 +00:00
|
|
|
case SELF: return Heap::catch_var_symbol();
|
2009-09-16 13:41:24 +00:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String* constructor_;
|
|
|
|
Object* instance_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-09-18 12:05:18 +00:00
|
|
|
struct JSObjectsClusterTreeConfig {
|
|
|
|
typedef JSObjectsCluster Key;
|
|
|
|
typedef NumberAndSizeInfo Value;
|
|
|
|
static const Key kNoKey;
|
|
|
|
static const Value kNoValue;
|
|
|
|
static int Compare(const Key& a, const Key& b) {
|
|
|
|
return Key::Compare(a, b);
|
|
|
|
}
|
|
|
|
};
|
2009-09-16 13:41:24 +00:00
|
|
|
typedef ZoneSplayTree<JSObjectsClusterTreeConfig> JSObjectsClusterTree;
|
|
|
|
|
2009-09-18 12:05:18 +00:00
|
|
|
|
|
|
|
// ConstructorHeapProfile is responsible for gathering and logging
|
|
|
|
// "constructor profile" of JS objects allocated on heap.
|
|
|
|
// It is run during garbage collection cycle, thus it doesn't need
|
|
|
|
// to use handles.
|
|
|
|
class ConstructorHeapProfile BASE_EMBEDDED {
|
|
|
|
public:
|
|
|
|
ConstructorHeapProfile();
|
|
|
|
virtual ~ConstructorHeapProfile() {}
|
|
|
|
void CollectStats(HeapObject* obj);
|
|
|
|
void PrintStats();
|
2010-08-18 08:19:29 +00:00
|
|
|
|
|
|
|
template<class Callback>
|
|
|
|
void ForEach(Callback* callback) { js_objects_info_tree_.ForEach(callback); }
|
2009-09-18 12:05:18 +00:00
|
|
|
// Used by ZoneSplayTree::ForEach. Made virtual to allow overriding in tests.
|
|
|
|
virtual void Call(const JSObjectsCluster& cluster,
|
|
|
|
const NumberAndSizeInfo& number_and_size);
|
|
|
|
|
|
|
|
private:
|
|
|
|
ZoneScope zscope_;
|
|
|
|
JSObjectsClusterTree js_objects_info_tree_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// JSObjectsRetainerTree is used to represent retainer graphs using
|
|
|
|
// adjacency list form:
|
|
|
|
//
|
|
|
|
// Cluster -> (Cluster -> NumberAndSizeInfo)
|
|
|
|
//
|
|
|
|
// Subordinate splay trees are stored by pointer. They are zone-allocated,
|
|
|
|
// so it isn't needed to manage their lifetime.
|
|
|
|
//
|
|
|
|
struct JSObjectsRetainerTreeConfig {
|
2009-09-16 13:41:24 +00:00
|
|
|
typedef JSObjectsCluster Key;
|
|
|
|
typedef JSObjectsClusterTree* Value;
|
|
|
|
static const Key kNoKey;
|
|
|
|
static const Value kNoValue;
|
|
|
|
static int Compare(const Key& a, const Key& b) {
|
|
|
|
return Key::Compare(a, b);
|
|
|
|
}
|
|
|
|
};
|
2009-09-18 12:05:18 +00:00
|
|
|
typedef ZoneSplayTree<JSObjectsRetainerTreeConfig> JSObjectsRetainerTree;
|
2009-09-16 13:41:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ClustersCoarser BASE_EMBEDDED {
|
|
|
|
public:
|
|
|
|
ClustersCoarser();
|
|
|
|
|
|
|
|
// Processes a given retainer graph.
|
2009-09-18 12:05:18 +00:00
|
|
|
void Process(JSObjectsRetainerTree* tree);
|
2009-09-16 13:41:24 +00:00
|
|
|
|
|
|
|
// Returns an equivalent cluster (can be the cluster itself).
|
|
|
|
// If the given cluster doesn't have an equivalent, returns null cluster.
|
|
|
|
JSObjectsCluster GetCoarseEquivalent(const JSObjectsCluster& cluster);
|
|
|
|
// Returns whether a cluster can be substitued with an equivalent and thus,
|
|
|
|
// skipped in some cases.
|
|
|
|
bool HasAnEquivalent(const JSObjectsCluster& cluster);
|
|
|
|
|
2009-09-18 12:05:18 +00:00
|
|
|
// Used by JSObjectsRetainerTree::ForEach.
|
2009-09-16 13:41:24 +00:00
|
|
|
void Call(const JSObjectsCluster& cluster, JSObjectsClusterTree* tree);
|
2009-09-18 12:05:18 +00:00
|
|
|
void Call(const JSObjectsCluster& cluster,
|
|
|
|
const NumberAndSizeInfo& number_and_size);
|
2009-09-16 13:41:24 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Stores a list of back references for a cluster.
|
|
|
|
struct ClusterBackRefs {
|
|
|
|
explicit ClusterBackRefs(const JSObjectsCluster& cluster_);
|
|
|
|
ClusterBackRefs(const ClusterBackRefs& src);
|
|
|
|
ClusterBackRefs& operator=(const ClusterBackRefs& src);
|
|
|
|
|
|
|
|
static int Compare(const ClusterBackRefs& a, const ClusterBackRefs& b);
|
2009-09-28 09:05:06 +00:00
|
|
|
void SortRefs() { refs.Sort(JSObjectsCluster::Compare); }
|
|
|
|
static void SortRefsIterator(ClusterBackRefs* ref) { ref->SortRefs(); }
|
2009-09-16 13:41:24 +00:00
|
|
|
|
|
|
|
JSObjectsCluster cluster;
|
|
|
|
ZoneList<JSObjectsCluster> refs;
|
|
|
|
};
|
|
|
|
typedef ZoneList<ClusterBackRefs> SimilarityList;
|
|
|
|
|
|
|
|
// A tree for storing a list of equivalents for a cluster.
|
|
|
|
struct ClusterEqualityConfig {
|
|
|
|
typedef JSObjectsCluster Key;
|
|
|
|
typedef JSObjectsCluster Value;
|
|
|
|
static const Key kNoKey;
|
|
|
|
static const Value kNoValue;
|
|
|
|
static int Compare(const Key& a, const Key& b) {
|
|
|
|
return Key::Compare(a, b);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
typedef ZoneSplayTree<ClusterEqualityConfig> EqualityTree;
|
|
|
|
|
2009-09-18 12:05:18 +00:00
|
|
|
static int ClusterBackRefsCmp(const ClusterBackRefs* a,
|
|
|
|
const ClusterBackRefs* b) {
|
2009-09-16 13:41:24 +00:00
|
|
|
return ClusterBackRefs::Compare(*a, *b);
|
|
|
|
}
|
2009-09-18 12:05:18 +00:00
|
|
|
int DoProcess(JSObjectsRetainerTree* tree);
|
2009-09-16 13:41:24 +00:00
|
|
|
int FillEqualityTree();
|
|
|
|
|
2009-09-16 14:54:59 +00:00
|
|
|
static const int kInitialBackrefsListCapacity = 2;
|
|
|
|
static const int kInitialSimilarityListCapacity = 2000;
|
2009-09-16 13:41:24 +00:00
|
|
|
// Number of passes for finding equivalents. Limits the length of paths
|
|
|
|
// that can be considered equivalent.
|
2009-09-16 14:54:59 +00:00
|
|
|
static const int kMaxPassesCount = 10;
|
2009-09-16 13:41:24 +00:00
|
|
|
|
|
|
|
ZoneScope zscope_;
|
2009-09-16 19:44:04 +00:00
|
|
|
SimilarityList sim_list_;
|
|
|
|
EqualityTree eq_tree_;
|
|
|
|
ClusterBackRefs* current_pair_;
|
2009-09-18 12:05:18 +00:00
|
|
|
JSObjectsRetainerTree* current_set_;
|
2009-09-28 09:05:06 +00:00
|
|
|
const JSObjectsCluster* self_;
|
2009-09-16 13:41:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// RetainerHeapProfile is responsible for gathering and logging
|
|
|
|
// "retainer profile" of JS objects allocated on heap.
|
|
|
|
// It is run during garbage collection cycle, thus it doesn't need
|
|
|
|
// to use handles.
|
2010-08-18 08:19:29 +00:00
|
|
|
class RetainerTreeAggregator;
|
|
|
|
|
2009-09-16 13:41:24 +00:00
|
|
|
class RetainerHeapProfile BASE_EMBEDDED {
|
|
|
|
public:
|
|
|
|
class Printer {
|
|
|
|
public:
|
|
|
|
virtual ~Printer() {}
|
2009-09-18 12:05:18 +00:00
|
|
|
virtual void PrintRetainers(const JSObjectsCluster& cluster,
|
|
|
|
const StringStream& retainers) = 0;
|
2009-09-16 13:41:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
RetainerHeapProfile();
|
2010-08-18 08:19:29 +00:00
|
|
|
~RetainerHeapProfile();
|
|
|
|
|
|
|
|
RetainerTreeAggregator* aggregator() { return aggregator_; }
|
|
|
|
ClustersCoarser* coarser() { return &coarser_; }
|
|
|
|
JSObjectsRetainerTree* retainers_tree() { return &retainers_tree_; }
|
|
|
|
|
2009-09-16 13:41:24 +00:00
|
|
|
void CollectStats(HeapObject* obj);
|
2010-08-18 08:19:29 +00:00
|
|
|
void CoarseAndAggregate();
|
2009-09-16 13:41:24 +00:00
|
|
|
void PrintStats();
|
|
|
|
void DebugPrintStats(Printer* printer);
|
2009-09-18 12:05:18 +00:00
|
|
|
void StoreReference(const JSObjectsCluster& cluster, HeapObject* ref);
|
2009-09-16 13:41:24 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
ZoneScope zscope_;
|
2009-09-18 12:05:18 +00:00
|
|
|
JSObjectsRetainerTree retainers_tree_;
|
2009-09-16 13:41:24 +00:00
|
|
|
ClustersCoarser coarser_;
|
2010-08-18 08:19:29 +00:00
|
|
|
RetainerTreeAggregator* aggregator_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class AggregatedHeapSnapshot {
|
|
|
|
public:
|
|
|
|
AggregatedHeapSnapshot();
|
|
|
|
~AggregatedHeapSnapshot();
|
|
|
|
|
|
|
|
HistogramInfo* info() { return info_; }
|
|
|
|
ConstructorHeapProfile* js_cons_profile() { return &js_cons_profile_; }
|
|
|
|
RetainerHeapProfile* js_retainer_profile() { return &js_retainer_profile_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
HistogramInfo* info_;
|
|
|
|
ConstructorHeapProfile js_cons_profile_;
|
|
|
|
RetainerHeapProfile js_retainer_profile_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class HeapEntriesMap;
|
|
|
|
class HeapSnapshot;
|
|
|
|
|
|
|
|
class AggregatedHeapSnapshotGenerator {
|
|
|
|
public:
|
|
|
|
explicit AggregatedHeapSnapshotGenerator(AggregatedHeapSnapshot* snapshot);
|
|
|
|
void GenerateSnapshot();
|
|
|
|
void FillHeapSnapshot(HeapSnapshot* snapshot);
|
|
|
|
|
|
|
|
static const int kAllStringsType = LAST_TYPE + 1;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void CalculateStringsStats();
|
|
|
|
void CollectStats(HeapObject* obj);
|
|
|
|
template<class Iterator>
|
|
|
|
void IterateRetainers(HeapEntriesMap* entries_map);
|
|
|
|
|
|
|
|
AggregatedHeapSnapshot* agg_snapshot_;
|
2009-09-16 13:41:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-10-15 07:50:23 +00:00
|
|
|
class ProducerHeapProfile : public AllStatic {
|
|
|
|
public:
|
|
|
|
static void Setup();
|
2009-12-17 09:16:44 +00:00
|
|
|
static void RecordJSObjectAllocation(Object* obj) {
|
|
|
|
if (FLAG_log_producers) DoRecordJSObjectAllocation(obj);
|
|
|
|
}
|
|
|
|
|
2009-10-15 07:50:23 +00:00
|
|
|
private:
|
2009-12-17 09:16:44 +00:00
|
|
|
static void DoRecordJSObjectAllocation(Object* obj);
|
2009-10-15 07:50:23 +00:00
|
|
|
static bool can_log_;
|
|
|
|
};
|
|
|
|
|
2009-09-16 13:41:24 +00:00
|
|
|
#endif // ENABLE_LOGGING_AND_PROFILING
|
|
|
|
|
|
|
|
} } // namespace v8::internal
|
|
|
|
|
|
|
|
#endif // V8_HEAP_PROFILER_H_
|