-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
stats: symbolize strings in HeapStatData and ThreadLocalStore #4281
Closed
Closed
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
50b8082
stats: symbolize strings in HeapStatData and ThreadLocalStore
ambuc a5bb73b
Fix mutex issues inside ThreadLocalStoreImpl
ambuc 50d6c9c
Assorted nits
ambuc 65ddd61
remove trailing underscores for custom hasher/comparators
ambuc cbf710b
Remove pure interfaces for StatNameImpl/SymbolTableImpl
ambuc 3096f72
Reduce scope of mutex in HeapStatDataAllocator
ambuc c869182
Explicitly guard objects in symbol_table_impl
ambuc c7d7437
Assorted parens nits
ambuc 5a21170
Stronger guarantees around thread safety within SymbolTable
ambuc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,9 +5,11 @@ | |
#include <unordered_set> | ||
|
||
#include "common/common/hash.h" | ||
#include "common/common/lock_guard.h" | ||
#include "common/common/thread.h" | ||
#include "common/common/thread_annotations.h" | ||
#include "common/stats/stat_data_allocator_impl.h" | ||
#include "common/stats/symbol_table_impl.h" | ||
|
||
namespace Envoy { | ||
namespace Stats { | ||
|
@@ -17,23 +19,25 @@ namespace Stats { | |
* so that it can be allocated efficiently from the heap on demand. | ||
*/ | ||
struct HeapStatData { | ||
explicit HeapStatData(absl::string_view key); | ||
explicit HeapStatData(StatNamePtr name_ptr); | ||
|
||
/** | ||
* @returns absl::string_view the name as a string_view. | ||
* @returns std::string the name as a std::string with no truncation. | ||
*/ | ||
absl::string_view key() const { return name_; } | ||
std::string name() const { return name_ptr_->toString(); } | ||
|
||
/** | ||
* @returns std::string the name as a std::string. | ||
* Alias for name(), because BlockMemoryHashSet<Value> expects Value::key(). | ||
mrice32 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
std::string name() const { return name_; } | ||
std::string key() const { return name(); } | ||
|
||
bool operator==(const HeapStatData& rhs) const { return *name_ptr_ == *(rhs.name_ptr_); } | ||
mrice32 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
std::atomic<uint64_t> value_{0}; | ||
std::atomic<uint64_t> pending_increment_{0}; | ||
std::atomic<uint16_t> flags_{0}; | ||
std::atomic<uint16_t> ref_count_{1}; | ||
std::string name_; | ||
StatNamePtr name_ptr_; | ||
}; | ||
|
||
/** | ||
|
@@ -52,27 +56,33 @@ class HeapStatDataAllocator : public StatDataAllocatorImpl<HeapStatData> { | |
// StatDataAllocator | ||
bool requiresBoundedStatNameSize() const override { return false; } | ||
|
||
// SymbolTableImpl | ||
StatNamePtr encode(absl::string_view sv) { | ||
Thread::LockGuard lock(mutex_); | ||
return table_.encode(sv); | ||
} | ||
|
||
private: | ||
struct HeapStatHash_ { | ||
size_t operator()(const HeapStatData* a) const { return HashUtil::xxHash64(a->key()); } | ||
size_t operator()(const HeapStatData* a) const { return a->name_ptr_->hash(); } | ||
}; | ||
struct HeapStatCompare_ { | ||
bool operator()(const HeapStatData* a, const HeapStatData* b) const { | ||
return (a->key() == b->key()); | ||
} | ||
bool operator()(const HeapStatData* a, const HeapStatData* b) const { return (*a == *b); } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: don't need parens around *a == *b |
||
}; | ||
|
||
// TODO(jmarantz): See https://github.com/envoyproxy/envoy/pull/3927 and | ||
// https://github.com/envoyproxy/envoy/issues/3585, which can help reorganize | ||
// the heap stats using a ref-counted symbol table to compress the stat strings. | ||
typedef std::unordered_set<HeapStatData*, HeapStatHash_, HeapStatCompare_> StatSet; | ||
|
||
// An unordered set of HeapStatData pointers which keys off the key() | ||
// field in each object. This necessitates a custom comparator and hasher. | ||
// field in each object. This necessitates a custom comparator and hasher, which key off of the | ||
// StatNamePtr's own StatNamePtrHash_ and StatNamePtrCompare_ operators. | ||
StatSet stats_ GUARDED_BY(mutex_); | ||
// A mutex is needed here to protect the stats_ object from both alloc() and free() operations. | ||
// Although alloc() operations are called under existing locking, free() operations are made from | ||
// the destructors of the individual stat objects, which are not protected by locks. | ||
// A locally held symbol table which encodes stat names as StatNamePtrs and decodes StatNamePtrs | ||
// back into strings. | ||
SymbolTableImpl table_ GUARDED_BY(mutex_); | ||
// A mutex is needed here to protect both the stats_ object and the table_ object from both | ||
// alloc() and free() operations. Although alloc() operations are called under existing locking, | ||
// free() operations are made from the destructors of the individual stat objects, which are not | ||
// protected by locks. | ||
Thread::MutexBasicLockable mutex_; | ||
}; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why the trailing underscore here? and StatNameCompare_ ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I must have thought it was idiomatic for some reason. Will change.