-
Notifications
You must be signed in to change notification settings - Fork 2.8k
refactor(timings): reuse timing metric collection logic between --timings and -Zbuild-analysis
#16497
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
base: master
Are you sure you want to change the base?
Conversation
…ings and -Zbuild-analysis
7530028 to
b2dc1e1
Compare
|
r? @weihanglo rustbot has assigned @weihanglo. Use |
The overall direction looks reasonable to me! |
|
Please see #16474 (comment), since they are already assigned to the parent issue, I have no objection to handing this PR over to @kurasaiteja (of course if they want). |
| if let Some(logger) = logger { | ||
| let root_unit_indexes: HashSet<_> = | ||
| root_units.iter().map(|unit| unit_to_index[&unit]).collect(); | ||
| let root_unit_indexes: HashSet<_> = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should move into the if block at line 537.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh right, thank you!
| fn prepare_context(log: &Path, run_id: &RunId) -> CargoResult<RenderContext<'static>> { | ||
| let reader = BufReader::new(File::open(&log)?); | ||
|
|
||
| // FIXME: simply the types here once `cargo build --timings` support is dropped |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The --timings flag will stay forever. I don't think we'll drop it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wasn't aware of that 👀
| let reader = BufReader::new(File::open(&log)?); | ||
|
|
||
| // FIXME: simply the types here once `cargo build --timings` support is dropped | ||
| pub(crate) fn prepare_context_from_iter<T: Iterator<Item = LogMessage>>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some nits
- Can just keep calling it
prepare_context. - I would probable prefer
where I: Iteratorif that geenric is a bit too long
|
|
||
| let (tx, rx) = mpsc::channel::<LogMessage>(); | ||
| /// Logger for `-Zbuild-analysis`. | ||
| // fixme: simplify this struct once `cargo build --timings` support dropped |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto. It will be supported like forever.
| // for legacy `cargo build --timings` flag | ||
| struct InMemoryLogger { | ||
| // using mutex to hide mutability | ||
| logs: Mutex<Vec<LogMessage>>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a Mutex here seems a little bit unfortunate. I think it is safe to use RefCell here to reduce the performance overhead, as timing appears to be on mainthread only.
| cause, | ||
| }); | ||
| } | ||
| let index = bcx.unit_to_index[unit]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't we check if bcx.logger.i_senabled() first then construct log message?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At this point, I think it is better to leave logger as Option so that pre-existing check codes doesn't need to be changed. Will fix in such way!
| /// When Cargo started. | ||
| start: Instant, | ||
| /// A rendered string of when compilation started. | ||
| start_str: String, | ||
| /// A summary of the root units. | ||
| /// | ||
| /// Tuples of `(package_description, target_descriptions)`. | ||
| root_targets: Vec<(String, Vec<String>)>, | ||
| /// The build profile. | ||
| profile: String, | ||
| /// Total number of fresh units. | ||
| total_fresh: u32, | ||
| /// Total number of dirty units. | ||
| total_dirty: u32, | ||
| /// A map from unit to index. | ||
| unit_to_index: HashMap<Unit, UnitIndex>, | ||
| /// Time tracking for each individual unit. | ||
| unit_times: Vec<UnitTime>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can all be dropped I feel like? The only thing we'll track here is CPU usage (and perhaps active)
| elapsed, | ||
| unblocked, | ||
| }); | ||
| self.unit_times.push(unit_time); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like the entire UnitTime struct could be removed. Did you see a reason keeping it?
| let timestamp = self.start_str.replace(&['-', ':'][..], ""); | ||
| let timings_path = build_runner | ||
| .files() | ||
| .timings_dir() | ||
| .expect("artifact-dir was not locked"); | ||
| paths::create_dir_all(&timings_path)?; | ||
| let filename = timings_path.join(format!("cargo-timing-{}.html", timestamp)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could probably change this file name to run-id name, just like how cargo report timings generate the report file name.
Close #16474.
What this PR addresses
As described in the issue above,
cargo build --timingsandcargo report timingseach had its own metric collection logic. Becausecargo report timingswill supersedecargo build --timings, this PR try to remove redundant logics fromcore/compiler/timings/mod.rs.Review points
core/compiler/timings/mod.rsalone is not enough forprepare_context_from_iter()to properly replay the build so I think this is an appropriate way.