Skip to content
This repository was archived by the owner on Dec 15, 2021. It is now read-only.

Commit bd3058e

Browse files
committed
Merge #16
16: reduce retry time r=japaric a=japaric
2 parents 86e4c77 + c576765 commit bd3058e

File tree

4 files changed

+98
-90
lines changed

4 files changed

+98
-90
lines changed

build.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use std::process::Command;
88
struct IgnoredError {}
99

1010
impl<E> From<E> for IgnoredError
11-
where E: Error
11+
where
12+
E: Error,
1213
{
1314
fn from(_: E) -> IgnoredError {
1415
IgnoredError {}
@@ -32,15 +33,21 @@ fn commit_info() -> String {
3233
}
3334

3435
fn commit_hash() -> Result<String, IgnoredError> {
35-
Ok(try!(String::from_utf8(try!(Command::new("git")
36-
.args(&["rev-parse", "--short", "HEAD"])
37-
.output())
38-
.stdout)))
36+
Ok(try!(String::from_utf8(
37+
try!(
38+
Command::new("git")
39+
.args(&["rev-parse", "--short", "HEAD"])
40+
.output()
41+
).stdout
42+
)))
3943
}
4044

4145
fn commit_date() -> Result<String, IgnoredError> {
42-
Ok(try!(String::from_utf8(try!(Command::new("git")
43-
.args(&["log", "-1", "--date=short", "--pretty=format:%cd"])
44-
.output())
45-
.stdout)))
46+
Ok(try!(String::from_utf8(
47+
try!(
48+
Command::new("git")
49+
.args(&["log", "-1", "--date=short", "--pretty=format:%cd"])
50+
.output()
51+
).stdout
52+
)))
4653
}

src/bin/itmdump.rs

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ extern crate itm;
77
#[macro_use]
88
extern crate log;
99

10-
use clap::{Arg, App, ArgMatches};
10+
use clap::{App, Arg, ArgMatches};
1111
use itm::{packet, Decoder};
1212
use itm::error::{Error, ErrorKind, Result, ResultExt};
13-
use log::{LogRecord, LogLevelFilter};
13+
use log::{LogLevelFilter, LogRecord};
1414
use std::fs::File;
1515
use std::io::Write;
1616
use std::time::Duration;
@@ -19,18 +19,19 @@ use std::{env, io, process, thread};
1919
fn main() {
2020
// Initialise logging.
2121
env_logger::LogBuilder::new()
22-
.format(|r: &LogRecord|
22+
.format(|r: &LogRecord| {
2323
format!("\n{time} {lvl} {mod} @{file}:{line}\n {args}",
2424
time = chrono::Utc::now().format("%Y-%m-%d %H:%M:%S%.3f_UTC"),
2525
lvl = r.level(),
2626
mod = r.location().module_path(),
2727
file = r.location().file(),
2828
line = r.location().line(),
2929
args = r.args())
30-
)
30+
})
3131
.filter(None, LogLevelFilter::Info)
3232
.parse(&env::var("RUST_LOG").unwrap_or(String::from("")))
33-
.init().unwrap();
33+
.init()
34+
.unwrap();
3435

3536
fn show_backtrace() -> bool {
3637
env::var("RUST_BACKTRACE").as_ref().map(|s| &s[..]) == Ok("1")
@@ -59,31 +60,36 @@ fn main() {
5960
fn run() -> Result<()> {
6061
let matches = App::new("itmdump")
6162
.version(include_str!(concat!(env!("OUT_DIR"), "/commit-info.txt")))
62-
.about("\n\
63-
Reads data from an ARM CPU ITM and decodes it. \n\
64-
\n\
65-
Input is from an existing file (or named pipe) at a \
66-
supplied path, or else from standard input.")
67-
.arg(Arg::with_name("file")
68-
.long("file")
69-
.short("f")
70-
.help("Path to file (or named pipe) to read from")
71-
.takes_value(true))
72-
.arg(Arg::with_name("follow")
73-
.long("follow")
74-
.short("F")
75-
.help("Keep the file open after reading through it and \
76-
append new output as it is written. Like `tail -f'."))
77-
.arg(Arg::with_name("port")
78-
.long("stimulus")
79-
.short("s")
80-
.help("Stimulus port to extract ITM data for.")
81-
.takes_value(true)
82-
.default_value("0")
83-
.validator(|s| match s.parse::<u8>() {
84-
Ok(_) => Ok(()),
85-
Err(e) => Err(e.to_string())
86-
}))
63+
.about(
64+
"\n\
65+
Reads data from an ARM CPU ITM and decodes it. \n\
66+
\n\
67+
Input is from an existing file (or named pipe) at a \
68+
supplied path, or else from standard input.",
69+
)
70+
.arg(
71+
Arg::with_name("file")
72+
.long("file")
73+
.short("f")
74+
.help("Path to file (or named pipe) to read from")
75+
.takes_value(true),
76+
)
77+
.arg(Arg::with_name("follow").long("follow").short("F").help(
78+
"Keep the file open after reading through it and \
79+
append new output as it is written. Like `tail -f'.",
80+
))
81+
.arg(
82+
Arg::with_name("port")
83+
.long("stimulus")
84+
.short("s")
85+
.help("Stimulus port to extract ITM data for.")
86+
.takes_value(true)
87+
.default_value("0")
88+
.validator(|s| match s.parse::<u8>() {
89+
Ok(_) => Ok(()),
90+
Err(e) => Err(e.to_string()),
91+
}),
92+
)
8793
.get_matches();
8894

8995
let port = matches.value_of("port")
@@ -101,27 +107,26 @@ fn run() -> Result<()> {
101107
loop {
102108
let p = decoder.read_packet();
103109
match p {
104-
Ok(p) => {
105-
match p.kind() {
106-
&packet::Kind::Instrumentation(ref i) if i.port() == port => {
107-
stdout.write_all(&i.payload())?;
108-
stdout.flush()?;
109-
}
110-
_ => (),
110+
Ok(p) => match p.kind() {
111+
&packet::Kind::Instrumentation(ref i) if i.port() == port => {
112+
stdout.write_all(&i.payload())?;
113+
stdout.flush()?;
111114
}
112-
}
115+
_ => (),
116+
},
113117
Err(e @ Error(ErrorKind::UnknownHeader(_), _)) => {
114118
// We don't know this header type; warn and continue.
115119
debug!("{}", e);
116-
},
120+
}
117121
Err(Error(ErrorKind::EofBeforePacket, _)) => {
118122
if follow {
119-
thread::sleep(Duration::from_millis(100));
123+
// NOTE 10 ms let us achieve 60 FPS in the worst case scenario
124+
thread::sleep(Duration::from_millis(10));
120125
} else {
121126
// !follow and EOF. Exit.
122-
return Ok(())
127+
return Ok(());
123128
}
124-
},
129+
}
125130

126131
// FIXME: If in follow mode, we may try to read a packet
127132
// but reach the end of the file in the middle. Currently
@@ -143,12 +148,9 @@ fn open_read(matches: &ArgMatches) -> Result<Box<io::Read + 'static>> {
143148
let path = matches.value_of("file");
144149
Ok(match path {
145150
Some(path) => {
146-
let f =
147-
File::open(path)
148-
.chain_err(|| format!("Couldn't open source file '{}'",
149-
path))?;
151+
let f = File::open(path).chain_err(|| format!("Couldn't open source file '{}'", path))?;
150152
Box::new(f) as Box<io::Read + 'static>
151-
},
153+
}
152154
None => Box::new(io::stdin()) as Box<io::Read + 'static>,
153155
})
154156
}

src/decoder.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Parse ITM packets from bytes and streams.
22
33
use error::{Error, ErrorKind, Result};
4-
use packet::{self, Packet, Instrumentation};
4+
use packet::{self, Instrumentation, Packet};
55
use std::io::{self, Cursor, Read};
66

77
/// Parses ITM packets.
@@ -13,9 +13,7 @@ impl<R: Read> Decoder<R> {
1313
/// Construct a new `Decoder` that reads encoded packets from
1414
/// `inner`.
1515
pub fn new(inner: R) -> Decoder<R> {
16-
Decoder::<R> {
17-
inner: inner,
18-
}
16+
Decoder::<R> { inner: inner }
1917
}
2018

2119
// TODO: If we need config for the Decoder, my plan is to:
@@ -29,34 +27,36 @@ impl<R: Read> Decoder<R> {
2927
pub fn read_packet(&mut self) -> Result<Packet> {
3028
let mut header = [0; 1];
3129
match self.inner.read_exact(&mut header) {
32-
Err(ref e) if e.kind() == io::ErrorKind::UnexpectedEof =>
33-
return Err(Error::from(ErrorKind::EofBeforePacket)),
30+
Err(ref e) if e.kind() == io::ErrorKind::UnexpectedEof => {
31+
return Err(Error::from(ErrorKind::EofBeforePacket))
32+
}
3433
Err(e) => return Err(Error::from(e)),
3534
Ok(_) => (),
3635
};
3736
let header = header[0];
3837
match header & 0b111 {
39-
0b001|0b010|0b011 => {
38+
0b001 | 0b010 | 0b011 => {
4039
// Instrumentation packet.
41-
let payload_len =
42-
match header & 0b11 {
43-
0b01 => 1,
44-
0b10 => 2,
45-
0b11 => 4,
46-
_ => unreachable!(), // Contradicts match on last 3 bits.
47-
};
40+
let payload_len = match header & 0b11 {
41+
0b01 => 1,
42+
0b10 => 2,
43+
0b11 => 4,
44+
_ => unreachable!(), // Contradicts match on last 3 bits.
45+
};
4846

4947
let mut ud = Instrumentation {
5048
payload: [0; packet::MAX_PAYLOAD_SIZE],
5149
payload_len: payload_len,
5250
port: header >> 3,
5351
};
5452

55-
{ // Scope the mutable borrow on buf to satisfy borrow checker.
53+
{
54+
// Scope the mutable borrow on buf to satisfy borrow checker.
5655
let buf = &mut ud.payload[0..payload_len];
5756
match self.inner.read_exact(buf) {
58-
Err(ref e) if e.kind() == io::ErrorKind::UnexpectedEof =>
59-
return Err(Error::from(ErrorKind::EofDuringPacket)),
57+
Err(ref e) if e.kind() == io::ErrorKind::UnexpectedEof => {
58+
return Err(Error::from(ErrorKind::EofDuringPacket))
59+
}
6060
Err(e) => return Err(Error::from(e)),
6161
Ok(_) => (),
6262
};
@@ -66,7 +66,7 @@ impl<R: Read> Decoder<R> {
6666
header: header,
6767
kind: packet::Kind::Instrumentation(ud),
6868
})
69-
},
69+
}
7070
_ => {
7171
return Err(Error::from(ErrorKind::UnknownHeader(header)));
7272
}
@@ -98,8 +98,8 @@ mod tests {
9898
match p.kind {
9999
Kind::Instrumentation(ref i) => {
100100
assert_eq!(i.payload(), [0x11]);
101-
},
102-
_ => panic!()
101+
}
102+
_ => panic!(),
103103
}
104104
}
105105

@@ -109,8 +109,8 @@ mod tests {
109109
match p.kind {
110110
Kind::Instrumentation(ref i) => {
111111
assert_eq!(i.payload(), [0x11, 0x12]);
112-
},
113-
_ => panic!()
112+
}
113+
_ => panic!(),
114114
}
115115
}
116116

@@ -120,8 +120,8 @@ mod tests {
120120
match p.kind {
121121
Kind::Instrumentation(ref i) => {
122122
assert_eq!(i.payload(), [0x11, 0x12, 0x13, 0x14]);
123-
},
124-
_ => panic!()
123+
}
124+
_ => panic!(),
125125
}
126126
}
127127

@@ -131,16 +131,16 @@ mod tests {
131131
match p.kind {
132132
Kind::Instrumentation(ref i) => {
133133
assert_eq!(i.port(), 0);
134-
},
135-
_ => panic!()
134+
}
135+
_ => panic!(),
136136
}
137137

138138
let p = decode_one(&[0b11111_001, 0x11]);
139139
match p.kind {
140140
Kind::Instrumentation(ref i) => {
141141
assert_eq!(i.port(), 31);
142-
},
143-
_ => panic!()
142+
}
143+
_ => panic!(),
144144
}
145145
}
146146

@@ -155,7 +155,7 @@ mod tests {
155155

156156
#[test]
157157
fn eof_before_payload() {
158-
let p = try_decode_one(&[0x01 /* Missing payload bytes */ ]);
158+
let p = try_decode_one(&[0x01 /* Missing payload bytes */]);
159159
match p {
160160
Err(Error(ErrorKind::EofDuringPacket, _)) => (),
161161
_ => panic!(),

tests/smoke.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ impl ItmDump {
2727
me.pop();
2828
}
2929
let mut command = Command::new(me.join("itmdump"));
30-
command.arg("-f")
31-
.arg(&path)
32-
.stdout(Stdio::piped());
30+
command.arg("-f").arg(&path).stdout(Stdio::piped());
3331

3432
let file = OpenOptions::new()
3533
.write(true)
@@ -50,7 +48,9 @@ impl ItmDump {
5048
}
5149

5250
fn write_u8x2(&mut self, payload: [u8; 2]) {
53-
self.file.write_all(&[0b10, payload[0], payload[1]]).unwrap();
51+
self.file
52+
.write_all(&[0b10, payload[0], payload[1]])
53+
.unwrap();
5454
self.file.flush().unwrap();
5555
}
5656

@@ -78,7 +78,6 @@ fn chunks() {
7878
let out = itmdump.output();
7979

8080
assert_eq!(*b"Hello, World\n", *out.stdout);
81-
8281
}
8382

8483
#[test]

0 commit comments

Comments
 (0)