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