• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 #include "src/traced/probes/ftrace/page_pool.h"
18 
19 #include <array>
20 
21 namespace perfetto {
22 
23 namespace {
24 constexpr size_t kMaxFreelistBlocks = 128;  // 128 * 32 * 4KB = 16MB.
25 }
26 
NewPageBlock()27 void PagePool::NewPageBlock() {
28   std::lock_guard<std::mutex> lock(mutex_);
29   if (freelist_.empty()) {
30     write_queue_.emplace_back(PageBlock::Create());
31   } else {
32     write_queue_.emplace_back(std::move(freelist_.back()));
33     freelist_.pop_back();
34   }
35   PERFETTO_DCHECK(write_queue_.back().size() == 0);
36 }
37 
EndRead(std::vector<PageBlock> page_blocks)38 void PagePool::EndRead(std::vector<PageBlock> page_blocks) {
39   PERFETTO_DCHECK_THREAD(reader_thread_);
40   for (PageBlock& page_block : page_blocks)
41     page_block.Clear();
42 
43   std::lock_guard<std::mutex> lock(mutex_);
44   freelist_.insert(freelist_.end(),
45                    std::make_move_iterator(page_blocks.begin()),
46                    std::make_move_iterator(page_blocks.end()));
47 
48   // Even if blocks in the freelist don't waste any resident memory (because
49   // the Clear() call above madvise()s them) let's avoid that in pathological
50   // cases we keep accumulating virtual address space reservations.
51   if (freelist_.size() > kMaxFreelistBlocks)
52     freelist_.erase(freelist_.begin() + kMaxFreelistBlocks, freelist_.end());
53 }
54 
55 }  // namespace perfetto
56