Skip to content

Commit 96bd455

Browse files
committed
-Ztimings: Fix more scale problems.
1 parent 73d8d2d commit 96bd455

File tree

2 files changed

+32
-35
lines changed

2 files changed

+32
-35
lines changed

src/cargo/core/compiler/timings.js

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,8 @@ function render_timing_graph() {
163163
for (c of CONCURRENCY_DATA) {
164164
max_v = Math.max(max_v, c.active, c.waiting, c.inactive);
165165
}
166-
let [step, top] = split_ticks(max_v, GRAPH_HEIGHT / MIN_TICK_DIST);
167-
let num_ticks = top / step;
168-
let tick_dist = GRAPH_HEIGHT / num_ticks;
166+
const px_per_v = GRAPH_HEIGHT / max_v;
167+
const {step, tick_dist, num_ticks} = split_ticks(max_v, px_per_v, GRAPH_HEIGHT);
169168
ctx.textAlign = 'end';
170169
for (n=0; n<num_ticks; n++) {
171170
let y = HEIGHT - Y_LINE - ((n + 1) * tick_dist);
@@ -299,7 +298,7 @@ function draw_graph_axes(id, graph_height) {
299298
// 4096 is still ridiculously large, and probably won't render on mobile
300299
// browsers, but should be ok for many desktop environments.
301300
const graph_width = Math.min(scale * DURATION, 4096);
302-
const px_per_sec = Math.floor(graph_width / DURATION);
301+
const px_per_sec = graph_width / DURATION;
303302
const canvas_width = Math.max(graph_width + X_LINE + 30, X_LINE + 250);
304303
const canvas_height = graph_height + MARGIN + Y_LINE;
305304
let ctx = setup_canvas(id, canvas_width, canvas_height);
@@ -318,9 +317,7 @@ function draw_graph_axes(id, graph_height) {
318317
ctx.stroke();
319318

320319
// Draw X tick marks.
321-
const [step, top] = split_ticks(DURATION, graph_width / MIN_TICK_DIST);
322-
const num_ticks = top / step;
323-
const tick_dist = graph_width / num_ticks;
320+
const {step, tick_dist, num_ticks} = split_ticks(DURATION, px_per_sec, graph_width);
324321
ctx.fillStyle = '#303030';
325322
for (let n=0; n<num_ticks; n++) {
326323
const x = X_LINE + ((n + 1) * tick_dist);
@@ -347,40 +344,39 @@ function draw_graph_axes(id, graph_height) {
347344
return {canvas_width, canvas_height, graph_width, graph_height, ctx, px_per_sec};
348345
}
349346

350-
function round_up(n, step) {
351-
if (n % step == 0) {
352-
return n;
353-
} else {
354-
return (step - n % step) + n;
347+
// Determine the spacing and number of ticks along an axis.
348+
function split_ticks(max_value, px_per_v, max_px) {
349+
const max_ticks = Math.floor(max_px / MIN_TICK_DIST);
350+
if (max_ticks <= 1) {
351+
// Graph is too small for even 1 tick.
352+
return {step: max_value, tick_dist: max_px, num_ticks: 1};
355353
}
356-
}
357-
358-
// Determine the `(step, max_value)` of the number of ticks along an axis.
359-
function split_ticks(n, max_ticks) {
360-
max_ticks = Math.ceil(max_ticks);
361-
if (n <= max_ticks) {
362-
return [1, n];
363-
} else if (n <= max_ticks * 2) {
364-
return [2, round_up(n, 2)];
365-
} else if (n <= max_ticks * 4) {
366-
return [4, round_up(n, 4)];
367-
} else if (n <= max_ticks * 5) {
368-
return [5, round_up(n, 5)];
354+
let step;
355+
if (max_value <= max_ticks) {
356+
step = 1;
357+
} else if (max_value <= max_ticks * 2) {
358+
step = 2;
359+
} else if (max_value <= max_ticks * 4) {
360+
step = 4;
361+
} else if (max_value <= max_ticks * 5) {
362+
step = 5;
369363
} else {
370-
let step = 10;
364+
step = 10;
371365
let count = 0;
372366
while (true) {
373367
if (count > 100) {
374368
throw Error("tick loop too long");
375369
}
376370
count += 1;
377-
let top = round_up(n, step);
378-
if (top <= max_ticks * step) {
379-
return [step, top];
371+
if (max_value <= max_ticks * step) {
372+
break;
380373
}
381374
step += 10;
382375
}
383376
}
377+
const tick_dist = px_per_v * step;
378+
const num_ticks = Math.floor(max_value / step);
379+
return {step, tick_dist, num_ticks};
384380
}
385381

386382
function codegen_time(unit) {

src/cargo/core/compiler/timings.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ impl<'a, 'cfg> Timings<'a, 'cfg> {
301301

302302
/// Save HTML report to disk.
303303
fn report_html(&self, bcx: &BuildContext<'_, '_>) -> CargoResult<()> {
304-
let duration = self.start.elapsed().as_secs() as u32 + 1;
304+
let duration = d_as_f64(self.start.elapsed());
305305
let timestamp = self.start_str.replace(&['-', ':'][..], "");
306306
let filename = format!("cargo-timing-{}.html", timestamp);
307307
let mut f = BufWriter::new(File::create(&filename)?);
@@ -314,11 +314,12 @@ impl<'a, 'cfg> Timings<'a, 'cfg> {
314314
self.write_summary_table(&mut f, duration, bcx)?;
315315
f.write_all(HTML_CANVAS.as_bytes())?;
316316
self.write_unit_table(&mut f)?;
317+
// It helps with pixel alignment to use whole numbers.
317318
writeln!(
318319
f,
319320
"<script>\n\
320321
DURATION = {};",
321-
duration
322+
f64::ceil(duration) as u32
322323
)?;
323324
self.write_js_data(&mut f)?;
324325
write!(
@@ -349,7 +350,7 @@ impl<'a, 'cfg> Timings<'a, 'cfg> {
349350
fn write_summary_table(
350351
&self,
351352
f: &mut impl Write,
352-
duration: u32,
353+
duration: f64,
353354
bcx: &BuildContext<'_, '_>,
354355
) -> CargoResult<()> {
355356
let targets: Vec<String> = self
@@ -358,12 +359,12 @@ impl<'a, 'cfg> Timings<'a, 'cfg> {
358359
.map(|(name, targets)| format!("{} ({})", name, targets.join(", ")))
359360
.collect();
360361
let targets = targets.join("<br>");
361-
let time_human = if duration > 60 {
362-
format!(" ({}m {:02}s)", duration / 60, duration % 60)
362+
let time_human = if duration > 60.0 {
363+
format!(" ({}m {:.1}s)", duration as u32 / 60, duration % 60.0)
363364
} else {
364365
"".to_string()
365366
};
366-
let total_time = format!("{}s{}", duration, time_human);
367+
let total_time = format!("{:.1}s{}", duration, time_human);
367368
let max_concurrency = self.concurrency.iter().map(|c| c.active).max().unwrap();
368369
let rustc_info = render_rustc_info(bcx);
369370
write!(

0 commit comments

Comments
 (0)