LLVM  16.0.0git
CachePruning.h
Go to the documentation of this file.
1 //=- CachePruning.h - Helper to manage the pruning of a cache dir -*- C++ -*-=//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements pruning of a directory intended for cache storage, using
10 // various policies.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_SUPPORT_CACHEPRUNING_H
15 #define LLVM_SUPPORT_CACHEPRUNING_H
16 
17 #include "llvm/ADT/Optional.h"
19 #include <chrono>
20 #include <optional>
21 
22 namespace llvm {
23 
24 template <typename T> class Expected;
25 class StringRef;
26 
27 /// Policy for the pruneCache() function. A default constructed
28 /// CachePruningPolicy provides a reasonable default policy.
30  /// The pruning interval. This is intended to be used to avoid scanning the
31  /// directory too often. It does not impact the decision of which file to
32  /// prune. A value of 0 forces the scan to occur. A value of None disables
33  /// pruning.
34  std::optional<std::chrono::seconds> Interval = std::chrono::seconds(1200);
35 
36  /// The expiration for a file. When a file hasn't been accessed for Expiration
37  /// seconds, it is removed from the cache. A value of 0 disables the
38  /// expiration-based pruning.
39  std::chrono::seconds Expiration = std::chrono::hours(7 * 24); // 1w
40 
41  /// The maximum size for the cache directory, in terms of percentage of the
42  /// available space on the disk. Set to 100 to indicate no limit, 50 to
43  /// indicate that the cache size will not be left over half the available disk
44  /// space. A value over 100 will be reduced to 100. A value of 0 disables the
45  /// percentage size-based pruning.
47 
48  /// The maximum size for the cache directory in bytes. A value over the amount
49  /// of available space on the disk will be reduced to the amount of available
50  /// space. A value of 0 disables the absolute size-based pruning.
52 
53  /// The maximum number of files in the cache directory. A value of 0 disables
54  /// the number of files based pruning.
55  ///
56  /// This defaults to 1000000 because with that many files there are
57  /// diminishing returns on the effectiveness of the cache. Some systems have a
58  /// limit on total number of files, and some also limit the number of files
59  /// per directory, such as Linux ext4, with the default setting (block size is
60  /// 4096 and large_dir disabled), there is a per-directory entry limit of
61  /// 508*510*floor(4096/(40+8))~=20M for average filename length of 40.
63 };
64 
65 /// Parse the given string as a cache pruning policy. Defaults are taken from a
66 /// default constructed CachePruningPolicy object.
67 /// For example: "prune_interval=30s:prune_after=24h:cache_size=50%"
68 /// which means a pruning interval of 30 seconds, expiration time of 24 hours
69 /// and maximum cache size of 50% of available disk space.
71 
72 /// Peform pruning using the supplied policy, returns true if pruning
73 /// occurred, i.e. if Policy.Interval was expired.
74 ///
75 /// Check whether cache pruning happens using the supplied policy, adds a
76 /// ThinLTO warning if cache_size_bytes or cache_size_files is too small for the
77 /// current link job. The warning recommends the user to consider adjusting
78 /// --thinlto-cache-policy.
79 ///
80 /// As a safeguard against data loss if the user specifies the wrong directory
81 /// as their cache directory, this function will ignore files not matching the
82 /// pattern "llvmcache-*".
83 bool pruneCache(StringRef Path, CachePruningPolicy Policy,
84  const std::vector<std::unique_ptr<MemoryBuffer>> &Files = {});
85 } // namespace llvm
86 
87 #endif
MemoryBuffer.h
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Optional.h
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
llvm::CachePruningPolicy::MaxSizeBytes
uint64_t MaxSizeBytes
The maximum size for the cache directory in bytes.
Definition: CachePruning.h:51
llvm::CachePruningPolicy::Expiration
std::chrono::seconds Expiration
The expiration for a file.
Definition: CachePruning.h:39
llvm::pruneCache
bool pruneCache(StringRef Path, CachePruningPolicy Policy, const std::vector< std::unique_ptr< MemoryBuffer >> &Files={})
Peform pruning using the supplied policy, returns true if pruning occurred, i.e.
Definition: CachePruning.cpp:145
uint64_t
llvm::Interval
Interval Class - An Interval is a set of nodes defined such that every node in the interval has all o...
Definition: Interval.h:36
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::CachePruningPolicy::MaxSizePercentageOfAvailableSpace
unsigned MaxSizePercentageOfAvailableSpace
The maximum size for the cache directory, in terms of percentage of the available space on the disk.
Definition: CachePruning.h:46
llvm::CachePruningPolicy
Policy for the pruneCache() function.
Definition: CachePruning.h:29
llvm::CachePruningPolicy::MaxSizeFiles
uint64_t MaxSizeFiles
The maximum number of files in the cache directory.
Definition: CachePruning.h:62
llvm::parseCachePruningPolicy
Expected< CachePruningPolicy > parseCachePruningPolicy(StringRef PolicyStr)
Parse the given string as a cache pruning policy.
Definition: CachePruning.cpp:78