CachePruning.h 3.5 KB

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