Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 63 additions & 17 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,7 +24,7 @@ struct ServerContext<'l> {
writer: BufWriter<&'l TcpStream>,
encoder: WriterBox<'l, Vec<u8>>,
last_thread_id: u32,
timestamp: u64,
timestamp: i64,
locations: &'l Vec<SourceLocation>,
strings: &'l HashMap<u64, String>,
events_data: ReadWrapper<'static>,
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -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<W: Encode>(&mut self, message: W) -> Result<(), String> {
if self.encoder.0.len() > 250 * 1024 {
self.flush_buffer()?
Expand Down Expand Up @@ -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}"
));
}

Expand Down
51 changes: 48 additions & 3 deletions src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<LittleEndian, Fixint> = 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,
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -405,6 +445,9 @@ pub enum QueryResponseType {
SourceCodeMetadata,
FiberEnter,
FiberLeave,
SectionEnter,
SectionLeave,
SectionSetup,
Terminate,
KeepAlive,
ThreadContext,
Expand Down Expand Up @@ -446,6 +489,8 @@ pub enum QueryResponseType {
CpuTopology,
SingleStringData,
SecondStringData,
SingleStringData8,
SecondStringData8,
MemNamePayload,
ThreadGroupHint,
GpuZoneAnnotation,
Expand Down