1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef ART_RUNTIME_GC_SPACE_IMAGE_SPACE_H_ 18 #define ART_RUNTIME_GC_SPACE_IMAGE_SPACE_H_ 19 20 #include "gc/accounting/space_bitmap.h" 21 #include "runtime.h" 22 #include "space.h" 23 24 namespace art { 25 26 class OatFile; 27 28 namespace gc { 29 namespace space { 30 31 // An image space is a space backed with a memory mapped image. 32 class ImageSpace : public MemMapSpace { 33 public: GetType()34 SpaceType GetType() const { 35 return kSpaceTypeImageSpace; 36 } 37 38 // Create a boot image space from an image file for a specified instruction 39 // set. Cannot be used for future allocation or collected. 40 // 41 // Create also opens the OatFile associated with the image file so 42 // that it be contiguously allocated with the image before the 43 // creation of the alloc space. The ReleaseOatFile will later be 44 // used to transfer ownership of the OatFile to the ClassLinker when 45 // it is initialized. 46 static ImageSpace* CreateBootImage(const char* image, 47 InstructionSet image_isa, 48 bool secondary_image, 49 std::string* error_msg) 50 SHARED_REQUIRES(Locks::mutator_lock_); 51 52 // Try to open an existing app image space. 53 static ImageSpace* CreateFromAppImage(const char* image, 54 const OatFile* oat_file, 55 std::string* error_msg) 56 SHARED_REQUIRES(Locks::mutator_lock_); 57 58 // Reads the image header from the specified image location for the 59 // instruction set image_isa or dies trying. 60 static ImageHeader* ReadImageHeaderOrDie(const char* image_location, 61 InstructionSet image_isa); 62 63 // Reads the image header from the specified image location for the 64 // instruction set image_isa. Returns null on failure, with 65 // reason in error_msg. 66 static ImageHeader* ReadImageHeader(const char* image_location, 67 InstructionSet image_isa, 68 std::string* error_msg); 69 70 // Give access to the OatFile. 71 const OatFile* GetOatFile() const; 72 73 // Releases the OatFile from the ImageSpace so it can be transfer to 74 // the caller, presumably the OatFileManager. 75 std::unique_ptr<const OatFile> ReleaseOatFile(); 76 77 void VerifyImageAllocations() 78 SHARED_REQUIRES(Locks::mutator_lock_); 79 GetImageHeader()80 const ImageHeader& GetImageHeader() const { 81 return *reinterpret_cast<ImageHeader*>(Begin()); 82 } 83 84 // Actual filename where image was loaded from. 85 // For example: /data/dalvik-cache/arm/system@framework@boot.art GetImageFilename()86 const std::string GetImageFilename() const { 87 return GetName(); 88 } 89 90 // Symbolic location for image. 91 // For example: /system/framework/boot.art GetImageLocation()92 const std::string GetImageLocation() const { 93 return image_location_; 94 } 95 GetLiveBitmap()96 accounting::ContinuousSpaceBitmap* GetLiveBitmap() const OVERRIDE { 97 return live_bitmap_.get(); 98 } 99 GetMarkBitmap()100 accounting::ContinuousSpaceBitmap* GetMarkBitmap() const OVERRIDE { 101 // ImageSpaces have the same bitmap for both live and marked. This helps reduce the number of 102 // special cases to test against. 103 return live_bitmap_.get(); 104 } 105 106 void Dump(std::ostream& os) const; 107 108 // Sweeping image spaces is a NOP. Sweep(bool,size_t *,size_t *)109 void Sweep(bool /* swap_bitmaps */, size_t* /* freed_objects */, size_t* /* freed_bytes */) { 110 } 111 CanMoveObjects()112 bool CanMoveObjects() const OVERRIDE { 113 return false; 114 } 115 116 // Returns the filename of the image corresponding to 117 // requested image_location, or the filename where a new image 118 // should be written if one doesn't exist. Looks for a generated 119 // image in the specified location and then in the dalvik-cache. 120 // 121 // Returns true if an image was found, false otherwise. 122 static bool FindImageFilename(const char* image_location, 123 InstructionSet image_isa, 124 std::string* system_location, 125 bool* has_system, 126 std::string* data_location, 127 bool* dalvik_cache_exists, 128 bool* has_data, 129 bool *is_global_cache); 130 131 // Use the input image filename to adapt the names in the given boot classpath to establish 132 // complete locations for secondary images. 133 static void CreateMultiImageLocations(const std::string& input_image_file_name, 134 const std::string& boot_classpath, 135 std::vector<std::string>* image_filenames); 136 137 // Return the end of the image which includes non-heap objects such as ArtMethods and ArtFields. GetImageEnd()138 uint8_t* GetImageEnd() const { 139 return Begin() + GetImageHeader().GetImageSize(); 140 } 141 142 // Return the start of the associated oat file. GetOatFileBegin()143 uint8_t* GetOatFileBegin() const { 144 return GetImageHeader().GetOatFileBegin(); 145 } 146 147 // Return the end of the associated oat file. GetOatFileEnd()148 uint8_t* GetOatFileEnd() const { 149 return GetImageHeader().GetOatFileEnd(); 150 } 151 152 void DumpSections(std::ostream& os) const; 153 154 protected: 155 // Tries to initialize an ImageSpace from the given image path, returning null on error. 156 // 157 // If validate_oat_file is false (for /system), do not verify that image's OatFile is up-to-date 158 // relative to its DexFile inputs. Otherwise (for /data), validate the inputs and generate the 159 // OatFile in /data/dalvik-cache if necessary. If the oat_file is null, it uses the oat file from 160 // the image. 161 static ImageSpace* Init(const char* image_filename, 162 const char* image_location, 163 bool validate_oat_file, 164 const OatFile* oat_file, 165 std::string* error_msg) 166 SHARED_REQUIRES(Locks::mutator_lock_); 167 168 OatFile* OpenOatFile(const char* image, std::string* error_msg) const 169 SHARED_REQUIRES(Locks::mutator_lock_); 170 171 bool ValidateOatFile(std::string* error_msg) const 172 SHARED_REQUIRES(Locks::mutator_lock_); 173 174 friend class Space; 175 176 static Atomic<uint32_t> bitmap_index_; 177 178 std::unique_ptr<accounting::ContinuousSpaceBitmap> live_bitmap_; 179 180 ImageSpace(const std::string& name, 181 const char* image_location, 182 MemMap* mem_map, 183 accounting::ContinuousSpaceBitmap* live_bitmap, 184 uint8_t* end); 185 186 // The OatFile associated with the image during early startup to 187 // reserve space contiguous to the image. It is later released to 188 // the ClassLinker during it's initialization. 189 std::unique_ptr<OatFile> oat_file_; 190 191 // There are times when we need to find the boot image oat file. As 192 // we release ownership during startup, keep a non-owned reference. 193 const OatFile* oat_file_non_owned_; 194 195 const std::string image_location_; 196 197 private: 198 DISALLOW_COPY_AND_ASSIGN(ImageSpace); 199 }; 200 201 } // namespace space 202 } // namespace gc 203 } // namespace art 204 205 #endif // ART_RUNTIME_GC_SPACE_IMAGE_SPACE_H_ 206