Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Improve CircleCI caching - #12328

Merged
jfirebaugh merged 4 commits into
masterfrom
ci-caching-improvements
Jul 13, 2018
Merged

jfirebaugh merged 4 commits into
masterfrom
ci-caching-improvements

Conversation

@jfirebaugh

Copy link
Copy Markdown
Contributor

Fixes #12322.

Separate node_modules, mason_packages, and cmake caches; and improve ccache freshness.

Save a fresh ccache on every build by including (a checksum of) the current revision in the cache key. In the worst case, saving the cache appears to take ~1:30 for android-release; other builds appear to take a small amount of time compared to the time saved by having a fresh cache, versus a cache that's up to a week old.

Restore caches in the following priority order (all scoped to current architecture and job):

  1. The cache for the current branch and revision.
  2. If that doesn't exist, the most recent cache for the current branch.
  3. If that doesn't exist, the cache for the parent branch and merge base, where "parent branch" means the base branch of the pull request specified by $CIRCLE_PULL_REQUEST (obtained from the GitHub API), and "merge base" means the revision returned by git merge-base when given the parent branch and $CIRCLE_BRANCH (obtained by executing that shell command). The intent here is to start a newly created branch off with the most relevant cache from its parent branch.
  4. If that doesn't exist, the most recent cache for the parent branch.
  5. If that doesn't exist, the most recent cache for master.
  6. If that doesn't exist, use the most recent cache for any build.

We cannot use the branch names or revisions themselves as key segments, because CircleCI lacks a way to interpolate arbitrary environment variables into the cache key name. Instead, we write the desired value into a file and use the {{ checksum "filename" }} syntax.

@jfirebaugh
jfirebaugh force-pushed the ci-caching-improvements branch from 5e2c5d0 to a8b649f Compare July 5, 2018 21:15
@jfirebaugh
jfirebaugh requested a review from kkaefer July 5, 2018 21:59
@jfirebaugh

Copy link
Copy Markdown
Contributor Author

Ugh. The environment variables don't work because each run step is executed in a subshell. We'll need one of the workarounds from https://discuss.circleci.com/t/environment-variable-from-output-of-command/12196/4.

@jfirebaugh
jfirebaugh force-pushed the ci-caching-improvements branch 7 times, most recently from a5f91db to 9728d45 Compare July 6, 2018 17:44
Comment thread circle.yml Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We could use tee here to simplify

Comment thread circle.yml Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm wondering whether we really need to cache Mason packages; if not cached, they're downloaded from S3 too, which probably has a similar speed, albeit not parallelized.

Comment thread circle.yml Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This one will only ever get triggered for rebuilds of the same gitsha, e.g. when hitting the rebuild button. Is there a way to find the previous HEAD SHA1 of the current branch?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't think so. However, unless builds complete out of order, the next fallback is likely to find the desired cache, because when a key prefix finds multiple matches, the most recently written one wins.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hm, but that prefix marching also applies to partial SHA1 strings?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not sure what you mean. There's no way these keys could match part of a SHA1. If the first one doesn't match anything exactly, the second one is tried. That will match any cache for the same arch/job/branch combination, and if there are two or more matches, the most recent one will be selected.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🤦‍♂️ you're right, I had my thinking backwards.

@friedbunny friedbunny added build performance Speed, stability, CPU usage, memory usage, or power usage labels Jul 6, 2018
Comment thread cmake/mason-dependencies.cmake Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This only tracks some of the dependencies. All platforms add additional dependencies with mason_use. E.g. Linux adds

mason_use(glfw VERSION 2017-07-13-67c9155)
mason_use(sqlite VERSION 3.14.2)
mason_use(libuv VERSION 1.9.1)
mason_use(nunicode VERSION 1.7.1)
mason_use(libpng VERSION 1.6.25)
mason_use(libjpeg-turbo VERSION 1.5.0)
mason_use(webp VERSION 0.5.1)
mason_use(gtest VERSION 1.8.0${MASON_CXXABI_SUFFIX})
mason_use(benchmark VERSION 1.2.0)
mason_use(icu VERSION 58.1-min-size)
mason_use(args VERSION 6.2.0 HEADER_ONLY)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

5dcd8e8. On the fence about this, WDYT?

@jfirebaugh
jfirebaugh force-pushed the ci-caching-improvements branch from 9728d45 to 0250e99 Compare July 9, 2018 20:09
So its checksum can be used in a CI cache key name.
Save a fresh ccache on every build by including (a checksum of) the current revision in the cache key. In the worst case, saving the cache appears to take ~1:30 for android-release; other builds appear to take a small amount of time compared to the time saved by having a fresh cache, versus a cache that's up to a week old.

Restore caches in the following priority order (all scoped to current architecture and job):

1. The cache for the current branch and revision.

2. If that doesn't exist, the most recent cache for the current branch.

3. If that doesn't exist, the cache for the parent branch and merge base, where "parent branch" means the base branch of the pull request specified by $CIRCLE_PULL_REQUEST (obtained from the GitHub API), and "merge base" means the revision returned by `git merge-base` when given the parent branch and $CIRCLE_BRANCH (obtained by executing that shell command). The intent here is to start a newly created branch off with the most relevant cache from its parent branch.

4. If that doesn't exist, the most recent cache for the parent branch.

5. If that doesn't exist, the most recent cache for master.

6. If that doesn't exist, use the most recent cache for any build.

We cannot use the branch names or revisions themselves as key segments, because CircleCI lacks a way to interpolate arbitrary environment variables into the cache key name. Instead, we write the desired value into a file and use the `{{ checksum "filename" }}` syntax.
@jfirebaugh
jfirebaugh force-pushed the ci-caching-improvements branch from 0250e99 to adc4b04 Compare July 12, 2018 21:05
@jfirebaugh

Copy link
Copy Markdown
Contributor Author

A flaw with this approach is that there is no expiration mechanism for the ccache until it reaches its maximum size, so for long lived branches (e.g. master), it grows until it reaches 5 GB, at which point it takes a long time to archive and upload (looks like about 5 minutes or more).

The ideal behavior would be to read from the prior ccache while writing-through to a new ccache, such that the new cache contains hits from the prior ccache and the compiled results for misses. But ccache doesn't seem to offer such a mode. As far as I can tell, the best we can do is reduce the maximum size to a smaller amount -- something like the expected size of a completely fresh cache plus a small buffer -- and update that from time to time as needed.

@jfirebaugh
jfirebaugh force-pushed the ci-caching-improvements branch from 7eb30ac to 4cbf533 Compare July 13, 2018 00:08
@jfirebaugh
jfirebaugh merged commit 5623fa1 into master Jul 13, 2018
@jfirebaugh
jfirebaugh deleted the ci-caching-improvements branch July 13, 2018 18:50
@kkaefer

kkaefer commented Jul 16, 2018

Copy link
Copy Markdown
Member

We could use the combination of cache hit (direct), cache hit (preprocessed), and cache miss to count the total compiler invocations. I've found that the number of files in the ccache is an exact multiple of this number (e.g. 4 for compiling with Xcode). We could then use

ccache --max-files <number>
ccache --cleanup
ccache --max-files 0

to limit the ccache to only the files that have been used during the current build before packaging up the cache for uploading.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

build performance Speed, stability, CPU usage, memory usage, or power usage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants