Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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_MONITOR_POOL_H_ |
| 18 | #define ART_RUNTIME_MONITOR_POOL_H_ |
| 19 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 20 | #include "monitor.h" |
| 21 | |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 22 | #include "base/allocator.h" |
Ian Rogers | 44d6ff1 | 2014-03-06 23:11:11 -0800 | [diff] [blame] | 23 | #ifdef __LP64__ |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 24 | #include <stdint.h> |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 25 | #include "atomic.h" |
Ian Rogers | 44d6ff1 | 2014-03-06 23:11:11 -0800 | [diff] [blame] | 26 | #include "runtime.h" |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 27 | #else |
| 28 | #include "base/stl_util.h" // STLDeleteElements |
Ian Rogers | 44d6ff1 | 2014-03-06 23:11:11 -0800 | [diff] [blame] | 29 | #endif |
| 30 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 31 | namespace art { |
| 32 | |
| 33 | // Abstraction to keep monitors small enough to fit in a lock word (32bits). On 32bit systems the |
| 34 | // monitor id loses the alignment bits of the Monitor*. |
| 35 | class MonitorPool { |
| 36 | public: |
| 37 | static MonitorPool* Create() { |
| 38 | #ifndef __LP64__ |
| 39 | return nullptr; |
| 40 | #else |
| 41 | return new MonitorPool(); |
| 42 | #endif |
| 43 | } |
| 44 | |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 45 | static Monitor* CreateMonitor(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code) |
| 46 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 47 | #ifndef __LP64__ |
| 48 | return new Monitor(self, owner, obj, hash_code); |
| 49 | #else |
| 50 | return GetMonitorPool()->CreateMonitorInPool(self, owner, obj, hash_code); |
| 51 | #endif |
| 52 | } |
| 53 | |
| 54 | static void ReleaseMonitor(Thread* self, Monitor* monitor) { |
| 55 | #ifndef __LP64__ |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 56 | UNUSED(self); |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 57 | delete monitor; |
| 58 | #else |
| 59 | GetMonitorPool()->ReleaseMonitorToPool(self, monitor); |
| 60 | #endif |
| 61 | } |
| 62 | |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 63 | static void ReleaseMonitors(Thread* self, MonitorList::Monitors* monitors) { |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 64 | #ifndef __LP64__ |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 65 | UNUSED(self); |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 66 | STLDeleteElements(monitors); |
| 67 | #else |
| 68 | GetMonitorPool()->ReleaseMonitorsToPool(self, monitors); |
| 69 | #endif |
| 70 | } |
| 71 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 72 | static Monitor* MonitorFromMonitorId(MonitorId mon_id) { |
| 73 | #ifndef __LP64__ |
| 74 | return reinterpret_cast<Monitor*>(mon_id << 3); |
| 75 | #else |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 76 | return GetMonitorPool()->LookupMonitor(mon_id); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 77 | #endif |
| 78 | } |
| 79 | |
| 80 | static MonitorId MonitorIdFromMonitor(Monitor* mon) { |
| 81 | #ifndef __LP64__ |
| 82 | return reinterpret_cast<MonitorId>(mon) >> 3; |
| 83 | #else |
| 84 | return mon->GetMonitorId(); |
| 85 | #endif |
| 86 | } |
| 87 | |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 88 | static MonitorId ComputeMonitorId(Monitor* mon, Thread* self) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 89 | #ifndef __LP64__ |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 90 | UNUSED(self); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 91 | return MonitorIdFromMonitor(mon); |
| 92 | #else |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 93 | return GetMonitorPool()->ComputeMonitorIdInPool(mon, self); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 94 | #endif |
| 95 | } |
| 96 | |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 97 | static MonitorPool* GetMonitorPool() { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 98 | #ifndef __LP64__ |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 99 | return nullptr; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 100 | #else |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 101 | return Runtime::Current()->GetMonitorPool(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 102 | #endif |
| 103 | } |
| 104 | |
| 105 | private: |
| 106 | #ifdef __LP64__ |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 107 | // When we create a monitor pool, threads have not been initialized, yet, so ignore thread-safety |
| 108 | // analysis. |
| 109 | MonitorPool() NO_THREAD_SAFETY_ANALYSIS; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 110 | |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 111 | void AllocateChunk() EXCLUSIVE_LOCKS_REQUIRED(Locks::allocated_monitor_ids_lock_); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 112 | |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 113 | Monitor* CreateMonitorInPool(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code) |
| 114 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 115 | |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 116 | void ReleaseMonitorToPool(Thread* self, Monitor* monitor); |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 117 | void ReleaseMonitorsToPool(Thread* self, MonitorList::Monitors* monitors); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 118 | |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 119 | // Note: This is safe as we do not ever move chunks. |
| 120 | Monitor* LookupMonitor(MonitorId mon_id) { |
| 121 | size_t offset = MonitorIdToOffset(mon_id); |
| 122 | size_t index = offset / kChunkSize; |
| 123 | size_t offset_in_chunk = offset % kChunkSize; |
| 124 | uintptr_t base = *(monitor_chunks_.LoadRelaxed()+index); |
| 125 | return reinterpret_cast<Monitor*>(base + offset_in_chunk); |
| 126 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 127 | |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 128 | static bool IsInChunk(uintptr_t base_addr, Monitor* mon) { |
| 129 | uintptr_t mon_ptr = reinterpret_cast<uintptr_t>(mon); |
| 130 | return base_addr <= mon_ptr && (mon_ptr - base_addr < kChunkSize); |
| 131 | } |
| 132 | |
| 133 | // Note: This is safe as we do not ever move chunks. |
| 134 | MonitorId ComputeMonitorIdInPool(Monitor* mon, Thread* self) { |
| 135 | MutexLock mu(self, *Locks::allocated_monitor_ids_lock_); |
| 136 | for (size_t index = 0; index < num_chunks_; ++index) { |
| 137 | uintptr_t chunk_addr = *(monitor_chunks_.LoadRelaxed() + index); |
| 138 | if (IsInChunk(chunk_addr, mon)) { |
| 139 | return OffsetToMonitorId(reinterpret_cast<uintptr_t>(mon) - chunk_addr + index * kChunkSize); |
| 140 | } |
| 141 | } |
| 142 | LOG(FATAL) << "Did not find chunk that contains monitor."; |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | static size_t MonitorIdToOffset(MonitorId id) { |
| 147 | return id << 3; |
| 148 | } |
| 149 | |
| 150 | static MonitorId OffsetToMonitorId(size_t offset) { |
| 151 | return static_cast<MonitorId>(offset >> 3); |
| 152 | } |
| 153 | |
| 154 | // TODO: There are assumptions in the code that monitor addresses are 8B aligned (>>3). |
| 155 | static constexpr size_t kMonitorAlignment = 8; |
| 156 | // Size of a monitor, rounded up to a multiple of alignment. |
| 157 | static constexpr size_t kAlignedMonitorSize = (sizeof(Monitor) + kMonitorAlignment - 1) & |
| 158 | -kMonitorAlignment; |
| 159 | // As close to a page as we can get seems a good start. |
| 160 | static constexpr size_t kChunkCapacity = kPageSize / kAlignedMonitorSize; |
| 161 | // Chunk size that is referenced in the id. We can collapse this to the actually used storage |
| 162 | // in a chunk, i.e., kChunkCapacity * kAlignedMonitorSize, but this will mean proper divisions. |
| 163 | static constexpr size_t kChunkSize = kPageSize; |
| 164 | // The number of initial chunks storable in monitor_chunks_. The number is large enough to make |
| 165 | // resizing unlikely, but small enough to not waste too much memory. |
| 166 | static constexpr size_t kInitialChunkStorage = 8U; |
| 167 | |
| 168 | // List of memory chunks. Each chunk is kChunkSize. |
| 169 | Atomic<uintptr_t*> monitor_chunks_; |
| 170 | // Number of chunks stored. |
| 171 | size_t num_chunks_ GUARDED_BY(Locks::allocated_monitor_ids_lock_); |
| 172 | // Number of chunks storable. |
| 173 | size_t capacity_ GUARDED_BY(Locks::allocated_monitor_ids_lock_); |
| 174 | |
| 175 | // To avoid race issues when resizing, we keep all the previous arrays. |
| 176 | std::vector<uintptr_t*> old_chunk_arrays_ GUARDED_BY(Locks::allocated_monitor_ids_lock_); |
| 177 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 178 | typedef TrackingAllocator<uint8_t, kAllocatorTagMonitorPool> Allocator; |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 179 | Allocator allocator_; |
| 180 | |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 181 | // Start of free list of monitors. |
| 182 | // Note: these point to the right memory regions, but do *not* denote initialized objects. |
| 183 | Monitor* first_free_ GUARDED_BY(Locks::allocated_monitor_ids_lock_); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 184 | #endif |
| 185 | }; |
| 186 | |
| 187 | } // namespace art |
| 188 | |
| 189 | #endif // ART_RUNTIME_MONITOR_POOL_H_ |