// The static field-name for the synthetic object generated to account for class static overhead. static constexpr constchar* kClassOverheadName = "$classOverhead";
// Android-specific tags. // Current time, based on CLOCK_MONOTONIC.
HPROF_TAG_ART_CLOCK_MONOTONIC = 0xA0,
}; // LINT.ThenChange(//depot/google3/art/tools/ahat/src/main/com/android/ahat/heapdump/Parser.java:hprof-tags)
// The ID for the synthetic object generated to account for class static overhead. void AddClassStaticsId(const mirror::Class* value) {
AddU4(1 | PointerToLowMemUInt32(value));
}
size_t length_; // Current record size.
size_t sum_length_; // Size of all data.
size_t max_length_; // Maximum seen length. bool started_; // Was StartRecord called?
};
// This keeps things buffered until flushed. class EndianOutputBuffered : public EndianOutput { public: explicit EndianOutputBuffered(size_t reserve_size) {
buffer_.reserve(reserve_size);
} virtual ~EndianOutputBuffered() {}
void HandleU1AsU2List(const uint8_t* values, size_t count) override {
DCHECK_EQ(length_, buffer_.size()); // All 8-bits are grouped in 2 to make 16-bit block like Java Char if (count & 1) {
buffer_.push_back(0);
} for (size_t i = 0; i < count; ++i) {
uint8_t value = *values;
buffer_.push_back(value);
values++;
}
}
void HandleU2List(const uint16_t* values, size_t count) override {
DCHECK_EQ(length_, buffer_.size()); for (size_t i = 0; i < count; ++i) {
uint16_t value = *values;
buffer_.push_back(static_cast<uint8_t>((value >> 8) & 0xFF));
buffer_.push_back(static_cast<uint8_t>((value >> 0) & 0xFF));
values++;
}
}
void ProcessBody() REQUIRES(Locks::mutator_lock_) {
Runtime* const runtime = Runtime::Current(); // Walk the roots and the heap.
output_->StartNewRecord(HPROF_TAG_HEAP_DUMP_SEGMENT, kHprofTime);
void ProcessHeader(bool string_first) REQUIRES(Locks::mutator_lock_) { // Write the header.
WriteFixedHeader(); // Write the string and class tables, and any stack traces, to the header. // (jhat requires that these appear before any of the data in the body that refers to them.) // jhat also requires the string table appear before class table and stack traces. // However, WriteStackTraces() can modify the string table, so it's necessary to call // WriteStringTable() last in the first pass, to compute the correct length of the output. if (string_first) {
WriteStringTable();
}
WriteClassTable();
WriteStackTraces(); if (!string_first) {
WriteStringTable();
}
output_->EndRecord();
}
void WriteClassTable() REQUIRES_SHARED(Locks::mutator_lock_) { for (constauto& p : classes_) {
mirror::Class* c = p.first;
HprofClassSerialNumber sn = p.second;
CHECK(c != nullptr);
output_->StartNewRecord(HPROF_TAG_LOAD_CLASS, kHprofTime); // LOAD CLASS format: // U4: class serial number (always > 0) // ID: class object ID. We use the address of the class object structure as its ID. // U4: stack trace serial number // ID: class name string ID
__ AddU4(sn);
__ AddObjectId(c);
__ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(c));
__ AddStringId(LookupClassNameId(c));
}
}
void WriteStringTable() { for (constauto& p : strings_) { const std::string& string = p.first; const HprofStringId id = p.second;
// STRING format: // ID: ID for this string // U1*: UTF8 characters for string (NOT null terminated) // (the record format encodes the length)
__ AddU4(id);
__ AddUtf8String(string.c_str());
}
}
void StartNewHeapDumpSegment() { // This flushes the old segment and starts a new one.
output_->StartNewRecord(HPROF_TAG_HEAP_DUMP_SEGMENT, kHprofTime);
objects_in_segment_ = 0; // Starting a new HEAP_DUMP resets the heap to default.
current_heap_ = HPROF_HEAP_DEFAULT;
}
HprofClassObjectId LookupClassId(mirror::Class* c) REQUIRES_SHARED(Locks::mutator_lock_) { if (c != nullptr) { auto it = classes_.find(c); if (it == classes_.end()) { // first time to see this class
HprofClassSerialNumber sn = next_class_serial_number_++;
classes_.Put(c, sn); // Make sure that we've assigned a string ID for this class' name
LookupClassNameId(c);
}
} return PointerToLowMemUInt32(c);
}
HprofStackTraceSerialNumber LookupStackTraceSerialNumber(const mirror::Object* obj)
REQUIRES_SHARED(Locks::mutator_lock_) { auto r = allocation_records_.find(obj); if (r == allocation_records_.end()) { return kHprofNullStackTrace;
} else { const gc::AllocRecordStackTrace* trace = r->second; auto result = traces_.find(trace);
CHECK(result != traces_.end()); return result->second;
}
}
// U4: size of identifiers. We're using addresses as IDs and our heap references are stored // as uint32_t. // Note of warning: hprof-conv hard-codes the size of identifiers to 4.
static_assert(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(uint32_t), "Unexpected HeapReference size");
__ AddU4(sizeof(uint32_t));
// The current time, in milliseconds since 0:00 GMT, 1/1/70.
timeval now; const uint64_t nowMs = (gettimeofday(&now, nullptr) < 0) ? 0 :
(uint64_t)now.tv_sec * 1000 + now.tv_usec / 1000; // TODO: It seems it would be correct to use U8. // U4: high word of the 64-bit time.
__ AddU4(static_cast<uint32_t>(nowMs >> 32)); // U4: low word of the 64-bit time.
__ AddU4(static_cast<uint32_t>(nowMs & 0xFFFFFFFF));
void WriteStackTraces() REQUIRES_SHARED(Locks::mutator_lock_) { // Write a fake stack trace record so the analysis tools don't freak out.
output_->StartNewRecord(HPROF_TAG_STACK_TRACE, kHprofTime);
__ AddStackTraceSerialNumber(kHprofNullStackTrace);
__ AddU4(kHprofNullThread);
__ AddU4(0); // no frames
// TODO: jhat complains "WARNING: Stack trace not found for serial # -1", but no trace should // have -1 as its serial number (as long as HprofStackTraceSerialNumber doesn't overflow). for (constauto& it : traces_) { const gc::AllocRecordStackTrace* trace = it.first;
HprofStackTraceSerialNumber trace_sn = it.second;
size_t depth = trace->GetDepth();
// First write stack frames of the trace for (size_t i = 0; i < depth; ++i) { const gc::AllocRecordStackTraceElement* frame = &trace->GetStackElement(i);
ArtMethod* method = frame->GetMethod();
CHECK(method != nullptr);
output_->StartNewRecord(HPROF_TAG_STACK_FRAME, kHprofTime); // STACK FRAME format: // ID: stack frame ID. We use the address of the AllocRecordStackTraceElement object as its ID. // ID: method name string ID // ID: method signature string ID // ID: source file name string ID // U4: class serial number // U4: >0, line number; 0, no line information available; -1, unknown location auto frame_result = frames_.find(frame);
CHECK(frame_result != frames_.end());
__ AddU4(frame_result->second);
__ AddStringId(LookupStringId(method->GetName()));
__ AddStringId(LookupStringId(method->GetSignature().ToString())); constchar* source_file = method->GetDeclaringClassSourceFile(); if (source_file == nullptr) {
source_file = "";
}
__ AddStringId(LookupStringId(source_file)); auto class_result = classes_.find(method->GetDeclaringClass().Ptr());
CHECK(class_result != classes_.end());
__ AddU4(class_result->second);
__ AddU4(frame->ComputeLineNumber());
}
// Then write the trace itself
output_->StartNewRecord(HPROF_TAG_STACK_TRACE, kHprofTime); // STACK TRACE format: // U4: stack trace serial number. We use the address of the AllocRecordStackTrace object as its serial number. // U4: thread serial number. We use Thread::GetTid(). // U4: number of frames // [ID]*: series of stack frame ID's
__ AddStackTraceSerialNumber(trace_sn);
__ AddU4(trace->GetTid());
__ AddU4(depth); for (size_t i = 0; i < depth; ++i) { const gc::AllocRecordStackTraceElement* frame = &trace->GetStackElement(i); auto frame_result = frames_.find(frame);
CHECK(frame_result != frames_.end());
__ AddU4(frame_result->second);
}
}
}
if (okay) { // Check for expected size. Output is expected to be less-or-equal than first phase, see // b/23521263.
DCHECK_LE(file_output.SumLength(), overall_size);
}
output_ = nullptr;
}
for (auto it = records->Begin(), end = records->End(); it != end; ++it) { const mirror::Object* obj = it->first.Read(); if (obj == nullptr) { continue;
}
++count; const gc::AllocRecordStackTrace* trace = it->second.GetStackTrace();
// Copy the pair into a real hash map to speed up look up. auto records_result = allocation_records_.emplace(obj, trace); // The insertion should always succeed, i.e. no duplicate object pointers in "records"
CHECK(records_result.second);
// Generate serial numbers for traces, and IDs for frames. auto traces_result = traces_.find(trace); if (traces_result == traces_.end()) {
traces_.emplace(trace, next_trace_sn++); // only check frames if the trace is newly discovered for (size_t i = 0, depth = trace->GetDepth(); i < depth; ++i) { const gc::AllocRecordStackTraceElement* frame = &trace->GetStackElement(i); auto frames_result = frames_.find(frame); if (frames_result == frames_.end()) {
frames_.emplace(frame, next_frame_id++);
}
}
}
}
CHECK_EQ(traces_.size(), next_trace_sn - kHprofNullStackTrace - 1);
CHECK_EQ(frames_.size(), next_frame_id);
total_objects_with_stack_trace_ = count;
}
// If direct_to_ddms_ is set, "filename_" and "fd" will be ignored. // Otherwise, "filename_" must be valid, though if "fd" >= 0 it will // only be used for debug messages.
std::string filename_; int fd_; bool direct_to_ddms_;
uint64_t start_ns_ = NanoTime();
EndianOutput* output_ = nullptr;
HprofHeapId current_heap_ = HPROF_HEAP_DEFAULT; // Which heap we're currently dumping.
size_t objects_in_segment_ = 0;
// Set used to keep track of what simple root records we have already // emitted, to avoid emitting duplicate entries. The simple root records are // those that contain no other information than the root type and the object // id. A pair of root type and object id is packed into a uint64_t, with // the root type in the upper 32 bits and the object id in the lower 32 // bits.
std::unordered_set<uint64_t> simple_roots_;
// To make sure we don't dump the same object multiple times. b/34967844
std::unordered_set<mirror::Object*> visited_objects_;
switch (c) { case'[': case'L':
ret = hprof_basic_object;
size = 4; break; case'Z':
ret = hprof_basic_boolean;
size = 1; break; case'C':
ret = hprof_basic_char;
size = 2; break; case'F':
ret = hprof_basic_float;
size = 4; break; case'D':
ret = hprof_basic_double;
size = 8; break; case'B':
ret = hprof_basic_byte;
size = 1; break; case'S':
ret = hprof_basic_short;
size = 2; break; case'I':
ret = hprof_basic_int;
size = 4; break; case'J':
ret = hprof_basic_long;
size = 8; break; default:
LOG(FATAL) << "UNREACHABLE";
UNREACHABLE();
}
if (size_out != nullptr) {
*size_out = size;
}
return ret;
}
// Always called when marking objects, but only does // something when ctx->gc_scan_state_ is non-zero, which is usually // only true when marking the root set or unreachable // objects. Used to add rootset references to obj. void Hprof::MarkRootObject(const mirror::Object* obj, jobject jni_obj, HprofHeapTag heap_tag,
uint32_t thread_serial) { if (heap_tag == 0) { return;
}
CheckHeapSegmentConstraints();
switch (heap_tag) { // ID: object ID case HPROF_ROOT_UNKNOWN: case HPROF_ROOT_STICKY_CLASS: case HPROF_ROOT_MONITOR_USED: case HPROF_ROOT_INTERNED_STRING: case HPROF_ROOT_DEBUGGER: case HPROF_ROOT_VM_INTERNAL: {
uint64_t key = (static_cast<uint64_t>(heap_tag) << 32) | PointerToLowMemUInt32(obj); if (simple_roots_.insert(key).second) {
__ AddU1(heap_tag);
__ AddObjectId(obj);
} break;
}
// ID: object ID // ID: JNI global ref ID case HPROF_ROOT_JNI_GLOBAL:
__ AddU1(heap_tag);
__ AddObjectId(obj);
__ AddJniGlobalRefId(jni_obj); break;
// ID: object ID // U4: thread serial number // U4: frame number in stack trace (-1 for empty) case HPROF_ROOT_JNI_LOCAL: case HPROF_ROOT_JNI_MONITOR: case HPROF_ROOT_JAVA_FRAME:
__ AddU1(heap_tag);
__ AddObjectId(obj);
__ AddU4(thread_serial);
__ AddU4((uint32_t)-1); break;
// ID: object ID // U4: thread serial number case HPROF_ROOT_NATIVE_STACK: case HPROF_ROOT_THREAD_BLOCK:
__ AddU1(heap_tag);
__ AddObjectId(obj);
__ AddU4(thread_serial); break;
// ID: thread object ID // U4: thread serial number // U4: stack trace serial number case HPROF_ROOT_THREAD_OBJECT:
__ AddU1(heap_tag);
__ AddObjectId(obj);
__ AddU4(thread_serial);
__ AddU4((uint32_t)-1); // xxx break;
case HPROF_CLASS_DUMP: case HPROF_INSTANCE_DUMP: case HPROF_OBJECT_ARRAY_DUMP: case HPROF_PRIMITIVE_ARRAY_DUMP: case HPROF_HEAP_DUMP_INFO: case HPROF_PRIMITIVE_ARRAY_NODATA_DUMP: // Ignored. break;
case HPROF_ROOT_FINALIZING: case HPROF_ROOT_REFERENCE_CLEANUP: case HPROF_UNREACHABLE:
LOG(FATAL) << "obsolete tag " << static_cast<int>(heap_tag);
UNREACHABLE();
}
++objects_in_segment_;
}
bool Hprof::AddRuntimeInternalObjectsField(mirror::Class* klass) { if (klass->IsDexCacheClass()) { returntrue;
} // IsClassLoaderClass is true for subclasses of classloader but we only want to add the fake // field to the java.lang.ClassLoader class. if (klass->IsClassLoaderClass() && klass->GetSuperClass()->IsObjectClass()) { returntrue;
} returnfalse;
}
void Hprof::DumpHeapObject(mirror::Object* obj) { // Ignore classes that are retired. if (obj->IsClass() && obj->AsClass()->IsRetired()) { return;
}
DCHECK(visited_objects_.insert(obj).second)
<< "Already visited " << obj << "(" << obj->PrettyTypeOf() << ")";
// Note that these don't have read barriers. Its OK however since the GC is guaranteed to not be // running during the hprof dumping process. void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const
REQUIRES_SHARED(Locks::mutator_lock_) { if (!root->IsNull()) {
VisitRoot(root);
}
}
private: // These roots are actually live from the object. Avoid marking them as roots in hprof to make // it easier to debug class unloading. mutable std::set<mirror::Object*> roots_;
};
RootCollector visitor; // Collect all native roots. if (!obj->IsClass()) {
obj->VisitReferences(visitor, VoidFunctor());
}
gc::Heap* const heap = Runtime::Current()->GetHeap(); const gc::space::ContinuousSpace* const space = heap->FindContinuousSpaceFromObject(obj, true);
HprofHeapId heap_type = HPROF_HEAP_APP; if (space != nullptr) { if (space->IsZygoteSpace()) {
heap_type = HPROF_HEAP_ZYGOTE;
VisitRoot(obj, RootInfo(kRootVMInternal));
} elseif (space->IsImageSpace() && heap->ObjectIsInBootImageSpace(obj)) { // Only count objects in the boot image as HPROF_HEAP_IMAGE, this leaves app image objects as // HPROF_HEAP_APP. b/35762934
heap_type = HPROF_HEAP_IMAGE;
VisitRoot(obj, RootInfo(kRootVMInternal));
}
} elseif (heap->IsZygoteLargeObject(obj)) {
heap_type = HPROF_HEAP_ZYGOTE;
VisitRoot(obj, RootInfo(kRootVMInternal));
}
CheckHeapSegmentConstraints();
if (heap_type != current_heap_) {
HprofStringId nameId;
// This object is in a different heap than the current one. // Emit a HEAP_DUMP_INFO tag to change heaps.
__ AddU1(HPROF_HEAP_DUMP_INFO);
__ AddU4(static_cast<uint32_t>(heap_type)); // uint32_t: heap type switch (heap_type) { case HPROF_HEAP_APP:
nameId = LookupStringId("app"); break; case HPROF_HEAP_ZYGOTE:
nameId = LookupStringId("zygote"); break; case HPROF_HEAP_IMAGE:
nameId = LookupStringId("image"); break; default: // Internal error
LOG(ERROR) << "Unexpected desiredHeap";
nameId = LookupStringId("<ILLEGAL>"); break;
}
__ AddStringId(nameId);
current_heap_ = heap_type;
}
mirror::Class* c = obj->GetClass(); if (c == nullptr) { // This object will bother HprofReader, because it has a null // class, so just don't dump it. It could be // gDvm.unlinkedJavaLangClass or it could be an object just // allocated which hasn't been initialized yet.
} else { if (obj->IsClass()) {
DumpHeapClass(obj->AsClass().Ptr());
} elseif (c->IsArrayClass()) {
DumpHeapArray(obj->AsArray().Ptr(), c);
} else {
DumpHeapInstanceObject(obj, c, visitor.GetRoots());
}
}
++objects_in_segment_;
}
void Hprof::DumpHeapClass(mirror::Class* klass) { if (!klass->IsResolved()) { // Class is allocated but not yet resolved: we cannot access its fields or super class. return;
}
// Note: We will emit instance fields of Class as synthetic static fields with a prefix of // "$class$" so the class fields are visible in hprof dumps. For tools to account for that // correctly, we'll emit an instance size of zero for java.lang.Class, and also emit the // instance fields of java.lang.Object. // // For other overhead (currently only the embedded vtable), we will generate a synthetic // byte array (or field[s] in case the overhead size is of reference size or less).
// Total class size: // * class instance fields (including Object instance fields) // * vtable // * class static fields const size_t total_class_size = klass->GetClassSize();
// Base class size (common parts of all Class instances): // * class instance fields (including Object instance fields)
constexpr size_t base_class_size = sizeof(mirror::Class);
CHECK_LE(base_class_size, total_class_size);
// Difference of Total and Base: // * vtable // * class static fields const size_t base_overhead_size = total_class_size - base_class_size;
// Tools (ahat/Studio) will count the static fields and account for them in the class size. We // must thus subtract them from base_overhead_size or they will be double-counted.
size_t class_static_fields_size = 0; for (ArtField& class_field : klass->GetFields()) { if (class_field.IsStatic()) {
size_t size = 0;
SignatureToBasicTypeAndSize(class_field.GetTypeDescriptor(), &size);
class_static_fields_size += size;
}
}
CHECK_GE(base_overhead_size, class_static_fields_size); // Now we have: // * vtable const size_t base_no_statics_overhead_size = base_overhead_size - class_static_fields_size;
// We may decide to display native overhead (the actual IMT, ArtFields and ArtMethods) in the // future. const size_t java_heap_overhead_size = base_no_statics_overhead_size;
// For overhead greater 4, we'll allocate a synthetic array. if (java_heap_overhead_size > 4) { // Create a byte array to reflect the allocation of the // StaticField array at the end of this class.
__ AddU1(HPROF_PRIMITIVE_ARRAY_DUMP);
__ AddClassStaticsId(klass);
__ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(klass));
__ AddU4(java_heap_overhead_size - 4);
__ AddU1(hprof_basic_byte); for (size_t i = 0; i < java_heap_overhead_size - 4; ++i) {
__ AddU1(0);
}
} const size_t java_heap_overhead_field_count = java_heap_overhead_size > 0
? (java_heap_overhead_size == 3 ? 2u : 1u)
: 0;
__ AddU1(HPROF_CLASS_DUMP);
__ AddClassId(LookupClassId(klass));
__ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(klass));
__ AddClassId(LookupClassId(klass->GetSuperClass().Ptr()));
__ AddObjectId(klass->GetClassLoader().Ptr());
__ AddObjectId(nullptr); // no signer
__ AddObjectId(nullptr); // no prot domain
__ AddObjectId(nullptr); // reserved
__ AddObjectId(nullptr); // reserved // Instance size. if (klass->IsClassClass()) { // As mentioned above, we will emit instance fields as synthetic static fields. So the // base object is "empty."
__ AddU4(0);
} elseif (klass->IsStringClass()) { // Strings are variable length with character data at the end like arrays. // This outputs the size of an empty string.
__ AddU4(sizeof(mirror::String));
} elseif (klass->IsArrayClass()) { // Arrays are variable length with element data at the end of the header, // padded to alignment. The offset to the data is the header memory size, // after padding.
uint32_t component_size = klass->GetComponentSize();
uint32_t array_header_size = mirror::Array::DataOffset(component_size).Uint32Value();
__ AddU4(array_header_size);
} elseif (klass->IsPrimitive()) {
__ AddU4(0);
} else {
__ AddU4(klass->GetObjectSize()); // instance size
}
__ AddU2(0); // empty const pool
// Static fields // // Note: we report Class' and Object's instance fields here, too. This is for visibility reasons. // (b/38167721)
mirror::Class* class_class = klass->GetClass();
// Helper lambda to emit the given static field. The second argument name_fn will be called to // generate the name to emit. This can be used to emit something else than the field's actual // name. auto static_field_writer = [&](ArtField& field, auto name_fn)
REQUIRES_SHARED(Locks::mutator_lock_) {
__ AddStringId(LookupStringId(name_fn(field)));
size_t size;
HprofBasicType t = SignatureToBasicTypeAndSize(field.GetTypeDescriptor(), &size);
__ AddU1(t); switch (t) { case hprof_basic_byte:
__ AddU1(field.GetByte(klass)); return; case hprof_basic_boolean:
__ AddU1(field.GetBoolean(klass)); return; case hprof_basic_char:
__ AddU2(field.GetChar(klass)); return; case hprof_basic_short:
__ AddU2(field.GetShort(klass)); return; case hprof_basic_float: case hprof_basic_int: case hprof_basic_object:
__ AddU4(field.Get32(klass)); return; case hprof_basic_double: case hprof_basic_long:
__ AddU8(field.Get64(klass)); return;
}
LOG(FATAL) << "Unexpected size " << size;
UNREACHABLE();
};
{ auto class_instance_field_name_fn = [](ArtField& field) REQUIRES_SHARED(Locks::mutator_lock_) { return std::string("$class$") + field.GetName();
}; for (ArtField& class_field : class_class->GetFields()) { if (!class_field.IsStatic()) {
static_field_writer(class_field, class_instance_field_name_fn);
}
} for (ArtField& object_field : class_class->GetSuperClass()->GetFields()) { if (!object_field.IsStatic()) {
static_field_writer(object_field, class_instance_field_name_fn);
}
}
}
{ auto class_static_field_name_fn = [](ArtField& field) REQUIRES_SHARED(Locks::mutator_lock_) { return field.GetName();
}; for (ArtField& class_field : klass->GetFields()) { if (class_field.IsStatic()) {
static_field_writer(class_field, class_static_field_name_fn);
}
}
}
// Instance fields for this class (no superclass fields) int iFieldCount = klass->ComputeNumInstanceFields(); // add_internal_runtime_objects is only for classes that may retain objects live through means // other than fields. It is never the case for strings. constbool add_internal_runtime_objects = AddRuntimeInternalObjectsField(klass); if (klass->IsStringClass() || add_internal_runtime_objects) {
__ AddU2((uint16_t)iFieldCount + 1);
} else {
__ AddU2((uint16_t)iFieldCount);
} for (uint32_t i = 0; i < klass->NumFields(); ++i) {
ArtField* f = klass->GetField(i); if (f->IsStatic()) { continue;
}
__ AddStringId(LookupStringId(f->GetName()));
HprofBasicType t = SignatureToBasicTypeAndSize(f->GetTypeDescriptor(), nullptr);
__ AddU1(t);
} // Add native value character array for strings / byte array for compressed strings. if (klass->IsStringClass()) {
__ AddStringId(LookupStringId("value"));
__ AddU1(hprof_basic_object);
} elseif (add_internal_runtime_objects) {
__ AddStringId(LookupStringId("runtimeInternalObjects"));
__ AddU1(hprof_basic_object);
}
}
// Dump the elements, which are always objects or null.
__ AddIdList(obj->AsObjectArray<mirror::Object>().Ptr());
} else {
size_t size;
HprofBasicType t = SignatureToBasicTypeAndSize(
Primitive::Descriptor(klass->GetComponentType()->GetPrimitiveType()), &size);
// obj is a primitive array.
__ AddU1(HPROF_PRIMITIVE_ARRAY_DUMP);
// Reserve some space for the length of the instance data, which we won't // know until we're done writing it.
size_t size_patch_offset = output_->Length();
__ AddU4(0x77777777);
// What we will use for the string value if the object is a string.
mirror::Object* string_value = nullptr;
mirror::Object* fake_object_array = nullptr;
// Write the instance data; fields for this class, followed by super class fields, and so on. do { for (size_t i = 0; i < klass->NumFields(); ++i) {
ArtField* f = klass->GetField(i); if (f->IsStatic()) { continue;
}
size_t size;
HprofBasicType t = SignatureToBasicTypeAndSize(f->GetTypeDescriptor(), &size); switch (t) { case hprof_basic_byte:
__ AddU1(f->GetByte(obj)); break; case hprof_basic_boolean:
__ AddU1(f->GetBoolean(obj)); break; case hprof_basic_char:
__ AddU2(f->GetChar(obj)); break; case hprof_basic_short:
__ AddU2(f->GetShort(obj)); break; case hprof_basic_int: if (mirror::kUseStringCompression &&
klass->IsStringClass() &&
f->GetOffset().SizeValue() == mirror::String::CountOffset().SizeValue()) { // Store the string length instead of the raw count field with compression flag.
__ AddU4(obj->AsString()->GetLength()); break;
}
FALLTHROUGH_INTENDED; case hprof_basic_float: case hprof_basic_object:
__ AddU4(f->Get32(obj)); break; case hprof_basic_double: case hprof_basic_long:
__ AddU8(f->Get64(obj)); break;
}
} // Add value field for String if necessary. if (klass->IsStringClass()) {
ObjPtr<mirror::String> s = obj->AsString(); if (s->GetLength() == 0) { // If string is empty, use an object-aligned address within the string for the value.
string_value = reinterpret_cast<mirror::Object*>( reinterpret_cast<uintptr_t>(s.Ptr()) + kObjectAlignment);
} else { if (s->IsCompressed()) {
string_value = reinterpret_cast<mirror::Object*>(s->GetValueCompressed());
} else {
string_value = reinterpret_cast<mirror::Object*>(s->GetValue());
}
}
__ AddObjectId(string_value);
} elseif (AddRuntimeInternalObjectsField(klass)) { // We need an id that is guaranteed to not be used, use 1/2 of the object alignment.
fake_object_array = reinterpret_cast<mirror::Object*>( reinterpret_cast<uintptr_t>(obj) + kObjectAlignment / 2);
__ AddObjectId(fake_object_array);
}
klass = klass->GetSuperClass().Ptr();
} while (klass != nullptr);
// Patch the instance field length.
__ UpdateU4(size_patch_offset, output_->Length() - (size_patch_offset + 4));
// Output native value character array for strings.
CHECK_EQ(obj->IsString(), string_value != nullptr); if (string_value != nullptr) {
ObjPtr<mirror::String> s = obj->AsString();
__ AddU1(HPROF_PRIMITIVE_ARRAY_DUMP);
__ AddObjectId(string_value);
__ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(obj));
__ AddU4(s->GetLength()); if (s->IsCompressed()) {
__ AddU1(hprof_basic_byte);
__ AddU1List(s->GetValueCompressed(), s->GetLength());
} else {
__ AddU1(hprof_basic_char);
__ AddU2List(s->GetValue(), s->GetLength());
}
} elseif (fake_object_array != nullptr) {
DumpFakeObjectArray(fake_object_array, fake_roots);
}
}
// If "direct_to_ddms" is true, the other arguments are ignored, and data is // sent directly to DDMS. // If "fd" is >= 0, the output will be written to that file descriptor. // Otherwise, "filename" is used to create an output file. void DumpHeap(constchar* filename, int fd, bool direct_to_ddms) {
CHECK(filename != nullptr);
Thread* self = Thread::Current(); // Need to take a heap dump while GC isn't running. See the comment in Heap::VisitObjects(). // Also we need the critical section to avoid visiting the same object twice. See b/34967844
gc::ScopedGCCriticalSection gcs(self,
gc::kGcCauseHprof,
gc::kCollectorTypeHprof);
ScopedSuspendAll ssa(__FUNCTION__, true/* long suspend */);
Hprof hprof(filename, fd, direct_to_ddms);
hprof.Dump();
}
} // namespace hprof
} // namespace art
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.26 Sekunden
(vorverarbeitet am 2026-06-29)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.