CachePruning.h 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //=- CachePruning.h - Helper to manage the pruning of a cache dir -*- C++ -*-=//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file implements pruning of a directory intended for cache storage, using
  15. // various policies.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_SUPPORT_CACHEPRUNING_H
  19. #define LLVM_SUPPORT_CACHEPRUNING_H
  20. #include "llvm/ADT/Optional.h"
  21. #include <chrono>
  22. namespace llvm {
  23. template <typename T> class Expected;
  24. class StringRef;
  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
  31. /// pruning.
  32. 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
  37. /// The maximum size for the cache directory, in terms of percentage of the
  38. /// available space on the disk. Set to 100 to indicate no limit, 50 to
  39. /// indicate that the cache size will not be left over half the available disk
  40. /// space. A value over 100 will be reduced to 100. A value of 0 disables the
  41. /// percentage size-based pruning.
  42. unsigned MaxSizePercentageOfAvailableSpace = 75;
  43. /// The maximum size for the cache directory in bytes. A value over the amount
  44. /// of available space on the disk will be reduced to the amount of available
  45. /// space. A value of 0 disables the absolute size-based pruning.
  46. uint64_t MaxSizeBytes = 0;
  47. /// The maximum number of files in the cache directory. A value of 0 disables
  48. /// the number of files based pruning.
  49. ///
  50. /// This defaults to 1000000 because with that many files there are
  51. /// diminishing returns on the effectiveness of the cache. Some systems have a
  52. /// limit on total number of files, and some also limit the number of files
  53. /// per directory, such as Linux ext4, with the default setting (block size is
  54. /// 4096 and large_dir disabled), there is a per-directory entry limit of
  55. /// 508*510*floor(4096/(40+8))~=20M for average filename length of 40.
  56. uint64_t MaxSizeFiles = 1000000;
  57. };
  58. /// Parse the given string as a cache pruning policy. Defaults are taken from a
  59. /// default constructed CachePruningPolicy object.
  60. /// For example: "prune_interval=30s:prune_after=24h:cache_size=50%"
  61. /// which means a pruning interval of 30 seconds, expiration time of 24 hours
  62. /// and maximum cache size of 50% of available disk space.
  63. Expected<CachePruningPolicy> parseCachePruningPolicy(StringRef PolicyStr);
  64. /// Peform pruning using the supplied policy, returns true if pruning
  65. /// occurred, i.e. if Policy.Interval was expired.
  66. ///
  67. /// As a safeguard against data loss if the user specifies the wrong directory
  68. /// as their cache directory, this function will ignore files not matching the
  69. /// pattern "llvmcache-*".
  70. bool pruneCache(StringRef Path, CachePruningPolicy Policy);
  71. } // namespace llvm
  72. #endif
  73. #ifdef __GNUC__
  74. #pragma GCC diagnostic pop
  75. #endif