Skip to content

Allow restricting the number of parallel linker invocations #9157

Open

Description

@luser

View all comments

In CI at my work, we ran into a situation where rustc would get OOM-killed while linking example binaries:

error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" <…>
  = note: collect2: fatal error: ld terminated with signal 9 [Killed]
          compilation terminated.

We were able to mitigate this by using a builder with more available memory, but it's unfortunate. We could dial down the parallelism of the whole build by explicitly passing -jN, but that would make the non-linking parts of the build slower by leaving CPU cores idle.

It would be ideal if we could explicitly ask cargo to lower the number of parallel linker invocations it will spawn. Compile steps are generally CPU-intensive, but linking is usually much more memory-intensive. In the extreme case, for large projects like Firefox and Chromium where the vast majority of code gets linked into a single binary, that link step far outweighs any other part of the build in terms of memory usage.

In terms of prior art, ninja has a concept of "pools" that allow expressing this sort of restriction in a more generic way:

Pools allow you to allocate one or more rules or edges a finite number of concurrent jobs which is more tightly restricted than the default parallelism.
This can be useful, for example, to restrict a particular expensive rule (like link steps for huge executables), or to restrict particular build statements which you know perform poorly when run concurrently.

The Ninja feature was originally motivated by Chromium builds switching to Ninja and wanting to support distributed builds, in which there might be capacity to spawn way more compile jobs in parallel since they can be run on distributed build nodes, but link jobs, needing to be run on the local machine, would want a lower limit.

If this were implemented, one could imagine a further step whereby cargo could estimate how heavy individual linker invocations are by the number of crates they link, and attempt to set a reasonable default value based on that and the amount of available system memory.

Activity

  1. added
    C-feature-requestCategory: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`
    on Feb 9, 2021
  2. luser commented on Feb 9, 2021

    @luser
    ContributorAuthor

    I believe this would also be useful for people using sccache in distributed compilation mode, as they could have an exaggerated version of this problem similar to what's described in that message about Chromium, with more build capacity for compiling than linking.

  3. added
    A-jobserverArea: jobserver, concurrency, parallelism
    A-linkageArea: linker issues, dylib, cdylib, shared libraries, so
    on Feb 10, 2021
  4. Be-ing commented on Feb 27, 2021

    @Be-ing

    I have no idea if this would be practical, but could cargo automatically monitor memory usage to adjust how many concurrent threads to use?

  5. levkk commented on Oct 26, 2021

    @levkk

    I've managed to work around this by enabling swap. Linking time did not suffer visibly. On Ubuntu, I followed this guide.

  6. sagudev commented on Mar 16, 2023

    @sagudev

    Proposed Solution

    Adding --link-jobs option to specify number of jobs for linking. The option would default to number of parallel jobs.

    Here what would help look like (-j option is displayed for comparison):

    -j, --jobs <N>                Number of parallel jobs, defaults to # of CPUs
    --link-jobs <N>               Number of parallel jobs for linking, defaults to # of parallel jobs
    
  7. added
    S-needs-designStatus: Needs someone to work further on the design for the feature or fix. NOT YET accepted.
    on Oct 31, 2023
  8. weihanglo commented on Oct 31, 2023

    @weihanglo
    Member

    @rustbot claim

  9. epage commented on Nov 2, 2023

    @epage
    Contributor

    @weihanglo see also #7480

  10. weihanglo commented on Nov 2, 2023

    @weihanglo
    Member

    FWIW, Cabel community had a discussion a while back: haskell/cabal#1529

  11. weihanglo commented on Nov 3, 2023

    @weihanglo
    Member

    Potentially the unstable rustc flag -Zno-link can separate linking phase from others (see #9019), and then Cargo can control the parallelism of linker invocations. Somebody needs to take a look at the status of -Zno-link/-Zlink-only in rustc (and that is very likely me).

  12. epage commented on Nov 3, 2023

    @epage
    Contributor

    As this is focusing on the problem of OOM, I'm going to close in favor of #12912 so we keep the conversations in one place.

  13. 3 remaining items

  14. weihanglo commented on Nov 7, 2023

    @weihanglo
    Member

    Thanks. Reopened as it might need both #9019 and #12912, and maybe other upstream works from rust to make it happen.

  15. weihanglo commented on Nov 9, 2023

    @weihanglo
    Member

    FWIW, there is a --no-keep-memory flag for GNU linker. Haven't tried it but might help before we make some progress on this.

    https://linux.die.net/man/1/ld

  16. weihanglo commented on Dec 13, 2023

    @weihanglo
    Member

    rust-lang/rust#117962 has made into nightly. It could alleviate the pain of linker OOM to some extent.

  17. luser commented on Dec 13, 2023

    @luser
    ContributorAuthor

    FWIW, there is a --no-keep-memory flag for GNU linker. Haven't tried it but might help before we make some progress on this.

    I suspect this will make performance much worse in the average case, unfortunately.

  18. soloturn commented on Oct 19, 2024

    @soloturn
    Contributor

    i think this can be closed, as it is the linkers business to not run out of memory. e.g. mold does this, see rui314/mold#1319 via its MOLD_JOBS environment variable. to avoid cargo does everything and nothing well ...

  19. removed their assignment
    on Oct 20, 2024
  20. epage commented on Oct 21, 2024

    @epage
    Contributor

    For something like that, jobserver support in a linker would be a big help so we can coordinate across rustc and the linker for how many threads are available to use.

    That also only focuses on number of parallel threads and not actual memory consumption. Like with threads, likely any solution coordination will be needed between links and rustc / cargo

  21. added a commit that references this issue on Jul 8, 2025
  22. added 2 commits that reference this issue on Jul 8, 2025
  23. kornelski commented on Aug 22, 2025

    @kornelski
    Contributor

    I don't expect jobserver support in linkers to solve the problem of linking running out of memory:

    • The main issue isn't the number of threads. Linkers may be keeping too much data in RAM regardless of how many threads are active. I'm running into OOMs with GNU ld (without LTO), which AFAIK is still mostly single-threaded.

    • It's not easy to change a linker, and may not even be possible to have one with jobserver support. Platforms other than x86 Linux may require use of OS vendor's own linker. It's also tricky to customize the default linker in Cargo in a robust way, because rustc has a bunch of -C flags for linkers, which can break when the linker is changed using Cargo's per-target linker path. It's hard to keep these two in sync and without duplicates when builds may be overriding them individually via env vars. It gets even more complicated when cross-compiling and cmake is involved.

    • Even if there was a jobserver support in linkers, Cargo still needs some setting to configure lower number of jobs for the linkers' jobserver.

    A simple concurrency limit for the number of linker invocations would work for me, something like build.link-jobs. Even a basic limit of just one linker invocation at a time would be helpful (which I find necessary for LTO).

  24. weihanglo commented on Aug 25, 2025

    @weihanglo
    Member

    Yeah jobserver may not help control "the number of parallel linker invocations" but only overall parallelism unless Cargo knows what calls are linker calls. Unfortunately Cargo has no control nor visibility over linker invocation now. In #9157 (comment) I linked some relevant issue we'd like to see rustc to support thought it has bug preventing Cargo from integration (#9019 (comment)).

    Apart from -Zno-link/-Zlink-only, another other possible workaround is Cargo to limit the parallelism of the rustc invocation producing binaries (e.g., --crate-type bin|dylib|…), so we can get an approximiate linker parallelism control. This might be bad to performance because the non-codegen part of the final crate is usually not that memory heavy and can be processed in parallel.

  25. weihanglo commented on Sep 28, 2026

    @weihanglo
    Member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-jobserverArea: jobserver, concurrency, parallelismA-linkageArea: linker issues, dylib, cdylib, shared libraries, soC-feature-requestCategory: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`S-needs-designStatus: Needs someone to work further on the design for the feature or fix. NOT YET accepted.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions