From 455a64f7c9fd575fcd3c17cfec8f80986f3cb159 Mon Sep 17 00:00:00 2001 From: Lucy Date: Sun, 9 Aug 2026 16:15:24 -0400 Subject: [PATCH] update to protocol 82 (tracy 0.14) --- src/server.rs | 80 +++++++++++++++++++++++++++++++++++++++----------- src/structs.rs | 51 ++++++++++++++++++++++++++++++-- 2 files changed, 111 insertions(+), 20 deletions(-) diff --git a/src/server.rs b/src/server.rs index 27a79f4..69710c0 100644 --- a/src/server.rs +++ b/src/server.rs @@ -2,8 +2,10 @@ use crate::reader::ReadWrapper; use crate::structs::{ EventType, HandshakeStatus, NetworkFrameMark, NetworkHeader, NetworkMessageSourceLocation, NetworkMessageString, NetworkQuery, NetworkSourceCode, NetworkThreadContext, NetworkZoneBegin, - NetworkZoneColor, NetworkZoneEnd, QueryResponseType, ServerQueryType, SourceLocation, - U16SizeString, UTracyEvent, UTracyHeader, WriterBox, BINCODE_CONFIG, + NetworkZoneBegin16, NetworkZoneBegin32, NetworkZoneColor, NetworkZoneEnd, NetworkZoneEnd16, + NetworkZoneEnd32, QueryResponseType, ServerQueryType, SourceLocation, U16SizeString, + UTracyEvent, UTracyHeader, WriterBox, BINCODE_CONFIG, PROTOCOL_OFFSET_16BIT, + PROTOCOL_OFFSET_32BIT, PROTOCOL_VERSION, }; use bincode::de::read::Reader; use bincode::error::DecodeError; @@ -22,7 +24,7 @@ struct ServerContext<'l> { writer: BufWriter<&'l TcpStream>, encoder: WriterBox<'l, Vec>, last_thread_id: u32, - timestamp: u64, + timestamp: i64, locations: &'l Vec, strings: &'l HashMap, events_data: ReadWrapper<'static>, @@ -50,22 +52,16 @@ impl ServerContext<'_> { EventType::Begin => { if frame > self.skip_frames { self.check_thread(event.event.begin.thread_id); - self.send_message(NetworkZoneBegin { - query_type: QueryResponseType::ZoneBegin, - timestamp: event.event.begin.timestamp - self.timestamp, - source_location: event.event.begin.source_location.into(), - })?; - self.timestamp = event.event.begin.timestamp; + self.send_zone_begin( + event.event.begin.timestamp as i64, + event.event.begin.source_location.into(), + )?; } } EventType::End => { if frame > self.skip_frames { self.check_thread(event.event.begin.thread_id); - self.send_message(NetworkZoneEnd { - query_type: QueryResponseType::ZoneEnd, - timestamp: event.event.end.timestamp - self.timestamp, - })?; - self.timestamp = event.event.end.timestamp; + self.send_zone_end(event.event.end.timestamp as i64)?; } } EventType::Color => { @@ -181,6 +177,57 @@ impl ServerContext<'_> { Ok(true) } + // funky varint timestamp shit. don't think about it too hard. + fn send_zone_begin(&mut self, timestamp: i64, source_location: u64) -> Result<(), String> { + let dt = timestamp - self.timestamp; + self.timestamp = timestamp; + match dt { + ..0 => self.send_message(NetworkZoneBegin { + query_type: QueryResponseType::ZoneBegin, + timestamp: dt, + source_location, + }), + 0..PROTOCOL_OFFSET_16BIT => self.send_message(NetworkZoneBegin16 { + query_type: QueryResponseType::ZoneBegin16, + timestamp: dt as u16, + source_location, + }), + PROTOCOL_OFFSET_16BIT..PROTOCOL_OFFSET_32BIT => self.send_message(NetworkZoneBegin32 { + query_type: QueryResponseType::ZoneBegin32, + timestamp: (dt - PROTOCOL_OFFSET_16BIT) as u32, + source_location, + }), + _ => self.send_message(NetworkZoneBegin { + query_type: QueryResponseType::ZoneBegin, + timestamp: dt - PROTOCOL_OFFSET_32BIT, + source_location, + }), + } + } + + fn send_zone_end(&mut self, timestamp: i64) -> Result<(), String> { + let dt = timestamp - self.timestamp; + self.timestamp = timestamp; + match dt { + ..0 => self.send_message(NetworkZoneEnd { + query_type: QueryResponseType::ZoneEnd, + timestamp: dt, + }), + 0..PROTOCOL_OFFSET_16BIT => self.send_message(NetworkZoneEnd16 { + query_type: QueryResponseType::ZoneEnd16, + timestamp: dt as u16, + }), + PROTOCOL_OFFSET_16BIT..PROTOCOL_OFFSET_32BIT => self.send_message(NetworkZoneEnd32 { + query_type: QueryResponseType::ZoneEnd32, + timestamp: (dt - PROTOCOL_OFFSET_16BIT) as u32, + }), + _ => self.send_message(NetworkZoneEnd { + query_type: QueryResponseType::ZoneEnd, + timestamp: dt - PROTOCOL_OFFSET_32BIT, + }), + } + } + fn send_message(&mut self, message: W) -> Result<(), String> { if self.encoder.0.len() > 250 * 1024 { self.flush_buffer()? @@ -253,13 +300,12 @@ pub fn handle_client( } let version: u32 = bincode::decode_from_reader(&mut reader, BINCODE_CONFIG).map_err(|e| format!("{}", e))?; - if version != 76 { + if version != PROTOCOL_VERSION { writer .write(&[HandshakeStatus::HandshakeProtocolMismatch as u8]) .map_err(|e| format!("{}", e))?; return Err(format!( - "Invalid client version, expected 76, got {}", - version + "Invalid client version, expected {PROTOCOL_VERSION}, got {version}" )); } diff --git a/src/structs.rs b/src/structs.rs index c090634..2012bf3 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -6,16 +6,20 @@ use bincode::enc::Encoder; use bincode::error::AllowedEnumVariants::{Allowed, Range}; use bincode::error::{DecodeError, EncodeError}; use bincode::{Decode, Encode}; +use clap::ValueEnum; use num_derive::{FromPrimitive, ToPrimitive}; use num_traits::{FromPrimitive, ToPrimitive}; use std::hash::{DefaultHasher, Hash, Hasher}; use std::io::Write; -use clap::ValueEnum; pub const BINCODE_CONFIG: Configuration = bincode::config::standard() .with_little_endian() .with_fixed_int_encoding(); +pub const PROTOCOL_VERSION: u32 = 82; +pub const PROTOCOL_OFFSET_16BIT: i64 = 1 << 16; +pub const PROTOCOL_OFFSET_32BIT: i64 = (1 << 16) + (1 << 32); + #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)] pub enum UtracyFormat { Compressed, @@ -251,14 +255,40 @@ pub struct NetworkQuery { #[derive(Encode, Debug)] pub struct NetworkZoneBegin { pub query_type: QueryResponseType, - pub timestamp: u64, + pub timestamp: i64, + pub source_location: u64, +} + +#[derive(Encode, Debug)] +pub struct NetworkZoneBegin32 { + pub query_type: QueryResponseType, + pub timestamp: u32, + pub source_location: u64, +} + +#[derive(Encode, Debug)] +pub struct NetworkZoneBegin16 { + pub query_type: QueryResponseType, + pub timestamp: u16, pub source_location: u64, } #[derive(Encode, Debug)] pub struct NetworkZoneEnd { pub query_type: QueryResponseType, - pub timestamp: u64, + pub timestamp: i64, +} + +#[derive(Encode, Debug)] +pub struct NetworkZoneEnd32 { + pub query_type: QueryResponseType, + pub timestamp: u32, +} + +#[derive(Encode, Debug)] +pub struct NetworkZoneEnd16 { + pub query_type: QueryResponseType, + pub timestamp: u16, } #[derive(Encode, Debug)] @@ -358,11 +388,21 @@ pub enum QueryResponseType { Callstack, CallstackAlloc, CallstackSample, + CallstackSample32, + CallstackSample16, CallstackSampleContextSwitch, + CallstackSampleContextSwitch32, + CallstackSampleContextSwitch16, FrameImage, ZoneBegin, + ZoneBegin32, + ZoneBegin16, ZoneBeginCallstack, + ZoneBeginCallstack32, + ZoneBeginCallstack16, ZoneEnd, + ZoneEnd32, + ZoneEnd16, LockWait, LockObtain, LockRelease, @@ -405,6 +445,9 @@ pub enum QueryResponseType { SourceCodeMetadata, FiberEnter, FiberLeave, + SectionEnter, + SectionLeave, + SectionSetup, Terminate, KeepAlive, ThreadContext, @@ -446,6 +489,8 @@ pub enum QueryResponseType { CpuTopology, SingleStringData, SecondStringData, + SingleStringData8, + SecondStringData8, MemNamePayload, ThreadGroupHint, GpuZoneAnnotation,