Repository navigation
trace: C++ pretty printer (ze first), byte-identical to the Ruby printer - #560
< 10BC8 /div>Draft
TApplencourt wants to merge 9 commits into
Draft
TApplencourt wants to merge 9 commits into
TApplencourt wants to merge 9 commits into
Conversation
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>
TApplencourt
force-pushed
the
pretty-cxx
branch
from
November 9, 2026 21:10
5c4ac57 to
e8cdaf2
Compare
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Draft: replace the Ruby pretty printer of
babeltrace_thapi trace/iprof -twith C++, starting with ze. Needs the BLOB fields of #543 (with the oldctf_sequence_textencoding, babeltrace 2.1 cuts packed structs at the first NUL).Design
filter.<backend>pretty.pretty: a metababel FILTER per backend. Its callbacks are generated byutils/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) andbackends/<be>/pretty_rules.yaml. Seeutils/pretty_print/README.md. Each ze event becomes onethapi_pretty:lineevent 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 ofxprof/btx_pretty_model.yaml(callbacks inxprof/btx_pretty_print_callbacks.cpp): prints the lines in order. Needs metababel's newon_upstreamcallback (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 gemField#to_s), unless--restrict.utils/include/thapi_pretty.hpp: Ruby-compatible primitives (Float#to_s,String#inspectof binary strings,"0x%016x",Time#strftime).--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 butcl, so the default backend list), hybrid: the filters in front of the Ruby sink, which prints their lines as-is and theclevents with its Ruby printers.THAPI_PRETTY=ruby|cxxforces one.Checks
The output is byte-identical to the Ruby printer:
sycl-ls,sycl-ls --verbose, a SYCL vector add, with sampling, and a 20000-launch loop), with and without-cmake check:backends/ze9/9,xprof2/2.The golden files are not in this PR (they are large; run locally for now).
Speed (221,467 lines, Sunspot login node)
babeltrace_thapi trace -b zebabeltrace_thapi trace(default backends, hybrid)iprof -b ze -t -rAbout 0.3 s of the C++ time is babeltrace reading the trace.
Found on the way
ze_external_memory_{import,export}_win32_handle_t. Here they print as plain structs.ZETypedValue#to_ssits on a misspelled class. Herezet_typed_value_tprints its value; other unions print all their members.babeltrace2CLI is built by icx with fast math, so it runs with DAZ/FTZ, and libstdc++to_charsthen prints denormals as 0.thapi_pretty.hppclears DAZ/FTZ while it formats a denormal.Next (in this PR)
fmt::formatterfor the C structs and enums (btx_zepretty_formatters.hpp; e.g.fmt::format("{}", props))zet_typed_value_t, see Ruby pretty printer prints ze union members as an object address (and ZETypedValue#to_s is on a misspelled class) #561)cl, keeps the Ruby printer)on_upstream, the sink counterpart ofon_downstream(Routing: --enable-additional-callbacks and --upstream-event-default (breaking) TApplencourt/metababel#158); the line printer is now a metababel sinkto_son the same bytes: ze 708 types, cuda 87, hip 90 (incl. its C++ vector types), mpi 2, itt 12, all identical except Ruby pretty printer crashes on ze_external_memory_{import,export}_win32_handle_t (Handle#to_s on a struct without data) #559/Ruby pretty printer prints ze union members as an object address (and ZETypedValue#to_s is on a misspelled class) #561to_chars, no MXCSR),fmt::formatters in a separate header, plugins export only the babeltrace descriptors, smaller generated code (ze 27.7k -> 17.7k lines), sink fixes🤖 Generated with Claude Code