Lalit Maganti | 8eba309 | 2019-03-27 13:25:29 +0000 | [diff] [blame] | 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 | |
Lalit Maganti | 9ecfab3 | 2019-12-17 17:45:38 +0000 | [diff] [blame] | 17 | #include "src/trace_processor/containers/string_pool.h" |
Lalit Maganti | 8eba309 | 2019-03-27 13:25:29 +0000 | [diff] [blame] | 18 | |
Eric Seckler | f93b8cf | 2019-11-20 21:33:01 +0000 | [diff] [blame] | 19 | #include <limits> |
Alan Zhao | 4c2da7d | 2023-02-27 15:27:01 -0800 | [diff] [blame^] | 20 | #include <tuple> |
Eric Seckler | f93b8cf | 2019-11-20 21:33:01 +0000 | [diff] [blame] | 21 | |
Lalit Maganti | 8eba309 | 2019-03-27 13:25:29 +0000 | [diff] [blame] | 22 | #include "perfetto/base/logging.h" |
Primiano Tucci | 80bbdd8 | 2019-08-30 07:54:04 +0200 | [diff] [blame] | 23 | #include "perfetto/ext/base/utils.h" |
Lalit Maganti | 8eba309 | 2019-03-27 13:25:29 +0000 | [diff] [blame] | 24 | |
| 25 | namespace perfetto { |
| 26 | namespace trace_processor { |
| 27 | |
Lalit Maganti | 03d178e | 2022-11-03 16:18:51 +0000 | [diff] [blame] | 28 | #if !PERFETTO_IS_AT_LEAST_CPP17() |
Eric Seckler | 56f27db | 2020-01-15 08:57:17 +0000 | [diff] [blame] | 29 | // static |
| 30 | constexpr size_t StringPool::kNumBlockIndexBits; |
| 31 | // static |
| 32 | constexpr size_t StringPool::kNumBlockOffsetBits; |
| 33 | // static |
| 34 | constexpr size_t StringPool::kLargeStringFlagBitMask; |
| 35 | // static |
| 36 | constexpr size_t StringPool::kBlockOffsetBitMask; |
| 37 | // static |
| 38 | constexpr size_t StringPool::kBlockIndexBitMask; |
| 39 | // static |
| 40 | constexpr size_t StringPool::kBlockSizeBytes; |
| 41 | // static |
| 42 | constexpr size_t StringPool::kMinLargeStringSizeBytes; |
Lalit Maganti | 03d178e | 2022-11-03 16:18:51 +0000 | [diff] [blame] | 43 | #endif |
Eric Seckler | 56f27db | 2020-01-15 08:57:17 +0000 | [diff] [blame] | 44 | |
| 45 | StringPool::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 Maganti | 8eba309 | 2019-03-27 13:25:29 +0000 | [diff] [blame] | 52 | |
| 53 | // Reserve a slot for the null string. |
Eric Seckler | 56f27db | 2020-01-15 08:57:17 +0000 | [diff] [blame] | 54 | PERFETTO_CHECK(blocks_.back().TryInsert(NullTermStringView()).first); |
Lalit Maganti | 8eba309 | 2019-03-27 13:25:29 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | StringPool::~StringPool() = default; |
| 58 | |
Primiano Tucci | c986ca1 | 2021-11-18 20:51:24 +0000 | [diff] [blame] | 59 | StringPool::StringPool(StringPool&&) noexcept = default; |
| 60 | StringPool& StringPool::operator=(StringPool&&) noexcept = default; |
Lalit Maganti | 8eba309 | 2019-03-27 13:25:29 +0000 | [diff] [blame] | 61 | |
| 62 | StringPool::Id StringPool::InsertString(base::StringView str, uint64_t hash) { |
Lalit Maganti | 8eba309 | 2019-03-27 13:25:29 +0000 | [diff] [blame] | 63 | // Try and find enough space in the current block for the string and the |
Mikhail Khokhlov | 132684e | 2019-08-14 16:22:22 +0100 | [diff] [blame] | 64 | // metadata (varint-encoded size + the string data + the null terminator). |
Eric Seckler | 56f27db | 2020-01-15 08:57:17 +0000 | [diff] [blame] | 65 | 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 Khokhlov | 132684e | 2019-08-14 16:22:22 +0100 | [diff] [blame] | 76 | } |
Primiano Tucci | c986ca1 | 2021-11-18 20:51:24 +0000 | [diff] [blame] | 77 | blocks_.emplace_back(kBlockSizeBytes); |
Lalit Maganti | 8eba309 | 2019-03-27 13:25:29 +0000 | [diff] [blame] | 78 | |
| 79 | // Try and reserve space again - this time we should definitely succeed. |
Eric Seckler | 56f27db | 2020-01-15 08:57:17 +0000 | [diff] [blame] | 80 | std::tie(success, offset) = blocks_.back().TryInsert(str); |
| 81 | PERFETTO_CHECK(success); |
Lalit Maganti | 8eba309 | 2019-03-27 13:25:29 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Eric Seckler | 56f27db | 2020-01-15 08:57:17 +0000 | [diff] [blame] | 84 | // Compute the id from the block index and offset and add a mapping from the |
| 85 | // hash to the id. |
Lalit Maganti | f0599a0 | 2020-01-15 15:45:20 +0000 | [diff] [blame] | 86 | Id string_id = Id::BlockString(blocks_.size() - 1, offset); |
Primiano Tucci | c986ca1 | 2021-11-18 20:51:24 +0000 | [diff] [blame] | 87 | |
| 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 Maganti | 8eba309 | 2019-03-27 13:25:29 +0000 | [diff] [blame] | 92 | return string_id; |
| 93 | } |
| 94 | |
Eric Seckler | 56f27db | 2020-01-15 08:57:17 +0000 | [diff] [blame] | 95 | StringPool::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 Maganti | f0599a0 | 2020-01-15 15:45:20 +0000 | [diff] [blame] | 99 | Id string_id = Id::LargeString(large_strings_.size() - 1); |
Primiano Tucci | c986ca1 | 2021-11-18 20:51:24 +0000 | [diff] [blame] | 100 | |
| 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 Seckler | 56f27db | 2020-01-15 08:57:17 +0000 | [diff] [blame] | 105 | return string_id; |
| 106 | } |
| 107 | |
| 108 | std::pair<bool /*success*/, uint32_t /*offset*/> StringPool::Block::TryInsert( |
| 109 | base::StringView str) { |
Lalit Maganti | 8eba309 | 2019-03-27 13:25:29 +0000 | [diff] [blame] | 110 | auto str_size = str.size(); |
Lalit Maganti | 40db151 | 2019-11-12 13:27:19 +0000 | [diff] [blame] | 111 | size_t max_pos = static_cast<size_t>(pos_) + str_size + kMaxMetadataSize; |
| 112 | if (max_pos > size_) |
Eric Seckler | 56f27db | 2020-01-15 08:57:17 +0000 | [diff] [blame] | 113 | return std::make_pair(false, 0u); |
Lalit Maganti | 8eba309 | 2019-03-27 13:25:29 +0000 | [diff] [blame] | 114 | |
Lalit Maganti | 40db151 | 2019-11-12 13:27:19 +0000 | [diff] [blame] | 115 | // Ensure that we commit up until the end of the string to memory. |
| 116 | mem_.EnsureCommitted(max_pos); |
| 117 | |
Lalit Maganti | 8eba309 | 2019-03-27 13:25:29 +0000 | [diff] [blame] | 118 | // Get where we should start writing this string. |
Eric Seckler | 56f27db | 2020-01-15 08:57:17 +0000 | [diff] [blame] | 119 | uint32_t offset = pos_; |
| 120 | uint8_t* begin = Get(offset); |
Lalit Maganti | 8eba309 | 2019-03-27 13:25:29 +0000 | [diff] [blame] | 121 | |
Mikhail Khokhlov | 132684e | 2019-08-14 16:22:22 +0100 | [diff] [blame] | 122 | // First write the size of the string using varint encoding. |
| 123 | uint8_t* end = protozero::proto_utils::WriteVarInt(str_size, begin); |
Lalit Maganti | 8eba309 | 2019-03-27 13:25:29 +0000 | [diff] [blame] | 124 | |
Mikhail Khokhlov | 132684e | 2019-08-14 16:22:22 +0100 | [diff] [blame] | 125 | // Next the string itself. |
| 126 | if (PERFETTO_LIKELY(str_size > 0)) { |
| 127 | memcpy(end, str.data(), str_size); |
| 128 | end += str_size; |
| 129 | } |
Lalit Maganti | 8eba309 | 2019-03-27 13:25:29 +0000 | [diff] [blame] | 130 | |
| 131 | // Finally add a null terminator. |
Mikhail Khokhlov | 132684e | 2019-08-14 16:22:22 +0100 | [diff] [blame] | 132 | *(end++) = '\0'; |
Lalit Maganti | 8eba309 | 2019-03-27 13:25:29 +0000 | [diff] [blame] | 133 | |
| 134 | // Update the end of the block and return the pointer to the string. |
Mikhail Khokhlov | 132684e | 2019-08-14 16:22:22 +0100 | [diff] [blame] | 135 | pos_ = OffsetOf(end); |
Lalit Maganti | 40db151 | 2019-11-12 13:27:19 +0000 | [diff] [blame] | 136 | |
Eric Seckler | 56f27db | 2020-01-15 08:57:17 +0000 | [diff] [blame] | 137 | return std::make_pair(true, offset); |
Lalit Maganti | 8eba309 | 2019-03-27 13:25:29 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | StringPool::Iterator::Iterator(const StringPool* pool) : pool_(pool) {} |
| 141 | |
| 142 | StringPool::Iterator& StringPool::Iterator::operator++() { |
Eric Seckler | 56f27db | 2020-01-15 08:57:17 +0000 | [diff] [blame] | 143 | 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 Maganti | 8eba309 | 2019-03-27 13:25:29 +0000 | [diff] [blame] | 146 | |
Eric Seckler | 56f27db | 2020-01-15 08:57:17 +0000 | [diff] [blame] | 147 | // 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 Maganti | 8eba309 | 2019-03-27 13:25:29 +0000 | [diff] [blame] | 154 | |
Eric Seckler | 56f27db | 2020-01-15 08:57:17 +0000 | [diff] [blame] | 155 | // 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 Maganti | 8eba309 | 2019-03-27 13:25:29 +0000 | [diff] [blame] | 160 | |
Eric Seckler | 56f27db | 2020-01-15 08:57:17 +0000 | [diff] [blame] | 161 | return *this; |
Lalit Maganti | 8eba309 | 2019-03-27 13:25:29 +0000 | [diff] [blame] | 162 | } |
Eric Seckler | 56f27db | 2020-01-15 08:57:17 +0000 | [diff] [blame] | 163 | |
| 164 | // Advance to the next string from |large_strings_|. |
| 165 | PERFETTO_DCHECK(large_strings_index_ < pool_->large_strings_.size()); |
| 166 | large_strings_index_++; |
Lalit Maganti | 8eba309 | 2019-03-27 13:25:29 +0000 | [diff] [blame] | 167 | return *this; |
| 168 | } |
| 169 | |
| 170 | StringPool::Iterator::operator bool() const { |
Eric Seckler | 56f27db | 2020-01-15 08:57:17 +0000 | [diff] [blame] | 171 | return block_index_ < pool_->blocks_.size() || |
| 172 | large_strings_index_ < pool_->large_strings_.size(); |
Lalit Maganti | 8eba309 | 2019-03-27 13:25:29 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | NullTermStringView StringPool::Iterator::StringView() { |
Eric Seckler | 56f27db | 2020-01-15 08:57:17 +0000 | [diff] [blame] | 176 | return pool_->Get(StringId()); |
Lalit Maganti | 8eba309 | 2019-03-27 13:25:29 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | StringPool::Id StringPool::Iterator::StringId() { |
Eric Seckler | 56f27db | 2020-01-15 08:57:17 +0000 | [diff] [blame] | 180 | if (block_index_ < pool_->blocks_.size()) { |
| 181 | PERFETTO_DCHECK(block_offset_ < pool_->blocks_[block_index_].pos()); |
Lalit Maganti | 8eba309 | 2019-03-27 13:25:29 +0000 | [diff] [blame] | 182 | |
Eric Seckler | 56f27db | 2020-01-15 08:57:17 +0000 | [diff] [blame] | 183 | // If we're at (0, 0), we have the null string which has id 0. |
| 184 | if (block_index_ == 0 && block_offset_ == 0) |
Lalit Maganti | f0599a0 | 2020-01-15 15:45:20 +0000 | [diff] [blame] | 185 | return Id::Null(); |
| 186 | return Id::BlockString(block_index_, block_offset_); |
Eric Seckler | 56f27db | 2020-01-15 08:57:17 +0000 | [diff] [blame] | 187 | } |
| 188 | PERFETTO_DCHECK(large_strings_index_ < pool_->large_strings_.size()); |
Lalit Maganti | f0599a0 | 2020-01-15 15:45:20 +0000 | [diff] [blame] | 189 | return Id::LargeString(large_strings_index_); |
Lalit Maganti | 8eba309 | 2019-03-27 13:25:29 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | } // namespace trace_processor |
| 193 | } // namespace perfetto |