Skip to content

support cache hits with differently named zig lib directories which have the same contents #13050

Closed

Description

@motiejus

Zig Version

0.10.0-dev.4176+6d7b0690a

Summary & Impact

When using a relative zig lib directory (ZIG_LIB_DIR=lib) and when building from different directories, libc shims get rebuilt for every working directory. This is a particularly nasty problem for Bazel, which uses a different directory for each "sandbox" (i.e. execution unit), but relative directory to ZIG_LIB_DIR due to reproducibility. In our case, it causes tens of gigabytes of libc++.a in zig cache directory, besides the CPU usage to generate those.

Steps to reproduce

Setup:

~$ mkdir -p a1 a2
~$ for d in a1 a2; do ln -s /code/zig/lib $d/lib; done

Directory a1:

~$ cd a1
~/a1$ time ZIG_LIB_DIR=lib zig cc -target aarch64-linux-gnu.2.28 - -o main
LLD Link... ld.lld: error: cannot open -: No such file or directory

real    0m16.092s
user    1m18.706s
sys     0m6.679s

Observe the zig c++ command takes ~16 seconds, which means it built the glibc shim. Now switch to directory a2:

~$ cd a2
~/a2$ time ZIG_LIB_DIR=lib zig c++ -target aarch64-linux-gnu.2.28 - -o main
LLD Link... ld.lld: error: cannot open -: No such file or directory

real    0m18.914s
user    1m31.513s
sys     0m7.670s

If we run this again in a2, we see the latency is significantly decreased:

~$ cd a2
~/a2$ time ZIG_LIB_DIR=lib zig c++ -target aarch64-linux-gnu.2.28 - -o main
LLD Link... ld.lld: error: cannot open -: No such file or directory

real    0m0.038s
user    0m0.017s
sys     0m0.021s

Expected Behavior

It takes <1 second to run the a2 from from the first attempt.

Activity

  1. added
    bug
    on Oct 3, 2022
  2. motiejus commented on Oct 3, 2022

    @motiejus
    ContributorAuthor

    I've observed that some of the ~/.cache/zig/h/*.txt files (in my understanding, cache manifests) contain non-canonicalized paths:

    $ grep -hr /home/motiejus/sandbox/a1/lib/std/std.zig ~/.cache/zig
    5621 305412 1656217371235171984 66001e1a55f68e4681c1f32a69223647 /home/motiejus/sandbox/a1/lib/std/std.zig
    

    IMO I would expcect it to be:

    5621 305412 1656217371235171984 66001e1a55f68e4681c1f32a69223647 /code/zig/lib/std/std.zig
    

    Or even better, if at all possible?

    5621 305412 1656217371235171984 66001e1a55f68e4681c1f32a69223647 lib/std/std.zig
    

    Though I did not find where this path is actually constructed.

  3. added 4 commits that reference this issue on Oct 3, 2022
    f2d029a
    4cae217
    8bebd94
    23a7a3b
  4. added this to the 0.10.0 milestone on Oct 5, 2022
  5. added
    frontend
    on Oct 5, 2022
  6. andrewrk commented on Oct 5, 2022

    @andrewrk
    Member

    Ideally, manifest files in the global cache would contain only absolute files, since the global cache is shared among multiple projects, each with a potentially different working directory.

    Meanwhile, manifest files in the local cache would ideally contain only paths relative to the local project root, so that the project directory could be moved to a new location and the cache would continue functioning seamlessly.

    I had a look around and any time we call Cache.addFile, it calls fs.path.resolve on the input path to convert from relative path to absolute path. The one exception is Cache.addFilePostContents, which accepts a pre-resolved path. One of the callsites does this correctly, but the other does not, and I think that is the cause of the bug.

    I don't think resolving symlinks is necessary or desirable.

    I'll propose a different PR shortly to solve this problem.

  7. andrewrk commented on Oct 5, 2022

    @andrewrk
    Member

    I opened #13071 after which no relative paths show up in the global cache manifest.

    However, the system will still consider two paths to be different even if they are symlinks resolving to the same directory, leaving the original problem detailed in this issue unresolved. However, might I suggest that the link is resolved prior to setting the environment variable? For example, this solves the problem:

    [nix-shell:~/Downloads/zig/build-release]$ cd a1
    
    [nix-shell:~/Downloads/zig/build-release/a1]$ ZIG_LIB_DIR=$(readlink lib) ../stage4/bin/zig build-exe ../../test/standalone/hello_world/hello.zig 
    
    [nix-shell:~/Downloads/zig/build-release/a1]$ cd ../a2
    
    [nix-shell:~/Downloads/zig/build-release/a2]$ ZIG_LIB_DIR=$(readlink lib) ../stage4/bin/zig build-exe ../../test/standalone/hello_world/hello.zig 
    

    With this strategy, only one copy of the global static libraries such as compiler_rt.a and libc++.a is generated.

  8. motiejus commented on Oct 5, 2022

    @motiejus
    ContributorAuthor

    However, might I suggest that the link is resolved prior to setting the environment variable? For example, this solves the problem:

    Unfortunately, the Bazel's symlink to zig's lib is an absolute path, which brings back #12980 back again:. If we readlink it, the ZIG_LIB_DIR becomes absolute, which causes the result of zig cc -M to be absolute, which beats Bazel's global cache.

    As a result, two machines/people with a different sandbox path (e.g. /home/motiejus and /home/john is enough to make a difference a difference) cannot share the compiled artifacts.

  9. motiejus commented on Oct 5, 2022

    @motiejus
    ContributorAuthor

    In other words, I am looking for the right ZIG_LIB_DIR, so the result of zig cc -M file.c contains only relative paths.

  10. andrewrk commented on Oct 12, 2022

    @andrewrk
    Member

    Hmm I'm actually not able to reproduce the problem. Here is an attempt where I am getting what I understand to be the desired behavior, based on our discussion:

    [nix-shell:~/Downloads/zig/build-release]$ cd a1
    [nix-shell:~/Downloads/zig/build-release/a1]$ ls -l lib
    lrwxrwxrwx 1 andy users 28 Oct  4 23:02 lib -> /home/andy/Downloads/zig/lib
    [nix-shell:~/Downloads/zig/build-release/a1]$ ZIG_LIB_DIR=lib ../stage4/bin/zig cc -o hello -c hello.c -MD -MV -MF hello.d -target x86_64-linux-gnu.2.33
    [nix-shell:~/Downloads/zig/build-release/a1]$ cat hello.d 
    hello: hello.c lib/libc/include/generic-glibc/time.h \
      lib/libc/include/generic-glibc/features.h \
      lib/libc/include/generic-glibc/features-time64.h \
      lib/libc/include/x86_64-linux-gnu/bits/wordsize.h \
      lib/libc/include/x86_64-linux-gnu/bits/timesize.h \
      lib/libc/include/generic-glibc/stdc-predef.h \
      lib/libc/include/generic-glibc/sys/cdefs.h \
      lib/libc/include/x86_64-linux-gnu/bits/long-double.h \
      lib/libc/include/x86_64-linux-gnu/gnu/stubs.h \
      lib/libc/include/x86_64-linux-gnu/gnu/stubs-64.h lib/include/stddef.h \
      lib/libc/include/generic-glibc/bits/time.h \
      lib/libc/include/generic-glibc/bits/types.h \
      lib/libc/include/x86_64-linux-gnu/bits/typesizes.h \
      lib/libc/include/generic-glibc/bits/time64.h \
      lib/libc/include/generic-glibc/bits/types/clock_t.h \
      lib/libc/include/generic-glibc/bits/types/time_t.h \
      lib/libc/include/generic-glibc/bits/types/struct_tm.h \
      lib/libc/include/generic-glibc/bits/types/struct_timespec.h \
      lib/libc/include/generic-glibc/bits/endian.h \
      lib/libc/include/x86_64-linux-gnu/bits/endianness.h \
      lib/libc/include/generic-glibc/bits/types/clockid_t.h \
      lib/libc/include/generic-glibc/bits/types/timer_t.h \
      lib/libc/include/generic-glibc/bits/types/struct_itimerspec.h \
      lib/libc/include/generic-glibc/bits/types/locale_t.h \
      lib/libc/include/generic-glibc/bits/types/__locale_t.h
    

    Here you can see that all the files are relative paths.

  11. 9 remaining items

  12. motiejus commented on Oct 26, 2022

    @motiejus
    ContributorAuthor

    I spent a couple of days investigating the intersection of bazel and zig-cc with regards to build performance issues. There is one more aspect to it:

    1. By default, Bazel creates a symlink farm per sandbox. Sandbox is created per action (action is a single compilation step). Zig has ~16k files, thus it takes time to create and teardown every sandbox. This is the cause of the current slowness that I've mentioned in other channels.
    2. To overcome (1), Bazel supports sandboxfs. It is a FUSE-mounted directory that makes the "sandbox" read-only, whlist avoiding the symlink farm. That could be used with ZIG_LIB_DIR=external/zig_sdk/lib. This fixes the performance issue with a symlink farm (16k symlinks are replaced with a single mount(2), however, as far as Zig is concerned, it's a completely different directory, which messes up global caching. With sandboxfs, even Cache: resolve symlinks when manipulating file names #13051 does not help.

    To sum up: for Zig to be friendly with Bazel, we need to find a way for zig to understand that different ZIG_LIB_DIRs (read: different sandboxes) may actually refer to identical directory contents.

  13. andrewrk commented on Nov 14, 2022

    @andrewrk
    Member

    Summary of a voice chat that @motiejus and I had: It looks like this problem can be solved by a combination of two things:

    • Not adding the zig lib directory path into cache hash. It's enough to consider the file contents of every file.
    • In cache manifest files, have a special way to encode a "zig lib directory" prefix. Files from the zig lib directory in the global cache will not be absolute file paths, but be relative to the zig lib directory, and indicate that they are such.

    This enhancement will benefit the portability of zig because "absolute file paths" are problematic for some systems, such as WASI, and operating systems that do not have realpath.

    I will look into this over the next week.

  14. added 4 commits that reference this issue on Nov 19, 2022
    532b0c8
    3da0ab5
    cabd955
    fb511e3
  15. changed the title [-]ZIG_LIB_DIR: relative paths cause shims to be rebuilt[/-] [+]support cache hits with differently named zig lib directories which have the same contents[/+] on Nov 20, 2022
  16. added
    accepted
    on Nov 20, 2022
  17. added 3 commits that reference this issue on Nov 21, 2022
    a02eaee
    fb7f4e4
    21bd136
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

    acceptedenhancementfrontendproposal

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions