• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //=- CachePruning.h - Helper to manage the pruning of a cache dir -*- C++ -*-=//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements pruning of a directory intended for cache storage, using
11 // various policies.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_SUPPORT_CACHE_PRUNING_H
16 #define LLVM_SUPPORT_CACHE_PRUNING_H
17 
18 #include "llvm/ADT/StringRef.h"
19 
20 namespace llvm {
21 
22 /// Handle pruning a directory provided a path and some options to control what
23 /// to prune.
24 class CachePruning {
25 public:
26   /// Prepare to prune \p Path.
CachePruning(StringRef Path)27   CachePruning(StringRef Path) : Path(Path) {}
28 
29   /// Define the pruning interval. This is intended to be used to avoid scanning
30   /// the directory too often. It does not impact the decision of which file to
31   /// prune. A value of 0 forces the scan to occurs.
setPruningInterval(int PruningInterval)32   CachePruning &setPruningInterval(int PruningInterval) {
33     Interval = PruningInterval;
34     return *this;
35   }
36 
37   /// Define the expiration for a file. When a file hasn't been accessed for
38   /// \p ExpireAfter seconds, it is removed from the cache. A value of 0 disable
39   /// the expiration-based pruning.
setEntryExpiration(unsigned ExpireAfter)40   CachePruning &setEntryExpiration(unsigned ExpireAfter) {
41     Expiration = ExpireAfter;
42     return *this;
43   }
44 
45   /// Define the maximum size for the cache directory, in terms of percentage of
46   /// the available space on the the disk. Set to 100 to indicate no limit, 50
47   /// to indicate that the cache size will not be left over half the
48   /// available disk space. A value over 100 will be reduced to 100. A value of
49   /// 0 disable the size-based pruning.
setMaxSize(unsigned Percentage)50   CachePruning &setMaxSize(unsigned Percentage) {
51     PercentageOfAvailableSpace = std::min(100u, Percentage);
52     return *this;
53   }
54 
55   /// Peform pruning using the supplied options, returns true if pruning
56   /// occured, i.e. if PruningInterval was expired.
57   bool prune();
58 
59 private:
60   // Options that matches the setters above.
61   std::string Path;
62   unsigned Expiration = 0;
63   unsigned Interval = 0;
64   unsigned PercentageOfAvailableSpace = 0;
65 };
66 
67 } // namespace llvm
68 
69 #endif