Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 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 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_GC_SPACE_IMAGE_SPACE_H_ |
| 18 | #define ART_RUNTIME_GC_SPACE_IMAGE_SPACE_H_ |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 19 | |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 20 | #include "gc/accounting/space_bitmap.h" |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 21 | #include "runtime.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 22 | #include "space.h" |
| 23 | |
| 24 | namespace art { |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 25 | |
| 26 | class OatFile; |
| 27 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 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: |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 34 | SpaceType GetType() const { |
| 35 | return kSpaceTypeImageSpace; |
| 36 | } |
| 37 | |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 38 | // Create a Space from an image file for a specified instruction |
| 39 | // set. Cannot be used for future allocation or collected. |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 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. |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 46 | static ImageSpace* Create(const char* image, InstructionSet image_isa, std::string* error_msg) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 47 | SHARED_REQUIRES(Locks::mutator_lock_); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 48 | |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 49 | // Reads the image header from the specified image location for the |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 50 | // instruction set image_isa or dies trying. |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 51 | static ImageHeader* ReadImageHeaderOrDie(const char* image_location, |
Brian Carlstrom | 2afe494 | 2014-05-19 10:25:33 -0700 | [diff] [blame] | 52 | InstructionSet image_isa); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 53 | |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 54 | // Reads the image header from the specified image location for the |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 55 | // instruction set image_isa. Returns null on failure, with |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 56 | // reason in error_msg. |
| 57 | static ImageHeader* ReadImageHeader(const char* image_location, |
| 58 | InstructionSet image_isa, |
| 59 | std::string* error_msg); |
| 60 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 61 | // Give access to the OatFile. |
| 62 | const OatFile* GetOatFile() const; |
| 63 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 64 | // Releases the OatFile from the ImageSpace so it can be transfer to |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 65 | // the caller, presumably the OatFileManager. |
| 66 | std::unique_ptr<const OatFile> ReleaseOatFile(); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 67 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 68 | void VerifyImageAllocations() |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 69 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 70 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 71 | const ImageHeader& GetImageHeader() const { |
| 72 | return *reinterpret_cast<ImageHeader*>(Begin()); |
| 73 | } |
| 74 | |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 75 | // Actual filename where image was loaded from. |
| 76 | // For example: /data/dalvik-cache/arm/system@framework@boot.art |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 77 | const std::string GetImageFilename() const { |
| 78 | return GetName(); |
| 79 | } |
| 80 | |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 81 | // Symbolic location for image. |
| 82 | // For example: /system/framework/boot.art |
| 83 | const std::string GetImageLocation() const { |
| 84 | return image_location_; |
| 85 | } |
| 86 | |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 87 | accounting::ContinuousSpaceBitmap* GetLiveBitmap() const OVERRIDE { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 88 | return live_bitmap_.get(); |
| 89 | } |
| 90 | |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 91 | accounting::ContinuousSpaceBitmap* GetMarkBitmap() const OVERRIDE { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 92 | // ImageSpaces have the same bitmap for both live and marked. This helps reduce the number of |
| 93 | // special cases to test against. |
| 94 | return live_bitmap_.get(); |
| 95 | } |
| 96 | |
| 97 | void Dump(std::ostream& os) const; |
| 98 | |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 99 | // Sweeping image spaces is a NOP. |
| 100 | void Sweep(bool /* swap_bitmaps */, size_t* /* freed_objects */, size_t* /* freed_bytes */) { |
| 101 | } |
| 102 | |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 103 | bool CanMoveObjects() const OVERRIDE { |
| 104 | return false; |
| 105 | } |
| 106 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 107 | // Returns the filename of the image corresponding to |
| 108 | // requested image_location, or the filename where a new image |
| 109 | // should be written if one doesn't exist. Looks for a generated |
| 110 | // image in the specified location and then in the dalvik-cache. |
| 111 | // |
| 112 | // Returns true if an image was found, false otherwise. |
| 113 | static bool FindImageFilename(const char* image_location, |
| 114 | InstructionSet image_isa, |
| 115 | std::string* system_location, |
| 116 | bool* has_system, |
| 117 | std::string* data_location, |
| 118 | bool* dalvik_cache_exists, |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 119 | bool* has_data, |
| 120 | bool *is_global_cache); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 121 | |
Mathieu Chartier | 763a31e | 2015-11-16 16:05:55 -0800 | [diff] [blame] | 122 | // Return the end of the image which includes non-heap objects such as ArtMethods and ArtFields. |
| 123 | uint8_t* GetImageEnd() const { |
| 124 | return Begin() + GetImageHeader().GetImageSize(); |
| 125 | } |
| 126 | |
| 127 | // Return the start of the associated oat file. |
| 128 | uint8_t* GetOatFileBegin() const { |
| 129 | return GetImageHeader().GetOatFileBegin(); |
| 130 | } |
| 131 | |
| 132 | // Return the end of the associated oat file. |
| 133 | uint8_t* GetOatFileEnd() const { |
| 134 | return GetImageHeader().GetOatFileEnd(); |
| 135 | } |
| 136 | |
| 137 | protected: |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 138 | // Tries to initialize an ImageSpace from the given image path, |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 139 | // returning null on error. |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 140 | // |
| 141 | // If validate_oat_file is false (for /system), do not verify that |
| 142 | // image's OatFile is up-to-date relative to its DexFile |
| 143 | // inputs. Otherwise (for /data), validate the inputs and generate |
| 144 | // the OatFile in /data/dalvik-cache if necessary. |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 145 | static ImageSpace* Init(const char* image_filename, const char* image_location, |
| 146 | bool validate_oat_file, std::string* error_msg) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 147 | SHARED_REQUIRES(Locks::mutator_lock_); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 148 | |
Nicolas Geoffray | 9583fbc | 2014-02-28 15:21:07 +0000 | [diff] [blame] | 149 | OatFile* OpenOatFile(const char* image, std::string* error_msg) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 150 | SHARED_REQUIRES(Locks::mutator_lock_); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 151 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 152 | bool ValidateOatFile(std::string* error_msg) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 153 | SHARED_REQUIRES(Locks::mutator_lock_); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 154 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 155 | friend class Space; |
| 156 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 157 | static Atomic<uint32_t> bitmap_index_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 158 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 159 | std::unique_ptr<accounting::ContinuousSpaceBitmap> live_bitmap_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 160 | |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 161 | ImageSpace(const std::string& name, const char* image_location, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 162 | MemMap* mem_map, accounting::ContinuousSpaceBitmap* live_bitmap, uint8_t* end); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 163 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 164 | // The OatFile associated with the image during early startup to |
| 165 | // reserve space contiguous to the image. It is later released to |
| 166 | // the ClassLinker during it's initialization. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 167 | std::unique_ptr<OatFile> oat_file_; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 168 | |
Andreas Gampe | 88da3b0 | 2015-06-12 20:38:49 -0700 | [diff] [blame] | 169 | // There are times when we need to find the boot image oat file. As |
| 170 | // we release ownership during startup, keep a non-owned reference. |
| 171 | const OatFile* oat_file_non_owned_; |
| 172 | |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 173 | const std::string image_location_; |
| 174 | |
Mathieu Chartier | 763a31e | 2015-11-16 16:05:55 -0800 | [diff] [blame] | 175 | private: |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 176 | DISALLOW_COPY_AND_ASSIGN(ImageSpace); |
| 177 | }; |
| 178 | |
| 179 | } // namespace space |
| 180 | } // namespace gc |
| 181 | } // namespace art |
| 182 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 183 | #endif // ART_RUNTIME_GC_SPACE_IMAGE_SPACE_H_ |