Skip to content

Commit d5a3e03

Browse files
committed
print a markdown table for the task cache stats
1 parent ea14605 commit d5a3e03

File tree

3 files changed

+129
-36
lines changed

3 files changed

+129
-36
lines changed

turbopack/crates/turbo-tasks-backend/src/backend/mod.rs

Lines changed: 76 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,9 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
12121212
.collect::<Vec<_>>();
12131213
if !task_cache_stats.is_empty() {
12141214
use turbo_tasks::util::FormatBytes;
1215+
1216+
use crate::utils::markdown_table::print_markdown_table;
1217+
12151218
task_cache_stats.sort_unstable_by(|(key_a, stats_a), (key_b, stats_b)| {
12161219
(stats_b.data_compressed + stats_b.meta_compressed, key_b)
12171220
.cmp(&(stats_a.data_compressed + stats_a.meta_compressed, key_a))
@@ -1232,42 +1235,79 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
12321235
)
12331236
);
12341237

1235-
for (task_desc, stats) in task_cache_stats {
1236-
println!(
1237-
" {} ({}) {task_desc} = {} ({}) meta {} x {} ({}), {} ({}) data {} x \
1238-
{} ({}), upper: {}, coll: {}, agg_coll: {}, children: {}, followers: \
1239-
{}, coll_deps: {}, agg_dirty: {}",
1240-
FormatBytes(stats.data_compressed + stats.meta_compressed),
1241-
FormatBytes(stats.data + stats.meta),
1242-
FormatBytes(stats.meta_compressed),
1243-
FormatBytes(stats.meta),
1244-
stats.meta_count,
1245-
FormatBytes(
1246-
stats
1247-
.meta_compressed
1248-
.checked_div(stats.meta_count)
1249-
.unwrap_or(0)
1250-
),
1251-
FormatBytes(stats.meta.checked_div(stats.meta_count).unwrap_or(0)),
1252-
FormatBytes(stats.data_compressed),
1253-
FormatBytes(stats.data),
1254-
stats.data_count,
1255-
FormatBytes(
1256-
stats
1257-
.data_compressed
1258-
.checked_div(stats.data_count)
1259-
.unwrap_or(0)
1260-
),
1261-
FormatBytes(stats.data.checked_div(stats.data_count).unwrap_or(0)),
1262-
stats.upper_count,
1263-
stats.collectibles_count,
1264-
stats.aggregated_collectibles_count,
1265-
stats.children_count,
1266-
stats.followers_count,
1267-
stats.collectibles_dependents_count,
1268-
stats.aggregated_dirty_containers_count,
1269-
);
1270-
}
1238+
print_markdown_table(
1239+
[
1240+
"Task",
1241+
"Total Size",
1242+
"Data Size",
1243+
"Data Count x Avg",
1244+
"Meta Size",
1245+
"Meta Count x Avg",
1246+
"Uppers",
1247+
"Coll",
1248+
"Agg Coll",
1249+
"Children",
1250+
"Followers",
1251+
"Coll Deps",
1252+
"Agg Dirty",
1253+
"Output Size",
1254+
],
1255+
task_cache_stats.iter(),
1256+
|(task_desc, stats)| {
1257+
[
1258+
task_desc.to_string(),
1259+
format!(
1260+
"{} ({})",
1261+
FormatBytes(stats.data_compressed + stats.meta_compressed),
1262+
FormatBytes(stats.data + stats.meta)
1263+
),
1264+
format!(
1265+
"{} ({})",
1266+
FormatBytes(stats.data_compressed),
1267+
FormatBytes(stats.data)
1268+
),
1269+
format!(
1270+
"{} x {} ({})",
1271+
stats.data_count,
1272+
FormatBytes(
1273+
stats
1274+
.data_compressed
1275+
.checked_div(stats.data_count)
1276+
.unwrap_or(0)
1277+
),
1278+
FormatBytes(
1279+
stats.data.checked_div(stats.data_count).unwrap_or(0)
1280+
),
1281+
),
1282+
format!(
1283+
"{} ({})",
1284+
FormatBytes(stats.meta_compressed),
1285+
FormatBytes(stats.meta)
1286+
),
1287+
format!(
1288+
"{} x {} ({})",
1289+
stats.meta_count,
1290+
FormatBytes(
1291+
stats
1292+
.meta_compressed
1293+
.checked_div(stats.meta_count)
1294+
.unwrap_or(0)
1295+
),
1296+
FormatBytes(
1297+
stats.meta.checked_div(stats.meta_count).unwrap_or(0)
1298+
),
1299+
),
1300+
stats.upper_count.to_string(),
1301+
stats.collectibles_count.to_string(),
1302+
stats.aggregated_collectibles_count.to_string(),
1303+
stats.children_count.to_string(),
1304+
stats.followers_count.to_string(),
1305+
stats.collectibles_dependents_count.to_string(),
1306+
stats.aggregated_dirty_containers_count.to_string(),
1307+
FormatBytes(stats.output_size).to_string(),
1308+
]
1309+
},
1310+
);
12711311
}
12721312
}
12731313
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use std::fmt::Write;
2+
3+
pub fn print_markdown_table<T, const N: usize>(
4+
headers: [&str; N],
5+
data: impl IntoIterator<Item = T> + Clone,
6+
get_fields: impl Fn(&T) -> [String; N],
7+
) {
8+
let mut sizes = headers.map(|h| h.len());
9+
// Measure max field size
10+
for item in data.clone() {
11+
let fields = get_fields(&item);
12+
for (i, field) in fields.iter().enumerate() {
13+
let field_size = field.len();
14+
if field_size > sizes[i] {
15+
sizes[i] = field_size;
16+
}
17+
}
18+
}
19+
// Print headers
20+
{
21+
let mut line = String::new();
22+
for (i, header) in headers.iter().enumerate() {
23+
let size = sizes[i];
24+
let escaped_header = escape_markdown_cell(header);
25+
write!(line, "| {:<width$} ", escaped_header, width = size).unwrap();
26+
}
27+
println!("{} |", line);
28+
}
29+
// Print separator
30+
{
31+
let mut line = String::new();
32+
for size in sizes.iter() {
33+
write!(line, "| {:-<width$} ", "", width = *size + 2).unwrap();
34+
}
35+
println!("{} |", line);
36+
}
37+
// Print rows
38+
for item in data {
39+
let row = get_fields(&item);
40+
let mut line = String::new();
41+
for (i, field) in row.iter().enumerate() {
42+
let size = sizes[i];
43+
let escaped_field = escape_markdown_cell(field);
44+
write!(line, "| {:<width$} ", escaped_field, width = size).unwrap();
45+
}
46+
println!("{} |", line);
47+
}
48+
}
49+
50+
fn escape_markdown_cell(content: &str) -> String {
51+
content.replace('|', "\\|").replace('\n', " ")
52+
}

turbopack/crates/turbo-tasks-backend/src/utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub mod chunked_vec;
33
pub mod dash_map_drop_contents;
44
pub mod dash_map_multi;
55
pub mod dash_map_raw_entry;
6+
pub mod markdown_table;
67
pub mod ptr_eq_arc;
78
pub mod shard_amount;
89
pub mod sharded;

0 commit comments

Comments
 (0)