From: Zhao Liu <zhao1.liu@intel.com>
To: "Stefan Hajnoczi" <stefanha@redhat.com>,
"Mads Ynddal" <mads@ynddal.dk>,
"Paolo Bonzini" <pbonzini@redhat.com>,
"Philippe Mathieu-Daudé" <philmd@linaro.org>,
"Peter Maydell" <peter.maydell@linaro.org>,
"Alex Bennée" <alex.bennee@linaro.org>,
"Daniel P . Berrangé" <berrange@redhat.com>,
"Thomas Huth" <thuth@redhat.com>,
"Markus Armbruster" <armbru@redhat.com>
Cc: qemu-devel@nongnu.org, Zhao Liu <zhao1.liu@intel.com>
Subject: [RFC 5/6] scripts/simpletrace-rust: Format simple trace output
Date: Mon, 27 May 2024 16:14:20 +0800 [thread overview]
Message-ID: <20240527081421.2258624-6-zhao1.liu@intel.com> (raw)
In-Reply-To: <20240527081421.2258624-1-zhao1.liu@intel.com>
Format simple trace output, as in the Python version. Further, complete
the trace file input and trace log output.
Additionally, remove `#![allow(dead_code)]` and
`#![allow(unused_variables)]` to allow rustc to do related checks.
Suggested-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Zhao Liu <zhao1.liu@intel.com>
---
scripts/simpletrace-rust/src/main.rs | 80 ++++++++++++++++++++++++++--
1 file changed, 75 insertions(+), 5 deletions(-)
diff --git a/scripts/simpletrace-rust/src/main.rs b/scripts/simpletrace-rust/src/main.rs
index f9a71d8dc243..ba4b96efb066 100644
--- a/scripts/simpletrace-rust/src/main.rs
+++ b/scripts/simpletrace-rust/src/main.rs
@@ -8,23 +8,25 @@
* SPDX-License-Identifier: GPL-2.0-or-later
*/
-#![allow(dead_code)]
-#![allow(unused_variables)]
-
mod trace;
use std::collections::HashMap;
use std::env;
+use std::fmt;
use std::fs::File;
+use std::io::stdout;
use std::io::Error as IOError;
use std::io::ErrorKind;
use std::io::Read;
+use std::io::Write;
use std::mem::size_of;
use backtrace::Backtrace;
use clap::Arg;
use clap::Command;
use thiserror::Error;
+use trace::read_events;
+use trace::Error as TraceError;
use trace::Event;
const DROPPED_EVENT_ID: u64 = 0xfffffffffffffffe;
@@ -45,8 +47,12 @@ pub enum Error
InvalidHeaderId(u64, u64),
#[error("Not a valid trace file, header magic {0} != {1}")]
InvalidHeaderMagic(u64, u64),
+ #[error("Failed to open file: {0}")]
+ OpenFile(IOError),
#[error("Failed to read file: {0}")]
ReadFile(IOError),
+ #[error("Failed to read trace: {0}")]
+ ReadTrace(TraceError),
#[error(
"event {0} is logged but is not declared in the trace events \
file, try using trace-events-all instead."
@@ -58,6 +64,8 @@ pub enum Error
UnknownVersion(u64),
#[error("Log format {0} not supported with this QEMU release!")]
UnsupportedVersion(u64),
+ #[error("Failed to write trace: {0}")]
+ WriteTrace(IOError),
}
pub type Result<T> = std::result::Result<T, Error>;
@@ -254,6 +262,30 @@ impl EventArgPayload
}
}
+impl fmt::Display for EventArgPayload
+{
+ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result
+ {
+ if let Some(s) = &self.raw_str {
+ write!(fmt, "{}", s)
+ } else {
+ write!(fmt, "")
+ }
+ }
+}
+
+impl fmt::LowerHex for EventArgPayload
+{
+ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result
+ {
+ if let Some(v) = self.raw_val {
+ write!(fmt, "{:x}", v)
+ } else {
+ write!(fmt, "")
+ }
+ }
+}
+
#[derive(Clone)]
struct EventEntry
{
@@ -485,10 +517,35 @@ impl Analyzer for Formatter
event: &Event,
timestamp_ns: u64,
pid: u32,
- event_id: u64,
+ _event_id: u64,
) -> Result<String>
{
- let fmt_str = String::new();
+ if self.last_timestamp_ns.is_none() {
+ self.last_timestamp_ns = Some(timestamp_ns);
+ }
+
+ let fields: Vec<String> = rec_args
+ .iter()
+ .zip(event.args.props.iter())
+ .map(|(arg, prop)| {
+ if prop.is_string() {
+ format!("{}={}", prop.name, arg)
+ } else {
+ format!("{}=0x{:x}", prop.name, arg)
+ }
+ })
+ .collect();
+
+ let delta_ns =
+ timestamp_ns as f64 - self.last_timestamp_ns.unwrap() as f64;
+ self.last_timestamp_ns = Some(timestamp_ns);
+ let fmt_str = format!(
+ "{} {:.3} pid={} {}\n",
+ event.name,
+ delta_ns / 1000.0,
+ pid,
+ fields.join(" "),
+ );
Ok(fmt_str)
}
@@ -501,7 +558,20 @@ fn process(
read_header: bool,
) -> Result<()>
{
+ let events = read_events(event_path).map_err(Error::ReadTrace)?;
+ let trace_fobj = File::open(trace_path).map_err(Error::OpenFile)?;
+ if read_header {
+ read_trace_header(&trace_fobj)?;
+ }
+
analyzer.begin();
+
+ let rec_strs =
+ read_trace_records(&events, &trace_fobj, analyzer, read_header)?;
+ let mut lock = stdout().lock();
+ lock.write_all(rec_strs.join("").as_ref())
+ .map_err(Error::WriteTrace)?;
+
analyzer.end();
Ok(())
--
2.34.1
next prev parent reply other threads:[~2024-05-27 8:01 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-05-27 8:14 [RFC 0/6] scripts: Rewrite simpletrace printer in Rust Zhao Liu
2024-05-27 8:14 ` [RFC 1/6] scripts/simpletrace-rust: Add the basic cargo framework Zhao Liu
2024-05-27 20:05 ` Stefan Hajnoczi
2024-05-28 7:53 ` Zhao Liu
2024-05-28 14:14 ` Stefan Hajnoczi
2024-05-29 14:30 ` Zhao Liu
2024-05-29 18:41 ` Stefan Hajnoczi
2024-05-31 12:22 ` Daniel P. Berrangé
2024-05-27 8:14 ` [RFC 2/6] scripts/simpletrace-rust: Support Event & Arguments in trace module Zhao Liu
2024-05-27 20:33 ` Stefan Hajnoczi
2024-05-28 8:32 ` Zhao Liu
2024-05-27 8:14 ` [RFC 3/6] scripts/simpletrace-rust: Add helpers to parse trace file Zhao Liu
2024-05-27 20:39 ` Stefan Hajnoczi
2024-05-28 8:37 ` Zhao Liu
2024-05-27 8:14 ` [RFC 4/6] scripts/simpletrace-rust: Parse and check trace recode file Zhao Liu
2024-05-27 20:44 ` Stefan Hajnoczi
2024-05-28 9:30 ` Zhao Liu
2024-05-27 8:14 ` Zhao Liu [this message]
2024-05-27 8:14 ` [RFC 6/6] docs/tracing: Add simpletrace-rust section Zhao Liu
2024-05-27 10:29 ` [RFC 0/6] scripts: Rewrite simpletrace printer in Rust Philippe Mathieu-Daudé
2024-05-27 10:49 ` Mads Ynddal
2024-05-28 6:15 ` Zhao Liu
2024-05-27 19:59 ` Stefan Hajnoczi
2024-05-28 6:48 ` Zhao Liu
2024-05-28 13:05 ` Stefan Hajnoczi
2024-05-29 9:33 ` Mads Ynddal
2024-05-29 14:10 ` Zhao Liu
2024-05-29 18:44 ` Stefan Hajnoczi
2024-05-31 12:27 ` Daniel P. Berrangé
2024-05-31 14:55 ` Alex Bennée
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240527081421.2258624-6-zhao1.liu@intel.com \
--to=zhao1.liu@intel.com \
--cc=alex.bennee@linaro.org \
--cc=armbru@redhat.com \
--cc=berrange@redhat.com \
--cc=mads@ynddal.dk \
--cc=pbonzini@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=philmd@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@redhat.com \
--cc=thuth@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).