Skip to content

observe: an installed sink costs 8-11% per local call (global mutex + shared_ptr copy per emit) #1177

Backlog

Description

@Yaraslaut

Summary

With a metric or trace sink installed, every local execute pays roughly 600–800 extra user-space instructions, which is 8–11% of the whole call. Every emit takes one process-wide mutex and copies a shared_ptr to the sink, which is one atomic read-modify-write on a shared cache line. LocalBackend also bumps one shared in-flight counter twice per call. Builds without a sink pay one relaxed load per emit site. Builds with MORPH_ENABLE_OBSERVABILITY=OFF pay nothing.

Evidence (measured)

User-space retired instructions per local execute round trip. The count comes from perf_event_open with inherit, so pool workers are counted too. The model holds one double, and every call is waited out before the next one starts.

Revision No sink Sinks installed Delta
dbc812a22 (before #1176) ~7400 ~8000 about +600 (+8%)
715a30728 (in #1176; pool spin off) 7544 8353 +809 (+10.7%)
  • Hardware: x86-64 Linux, 12 cores.
  • Build: Release, GCC 16.2 / libstdc++ 16.2.
  • Calls in flight: one.
  • Probe: a scratch probe, not committed. tests/bench/bench_dispatch_instructions.cpp measures the no-sink figure the same way and could take a --with-sink mode.

Not verified

  • Where the cost goes. The mutex, the shared_ptr refcount, the std::function call, building the tag array and the sink body itself are not separated. A lock-free read would remove only the first two.
  • Contention. Every figure above has one call in flight. The cost of the shared mutex and counter cache lines with many threads emitting at once was not measured. It is the likelier place for a large effect, and it is unknown.
  • Counter sharding. Whether a sharded executeInFlight counter, or one maintained only while a sink is present, is worth it was not measured on its own.

Directions

These were considered and not implemented in #1176.

  • Read the sink through an atomic<const Sink*> instead of mutex + shared_ptr. The catch is lifetime. A reader can still be inside a sink while another thread replaces it, so replaced sinks must be retained, not freed. A host that toggles sinks repeatedly then grows without bound, and captures are never destroyed. That semantic change is why it was deferred. Hazard pointers or an epoch scheme avoid it at more complexity.
  • Shard the in-flight counter, or keep it only while a metric sink is installed. The second option needs care, because executeInFlight would then report relative counts.

What would change the verdict

  • Close if a --with-sink instruction count, with a matching many-threads throughput figure, shows the mutex and refcount at under about 2% of a call.
  • Raise priority if a many-threads run with a sink shows throughput falling well below the no-sink figure, because that would point at contention rather than per-call cost.

Activity

  1. added
    enhancement
    area: core
    on Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: coreenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions