From c6aaf8edd8f41bbd4369985c0d0dd480c1239063 Mon Sep 17 00:00:00 2001 From: Mark Mansi Date: Sat, 13 Apr 2019 18:59:48 -0500 Subject: [PATCH] initial --- .gitignore | 2 + Cargo.lock | 26 +++++++++++++ Cargo.toml | 9 +++++ src/main.rs | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 140 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f0e3bca --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +**/*.rs.bk \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..0d804a3 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,26 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "libc" +version = "0.2.51" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "num_cpus" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "zerosim-trace" +version = "0.1.0" +dependencies = [ + "libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)", + "num_cpus 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[metadata] +"checksum libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)" = "bedcc7a809076656486ffe045abeeac163da1b558e963a31e29fbfbeba916917" +"checksum num_cpus 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1a23f0ed30a54abaa0c7e83b1d2d87ada7c3c23078d1d87815af3e3b6385fbba" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..c7c8bcd --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "zerosim-trace" +version = "0.1.0" +authors = ["Mark Mansi "] +edition = "2018" + +[dependencies] +libc = "0.2.51" +num_cpus = "1.10.0" diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..fb09dd5 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,103 @@ +use libc::syscall; + +const BEGIN_SYSCALL_NR: i64 = 546; +const SNAPSHOT_SYSCALL_NR: i64 = 547; +const SIZE_SYSCALL_NR: i64 = 548; + +const PER_CPU_TRACE_BUFFER_SIZE: usize = 1_000_000; + +const ZEROSIM_TRACE_TASK_SWITCH: u32 = 0x0000_0001; +const ZEROSIM_TRACE_INTERRUPT: u32 = 0x0000_0002; +const ZEROSIM_TRACE_FAULT: u32 = 0x0000_0004; +const ZEROSIM_TRACE_SYSCALL: u32 = 0x0000_0008; +const ZEROSIM_TRACE_START: u32 = 0x0000_0010; + +#[derive(Copy, Clone)] +#[repr(C)] +struct Trace { + timestamp: u64, + id: u32, + flags: u32, +} + +impl std::fmt::Debug for Trace { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!( + f, + "{{ {:<15} {:5} ts: {}, flags: {:8b}, id: {} }}", + if self.flags & ZEROSIM_TRACE_TASK_SWITCH != 0 { + "TASK_SWITCH" + } else if self.flags & ZEROSIM_TRACE_INTERRUPT != 0 { + "INTERRUPT" + } else if self.flags & ZEROSIM_TRACE_FAULT != 0 { + "FAULT" + } else if self.flags & ZEROSIM_TRACE_SYSCALL != 0 { + "SYSCALL" + } else { + "??" + }, + if self.flags & ZEROSIM_TRACE_START != 0 { + "START" + } else { + "" + }, + self.timestamp, + self.flags, + self.id, + ) + } +} + +#[derive(Debug, Clone)] +struct Snapshot { + pub buffer: Vec, +} + +fn main() { + size(); + + begin(); + + std::thread::sleep(std::time::Duration::from_secs(5)); + + let snap = snapshot(); + + println!("{:#?}", snap.buffer.iter().take(50).collect::>()); +} + +fn size() { + let ret = unsafe { syscall(SIZE_SYSCALL_NR, 1<<12) }; + if ret != 0 { + unsafe { libc::perror(std::ptr::null_mut()); } + panic!(); + } +} + +fn begin() { + let ret = unsafe { syscall(BEGIN_SYSCALL_NR) }; + if ret != 0 { + unsafe { libc::perror(std::ptr::null_mut()); } + panic!(); + } +} + +fn snapshot() -> Snapshot { + let mut buffer = Vec::with_capacity(PER_CPU_TRACE_BUFFER_SIZE * num_cpus::get()); + + let ret = unsafe { + let ptr = buffer.as_mut_ptr(); + let cap = buffer.capacity(); + + syscall(SNAPSHOT_SYSCALL_NR, ptr, cap) + }; + if ret != 0 { + unsafe { libc::perror(std::ptr::null_mut()); } + panic!(); + } + + unsafe { + buffer.set_len(buffer.capacity()); + } + + Snapshot { buffer } +}