Currently ThreadInfo objects all share gBuffer, while SyncProfile objects each
get their own ProfileBuffer.
This patch removes ThreadInfo::mBuffer to reduce this difference, taking us a
step towards eliminating SyncProfile.
To support this, the patch:
- passes in a buffer as an additional argument in a bunch of places where the
buffer used to be obtained from a ThreadInfo;
- adds an mBuffer field to ProfilerBacktrace;
- changes ThreadInfo::SetProfile() to SetHasProfile();
- removes ThreadInfo::{addTag,StoredMarker,bufferGeneration}(), all of which
just redirected to ThreadInfo anyway;
- changes ProfileBuffer so it's no longer refcounted, which is unnecessary now
that gBuffer and ProfilerBacktrace::mBuffer don't have multiple references,
which makes their lifetimes obvious.
The patch also removes some ThreadInfo& args in functions in platform.cpp, in
places where that ThreadInfo is available within the accompanying TickSampler*
arg.
The MacOS and Windows profiler cores have a threading structure where one
thread ("sampler thread") collects information from a thread to be profiled
("samplee thread") by suspending it, getting its register state, unwinding its
stack, and then resuming it. This requires kernel-level primitives to perform
the suspend, get-registers and resume steps.
The Linux/Android core is different, because none of those three primitives
exist. Until now, data collection has been done by sending a SIGPROF to the
samplee, and collecting all relevant data within the signal handler. This has
a number of disadvantages:
(1) Current work to rationalise/clean up the threading structure of the
profiler is complicated by the need to reason about/verify two different
schemes.
In particular, the Tick call in the Windows and MacOS implementations will
produce its output on the sampler thread. In the Linux implementation
that is produced on the sampled threads.
(2) Dynamic verification results (primarily, absence of data races and
deadlocks) established for the Linux implementation are less likely to
carry over to the other two implementations, because the threading
structures are different.
(3) It causes a lot of duplicated code in platform-*.cpp. For example
SamplerThread::Run() in the -win32.cpp and -macos.cpp files are very
similar. Ideally all three could be merged into a single file with the
identical logic commoned up.
(4) Running lots of code -- the entire contents of Tick -- in a signal handler
isn't considered good practice. POSIX severely restricts the set of
functions we can safely call from within a signal handler.
This commit changes the Linux implementation by using semaphores to implement
the suspend and resume primitives, and moves the bulk of the data collection
work to the sampler thread. By doing this, it causes the Linux implementation
to have the same threading structure as the other two.
This patch does the following.
- Uses "entries" consistently for the name of the value that is obtained from
MOZ_PROFILER_ENTRIES and is the first argument to profiler_start(). (I.e. not
"entry" or "entrySize".)
- Removes variables (e.g. PROFILER_HELP) holding env var names and uses the
names (e.g. "MOZ_PROFILER_HELP") directly. Some of the names are already used
directly and I think the slight repetition isn't harmful. It's unlikely that
we'd want to change these names the way we might need to change a numeric
value, and they're perfectly descriptive.
- Changes the "MOZ_PROFILING_FEATURES" string in the weird Android-only startup
code to be "MOZ_PROFILER_FEATURES", for consistency.
- Renames gUnwindInterval and gProfileEntries as gEnvVarInterval and
gEnvVarEntries to make it clearer that they come from environment variables,
but otherwise are parallel to gInterval and gEntries.
- Puts entries before intervals in most places, to match the profiler_start()
argument order.
- Changes profiler_usage() so that (a) it always prints, no matter the
verbosity, (b) it exits at its end, and (c) doesn't double-print "Profiler: "
at the start of each line.
PlatformStart() and PlatformStop() are currently responsible for setting and
clearing gIsActive, but it's better if we do it in profiler_{start,stop}().
The patch also does the following.
- Adds some missing emacs/vim modelines.
- Makes Platform{Start,Stop}() crash if they have failures. I'm not at all
confident that ignoring the errors as is currently done will result in
sensible behaviour, so brittleness is better.
This avoids the need for platform-linux-android.cpp to read gInterval off the
main thread in an awkward spot. It also makes platform-linux-android.cpp
more like platform-{win32,macos}.cpp.
- Don't bother checking gSampler in ProfilerSignalHandler. It is equivalent
to checking gIsActive and we do that at the top of the loop in
SignalSender(). There is no point repeatedly checking the same condition in
the middle of that loop; that just opens up the possibility of partially
complete samples where some threads are missing.
- Clear gCurrentThreadInfo in SignalSender() instead of in
ProfilerSignalHandler(). The effect is much the same, but this change means
gCurrentThreadInfo is both set and cleared in SignalSender(), i.e. on a
single thread, removing any need for Atomic<>.
We don't need OS now that the platform-*.cpp files are in the same compilation
unit as platform.cpp.
The patch removes the sleep functions because they are unnecessary indirection.
OS::Startup() is necessary, but the patch renames it PlatformInit() to match
Platform{Start,Stop}() and profiler_init(), from which it is called.
Specifically:
- platform-linux.cc -> platform-linux-android.cpp
- platform-macos.cc -> platform-macos.cpp
- platform-win32.cc -> platform-win32.cpp
Adding "android" to the first one is the most important part, because it makes
things clearer. The .cc to .cpp change is less important but I might as well do
it while I'm in here.