blob: 489a2890fe1b284d88a9e5c2a0eaa761c2953e0a [file] [log] [blame]
Ian Rogers1d54e732013-05-02 21:10:01 -07001/*
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 Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_GC_SPACE_IMAGE_SPACE_H_
18#define ART_RUNTIME_GC_SPACE_IMAGE_SPACE_H_
Ian Rogers1d54e732013-05-02 21:10:01 -070019
Andreas Gampe2bd84282016-12-05 12:37:36 -080020#include "arch/instruction_set.h"
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070021#include "gc/accounting/space_bitmap.h"
Narayan Kamath11d9f062014-04-23 20:24:57 +010022#include "runtime.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070023#include "space.h"
24
25namespace art {
Brian Carlstrom56d947f2013-07-15 13:14:23 -070026
27class OatFile;
28
Ian Rogers1d54e732013-05-02 21:10:01 -070029namespace gc {
30namespace space {
31
32// An image space is a space backed with a memory mapped image.
33class ImageSpace : public MemMapSpace {
34 public:
Ian Rogers1d54e732013-05-02 21:10:01 -070035 SpaceType GetType() const {
36 return kSpaceTypeImageSpace;
37 }
38
Andreas Gampe2bd84282016-12-05 12:37:36 -080039 // Load boot image spaces from a primary image file for a specified instruction set.
Brian Carlstrom56d947f2013-07-15 13:14:23 -070040 //
Andreas Gampe2bd84282016-12-05 12:37:36 -080041 // On successful return, the loaded spaces are added to boot_image_spaces (which must be
42 // empty on entry) and oat_file_end is updated with the (page-aligned) end of the last
43 // oat file.
44 static bool LoadBootImage(const std::string& image_file_name,
45 const InstructionSet image_instruction_set,
46 std::vector<space::ImageSpace*>* boot_image_spaces,
47 uint8_t** oat_file_end)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070048 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierfbc31082016-01-24 11:59:56 -080049
50 // Try to open an existing app image space.
Andreas Gampea463b6a2016-08-12 21:53:32 -070051 static std::unique_ptr<ImageSpace> CreateFromAppImage(const char* image,
52 const OatFile* oat_file,
53 std::string* error_msg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070054 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers1d54e732013-05-02 21:10:01 -070055
Narayan Kamath52f84882014-05-02 10:10:39 +010056 // Reads the image header from the specified image location for the
Mathieu Chartier2cebb242015-04-21 16:50:40 -070057 // instruction set image_isa. Returns null on failure, with
Brian Carlstrom31d8f522014-09-29 11:22:54 -070058 // reason in error_msg.
59 static ImageHeader* ReadImageHeader(const char* image_location,
60 InstructionSet image_isa,
61 std::string* error_msg);
62
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070063 // Give access to the OatFile.
64 const OatFile* GetOatFile() const;
65
Brian Carlstrom56d947f2013-07-15 13:14:23 -070066 // Releases the OatFile from the ImageSpace so it can be transfer to
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070067 // the caller, presumably the OatFileManager.
68 std::unique_ptr<const OatFile> ReleaseOatFile();
Brian Carlstrom56d947f2013-07-15 13:14:23 -070069
Mathieu Chartier31e89252013-08-28 11:29:12 -070070 void VerifyImageAllocations()
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070071 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier31e89252013-08-28 11:29:12 -070072
Ian Rogers1d54e732013-05-02 21:10:01 -070073 const ImageHeader& GetImageHeader() const {
74 return *reinterpret_cast<ImageHeader*>(Begin());
75 }
76
Narayan Kamath52f84882014-05-02 10:10:39 +010077 // Actual filename where image was loaded from.
78 // For example: /data/dalvik-cache/arm/system@framework@boot.art
Ian Rogers1d54e732013-05-02 21:10:01 -070079 const std::string GetImageFilename() const {
80 return GetName();
81 }
82
Narayan Kamath52f84882014-05-02 10:10:39 +010083 // Symbolic location for image.
84 // For example: /system/framework/boot.art
85 const std::string GetImageLocation() const {
86 return image_location_;
87 }
88
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070089 accounting::ContinuousSpaceBitmap* GetLiveBitmap() const OVERRIDE {
Ian Rogers1d54e732013-05-02 21:10:01 -070090 return live_bitmap_.get();
91 }
92
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070093 accounting::ContinuousSpaceBitmap* GetMarkBitmap() const OVERRIDE {
Ian Rogers1d54e732013-05-02 21:10:01 -070094 // ImageSpaces have the same bitmap for both live and marked. This helps reduce the number of
95 // special cases to test against.
96 return live_bitmap_.get();
97 }
98
99 void Dump(std::ostream& os) const;
100
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800101 // Sweeping image spaces is a NOP.
102 void Sweep(bool /* swap_bitmaps */, size_t* /* freed_objects */, size_t* /* freed_bytes */) {
103 }
104
Mathieu Chartier31f44142014-04-08 14:40:03 -0700105 bool CanMoveObjects() const OVERRIDE {
106 return false;
107 }
108
Alex Lighta59dd802014-07-02 16:28:08 -0700109 // Returns the filename of the image corresponding to
110 // requested image_location, or the filename where a new image
111 // should be written if one doesn't exist. Looks for a generated
112 // image in the specified location and then in the dalvik-cache.
113 //
114 // Returns true if an image was found, false otherwise.
115 static bool FindImageFilename(const char* image_location,
116 InstructionSet image_isa,
117 std::string* system_location,
118 bool* has_system,
119 std::string* data_location,
120 bool* dalvik_cache_exists,
Andreas Gampe3c13a792014-09-18 20:56:04 -0700121 bool* has_data,
122 bool *is_global_cache);
Alex Lighta59dd802014-07-02 16:28:08 -0700123
Andreas Gampe8994a042015-12-30 19:03:17 +0000124 // Use the input image filename to adapt the names in the given boot classpath to establish
125 // complete locations for secondary images.
Mathieu Chartier866d8742016-09-21 15:24:18 -0700126 static void ExtractMultiImageLocations(const std::string& input_image_file_name,
Andreas Gampe8994a042015-12-30 19:03:17 +0000127 const std::string& boot_classpath,
128 std::vector<std::string>* image_filenames);
129
Mathieu Chartier866d8742016-09-21 15:24:18 -0700130 static std::string GetMultiImageBootClassPath(const std::vector<const char*>& dex_locations,
131 const std::vector<const char*>& oat_filenames,
132 const std::vector<const char*>& image_filenames);
133
Mathieu Chartier763a31e2015-11-16 16:05:55 -0800134 // Return the end of the image which includes non-heap objects such as ArtMethods and ArtFields.
135 uint8_t* GetImageEnd() const {
136 return Begin() + GetImageHeader().GetImageSize();
137 }
138
139 // Return the start of the associated oat file.
140 uint8_t* GetOatFileBegin() const {
141 return GetImageHeader().GetOatFileBegin();
142 }
143
144 // Return the end of the associated oat file.
145 uint8_t* GetOatFileEnd() const {
146 return GetImageHeader().GetOatFileEnd();
147 }
148
Mathieu Chartierd5f3f322016-03-21 14:05:56 -0700149 void DumpSections(std::ostream& os) const;
150
Mathieu Chartier763a31e2015-11-16 16:05:55 -0800151 protected:
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800152 // Tries to initialize an ImageSpace from the given image path, returning null on error.
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700153 //
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800154 // If validate_oat_file is false (for /system), do not verify that image's OatFile is up-to-date
155 // relative to its DexFile inputs. Otherwise (for /data), validate the inputs and generate the
156 // OatFile in /data/dalvik-cache if necessary. If the oat_file is null, it uses the oat file from
157 // the image.
Andreas Gampea463b6a2016-08-12 21:53:32 -0700158 static std::unique_ptr<ImageSpace> Init(const char* image_filename,
159 const char* image_location,
160 bool validate_oat_file,
161 const OatFile* oat_file,
162 std::string* error_msg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700163 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700164
Ian Rogersef7d42f2014-01-06 12:55:46 -0800165 static Atomic<uint32_t> bitmap_index_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700166
Ian Rogers700a4022014-05-19 16:49:03 -0700167 std::unique_ptr<accounting::ContinuousSpaceBitmap> live_bitmap_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700168
Jeff Haodcdc85b2015-12-04 14:06:18 -0800169 ImageSpace(const std::string& name,
170 const char* image_location,
171 MemMap* mem_map,
172 accounting::ContinuousSpaceBitmap* live_bitmap,
Mathieu Chartier2d124ec2016-01-05 18:03:15 -0800173 uint8_t* end);
Ian Rogers1d54e732013-05-02 21:10:01 -0700174
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700175 // The OatFile associated with the image during early startup to
176 // reserve space contiguous to the image. It is later released to
177 // the ClassLinker during it's initialization.
Ian Rogers700a4022014-05-19 16:49:03 -0700178 std::unique_ptr<OatFile> oat_file_;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700179
Andreas Gampe88da3b02015-06-12 20:38:49 -0700180 // There are times when we need to find the boot image oat file. As
181 // we release ownership during startup, keep a non-owned reference.
182 const OatFile* oat_file_non_owned_;
183
Narayan Kamath52f84882014-05-02 10:10:39 +0100184 const std::string image_location_;
185
Andreas Gampea463b6a2016-08-12 21:53:32 -0700186 friend class ImageSpaceLoader;
187 friend class Space;
188
Mathieu Chartier763a31e2015-11-16 16:05:55 -0800189 private:
Andreas Gampe2bd84282016-12-05 12:37:36 -0800190 // Create a boot image space from an image file for a specified instruction
191 // set. Cannot be used for future allocation or collected.
192 //
193 // Create also opens the OatFile associated with the image file so
194 // that it be contiguously allocated with the image before the
195 // creation of the alloc space. The ReleaseOatFile will later be
196 // used to transfer ownership of the OatFile to the ClassLinker when
197 // it is initialized.
198 static std::unique_ptr<ImageSpace> CreateBootImage(const char* image,
199 InstructionSet image_isa,
200 bool secondary_image,
201 std::string* error_msg)
202 REQUIRES_SHARED(Locks::mutator_lock_);
203
Ian Rogers1d54e732013-05-02 21:10:01 -0700204 DISALLOW_COPY_AND_ASSIGN(ImageSpace);
205};
206
207} // namespace space
208} // namespace gc
209} // namespace art
210
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700211#endif // ART_RUNTIME_GC_SPACE_IMAGE_SPACE_H_