lix-releng-staging/src/libstore
Sergei Trofimovich edfc5b2f12 ca-specific-schema.sql: add index on RealisationsRefs(referrer) and (outputPath)
For a typical desktop system (~2K packages) we can easily get 100K
entries in RealisationsRefs. Without indices query for RealisationsRefs
requires linear scan.

RealisationsRefs(referrer)
--------------------------

Inefficiency is seen as a 100% CPU load of nix-daemon for the following
scenario:

    $ nix edit -f . bash # add unused environment variable, like FOO="1"
    # populate RealisationsRefs, build fresh system
    $ nix build -f nixos system --arg config '{ contentAddressedByDefault = true; }'
    $ nix edit -f . bash # add unused environment variable, like FOO="2"
    $ time nix build -f nixos system --arg config '{ contentAddressedByDefault = true; }'

In this case `bash `will be rebuilt a few times and then rest of CPU
time is spent on scanning RealisationsRefs table (about 5 CPU-minutes
on my machine).

Before the change:

    $ time nix build -f nixos system ... # step 4 above
    real    34m3,613s
    user    0m5,232s
    sys     0m0,758s

Of all this time about 29.5 minutes are taken by nix-daemon's CPU time.

After the change:

    $ time nix build -f nixos system ... # step 4 above
    real    4m50,061s
    user    0m5,038s
    sys     0m0,677s

Of all this time about 1 minute is taken by nix-daemon's CPU time.
Most of the time is spent polling for non-existent realisations on
cache-nixos.org.

Realisations(outputPath)
------------------------

After running CA system for two weeks I got ~1M entries in Realisations
table. `nix-collect-garbage` became very slow (seemingly 100 path deletions
per second). It happens due to a slow cascading delete from Realisations
triggered by deletion from ValidPaths.

The fix is to add an index on primary key from ValidPaths(id) that
triggers cascading deletions.

Before the change:
    $ time nix-collect-garbage -d --max-freed 100G
    <interrupted before finish, took too long>
    real    23m32.411s
    user    17m49.679s
    sys     4m50.609s

Most of time was spent in re-scanning Realisations table on each path deletion.

After the change:
    $ time nix-collect-garbage -d --max-freed 100G

    real    8m43.226s
    user    6m16.317s
    sys     1m40.188s

Time is spent scanning sqlite indices and in kernel when unlinking directories.
2021-11-10 08:32:05 +00:00
..
build Merge pull request #5494 from tweag/balsoft/allow-references-in-addPath 2021-11-09 15:57:39 +01:00
builtins Improve error formatting 2021-01-21 11:02:09 +01:00
tests Add error reporting to machine spec paser 2021-10-17 12:45:56 +04:00
binary-cache-store.cc Merge pull request #5494 from tweag/balsoft/allow-references-in-addPath 2021-11-09 15:57:39 +01:00
binary-cache-store.hh addToStore, addToStoreFromDump: refactor: pass refs by const reference 2021-11-09 12:24:49 +03:00
builtins.hh buildenv: Eliminate global variables, other cleanup 2020-03-24 14:06:47 +01:00
ca-specific-schema.sql ca-specific-schema.sql: add index on RealisationsRefs(referrer) and (outputPath) 2021-11-10 08:32:05 +00:00
content-address.cc std::visit by reference 2021-09-30 21:35:09 +00:00
content-address.hh Add ContentAddressMethod and parse/render it 2020-09-17 20:21:04 +02:00
crypto.cc Make sodium a required dependency 2021-01-06 17:56:53 +01:00
crypto.hh crypto.cc: API cleanup and add generate() / to_string() methods 2021-01-06 17:04:46 +01:00
daemon.cc Merge pull request #5494 from tweag/balsoft/allow-references-in-addPath 2021-11-09 15:57:39 +01:00
daemon.hh Add missing #pragma once 2020-10-06 10:40:07 +02:00
derivations.cc Make experimental-features a proper type 2021-10-26 07:02:31 +02:00
derivations.hh Remove tabs 2021-09-10 11:00:50 +02:00
derived-path.cc Make experimental-features a proper type 2021-10-26 07:02:31 +02:00
derived-path.hh Enfore the use of properly built paths in libcmd 2021-05-17 08:45:08 +02:00
dummy-store.cc DummyStore: Remove redundant method 2021-07-22 09:59:51 +02:00
export-import.cc nix::worker_proto -> worker_proto 2020-09-30 00:41:18 +00:00
filetransfer.cc Restore parent mount namespace in restoreProcessContext 2021-10-15 16:25:49 +02:00
filetransfer.hh Add 'nix store prefetch-{file,tarball}' 2021-01-07 21:51:46 +01:00
fs-accessor.hh Use the fs accessor for readInvalidDerivation 2020-12-15 20:10:46 +01:00
gc.cc Use nix::connect() to connect to the garbage collector 2021-10-28 22:51:11 +02:00
globals.cc Make experimental-features a proper type 2021-10-26 07:02:31 +02:00
globals.hh Make experimental-features a proper type 2021-10-26 07:02:31 +02:00
http-binary-cache-store.cc Make setDefault() typed 2021-09-22 14:15:35 +02:00
legacy-ssh-store.cc addToStore, addToStoreFromDump: refactor: pass refs by const reference 2021-11-09 12:24:49 +03:00
local-binary-cache-store.cc Fix the double-slash in the realisations path 2021-05-04 10:35:34 +02:00
local-fs-store.cc Use the fs accessor for readInvalidDerivation 2020-12-15 20:10:46 +01:00
local-fs-store.hh UDSRemoteStore: Support the 'root' store parameter 2021-06-18 17:04:11 +02:00
local-store.cc ca-specific-schema.sql: add index on RealisationsRefs(referrer) and (outputPath) 2021-11-10 08:32:05 +00:00
local-store.hh Merge pull request #5494 from tweag/balsoft/allow-references-in-addPath 2021-11-09 15:57:39 +01:00
local.mk Merge pull request #5175 from Pamplemousse/make 2021-08-30 12:44:29 +02:00
lock.cc Trim lock.cc 2020-10-11 16:44:19 +00:00
lock.hh Trim lock.hh 2020-10-11 16:44:07 +00:00
machines.cc Style 2021-10-29 14:45:13 +02:00
machines.hh Make system-features a store setting 2020-08-12 18:13:00 +00:00
misc.cc Cleanup 2021-10-14 10:04:13 +02:00
names.cc string2Int(): Return std::optional 2021-01-08 12:22:21 +01:00
names.hh Don't include <regex> in header files 2020-09-21 18:22:45 +02:00
nar-accessor.cc Use the fs accessor for readInvalidDerivation 2020-12-15 20:10:46 +01:00
nar-accessor.hh NarAccessor: Run in constant memory 2020-07-13 17:30:42 +02:00
nar-info-disk-cache.cc Simplify the realisations disk cache 2021-05-10 17:47:14 +02:00
nar-info-disk-cache.hh Add a realisations disk cache 2021-05-06 17:38:20 +02:00
nar-info.cc string2Int(): Return std::optional 2021-01-08 12:22:21 +01:00
nar-info.hh Minimize the usage of Hash::dummy 2020-08-06 18:31:48 +00:00
nix-store.pc.in pkg-config files: Use c++17 2019-03-14 14:11:12 +01:00
optimise-store.cc UDSRemoteStore: Support the 'root' store parameter 2021-06-18 17:04:11 +02:00
parsed-derivations.cc Style 2021-07-12 17:32:23 +02:00
parsed-derivations.hh Style 2021-07-12 17:32:23 +02:00
path-info.cc Low-latency closure copy 2021-07-26 13:31:09 +02:00
path-info.hh Low-latency closure copy 2021-07-26 13:31:09 +02:00
path-with-outputs.cc std::visit by reference 2021-09-30 21:35:09 +00:00
path-with-outputs.hh buildable.{cc,hh} -> derived-path.{cc,hh} 2021-04-05 10:33:28 -04:00
path.cc Move StorePathWithOutputs into its own header/file 2021-04-05 08:31:37 -04:00
path.hh Move StorePathWithOutputs into its own header/file 2021-04-05 08:31:37 -04:00
pathlocks.cc Non-blocking garbage collector 2021-10-13 12:12:44 +02:00
pathlocks.hh Non-blocking garbage collector 2021-10-13 12:12:44 +02:00
profiles.cc Add 'nix profile wipe-history' command 2021-09-14 20:35:41 +02:00
profiles.hh Add "nix profile rollback" command 2021-09-14 19:32:33 +02:00
realisation.cc Be more lenient when realisations have a conflicting dependency set 2021-07-16 11:48:41 +02:00
realisation.hh buildPaths(): Add an evalStore argument 2021-07-22 09:59:51 +02:00
references.cc Add a test for RefScanSink and clean up the code 2021-10-04 14:29:42 +02:00
references.hh Add a test for RefScanSink and clean up the code 2021-10-04 14:29:42 +02:00
remote-fs-accessor.cc Use the fs accessor for readInvalidDerivation 2020-12-15 20:10:46 +01:00
remote-fs-accessor.hh Use the fs accessor for readInvalidDerivation 2020-12-15 20:10:46 +01:00
remote-store.cc Merge pull request #5494 from tweag/balsoft/allow-references-in-addPath 2021-11-09 15:57:39 +01:00
remote-store.hh Merge pull request #5494 from tweag/balsoft/allow-references-in-addPath 2021-11-09 15:57:39 +01:00
s3-binary-cache-store.cc Make setDefault() typed 2021-09-22 14:15:35 +02:00
s3-binary-cache-store.hh Overhaul store subclassing 2020-12-20 15:47:14 +00:00
s3.hh DataTransfer -> FileTransfer 2020-04-08 22:26:57 +02:00
sandbox-defaults.sb sandbox: allow Rosetta 2 on Darwin 2021-09-15 02:02:03 +01:00
sandbox-minimal.sb Always use the Darwin sandbox 2017-06-06 18:44:49 +02:00
sandbox-network.sb OS X sandbox: Improve builtin sandbox profile 2017-05-31 17:25:51 +02:00
schema.sql Mark content-addressed paths in the Nix database and in .narinfo 2016-08-10 18:05:35 +02:00
serve-protocol.hh nix-store --serve: pass on settings.keepFailed from SSH store 2021-08-31 13:11:46 +02:00
sqlite.cc libstore: Use unix-dotfile vfs if useSQLiteWAL is false 2021-11-03 14:19:11 +02:00
sqlite.hh Merge remote-tracking branch 'origin/master' into flakes 2020-06-17 10:26:52 +02:00
ssh-store.cc Shut down write side before draining the read side 2021-09-23 18:01:04 +02:00
ssh.cc restoreSignals() + restoreAffinity() -> restoreProcessContext() 2021-04-07 13:10:02 +02:00
ssh.hh distributed builds: load remote builder host key from the machines file 2021-02-25 09:17:34 -05:00
store-api.cc Merge pull request #5494 from tweag/balsoft/allow-references-in-addPath 2021-11-09 15:57:39 +01:00
store-api.hh Merge pull request #5494 from tweag/balsoft/allow-references-in-addPath 2021-11-09 15:57:39 +01:00
uds-remote-store.cc Non-blocking garbage collector 2021-10-13 12:12:44 +02:00
uds-remote-store.hh Shut down write side before draining the read side 2021-09-23 18:01:04 +02:00
worker-protocol.hh Low-latency closure copy 2021-07-26 13:31:09 +02:00