From e7f75310ab1ecb14c913b26ab815b4a4c8abe0ed Mon Sep 17 00:00:00 2001 From: hal8174 Date: Thu, 13 Feb 2025 22:37:39 +0100 Subject: [PATCH] first commit --- .gitignore | 2 + Cargo.lock | 450 ++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 12 ++ build.rs | 5 + src/lib.rs | 33 ++++ src/main.rs | 132 ++++++++++++++ src/trace.proto | 75 ++++++++ 7 files changed, 709 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 build.rs create mode 100644 src/lib.rs create mode 100644 src/main.rs create mode 100644 src/trace.proto diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dee4f33 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +target +out.trace diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..932f2f7 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,450 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] +name = "anyhow" +version = "1.0.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34ac096ce696dc2fcabef30516bb13c0a68a11d30131d3df6f04711467681b04" + +[[package]] +name = "bitflags" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f68f53c83ab957f72c32642f3868eec03eb974d1fb82e453128456482613d36" + +[[package]] +name = "bytes" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f61dac84819c6588b558454b194026eb1f09c293b9036ae9b159e74e73ab6cf9" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "either" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" + +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + +[[package]] +name = "errno" +version = "0.3.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d" +dependencies = [ + "libc", + "windows-sys", +] + +[[package]] +name = "fastrand" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" + +[[package]] +name = "fixedbitset" +version = "0.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d674e81391d1e1ab681a28d99df07927c6d4aa5b027d7da16ba32d1d21ecd99" + +[[package]] +name = "getrandom" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43a49c392881ce6d5c3b8cb70f98717b7c07aabbdff06687b9030dbfbe2725f8" +dependencies = [ + "cfg-if", + "libc", + "wasi", + "windows-targets", +] + +[[package]] +name = "hashbrown" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "indexmap" +version = "2.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c9c992b02b5b4c94ea26e32fe5bccb7aa7d9f390ab5c1221ff895bc7ea8b652" +dependencies = [ + "equivalent", + "hashbrown", +] + +[[package]] +name = "itertools" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285" +dependencies = [ + "either", +] + +[[package]] +name = "libc" +version = "0.2.169" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" + +[[package]] +name = "linux-raw-sys" +version = "0.4.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d26c52dbd32dccf2d10cac7725f8eae5296885fb5703b261f7d0a0739ec807ab" + +[[package]] +name = "log" +version = "0.4.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04cbf5b083de1c7e0222a7a51dbfdba1cbe1c6ab0b15e29fff3f6c077fd9cd9f" + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "multimap" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "defc4c55412d89136f966bbb339008b474350e5e6e78d2714439c386b3137a03" + +[[package]] +name = "once_cell" +version = "1.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "945462a4b81e43c4e3ba96bd7b49d834c6f61198356aa858733bc4acf3cbe62e" + +[[package]] +name = "petgraph" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3672b37090dbd86368a4145bc067582552b29c27377cad4e0a306c97f9bd7772" +dependencies = [ + "fixedbitset", + "indexmap", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" + +[[package]] +name = "prettyplease" +version = "0.2.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6924ced06e1f7dfe3fa48d57b9f74f55d8915f5036121bef647ef4b204895fac" +dependencies = [ + "proc-macro2", + "syn", +] + +[[package]] +name = "proc-macro2" +version = "1.0.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60946a68e5f9d28b0dc1c21bb8a97ee7d018a8b322fa57838ba31cc878e22d99" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "prost" +version = "0.13.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2796faa41db3ec313a31f7624d9286acf277b52de526150b7e69f3debf891ee5" +dependencies = [ + "bytes", + "prost-derive", +] + +[[package]] +name = "prost-build" +version = "0.13.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be769465445e8c1474e9c5dac2018218498557af32d9ed057325ec9a41ae81bf" +dependencies = [ + "heck", + "itertools", + "log", + "multimap", + "once_cell", + "petgraph", + "prettyplease", + "prost", + "prost-types", + "regex", + "syn", + "tempfile", +] + +[[package]] +name = "prost-derive" +version = "0.13.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a56d757972c98b346a9b766e3f02746cde6dd1cd1d1d563472929fdd74bec4d" +dependencies = [ + "anyhow", + "itertools", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "prost-types" +version = "0.13.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52c2c1bf36ddb1a1c396b3601a3cec27c2462e45f07c386894ec3ccf5332bd16" +dependencies = [ + "prost", +] + +[[package]] +name = "quote" +version = "1.0.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "regex" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" + +[[package]] +name = "rustix" +version = "0.38.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fdb5bc1ae2baa591800df16c9ca78619bf65c0488b41b96ccec5d11220d8c154" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys", +] + +[[package]] +name = "syn" +version = "2.0.98" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36147f1a48ae0ec2b5b3bc5b537d267457555a10dc06f3dbc8cb11ba3006d3b1" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "tempfile" +version = "3.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38c246215d7d24f48ae091a2902398798e05d978b24315d6efbc00ede9a8bb91" +dependencies = [ + "cfg-if", + "fastrand", + "getrandom", + "once_cell", + "rustix", + "windows-sys", +] + +[[package]] +name = "tracing" +version = "0.1.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0" +dependencies = [ + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "395ae124c09f9e6918a2310af6038fba074bcf474ac352496d5910dd59a2226d" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing-core" +version = "0.1.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e672c95779cf947c5311f83787af4fa8fffd12fb27e4993211a84bdfd9610f9c" +dependencies = [ + "once_cell", +] + +[[package]] +name = "tracing-perfetto" +version = "0.1.0" +dependencies = [ + "prost", + "prost-build", + "prost-types", + "tracing", +] + +[[package]] +name = "unicode-ident" +version = "1.0.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a210d160f08b701c8721ba1c726c11662f877ea6b7094007e1ca9a1041945034" + +[[package]] +name = "wasi" +version = "0.13.3+wasi-0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26816d2e1a4a36a2940b96c5296ce403917633dff8f3440e9b236ed6f6bacad2" +dependencies = [ + "wit-bindgen-rt", +] + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "wit-bindgen-rt" +version = "0.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3268f3d866458b787f390cf61f4bbb563b922d091359f9608842999eaee3943c" +dependencies = [ + "bitflags", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..18b8b43 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "tracing-perfetto" +version = "0.1.0" +edition = "2021" + +[dependencies] +prost = "0.13.5" +prost-types = "0.13.5" +tracing = "0.1.41" + +[build-dependencies] +prost-build = "0.13.5" diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..354f48e --- /dev/null +++ b/build.rs @@ -0,0 +1,5 @@ +extern crate prost_build; + +fn main() { + prost_build::compile_protos(&["src/trace.proto"], &["src/"]).unwrap(); +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..2711b99 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,33 @@ +use tracing::{span, Subscriber}; + +struct PerfettoSubscriber {} + +impl Subscriber for PerfettoSubscriber { + fn enabled(&self, metadata: &tracing::Metadata<'_>) -> bool { + true + } + + fn new_span(&self, span: &span::Attributes<'_>) -> span::Id { + todo!() + } + + fn record(&self, span: &span::Id, values: &span::Record<'_>) { + todo!() + } + + fn record_follows_from(&self, span: &span::Id, follows: &span::Id) { + todo!() + } + + fn event(&self, event: &tracing::Event<'_>) { + todo!() + } + + fn enter(&self, span: &span::Id) { + todo!() + } + + fn exit(&self, span: &span::Id) { + todo!() + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..7571c34 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,132 @@ +use prost::Message; + +mod perfetto { + include!(concat!(env!("OUT_DIR"), "/perfetto.trace.rs")); +} + +fn main() { + let mut packets = Vec::new(); + packets.push(perfetto::TracePacket { + timestamp: None, + data: Some(perfetto::trace_packet::Data::TrackDescriptor( + perfetto::TrackDescriptor { + uuid: Some(894893984), + parent_uuid: None, + static_or_dynamic_name: None, + process: Some(perfetto::ProcessDescriptor { + pid: Some(1234), + process_name: Some(String::from("test")), + }), + thread: None, + }, + )), + optional_trusted_packet_sequence_id: Some( + perfetto::trace_packet::OptionalTrustedPacketSequenceId::TrustedPacketSequenceId( + 3903809, + ), + ), + }); + packets.push(perfetto::TracePacket { + timestamp: None, + data: Some(perfetto::trace_packet::Data::TrackDescriptor( + perfetto::TrackDescriptor { + uuid: Some(49083589894), + parent_uuid: None, + static_or_dynamic_name: None, + process: None, + thread: Some(perfetto::ThreadDescriptor { + pid: Some(1234), + thread_name: Some(String::from("test_thread")), + tid: Some(10), + }), + }, + )), + optional_trusted_packet_sequence_id: Some( + perfetto::trace_packet::OptionalTrustedPacketSequenceId::TrustedPacketSequenceId( + 3903809, + ), + ), + }); + + packets.push(perfetto::TracePacket { + timestamp: Some(100), + data: Some(perfetto::trace_packet::Data::TrackEvent( + perfetto::TrackEvent { + track_uuid: Some(49083589894), + categories: vec![], + category_iids: vec![], + name_field: Some(perfetto::track_event::NameField::Name(String::from("Test"))), + r#type: Some(perfetto::track_event::Type::Instant as i32), + }, + )), + optional_trusted_packet_sequence_id: Some( + perfetto::trace_packet::OptionalTrustedPacketSequenceId::TrustedPacketSequenceId( + 3903809, + ), + ), + }); + + packets.push(perfetto::TracePacket { + timestamp: Some(200), + data: Some(perfetto::trace_packet::Data::TrackEvent( + perfetto::TrackEvent { + track_uuid: Some(49083589894), + categories: vec![], + category_iids: vec![], + name_field: Some(perfetto::track_event::NameField::Name(String::from("Test"))), + r#type: Some(perfetto::track_event::Type::SliceBegin as i32), + }, + )), + optional_trusted_packet_sequence_id: Some( + perfetto::trace_packet::OptionalTrustedPacketSequenceId::TrustedPacketSequenceId( + 3903809, + ), + ), + }); + + packets.push(perfetto::TracePacket { + timestamp: Some(400), + data: Some(perfetto::trace_packet::Data::TrackEvent( + perfetto::TrackEvent { + track_uuid: Some(49083589894), + categories: vec![], + category_iids: vec![], + name_field: Some(perfetto::track_event::NameField::Name(String::from("Test"))), + r#type: Some(perfetto::track_event::Type::Instant as i32), + }, + )), + optional_trusted_packet_sequence_id: Some( + perfetto::trace_packet::OptionalTrustedPacketSequenceId::TrustedPacketSequenceId( + 3903809, + ), + ), + }); + + packets.push(perfetto::TracePacket { + timestamp: Some(600), + data: Some(perfetto::trace_packet::Data::TrackEvent( + perfetto::TrackEvent { + track_uuid: Some(49083589894), + categories: vec![], + category_iids: vec![], + name_field: Some(perfetto::track_event::NameField::Name(String::from("Test"))), + r#type: Some(perfetto::track_event::Type::SliceEnd as i32), + }, + )), + optional_trusted_packet_sequence_id: Some( + perfetto::trace_packet::OptionalTrustedPacketSequenceId::TrustedPacketSequenceId( + 3903809, + ), + ), + }); + + let trace = perfetto::Trace { packet: packets }; + + let mut buf = Vec::with_capacity(trace.encoded_len()); + + trace.encode(&mut buf).unwrap(); + + std::fs::write("out.trace", buf); + + dbg!(trace); +} diff --git a/src/trace.proto b/src/trace.proto new file mode 100644 index 0000000..2456b4d --- /dev/null +++ b/src/trace.proto @@ -0,0 +1,75 @@ +syntax = "proto3"; + +package perfetto.trace; + +message Trace { + repeated TracePacket packet = 1; +} + +// https://cs.android.com/android/platform/superproject/main/+/main:external/perfetto/protos/perfetto/trace/trace_packet.proto +message TracePacket { + optional uint64 timestamp = 8; + + oneof data { + TrackEvent track_event = 11; + TrackDescriptor track_descriptor = 60; + } + + oneof optional_trusted_packet_sequence_id { + uint32 trusted_packet_sequence_id = 10; + } +} + +// https://cs.android.com/android/platform/superproject/main/+/main:external/perfetto/protos/perfetto/trace/track_event/track_event.proto +message TrackEvent { + repeated uint64 category_iids = 3; + repeated string categories = 22; + + oneof name_field { + uint64 name_iid = 10; + string name = 23; + } + + enum Type { + TYPE_UNSPECIFIED = 0; + TYPE_SLICE_BEGIN = 1; + TYPE_SLICE_END = 2; + TYPE_INSTANT = 3; + TYPE_COUNTER = 4; + } + + optional Type type = 9; + + optional uint64 track_uuid = 11; + +} + +// https://cs.android.com/android/platform/superproject/main/+/main:external/perfetto/protos/perfetto/trace/track_event/track_descriptor.proto +message TrackDescriptor { + optional uint64 uuid = 1; + optional uint64 parent_uuid = 5; + + oneof static_or_dynamic_name { + string name = 2; + string static_name = 10; + string atrace_name = 13; + } + + optional ProcessDescriptor process = 3; + optional ThreadDescriptor thread = 4; +} + + +// https://cs.android.com/android/platform/superproject/main/+/main:external/perfetto/protos/perfetto/trace/track_event/process_descriptor.proto +message ProcessDescriptor { + optional int32 pid = 1; + optional string process_name = 6; +} + +// https://cs.android.com/android/platform/superproject/main/+/main:external/perfetto/protos/perfetto/trace/track_event/thread_descriptor.proto +message ThreadDescriptor { + optional int32 pid = 1; + optional int32 tid = 2; + + optional string thread_name = 5; +}