[all-commits] [llvm/llvm-project] b2dea4: [ctxprof] root autodetection mechanism (#133147)

Mircea Trofin via All-commits all-commits at lists.llvm.org
Tue Apr 8 07:00:07 PDT 2025


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: b2dea4fd22b79fa27ef1ebd737401616095a7de6
      https://github.com/llvm/llvm-project/commit/b2dea4fd22b79fa27ef1ebd737401616095a7de6
  Author: Mircea Trofin <mtrofin at google.com>
  Date:   2025-04-08 (Tue, 08 Apr 2025)

  Changed paths:
    M compiler-rt/lib/ctx_profile/CMakeLists.txt
    M compiler-rt/lib/ctx_profile/CtxInstrContextNode.h
    M compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
    M compiler-rt/lib/ctx_profile/CtxInstrProfiling.h
    M compiler-rt/lib/ctx_profile/RootAutoDetector.cpp
    M compiler-rt/lib/ctx_profile/RootAutoDetector.h
    A compiler-rt/test/ctx_profile/TestCases/autodetect-roots.cpp
    M compiler-rt/test/ctx_profile/TestCases/generate-context.cpp
    M llvm/include/llvm/ProfileData/CtxInstrContextNode.h
    M llvm/lib/Transforms/Instrumentation/PGOCtxProfLowering.cpp
    M llvm/test/Transforms/PGOProfile/ctx-instrumentation.ll

  Log Message:
  -----------
  [ctxprof] root autodetection mechanism (#133147)

This is an optional mechanism that automatically detects roots. It's a best-effort mechanism, and its main goal is to *avoid* pointing at the message pump function as a root. This is the function that polls message queue(s) in an infinite loop, and is thus a bad root (it never exits).

High-level, when collection is requested - which should happen when a server has already been set up and handing requests - we spend a bit of time sampling all the server's threads. Each sample is a stack which we insert in a `PerThreadCallsiteTrie`. After a while, we run for each `PerThreadCallsiteTrie` the root detection logic. We then traverse all the `FunctionData`, find the ones matching the detected roots, and allocate a `ContextRoot` for them. From here, we special case `FunctionData` objects, in `__llvm_ctx_profile_get_context, that have a `CtxRoot` and route them to `__llvm_ctx_profile_start_context`.

For this to work, on the llvm side, we need to have all functions call `__llvm_ctx_profile_release_context` because they _might_ be roots. This comes at a slight (percentages) penalty during collection - which we can afford since the overall technique is ~5x faster than normal instrumentation. We can later explore conditionally enabling autoroot detection and avoiding this penalty, if desired. 

Note that functions that `musttail call` can't have their return instrumented this way, and a subsequent patch will harden the mechanism against this case.

The mechanism could be used in combination with explicit root specification, too.



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list