Home | History | Annotate | Download | only in Support
      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 #include <chrono>
     20 
     21 namespace llvm {
     22 
     23 template <typename T> class Expected;
     24 
     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.
     31   std::chrono::seconds Interval = std::chrono::seconds(1200);
     32 
     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 
     38   /// The maximum size for the cache directory, in terms of percentage of the
     39   /// available space on the the disk. Set to 100 to indicate no limit, 50 to
     40   /// indicate that the cache size will not be left over half the available disk
     41   /// space. A value over 100 will be reduced to 100. A value of 0 disables the
     42   /// percentage size-based pruning.
     43   unsigned MaxSizePercentageOfAvailableSpace = 75;
     44 
     45   /// The maximum size for the cache directory in bytes. A value over the amount
     46   /// of available space on the disk will be reduced to the amount of available
     47   /// space. A value of 0 disables the absolute size-based pruning.
     48   uint64_t MaxSizeBytes = 0;
     49 };
     50 
     51 /// Parse the given string as a cache pruning policy. Defaults are taken from a
     52 /// default constructed CachePruningPolicy object.
     53 /// For example: "prune_interval=30s:prune_after=24h:cache_size=50%"
     54 /// which means a pruning interval of 30 seconds, expiration time of 24 hours
     55 /// and maximum cache size of 50% of available disk space.
     56 Expected<CachePruningPolicy> parseCachePruningPolicy(StringRef PolicyStr);
     57 
     58 /// Peform pruning using the supplied policy, returns true if pruning
     59 /// occured, i.e. if Policy.Interval was expired.
     60 ///
     61 /// As a safeguard against data loss if the user specifies the wrong directory
     62 /// as their cache directory, this function will ignore files not matching the
     63 /// pattern "llvmcache-*".
     64 bool pruneCache(StringRef Path, CachePruningPolicy Policy);
     65 
     66 } // namespace llvm
     67 
     68 #endif
     69