namespace art { namespace gc { namespace accounting { template <size_t kAlignment> class SpaceBitmap; using ContinuousSpaceBitmap = SpaceBitmap<kObjectAlignment>;
} // namespace accounting namespace space { class ImageSpace;
} // namespace space
} // namespace gc
namespace mirror { class ClassLoader;
} // namespace mirror
class ClassLoaderVisitor; class CompilerOptions; template<class T> class Handle; class ImTable; class ImtConflictTable; class JavaVMExt; class TimingLogger;
namespace linker {
// Write a Space built during compilation for use during execution. class ImageWriter final { public:
ImageWriter(const CompilerOptions& compiler_options,
uintptr_t image_begin,
ImageHeader::StorageMode image_storage_mode, const std::vector<std::string>& oat_filenames, const HashMap<const DexFile*, size_t>& dex_file_oat_index_map,
jobject class_loader, const std::vector<std::string>* dirty_image_objects);
~ImageWriter();
// If image_fd is not File::kInvalidFd, then we use that for the image file. Otherwise we open // the names in image_filenames. // If oat_fd is not File::kInvalidFd, then we use that for the oat file. Otherwise we open // the names in oat_filenames. bool Write(int image_fd, const std::vector<std::string>& image_filenames,
size_t component_count)
REQUIRES(!Locks::mutator_lock_);
// Get the index of the oat file containing the dex file. // // This "oat_index" is used to retrieve information about the the memory layout // of the oat file and its associated image file, needed for link-time patching // of references to the image or across oat files.
size_t GetOatIndexForDexFile(const DexFile* dex_file) const;
// Get the index of the oat file containing the definition of the class.
size_t GetOatIndexForClass(ObjPtr<mirror::Class> klass) const
REQUIRES_SHARED(Locks::mutator_lock_);
// Update the oat layout for the given oat file. // This will make the oat_offset for the next oat file valid. void UpdateOatFileLayout(size_t oat_index,
size_t oat_loaded_size,
size_t oat_data_offset,
size_t oat_data_size); // Update information about the oat header, i.e. checksum and trampoline offsets. void UpdateOatFileHeader(size_t oat_index, const OatHeader& oat_header);
private: bool AllocMemory();
// Mark the objects defined in this space in the given live bitmap. void RecordImageAllocations() REQUIRES_SHARED(Locks::mutator_lock_);
// Classify different kinds of bins that objects end up getting packed into during image writing. // Ordered from dirtiest to cleanest (until ArtMethods). enumclass Bin {
kKnownDirty, // Known dirty objects from --dirty-image-objects list
kMiscDirty, // Dex caches, object locks, etc...
kClassVerified, // Class verified, but initializers haven't been run // Unknown mix of clean/dirty:
kRegular,
kClassInitialized, // Class initializers have been run // All classes get their own bins since their fields often dirty
kClassInitializedFinalStatics, // Class initializers have been run, no non-final statics // Likely-clean:
kString, // [String] Almost always immutable (except for obj header). // Definitely clean:
kInternalClean, // ART internal: image roots, boot image live objects, vtables // and interface tables, Object[]/int[]/long[]. // Add more bins here if we add more segregation code. // Non mirror fields must be below. // ArtFields should be always clean.
kArtField, // If the class is initialized, then the ArtMethods are probably clean.
kArtMethodClean, // ArtMethods may be dirty if the class has native methods or a declaring class that isn't // initialized.
kArtMethodDirty, // IMT (clean)
kImTable, // Conflict tables (clean).
kIMTConflictTable, // Runtime methods (always clean, do not have a length prefix array).
kRuntimeMethod, // Methods with unique JNI stubs.
kJniStubMethod, // Metadata bin for data that is temporary during image lifetime.
kMetadata,
kLast = kMetadata, // Number of bins which are for mirror objects.
kMirrorCount = kArtField,
}; friend std::ostream& operator<<(std::ostream& stream, Bin bin);
static constexpr size_t kBinBits =
MinimumBitsToStore<uint32_t>(static_cast<size_t>(Bin::kMirrorCount) - 1); // uint32 = typeof(lockword_) // Subtract read barrier bits since we want these to remain 0, or else it may result in DCHECK // failures due to invalid read barrier bits during object field reads. staticconst size_t kBinShift = BitSizeOf<uint32_t>() - kBinBits - LockWord::kGCStateSize; // 111000.....0 staticconst size_t kBinMask = ((static_cast<size_t>(1) << kBinBits) - 1) << kBinShift;
// Number of bins, including non-mirror bins. static constexpr size_t kNumberOfBins = static_cast<size_t>(Bin::kLast) + 1u;
// Number of stub types. static constexpr size_t kNumberOfStubTypes = static_cast<size_t>(StubType::kLast) + 1u;
// We use the lock word to store the bin # and bin index of the object in the image. // // The struct size must be exactly sizeof(LockWord), currently 32-bits, since this will end up // stored in the lock word bit-for-bit when object forwarding addresses are being calculated. struct BinSlot { explicit BinSlot(uint32_t lockword);
BinSlot(Bin bin, uint32_t index);
// The bin an object belongs to, i.e. regular, class/verified, class/initialized, etc.
Bin GetBin() const; // The offset in bytes from the beginning of the bin. Aligned to object size.
uint32_t GetOffset() const; // Pack into a single uint32_t, for storing into a lock word.
uint32_t Uint32Value() const { return lockword_; } // Comparison operator for map support booloperator<(const BinSlot& other) const { return lockword_ < other.lockword_; }
private: // Must be the same size as LockWord, any larger and we would truncate the data.
uint32_t lockword_;
};
// Calculate the sum total of the bin slot sizes in [0, up_to). Defaults to all bins.
size_t GetBinSizeSum(Bin up_to) const;
MemMap image_; // Memory mapped for generating the image.
// Target begin of this image. Notes: It is not valid to write here, this is the address // of the target image, not necessarily where image_ is mapped. The address is only valid // after layouting (otherwise null).
uint8_t* image_begin_ = nullptr;
// Offset to the free space in image_, initially size of image header.
size_t image_end_ = RoundUp(sizeof(ImageHeader), kObjectAlignment);
uint32_t image_roots_address_ = 0; // The image roots address in the image.
size_t image_offset_ = 0; // Offset of this image from the start of the first image.
// Image size is the *address space* covered by this image. As the live bitmap is aligned // to the page size, the live bitmap will cover more address space than necessary. But live // bitmaps may not overlap, so an image has a "shadow," which is accounted for in the size. // The next image may only start at image_begin_ + image_size_ (which is guaranteed to be // page-aligned).
size_t image_size_ = 0;
// Oat data. // Offset of the oat file for this image from start of oat files. This is // valid when the previous oat file has been written.
size_t oat_offset_ = 0; // Layout of the loaded ELF file containing the oat file, valid after UpdateOatFileLayout(). const uint8_t* oat_file_begin_ = nullptr;
size_t oat_loaded_size_ = 0; const uint8_t* oat_data_begin_ = nullptr;
size_t oat_size_ = 0; // Size of the corresponding oat data. // The oat header checksum, valid after UpdateOatFileHeader().
uint32_t oat_checksum_ = 0u;
// Image bitmap which lets us know where the objects inside of the image reside.
gc::accounting::ContinuousSpaceBitmap image_bitmap_;
// Offset from oat_data_begin_ to the stubs.
uint32_t stub_offsets_[kNumberOfStubTypes] = {};
// Bin slot tracking for dirty object packing.
size_t bin_slot_sizes_[kNumberOfBins] = {}; // Number of bytes in a bin.
size_t bin_slot_offsets_[kNumberOfBins] = {}; // Number of bytes in previous bins.
size_t bin_slot_count_[kNumberOfBins] = {}; // Number of objects in a bin.
// Cached size of the intern table for when we allocate memory.
size_t intern_table_bytes_ = 0;
// Number of image class table bytes.
size_t class_table_bytes_ = 0;
// Number of object fixup bytes.
size_t object_fixup_bytes_ = 0;
// Number of pointer fixup bytes.
size_t pointer_fixup_bytes_ = 0;
// Number of offsets to string references that will be written to the // StringFieldOffsets section.
size_t num_string_references_ = 0;
// Offsets into the image that indicate where string references are recorded.
dchecked_vector<AppImageReferenceOffsetInfo> string_reference_offsets_;
// Intern table associated with this image for serialization.
size_t intern_table_size_ = 0;
std::unique_ptr<GcRoot<mirror::String>[]> intern_table_buffer_;
std::optional<InternTable::UnorderedSet> intern_table_;
// Class table associated with this image for serialization.
size_t class_table_size_ = 0;
std::unique_ptr<ClassTable::ClassSet::value_type[]> class_table_buffer_;
std::optional<ClassTable::ClassSet> class_table_;
// Padding offsets to ensure region alignment (if required). // Objects need to be added from the recorded offset until the end of the region.
dchecked_vector<size_t> padding_offsets_;
};
// We use the lock word to store the offset of the object in the image.
size_t GetImageOffset(mirror::Object* object, size_t oat_index) const
REQUIRES_SHARED(Locks::mutator_lock_);
// Returns the address in the boot image if we are compiling the app image. const uint8_t* GetOatAddress(StubType type) const;
const uint8_t* GetOatAddressForOffset(uint32_t offset, const ImageInfo& image_info) const { // With Quick, code is within the OatFile, as there are all in one // .o ELF object. But interpret it as signed.
DCHECK_LE(static_cast<int32_t>(offset), static_cast<int32_t>(image_info.oat_size_));
DCHECK(image_info.oat_data_begin_ != nullptr); return offset == 0u ? nullptr : image_info.oat_data_begin_ + static_cast<int32_t>(offset);
}
// Returns true if the class was in the original requested image classes list. bool KeepClass(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_);
// Debug aid that list of requested image classes. void DumpImageClasses();
// Visit all class loaders. void VisitClassLoaders(ClassLoaderVisitor* visitor) REQUIRES_SHARED(Locks::mutator_lock_);
// Remove unwanted classes from various roots. void PruneNonImageClasses() REQUIRES_SHARED(Locks::mutator_lock_);
// Find dex caches for pruning or preloading.
dchecked_vector<ObjPtr<mirror::DexCache>> FindDexCaches(Thread* self)
REQUIRES_SHARED(Locks::mutator_lock_)
REQUIRES(!Locks::classlinker_classes_lock_);
// Lays out where the image objects will be at runtime. void CalculateNewObjectOffsets()
REQUIRES_SHARED(Locks::mutator_lock_); void CreateHeader(size_t oat_index, size_t component_count)
REQUIRES_SHARED(Locks::mutator_lock_); bool CreateImageRoots() REQUIRES_SHARED(Locks::mutator_lock_);
// Get quick code for non-resolution/imt_conflict/abstract method. const uint8_t* GetQuickCode(ArtMethod* method, const ImageInfo& image_info)
REQUIRES_SHARED(Locks::mutator_lock_);
// Return true if a method is likely to be dirtied at runtime. bool WillMethodBeDirty(ArtMethod* m) const REQUIRES_SHARED(Locks::mutator_lock_);
// Assign the offset for an ArtMethod. void AssignMethodOffset(ArtMethod* method,
NativeObjectRelocationType type,
size_t oat_index)
REQUIRES_SHARED(Locks::mutator_lock_);
// Assign the offset for a method with unique JNI stub. void AssignJniStubMethodOffset(ArtMethod* method, size_t oat_index)
REQUIRES_SHARED(Locks::mutator_lock_);
// Return true if imt was newly inserted. bool TryAssignImTableOffset(ImTable* imt, size_t oat_index) REQUIRES_SHARED(Locks::mutator_lock_);
// Assign the offset for an IMT conflict table. Does nothing if the table already has a native // relocation. void TryAssignConflictTableOffset(ImtConflictTable* table, size_t oat_index)
REQUIRES_SHARED(Locks::mutator_lock_);
// Return true if `klass` depends on a class defined by the boot class path // we're compiling against but not present in the boot image spaces. We want // to prune these classes since we cannot guarantee that they will not be // already loaded at run time when loading this image. This means that we // also cannot have any classes which refer to these non image classes. bool PruneImageClass(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_);
// early_exit is true if we had a cyclic dependency anywhere down the chain. bool PruneImageClassInternal(ObjPtr<mirror::Class> klass, bool* early_exit,
HashSet<mirror::Object*>* visited)
REQUIRES_SHARED(Locks::mutator_lock_);
// Location of where the object will be when the image is loaded at runtime. template <typename T>
T* NativeLocationInImage(T* obj) REQUIRES_SHARED(Locks::mutator_lock_);
ArtField* NativeLocationInImage(ArtField* src_field) REQUIRES_SHARED(Locks::mutator_lock_);
// Return true if `dex_cache` belongs to the image we're writing. // For a boot image, this is true for all dex caches. // For an app image, boot class path dex caches are excluded. bool IsImageDexCache(ObjPtr<mirror::DexCache> dex_cache) const
REQUIRES_SHARED(Locks::mutator_lock_);
// Return true if `obj` is inside of a boot image space that we're compiling against. // (Always false when compiling the boot image.)
ALWAYS_INLINE bool IsInBootImage(constvoid* obj) const { returnreinterpret_cast<uintptr_t>(obj) - boot_image_begin_ < boot_image_size_;
}
// Size of pointers on the target architecture.
PointerSize target_ptr_size_;
// Whether to mark non-abstract, non-intrinsic methods as "memory shared methods". bool mark_memory_shared_methods_;
// Cached boot image begin and size. This includes heap, native objects and oat files. const uint32_t boot_image_begin_; const uint32_t boot_image_size_;
// Beginning target image address for the first image.
uint8_t* global_image_begin_;
// Offset from image_begin_ to where the first object is in image_.
size_t image_objects_offset_begin_;
// Saved hash codes. We use these to restore lockwords which were temporarily used to have // forwarding addresses as well as copying over hash codes.
HashMap<mirror::Object*, uint32_t> saved_hashcode_map_;
// Oat index map for objects.
HashMap<mirror::Object*, uint32_t> oat_index_map_;
// Image data indexed by the oat file index.
dchecked_vector<ImageInfo> image_infos_;
// ArtField, ArtMethod relocating map. These are allocated as array of structs but we want to // have one entry per art field for convenience. ArtFields are placed right after the end of the // image objects (aka sum of bin_slot_sizes_). ArtMethods are placed right after the ArtFields.
HashMap<void*, NativeObjectRelocation> native_object_relocations_;
// HashMap used for generating JniStubMethodsSection.
JniStubHashMap<std::pair<ArtMethod*, JniStubMethodRelocation>> jni_stub_map_;
// Runtime ArtMethods which aren't reachable from any Class but need to be copied into the image.
ArtMethod* image_methods_[ImageHeader::kImageMethodsCount];
// Counters for measurements, used for logging only.
uint64_t dirty_methods_;
uint64_t clean_methods_;
// Prune class memoization table to speed up ContainsBootClassLoaderNonImageClass.
HashMap<mirror::Class*, bool> prune_class_memo_;
// The application class loader. Null for boot image.
jobject app_class_loader_;
// Boot image live objects, invalid for app image.
mirror::ObjectArray<mirror::Object>* boot_image_live_objects_;
// Image roots corresponding to individual image files.
dchecked_vector<jobject> image_roots_;
// Which mode the image is stored as, see image.h const ImageHeader::StorageMode image_storage_mode_;
// The file names of oat files. const std::vector<std::string>& oat_filenames_;
// Map of dex files to the indexes of oat files that they were compiled into. const HashMap<const DexFile*, size_t>& dex_file_oat_index_map_;
// Set of classes/objects known to be dirty in the image. Can be nullptr if there are none. // Each entry contains a class descriptor with zero or more reference fields, which denote a path // to the dirty object. const std::vector<std::string>* dirty_image_objects_;
// Dirty object instances and their sort keys parsed from dirty_image_object_
HashMap<mirror::Object*, uint32_t> dirty_objects_;
// Objects are guaranteed to not cross the region size boundary.
size_t region_size_ = 0u;
// Region alignment bytes wasted.
size_t region_alignment_wasted_ = 0u;
class FixupClassVisitor; class FixupRootVisitor; class FixupVisitor; class LayoutHelper; class NativeLocationVisitor; class PruneClassesVisitor; class PruneClassLoaderClassesVisitor; class PruneObjectReferenceVisitor;
// A visitor used by the VerifyNativeGCRootInvariants() function. class NativeGCRootInvariantVisitor;
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.