Open Bug 1790509 Opened 4 years ago Updated 5 months ago

Consider hosting a clangd remote index of mozilla-central

Categories

(Developer Infrastructure :: Developer Environment Integration, enhancement)

x86_64
Linux
enhancement

Tracking

(Not tracked)

People

(Reporter: botond, Unassigned, NeedInfo)

References

Details

Attachments

(1 external file)

clangd has a remote index feature which allows users to consume index information from a remotely hosted pre-built index.

The feature has some limitations (the accuracy of the index information degrades as the delta between your local version and the version that was indexed grows), but using it does allow developers to avoid running the expensive and long-running indexing task locally, which is a tradeoff some developers might prefer.

Some large C++ open source projects, such as LLVM and Chromium, have publically hosted clangd remote indexes. We could consider hosting such a remote index for mozilla-central.

To be useful, the remote index needs to be rebuilt periodically (e.g. nightly), and we need to have one for each of our major platforms (Windows, Mac, Linux, Android).

So I was looking into this, and it seems tricky, but doable. Above is my WIP. Without the ENABLE_REMOTE thing, it gets through and builds clangd and clangd-indexer.

However to build with remote support you need gRPC and protoc in the PATH, which is unfortunate.

An alternative would be to use the official releases from https://github.com/clangd/clangd/releases. Not sure which approach is preferable.

Once we have a clangd-indexer + clangd + clangd-index-server combo in taskcluster I can hook searchfox to serve it.

Mike, Andi, which approach would be better? Would any of you be able to help with getting grpc / protoc in the clang toolchain builds? I'm not sure what the best approach for that is.

Flags: needinfo?(mh+mozilla)
Flags: needinfo?(bpostelnicu)

Do we know what the general working set of the clangd server is memory wise for m-c? The searchfox configs most recently were balanced so there should generally be enough RAM to keep the livegrep codesearch indices cached in RAM (and which are proactively vmtouched into memory) across all configs as well as the crossref data for m-c on config1. Without the caching the codesearch latency can get bad and result in timeouts, but it's certainly easy enough to upgrade the VM to have more RAM or switch to giving it a local SSD, etc.

As per Andi's offline feedback, I tried to make it work on our clang-tidy task.

https://treeherder.mozilla.org/jobs?repo=try&revision=bbb21ac55741bc5bb1fc35deb6db73d3001bd93f (the current version of the rev) is the closest I've got.

It fails with some sort of generated include error, but also:

[task 2023-04-25T19:33:04.630Z] gRPC and Protobuf will be linked dynamically. If you want static linking, build gRPC from sources with -DBUILD_SHARED_LIBS=Off.

Which is rather unfortunate because the debian packages do contain the relevant static libraries...

(In reply to Emilio Cobos Álvarez (:emilio) from comment #4)

As per Andi's offline feedback, I tried to make it work on our clang-tidy task.

https://treeherder.mozilla.org/jobs?repo=try&revision=bbb21ac55741bc5bb1fc35deb6db73d3001bd93f (the current version of the rev) is the closest I've got.

It fails with some sort of generated include error, but also:

[task 2023-04-25T19:33:04.630Z] gRPC and Protobuf will be linked dynamically. If you want static linking, build gRPC from sources with -DBUILD_SHARED_LIBS=Off.

Which is rather unfortunate because the debian packages do contain the relevant static libraries...

Regarding the dynamic link what if we ship the dynamic libraries in the package?
I think in order to solve the build issues why don't you patch https://github.com/llvm/llvm-project/blob/release/16.x/clang-tools-extra/clangd/index/remote/CMakeLists.txt#L3 with something like find_package(Protobuf REQUIRED).

Flags: needinfo?(bpostelnicu)
Flags: needinfo?(emilio)

It seems the issue with protobuf is that it's an old version. There's a newer version in bullseye-backports, do you know if it's fine to just add-apt-repository etc in the Dockerfile?

Flags: needinfo?(emilio) → needinfo?(bpostelnicu)

(In reply to Emilio Cobos Álvarez (:emilio) from comment #6)

It seems the issue with protobuf is that it's an old version. There's a newer version in bullseye-backports, do you know if it's fine to just add-apt-repository etc in the Dockerfile?

of course!

Flags: needinfo?(bpostelnicu)

You don't need to. backports are already there, you can apt-get install package/bullseye-backports.

Flags: needinfo?(mh+mozilla)

Oh wait, these builds are using a sysroot... based on a much older Debian (because we want the resulting toolchains to run on a broad spectrum of distros). Also, what about non-linux toolchains?

For non-linux we'd only need the clangd client, not the indexer + server. But yeah presumably we'd also need to build with -DCLANGD_ENABLE_REMOTE=On...

(In reply to Emilio Cobos Álvarez (:emilio) from comment #10)

For non-linux we'd only need the clangd client, not the indexer + server. But yeah presumably we'd also need to build with -DCLANGD_ENABLE_REMOTE=On...

Is the idea to serve Mac and Windows indexes created using cross-compile builds on a Linux server?

As for the client, do we need to build our own at all, as opposed to directing users to the official builds which I believe are built with -DCLANGD_ENABLE_REMOTE=On?

(In reply to Botond Ballo [:botond] from comment #11)

(In reply to Emilio Cobos Álvarez (:emilio) from comment #10)

For non-linux we'd only need the clangd client, not the indexer + server. But yeah presumably we'd also need to build with -DCLANGD_ENABLE_REMOTE=On...

Is the idea to serve Mac and Windows indexes created using cross-compile builds on a Linux server?

Yes (that's how Searchfox indexes stuff already effectively fwiw).

As for the client, do we need to build our own at all, as opposed to directing users to the official builds which I believe are built with -DCLANGD_ENABLE_REMOTE=On?

That alternative is mentioned in comment 2... I wonder if the complexities on building our own would make that more palatable.

But how will the index be connected with searchfox? Do we track this work anywhere?
We will still have to do a full build prior to have the indexer started because there are translation units that are generated and referenced by the compile_commands.json during the build time. I think there are also some header files in the rlbox component.

Flags: needinfo?(emilio)

(In reply to Andi [:andi] from comment #13)

But how will the index be connected with searchfox?

So we already have searchfox builds running on taskcluster that run e.g. rust-analyzer to index the repo, see this for example.

We could generate the index from any other job I guess, and serve it from any other place, too, but given Searchfox has already the infrastructure to regularly run an index of the codebase on taskcluster, and then consume its artifacts and serve stuff, it seemed like the most obvious place to put this rather than reinventing a new service or what not. Happy to do it some other way tho!

Do we track this work anywhere?

This bug? Happy to file a dependent one or something.

We will still have to do a full build prior to have the indexer started because there are translation units that are generated and referenced by the compile_commands.json during the build time. I think there are also some header files in the rlbox component.

Searchfox tasks do full builds fwiw, so that shouldn't be an issue? But let me know if I'm missing something :)

Flags: needinfo?(emilio) → needinfo?(bpostelnicu)

(In reply to Emilio Cobos Álvarez (:emilio) from comment #14)

(In reply to Andi [:andi] from comment #13)

But how will the index be connected with searchfox?

So we already have searchfox builds running on taskcluster that run e.g. rust-analyzer to index the repo, see this for example.

We could generate the index from any other job I guess, and serve it from any other place, too, but given Searchfox has already the infrastructure to regularly run an index of the codebase on taskcluster, and then consume its artifacts and serve stuff, it seemed like the most obvious place to put this rather than reinventing a new service or what not. Happy to do it some other way tho!

Do we track this work anywhere?

This bug? Happy to file a dependent one or something.

Great!

We will still have to do a full build prior to have the indexer started because there are translation units that are generated and referenced by the compile_commands.json during the build time. I think there are also some header files in the rlbox component.

Searchfox tasks do full builds fwiw, so that shouldn't be an issue? But let me know if I'm missing something :)
This is perfect then!

Flags: needinfo?(bpostelnicu)

While annoyed at how often clangd indexing runs cause my build machine to hang/OOM, I recently stumbled across the existence of these remote indexer servers for other projects.

It'd be awesome if we could e.g. point clangd at linux64.clangd-index.firefox-main.searchfox.org:5900 (similar to the chromium situation), and have mostly up-to-date index information about the tree integrated into clangd.

I'm guessing there hasn't been a ton of investigation into this since 2023. Leaving a ni? for :emilio in case they have any more context as to where this was left-off at.

Flags: needinfo?(emilio)

Yeah, that's correct. I recently tweaked the code that does the rust scip indexing which would be similar in bug 2012639... Seems https://phabricator.services.mozilla.com/D176408 would still be needed and we'd just call into clangd-indexer and upload the index there.

Hopefully the dependency issues I was missing aren't an issue anymore tho, since we updated our infra's ubuntu version and so on?

But yeah, +1, I also hit OOMs from time to time due to clangd...

Flags: needinfo?(emilio)

(In reply to Emilio Cobos Álvarez [:emilio] from comment #14)

(In reply to Andi [:andi] from comment #13)

But how will the index be connected with searchfox?

So we already have searchfox builds running on taskcluster that run e.g. rust-analyzer to index the repo, see this for example.

We could generate the index from any other job I guess, and serve it from any other place, too, but given Searchfox has already the infrastructure to regularly run an index of the codebase on taskcluster, and then consume its artifacts and serve stuff, it seemed like the most obvious place to put this rather than reinventing a new service or what not. Happy to do it some other way tho!

Note that (a few years later now) the searchfox jobs now take 2+ hours (seems worse on mozilla-beta?) and while bug 2013415 may help with that, it seems likely those taskcluster jobs are probably not the optimal place to add more work since the spot jobs are regularly getting killed. Somewhat similarly, load on the searchfox web-servers is increasing from use of searchfox-cli from Claude and other AI tools and the load now has a different shape as searchfox-cli can issue graph queries which weren't a thing in 2023. Which is to say, I think the clangd runtime would want to run on separate VMs from the current web-servers as well to avoid them interfering with each other. (Also, in theory ideally searchfox would migrate to GCP so adding more stuff on AWS instead of GCP might not be desirable.)

(In reply to Nika Layzell [:nika] (ni? for response) from comment #16)

While annoyed at how often clangd indexing runs cause my build machine to hang/OOM

A mitigation for this is to run clangd with -j=<N> in its arguments (configured in your editor's settings, e.g. "clangd.arguments" in vscode), where <N> is some smaller number that the default (which I think is the number of cores). The downside of course is that the indexing will take longer to complete.

We already use half of the cores by default here FWIW.

(In reply to Botond Ballo [:botond] from comment #19)

A mitigation for this is to run clangd with -j=<N> in its arguments (configured in your editor's settings, e.g. "clangd.arguments" in vscode), where <N> is some smaller number that the default (which I think is the number of cores). The downside of course is that the indexing will take longer to complete.

Yup, I already do this with a heavily throttled N nowadays. My thoughts are mostly that it seems like the sort of thing which could be shared fairly easily by hosting this on common infrastructure, saving the need to run background indexing at all :-).

FWIW another minor annoyance with the current indexing is actually that clangd doesn't have any niceness specified, unlike the actual ./mach build builds (which specify a niceness of 19: https://searchfox.org/firefox-main/source/python/mozbuild/mozbuild/build_commands.py#157,260-266, https://searchfox.org/firefox-main/rev/33fd6bd39c625067a29f153adce6a4646e45750f/python/mozbuild/mozbuild/build_commands.py#33,47), but it's not a big deal if you throttle the indexing a ton (or just turn it completely off).

(In reply to Andrew Sutherland [:asuth] (he/him) from comment #18)

Note that (a few years later now) the searchfox jobs now take 2+ hours (seems worse on mozilla-beta?) and while bug 2013415 may help with that, it seems likely those taskcluster jobs are probably not the optimal place to add more work since the spot jobs are regularly getting killed. Somewhat similarly, load on the searchfox web-servers is increasing from use of searchfox-cli from Claude and other AI tools and the load now has a different shape as searchfox-cli can issue graph queries which weren't a thing in 2023. Which is to say, I think the clangd runtime would want to run on separate VMs from the current web-servers as well to avoid them interfering with each other. (Also, in theory ideally searchfox would migrate to GCP so adding more stuff on AWS instead of GCP might not be desirable.)

I do think that having them be on separate VM/instances makes sense - no reason to shove unrelated tasks together especially when they won't even be served from the same subdomain. I think that it should still be possible to serve it under the searchfox.com domain though if we wanted to keep them conceptually connected with one-another.

I don't suppose searchfox would benefit at all from being able to injest the clangd index as part of it's indexing? I'm guessing it's a bit redundant with the work searchfox is already doing under the hood :-/

(In reply to Nika Layzell [:nika] (ni? for response) from comment #22)

I don't suppose searchfox would benefit at all from being able to injest the clangd index as part of it's indexing? I'm guessing it's a bit redundant with the work searchfox is already doing under the hood :-/

I don't think so; clangd's index format is pretty narrowly geared towards clangd's interactive editing use cases.

(There is an interesting discussion in this issue where someone at Sourcegraph, the folks behinds SCIP, evaluated various existing C++ indexers, including clangd's and Searchfox's, for being the basis of a C++ SCIP indexer, but ultimately decided to roll their own*.)

(*Still based on the clang frontend, which is where the most valuable code reuse is. The various C++ indexers out there are all pretty lightweight wrappers around the clang frontend.)

My patch doesn't quite work, I suspect a too old protobuf version: https://treeherder.mozilla.org/jobs?repo=try&revision=ff73b54fe9ce126145d2e72ef8bda864b983491d

Serge said he could take a look, which would be awesome :)

Flags: needinfo?(sguelton)
No longer blocks: 2038826
See Also: → 2038826
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: