LLDB mainline
ConstString.cpp
Go to the documentation of this file.
1//===-- ConstString.cpp ---------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10
11#include "lldb/Utility/Stream.h"
12
13#include "llvm/ADT/StringMap.h"
14#include "llvm/ADT/StringRef.h"
15#include "llvm/ADT/iterator.h"
16#include "llvm/Support/Allocator.h"
17#include "llvm/Support/DJB.h"
18#include "llvm/Support/FormatProviders.h"
19#include "llvm/Support/Threading.h"
20
21#include <array>
22#include <mutex>
23#include <shared_mutex>
24#include <utility>
25
26#include <cinttypes>
27#include <cstdint>
28#include <cstring>
29
30using namespace lldb_private;
31
32#if !defined(__APPLE__)
33using PoolMutex = std::shared_mutex;
34#else
35#include <os/lock.h>
36
37namespace {
38/// On Apple platforms os_unfair_lock is significantly faster than
39/// pthread_rwlock for concurrent writes, and roughly on par for concurrent
40/// reads.
41///
42/// The class satisfies both Lockable and SharedLockable so it composes with
43/// std::lock_guard and std::shared_lock.
44class PoolMutex {
45public:
46 void lock() { os_unfair_lock_lock(&m_lock); }
47 void unlock() { os_unfair_lock_unlock(&m_lock); }
48 void lock_shared() { os_unfair_lock_lock(&m_lock); }
49 void unlock_shared() { os_unfair_lock_unlock(&m_lock); }
50
51private:
52 os_unfair_lock m_lock = OS_UNFAIR_LOCK_INIT;
53};
54} // namespace
55#endif
56
57class Pool {
58public:
59 /// The default BumpPtrAllocatorImpl slab size.
60 static const size_t AllocatorSlabSize = 4096;
61 static const size_t SizeThreshold = AllocatorSlabSize;
62 /// Every Pool has its own allocator which receives an equal share of
63 /// the ConstString allocations. This means that when allocating many
64 /// ConstStrings, every allocator sees only its small share of allocations and
65 /// assumes LLDB only allocated a small amount of memory so far. In reality
66 /// LLDB allocated a total memory that is N times as large as what the
67 /// allocator sees (where N is the number of string pools). This causes that
68 /// the BumpPtrAllocator continues a long time to allocate memory in small
69 /// chunks which only makes sense when allocating a small amount of memory
70 /// (which is true from the perspective of a single allocator). On some
71 /// systems doing all these small memory allocations causes LLDB to spend
72 /// a lot of time in malloc, so we need to force all these allocators to
73 /// behave like one allocator in terms of scaling their memory allocations
74 /// with increased demand. To do this we set the growth delay for each single
75 /// allocator to a rate so that our pool of allocators scales their memory
76 /// allocations similar to a single BumpPtrAllocatorImpl.
77 ///
78 /// Currently we have 256 string pools and the normal growth delay of the
79 /// BumpPtrAllocatorImpl is 128 (i.e., the memory allocation size increases
80 /// every 128 full chunks), so by changing the delay to 1 we get a
81 /// total growth delay in our allocator collection of 256/1 = 256. This is
82 /// still only half as fast as a normal allocator but we can't go any faster
83 /// without decreasing the number of string pools.
84 static const size_t AllocatorGrowthDelay = 1;
85 typedef llvm::BumpPtrAllocatorImpl<llvm::MallocAllocator, AllocatorSlabSize,
88 typedef const char *StringPoolValueType;
89 typedef llvm::StringMap<StringPoolValueType, Allocator> StringPool;
90 typedef llvm::StringMapEntry<StringPoolValueType> StringPoolEntryType;
91
92 static StringPoolEntryType &
93 GetStringMapEntryFromKeyData(const char *keyData) {
94 return StringPoolEntryType::GetStringMapEntryFromKeyData(keyData);
95 }
96
97 static size_t GetConstCStringLength(const char *ccstr) {
98 if (ccstr != nullptr) {
99 // Since the entry is read only, and we derive the entry entirely from
100 // the pointer, we don't need the lock.
102 return entry.getKeyLength();
103 }
104 return 0;
105 }
106
108 const char *const ccstr = str.data();
109 if (ccstr != nullptr) {
110 const PoolEntry &pool = selectPool(str);
111 std::shared_lock<PoolMutex> lock(pool.m_mutex);
112 return GetStringMapEntryFromKeyData(ccstr).getValue();
113 }
114 return nullptr;
115 }
116
117 const char *GetConstCString(const char *cstr) {
118 if (cstr != nullptr)
119 return GetConstCStringWithLength(cstr, strlen(cstr));
120 return nullptr;
121 }
122
123 const char *GetConstCStringWithLength(const char *cstr, size_t cstr_len) {
124 if (cstr != nullptr)
125 return GetConstCStringWithStringRef(llvm::StringRef(cstr, cstr_len));
126 return nullptr;
127 }
128
129 const char *GetConstCStringWithStringRef(llvm::StringRef string_ref) {
130 if (string_ref.data()) {
131 const uint32_t string_hash = StringPool::hash(string_ref);
132 PoolEntry &pool = selectPool(string_hash);
133
134 {
135 std::shared_lock<PoolMutex> lock(pool.m_mutex);
136 auto it = pool.m_string_map.find(string_ref, string_hash);
137 if (it != pool.m_string_map.end())
138 return it->getKeyData();
139 }
140
141 std::lock_guard<PoolMutex> lock(pool.m_mutex);
142 pool.used_bytes += string_ref.size();
143 StringPoolEntryType &entry =
144 *pool.m_string_map
145 .insert(std::make_pair(string_ref, nullptr), string_hash)
146 .first;
147 return entry.getKeyData();
148 }
149 return nullptr;
150 }
151
152 const char *GetConstCStringAndSetMangledCounterPart(llvm::StringRef demangled,
153 llvm::StringRef mangled) {
154 const char *demangled_ccstr = nullptr;
155 const char *const mangled_ccstr = mangled.data();
156
157 {
158 const uint32_t demangled_hash = StringPool::hash(demangled);
159 PoolEntry &pool = selectPool(demangled_hash);
160 std::lock_guard<PoolMutex> lock(pool.m_mutex);
161
162 // Make or update string pool entry with the mangled counterpart
163 StringPool &map = pool.m_string_map;
164 auto [entry, inserted] =
165 map.try_emplace_with_hash(demangled, demangled_hash);
166 if (inserted)
167 pool.used_bytes += demangled.size();
168
169 entry->second = mangled_ccstr;
170
171 // Extract the const version of the demangled_cstr
172 demangled_ccstr = entry->getKeyData();
173 }
174
175 {
176 // Now assign the demangled const string as the counterpart of the
177 // mangled const string...
178 PoolEntry &pool = selectPool(mangled);
179 std::lock_guard<PoolMutex> lock(pool.m_mutex);
180 GetStringMapEntryFromKeyData(mangled_ccstr).setValue(demangled_ccstr);
181 }
182
183 // Return the constant demangled C string
184 return demangled_ccstr;
185 }
186
187 const char *GetConstTrimmedCStringWithLength(const char *cstr,
188 size_t cstr_len) {
189 if (cstr != nullptr) {
190 const size_t trimmed_len = strnlen(cstr, cstr_len);
191 return GetConstCStringWithLength(cstr, trimmed_len);
192 }
193 return nullptr;
194 }
195
198 for (const auto &pool : m_string_pools) {
199 std::shared_lock<PoolMutex> lock(pool.m_mutex);
200 const Allocator &alloc = pool.m_string_map.getAllocator();
201 stats.bytes_total += alloc.getTotalMemory();
202 stats.bytes_used += pool.used_bytes;
203 }
204 return stats;
205 }
206
207protected:
208 struct PoolEntry {
211 /// The exact number of bytes used by this pool.
212 /// This excludes alignment, padding and redzones.
213 std::size_t used_bytes = 0;
214 };
215
216 std::array<PoolEntry, 256> m_string_pools;
217
218 PoolEntry &selectPool(const llvm::StringRef &s) {
219 return selectPool(StringPool::hash(s));
220 }
221
222 PoolEntry &selectPool(uint32_t h) {
223 return m_string_pools[((h >> 24) ^ (h >> 16) ^ (h >> 8) ^ h) & 0xff];
224 }
225};
226
227// Frameworks and dylibs aren't supposed to have global C++ initializers so we
228// hide the string pool in a static function so that it will get initialized on
229// the first call to this static function.
230//
231// Note, for now we make the string pool a pointer to the pool, because we
232// can't guarantee that some objects won't get destroyed after the global
233// destructor chain is run, and trying to make sure no destructors touch
234// ConstStrings is difficult. So we leak the pool instead.
235static Pool &StringPool() {
236 static llvm::once_flag g_pool_initialization_flag;
237 static Pool *g_string_pool = nullptr;
238
239 llvm::call_once(g_pool_initialization_flag,
240 []() { g_string_pool = new Pool(); });
241
242 return *g_string_pool;
243}
244
246 : m_string(StringPool().GetConstCString(cstr)) {}
247
248ConstString::ConstString(const char *cstr, size_t cstr_len)
249 : m_string(StringPool().GetConstCStringWithLength(cstr, cstr_len)) {}
250
251ConstString::ConstString(llvm::StringRef s)
252 : m_string(StringPool().GetConstCStringWithStringRef(s)) {}
253
255 if (m_string == rhs.m_string)
256 return false;
257
258 llvm::StringRef lhs_string_ref(GetStringRef());
259 llvm::StringRef rhs_string_ref(rhs.GetStringRef());
260
261 // If both have valid C strings, then return the comparison
262 if (lhs_string_ref.data() && rhs_string_ref.data())
263 return lhs_string_ref < rhs_string_ref;
264
265 // Else one of them was nullptr, so if LHS is nullptr then it is less than
266 return lhs_string_ref.data() == nullptr;
267}
268
270 const char *cstr = str.GetCString();
271 if (cstr != nullptr)
272 s << cstr;
273
274 return s;
275}
276
280
282 const bool case_sensitive) {
283 if (lhs.m_string == rhs.m_string)
284 return true;
285
286 // Since the pointers weren't equal, and identical ConstStrings always have
287 // identical pointers, the result must be false for case sensitive equality
288 // test.
289 if (case_sensitive)
290 return false;
291
292 // perform case insensitive equality test
293 llvm::StringRef lhs_string_ref(lhs.GetStringRef());
294 llvm::StringRef rhs_string_ref(rhs.GetStringRef());
295 return lhs_string_ref.equals_insensitive(rhs_string_ref);
296}
297
299 const bool case_sensitive) {
300 // If the iterators are the same, this is the same string
301 const char *lhs_cstr = lhs.m_string;
302 const char *rhs_cstr = rhs.m_string;
303 if (lhs_cstr == rhs_cstr)
304 return 0;
305 if (lhs_cstr && rhs_cstr) {
306 llvm::StringRef lhs_string_ref(lhs.GetStringRef());
307 llvm::StringRef rhs_string_ref(rhs.GetStringRef());
308
309 if (case_sensitive) {
310 return lhs_string_ref.compare(rhs_string_ref);
311 } else {
312 return lhs_string_ref.compare_insensitive(rhs_string_ref);
313 }
314 }
315
316 if (lhs_cstr)
317 return +1; // LHS isn't nullptr but RHS is
318 else
319 return -1; // LHS is nullptr but RHS isn't
320}
321
322void ConstString::Dump(Stream *s, const char *fail_value) const {
323 if (s != nullptr) {
324 const char *cstr = AsCString(fail_value);
325 if (cstr != nullptr)
326 s->PutCString(cstr);
327 }
328}
329
331 const char *cstr = GetCString();
332 size_t cstr_len = GetLength();
333 // Only print the parens if we have a non-nullptr string
334 const char *parens = cstr ? "\"" : "";
335 s->Printf("%*p: ConstString, string = %s%s%s, length = %" PRIu64,
336 static_cast<int>(sizeof(void *) * 2),
337 static_cast<const void *>(this), parens, cstr, parens,
338 static_cast<uint64_t>(cstr_len));
339}
340
341void ConstString::SetCString(const char *cstr) {
343}
344
348
349void ConstString::SetStringWithMangledCounterpart(llvm::StringRef demangled,
350 ConstString mangled) {
352 demangled, mangled.GetStringRef());
353}
354
357 return (bool)counterpart;
358}
359
360void ConstString::SetCStringWithLength(const char *cstr, size_t cstr_len) {
362}
363
365 size_t cstr_len) {
367}
368
372
373void llvm::format_provider<ConstString>::format(const ConstString &CS,
374 llvm::raw_ostream &OS,
375 llvm::StringRef Options) {
376 format_provider<StringRef>::format(CS.GetStringRef(), OS, Options);
377}
std::shared_mutex PoolMutex
static Pool & StringPool()
PoolEntry & selectPool(const llvm::StringRef &s)
static const size_t SizeThreshold
static StringPoolEntryType & GetStringMapEntryFromKeyData(const char *keyData)
const char * GetConstCStringWithStringRef(llvm::StringRef string_ref)
static const size_t AllocatorSlabSize
The default BumpPtrAllocatorImpl slab size.
llvm::StringMap< StringPoolValueType, Allocator > StringPool
const char * GetConstCStringWithLength(const char *cstr, size_t cstr_len)
StringPoolValueType GetMangledCounterpart(llvm::StringRef str)
const char * GetConstCStringAndSetMangledCounterPart(llvm::StringRef demangled, llvm::StringRef mangled)
const char * GetConstTrimmedCStringWithLength(const char *cstr, size_t cstr_len)
ConstString::MemoryStats GetMemoryStats() const
llvm::BumpPtrAllocatorImpl< llvm::MallocAllocator, AllocatorSlabSize, SizeThreshold, AllocatorGrowthDelay > Allocator
std::array< PoolEntry, 256 > m_string_pools
llvm::StringMapEntry< StringPoolValueType > StringPoolEntryType
const char * GetConstCString(const char *cstr)
PoolEntry & selectPool(uint32_t h)
static const size_t AllocatorGrowthDelay
Every Pool has its own allocator which receives an equal share of the ConstString allocations.
const char * StringPoolValueType
static size_t GetConstCStringLength(const char *ccstr)
A uniqued constant string class.
Definition ConstString.h:40
bool GetMangledCounterpart(ConstString &counterpart) const
Retrieve the mangled or demangled counterpart for a mangled or demangled ConstString.
static MemoryStats GetMemoryStats()
void SetCStringWithLength(const char *cstr, size_t cstr_len)
Set the C string value with length.
void SetCString(const char *cstr)
Set the C string value.
static int Compare(ConstString lhs, ConstString rhs, const bool case_sensitive=true)
Compare two string objects.
ConstString()=default
Default constructor.
void Dump(Stream *s, const char *value_if_empty=nullptr) const
Dump the object description to a stream.
static bool Equals(ConstString lhs, ConstString rhs, const bool case_sensitive=true)
Equal to operator.
void DumpDebug(Stream *s) const
Dump the object debug description to a stream.
void SetTrimmedCStringWithLength(const char *cstr, size_t fixed_cstr_len)
Set the C string value with the minimum length between fixed_cstr_len and the actual length of the C ...
size_t GetLength() const
Get the length in bytes of string value.
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
void SetString(llvm::StringRef s)
bool operator<(ConstString rhs) const
const char * GetCString() const
Get the string value as a C string.
void SetStringWithMangledCounterpart(llvm::StringRef demangled, ConstString mangled)
Set the C string value and its mangled counterpart.
const char * AsCString(const char *value_if_empty) const
Get the string value as a C string.
A command line option parsing protocol class.
Definition Options.h:58
A stream class that can stream formatted output to a file.
Definition Stream.h:28
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition Stream.cpp:63
A class that represents a running process on the host machine.
Stream & operator<<(Stream &s, const Mangled &obj)
std::size_t used_bytes
The exact number of bytes used by this pool.
StringPool m_string_map