• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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/trace_processor/containers/string_pool.h"
18 
19 #include <limits>
20 
21 #include "perfetto/base/logging.h"
22 #include "perfetto/ext/base/utils.h"
23 
24 namespace perfetto {
25 namespace trace_processor {
26 
27 // static
28 constexpr size_t StringPool::kNumBlockIndexBits;
29 // static
30 constexpr size_t StringPool::kNumBlockOffsetBits;
31 // static
32 constexpr size_t StringPool::kLargeStringFlagBitMask;
33 // static
34 constexpr size_t StringPool::kBlockOffsetBitMask;
35 // static
36 constexpr size_t StringPool::kBlockIndexBitMask;
37 // static
38 constexpr size_t StringPool::kBlockSizeBytes;
39 // static
40 constexpr size_t StringPool::kMinLargeStringSizeBytes;
41 
StringPool()42 StringPool::StringPool() {
43   static_assert(
44       StringPool::kMinLargeStringSizeBytes <= StringPool::kBlockSizeBytes + 1,
45       "minimum size of large strings must be small enough to support any "
46       "string that doesn't fit in a Block.");
47 
48   blocks_.emplace_back(kBlockSizeBytes);
49 
50   // Reserve a slot for the null string.
51   PERFETTO_CHECK(blocks_.back().TryInsert(NullTermStringView()).first);
52 }
53 
54 StringPool::~StringPool() = default;
55 
56 StringPool::StringPool(StringPool&&) noexcept = default;
57 StringPool& StringPool::operator=(StringPool&&) noexcept = default;
58 
InsertString(base::StringView str,uint64_t hash)59 StringPool::Id StringPool::InsertString(base::StringView str, uint64_t hash) {
60   // Try and find enough space in the current block for the string and the
61   // metadata (varint-encoded size + the string data + the null terminator).
62   bool success;
63   uint32_t offset;
64   std::tie(success, offset) = blocks_.back().TryInsert(str);
65   if (PERFETTO_UNLIKELY(!success)) {
66     // The block did not have enough space for the string. If the string is
67     // large, add it into the |large_strings_| vector, to avoid discarding a
68     // large portion of the current block's memory. This also enables us to
69     // support strings that wouldn't fit into a single block. Otherwise, add a
70     // new block to store the string.
71     if (str.size() + kMaxMetadataSize >= kMinLargeStringSizeBytes) {
72       return InsertLargeString(str, hash);
73     }
74     blocks_.emplace_back(kBlockSizeBytes);
75 
76     // Try and reserve space again - this time we should definitely succeed.
77     std::tie(success, offset) = blocks_.back().TryInsert(str);
78     PERFETTO_CHECK(success);
79   }
80 
81   // Compute the id from the block index and offset and add a mapping from the
82   // hash to the id.
83   Id string_id = Id::BlockString(blocks_.size() - 1, offset);
84 
85   // Deliberately not adding |string_id| to |string_index_|. The caller
86   // (InternString()) must take care of this.
87   PERFETTO_DCHECK(string_index_.Find(hash));
88 
89   return string_id;
90 }
91 
InsertLargeString(base::StringView str,uint64_t hash)92 StringPool::Id StringPool::InsertLargeString(base::StringView str,
93                                              uint64_t hash) {
94   large_strings_.emplace_back(new std::string(str.begin(), str.size()));
95   // Compute id from the index and add a mapping from the hash to the id.
96   Id string_id = Id::LargeString(large_strings_.size() - 1);
97 
98   // Deliberately not adding |string_id| to |string_index_|. The caller
99   // (InternString()) must take care of this.
100   PERFETTO_DCHECK(string_index_.Find(hash));
101 
102   return string_id;
103 }
104 
TryInsert(base::StringView str)105 std::pair<bool /*success*/, uint32_t /*offset*/> StringPool::Block::TryInsert(
106     base::StringView str) {
107   auto str_size = str.size();
108   size_t max_pos = static_cast<size_t>(pos_) + str_size + kMaxMetadataSize;
109   if (max_pos > size_)
110     return std::make_pair(false, 0u);
111 
112   // Ensure that we commit up until the end of the string to memory.
113   mem_.EnsureCommitted(max_pos);
114 
115   // Get where we should start writing this string.
116   uint32_t offset = pos_;
117   uint8_t* begin = Get(offset);
118 
119   // First write the size of the string using varint encoding.
120   uint8_t* end = protozero::proto_utils::WriteVarInt(str_size, begin);
121 
122   // Next the string itself.
123   if (PERFETTO_LIKELY(str_size > 0)) {
124     memcpy(end, str.data(), str_size);
125     end += str_size;
126   }
127 
128   // Finally add a null terminator.
129   *(end++) = '\0';
130 
131   // Update the end of the block and return the pointer to the string.
132   pos_ = OffsetOf(end);
133 
134   return std::make_pair(true, offset);
135 }
136 
Iterator(const StringPool * pool)137 StringPool::Iterator::Iterator(const StringPool* pool) : pool_(pool) {}
138 
operator ++()139 StringPool::Iterator& StringPool::Iterator::operator++() {
140   if (block_index_ < pool_->blocks_.size()) {
141     // Try and go to the next string in the current block.
142     const auto& block = pool_->blocks_[block_index_];
143 
144     // Find the size of the string at the current offset in the block
145     // and increment the offset by that size.
146     uint32_t str_size = 0;
147     const uint8_t* ptr = block.Get(block_offset_);
148     ptr = ReadSize(ptr, &str_size);
149     ptr += str_size + 1;
150     block_offset_ = block.OffsetOf(ptr);
151 
152     // If we're out of bounds for this block, go to the start of the next block.
153     if (block.pos() <= block_offset_) {
154       block_index_++;
155       block_offset_ = 0;
156     }
157 
158     return *this;
159   }
160 
161   // Advance to the next string from |large_strings_|.
162   PERFETTO_DCHECK(large_strings_index_ < pool_->large_strings_.size());
163   large_strings_index_++;
164   return *this;
165 }
166 
operator bool() const167 StringPool::Iterator::operator bool() const {
168   return block_index_ < pool_->blocks_.size() ||
169          large_strings_index_ < pool_->large_strings_.size();
170 }
171 
StringView()172 NullTermStringView StringPool::Iterator::StringView() {
173   return pool_->Get(StringId());
174 }
175 
StringId()176 StringPool::Id StringPool::Iterator::StringId() {
177   if (block_index_ < pool_->blocks_.size()) {
178     PERFETTO_DCHECK(block_offset_ < pool_->blocks_[block_index_].pos());
179 
180     // If we're at (0, 0), we have the null string which has id 0.
181     if (block_index_ == 0 && block_offset_ == 0)
182       return Id::Null();
183     return Id::BlockString(block_index_, block_offset_);
184   }
185   PERFETTO_DCHECK(large_strings_index_ < pool_->large_strings_.size());
186   return Id::LargeString(large_strings_index_);
187 }
188 
189 }  // namespace trace_processor
190 }  // namespace perfetto
191