Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:28

0001 //=- CachePruning.h - Helper to manage the pruning of a cache dir -*- C++ -*-=//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file implements pruning of a directory intended for cache storage, using
0010 // various policies.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_SUPPORT_CACHEPRUNING_H
0015 #define LLVM_SUPPORT_CACHEPRUNING_H
0016 
0017 #include "llvm/Support/MemoryBuffer.h"
0018 #include <chrono>
0019 #include <optional>
0020 
0021 namespace llvm {
0022 
0023 template <typename T> class Expected;
0024 class StringRef;
0025 
0026 /// Policy for the pruneCache() function. A default constructed
0027 /// CachePruningPolicy provides a reasonable default policy.
0028 struct CachePruningPolicy {
0029   /// The pruning interval. This is intended to be used to avoid scanning the
0030   /// directory too often. It does not impact the decision of which file to
0031   /// prune. A value of 0 forces the scan to occur. A value of std::nullopt
0032   /// disables pruning.
0033   std::optional<std::chrono::seconds> Interval = std::chrono::seconds(1200);
0034 
0035   /// The expiration for a file. When a file hasn't been accessed for Expiration
0036   /// seconds, it is removed from the cache. A value of 0 disables the
0037   /// expiration-based pruning.
0038   std::chrono::seconds Expiration = std::chrono::hours(7 * 24); // 1w
0039 
0040   /// The maximum size for the cache directory, in terms of percentage of the
0041   /// available space on the disk. Set to 100 to indicate no limit, 50 to
0042   /// indicate that the cache size will not be left over half the available disk
0043   /// space. A value over 100 will be reduced to 100. A value of 0 disables the
0044   /// percentage size-based pruning.
0045   unsigned MaxSizePercentageOfAvailableSpace = 75;
0046 
0047   /// The maximum size for the cache directory in bytes. A value over the amount
0048   /// of available space on the disk will be reduced to the amount of available
0049   /// space. A value of 0 disables the absolute size-based pruning.
0050   uint64_t MaxSizeBytes = 0;
0051 
0052   /// The maximum number of files in the cache directory. A value of 0 disables
0053   /// the number of files based pruning.
0054   ///
0055   /// This defaults to 1000000 because with that many files there are
0056   /// diminishing returns on the effectiveness of the cache. Some systems have a
0057   /// limit on total number of files, and some also limit the number of files
0058   /// per directory, such as Linux ext4, with the default setting (block size is
0059   /// 4096 and large_dir disabled), there is a per-directory entry limit of
0060   /// 508*510*floor(4096/(40+8))~=20M for average filename length of 40.
0061   uint64_t MaxSizeFiles = 1000000;
0062 };
0063 
0064 /// Parse the given string as a cache pruning policy. Defaults are taken from a
0065 /// default constructed CachePruningPolicy object.
0066 /// For example: "prune_interval=30s:prune_after=24h:cache_size=50%"
0067 /// which means a pruning interval of 30 seconds, expiration time of 24 hours
0068 /// and maximum cache size of 50% of available disk space.
0069 Expected<CachePruningPolicy> parseCachePruningPolicy(StringRef PolicyStr);
0070 
0071 /// Peform pruning using the supplied policy, returns true if pruning
0072 /// occurred, i.e. if Policy.Interval was expired.
0073 ///
0074 /// Check whether cache pruning happens using the supplied policy, adds a
0075 /// ThinLTO warning if cache_size_bytes or cache_size_files is too small for the
0076 /// current link job. The warning recommends the user to consider adjusting
0077 /// --thinlto-cache-policy.
0078 ///
0079 /// As a safeguard against data loss if the user specifies the wrong directory
0080 /// as their cache directory, this function will ignore files not matching the
0081 /// pattern "llvmcache-*".
0082 bool pruneCache(StringRef Path, CachePruningPolicy Policy,
0083                 const std::vector<std::unique_ptr<MemoryBuffer>> &Files = {});
0084 } // namespace llvm
0085 
0086 #endif