Skip to content

Implement --perf flag to lintcheck for benchmarking #14116

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

Merged
merged 3 commits into from
Feb 2, 2025
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
9 changes: 9 additions & 0 deletions lintcheck/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use clap::{Parser, Subcommand, ValueEnum};
use std::num::NonZero;
use std::path::PathBuf;

#[allow(clippy::struct_excessive_bools)]
#[derive(Parser, Clone, Debug)]
#[command(args_conflicts_with_subcommands = true)]
pub(crate) struct LintcheckConfig {
Expand All @@ -11,6 +12,9 @@ pub(crate) struct LintcheckConfig {
short = 'j',
value_name = "N",
default_value_t = 0,
default_value_if("perf", "true", Some("1")), // Limit jobs to 1 when benchmarking
conflicts_with("perf"),
required = false,
hide_default_value = true
)]
pub max_jobs: usize,
Expand Down Expand Up @@ -46,6 +50,11 @@ pub(crate) struct LintcheckConfig {
/// Run clippy on the dependencies of crates specified in crates-toml
#[clap(long, conflicts_with("max_jobs"))]
pub recursive: bool,
/// Also produce a `perf.data` file, implies --jobs=1,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't look like this implies is true currently, there might be a #[clap] flag for it though

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realized it immediately after uploading the issue, just updated it to actually imply (declare a default value, and exit if it the flags don't comply)

/// the `perf.data` file can be found at
/// `target/lintcheck/sources/<package>-<version>/perf.data`
#[clap(long)]
pub perf: bool,
#[command(subcommand)]
pub subcommand: Option<Commands>,
}
Expand Down
53 changes: 43 additions & 10 deletions lintcheck/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,25 @@ impl Crate {

clippy_args.extend(lint_levels_args.iter().map(String::as_str));

let mut cmd = Command::new("cargo");
let mut cmd;

if config.perf {
cmd = Command::new("perf");
cmd.args(&[
"record",
"-e",
"instructions", // Only count instructions
"-g", // Enable call-graph, useful for flamegraphs and produces richer reports
"--quiet", // Do not tamper with lintcheck's normal output
"-o",
"perf.data",
"--",
"cargo",
]);
} else {
cmd = Command::new("cargo");
}

cmd.arg(if config.fix { "fix" } else { "check" })
.arg("--quiet")
.current_dir(&self.path)
Expand Down Expand Up @@ -234,12 +252,22 @@ fn normalize_diag(
}

/// Builds clippy inside the repo to make sure we have a clippy executable we can use.
fn build_clippy() -> String {
let output = Command::new("cargo")
.args(["run", "--bin=clippy-driver", "--", "--version"])
.stderr(Stdio::inherit())
.output()
.unwrap();
fn build_clippy(release_build: bool) -> String {
let mut build_cmd = Command::new("cargo");
build_cmd.args([
"run",
"--bin=clippy-driver",
if release_build { "-r" } else { "" },
"--",
"--version",
]);

if release_build {
build_cmd.env("CARGO_PROFILE_RELEASE_DEBUG", "true");
}

let output = build_cmd.stderr(Stdio::inherit()).output().unwrap();

if !output.status.success() {
eprintln!("Error: Failed to compile Clippy!");
std::process::exit(1);
Expand Down Expand Up @@ -270,13 +298,18 @@ fn main() {

#[allow(clippy::too_many_lines)]
fn lintcheck(config: LintcheckConfig) {
let clippy_ver = build_clippy();
let clippy_driver_path = fs::canonicalize(format!("target/debug/clippy-driver{EXE_SUFFIX}")).unwrap();
let clippy_ver = build_clippy(config.perf);
let clippy_driver_path = fs::canonicalize(format!(
"target/{}/clippy-driver{EXE_SUFFIX}",
if config.perf { "release" } else { "debug" }
))
.unwrap();

// assert that clippy is found
assert!(
clippy_driver_path.is_file(),
"target/debug/clippy-driver binary not found! {}",
"target/{}/clippy-driver binary not found! {}",
if config.perf { "release" } else { "debug" },
clippy_driver_path.display()
);

Expand Down