forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 341
[lldb] Cache Task pointer location and Task names #10894
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
Open
felipepiovezan
wants to merge
3
commits into
swiftlang:swift/release/6.2
Choose a base branch
from
felipepiovezan:felipe/cache_tls
base: swift/release/6.2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
c83f071
[lldb][swift] Cache location of Task Pointers for OperatingSystemSwif…
felipepiovezan 45d8e62
[lldb][swift] Add target option for caching task ptr locations
felipepiovezan 5da29ab
[lldb][swift] Cache and lazily-evaluate Task names in OperatingSystem…
felipepiovezan 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -2325,8 +2325,9 @@ class CommandObjectLanguageSwiftTaskInfo final : public CommandObjectParsed { | |
return; | ||
} | ||
|
||
auto task_addr_or_err = | ||
GetTaskAddrFromThreadLocalStorage(m_exe_ctx.GetThreadRef()); | ||
TaskInspector task_inspector; | ||
auto task_addr_or_err = task_inspector.GetTaskAddrFromThreadLocalStorage( | ||
m_exe_ctx.GetThreadRef()); | ||
if (auto error = task_addr_or_err.takeError()) { | ||
result.AppendError(toString(std::move(error))); | ||
return; | ||
|
@@ -2939,17 +2940,29 @@ std::optional<lldb::addr_t> SwiftLanguageRuntime::TrySkipVirtualParentProlog( | |
return pc_value + prologue_size; | ||
} | ||
|
||
llvm::Expected<lldb::addr_t> GetTaskAddrFromThreadLocalStorage(Thread &thread) { | ||
/// Attempts to read the memory location at `task_addr_location`, producing | ||
/// the Task pointer if possible. | ||
static llvm::Expected<lldb::addr_t> | ||
ReadTaskAddr(lldb::addr_t task_addr_location, Process &process) { | ||
Status status; | ||
addr_t task_addr = process.ReadPointerFromMemory(task_addr_location, status); | ||
if (status.Fail()) | ||
return llvm::createStringError("could not get current task from thread: %s", | ||
status.AsCString()); | ||
return task_addr; | ||
} | ||
|
||
/// Compute the location where the Task pointer for `real_thread` is stored by | ||
/// the runtime. | ||
static llvm::Expected<lldb::addr_t> | ||
ComputeTaskAddrLocationFromThreadLocalStorage(Thread &real_thread) { | ||
#if !SWIFT_THREADING_USE_RESERVED_TLS_KEYS | ||
return llvm::createStringError( | ||
"getting the current task from a thread is not supported"); | ||
#else | ||
// Compute the thread local storage address for this thread. | ||
addr_t tsd_addr = LLDB_INVALID_ADDRESS; | ||
|
||
// Look through backing threads when inspecting TLS. | ||
Thread &real_thread = | ||
thread.GetBackingThread() ? *thread.GetBackingThread() : thread; | ||
if (auto info_sp = real_thread.GetExtendedInfo()) | ||
if (auto *info_dict = info_sp->GetAsDictionary()) | ||
info_dict->GetValueForKeyAsInteger("tsd_address", tsd_addr); | ||
|
@@ -2958,18 +2971,48 @@ llvm::Expected<lldb::addr_t> GetTaskAddrFromThreadLocalStorage(Thread &thread) { | |
return llvm::createStringError("could not read current task from thread"); | ||
|
||
// Offset of the Task pointer in a Thread's local storage. | ||
Process &process = *thread.GetProcess(); | ||
Process &process = *real_thread.GetProcess(); | ||
size_t ptr_size = process.GetAddressByteSize(); | ||
uint64_t task_ptr_offset_in_tls = | ||
swift::tls_get_key(swift::tls_key::concurrency_task) * ptr_size; | ||
addr_t task_addr_location = tsd_addr + task_ptr_offset_in_tls; | ||
Status status; | ||
addr_t task_addr = process.ReadPointerFromMemory(task_addr_location, status); | ||
if (status.Fail()) | ||
return llvm::createStringError("could not get current task from thread: %s", | ||
status.AsCString()); | ||
return task_addr; | ||
return tsd_addr + task_ptr_offset_in_tls; | ||
#endif | ||
} | ||
|
||
llvm::Expected<lldb::addr_t> | ||
TaskInspector::GetTaskAddrFromThreadLocalStorage(Thread &thread) { | ||
// Look through backing threads when inspecting TLS. | ||
Thread &real_thread = | ||
thread.GetBackingThread() ? *thread.GetBackingThread() : thread; | ||
|
||
if (auto it = m_tid_to_task_addr_location.find(real_thread.GetID()); | ||
it != m_tid_to_task_addr_location.end()) { | ||
#ifndef NDEBUG | ||
// In assert builds, check that caching did not produce incorrect results. | ||
llvm::Expected<lldb::addr_t> task_addr_location = | ||
ComputeTaskAddrLocationFromThreadLocalStorage(real_thread); | ||
assert(task_addr_location); | ||
assert(it->second == *task_addr_location); | ||
#endif | ||
return ReadTaskAddr(it->second, *thread.GetProcess()); | ||
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. if this read fails, should the cache entry be evicted? |
||
} | ||
|
||
llvm::Expected<lldb::addr_t> task_addr_location = | ||
ComputeTaskAddrLocationFromThreadLocalStorage(real_thread); | ||
if (!task_addr_location) | ||
return task_addr_location; | ||
|
||
llvm::Expected<lldb::addr_t> task_addr = | ||
ReadTaskAddr(*task_addr_location, *thread.GetProcess()); | ||
|
||
// If the read from this TLS address is successful, cache the TLS address. | ||
// Caching without a valid read is dangerous: earlier in the thread | ||
// lifetime, the result of GetExtendedInfo can be invalid. | ||
if (task_addr && | ||
real_thread.GetProcess()->GetTarget().GetSwiftCacheTaskPtrLocation()) | ||
m_tid_to_task_addr_location.try_emplace(real_thread.GetID(), | ||
*task_addr_location); | ||
return task_addr; | ||
} | ||
|
||
namespace { | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
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.
The asserts look unlikely to break, if they're going to make asserts builds slow, should this be
#ifdef EXPENSIVE_CHECKS
instead?Uh oh!
There was an error while loading. Please reload this page.
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.
The uncertainty here is sort of why we thought of putting the assert: can we truly cache TLS? Nobody I talked to was able to give me a definitive answer, and experimenting failed to produce counter examples... Looking at the code of libpthread also did not produce a good definitive answer :/
Sort of! They will be "slow" for slow connections only. On a wired connection, the slowdown we're talking about is a step operating going from 200ms to 300ms.
Completely disabling the Task plugin would make the step be about 100ms.