8000 trace: C++ pretty printer (ze first), byte-identical to the Ruby printer by TApplencourt · Pull Request #560 · argonne-lcf/THAPI · GitHub
[go: up one dir, main page]

Skip to content

trace: C++ pretty printer (ze first), byte-identical to the Ruby printer - #560

< 10BC8 /div>
Draft
TApplencourt wants to merge 9 commits into
develfrom
pretty-cxx
Draft

TApplencourt wants to merge 9 commits into
develfrom
pretty-cxx

Conversation

@TApplencourt
@TApplencourt TApplencourt commented Nov 9, 2026 •
Copy link
Copy Markdown
Collaborator

Draft: replace the Ruby pretty printer of babeltrace_thapi trace / iprof -t with C++, starting with ze. Needs the BLOB fields of #543 (with the old ctf_sequence_text encoding, babeltrace 2.1 cuts packed structs at the first NUL).

Design

  • filter.<backend>pretty.pretty: a metababel FILTER per backend. Its callbacks are generated by utils/pretty_print/gen_pretty.rb, a standalone tool that reads only YAML: the event model (btx_<be>_model.yaml), the h2yaml API models (<be>_api.yaml) and backends/<be>/pretty_rules.yaml. See utils/pretty_print/README.md. Each ze event becomes one thapi_pretty:line event with the same timestamp. Every other event is forwarded, so the per-backend filters chain like the interval filters.
  • sink.thapipretty.text: a metababel SINK of xprof/btx_pretty_model.yaml (callbacks in xprof/btx_pretty_print_callbacks.cpp): prints the lines in order. Needs metababel's new on_upstream callback (Routing: --enable-additional-callbacks and --upstream-event-default (breaking) TApplencourt/metababel#158). An event that no filter formatted is printed like the Ruby printer prints an event it has no printer for (the bt2 gem Field#to_s), unless --restrict.
  • utils/include/thapi_pretty.hpp: Ruby-compatible primitives (Float#to_s, String#inspect of binary strings, "0x%016x", Time#strftime).
  • fmt: header-only. --with-fmt=PREFIX, else the system fmt, else the bundled copy (utils/include/fmt_bundled, fmt 12.1.0, MIT).
  • babeltrace_thapi trace: when every selected backend has a pretty filter, filters + sink.thapipretty.text. When only some do (today: all but cl, so the default backend list), hybrid: the filters in front of the Ruby sink, which prints their lines as-is and the cl events with its Ruby printers. THAPI_PRETTY=ruby|cxx forces one.

Checks

The output is byte-identical to the Ruby printer:

  • 5 real ze traces (lttng 2.16, CTF 2; sycl-ls, sycl-ls --verbose, a SYCL vector add, with sampling, and a 20000-launch loop), with and without -c
  • 59 synthetic events, one group per rendering case: unknown enum/flag values, NaN/Inf/denormals/exponent forms, escapes and invalid UTF-8, null/nested structs, struct arrays, raw and static blobs
  • the primitives against Ruby on 1.4M random values (g++ and icpx, two time zones, with and without DAZ/FTZ)

make check: backends/ze 9/9, xprof 2/2.

The golden files are not in this PR (they are large; run locally for now).

Speed (221,467 lines, Sunspot login node)

C++ Ruby
babeltrace_thapi trace -b ze 0.88 s 10.3 s
babeltrace_thapi trace (default backends, hybrid) 4.75 s 10.3 s
iprof -b ze -t -r 0.92 s 15.9 s

About 0.3 s of the C++ time is babeltrace reading the trace.

Found on the way

Next (in this PR)

🤖 Generated with Claude Code

TApplencourt and others added 8 commits November 9, 2026 20:56
The `trace` command printed every event in Ruby (sink.text.rubypretty). This
adds a C++ path that prints the same text, byte for byte, about 13-19x faster:

- filter.zepretty.pretty: a metababel FILTER over btx_ze_model.yaml. Its
  callbacks are generated by utils/gen_cxx_pretty.rb. Each ze event becomes a
  `thapi_pretty:line` event; every other event is forwarded, so one filter per
  backend chains like the interval filters.
- sink.thapipretty.text (xprof/btx_pretty_sink.cpp): prints the lines in order.
  An event no filter formatted is printed like the Ruby printer prints an event
  it has no printer for.
- utils/include/thapi_pretty.hpp: Ruby-compatible primitives (Float#to_s,
  String#inspect, "0x%016x", Time#strftime). Zero, sign and denormals are
  handled on the bits, so the output does not depend on DAZ/FTZ.
- configure: fmt, header-only: --with-fmt=PREFIX, else the system fmt, else
  the copy bundled in utils/include/fmt_bundled (fmt 12.1.0, MIT).
- babeltrace_thapi: `trace` uses the C++ chain when every selected backend has
  a pretty filter (today: `-b ze`), else the Ruby printer.
  THAPI_PRETTY=ruby|cxx forces one.

The generator still reads the FFI library (ze_library.rb) for enum names,
bitmask flags and struct layouts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
gen_cxx_pretty.rb now writes two files: the filter callbacks and
btx_zepretty_formatters.hpp, an fmt::formatter for each C struct and enum the
events carry (319 for ze). The generated code reads struct members by name
from the C types, so `fmt::format("{}", props)` prints a
ze_device_properties_t the way the Ruby printer does. Flags (uint32_t
typedefs) and pointers are printed by explicit calls: fmt cannot tell them
from integers.

The header also checks, with static_assert, that sizeof and every member
offset of the C types equal the FFI layout the Ruby printer reads (1322
checks for ze; all hold).

The generator is split in two: a frontend that describes the types (today
from the FFI library) and an emitter that writes C++ from that description,
so the frontend can move to the YAML models.

thapi_pretty.hpp: the primitives write to an fmt::appender, so the
formatters can call them.

Output unchanged: byte-identical to the Ruby printer on all references.
The generated ze file compiles in 1m20 instead of 2m (icpx -O2).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
gen_cxx_pretty.rb now reads the backend's API model (gen_ze_library_base.rb:
API, NAMING) instead of the Ruby FFI library ze_library.rb. The C++ code
prints most values by their C type (overloads of `pv`); the API model gives
only what the C type cannot: the order of the struct members, which integer
members hold flags, and which structs print in a special way (UUID, Handle,
`_version_t`). Enum values are never evaluated in Ruby: the generated code
names the enumerators and the compiler gives the values. The bitmask rules of
gen_library_base.rb (a positive power of two is a flag, 0 the default name)
are `constexpr` tests on those values.

Checked against the FFI-based generator: 478000 random values of the 239
reachable ze structs print the same, except zet_typed_value_t (below).
Byte-identical to the Ruby printer on all references.

zet_typed_value_t prints its value, as the to_s gen_ze_library.rb defines
for it was meant to: that to_s sits on a misspelled class, and the Ruby
printer prints the union as an object address (#561). Other unions print all
their members.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Two differences with the Ruby printer, found by pushing 20 random events of
every ze event class through both printers (33311 lines, now identical):

- FFI gives an int8 *or uint8* inline array as a CharArray, whose to_s is the
  string up to the first NUL (e.g. ze_device_luid_ext_t.id prints "L", not
  [ 76, 0, ... ]). The generator only did that for int8.
- A float denormal widened to double under DAZ (the babeltrace2 CLI built by
  icx runs with DAZ) reads as 0. thapi_pretty::widen() widens it with integer
  arithmetic, and ruby_float() has a float overload. The primitive test now
  also covers 200000 single-precision values, half of them denormals.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Each backend gets a filter.<backend>pretty.pretty, generated at build time
like the ze one (backends/<backend>/Makefile.am). gen_cxx_pretty.rb learns
what the other Ruby FFI libraries do differently:

- cuda, hip, mpi: a struct named *UUID* prints its `bytes` in order
  (print_handle_uuid_modules), not back to front as in ze.
- cuda drops CU_COREDUMP_LIGHTWEIGHT_FLAGS, omp drops ompt_callback_master
  (checked: no later enumerator depends on them for its value).
- cuda declares `typedef :pointer, CUdeviceptr` (an integer in C): printed
  as a pointer.
- hip prints enums known by their tag only.
- alias typedefs (`typedef CUdevprop_v1 CUdevprop`) resolve to their target.
- C bit-fields: FFI merges a run into `_aggregated_bitfields_N`, an integer of
  the first one's type (hipDeviceArch_t).
- unnamed members (an anonymous union in CUgraphNodeParams): `_unamed_N`.
- a signed field shown in hexadecimal prints as Ruby does: "0x..fffb".
- pytorch has no API model: `-` instead of the library base.

Checked by pushing 20 random events of every event class through the Ruby
printer and the C++ chain: identical for cuda (27400 events), hip (17880),
mpi (52980), omp (640), itt (180), pytorch (100) and ze (33300), except the
events where Ruby prints a union as an object address (#561).

babeltrace_thapi: `trace` uses the C++ chain when every selected backend that
has a Ruby printer also has a pretty filter. cxi has no Ruby printer (its
events always take the fallback), so it does not count. Only cl (OpenCL)
remains, so the default backend list still selects the Ruby printer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ream)

sink.thapipretty.text was a hand-written babeltrace component. It is now a
metababel SINK of xprof/btx_pretty_model.yaml, generated at build time: the
`thapi_pretty:line` events come to a typed callback, every other event to the
new metababel `on_upstream` callback (TApplencourt/metababel#158), which
prints it like the Ruby printer prints an event it has no printer for.
xprof/btx_pretty_print_callbacks.cpp keeps only the printing.

The `restrict` parameter is now `restricted`: metababel turns parameters into
C struct members, and `restrict` is a C keyword.

Needs a metababel with on_upstream (#158); configure still checks for
>= 2.0.0 until a release has it.

Unchanged output: the references, `make check` (54), and random events of
every event class of every backend, printed with the C++ chain hosted in
Ruby as babeltrace_thapi runs it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… type

Found by testing the generated formatters on their own: random objects of
every struct and union type of each API (not only the ones events carry),
printed with fmt and compared with Ruby FFI `to_s` on the same bytes.

- FFI reads a :bool as the low bit of its byte (2 prints false, 3 true).
  A bool member is now read that way (hipArray.isDrv).
- A member of C scalar type is printed through a cast to that type. A no-op
  for C structs; for hip's vector types, C++ classes whose x/y/z/w are
  Scalar_accessor wrappers, it calls their conversion.
- PRETTY_ALL_TYPES=1 (tests only) generates a printer for every struct and
  union of the API, and writes the FFI class of each next to the header.

Results (50 random objects per type; besides these, Ruby raises on the two
Win32 handle structs, #559, or prints a union as an address, #561):
  ze 708 types: 34850 identical, 0 differ
  cuda 87: 3600 identical, 0 differ;  hip 90: 4200, 0 differ
  mpi 2: 100, 0 differ;  itt 12: 600, 0 differ
  (omp: its FFI library binds no struct, Ruby never prints one)
The deepest types are covered, e.g. CUgraphNodeParams (anonymous union >
struct > struct > struct) and zes_fan_config_t (struct > array of struct >
struct).

Event output unchanged: references, make check (54), and random events of
every event class of every backend.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
utils/pretty_print/gen_pretty.rb replaces utils/gen_cxx_pretty.rb. It reads
only YAML, and none of THAPI's Ruby model code:

- the babeltrace event model (btx_<backend>_model.yaml);
- the API model h2yaml writes from the C headers (<backend>_api.yaml ...):
  struct members, enumerators, typedefs;
- backends/<backend>/pretty_rules.yaml: what neither file says, as type names:
  the structs printed in a special way (UUIDs, IPC handles, typed values),
  ze's `_version_t` enums, dropped enumerators, and cuda's CUdeviceptr printed
  as a pointer. They were derived once from the old rules (class-name matches
  in the Ruby FFI libraries), so the behaviour is the same.

The generated code is unchanged: for every backend, the callbacks and the
formatters are identical to the ones the old generator wrote (except the
header comment).

utils/pretty_print/pretty.am holds the make rules every backend shares; a
backend sets PRETTY_BE, PRETTY_MODEL, PRETTY_API and PRETTY_INC, includes it,
and spells out its library (automake needs the name). thapi_pretty.hpp moves
next to the generator.

`--all-types` generates a formatter for every struct and union of the API,
not only those the events carry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
From the review of #560:

- Default runs now use the C++ printers. While some selected backends have no
  pretty filter yet (cl), `trace` puts the filters in front of the Ruby sink,
  which prints their lines as they are and the other events with its Ruby
  printers (about 2x faster than Ruby on a ze trace). When every backend has a
  filter: filters + sink.thapipretty.text (about 12x). THAPI_PRETTY=ruby|cxx
  forces one; THAPI_PRETTY=cxx no longer fails on a backend without a filter
  (its events take the C++ fallback).
- fmt: configure requires fmt >= 11 (the versions the output was checked with),
  else uses the bundled copy; --with-fmt=no selects the bundled copy.
- Float#to_s takes its shortest digits from fmt (Dragonbox) instead of
  floating-point std::to_chars (libstdc++ >= 11 only); the x86 MXCSR code is
  gone: fmt works on the bits, so the output does not depend on DAZ/FTZ
  (checked with and without DAZ, with fmt 11 and 12, g++ and icpx).
- The fmt::formatter specializations move to btx_<be>pretty_fmt.hpp, which
  the plugins do not compile; the printers are in btx_<be>pretty.hpp.
- The plugins export only the babeltrace descriptors (-fvisibility=hidden, and
  the generated callbacks are in an anonymous namespace): 20 symbols, from
  about 2000 (fmt included).
- The sink no longer calls setvbuf on stdout, and prints enumeration and
  variant fields, and names that are Ruby symbols, as the bt2 gem does.
- Generated code: `_Bool` is spelled `bool` in the callbacks (`#undef` after
  metababel.h); context arguments are c0, c1...; one callback counter for all
  stream classes; a static_assert keeps a bit-field read inside the struct.

Smaller generated code: the callbacks share a head (time, context, event name)
and a tail (push the line); the struct printers are the `pv` overloads
themselves; no mask on 64-bit fields; one uuid printer. ze: 27700 -> 17700
lines, mpi: 47900 -> 32000.

utils/pretty_print/README.md describes the tool, the rules and the graph.

Unchanged output: the references (all three modes), `make check` (54), random
events of every event class of every backend, and random objects of every
API struct (ze 708 types, cuda 87, hip 90, mpi 2, itt 12) against Ruby FFI.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0