Home | History | Annotate | Download | only in space
      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:
     34   SpaceType GetType() const {
     35     return kSpaceTypeImageSpace;
     36   }
     37 
     38   // Create a 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* Create(const char* image, InstructionSet image_isa, std::string* error_msg)
     47       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
     48 
     49   // Reads the image header from the specified image location for the
     50   // instruction set image_isa or dies trying.
     51   static ImageHeader* ReadImageHeaderOrDie(const char* image_location,
     52                                            InstructionSet image_isa);
     53 
     54   // Reads the image header from the specified image location for the
     55   // instruction set image_isa. Returns nullptr on failure, with
     56   // reason in error_msg.
     57   static ImageHeader* ReadImageHeader(const char* image_location,
     58                                       InstructionSet image_isa,
     59                                       std::string* error_msg);
     60 
     61   // Give access to the OatFile.
     62   const OatFile* GetOatFile() const;
     63 
     64   // Releases the OatFile from the ImageSpace so it can be transfer to
     65   // the caller, presumably the ClassLinker.
     66   OatFile* ReleaseOatFile()
     67       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
     68 
     69   void VerifyImageAllocations()
     70       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
     71 
     72   const ImageHeader& GetImageHeader() const {
     73     return *reinterpret_cast<ImageHeader*>(Begin());
     74   }
     75 
     76   // Actual filename where image was loaded from.
     77   // For example: /data/dalvik-cache/arm/system@framework (at) boot.art
     78   const std::string GetImageFilename() const {
     79     return GetName();
     80   }
     81 
     82   // Symbolic location for image.
     83   // For example: /system/framework/boot.art
     84   const std::string GetImageLocation() const {
     85     return image_location_;
     86   }
     87 
     88   accounting::ContinuousSpaceBitmap* GetLiveBitmap() const OVERRIDE {
     89     return live_bitmap_.get();
     90   }
     91 
     92   accounting::ContinuousSpaceBitmap* GetMarkBitmap() const OVERRIDE {
     93     // ImageSpaces have the same bitmap for both live and marked. This helps reduce the number of
     94     // special cases to test against.
     95     return live_bitmap_.get();
     96   }
     97 
     98   void Dump(std::ostream& os) const;
     99 
    100   // Sweeping image spaces is a NOP.
    101   void Sweep(bool /* swap_bitmaps */, size_t* /* freed_objects */, size_t* /* freed_bytes */) {
    102   }
    103 
    104   bool CanMoveObjects() const OVERRIDE {
    105     return false;
    106   }
    107 
    108   // Returns the filename of the image corresponding to
    109   // requested image_location, or the filename where a new image
    110   // should be written if one doesn't exist. Looks for a generated
    111   // image in the specified location and then in the dalvik-cache.
    112   //
    113   // Returns true if an image was found, false otherwise.
    114   static bool FindImageFilename(const char* image_location,
    115                                 InstructionSet image_isa,
    116                                 std::string* system_location,
    117                                 bool* has_system,
    118                                 std::string* data_location,
    119                                 bool* dalvik_cache_exists,
    120                                 bool* has_data,
    121                                 bool *is_global_cache);
    122 
    123  private:
    124   // Tries to initialize an ImageSpace from the given image path,
    125   // returning NULL on error.
    126   //
    127   // If validate_oat_file is false (for /system), do not verify that
    128   // image's OatFile is up-to-date relative to its DexFile
    129   // inputs. Otherwise (for /data), validate the inputs and generate
    130   // the OatFile in /data/dalvik-cache if necessary.
    131   static ImageSpace* Init(const char* image_filename, const char* image_location,
    132                           bool validate_oat_file, std::string* error_msg)
    133       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    134 
    135   OatFile* OpenOatFile(const char* image, std::string* error_msg) const
    136       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    137 
    138   bool ValidateOatFile(std::string* error_msg) const
    139       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
    140 
    141   friend class Space;
    142 
    143   static Atomic<uint32_t> bitmap_index_;
    144 
    145   std::unique_ptr<accounting::ContinuousSpaceBitmap> live_bitmap_;
    146 
    147   ImageSpace(const std::string& name, const char* image_location,
    148              MemMap* mem_map, accounting::ContinuousSpaceBitmap* live_bitmap);
    149 
    150   // The OatFile associated with the image during early startup to
    151   // reserve space contiguous to the image. It is later released to
    152   // the ClassLinker during it's initialization.
    153   std::unique_ptr<OatFile> oat_file_;
    154 
    155   const std::string image_location_;
    156 
    157   DISALLOW_COPY_AND_ASSIGN(ImageSpace);
    158 };
    159 
    160 }  // namespace space
    161 }  // namespace gc
    162 }  // namespace art
    163 
    164 #endif  // ART_RUNTIME_GC_SPACE_IMAGE_SPACE_H_
    165