| 202 | } |
| 203 | |
| 204 | void PoolAllocator::EvictOne() { |
| 205 | DCHECK(lru_tail_ != nullptr); |
| 206 | PtrRecord* prec = lru_tail_; |
| 207 | RemoveFromList(prec); |
| 208 | auto iter = pool_.find(prec->num_bytes); |
| 209 | while (iter->second != prec) { |
| 210 | ++iter; |
| 211 | DCHECK(iter != pool_.end()); |
| 212 | } |
| 213 | pool_.erase(iter); |
| 214 | allocator_->Free(prec->ptr, prec->num_bytes); |
| 215 | delete prec; |
| 216 | ++evicted_count_; |
| 217 | // Auto-resizing, and warning messages. |
| 218 | static const double kTolerable = 2e-3; |
| 219 | static const int kCheckInterval = 1000; |
| 220 | static const double kIncreaseFactor = 1.1; |
| 221 | static const int kMinPoolSize = 100; |
| 222 | if (0 == evicted_count_ % kCheckInterval) { |
| 223 | const double eviction_rate = |
| 224 | evicted_count_ / static_cast<double>(put_count_); |
| 225 | const int64 alloc_request_count = allocated_count_ + get_from_pool_count_; |
| 226 | const double alloc_rate = |
| 227 | (alloc_request_count == 0) |
| 228 | ? 0.0 |
| 229 | : allocated_count_ / static_cast<double>(alloc_request_count); |
| 230 | // Can turn on for debugging purposes. |
| 231 | const bool kShouldLog = false; |
| 232 | if (kShouldLog) { |
| 233 | LOG(INFO) << "PoolAllocator: After " << alloc_request_count |
| 234 | << " get requests, put_count=" << put_count_ |
| 235 | << " evicted_count=" << evicted_count_ |
| 236 | << " eviction_rate=" << eviction_rate |
| 237 | << " and unsatisfied allocation rate=" << alloc_rate; |
| 238 | } |
| 239 | if (auto_resize_ && (eviction_rate > kTolerable) && |
| 240 | (alloc_rate > kTolerable)) { |
| 241 | size_t new_size_limit = (pool_size_limit_ < kMinPoolSize) |
| 242 | ? kMinPoolSize |
| 243 | : (kIncreaseFactor * pool_size_limit_); |
| 244 | if (kShouldLog) { |
| 245 | LOG(INFO) << "Raising pool_size_limit_ from " << pool_size_limit_ |
| 246 | << " to " << new_size_limit; |
| 247 | } |
| 248 | pool_size_limit_ = new_size_limit; |
| 249 | // Reset all the counters so that ratios are relative to new sizes |
| 250 | // at next test interval. |
| 251 | put_count_ = 0; |
| 252 | allocated_count_ = 0; |
| 253 | evicted_count_ = 0; |
| 254 | get_from_pool_count_ = 0; |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | void* BasicCPUAllocator::Alloc(size_t alignment, size_t num_bytes) { |
| 260 | void* ptr = nullptr; |