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
21namespace llvm {
22
23template <typename T> class Expected;
24
25/// Policy for the pruneCache() function. A default constructed
26/// CachePruningPolicy provides a reasonable default policy.
27struct 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  /// size-based pruning.
43  unsigned PercentageOfAvailableSpace = 75;
44};
45
46/// Parse the given string as a cache pruning policy. Defaults are taken from a
47/// default constructed CachePruningPolicy object.
48/// For example: "prune_interval=30s:prune_after=24h:cache_size=50%"
49/// which means a pruning interval of 30 seconds, expiration time of 24 hours
50/// and maximum cache size of 50% of available disk space.
51Expected<CachePruningPolicy> parseCachePruningPolicy(StringRef PolicyStr);
52
53/// Peform pruning using the supplied policy, returns true if pruning
54/// occured, i.e. if Policy.Interval was expired.
55///
56/// As a safeguard against data loss if the user specifies the wrong directory
57/// as their cache directory, this function will ignore files not matching the
58/// pattern "llvmcache-*".
59bool pruneCache(StringRef Path, CachePruningPolicy Policy);
60
61} // namespace llvm
62
63#endif
64