Skip to content

Commit

Permalink
fix off-by-one bug while reading heaptrack files (vercel#8101)
Browse files Browse the repository at this point in the history
### Description

allocations are zero indexed...

### Testing Instructions

<!--
  Give a quick description of steps to test your changes.
-->


Closes PACK-3043
  • Loading branch information
sokra authored and Neosoulink committed Jun 14, 2024
1 parent 77ee752 commit 84b40db
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
5 changes: 1 addition & 4 deletions crates/turbopack-trace-server/src/reader/heaptrack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,7 @@ impl HeaptrackFormat {
}
},
trace_instruction_pointers: vec![0],
allocations: vec![AllocationInfo {
size: 0,
trace_index: 0,
}],
allocations: vec![],
spans: 0,
collapse_crates: env::var("COLLAPSE_CRATES")
.unwrap_or_default()
Expand Down
31 changes: 17 additions & 14 deletions crates/turbopack-trace-server/src/reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ use crate::{
store_container::StoreContainer,
};

const MIN_INITIAL_REPORT_SIZE: u64 = 100 * 1024 * 1024;

trait TraceFormat {
fn read(&mut self, buffer: &[u8]) -> Result<usize>;
fn stats(&self) -> String {
Expand Down Expand Up @@ -115,17 +117,7 @@ impl TraceReader {
let mut format: Option<Box<dyn TraceFormat>> = None;

let mut current_read = 0;
let mut initial_read = {
if let Ok(pos) = file.seek(SeekFrom::End(0)) {
if pos > 100 * 1024 * 1024 {
Some(pos)
} else {
None
}
} else {
None
}
};
let mut initial_read = { file.seek(SeekFrom::End(0)).ok() };
if file.seek(SeekFrom::Start(0)).is_err() {
return false;
}
Expand All @@ -145,7 +137,9 @@ impl TraceReader {
match file.read(&mut chunk) {
Ok(bytes_read) => {
if bytes_read == 0 {
if let Some(value) = self.wait_for_more_data(&mut file, &mut initial_read) {
if let Some(value) =
self.wait_for_more_data(&mut file, &mut initial_read, format.as_deref())
{
return value;
}
} else {
Expand Down Expand Up @@ -220,7 +214,9 @@ impl TraceReader {
}
Err(err) => {
if err.kind() == io::ErrorKind::UnexpectedEof {
if let Some(value) = self.wait_for_more_data(&mut file, &mut initial_read) {
if let Some(value) =
self.wait_for_more_data(&mut file, &mut initial_read, format.as_deref())
{
return value;
}
} else {
Expand All @@ -237,12 +233,19 @@ impl TraceReader {
&mut self,
file: &mut TraceFile,
initial_read: &mut Option<u64>,
format: Option<&dyn TraceFormat>,
) -> Option<bool> {
let Ok(pos) = file.stream_position() else {
return Some(true);
};
if let Some(total) = initial_read.take() {
println!("Initial read completed ({} MB)", total / (1024 * 1024),);
if let Some(format) = format {
let stats = format.stats();
println!("{}", stats);
}
if total > MIN_INITIAL_REPORT_SIZE {
println!("Initial read completed ({} MB)", total / (1024 * 1024));
}
}
thread::sleep(Duration::from_millis(100));
let Ok(end) = file.seek(SeekFrom::End(0)) else {
Expand Down
2 changes: 1 addition & 1 deletion crates/turbopack-trace-server/src/viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ impl Viewer {
.collect();
Update {
lines,
max: current,
max: max(1, current),
}
}
}
Expand Down

0 comments on commit 84b40db

Please sign in to comment.