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