Skip to content

Commit a0cad1e

Browse files
committed
Auto merge of #13532 - epage:t, r=weihanglo
fix(cli): Trace core cargo operations ### What does this PR try to resolve? This is preparation for #13339 and covers hot spots I found as well as areas currently covered by `profile::start(...)`. This is split out to avoid conflicts while working through the remaining issues for #13339. Maybe it will also serve to help debugging... ### How should we test and review this PR? ### Additional information ```console $ rg profile::start src/ src/cargo/ops/cargo_compile/mod.rs: let _p = profile::start("compiling"); src/cargo/ops/resolve.rs: let _p = profile::start("resolving with overrides..."); src/cargo/util/rustc.rs: let _p = profile::start("Rustc::new"); src/cargo/core/global_cache_tracker.rs: let _p = crate::util::profile::start("cleaning global cache files"); src/cargo/core/global_cache_tracker.rs: let _p = crate::util::profile::start("global cache db sync"); src/cargo/core/global_cache_tracker.rs: let _p = crate::util::profile::start(format!( src/cargo/core/global_cache_tracker.rs: let _p = crate::util::profile::start(format!("update db for removed {table_name}")); src/cargo/core/global_cache_tracker.rs: let _p = crate::util::profile::start(format!( src/cargo/core/global_cache_tracker.rs: let _p = crate::util::profile::start("populate untracked crate"); src/cargo/core/global_cache_tracker.rs: let _p = crate::util::profile::start(format!("populate untracked {table_name}")); src/cargo/core/global_cache_tracker.rs: let _p = crate::util::profile::start(format!("update NULL sizes {table_name}")); src/cargo/core/global_cache_tracker.rs: let _p = crate::util::profile::start("saving last-use data"); src/cargo/core/resolver/features.rs: let _p = profile::start("resolve features"); src/cargo/core/resolver/mod.rs: let _p = profile::start("resolving"); src/cargo/core/compiler/fingerprint/mod.rs: let _p = profile::start(format!( src/cargo/core/compiler/mod.rs: let p = profile::start(format!("preparing: {}/{}", unit.pkg, unit.target.name())); src/cargo/core/compiler/build_runner/mod.rs: let _p = profile::start("preparing layout"); src/cargo/core/compiler/custom_build.rs: let _p = profile::start(format!( src/cargo/core/compiler/job_queue/mod.rs: let _p = profile::start("executing the job graph"); ```
2 parents 8ba82fc + df18c53 commit a0cad1e

File tree

19 files changed

+42
-0
lines changed

19 files changed

+42
-0
lines changed

src/bin/cargo/cli.rs

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::util::is_rustup;
1616
use cargo::core::shell::ColorChoice;
1717
use cargo::util::style;
1818

19+
#[tracing::instrument(skip_all)]
1920
pub fn main(gctx: &mut GlobalContext) -> CliResult {
2021
// CAUTION: Be careful with using `config` until it is configured below.
2122
// In general, try to avoid loading config values unless necessary (like
@@ -272,6 +273,7 @@ fn add_ssl(version_string: &mut String) {
272273
/// [`GlobalArgs`] need to be extracted before expanding aliases because the
273274
/// clap code for extracting a subcommand discards global options
274275
/// (appearing before the subcommand).
276+
#[tracing::instrument(skip_all)]
275277
fn expand_aliases(
276278
gctx: &mut GlobalContext,
277279
args: ArgMatches,
@@ -377,6 +379,7 @@ For more information, see issue #12207 <https://github.com/rust-lang/cargo/issue
377379
Ok((args, GlobalArgs::default()))
378380
}
379381

382+
#[tracing::instrument(skip_all)]
380383
fn configure_gctx(
381384
gctx: &mut GlobalContext,
382385
args: &ArgMatches,
@@ -459,6 +462,7 @@ impl Exec {
459462
}
460463
}
461464

465+
#[tracing::instrument(skip_all)]
462466
fn exec(self, gctx: &mut GlobalContext, subcommand_args: &ArgMatches) -> CliResult {
463467
match self {
464468
Self::Builtin(exec) => exec(gctx, subcommand_args),
@@ -530,6 +534,7 @@ impl GlobalArgs {
530534
}
531535
}
532536

537+
#[tracing::instrument(skip_all)]
533538
pub fn cli(gctx: &GlobalContext) -> Command {
534539
// Don't let config errors get in the way of parsing arguments
535540
let term = gctx.get::<TermConfig>("term").unwrap_or_default();

src/bin/cargo/main.rs

+2
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ fn search_directories(gctx: &GlobalContext) -> Vec<PathBuf> {
287287
}
288288

289289
/// Initialize libgit2.
290+
#[tracing::instrument(skip_all)]
290291
fn init_git(gctx: &GlobalContext) {
291292
// Disabling the owner validation in git can, in theory, lead to code execution
292293
// vulnerabilities. However, libgit2 does not launch executables, which is the foundation of
@@ -318,6 +319,7 @@ fn init_git(gctx: &GlobalContext) {
318319
/// If the user has a non-default network configuration, then libgit2 will be
319320
/// configured to use libcurl instead of the built-in networking support so
320321
/// that those configuration settings can be used.
322+
#[tracing::instrument(skip_all)]
321323
fn init_git_transports(gctx: &GlobalContext) {
322324
match needs_custom_http_transport(gctx) {
323325
Ok(true) => {}

src/cargo/core/compiler/build_runner/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ impl<'a, 'gctx> BuildRunner<'a, 'gctx> {
133133
/// See [`ops::cargo_compile`] for a higher-level view of the compile process.
134134
///
135135
/// [`ops::cargo_compile`]: ../../../ops/cargo_compile/index.html
136+
#[tracing::instrument(skip_all)]
136137
pub fn compile(mut self, exec: &Arc<dyn Executor>) -> CargoResult<Compilation<'gctx>> {
137138
// A shared lock is held during the duration of the build since rustc
138139
// needs to read from the `src` cache, and we don't want other
@@ -324,6 +325,7 @@ impl<'a, 'gctx> BuildRunner<'a, 'gctx> {
324325
.map(|output| output.bin_dst().clone()))
325326
}
326327

328+
#[tracing::instrument(skip_all)]
327329
pub fn prepare_units(&mut self) -> CargoResult<()> {
328330
let dest = self.bcx.profiles.get_dir_name();
329331
let host_layout = Layout::new(self.bcx.ws, None, &dest)?;
@@ -349,6 +351,7 @@ impl<'a, 'gctx> BuildRunner<'a, 'gctx> {
349351

350352
/// Prepare this context, ensuring that all filesystem directories are in
351353
/// place.
354+
#[tracing::instrument(skip_all)]
352355
pub fn prepare(&mut self) -> CargoResult<()> {
353356
let _p = profile::start("preparing layout");
354357

@@ -451,6 +454,7 @@ impl<'a, 'gctx> BuildRunner<'a, 'gctx> {
451454

452455
/// Check if any output file name collision happens.
453456
/// See <https://github.com/rust-lang/cargo/issues/6313> for more.
457+
#[tracing::instrument(skip_all)]
454458
fn check_collisions(&self) -> CargoResult<()> {
455459
let mut output_collisions = HashMap::new();
456460
let describe_collision = |unit: &Unit, other_unit: &Unit, path: &PathBuf| -> String {
@@ -633,6 +637,7 @@ impl<'a, 'gctx> BuildRunner<'a, 'gctx> {
633637
/// If the current crate has reverse-dependencies, such a Check unit should exist, and so
634638
/// we use that crate's metadata. If not, we use the crate's Doc unit so at least examples
635639
/// scraped from the current crate can be used when documenting the current crate.
640+
#[tracing::instrument(skip_all)]
636641
pub fn compute_metadata_for_doc_units(&mut self) {
637642
for unit in self.bcx.unit_graph.keys() {
638643
if !unit.mode.is_doc() && !unit.mode.is_doc_scrape() {

src/cargo/core/compiler/custom_build.rs

+1
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ impl LinkArgTarget {
194194
}
195195

196196
/// Prepares a `Work` that executes the target as a custom build script.
197+
#[tracing::instrument(skip_all)]
197198
pub fn prepare(build_runner: &mut BuildRunner<'_, '_>, unit: &Unit) -> CargoResult<Job> {
198199
let _p = profile::start(format!(
199200
"build script prepare: {}/{}",

src/cargo/core/compiler/fingerprint/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ pub use dirty_reason::DirtyReason;
399399
/// transitively propagate throughout the dependency graph, it only forces this
400400
/// one unit which is very unlikely to be what you want unless you're
401401
/// exclusively talking about top-level units.
402+
#[tracing::instrument(skip(build_runner, unit))]
402403
pub fn prepare_target(
403404
build_runner: &mut BuildRunner<'_, '_>,
404405
unit: &Unit,

src/cargo/core/compiler/job_queue/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,7 @@ impl<'gctx> JobQueue<'gctx> {
467467
/// This function will spawn off `config.jobs()` workers to build all of the
468468
/// necessary dependencies, in order. Freshness is propagated as far as
469469
/// possible along each dependency chain.
470+
#[tracing::instrument(skip_all)]
470471
pub fn execute(
471472
mut self,
472473
build_runner: &mut BuildRunner<'_, '_>,

src/cargo/core/compiler/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ impl Executor for DefaultExecutor {
158158
/// Note that **no actual work is executed as part of this**, that's all done
159159
/// next as part of [`JobQueue::execute`] function which will run everything
160160
/// in order with proper parallelism.
161+
#[tracing::instrument(skip(build_runner, jobs, plan, exec))]
161162
fn compile<'gctx>(
162163
build_runner: &mut BuildRunner<'_, 'gctx>,
163164
jobs: &mut JobQueue<'gctx>,

src/cargo/core/compiler/unit_dependencies.rs

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ impl IsArtifact {
8181
/// Then entry point for building a dependency graph of compilation units.
8282
///
8383
/// You can find some information for arguments from doc of [`State`].
84+
#[tracing::instrument(skip_all)]
8485
pub fn build_unit_dependencies<'a, 'gctx>(
8586
ws: &'a Workspace<'gctx>,
8687
package_set: &'a PackageSet<'gctx>,

src/cargo/core/global_cache_tracker.rs

+9
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ impl GlobalCacheTracker {
546546
.with_context(|| "failed to clean entries from the global cache")
547547
}
548548

549+
#[tracing::instrument(skip_all)]
549550
fn clean_inner(
550551
&mut self,
551552
clean_ctx: &mut CleanContext<'_>,
@@ -696,6 +697,7 @@ impl GlobalCacheTracker {
696697
///
697698
/// These orphaned files will be added to `delete_paths` so that the
698699
/// caller can delete them.
700+
#[tracing::instrument(skip_all)]
699701
fn sync_db_with_files(
700702
conn: &Connection,
701703
now: Timestamp,
@@ -795,6 +797,7 @@ impl GlobalCacheTracker {
795797
}
796798

797799
/// For parent tables, add any entries that are on disk but aren't tracked in the db.
800+
#[tracing::instrument(skip_all)]
798801
fn update_parent_for_missing_from_db(
799802
conn: &Connection,
800803
now: Timestamp,
@@ -822,6 +825,7 @@ impl GlobalCacheTracker {
822825
///
823826
/// This could happen for example if the user manually deleted the file or
824827
/// any such scenario where the filesystem and db are out of sync.
828+
#[tracing::instrument(skip_all)]
825829
fn update_db_for_removed(
826830
conn: &Connection,
827831
parent_table_name: &str,
@@ -851,6 +855,7 @@ impl GlobalCacheTracker {
851855
}
852856

853857
/// Removes database entries for any files that are not on disk for the parent tables.
858+
#[tracing::instrument(skip_all)]
854859
fn update_db_parent_for_removed_from_disk(
855860
conn: &Connection,
856861
parent_table_name: &str,
@@ -888,6 +893,7 @@ impl GlobalCacheTracker {
888893
/// Updates the database to add any `.crate` files that are currently
889894
/// not tracked (such as when they are downloaded by an older version of
890895
/// cargo).
896+
#[tracing::instrument(skip_all)]
891897
fn populate_untracked_crate(
892898
conn: &Connection,
893899
now: Timestamp,
@@ -922,6 +928,7 @@ impl GlobalCacheTracker {
922928

923929
/// Updates the database to add any files that are currently not tracked
924930
/// (such as when they are downloaded by an older version of cargo).
931+
#[tracing::instrument(skip_all)]
925932
fn populate_untracked(
926933
conn: &Connection,
927934
now: Timestamp,
@@ -987,6 +994,7 @@ impl GlobalCacheTracker {
987994
/// size.
988995
///
989996
/// `update_db_for_removed` should be called before this is called.
997+
#[tracing::instrument(skip_all)]
990998
fn update_null_sizes(
991999
conn: &Connection,
9921000
gctx: &GlobalContext,
@@ -1560,6 +1568,7 @@ impl DeferredGlobalLastUse {
15601568
/// Saves all of the deferred information to the database.
15611569
///
15621570
/// This will also clear the state of `self`.
1571+
#[tracing::instrument(skip_all)]
15631572
pub fn save(&mut self, tracker: &mut GlobalCacheTracker) -> CargoResult<()> {
15641573
let _p = crate::util::profile::start("saving last-use data");
15651574
trace!(target: "gc", "saving last-use data");

src/cargo/core/package.rs

+1
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@ impl<'gctx> PackageSet<'gctx> {
497497
}
498498

499499
/// Downloads any packages accessible from the give root ids.
500+
#[tracing::instrument(skip_all)]
500501
pub fn download_accessible(
501502
&self,
502503
resolve: &Resolve,

src/cargo/core/resolver/features.rs

+1
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ pub struct FeatureResolver<'a, 'gctx> {
444444
impl<'a, 'gctx> FeatureResolver<'a, 'gctx> {
445445
/// Runs the resolution algorithm and returns a new [`ResolvedFeatures`]
446446
/// with the result.
447+
#[tracing::instrument(skip_all)]
447448
pub fn resolve(
448449
ws: &Workspace<'gctx>,
449450
target_data: &'a mut RustcTargetData<'gctx>,

src/cargo/core/resolver/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ mod version_prefs;
122122
///
123123
/// * `config` - a location to print warnings and such, or `None` if no warnings
124124
/// should be printed
125+
#[tracing::instrument(skip_all)]
125126
pub fn resolve(
126127
summaries: &[(Summary, ResolveOpts)],
127128
replacements: &[(PackageIdSpec, Dependency)],

src/cargo/ops/cargo_compile/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ pub fn compile_with_exec<'a>(
142142
}
143143

144144
/// Like [`compile_with_exec`] but without warnings from manifest parsing.
145+
#[tracing::instrument(skip_all)]
145146
pub fn compile_ws<'a>(
146147
ws: &Workspace<'a>,
147148
options: &CompileOptions,
@@ -197,6 +198,7 @@ pub fn print<'a>(
197198
///
198199
/// For how it works and what data it collects,
199200
/// please see the [module-level documentation](self).
201+
#[tracing::instrument(skip_all)]
200202
pub fn create_bcx<'a, 'gctx>(
201203
ws: &'a Workspace<'gctx>,
202204
options: &'a CompileOptions,

src/cargo/ops/lockfile.rs

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::util::Filesystem;
66

77
use anyhow::Context as _;
88

9+
#[tracing::instrument(skip_all)]
910
pub fn load_pkg_lockfile(ws: &Workspace<'_>) -> CargoResult<Option<Resolve>> {
1011
let lock_root = lock_root(ws);
1112
if !lock_root.as_path_unlocked().join("Cargo.lock").exists() {
@@ -32,6 +33,7 @@ pub fn resolve_to_string(ws: &Workspace<'_>, resolve: &mut Resolve) -> CargoResu
3233
Ok(out)
3334
}
3435

36+
#[tracing::instrument(skip_all)]
3537
pub fn write_pkg_lockfile(ws: &Workspace<'_>, resolve: &mut Resolve) -> CargoResult<()> {
3638
let (orig, mut out, lock_root) = resolve_to_string_orig(ws, resolve);
3739

src/cargo/ops/resolve.rs

+3
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ pub fn resolve_ws_with_opts<'gctx>(
234234
})
235235
}
236236

237+
#[tracing::instrument(skip_all)]
237238
fn resolve_with_registry<'gctx>(
238239
ws: &Workspace<'gctx>,
239240
registry: &mut PackageRegistry<'gctx>,
@@ -271,6 +272,7 @@ fn resolve_with_registry<'gctx>(
271272
///
272273
/// If `register_patches` is true, then entries from the `[patch]` table in
273274
/// the manifest will be added to the given `PackageRegistry`.
275+
#[tracing::instrument(skip_all)]
274276
pub fn resolve_with_previous<'gctx>(
275277
registry: &mut PackageRegistry<'gctx>,
276278
ws: &Workspace<'gctx>,
@@ -529,6 +531,7 @@ pub fn resolve_with_previous<'gctx>(
529531

530532
/// Read the `paths` configuration variable to discover all path overrides that
531533
/// have been configured.
534+
#[tracing::instrument(skip_all)]
532535
pub fn add_overrides<'a>(
533536
registry: &mut PackageRegistry<'a>,
534537
ws: &Workspace<'a>,

src/cargo/util/command_prelude.rs

+1
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ pub trait ArgMatchesExt {
500500
root_manifest(self._value_of("manifest-path").map(Path::new), gctx)
501501
}
502502

503+
#[tracing::instrument(skip_all)]
503504
fn workspace<'a>(&self, gctx: &'a GlobalContext) -> CargoResult<Workspace<'a>> {
504505
let root = self.root_manifest(gctx)?;
505506
let mut ws = Workspace::new(&root, gctx)?;

src/cargo/util/context/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1945,6 +1945,7 @@ impl GlobalContext {
19451945
/// Locks are usually acquired via [`GlobalContext::acquire_package_cache_lock`]
19461946
/// or [`GlobalContext::try_acquire_package_cache_lock`].
19471947
#[track_caller]
1948+
#[tracing::instrument(skip_all)]
19481949
pub fn assert_package_cache_locked<'a>(
19491950
&self,
19501951
mode: CacheLockMode,
@@ -1965,6 +1966,7 @@ impl GlobalContext {
19651966
///
19661967
/// See [`crate::util::cache_lock`] for an in-depth discussion of locking
19671968
/// and lock modes.
1969+
#[tracing::instrument(skip_all)]
19681970
pub fn acquire_package_cache_lock(&self, mode: CacheLockMode) -> CargoResult<CacheLock<'_>> {
19691971
self.package_cache_lock.lock(self, mode)
19701972
}
@@ -1974,6 +1976,7 @@ impl GlobalContext {
19741976
///
19751977
/// See [`crate::util::cache_lock`] for an in-depth discussion of locking
19761978
/// and lock modes.
1979+
#[tracing::instrument(skip_all)]
19771980
pub fn try_acquire_package_cache_lock(
19781981
&self,
19791982
mode: CacheLockMode,

src/cargo/util/rustc.rs

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ impl Rustc {
3939
///
4040
/// If successful this function returns a description of the compiler along
4141
/// with a list of its capabilities.
42+
#[tracing::instrument(skip(gctx))]
4243
pub fn new(
4344
path: PathBuf,
4445
wrapper: Option<PathBuf>,

src/cargo/util/toml/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ use self::targets::targets;
4444
/// within the manifest. For virtual manifests, these paths can only
4545
/// come from patched or replaced dependencies. These paths are not
4646
/// canonicalized.
47+
#[tracing::instrument(skip(gctx))]
4748
pub fn read_manifest(
4849
path: &Path,
4950
source_id: SourceId,

0 commit comments

Comments
 (0)