Index: components/tracing/core/string_interning.h |
diff --git a/components/tracing/core/string_interning.h b/components/tracing/core/string_interning.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..bee7a5cb2e499b8dd857f1ec51a586af92318830 |
--- /dev/null |
+++ b/components/tracing/core/string_interning.h |
@@ -0,0 +1,55 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef COMPONENTS_TRACING_CORE_STRING_INTERNING_H_ |
+#define COMPONENTS_TRACING_CORE_STRING_INTERNING_H_ |
+ |
+#include <stddef.h> |
+#include <stdint.h> |
+ |
+#include <limits> |
+ |
+#include "base/logging.h" |
+#include "components/tracing/tracing_export.h" |
+ |
+// All non-copied strings in tracing are, by API contract, long lived |
+// const char* pointers. In order to save trace buffer size (and performance), |
+// tracing doesn't copy those strings, but keeps only an index for each event. |
+// The way these strings are interned is pretty straightforward: we encode the |
+// ptr distance from a known constant in the binary (kInternedStringBase) and |
+// use that as string index. |
+// There are two use cases for interned strings: |
+// 1. In the most common cases they are looked up in batch when the trace is |
+// finalized and trace metadata is written, to produce the final string |
+// table. This case always works (as long as the strings are long lived) |
+// in both component and static builds. We just need to do the reverse math |
+// to work out the const char* from the offset at finalization time. |
+// 2. When supporting the recovery of non-finalized traces from a crahed |
+// process, the same principle can be applied, but only in the case of |
+// non-component builds. In a monolithic build the string offset is constatnt |
DmitrySkiba
2016/11/29 18:09:56
constatnt -> constant
|
+// regardless of ASLR. |
+ |
+namespace tracing { |
+namespace v2 { |
+ |
+TRACING_EXPORT extern const char kInternedStringBase[]; |
+ |
+inline int64_t InternString(const char* str) { |
DmitrySkiba
2016/11/29 18:09:56
Maybe rename "intern" to "index" or "stable offset
|
+ // keeping the diff of two arbitrary pointers into a int64_t is safe as the |
+ // virtual address space of all the 64-bit architectures we care about is |
+ // << 64 bits (MMUs are typically limited at 38 bits). |
+ ptrdiff_t offset = reinterpret_cast<intptr_t>(str) - |
+ reinterpret_cast<intptr_t>(kInternedStringBase); |
+ return static_cast<int64_t>(offset); |
+} |
+ |
+inline const char* GetInternedStringValue(int64_t offset) { |
+ intptr_t ptr = reinterpret_cast<intptr_t>(kInternedStringBase) + offset; |
+ return reinterpret_cast<const char*>(ptr); |
+} |
+ |
+} // namespace v2 |
+} // namespace tracing |
+ |
+#endif // COMPONENTS_TRACING_CORE_STRING_INTERNING_H_ |