Skip to content
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

Add threadid to AsyncStackRootHolder #644

Merged
merged 14 commits into from
Dec 4, 2024

Conversation

jesswong
Copy link
Contributor

This diff adds the threadid to the ASR in order to unambiguously correlate the ASR's symbol to the thread.

This is loosely based off folly's implementation of getOSThreadID: https://github.com/facebook/folly/blob/main/folly/system/ThreadId.cpp#L41

The main difference is that folly supports EMSCRIPTEN and BSD, which I do not include.

We expect to only need this for ios, android, and linux applications.

@jesswong jesswong requested a review from ispeters November 27, 2024 21:28
@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 27, 2024
Copy link
Contributor

@rileyberton rileyberton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@rileyberton rileyberton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the windows build failed in CI

Copy link
Contributor

@ispeters ispeters left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly nits but there's one possibly non-nit question about whether the syscall in the Linux implementation is expensive.

include/unifex/tracing/async_stack-inl.hpp Outdated Show resolved Hide resolved
include/unifex/tracing/async_stack.hpp Outdated Show resolved Hide resolved
include/unifex/tracing/async_stack.hpp Outdated Show resolved Hide resolved
source/async_stack.cpp Outdated Show resolved Hide resolved
#elif defined(_WIN32)
return uint64_t(GetCurrentThreadId());
#else
return uint64_t(syscall(UNIFEX_SYS_gettid));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we avoid defining UNIFEX_SYS_gettid by just switching here on whether SYS_gettid is defined?

Also, I'm a bit anxious about the cost of a syscall in each AsyncStackRoot constructor since we construct so many of them. Is this a cheap syscall implemented in user space? If so then it doesn't matter, but if it's a typical syscall that transits into kernel space then we should probably revisit capturing this value in the AsyncStackRootHolder since we'll have one of those per thread and the per-thread value of this is constant.

Copy link
Contributor Author

@jesswong jesswong Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I updated this to use the glibc wrapper of gettid because it implements caching (D49174906)

I also spoke to David and his response is that it's difficult to say because it depends on the implementation and we need to profile the code in order to know. This will be used a lot on android and his response is below:

I see. The overhead of the syscall is mostly dependent on what speculative execution mitigations are turned on e.g. is the kernel hardened. I don't know how we configure our Android kernels.
The more mitigations the costlier the overhead of a user to kernel transition
The only real way to know is to profile the code. But as a hunch thread creation should be off the hot path so I don't imagine adding a gettid in there will be problematic

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Synced with @ispeters offline and decided to move threadid to async stack root holder for getting the threadid per thread only

@jesswong jesswong requested a review from ispeters December 2, 2024 21:49
@jesswong jesswong changed the title Add threadid to AsyncStackRoot Add threadid to AsyncStackRootHolder Dec 4, 2024
Copy link
Contributor

@ispeters ispeters left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment on lines 95 to 107
namespace utils {
static std::uint64_t get_os_thread_id() {
# if defined(__APPLE__)
std::uint64_t tid;
pthread_threadid_np(nullptr, &tid);
return tid;
# elif defined(_WIN32)
return std::uint64_t(GetCurrentThreadId());
# else
return std::uint64_t(gettid());
# endif
}
#endif // UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD == 0
} // namespace utils
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since get_os_thread_id is static and inside the unifex namespace, I don't think you don't need the utils namespace.

@jesswong jesswong merged commit ef5d04e into facebookexperimental:main Dec 4, 2024
53 checks passed
@jesswong jesswong deleted the threadids branch December 4, 2024 22:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants