Skip to content

PerfTools/Perfetto: in-process Perfetto tracing service - #51271

Open
felicepantaleo wants to merge 4 commits into
cms-sw:masterfrom
felicepantaleo:perfetto-use-external
Open

PerfTools/Perfetto: in-process Perfetto tracing service#51271
felicepantaleo wants to merge 4 commits into
cms-sw:masterfrom
felicepantaleo:perfetto-use-external

Conversation

@felicepantaleo

@felicepantaleo felicepantaleo commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

Adds PerfTools/Perfetto, an EDM service (PerfettoTraceService) that records an in-process Perfetto (https://perfetto.dev) trace (.pftrace) of a cmsRun job, openable by drag-and-drop at https://perfetto.web.cern.ch , entirely client-side, together with a small dependency-free monitor hook in HeterogeneousCore/AlpakaInterface that the Alpaka caching allocator uses to report device-memory traffic.
What it records:

  • module / acquire / EventSetup / source / cleanup slices on a per-(stream, thread) lane under each edm::stream, so independent modules running concurrently within a stream, and an ExternalWork module's acquire()/produce() running on different threads — nest correctly without overlapping or mis-paired slices;
  • a global Throughput (events/s) counter plus per-stream run/lumi/event counters;
  • (optional, traceAllocations) Alpaka caching-allocator transactions: each alloc/free attributed to the module that triggered it, plus per-device live/cached/requested device-memory counters;
  • (optional, traceGpuKernels) CUDA kernel activity via CUPTI: real device-side start/end, registers per thread, static and dynamic shared memory, per-thread and total local memory, an estimated occupancy, and the CUPTI correlation id linking each kernel back to the host module that launched it;
  • (optional, tracePower) CPU (RAPL) and GPU (NVML) power as counter tracks, at a configurable sampling period;
  • CMS_PERFETTO_FUNC()/CMS_PERFETTO_SCOPE() macros for optional intra-module instrumentation, and a traceModules filter for focused, low-overhead runs.

Everything beyond the per-stream slices and counters is opt-in and off by default: with the optional features disabled the per-allocation cost is a single relaxed atomic load, and disabled trace categories cost only a predicated load.

Usage: cmsDriver.py … --customise PerfTools/Perfetto/customisePerfetto.customise, or add the service directly; see PerfTools/Perfetto/README.md

@rovere @makortel @fwyzard

@cmsbuild

cmsbuild commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

cms-bot internal usage

@cmsbuild

Copy link
Copy Markdown
Contributor

-code-checks

Logs: https://cmssdt.cern.ch/SDT/code-checks/cms-sw-PR-51271/49862

ERROR: Build errors found during clang-tidy run.

src/PerfTools/Perfetto/interface/CMSSWPerfettoCategories.h:4:10: error: 'perfetto.h' file not found [clang-diagnostic-error]
    4 | #include <perfetto.h>
      |          ^~~~~~~~~~~~
Found compiler error(s).
--
src/PerfTools/Perfetto/interface/CMSSWPerfettoCategories.h:4:10: error: 'perfetto.h' file not found [clang-diagnostic-error]
    4 | #include <perfetto.h>
      |          ^~~~~~~~~~~~
Suppressed 250 warnings (250 in non-user code).
--
gmake: *** [config/SCRAM/GMake/Makefile.coderules:129: code-checks] Error 2
gmake: *** [There are compilation/build errors. Please see the detail log above.] Error 2

@felicepantaleo

Copy link
Copy Markdown
Contributor Author

Demonstration video
Screencast_20260620_153823.webm

@felicepantaleo

Copy link
Copy Markdown
Contributor Author

test parameters:

@felicepantaleo

Copy link
Copy Markdown
Contributor Author

@cmsbuild please test

@felicepantaleo

Copy link
Copy Markdown
Contributor Author

type ngt

@cmsbuild cmsbuild added the ngt label Jun 20, 2026
@felicepantaleo

Copy link
Copy Markdown
Contributor Author

@cmsbuild code-checks

@cmsbuild

Copy link
Copy Markdown
Contributor

-code-checks

Logs: https://cmssdt.cern.ch/SDT/code-checks/cms-sw-PR-51271/49863

ERROR: Build errors found during clang-tidy run.

src/PerfTools/Perfetto/interface/CMSSWPerfettoCategories.h:4:10: error: 'perfetto.h' file not found [clang-diagnostic-error]
    4 | #include <perfetto.h>
      |          ^~~~~~~~~~~~
Found compiler error(s).
--
src/PerfTools/Perfetto/interface/CMSSWPerfettoCategories.h:4:10: error: 'perfetto.h' file not found [clang-diagnostic-error]
    4 | #include <perfetto.h>
      |          ^~~~~~~~~~~~
Suppressed 250 warnings (250 in non-user code).
--
gmake: *** [config/SCRAM/GMake/Makefile.coderules:129: code-checks] Error 2
gmake: *** [There are compilation/build errors. Please see the detail log above.] Error 2

@felicepantaleo

Copy link
Copy Markdown
Contributor Author

@cmsbuild please test with cms-sw/cmsdist#10668

@felicepantaleo

Copy link
Copy Markdown
Contributor Author

@cmsbuild code-checks with cms-sw/cmsdist#10668

@felicepantaleo

Copy link
Copy Markdown
Contributor Author

@cmsbuild code-checks with cms.week0.PR_596340/56.1

@felicepantaleo

Copy link
Copy Markdown
Contributor Author

@cmsbuild code-checks with cms.week0_PR_596340/56.1

@smuzaffar

Copy link
Copy Markdown
Contributor

code-checks with cms.week0.PR_3f29859a/100.0-cced86a6d5071160d38b54fd5b3ba33d

@@ -0,0 +1,101 @@
// Original author: Felice Pantaleo, felice.pantaleo@cern.ch, 02/2026
#pragma once

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AFAIK we do not use #pragma once in CMSSW.

@fwyzard

fwyzard commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

+heterogenous

@felicepantaleo

Copy link
Copy Markdown
Contributor Author

+heterogenous

for some reason this did not work, so I've removed pragma onces

@felicepantaleo

Copy link
Copy Markdown
Contributor Author

@cmsbuild

@felicepantaleo

Copy link
Copy Markdown
Contributor Author

@cmsbuild please test

@cmsbuild

Copy link
Copy Markdown
Contributor

+code-checks

Logs: https://cmssdt.cern.ch/SDT/code-checks/cms-sw-PR-51271/49968

@cmsbuild

Copy link
Copy Markdown
Contributor

Pull request #51271 was updated. @fwyzard, @makortel can you please check and sign again.

@cmsbuild

Copy link
Copy Markdown
Contributor

+1

Size: This PR adds an extra 32KB to repository
Summary: https://cmssdt.cern.ch/SDT/jenkins-artifacts/pull-request-integration/PR-9ef445/54322/summary.html
COMMIT: 16b55d1
CMSSW: CMSSW_20_1_X_2026-06-26-1100/el9_amd64_gcc13
User test area: For local testing, you can use /cvmfs/cms-ci.cern.ch/week1/cms-sw/cmssw/51271/54322/install.sh to create a dev area with all the needed externals and cmssw changes.

Comparison Summary

Summary:

  • You potentially added 8 lines to the logs
  • Reco comparison results: 0 differences found in the comparisons
  • DQMHistoTests: Total files compared: 45
  • DQMHistoTests: Total histograms compared: 3414477
  • DQMHistoTests: Total failures: 0
  • DQMHistoTests: Total nulls: 0
  • DQMHistoTests: Total successes: 3414459
  • DQMHistoTests: Total skipped: 18
  • DQMHistoTests: Total Missing objects: 0
  • DQMHistoSizes: Histogram memory added: 0.0 KiB( 44 files compared)
  • Checked 195 log files, 163 edm output root files, 45 DQM output files
  • TriggerResults: no differences found

@fwyzard

fwyzard commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

+heterogenous

for some reason this did not work, so I've removed pragma onces

Ah... that's because my phone has decided to write "heterogenous" instead of "heterogeneous", and it doesn't seem possible to make it re-learn the correct spelling :-(

@fwyzard

fwyzard commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

+heterogeneous

@felicepantaleo

Copy link
Copy Markdown
Contributor Author

@cms-sw/orp-l2 this is now ready for merging.

@fwyzard

fwyzard commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

@felicepantaleo, there is still @makortel's question about adding the new package, and in particular of who should be responsible for reviewing and signing for it.

@felicepantaleo

Copy link
Copy Markdown
Contributor Author

@felicepantaleo, there is still @makortel's question about adding the new package, and in particular of who should be responsible for reviewing and signing for it.

cms-sw/cms-bot#2802
I've put it under core like the rest of PerfTools.

@cmsbuild

Copy link
Copy Markdown
Contributor

Pull request #51271 was updated. @Dr15Jones, @makortel, @smuzaffar can you please check and sign again.

@Dr15Jones

Copy link
Copy Markdown
Contributor

The Tracer service already can create a perfecto trace.

@felicepantaleo

Copy link
Copy Markdown
Contributor Author

The Tracer service already can create a perfecto trace.

fair point, and you're right that the Tracer can already produce a Perfetto-viewable trace. The difference is how: the Tracer converts its text log offline (edmTracerCompactLogViewer.py --web), whereas this service writes a native .pftrace in-process, so you can profile the application live as it runs rather than post-processing a log.
It also captures things the Tracer doesn't: throughput/event counters, CPU/GPU power, GPU kernels via CUPTI (real device timing, occupancy, and the correlation back to the launching module), and optional per-function profiling.
If you have a minute, two example traces from this service are here.. worth opening to see the GPU and counter tracks alongside the module timeline:
https://felice.web.cern.ch/orbit/?path=%2Fdata%2Fperfetto-traces

@felicepantaleo

Copy link
Copy Markdown
Contributor Author

any other comment? @smuzaffar @Dr15Jones @makortel

@Dr15Jones

Copy link
Copy Markdown
Contributor

@makortel is on vacation for the next week or so. The full review will happen once he's back.

@makortel

Copy link
Copy Markdown
Contributor

I don't see anything here that would require this package to be under core. Given the close connection to GPUs, and direct dependence on HeterogeneousCore/AlpakaInterface and CUDA/CUPTI, being under heterogeneous would seem more natural. Another possibility could be reconstruction given the emphasis on reconstruction developers.

The dependence on CUDA makes me wonder if HeterogeneousCore/CUDAServices package could be used, but on the other hand the dependence on HeterogeneousCore/AlpakaInterface counters that. I'm indifferent between HeterogeneousCore and PerfTools.

I can take a deeper look in the coming days.

@ericcano

ericcano commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

I think this PR can go as it is now.

  • The stack is slightly inefficient but functional (see suggestion inline).
  • The system to subscribe to the tracking of memory allocation is disjoint from the framework's activity registry, but this is justified by the much higher rate of memory allocation warranted the search for a solution as light as possible. It is also independent from the Perfetto service and therefor allows monitoring in other systems, which is nice.
  • The CUPTI hooks (and some future ROCprofiler-SDK ones?) can be seen as belonging to the Perfetto service module as the tracking of kernels is already provided by the provide's tools (nsys, rocprofv3).
  • The power monitoring hooks are also valuable. On the long run, I would suggest to add signals to the Activity monitor and make the CUDAService/ROCmService emit them optionally so they are available globally (NVTX,ROCTX). They have a very low rate (by default).

The value of making this PR available now for memory management optimization outweighs the tiny details I mentioned, that will cost extra delays.

PS: there is also a glitch on the event rate for the very first events: (It's high, should be low):
image

// A small fixed-size, allocation-free per-thread stack. Module re-entrancy
// from work-stealing is shallow in practice; beyond the cap we keep counting
// depth (so push/pop stay balanced) but stop recording, and report no module.
constexpr int kMaxDepth = 64;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We don't need a stack here. Tasks are not preempted by TBB, nor suspended by us (in which future case we would need to stop/restart the range using new FW signals).

Pre/post signals guaranteed as thread local here are also guaranteed to not intersect with each other within a thread (@makortel ?).

A successful run with this patch (suggested) confirmed it:

 // Original author: Felice Pantaleo, felice.pantaleo@cern.ch, 02/2026
 #include "PerfTools/Perfetto/interface/CMSSWPerfettoModuleContext.h"
 
+#include <cassert>
+
 namespace cms::perfetto {
   namespace {
     // A small fixed-size, allocation-free per-thread stack. Module re-entrancy
     // from work-stealing is shallow in practice; beyond the cap we keep counting
     // depth (so push/pop stay balanced) but stop recording, and report no module.
-    constexpr int kMaxDepth = 64;
-    thread_local ModuleContext g_stack[kMaxDepth];
-    thread_local int g_depth = 0;
+    thread_local ModuleContext g_context;
+    thread_local bool g_present = false;
     const ModuleContext g_none{};
   }  // namespace
 
   void pushModuleContext(ModuleContext const& ctx) noexcept {
-    if (g_depth >= 0 && g_depth < kMaxDepth)
-      g_stack[g_depth] = ctx;
-    ++g_depth;
+    assert(not g_present);
+    g_present = true;
+    g_context = ctx;
   }
 
   void popModuleContext() noexcept {
-    if (g_depth > 0)
-      --g_depth;
+    assert(g_present);
+    g_present = false;
   }
 
-  void resetModuleContext() noexcept { g_depth = 0; }
+  void resetModuleContext() noexcept { g_present = false; }
 
   ModuleContext const& currentModuleContext() noexcept {
-    if (g_depth > 0 && g_depth <= kMaxDepth)
-      return g_stack[g_depth - 1];
+    if (g_present) {
+      return g_context;
+    }
     return g_none;
   }
 }  // namespace cms::perfetto

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We don't need a stack here. Tasks are not preempted by TBB, nor suspended by us (in which future case we would need to stop/restart the range using new FW signals).

If a module uses TBB's parallel constructs, e.g. tbb::parallel_for() without enclosing the call in tbb::this_task_arena::isolate(), that parallel_for() can steal tasks from other activities in other threads.

While our recommendation is to isolate all TBB parallel calls, it is not enforced.

Seed the sliding rate window with the start of the first event: at startup all
streams complete their first (slow) event almost simultaneously, so a window of
completion times alone spans only that tight burst and reports a spuriously
high rate exactly when the job is at its slowest.
@cmsbuild

cmsbuild commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

+code-checks

Logs: https://cmssdt.cern.ch/SDT/code-checks/cms-sw-PR-51271/50430

@cmsbuild

cmsbuild commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Pull request #51271 was updated. @Dr15Jones, @cmsbuild, @makortel, @smuzaffar can you please check and sign again.

@felicepantaleo

Copy link
Copy Markdown
Contributor Author

PS: there is also a glitch on the event rate for the very first events: (It's high, should be low): image

Right, the throughput counter's sliding window was built from event-completion times only, at startup all N streams complete their first (slow) event nearly simultaneously, so the window spanned just that burst and read spuriously high (up to 4.9 ev/s vs 1.4 ev/s steady in an 8-stream NGT-scouting test). The window is now seeded with the first event start, so the counter starts at ~1/T(first event) and ramps up honestly: first samples 0.74 → 0.94 → 1.30 → … → 1.40 ev/s steady, no spike.

@felicepantaleo

Copy link
Copy Markdown
Contributor Author

@cmsbuild please test

@cmsbuild

cmsbuild commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

+1

Size: This PR adds an extra 28KB to repository
Summary: https://cmssdt.cern.ch/SDT/jenkins-artifacts/pull-request-integration/PR-9ef445/55068/summary.html
COMMIT: 228704a
CMSSW: CMSSW_20_1_X_2026-08-03-2300/el9_amd64_gcc13
User test area: For local testing, you can use /cvmfs/cms-ci.cern.ch/week1/cms-sw/cmssw/51271/55068/install.sh to create a dev area with all the needed externals and cmssw changes.

Comparison Summary

Summary:

  • You potentially added 1 lines to the logs
  • ROOTFileChecks: Some differences in event products or their sizes found
  • Reco comparison results: 0 differences found in the comparisons
  • DQMHistoTests: Total files compared: 45
  • DQMHistoTests: Total histograms compared: 3588680
  • DQMHistoTests: Total failures: 11
  • DQMHistoTests: Total nulls: 0
  • DQMHistoTests: Total successes: 3588651
  • DQMHistoTests: Total skipped: 18
  • DQMHistoTests: Total Missing objects: 0
  • DQMHistoSizes: Histogram memory added: 0.0 KiB( 44 files compared)
  • Checked 195 log files, 163 edm output root files, 45 DQM output files
  • TriggerResults: no differences found

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants