1 //=- CachePruning.h - Helper to manage the pruning of a cache dir -*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements pruning of a directory intended for cache storage, using
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_CACHE_PRUNING_H
16 #define LLVM_SUPPORT_CACHE_PRUNING_H
18 #include "llvm/ADT/StringRef.h"
23 template <typename T> class Expected;
25 /// Policy for the pruneCache() function. A default constructed
26 /// CachePruningPolicy provides a reasonable default policy.
27 struct CachePruningPolicy {
28 /// The pruning interval. This is intended to be used to avoid scanning the
29 /// directory too often. It does not impact the decision of which file to
30 /// prune. A value of 0 forces the scan to occur. A value of None disables
32 llvm::Optional<std::chrono::seconds> Interval = std::chrono::seconds(1200);
34 /// The expiration for a file. When a file hasn't been accessed for Expiration
35 /// seconds, it is removed from the cache. A value of 0 disables the
36 /// expiration-based pruning.
37 std::chrono::seconds Expiration = std::chrono::hours(7 * 24); // 1w
39 /// The maximum size for the cache directory, in terms of percentage of the
40 /// available space on the the disk. Set to 100 to indicate no limit, 50 to
41 /// indicate that the cache size will not be left over half the available disk
42 /// space. A value over 100 will be reduced to 100. A value of 0 disables the
43 /// percentage size-based pruning.
44 unsigned MaxSizePercentageOfAvailableSpace = 75;
46 /// The maximum size for the cache directory in bytes. A value over the amount
47 /// of available space on the disk will be reduced to the amount of available
48 /// space. A value of 0 disables the absolute size-based pruning.
49 uint64_t MaxSizeBytes = 0;
51 /// The maximum number of files in the cache directory. A value of 0 disables
52 /// the number of files based pruning.
54 /// This defaults to 1000000 because with that many files there are
55 /// diminishing returns on the effectiveness of the cache, and some file
56 /// systems have a limit on how many files can be contained in a directory
57 /// (notably ext4, which is limited to around 6000000 files).
58 uint64_t MaxSizeFiles = 1000000;
61 /// Parse the given string as a cache pruning policy. Defaults are taken from a
62 /// default constructed CachePruningPolicy object.
63 /// For example: "prune_interval=30s:prune_after=24h:cache_size=50%"
64 /// which means a pruning interval of 30 seconds, expiration time of 24 hours
65 /// and maximum cache size of 50% of available disk space.
66 Expected<CachePruningPolicy> parseCachePruningPolicy(StringRef PolicyStr);
68 /// Peform pruning using the supplied policy, returns true if pruning
69 /// occured, i.e. if Policy.Interval was expired.
71 /// As a safeguard against data loss if the user specifies the wrong directory
72 /// as their cache directory, this function will ignore files not matching the
73 /// pattern "llvmcache-*".
74 bool pruneCache(StringRef Path, CachePruningPolicy Policy);