blob: 0ba131b5bf675d662a69198042c348ef81aba5ec [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
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070020#include "gc/accounting/space_bitmap.h"
Narayan Kamath11d9f062014-04-23 20:24:57 +010021#include "runtime.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070022#include "space.h"
23
24namespace art {
Brian Carlstrom56d947f2013-07-15 13:14:23 -070025
26class OatFile;
27
Ian Rogers1d54e732013-05-02 21:10:01 -070028namespace gc {
29namespace space {
30
31// An image space is a space backed with a memory mapped image.
32class ImageSpace : public MemMapSpace {
33 public:
Ian Rogers1d54e732013-05-02 21:10:01 -070034 SpaceType GetType() const {
35 return kSpaceTypeImageSpace;
36 }
37
Mathieu Chartierfbc31082016-01-24 11:59:56 -080038 // Create a boot image space from an image file for a specified instruction
Narayan Kamath11d9f062014-04-23 20:24:57 +010039 // set. Cannot be used for future allocation or collected.
Brian Carlstrom56d947f2013-07-15 13:14:23 -070040 //
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.
Andreas Gampea463b6a2016-08-12 21:53:32 -070046 static std::unique_ptr<ImageSpace> CreateBootImage(const char* image,
Mathieu Chartierfbc31082016-01-24 11:59:56 -080047 InstructionSet image_isa,
48 bool secondary_image,
49 std::string* error_msg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070050 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierfbc31082016-01-24 11:59:56 -080051
52 // Try to open an existing app image space.
Andreas Gampea463b6a2016-08-12 21:53:32 -070053 static std::unique_ptr<ImageSpace> CreateFromAppImage(const char* image,
54 const OatFile* oat_file,
55 std::string* error_msg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070056 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers1d54e732013-05-02 21:10:01 -070057
Narayan Kamath52f84882014-05-02 10:10:39 +010058 // Reads the image header from the specified image location for the
Mathieu Chartier2cebb242015-04-21 16:50:40 -070059 // instruction set image_isa. Returns null on failure, with
Brian Carlstrom31d8f522014-09-29 11:22:54 -070060 // reason in error_msg.
61 static ImageHeader* ReadImageHeader(const char* image_location,
62 InstructionSet image_isa,
63 std::string* error_msg);
64
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070065 // Give access to the OatFile.
66 const OatFile* GetOatFile() const;
67
Brian Carlstrom56d947f2013-07-15 13:14:23 -070068 // Releases the OatFile from the ImageSpace so it can be transfer to
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070069 // the caller, presumably the OatFileManager.
70 std::unique_ptr<const OatFile> ReleaseOatFile();
Brian Carlstrom56d947f2013-07-15 13:14:23 -070071
Mathieu Chartier31e89252013-08-28 11:29:12 -070072 void VerifyImageAllocations()
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070073 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier31e89252013-08-28 11:29:12 -070074
Ian Rogers1d54e732013-05-02 21:10:01 -070075 const ImageHeader& GetImageHeader() const {
76 return *reinterpret_cast<ImageHeader*>(Begin());
77 }
78
Narayan Kamath52f84882014-05-02 10:10:39 +010079 // Actual filename where image was loaded from.
80 // For example: /data/dalvik-cache/arm/system@framework@boot.art
Ian Rogers1d54e732013-05-02 21:10:01 -070081 const std::string GetImageFilename() const {
82 return GetName();
83 }
84
Narayan Kamath52f84882014-05-02 10:10:39 +010085 // Symbolic location for image.
86 // For example: /system/framework/boot.art
87 const std::string GetImageLocation() const {
88 return image_location_;
89 }
90
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070091 accounting::ContinuousSpaceBitmap* GetLiveBitmap() const OVERRIDE {
Ian Rogers1d54e732013-05-02 21:10:01 -070092 return live_bitmap_.get();
93 }
94
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070095 accounting::ContinuousSpaceBitmap* GetMarkBitmap() const OVERRIDE {
Ian Rogers1d54e732013-05-02 21:10:01 -070096 // ImageSpaces have the same bitmap for both live and marked. This helps reduce the number of
97 // special cases to test against.
98 return live_bitmap_.get();
99 }
100
101 void Dump(std::ostream& os) const;
102
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800103 // Sweeping image spaces is a NOP.
104 void Sweep(bool /* swap_bitmaps */, size_t* /* freed_objects */, size_t* /* freed_bytes */) {
105 }
106
Mathieu Chartier31f44142014-04-08 14:40:03 -0700107 bool CanMoveObjects() const OVERRIDE {
108 return false;
109 }
110
Alex Lighta59dd802014-07-02 16:28:08 -0700111 // Returns the filename of the image corresponding to
112 // requested image_location, or the filename where a new image
113 // should be written if one doesn't exist. Looks for a generated
114 // image in the specified location and then in the dalvik-cache.
115 //
116 // Returns true if an image was found, false otherwise.
117 static bool FindImageFilename(const char* image_location,
118 InstructionSet image_isa,
119 std::string* system_location,
120 bool* has_system,
121 std::string* data_location,
122 bool* dalvik_cache_exists,
Andreas Gampe3c13a792014-09-18 20:56:04 -0700123 bool* has_data,
124 bool *is_global_cache);
Alex Lighta59dd802014-07-02 16:28:08 -0700125
Andreas Gampe8994a042015-12-30 19:03:17 +0000126 // Use the input image filename to adapt the names in the given boot classpath to establish
127 // complete locations for secondary images.
Mathieu Chartier866d8742016-09-21 15:24:18 -0700128 static void ExtractMultiImageLocations(const std::string& input_image_file_name,
Andreas Gampe8994a042015-12-30 19:03:17 +0000129 const std::string& boot_classpath,
130 std::vector<std::string>* image_filenames);
131
Mathieu Chartier866d8742016-09-21 15:24:18 -0700132 static std::string GetMultiImageBootClassPath(const std::vector<const char*>& dex_locations,
133 const std::vector<const char*>& oat_filenames,
134 const std::vector<const char*>& image_filenames);
135
Mathieu Chartier763a31e2015-11-16 16:05:55 -0800136 // Return the end of the image which includes non-heap objects such as ArtMethods and ArtFields.
137 uint8_t* GetImageEnd() const {
138 return Begin() + GetImageHeader().GetImageSize();
139 }
140
141 // Return the start of the associated oat file.
142 uint8_t* GetOatFileBegin() const {
143 return GetImageHeader().GetOatFileBegin();
144 }
145
146 // Return the end of the associated oat file.
147 uint8_t* GetOatFileEnd() const {
148 return GetImageHeader().GetOatFileEnd();
149 }
150
Mathieu Chartierd5f3f322016-03-21 14:05:56 -0700151 void DumpSections(std::ostream& os) const;
152
Mathieu Chartier763a31e2015-11-16 16:05:55 -0800153 protected:
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800154 // Tries to initialize an ImageSpace from the given image path, returning null on error.
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700155 //
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800156 // If validate_oat_file is false (for /system), do not verify that image's OatFile is up-to-date
157 // relative to its DexFile inputs. Otherwise (for /data), validate the inputs and generate the
158 // OatFile in /data/dalvik-cache if necessary. If the oat_file is null, it uses the oat file from
159 // the image.
Andreas Gampea463b6a2016-08-12 21:53:32 -0700160 static std::unique_ptr<ImageSpace> Init(const char* image_filename,
161 const char* image_location,
162 bool validate_oat_file,
163 const OatFile* oat_file,
164 std::string* error_msg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700165 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700166
Ian Rogersef7d42f2014-01-06 12:55:46 -0800167 static Atomic<uint32_t> bitmap_index_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700168
Ian Rogers700a4022014-05-19 16:49:03 -0700169 std::unique_ptr<accounting::ContinuousSpaceBitmap> live_bitmap_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700170
Jeff Haodcdc85b2015-12-04 14:06:18 -0800171 ImageSpace(const std::string& name,
172 const char* image_location,
173 MemMap* mem_map,
174 accounting::ContinuousSpaceBitmap* live_bitmap,
Mathieu Chartier2d124ec2016-01-05 18:03:15 -0800175 uint8_t* end);
Ian Rogers1d54e732013-05-02 21:10:01 -0700176
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700177 // The OatFile associated with the image during early startup to
178 // reserve space contiguous to the image. It is later released to
179 // the ClassLinker during it's initialization.
Ian Rogers700a4022014-05-19 16:49:03 -0700180 std::unique_ptr<OatFile> oat_file_;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700181
Andreas Gampe88da3b02015-06-12 20:38:49 -0700182 // There are times when we need to find the boot image oat file. As
183 // we release ownership during startup, keep a non-owned reference.
184 const OatFile* oat_file_non_owned_;
185
Narayan Kamath52f84882014-05-02 10:10:39 +0100186 const std::string image_location_;
187
Andreas Gampea463b6a2016-08-12 21:53:32 -0700188 friend class ImageSpaceLoader;
189 friend class Space;
190
Mathieu Chartier763a31e2015-11-16 16:05:55 -0800191 private:
Ian Rogers1d54e732013-05-02 21:10:01 -0700192 DISALLOW_COPY_AND_ASSIGN(ImageSpace);
193};
194
195} // namespace space
196} // namespace gc
197} // namespace art
198
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700199#endif // ART_RUNTIME_GC_SPACE_IMAGE_SPACE_H_