Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use BufWriter when STDOUT is not a TTY #206

Merged
merged 4 commits into from
Jan 8, 2021
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed
- Decreased default minimum width from 0.1% to 0.01%. [#204](https://github.com/jonhoo/inferno/pull/204)
- Detect if STDOUT is a TTY and if it's not, use a `BufWriter` to avoid line buffering. [#206](https://github.com/jonhoo/inferno/pull/206)

### Removed

Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ nameattr = ["indexmap"]

[dependencies]
ahash = "0.6"
atty = "0.2"
crossbeam-utils = { version = "0.8", optional = true }
crossbeam-channel = { version = "0.5", optional = true }
dashmap = { version = "3", optional = true }
Expand Down
2 changes: 1 addition & 1 deletion src/bin/collapse-dtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,5 @@ fn main() -> io::Result<()> {
}

let (infile, options) = opt.into_parts();
Folder::from(options).collapse_file(infile.as_ref(), io::stdout().lock())
Folder::from(options).collapse_file_to_stdout(infile.as_ref())
}
2 changes: 1 addition & 1 deletion src/bin/collapse-guess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,5 @@ fn main() -> io::Result<()> {
}

let (infile, options) = opt.into_parts();
Folder::from(options).collapse_file(infile.as_ref(), io::stdout().lock())
Folder::from(options).collapse_file_to_stdout(infile.as_ref())
}
2 changes: 1 addition & 1 deletion src/bin/collapse-perf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,5 @@ fn main() -> io::Result<()> {
}

let (infile, options) = opt.into_parts();
Folder::from(options).collapse_file(infile.as_ref(), io::stdout().lock())
Folder::from(options).collapse_file_to_stdout(infile.as_ref())
}
2 changes: 1 addition & 1 deletion src/bin/collapse-sample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,5 @@ fn main() -> io::Result<()> {
}

let (infile, options) = opt.into_parts();
Folder::from(options).collapse_file(infile.as_ref(), io::stdout().lock())
Folder::from(options).collapse_file_to_stdout(infile.as_ref())
}
2 changes: 1 addition & 1 deletion src/bin/collapse-vtune.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,5 @@ fn main() -> io::Result<()> {
}

let (infile, options) = opt.into_parts();
Folder::from(options).collapse_file(infile.as_ref(), io::stdout().lock())
Folder::from(options).collapse_file_to_stdout(infile.as_ref())
}
12 changes: 11 additions & 1 deletion src/bin/diff-folded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,15 @@ fn main() -> io::Result<()> {
}

let (folded1, folded2, options) = opt.into_parts();
differential::from_files(options, folded1, folded2, io::stdout().lock())

if atty::is(atty::Stream::Stdout) {
differential::from_files(options, folded1, folded2, io::stdout().lock())
} else {
differential::from_files(
options,
folded1,
folded2,
io::BufWriter::new(io::stdout().lock()),
)
}
}
12 changes: 11 additions & 1 deletion src/bin/flamegraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,19 @@ fn main() -> quick_xml::Result<()> {
};

let (infiles, mut options) = opt.into_parts();

options.palette_map = palette_map.as_mut();

flamegraph::from_files(&mut options, &infiles, io::stdout().lock())?;
if atty::is(atty::Stream::Stdout) {
flamegraph::from_files(&mut options, &infiles, io::stdout().lock())?;
} else {
flamegraph::from_files(
&mut options,
&infiles,
io::BufWriter::new(io::stdout().lock()),
)?;
}

save_consistent_palette_if_needed(&palette_map, PALETTE_MAP_FILE).map_err(quick_xml::Error::Io)
}

Expand Down
3 changes: 2 additions & 1 deletion src/collapse/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ impl Occurrences {
}
}
}
writer.flush()?;
Ok(())
}
}
Expand Down Expand Up @@ -510,7 +511,7 @@ pub(crate) fn fix_partially_demangled_rust_symbol(symbol: &str) -> Cow<str> {
demangled.push_str("::");
rest = &rest[2..];
} else {
demangled.push_str(".");
demangled.push('.');
rest = &rest[1..];
}
} else if rest.starts_with('$') {
Expand Down
12 changes: 8 additions & 4 deletions src/collapse/guess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,17 @@ impl Collapse for Folder {
W: io::Write,
{
let mut dtrace = {
let mut options = dtrace::Options::default();
options.nthreads = self.opt.nthreads;
let options = dtrace::Options {
nthreads: self.opt.nthreads,
..Default::default()
};
dtrace::Folder::from(options)
};
let mut perf = {
let mut options = perf::Options::default();
options.nthreads = self.opt.nthreads;
let options = perf::Options {
nthreads: self.opt.nthreads,
..Default::default()
};
perf::Folder::from(options)
};
let mut sample = sample::Folder::default();
Expand Down
10 changes: 3 additions & 7 deletions src/collapse/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@
#[inline]
pub fn is_vmlinux(s: &str) -> bool {
if let Some(vm) = s.rfind("vmlinux") {
s[vm..].chars().all(|c| {
c.is_ascii_alphanumeric()
|| match c {
'-' | '.' | '_' => true,
_ => false,
}
})
s[vm..]
.chars()
.all(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '.' | '_'))
} else {
false
}
Expand Down
19 changes: 16 additions & 3 deletions src/collapse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,27 @@ pub trait Collapse {
self.collapse(reader, writer)
}
None => {
let stdio = io::stdin();
let stdio_guard = stdio.lock();
let reader = io::BufReader::with_capacity(CAPACITY_READER, stdio_guard);
let stdin = io::stdin();
let stdin_guard = stdin.lock();
let reader = io::BufReader::with_capacity(CAPACITY_READER, stdin_guard);
self.collapse(reader, writer)
}
}
}

/// Collapses the contents of the provided file (or of STDIN if `infile` is `None`) and
/// writes folded stack lines to STDOUT.
fn collapse_file_to_stdout<P>(&mut self, infile: Option<P>) -> io::Result<()>
where
P: AsRef<Path>,
{
if atty::is(atty::Stream::Stdout) {
self.collapse_file(infile, io::stdout().lock())
} else {
self.collapse_file(infile, io::BufWriter::new(io::stdout().lock()))
}
}

/// Returns whether this implementation is appropriate for the given input.
///
/// - `None` means "not sure -- need more input"
Expand Down
14 changes: 7 additions & 7 deletions src/collapse/perf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,12 +409,12 @@ impl Folder {
// XXX: re-use existing memory in pname if possible
self.pname = comm.replace(' ', "_");
if self.opt.include_tid {
self.pname.push_str("-");
self.pname.push('-');
self.pname.push_str(pid);
self.pname.push_str("/");
self.pname.push('/');
self.pname.push_str(tid);
} else if self.opt.include_pid {
self.pname.push_str("-");
self.pname.push('-');
self.pname.push_str(pid);
}

Expand Down Expand Up @@ -548,7 +548,7 @@ impl Folder {
stack_str.push_str(&self.pname);
// add the other stack entries (if any)
for e in self.stack.drain(..) {
stack_str.push_str(";");
stack_str.push(';');
stack_str.push_str(&e);
}

Expand Down Expand Up @@ -587,15 +587,15 @@ fn with_module_fallback(module: &str, func: &str, pc: &str, include_addrs: bool)
let mut res = String::with_capacity(func.len() + 12);

if include_addrs {
res.push_str("[");
res.push('[');
res.push_str(func);
res.push_str(" <");
res.push_str(pc);
res.push_str(">]");
} else {
res.push_str("[");
res.push('[');
res.push_str(func);
res.push_str("]");
res.push(']');
}

res
Expand Down
6 changes: 3 additions & 3 deletions src/flamegraph/color/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,16 +347,16 @@ pub(super) fn color(
let mut hash: u64 = 0xcbf29ce484222325;
// https://github.com/servo/rust-fnv/blob/4b4784ebfd3332dc61f0640764d6f1140e03a9ab/lib.rs#L118-L121
for byte in name.as_bytes() {
hash = hash ^ (*byte as u64);
hash ^= *byte as u64;
hash = hash.wrapping_mul(0x100000001b3);
}
let hash1 = (hash as f64 / std::u64::MAX as f64) as f32;

// Rotate hash so we get two more distinct numbers
hash = hash ^ 0;
hash ^= 0;
hash = hash.wrapping_mul(0x100000001b3);
let hash2 = (hash as f64 / std::u64::MAX as f64) as f32;
hash = hash ^ 0;
hash ^= 0;
hash = hash.wrapping_mul(0x100000001b3);
let hash3 = (hash as f64 / std::u64::MAX as f64) as f32;

Expand Down
4 changes: 2 additions & 2 deletions src/flamegraph/color/palettes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ pub(super) mod java {
if name.contains("::") || name.starts_with("-[") || name.starts_with("+[") {
// C++ or Objective C
BasicPalette::Yellow
} else if java_prefix.contains("/")
|| (java_prefix.contains(".") && !java_prefix.starts_with("["))
} else if java_prefix.contains('/')
|| (java_prefix.contains('.') && !java_prefix.starts_with('['))
|| match java_prefix.chars().next() {
Some(c) => c.is_ascii_uppercase(),
_ => false,
Expand Down
1 change: 1 addition & 0 deletions src/flamegraph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,7 @@ where
svg.write_event(Event::End(BytesEnd::borrowed(b"svg")))?;
svg.write_event(Event::Eof)?;

svg.into_inner().flush()?;
Ok(())
}

Expand Down