1bf526b62SAlex Crichton# Cache Configuration of `wasmtime` 2bf526b62SAlex Crichton 359258730SPeter HueneThe configuration file uses the [toml] format. 4cf0af201SAlex CrichtonYou can create a configuration file at the default location with: 592cfda1bSVictor Adossi```console 692cfda1bSVictor Adossiwasmtime config new 7cf0af201SAlex Crichton``` 8cf0af201SAlex CrichtonIt will print the location regardless of the success. 9cf0af201SAlex CrichtonPlease refer to the `--help` message for using a custom location. 10cf0af201SAlex Crichton 11*0ffa7455SBen BrandtAll settings are **optional**. 12cf0af201SAlex CrichtonIf the setting is not specified, the **default** value is used. 13cf0af201SAlex Crichton***Thus, if you don't know what values to use, don't specify them.*** 14cf0af201SAlex CrichtonThe default values might be tuned in the future. 15cf0af201SAlex Crichton 16cf0af201SAlex CrichtonWasmtime assumes all the options are in the `cache` section. 17cf0af201SAlex Crichton 18cf0af201SAlex CrichtonExample config: 19cf0af201SAlex Crichton```toml 20cf0af201SAlex Crichton[cache] 21cf0af201SAlex Crichtondirectory = "/nfs-share/wasmtime-cache/" 22cf0af201SAlex Crichtoncleanup-interval = "30m" 23cf0af201SAlex Crichtonfiles-total-size-soft-limit = "1Gi" 24cf0af201SAlex Crichton``` 25cf0af201SAlex Crichton 26cf0af201SAlex CrichtonPlease refer to the [cache system] section to learn how it works. 27cf0af201SAlex Crichton 28cf0af201SAlex CrichtonIf you think some default value should be tuned, some new settings 29cf0af201SAlex Crichtonshould be introduced or some behavior should be changed, you are 30cf0af201SAlex Crichtonwelcome to discuss it and contribute to [the Wasmtime repository]. 31cf0af201SAlex Crichton 32c78196bdSDan Gohman[the Wasmtime repository]: https://github.com/bytecodealliance/wasmtime 33cf0af201SAlex Crichton 34cf0af201SAlex CrichtonSetting `directory` 35cf0af201SAlex Crichton----------------- 36cf0af201SAlex Crichton- **type**: string (path) 37cf0af201SAlex Crichton- **default**: look up `cache_dir` in [directories] crate 38cf0af201SAlex Crichton 39cf0af201SAlex CrichtonSpecifies where the cache directory is. Must be an absolute path. 40cf0af201SAlex Crichton 41cf0af201SAlex Crichton[`directory`]: #setting-directory 42cf0af201SAlex Crichton 43cf0af201SAlex CrichtonSetting `worker-event-queue-size` 44cf0af201SAlex Crichton----------------- 45cf0af201SAlex Crichton- **type**: string (SI prefix) 46cf0af201SAlex Crichton- **format**: `"{integer}(K | M | G | T | P)?"` 47cf0af201SAlex Crichton- **default**: `"16"` 48cf0af201SAlex Crichton 49cf0af201SAlex CrichtonSize of [cache worker] event queue. 50cf0af201SAlex CrichtonIf the queue is full, incoming cache usage events will be dropped. 51cf0af201SAlex Crichton 52cf0af201SAlex Crichton[`worker-event-queue-size`]: #setting-worker-event-queue-size 53cf0af201SAlex Crichton 54cf0af201SAlex CrichtonSetting `baseline-compression-level` 55cf0af201SAlex Crichton------------------ 56cf0af201SAlex Crichton- **type**: integer 57cf0af201SAlex Crichton- **default**: `3`, the default zstd compression level 58cf0af201SAlex Crichton 59cf0af201SAlex CrichtonCompression level used when a new cache file is being written by the [cache system]. 60cf0af201SAlex CrichtonWasmtime uses [zstd] compression. 61cf0af201SAlex Crichton 62cf0af201SAlex Crichton[`baseline-compression-level`]: #setting-baseline-compression-level 63cf0af201SAlex Crichton 64cf0af201SAlex CrichtonSetting `optimized-compression-level` 65cf0af201SAlex Crichton------------------ 66cf0af201SAlex Crichton- **type**: integer 67cf0af201SAlex Crichton- **default**: `20` 68cf0af201SAlex Crichton 69cf0af201SAlex CrichtonCompression level used when the [cache worker] decides to recompress a cache file. 70cf0af201SAlex CrichtonWasmtime uses [zstd] compression. 71cf0af201SAlex Crichton 72cf0af201SAlex Crichton[`optimized-compression-level`]: #setting-optimized-compression-level 73cf0af201SAlex Crichton 74cf0af201SAlex CrichtonSetting `optimized-compression-usage-counter-threshold` 75cf0af201SAlex Crichton------------------ 76cf0af201SAlex Crichton- **type**: string (SI prefix) 77cf0af201SAlex Crichton- **format**: `"{integer}(K | M | G | T | P)?"` 78cf0af201SAlex Crichton- **default**: `"256"` 79cf0af201SAlex Crichton 80cf0af201SAlex CrichtonOne of the conditions for the [cache worker] to recompress a cache file 81cf0af201SAlex Crichtonis to have usage count of the file exceeding this threshold. 82cf0af201SAlex Crichton 83cf0af201SAlex Crichton[`optimized-compression-usage-counter-threshold`]: #setting-optimized-compression-usage-counter-threshold 84cf0af201SAlex Crichton 85cf0af201SAlex CrichtonSetting `cleanup-interval` 86cf0af201SAlex Crichton------------------ 87cf0af201SAlex Crichton- **type**: string (duration) 88cf0af201SAlex Crichton- **format**: `"{integer}(s | m | h | d)"` 89cf0af201SAlex Crichton- **default**: `"1h"` 90cf0af201SAlex Crichton 91cf0af201SAlex CrichtonWhen the [cache worker] is notified about a cache file being updated by the [cache system] 92cf0af201SAlex Crichtonand this interval has already passed since last cleaning up, 93cf0af201SAlex Crichtonthe worker will attempt a new cleanup. 94cf0af201SAlex Crichton 95cf0af201SAlex CrichtonPlease also refer to [`allowed-clock-drift-for-files-from-future`]. 96cf0af201SAlex Crichton 97cf0af201SAlex Crichton[`cleanup-interval`]: #setting-cleanup-interval 98cf0af201SAlex Crichton 99cf0af201SAlex CrichtonSetting `optimizing-compression-task-timeout` 100cf0af201SAlex Crichton------------------ 101cf0af201SAlex Crichton- **type**: string (duration) 102cf0af201SAlex Crichton- **format**: `"{integer}(s | m | h | d)"` 103cf0af201SAlex Crichton- **default**: `"30m"` 104cf0af201SAlex Crichton 105cf0af201SAlex CrichtonWhen the [cache worker] decides to recompress a cache file, it makes sure that 106cf0af201SAlex Crichtonno other worker has started the task for this file within the last 107cf0af201SAlex Crichton[`optimizing-compression-task-timeout`] interval. 108cf0af201SAlex CrichtonIf some worker has started working on it, other workers are skipping this task. 109cf0af201SAlex Crichton 110cf0af201SAlex CrichtonPlease also refer to the [`allowed-clock-drift-for-files-from-future`] section. 111cf0af201SAlex Crichton 112cf0af201SAlex Crichton[`optimizing-compression-task-timeout`]: #setting-optimizing-compression-task-timeout 113cf0af201SAlex Crichton 114cf0af201SAlex CrichtonSetting `allowed-clock-drift-for-files-from-future` 115cf0af201SAlex Crichton------------------ 116cf0af201SAlex Crichton- **type**: string (duration) 117cf0af201SAlex Crichton- **format**: `"{integer}(s | m | h | d)"` 118cf0af201SAlex Crichton- **default**: `"1d"` 119cf0af201SAlex Crichton 120cf0af201SAlex Crichton### Locks 121cf0af201SAlex CrichtonWhen the [cache worker] attempts acquiring a lock for some task, 122cf0af201SAlex Crichtonit checks if some other worker has already acquired such a lock. 123cf0af201SAlex CrichtonTo be fault tolerant and eventually execute every task, 124cf0af201SAlex Crichtonthe locks expire after some interval. 125cf0af201SAlex CrichtonHowever, because of clock drifts and different timezones, 126cf0af201SAlex Crichtonit would happen that some lock was created in the future. 127cf0af201SAlex CrichtonThis setting defines a tolerance limit for these locks. 128cf0af201SAlex CrichtonIf the time has been changed in the system (i.e. two years backwards), 129cf0af201SAlex Crichtonthe [cache system] should still work properly. 130cf0af201SAlex CrichtonThus, these locks will be treated as expired 131cf0af201SAlex Crichton(assuming the tolerance is not too big). 132cf0af201SAlex Crichton 133cf0af201SAlex Crichton### Cache files 134cf0af201SAlex CrichtonSimilarly to the locks, the cache files or their metadata might 135cf0af201SAlex Crichtonhave modification time in distant future. 136cf0af201SAlex CrichtonThe cache system tries to keep these files as long as possible. 137cf0af201SAlex CrichtonIf the limits are not reached, the cache files will not be deleted. 138cf0af201SAlex CrichtonOtherwise, they will be treated as the oldest files, so they might survive. 139cf0af201SAlex CrichtonIf the user actually uses the cache file, the modification time will be updated. 140cf0af201SAlex Crichton 141cf0af201SAlex Crichton[`allowed-clock-drift-for-files-from-future`]: #setting-allowed-clock-drift-for-files-from-future 142cf0af201SAlex Crichton 143cf0af201SAlex CrichtonSetting `file-count-soft-limit` 144cf0af201SAlex Crichton------------------ 145cf0af201SAlex Crichton- **type**: string (SI prefix) 146cf0af201SAlex Crichton- **format**: `"{integer}(K | M | G | T | P)?"` 147cf0af201SAlex Crichton- **default**: `"65536"` 148cf0af201SAlex Crichton 149cf0af201SAlex CrichtonSoft limit for the file count in the cache directory. 150cf0af201SAlex Crichton 151cf0af201SAlex CrichtonThis doesn't include files with metadata. 152cf0af201SAlex CrichtonTo learn more, please refer to the [cache system] section. 153cf0af201SAlex Crichton 154cf0af201SAlex Crichton[`file-count-soft-limit`]: #setting-file-count-soft-limit 155cf0af201SAlex Crichton 156cf0af201SAlex CrichtonSetting `files-total-size-soft-limit` 157cf0af201SAlex Crichton------------------ 158cf0af201SAlex Crichton- **type**: string (disk space) 159cf0af201SAlex Crichton- **format**: `"{integer}(K | Ki | M | Mi | G | Gi | T | Ti | P | Pi)?"` 160cf0af201SAlex Crichton- **default**: `"512Mi"` 161cf0af201SAlex Crichton 162cf0af201SAlex CrichtonSoft limit for the total size* of files in the cache directory. 163cf0af201SAlex Crichton 164cf0af201SAlex CrichtonThis doesn't include files with metadata. 165cf0af201SAlex CrichtonTo learn more, please refer to the [cache system] section. 166cf0af201SAlex Crichton 167cf0af201SAlex Crichton*this is the file size, not the space physically occupied on the disk. 168cf0af201SAlex Crichton 169cf0af201SAlex Crichton[`files-total-size-soft-limit`]: #setting-files-total-size-soft-limit 170cf0af201SAlex Crichton 171cf0af201SAlex CrichtonSetting `file-count-limit-percent-if-deleting` 172cf0af201SAlex Crichton------------------ 173cf0af201SAlex Crichton- **type**: string (percent) 174cf0af201SAlex Crichton- **format**: `"{integer}%"` 175cf0af201SAlex Crichton- **default**: `"70%"` 176cf0af201SAlex Crichton 177cf0af201SAlex CrichtonIf [`file-count-soft-limit`] is exceeded and the [cache worker] performs the cleanup task, 178cf0af201SAlex Crichtonthen the worker will delete some cache files, so after the task, 179cf0af201SAlex Crichtonthe file count should not exceed 180cf0af201SAlex Crichton[`file-count-soft-limit`] * [`file-count-limit-percent-if-deleting`]. 181cf0af201SAlex Crichton 182cf0af201SAlex CrichtonThis doesn't include files with metadata. 183cf0af201SAlex CrichtonTo learn more, please refer to the [cache system] section. 184cf0af201SAlex Crichton 185cf0af201SAlex Crichton[`file-count-limit-percent-if-deleting`]: #setting-file-count-limit-percent-if-deleting 186cf0af201SAlex Crichton 187cf0af201SAlex CrichtonSetting `files-total-size-limit-percent-if-deleting` 188cf0af201SAlex Crichton------------------ 189cf0af201SAlex Crichton- **type**: string (percent) 190cf0af201SAlex Crichton- **format**: `"{integer}%"` 191cf0af201SAlex Crichton- **default**: `"70%"` 192cf0af201SAlex Crichton 193cf0af201SAlex CrichtonIf [`files-total-size-soft-limit`] is exceeded and [cache worker] performs the cleanup task, 194cf0af201SAlex Crichtonthen the worker will delete some cache files, so after the task, 195cf0af201SAlex Crichtonthe files total size should not exceed 196cf0af201SAlex Crichton[`files-total-size-soft-limit`] * [`files-total-size-limit-percent-if-deleting`]. 197cf0af201SAlex Crichton 198cf0af201SAlex CrichtonThis doesn't include files with metadata. 199cf0af201SAlex CrichtonTo learn more, please refer to the [cache system] section. 200cf0af201SAlex Crichton 201cf0af201SAlex Crichton[`files-total-size-limit-percent-if-deleting`]: #setting-files-total-size-limit-percent-if-deleting 202cf0af201SAlex Crichton 203cf0af201SAlex Crichton[toml]: https://github.com/toml-lang/toml 204cf0af201SAlex Crichton[directories]: https://crates.io/crates/directories 205cf0af201SAlex Crichton[cache system]: #how-does-the-cache-work 206cf0af201SAlex Crichton[cache worker]: #how-does-the-cache-work 207cf0af201SAlex Crichton[zstd]: https://facebook.github.io/zstd/ 208cf0af201SAlex Crichton[Least Recently Used (LRU)]: https://en.wikipedia.org/wiki/Cache_replacement_policies#Least_recently_used_(LRU) 209cf0af201SAlex Crichton 210cf0af201SAlex CrichtonHow does the cache work? 211cf0af201SAlex Crichton======================== 212cf0af201SAlex Crichton 213cf0af201SAlex Crichton**This is an implementation detail and might change in the future.** 214cf0af201SAlex CrichtonInformation provided here is meant to help understanding the big picture 215cf0af201SAlex Crichtonand configuring the cache. 216cf0af201SAlex Crichton 217cf0af201SAlex CrichtonThere are two main components - the *cache system* and the *cache worker*. 218cf0af201SAlex Crichton 219cf0af201SAlex CrichtonCache system 220cf0af201SAlex Crichton------------ 221cf0af201SAlex Crichton 222cf0af201SAlex CrichtonHandles GET and UPDATE cache requests. 223cf0af201SAlex Crichton- **GET request** - simply loads the cache from disk if it is there. 224cf0af201SAlex Crichton- **UPDATE request** - compresses received data with [zstd] and [`baseline-compression-level`], then writes the data to the disk. 225cf0af201SAlex Crichton 226cf0af201SAlex CrichtonIn case of successful handling of a request, it notifies the *cache worker* about this 227cf0af201SAlex Crichtonevent using the queue. 228cf0af201SAlex CrichtonThe queue has a limited size of [`worker-event-queue-size`]. If it is full, it will drop 229cf0af201SAlex Crichtonnew events until the *cache worker* pops some event from the queue. 230cf0af201SAlex Crichton 231cf0af201SAlex CrichtonCache worker 232cf0af201SAlex Crichton------------ 233cf0af201SAlex Crichton 234cf0af201SAlex CrichtonThe cache worker runs in a single thread with lower priority and pops events from the queue 235cf0af201SAlex Crichtonin a loop handling them one by one. 236cf0af201SAlex Crichton 237cf0af201SAlex Crichton### On GET request 238cf0af201SAlex Crichton1. Read the statistics file for the cache file, 239cf0af201SAlex Crichton increase the usage counter and write it back to the disk. 240cf0af201SAlex Crichton2. Attempt recompressing the cache file if all of the following conditions are met: 241cf0af201SAlex Crichton - usage counter exceeds [`optimized-compression-usage-counter-threshold`], 242cf0af201SAlex Crichton - the file is compressed with compression level lower than [`optimized-compression-level`], 243cf0af201SAlex Crichton - no other worker has started working on this particular task within the last 244cf0af201SAlex Crichton [`optimizing-compression-task-timeout`] interval. 245cf0af201SAlex Crichton 246cf0af201SAlex Crichton When recompressing, [`optimized-compression-level`] is used as a compression level. 247cf0af201SAlex Crichton 248cf0af201SAlex Crichton### On UPDATE request 249cf0af201SAlex Crichton1. Write a fresh statistics file for the cache file. 250cf0af201SAlex Crichton2. Clean up the cache if no worker has attempted to do this within the last [`cleanup-interval`]. 251cf0af201SAlex Crichton During this task: 252cf0af201SAlex Crichton - all unrecognized files and expired task locks in cache directory will be deleted 253cf0af201SAlex Crichton - if [`file-count-soft-limit`] or [`files-total-size-soft-limit`] is exceeded, 254cf0af201SAlex Crichton then recognized files will be deleted according to 255cf0af201SAlex Crichton [`file-count-limit-percent-if-deleting`] and [`files-total-size-limit-percent-if-deleting`]. 256cf0af201SAlex Crichton Wasmtime uses [Least Recently Used (LRU)] cache replacement policy and requires that 257cf0af201SAlex Crichton the filesystem maintains proper mtime (modification time) of the files. 258cf0af201SAlex Crichton Files with future mtimes are treated specially - more details 259cf0af201SAlex Crichton in [`allowed-clock-drift-for-files-from-future`]. 260cf0af201SAlex Crichton 261cf0af201SAlex Crichton### Metadata files 262cf0af201SAlex Crichton- every cached WebAssembly module has its own statistics file 263cf0af201SAlex Crichton- every lock is a file 264