1 //=- CachePruning.h - Helper to manage the pruning of a cache dir -*- C++ -*-=//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements pruning of a directory intended for cache storage, using
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SUPPORT_CACHE_PRUNING_H
15 #define LLVM_SUPPORT_CACHE_PRUNING_H
17 #include "llvm/ADT/StringRef.h"
22 template <typename T
> class Expected
;
24 /// Policy for the pruneCache() function. A default constructed
25 /// CachePruningPolicy provides a reasonable default policy.
26 struct CachePruningPolicy
{
27 /// The pruning interval. This is intended to be used to avoid scanning the
28 /// directory too often. It does not impact the decision of which file to
29 /// prune. A value of 0 forces the scan to occur. A value of None disables
31 llvm::Optional
<std::chrono::seconds
> Interval
= std::chrono::seconds(1200);
33 /// The expiration for a file. When a file hasn't been accessed for Expiration
34 /// seconds, it is removed from the cache. A value of 0 disables the
35 /// expiration-based pruning.
36 std::chrono::seconds Expiration
= std::chrono::hours(7 * 24); // 1w
38 /// The maximum size for the cache directory, in terms of percentage of the
39 /// available space on the disk. Set to 100 to indicate no limit, 50 to
40 /// indicate that the cache size will not be left over half the available disk
41 /// space. A value over 100 will be reduced to 100. A value of 0 disables the
42 /// percentage size-based pruning.
43 unsigned MaxSizePercentageOfAvailableSpace
= 75;
45 /// The maximum size for the cache directory in bytes. A value over the amount
46 /// of available space on the disk will be reduced to the amount of available
47 /// space. A value of 0 disables the absolute size-based pruning.
48 uint64_t MaxSizeBytes
= 0;
50 /// The maximum number of files in the cache directory. A value of 0 disables
51 /// the number of files based pruning.
53 /// This defaults to 1000000 because with that many files there are
54 /// diminishing returns on the effectiveness of the cache. Some systems have a
55 /// limit on total number of files, and some also limit the number of files
56 /// per directory, such as Linux ext4, with the default setting (block size is
57 /// 4096 and large_dir disabled), there is a per-directory entry limit of
58 /// 508*510*floor(4096/(40+8))~=20M for average filename length of 40.
59 uint64_t MaxSizeFiles
= 1000000;
62 /// Parse the given string as a cache pruning policy. Defaults are taken from a
63 /// default constructed CachePruningPolicy object.
64 /// For example: "prune_interval=30s:prune_after=24h:cache_size=50%"
65 /// which means a pruning interval of 30 seconds, expiration time of 24 hours
66 /// and maximum cache size of 50% of available disk space.
67 Expected
<CachePruningPolicy
> parseCachePruningPolicy(StringRef PolicyStr
);
69 /// Peform pruning using the supplied policy, returns true if pruning
70 /// occurred, i.e. if Policy.Interval was expired.
72 /// As a safeguard against data loss if the user specifies the wrong directory
73 /// as their cache directory, this function will ignore files not matching the
74 /// pattern "llvmcache-*".
75 bool pruneCache(StringRef Path
, CachePruningPolicy Policy
);