1ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers/*
2ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers * Copyright (C) 2014 The Android Open Source Project
3ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers *
4ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers * Licensed under the Apache License, Version 2.0 (the "License");
5ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers * you may not use this file except in compliance with the License.
6ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers * You may obtain a copy of the License at
7ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers *
8ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers *      http://www.apache.org/licenses/LICENSE-2.0
9ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers *
10ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers * Unless required by applicable law or agreed to in writing, software
11ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers * distributed under the License is distributed on an "AS IS" BASIS,
12ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers * See the License for the specific language governing permissions and
14ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers * limitations under the License.
15ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers */
16ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers
17ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers#include "monitor_pool.h"
18ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers
19ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers#include "base/logging.h"
20ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers#include "base/mutex-inl.h"
2144d6ff197b340b5ac2a4094db148b39c366317ddIan Rogers#include "thread-inl.h"
22ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers#include "monitor.h"
23ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers
24ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersnamespace art {
25ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers
2674240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampenamespace mirror {
2774240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  class Object;
2874240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe}  // namespace mirror
29ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers
3074240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas GampeMonitorPool::MonitorPool()
318dab193044c74755a6b78f5f74b844d559746aedHans Boehm    : current_chunk_list_index_(0), num_chunks_(0), current_chunk_list_capacity_(0),
328dab193044c74755a6b78f5f74b844d559746aedHans Boehm    first_free_(nullptr) {
338dab193044c74755a6b78f5f74b844d559746aedHans Boehm  for (size_t i = 0; i < kMaxChunkLists; ++i) {
348dab193044c74755a6b78f5f74b844d559746aedHans Boehm    monitor_chunks_[i] = nullptr;  // Not absolutely required, but ...
358dab193044c74755a6b78f5f74b844d559746aedHans Boehm  }
3674240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  AllocateChunk();  // Get our first chunk.
37ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers}
38ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers
3974240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe// Assumes locks are held appropriately when necessary.
4074240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe// We do not need a lock in the constructor, but we need one when in CreateMonitorInPool.
4174240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampevoid MonitorPool::AllocateChunk() {
4274240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  DCHECK(first_free_ == nullptr);
4374240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe
448dab193044c74755a6b78f5f74b844d559746aedHans Boehm  // Do we need to allocate another chunk list?
458dab193044c74755a6b78f5f74b844d559746aedHans Boehm  if (num_chunks_ == current_chunk_list_capacity_) {
468dab193044c74755a6b78f5f74b844d559746aedHans Boehm    if (current_chunk_list_capacity_ != 0U) {
478dab193044c74755a6b78f5f74b844d559746aedHans Boehm      ++current_chunk_list_index_;
488dab193044c74755a6b78f5f74b844d559746aedHans Boehm      CHECK_LT(current_chunk_list_index_, kMaxChunkLists) << "Out of space for inflated monitors";
498dab193044c74755a6b78f5f74b844d559746aedHans Boehm      VLOG(monitor) << "Expanding to capacity "
508dab193044c74755a6b78f5f74b844d559746aedHans Boehm          << 2 * ChunkListCapacity(current_chunk_list_index_) - kInitialChunkStorage;
518dab193044c74755a6b78f5f74b844d559746aedHans Boehm    }  // else we're initializing
528dab193044c74755a6b78f5f74b844d559746aedHans Boehm    current_chunk_list_capacity_ = ChunkListCapacity(current_chunk_list_index_);
538dab193044c74755a6b78f5f74b844d559746aedHans Boehm    uintptr_t* new_list = new uintptr_t[current_chunk_list_capacity_]();
548dab193044c74755a6b78f5f74b844d559746aedHans Boehm    DCHECK(monitor_chunks_[current_chunk_list_index_] == nullptr);
558dab193044c74755a6b78f5f74b844d559746aedHans Boehm    monitor_chunks_[current_chunk_list_index_] = new_list;
568dab193044c74755a6b78f5f74b844d559746aedHans Boehm    num_chunks_ = 0;
57ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  }
5874240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe
5974240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  // Allocate the chunk.
60bad0267eaab9d6a522d05469ff90501deefdb88bMathieu Chartier  void* chunk = allocator_.allocate(kChunkSize);
6174240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  // Check we allocated memory.
6274240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  CHECK_NE(reinterpret_cast<uintptr_t>(nullptr), reinterpret_cast<uintptr_t>(chunk));
6374240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  // Check it is aligned as we need it.
6474240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  CHECK_EQ(0U, reinterpret_cast<uintptr_t>(chunk) % kMonitorAlignment);
6574240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe
6674240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  // Add the chunk.
678dab193044c74755a6b78f5f74b844d559746aedHans Boehm  monitor_chunks_[current_chunk_list_index_][num_chunks_] = reinterpret_cast<uintptr_t>(chunk);
6874240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  num_chunks_++;
6974240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe
7074240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  // Set up the free list
7174240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  Monitor* last = reinterpret_cast<Monitor*>(reinterpret_cast<uintptr_t>(chunk) +
7274240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe                                             (kChunkCapacity - 1) * kAlignedMonitorSize);
7374240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  last->next_free_ = nullptr;
7474240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  // Eagerly compute id.
758dab193044c74755a6b78f5f74b844d559746aedHans Boehm  last->monitor_id_ = OffsetToMonitorId(current_chunk_list_index_* (kMaxListSize * kChunkSize)
768dab193044c74755a6b78f5f74b844d559746aedHans Boehm      + (num_chunks_ - 1) * kChunkSize + (kChunkCapacity - 1) * kAlignedMonitorSize);
7774240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  for (size_t i = 0; i < kChunkCapacity - 1; ++i) {
7874240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe    Monitor* before = reinterpret_cast<Monitor*>(reinterpret_cast<uintptr_t>(last) -
7974240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe                                                 kAlignedMonitorSize);
8074240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe    before->next_free_ = last;
8174240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe    // Derive monitor_id from last.
8274240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe    before->monitor_id_ = OffsetToMonitorId(MonitorIdToOffset(last->monitor_id_) -
8374240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe                                            kAlignedMonitorSize);
8474240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe
8574240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe    last = before;
8674240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  }
8774240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  DCHECK(last == reinterpret_cast<Monitor*>(chunk));
8874240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  first_free_ = last;
8974240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe}
9074240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe
91057134bdf40981555a8bf56ab8d703a503b40f8fAndreas Gampevoid MonitorPool::FreeInternal() {
92057134bdf40981555a8bf56ab8d703a503b40f8fAndreas Gampe  // This is on shutdown with NO_THREAD_SAFETY_ANALYSIS, can't/don't need to lock.
938dab193044c74755a6b78f5f74b844d559746aedHans Boehm  DCHECK_NE(current_chunk_list_capacity_, 0UL);
948dab193044c74755a6b78f5f74b844d559746aedHans Boehm  for (size_t i = 0; i <= current_chunk_list_index_; ++i) {
958dab193044c74755a6b78f5f74b844d559746aedHans Boehm    DCHECK_NE(monitor_chunks_[i], static_cast<uintptr_t*>(nullptr));
968dab193044c74755a6b78f5f74b844d559746aedHans Boehm    for (size_t j = 0; j < ChunkListCapacity(i); ++j) {
978dab193044c74755a6b78f5f74b844d559746aedHans Boehm      if (i < current_chunk_list_index_ || j < num_chunks_) {
988dab193044c74755a6b78f5f74b844d559746aedHans Boehm        DCHECK_NE(monitor_chunks_[i][j], 0U);
998dab193044c74755a6b78f5f74b844d559746aedHans Boehm        allocator_.deallocate(reinterpret_cast<uint8_t*>(monitor_chunks_[i][j]), kChunkSize);
1008dab193044c74755a6b78f5f74b844d559746aedHans Boehm      } else {
1018dab193044c74755a6b78f5f74b844d559746aedHans Boehm        DCHECK_EQ(monitor_chunks_[i][j], 0U);
1028dab193044c74755a6b78f5f74b844d559746aedHans Boehm      }
103057134bdf40981555a8bf56ab8d703a503b40f8fAndreas Gampe    }
1048dab193044c74755a6b78f5f74b844d559746aedHans Boehm    delete[] monitor_chunks_[i];
105057134bdf40981555a8bf56ab8d703a503b40f8fAndreas Gampe  }
106057134bdf40981555a8bf56ab8d703a503b40f8fAndreas Gampe}
107057134bdf40981555a8bf56ab8d703a503b40f8fAndreas Gampe
10874240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas GampeMonitor* MonitorPool::CreateMonitorInPool(Thread* self, Thread* owner, mirror::Object* obj,
10974240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe                                          int32_t hash_code)
11090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier    SHARED_REQUIRES(Locks::mutator_lock_) {
11174240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  // We are gonna allocate, so acquire the writer lock.
11274240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  MutexLock mu(self, *Locks::allocated_monitor_ids_lock_);
11374240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe
11474240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  // Enough space, or need to resize?
11574240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  if (first_free_ == nullptr) {
1162c26501d24d929abe096ecce44f91410290b33c0Mathieu Chartier    VLOG(monitor) << "Allocating a new chunk.";
11774240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe    AllocateChunk();
11874240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  }
11974240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe
12074240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  Monitor* mon_uninitialized = first_free_;
12174240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  first_free_ = first_free_->next_free_;
12274240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe
12374240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  // Pull out the id which was preinitialized.
12474240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  MonitorId id = mon_uninitialized->monitor_id_;
12574240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe
12674240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  // Initialize it.
12774240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  Monitor* monitor = new(mon_uninitialized) Monitor(self, owner, obj, hash_code, id);
12874240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe
12974240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  return monitor;
130ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers}
131ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers
13274240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampevoid MonitorPool::ReleaseMonitorToPool(Thread* self, Monitor* monitor) {
13374240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  // Might be racy with allocation, so acquire lock.
13474240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  MutexLock mu(self, *Locks::allocated_monitor_ids_lock_);
13574240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe
13674240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  // Keep the monitor id. Don't trust it's not cleared.
13774240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  MonitorId id = monitor->monitor_id_;
13874240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe
13974240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  // Call the destructor.
14074240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  // TODO: Exception safety?
14174240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  monitor->~Monitor();
14274240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe
14374240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  // Add to the head of the free list.
14474240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  monitor->next_free_ = first_free_;
14574240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  first_free_ = monitor;
14674240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe
14774240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  // Rewrite monitor id.
14874240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  monitor->monitor_id_ = id;
14974240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe}
15074240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe
151bad0267eaab9d6a522d05469ff90501deefdb88bMathieu Chartiervoid MonitorPool::ReleaseMonitorsToPool(Thread* self, MonitorList::Monitors* monitors) {
15274240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  for (Monitor* mon : *monitors) {
15374240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe    ReleaseMonitorToPool(self, mon);
15474240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  }
155ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers}
156ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers
157ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers}  // namespace art
158