GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/quota.h
Date: 2026-04-05 02:35:23
Exec Total Coverage
Lines: 16 40 40.0%
Branches: 2 4 50.0%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 */
4
5 #ifndef CVMFS_QUOTA_H_
6 #define CVMFS_QUOTA_H_
7
8 #include <pthread.h>
9 #include <stdint.h>
10 #include <unistd.h>
11
12 #include <map>
13 #include <string>
14 #include <vector>
15
16 #include "crypto/hash.h"
17 #include "util/single_copy.h"
18
19 /**
20 * The QuotaManager keeps track of the cache contents. It is informed by the
21 * cache manager about opens and inserts. The cache manager picks a quota
22 * manager that fits to the backend storage (e.g. POSIX, key-value store). File
23 * catalogs are "pinned" in the quota manager. Since they remain loaded
24 * (virtual file descriptor stays open), it does not make sense to remove them.
25 * Regular files might get pinned occasionally as well, for instance for the
26 * CernVM "core files".
27 *
28 * Multiple cache managers can share a single quota manager instance, as it is
29 * done for the local shared hard disk cache.
30 *
31 * Sometimes it is necessary that a quota manager instance gives feedback to its
32 * users. This is where back channels are used. Users can register a back
33 * channel, which gets informed for instance if the number of pinned catalogs
34 * get large and should be released.
35 */
36 class QuotaManager : SingleCopy {
37 public:
38 /**
39 * Quota manager protocol revision.
40 * Revision 1:
41 * - backchannel command 'R': release pinned files if possible
42 * Revision 2:
43 * - add kCleanupRate command
44 * Revision 3:
45 * - add kRegisterMountpoint, kGetMountpoints, kGetGroupHashes, and
46 * kSetCleanupPolicy commands
47 */
48 static const uint32_t kProtocolRevision;
49
50 enum Capabilities {
51 kCapIntrospectSize = 0,
52 kCapIntrospectCleanupRate,
53 kCapList,
54 kCapShrink,
55 kCapListeners,
56 };
57
58 QuotaManager();
59 virtual ~QuotaManager();
60 virtual bool HasCapability(Capabilities capability) = 0;
61
62 virtual void Insert(const shash::Any &hash, const uint64_t size,
63 const std::string &description) = 0;
64 virtual void InsertVolatile(const shash::Any &hash, const uint64_t size,
65 const std::string &description) = 0;
66 virtual bool Pin(const shash::Any &hash, const uint64_t size,
67 const std::string &description, const bool is_catalog) = 0;
68 virtual void Unpin(const shash::Any &hash) = 0;
69 virtual void Touch(const shash::Any &hash) = 0;
70 virtual void Remove(const shash::Any &file) = 0;
71 virtual bool Cleanup(const uint64_t leave_size) = 0;
72
73 virtual std::vector<std::string> List() = 0;
74 virtual std::vector<std::string> ListPinned() = 0;
75 virtual std::vector<std::string> ListCatalogs() = 0;
76 virtual std::vector<std::string> ListVolatile() = 0;
77 virtual uint64_t GetMaxFileSize() = 0;
78 virtual uint64_t GetCapacity() = 0;
79 virtual uint64_t GetSize() = 0;
80 virtual uint64_t GetSizePinned() = 0;
81 virtual bool SetLimit(uint64_t limit) = 0;
82 virtual uint64_t GetCleanupRate(uint64_t period_s) = 0;
83
84 virtual void Spawn() = 0;
85 virtual pid_t GetPid() = 0;
86 virtual uint32_t GetProtocolRevision() = 0;
87
88 virtual void RegisterBackChannel(int back_channel[2],
89 const std::string &channel_id) = 0;
90 virtual void UnregisterBackChannel(int back_channel[2],
91 const std::string &channel_id) = 0;
92 void BroadcastBackchannels(const std::string &message);
93
94 protected:
95 /**
96 * Hashes over the channel identifier mapped to writing ends of pipes.
97 */
98 std::map<shash::Md5, int> back_channels_;
99 pthread_mutex_t *lock_back_channels_;
100 112 void LockBackChannels() {
101 112 const int retval = pthread_mutex_lock(lock_back_channels_);
102
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 112 times.
112 assert(retval == 0);
103 112 }
104 112 void UnlockBackChannels() {
105 112 const int retval = pthread_mutex_unlock(lock_back_channels_);
106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 112 times.
112 assert(retval == 0);
107 112 }
108
109 /**
110 * Protocol of the running cache manager instance. Needs to be figured out
111 * and set during initialization of concrete instances.
112 */
113 uint32_t protocol_revision_;
114 };
115
116
117 /**
118 * No quota management.
119 */
120 class NoopQuotaManager : public QuotaManager {
121 public:
122 22540 virtual ~NoopQuotaManager() { }
123 virtual bool HasCapability(Capabilities capability) { return false; }
124
125 2166 virtual void Insert(const shash::Any &hash, const uint64_t size,
126 2166 const std::string &description) { }
127 virtual void InsertVolatile(const shash::Any &hash, const uint64_t size,
128 const std::string &description) { }
129 230 virtual bool Pin(const shash::Any &hash, const uint64_t size,
130 const std::string &description, const bool is_catalog) {
131 230 return true;
132 }
133 126 virtual void Unpin(const shash::Any &hash) { }
134 561 virtual void Touch(const shash::Any &hash) { }
135 virtual void Remove(const shash::Any &file) { }
136 virtual bool Cleanup(const uint64_t leave_size) { return false; }
137
138 virtual void RegisterBackChannel(int back_channel[2],
139 const std::string &channel_id) { }
140 virtual void UnregisterBackChannel(int back_channel[2],
141 const std::string &channel_id) { }
142
143 virtual std::vector<std::string> List() { return std::vector<std::string>(); }
144 virtual std::vector<std::string> ListPinned() {
145 return std::vector<std::string>();
146 }
147 virtual std::vector<std::string> ListCatalogs() {
148 return std::vector<std::string>();
149 }
150 virtual std::vector<std::string> ListVolatile() {
151 return std::vector<std::string>();
152 }
153 2480 virtual uint64_t GetMaxFileSize() { return uint64_t(-1); }
154 virtual uint64_t GetCapacity() { return uint64_t(-1); }
155 virtual uint64_t GetSize() { return 0; }
156 virtual uint64_t GetSizePinned() { return 0; }
157 virtual bool SetLimit(uint64_t) { return false; }
158 virtual uint64_t GetCleanupRate(uint64_t period_s) { return 0; }
159
160 virtual void Spawn() { }
161 virtual pid_t GetPid() { return getpid(); }
162 virtual uint32_t GetProtocolRevision() { return 0; }
163 };
164
165 #endif // CVMFS_QUOTA_H_
166