blob: 9c8e8b2014692a22dfa11cf1dce0cd056d32c4f9 [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
Nicolas Geoffray1bc977c2016-01-23 14:15:49 +000038 // Create a 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.
Nicolas Geoffray1bc977c2016-01-23 14:15:49 +000046 static ImageSpace* Create(const char* image,
47 InstructionSet image_isa,
48 bool secondary_image,
49 std::string* error_msg)
Mathieu Chartier90443472015-07-16 20:32:27 -070050 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers1d54e732013-05-02 21:10:01 -070051
Narayan Kamath52f84882014-05-02 10:10:39 +010052 // Reads the image header from the specified image location for the
Brian Carlstrom31d8f522014-09-29 11:22:54 -070053 // instruction set image_isa or dies trying.
Narayan Kamath52f84882014-05-02 10:10:39 +010054 static ImageHeader* ReadImageHeaderOrDie(const char* image_location,
Brian Carlstrom2afe4942014-05-19 10:25:33 -070055 InstructionSet image_isa);
Narayan Kamath52f84882014-05-02 10:10:39 +010056
Brian Carlstrom31d8f522014-09-29 11:22:54 -070057 // Reads the image header from the specified image location for the
Mathieu Chartier2cebb242015-04-21 16:50:40 -070058 // instruction set image_isa. Returns null on failure, with
Brian Carlstrom31d8f522014-09-29 11:22:54 -070059 // reason in error_msg.
60 static ImageHeader* ReadImageHeader(const char* image_location,
61 InstructionSet image_isa,
62 std::string* error_msg);
63
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070064 // Give access to the OatFile.
65 const OatFile* GetOatFile() const;
66
Brian Carlstrom56d947f2013-07-15 13:14:23 -070067 // Releases the OatFile from the ImageSpace so it can be transfer to
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070068 // the caller, presumably the OatFileManager.
69 std::unique_ptr<const OatFile> ReleaseOatFile();
Brian Carlstrom56d947f2013-07-15 13:14:23 -070070
Mathieu Chartier31e89252013-08-28 11:29:12 -070071 void VerifyImageAllocations()
Mathieu Chartier90443472015-07-16 20:32:27 -070072 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier31e89252013-08-28 11:29:12 -070073
Ian Rogers1d54e732013-05-02 21:10:01 -070074 const ImageHeader& GetImageHeader() const {
75 return *reinterpret_cast<ImageHeader*>(Begin());
76 }
77
Narayan Kamath52f84882014-05-02 10:10:39 +010078 // Actual filename where image was loaded from.
79 // For example: /data/dalvik-cache/arm/system@framework@boot.art
Ian Rogers1d54e732013-05-02 21:10:01 -070080 const std::string GetImageFilename() const {
81 return GetName();
82 }
83
Narayan Kamath52f84882014-05-02 10:10:39 +010084 // Symbolic location for image.
85 // For example: /system/framework/boot.art
86 const std::string GetImageLocation() const {
87 return image_location_;
88 }
89
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070090 accounting::ContinuousSpaceBitmap* GetLiveBitmap() const OVERRIDE {
Ian Rogers1d54e732013-05-02 21:10:01 -070091 return live_bitmap_.get();
92 }
93
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070094 accounting::ContinuousSpaceBitmap* GetMarkBitmap() const OVERRIDE {
Ian Rogers1d54e732013-05-02 21:10:01 -070095 // ImageSpaces have the same bitmap for both live and marked. This helps reduce the number of
96 // special cases to test against.
97 return live_bitmap_.get();
98 }
99
100 void Dump(std::ostream& os) const;
101
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800102 // Sweeping image spaces is a NOP.
103 void Sweep(bool /* swap_bitmaps */, size_t* /* freed_objects */, size_t* /* freed_bytes */) {
104 }
105
Mathieu Chartier31f44142014-04-08 14:40:03 -0700106 bool CanMoveObjects() const OVERRIDE {
107 return false;
108 }
109
Alex Lighta59dd802014-07-02 16:28:08 -0700110 // Returns the filename of the image corresponding to
111 // requested image_location, or the filename where a new image
112 // should be written if one doesn't exist. Looks for a generated
113 // image in the specified location and then in the dalvik-cache.
114 //
115 // Returns true if an image was found, false otherwise.
116 static bool FindImageFilename(const char* image_location,
117 InstructionSet image_isa,
118 std::string* system_location,
119 bool* has_system,
120 std::string* data_location,
121 bool* dalvik_cache_exists,
Andreas Gampe3c13a792014-09-18 20:56:04 -0700122 bool* has_data,
123 bool *is_global_cache);
Alex Lighta59dd802014-07-02 16:28:08 -0700124
Andreas Gampe8994a042015-12-30 19:03:17 +0000125 // Use the input image filename to adapt the names in the given boot classpath to establish
126 // complete locations for secondary images.
127 static void CreateMultiImageLocations(const std::string& input_image_file_name,
128 const std::string& boot_classpath,
129 std::vector<std::string>* image_filenames);
130
Mathieu Chartier763a31e2015-11-16 16:05:55 -0800131 // Return the end of the image which includes non-heap objects such as ArtMethods and ArtFields.
132 uint8_t* GetImageEnd() const {
133 return Begin() + GetImageHeader().GetImageSize();
134 }
135
136 // Return the start of the associated oat file.
137 uint8_t* GetOatFileBegin() const {
138 return GetImageHeader().GetOatFileBegin();
139 }
140
141 // Return the end of the associated oat file.
142 uint8_t* GetOatFileEnd() const {
143 return GetImageHeader().GetOatFileEnd();
144 }
145
146 protected:
Nicolas Geoffray1bc977c2016-01-23 14:15:49 +0000147 // Tries to initialize an ImageSpace from the given image path,
148 // returning null on error.
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700149 //
Nicolas Geoffray1bc977c2016-01-23 14:15:49 +0000150 // If validate_oat_file is false (for /system), do not verify that
151 // image's OatFile is up-to-date relative to its DexFile
152 // inputs. Otherwise (for /data), validate the inputs and generate
153 // the OatFile in /data/dalvik-cache if necessary.
154 static ImageSpace* Init(const char* image_filename, const char* image_location,
155 bool validate_oat_file, std::string* error_msg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700156 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700157
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000158 OatFile* OpenOatFile(const char* image, std::string* error_msg) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700159 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700160
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700161 bool ValidateOatFile(std::string* error_msg) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700162 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700163
Ian Rogers1d54e732013-05-02 21:10:01 -0700164 friend class Space;
165
Ian Rogersef7d42f2014-01-06 12:55:46 -0800166 static Atomic<uint32_t> bitmap_index_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700167
Ian Rogers700a4022014-05-19 16:49:03 -0700168 std::unique_ptr<accounting::ContinuousSpaceBitmap> live_bitmap_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700169
Jeff Haodcdc85b2015-12-04 14:06:18 -0800170 ImageSpace(const std::string& name,
171 const char* image_location,
172 MemMap* mem_map,
173 accounting::ContinuousSpaceBitmap* live_bitmap,
Mathieu Chartier2d124ec2016-01-05 18:03:15 -0800174 uint8_t* end);
Ian Rogers1d54e732013-05-02 21:10:01 -0700175
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700176 // The OatFile associated with the image during early startup to
177 // reserve space contiguous to the image. It is later released to
178 // the ClassLinker during it's initialization.
Ian Rogers700a4022014-05-19 16:49:03 -0700179 std::unique_ptr<OatFile> oat_file_;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700180
Andreas Gampe88da3b02015-06-12 20:38:49 -0700181 // There are times when we need to find the boot image oat file. As
182 // we release ownership during startup, keep a non-owned reference.
183 const OatFile* oat_file_non_owned_;
184
Narayan Kamath52f84882014-05-02 10:10:39 +0100185 const std::string image_location_;
186
Mathieu Chartier763a31e2015-11-16 16:05:55 -0800187 private:
Ian Rogers1d54e732013-05-02 21:10:01 -0700188 DISALLOW_COPY_AND_ASSIGN(ImageSpace);
189};
190
191} // namespace space
192} // namespace gc
193} // namespace art
194
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700195#endif // ART_RUNTIME_GC_SPACE_IMAGE_SPACE_H_