blob: ab0b42a97501ded90439ba38e3e9a5530eaf9143 [file] [log] [blame]
Lalit Maganti8eba3092019-03-27 13:25:29 +00001/*
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
Lalit Maganti9ecfab32019-12-17 17:45:38 +000017#include "src/trace_processor/containers/string_pool.h"
Lalit Maganti8eba3092019-03-27 13:25:29 +000018
Eric Secklerf93b8cf2019-11-20 21:33:01 +000019#include <limits>
Alan Zhao4c2da7d2023-02-27 15:27:01 -080020#include <tuple>
Eric Secklerf93b8cf2019-11-20 21:33:01 +000021
Lalit Maganti8eba3092019-03-27 13:25:29 +000022#include "perfetto/base/logging.h"
Primiano Tucci80bbdd82019-08-30 07:54:04 +020023#include "perfetto/ext/base/utils.h"
Lalit Maganti8eba3092019-03-27 13:25:29 +000024
25namespace perfetto {
26namespace trace_processor {
27
Lalit Maganti03d178e2022-11-03 16:18:51 +000028#if !PERFETTO_IS_AT_LEAST_CPP17()
Eric Seckler56f27db2020-01-15 08:57:17 +000029// static
30constexpr size_t StringPool::kNumBlockIndexBits;
31// static
32constexpr size_t StringPool::kNumBlockOffsetBits;
33// static
34constexpr size_t StringPool::kLargeStringFlagBitMask;
35// static
36constexpr size_t StringPool::kBlockOffsetBitMask;
37// static
38constexpr size_t StringPool::kBlockIndexBitMask;
39// static
40constexpr size_t StringPool::kBlockSizeBytes;
41// static
42constexpr size_t StringPool::kMinLargeStringSizeBytes;
Lalit Maganti03d178e2022-11-03 16:18:51 +000043#endif
Eric Seckler56f27db2020-01-15 08:57:17 +000044
45StringPool::StringPool() {
46 static_assert(
47 StringPool::kMinLargeStringSizeBytes <= StringPool::kBlockSizeBytes + 1,
48 "minimum size of large strings must be small enough to support any "
49 "string that doesn't fit in a Block.");
50
51 blocks_.emplace_back(kBlockSizeBytes);
Lalit Maganti8eba3092019-03-27 13:25:29 +000052
53 // Reserve a slot for the null string.
Eric Seckler56f27db2020-01-15 08:57:17 +000054 PERFETTO_CHECK(blocks_.back().TryInsert(NullTermStringView()).first);
Lalit Maganti8eba3092019-03-27 13:25:29 +000055}
56
57StringPool::~StringPool() = default;
58
Primiano Tuccic986ca12021-11-18 20:51:24 +000059StringPool::StringPool(StringPool&&) noexcept = default;
60StringPool& StringPool::operator=(StringPool&&) noexcept = default;
Lalit Maganti8eba3092019-03-27 13:25:29 +000061
62StringPool::Id StringPool::InsertString(base::StringView str, uint64_t hash) {
Lalit Maganti8eba3092019-03-27 13:25:29 +000063 // Try and find enough space in the current block for the string and the
Mikhail Khokhlov132684e2019-08-14 16:22:22 +010064 // metadata (varint-encoded size + the string data + the null terminator).
Eric Seckler56f27db2020-01-15 08:57:17 +000065 bool success;
66 uint32_t offset;
67 std::tie(success, offset) = blocks_.back().TryInsert(str);
68 if (PERFETTO_UNLIKELY(!success)) {
69 // The block did not have enough space for the string. If the string is
70 // large, add it into the |large_strings_| vector, to avoid discarding a
71 // large portion of the current block's memory. This also enables us to
72 // support strings that wouldn't fit into a single block. Otherwise, add a
73 // new block to store the string.
74 if (str.size() + kMaxMetadataSize >= kMinLargeStringSizeBytes) {
75 return InsertLargeString(str, hash);
Mikhail Khokhlov132684e2019-08-14 16:22:22 +010076 }
Primiano Tuccic986ca12021-11-18 20:51:24 +000077 blocks_.emplace_back(kBlockSizeBytes);
Lalit Maganti8eba3092019-03-27 13:25:29 +000078
79 // Try and reserve space again - this time we should definitely succeed.
Eric Seckler56f27db2020-01-15 08:57:17 +000080 std::tie(success, offset) = blocks_.back().TryInsert(str);
81 PERFETTO_CHECK(success);
Lalit Maganti8eba3092019-03-27 13:25:29 +000082 }
83
Eric Seckler56f27db2020-01-15 08:57:17 +000084 // Compute the id from the block index and offset and add a mapping from the
85 // hash to the id.
Lalit Magantif0599a02020-01-15 15:45:20 +000086 Id string_id = Id::BlockString(blocks_.size() - 1, offset);
Primiano Tuccic986ca12021-11-18 20:51:24 +000087
88 // Deliberately not adding |string_id| to |string_index_|. The caller
89 // (InternString()) must take care of this.
90 PERFETTO_DCHECK(string_index_.Find(hash));
91
Lalit Maganti8eba3092019-03-27 13:25:29 +000092 return string_id;
93}
94
Eric Seckler56f27db2020-01-15 08:57:17 +000095StringPool::Id StringPool::InsertLargeString(base::StringView str,
96 uint64_t hash) {
97 large_strings_.emplace_back(new std::string(str.begin(), str.size()));
98 // Compute id from the index and add a mapping from the hash to the id.
Lalit Magantif0599a02020-01-15 15:45:20 +000099 Id string_id = Id::LargeString(large_strings_.size() - 1);
Primiano Tuccic986ca12021-11-18 20:51:24 +0000100
101 // Deliberately not adding |string_id| to |string_index_|. The caller
102 // (InternString()) must take care of this.
103 PERFETTO_DCHECK(string_index_.Find(hash));
104
Eric Seckler56f27db2020-01-15 08:57:17 +0000105 return string_id;
106}
107
108std::pair<bool /*success*/, uint32_t /*offset*/> StringPool::Block::TryInsert(
109 base::StringView str) {
Lalit Maganti8eba3092019-03-27 13:25:29 +0000110 auto str_size = str.size();
Lalit Maganti40db1512019-11-12 13:27:19 +0000111 size_t max_pos = static_cast<size_t>(pos_) + str_size + kMaxMetadataSize;
112 if (max_pos > size_)
Eric Seckler56f27db2020-01-15 08:57:17 +0000113 return std::make_pair(false, 0u);
Lalit Maganti8eba3092019-03-27 13:25:29 +0000114
Lalit Maganti40db1512019-11-12 13:27:19 +0000115 // Ensure that we commit up until the end of the string to memory.
116 mem_.EnsureCommitted(max_pos);
117
Lalit Maganti8eba3092019-03-27 13:25:29 +0000118 // Get where we should start writing this string.
Eric Seckler56f27db2020-01-15 08:57:17 +0000119 uint32_t offset = pos_;
120 uint8_t* begin = Get(offset);
Lalit Maganti8eba3092019-03-27 13:25:29 +0000121
Mikhail Khokhlov132684e2019-08-14 16:22:22 +0100122 // First write the size of the string using varint encoding.
123 uint8_t* end = protozero::proto_utils::WriteVarInt(str_size, begin);
Lalit Maganti8eba3092019-03-27 13:25:29 +0000124
Mikhail Khokhlov132684e2019-08-14 16:22:22 +0100125 // Next the string itself.
126 if (PERFETTO_LIKELY(str_size > 0)) {
127 memcpy(end, str.data(), str_size);
128 end += str_size;
129 }
Lalit Maganti8eba3092019-03-27 13:25:29 +0000130
131 // Finally add a null terminator.
Mikhail Khokhlov132684e2019-08-14 16:22:22 +0100132 *(end++) = '\0';
Lalit Maganti8eba3092019-03-27 13:25:29 +0000133
134 // Update the end of the block and return the pointer to the string.
Mikhail Khokhlov132684e2019-08-14 16:22:22 +0100135 pos_ = OffsetOf(end);
Lalit Maganti40db1512019-11-12 13:27:19 +0000136
Eric Seckler56f27db2020-01-15 08:57:17 +0000137 return std::make_pair(true, offset);
Lalit Maganti8eba3092019-03-27 13:25:29 +0000138}
139
140StringPool::Iterator::Iterator(const StringPool* pool) : pool_(pool) {}
141
142StringPool::Iterator& StringPool::Iterator::operator++() {
Eric Seckler56f27db2020-01-15 08:57:17 +0000143 if (block_index_ < pool_->blocks_.size()) {
144 // Try and go to the next string in the current block.
145 const auto& block = pool_->blocks_[block_index_];
Lalit Maganti8eba3092019-03-27 13:25:29 +0000146
Eric Seckler56f27db2020-01-15 08:57:17 +0000147 // Find the size of the string at the current offset in the block
148 // and increment the offset by that size.
149 uint32_t str_size = 0;
150 const uint8_t* ptr = block.Get(block_offset_);
151 ptr = ReadSize(ptr, &str_size);
152 ptr += str_size + 1;
153 block_offset_ = block.OffsetOf(ptr);
Lalit Maganti8eba3092019-03-27 13:25:29 +0000154
Eric Seckler56f27db2020-01-15 08:57:17 +0000155 // If we're out of bounds for this block, go to the start of the next block.
156 if (block.pos() <= block_offset_) {
157 block_index_++;
158 block_offset_ = 0;
159 }
Lalit Maganti8eba3092019-03-27 13:25:29 +0000160
Eric Seckler56f27db2020-01-15 08:57:17 +0000161 return *this;
Lalit Maganti8eba3092019-03-27 13:25:29 +0000162 }
Eric Seckler56f27db2020-01-15 08:57:17 +0000163
164 // Advance to the next string from |large_strings_|.
165 PERFETTO_DCHECK(large_strings_index_ < pool_->large_strings_.size());
166 large_strings_index_++;
Lalit Maganti8eba3092019-03-27 13:25:29 +0000167 return *this;
168}
169
170StringPool::Iterator::operator bool() const {
Eric Seckler56f27db2020-01-15 08:57:17 +0000171 return block_index_ < pool_->blocks_.size() ||
172 large_strings_index_ < pool_->large_strings_.size();
Lalit Maganti8eba3092019-03-27 13:25:29 +0000173}
174
175NullTermStringView StringPool::Iterator::StringView() {
Eric Seckler56f27db2020-01-15 08:57:17 +0000176 return pool_->Get(StringId());
Lalit Maganti8eba3092019-03-27 13:25:29 +0000177}
178
179StringPool::Id StringPool::Iterator::StringId() {
Eric Seckler56f27db2020-01-15 08:57:17 +0000180 if (block_index_ < pool_->blocks_.size()) {
181 PERFETTO_DCHECK(block_offset_ < pool_->blocks_[block_index_].pos());
Lalit Maganti8eba3092019-03-27 13:25:29 +0000182
Eric Seckler56f27db2020-01-15 08:57:17 +0000183 // If we're at (0, 0), we have the null string which has id 0.
184 if (block_index_ == 0 && block_offset_ == 0)
Lalit Magantif0599a02020-01-15 15:45:20 +0000185 return Id::Null();
186 return Id::BlockString(block_index_, block_offset_);
Eric Seckler56f27db2020-01-15 08:57:17 +0000187 }
188 PERFETTO_DCHECK(large_strings_index_ < pool_->large_strings_.size());
Lalit Magantif0599a02020-01-15 15:45:20 +0000189 return Id::LargeString(large_strings_index_);
Lalit Maganti8eba3092019-03-27 13:25:29 +0000190}
191
192} // namespace trace_processor
193} // namespace perfetto