Home | History | Annotate | Download | only in space
      1 /*
      2  * Copyright (C) 2013 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_ROSALLOC_SPACE_H_
     18 #define ART_RUNTIME_GC_SPACE_ROSALLOC_SPACE_H_
     19 
     20 #include "gc/allocator/rosalloc.h"
     21 #include "malloc_space.h"
     22 #include "space.h"
     23 
     24 namespace art {
     25 namespace gc {
     26 
     27 namespace collector {
     28 class MarkSweep;
     29 }  // namespace collector
     30 
     31 namespace space {
     32 
     33 // An alloc space implemented using a runs-of-slots memory allocator. Not final as may be
     34 // overridden by a MemoryToolMallocSpace.
     35 class RosAllocSpace : public MallocSpace {
     36  public:
     37   // Create a RosAllocSpace with the requested sizes. The requested
     38   // base address is not guaranteed to be granted, if it is required,
     39   // the caller should call Begin on the returned space to confirm the
     40   // request was granted.
     41   static RosAllocSpace* Create(const std::string& name,
     42                                size_t initial_size,
     43                                size_t growth_limit,
     44                                size_t capacity,
     45                                bool low_memory_mode,
     46                                bool can_move_objects);
     47   static RosAllocSpace* CreateFromMemMap(MemMap&& mem_map,
     48                                          const std::string& name,
     49                                          size_t starting_size,
     50                                          size_t initial_size,
     51                                          size_t growth_limit,
     52                                          size_t capacity,
     53                                          bool low_memory_mode,
     54                                          bool can_move_objects);
     55 
     56   mirror::Object* AllocWithGrowth(Thread* self, size_t num_bytes, size_t* bytes_allocated,
     57                                   size_t* usable_size, size_t* bytes_tl_bulk_allocated)
     58       override REQUIRES(!lock_);
     59   mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated,
     60                         size_t* usable_size, size_t* bytes_tl_bulk_allocated) override {
     61     return AllocNonvirtual(self, num_bytes, bytes_allocated, usable_size,
     62                            bytes_tl_bulk_allocated);
     63   }
     64   mirror::Object* AllocThreadUnsafe(Thread* self, size_t num_bytes, size_t* bytes_allocated,
     65                                     size_t* usable_size, size_t* bytes_tl_bulk_allocated)
     66       override REQUIRES(Locks::mutator_lock_) {
     67     return AllocNonvirtualThreadUnsafe(self, num_bytes, bytes_allocated, usable_size,
     68                                        bytes_tl_bulk_allocated);
     69   }
     70   size_t AllocationSize(mirror::Object* obj, size_t* usable_size) override {
     71     return AllocationSizeNonvirtual<true>(obj, usable_size);
     72   }
     73   size_t Free(Thread* self, mirror::Object* ptr) override
     74       REQUIRES_SHARED(Locks::mutator_lock_);
     75   size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) override
     76       REQUIRES_SHARED(Locks::mutator_lock_);
     77 
     78   mirror::Object* AllocNonvirtual(Thread* self, size_t num_bytes, size_t* bytes_allocated,
     79                                   size_t* usable_size, size_t* bytes_tl_bulk_allocated) {
     80     // RosAlloc zeroes memory internally.
     81     return AllocCommon(self, num_bytes, bytes_allocated, usable_size,
     82                        bytes_tl_bulk_allocated);
     83   }
     84   mirror::Object* AllocNonvirtualThreadUnsafe(Thread* self, size_t num_bytes,
     85                                               size_t* bytes_allocated, size_t* usable_size,
     86                                               size_t* bytes_tl_bulk_allocated) {
     87     // RosAlloc zeroes memory internally. Pass in false for thread unsafe.
     88     return AllocCommon<false>(self, num_bytes, bytes_allocated, usable_size,
     89                               bytes_tl_bulk_allocated);
     90   }
     91 
     92   // Returns true if the given allocation request can be allocated in
     93   // an existing thread local run without allocating a new run.
     94   ALWAYS_INLINE bool CanAllocThreadLocal(Thread* self, size_t num_bytes);
     95   // Allocate the given allocation request in an existing thread local
     96   // run without allocating a new run.
     97   ALWAYS_INLINE mirror::Object* AllocThreadLocal(Thread* self, size_t num_bytes,
     98                                                  size_t* bytes_allocated);
     99   size_t MaxBytesBulkAllocatedFor(size_t num_bytes) override {
    100     return MaxBytesBulkAllocatedForNonvirtual(num_bytes);
    101   }
    102   ALWAYS_INLINE size_t MaxBytesBulkAllocatedForNonvirtual(size_t num_bytes);
    103 
    104   // TODO: NO_THREAD_SAFETY_ANALYSIS because SizeOf() requires that mutator_lock is held.
    105   template<bool kMaybeIsRunningOnMemoryTool>
    106   size_t AllocationSizeNonvirtual(mirror::Object* obj, size_t* usable_size)
    107       NO_THREAD_SAFETY_ANALYSIS;
    108 
    109   allocator::RosAlloc* GetRosAlloc() const {
    110     return rosalloc_;
    111   }
    112 
    113   size_t Trim() override;
    114   void Walk(WalkCallback callback, void* arg) override REQUIRES(!lock_);
    115   size_t GetFootprint() override;
    116   size_t GetFootprintLimit() override;
    117   void SetFootprintLimit(size_t limit) override;
    118 
    119   void Clear() override;
    120 
    121   MallocSpace* CreateInstance(MemMap&& mem_map,
    122                               const std::string& name,
    123                               void* allocator,
    124                               uint8_t* begin,
    125                               uint8_t* end,
    126                               uint8_t* limit,
    127                               size_t growth_limit,
    128                               bool can_move_objects) override;
    129 
    130   uint64_t GetBytesAllocated() override;
    131   uint64_t GetObjectsAllocated() override;
    132 
    133   size_t RevokeThreadLocalBuffers(Thread* thread) override;
    134   size_t RevokeAllThreadLocalBuffers() override;
    135   void AssertThreadLocalBuffersAreRevoked(Thread* thread);
    136   void AssertAllThreadLocalBuffersAreRevoked();
    137 
    138   // Returns the class of a recently freed object.
    139   mirror::Class* FindRecentFreedObject(const mirror::Object* obj);
    140 
    141   bool IsRosAllocSpace() const override {
    142     return true;
    143   }
    144 
    145   RosAllocSpace* AsRosAllocSpace() override {
    146     return this;
    147   }
    148 
    149   void Verify() REQUIRES(Locks::mutator_lock_) {
    150     rosalloc_->Verify();
    151   }
    152 
    153   virtual ~RosAllocSpace();
    154 
    155   void LogFragmentationAllocFailure(std::ostream& os, size_t failed_alloc_bytes) override {
    156     rosalloc_->LogFragmentationAllocFailure(os, failed_alloc_bytes);
    157   }
    158 
    159   void DumpStats(std::ostream& os);
    160 
    161  protected:
    162   RosAllocSpace(MemMap&& mem_map,
    163                 size_t initial_size,
    164                 const std::string& name,
    165                 allocator::RosAlloc* rosalloc,
    166                 uint8_t* begin,
    167                 uint8_t* end,
    168                 uint8_t* limit,
    169                 size_t growth_limit,
    170                 bool can_move_objects,
    171                 size_t starting_size,
    172                 bool low_memory_mode);
    173 
    174  private:
    175   template<bool kThreadSafe = true>
    176   mirror::Object* AllocCommon(Thread* self, size_t num_bytes, size_t* bytes_allocated,
    177                               size_t* usable_size, size_t* bytes_tl_bulk_allocated);
    178 
    179   void* CreateAllocator(void* base, size_t morecore_start, size_t initial_size,
    180                         size_t maximum_size, bool low_memory_mode) override {
    181     return CreateRosAlloc(
    182         base, morecore_start, initial_size, maximum_size, low_memory_mode, kRunningOnMemoryTool);
    183   }
    184   static allocator::RosAlloc* CreateRosAlloc(void* base, size_t morecore_start, size_t initial_size,
    185                                              size_t maximum_size, bool low_memory_mode,
    186                                              bool running_on_memory_tool);
    187 
    188   void InspectAllRosAlloc(void (*callback)(void *start, void *end, size_t num_bytes, void* callback_arg),
    189                           void* arg, bool do_null_callback_at_end)
    190       REQUIRES(!Locks::runtime_shutdown_lock_, !Locks::thread_list_lock_);
    191   void InspectAllRosAllocWithSuspendAll(
    192       void (*callback)(void *start, void *end, size_t num_bytes, void* callback_arg),
    193       void* arg, bool do_null_callback_at_end)
    194       REQUIRES(!Locks::runtime_shutdown_lock_, !Locks::thread_list_lock_);
    195 
    196   // Underlying rosalloc.
    197   allocator::RosAlloc* rosalloc_;
    198 
    199   const bool low_memory_mode_;
    200 
    201   friend class collector::MarkSweep;
    202 
    203   DISALLOW_COPY_AND_ASSIGN(RosAllocSpace);
    204 };
    205 
    206 }  // namespace space
    207 }  // namespace gc
    208 }  // namespace art
    209 
    210 #endif  // ART_RUNTIME_GC_SPACE_ROSALLOC_SPACE_H_
    211