GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/quota.h
Date: 2025-06-29 02:35:41
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 */
45 static const uint32_t kProtocolRevision;
46
47 enum Capabilities {
48 kCapIntrospectSize = 0,
49 kCapIntrospectCleanupRate,
50 kCapList,
51 kCapShrink,
52 kCapListeners,
53 };
54
55 QuotaManager();
56 virtual ~QuotaManager();
57 virtual bool HasCapability(Capabilities capability) = 0;
58
59 virtual void Insert(const shash::Any &hash, const uint64_t size,
60 const std::string &description) = 0;
61 virtual void InsertVolatile(const shash::Any &hash, const uint64_t size,
62 const std::string &description) = 0;
63 virtual bool Pin(const shash::Any &hash, const uint64_t size,
64 const std::string &description, const bool is_catalog) = 0;
65 virtual void Unpin(const shash::Any &hash) = 0;
66 virtual void Touch(const shash::Any &hash) = 0;
67 virtual void Remove(const shash::Any &file) = 0;
68 virtual bool Cleanup(const uint64_t leave_size) = 0;
69
70 virtual std::vector<std::string> List() = 0;
71 virtual std::vector<std::string> ListPinned() = 0;
72 virtual std::vector<std::string> ListCatalogs() = 0;
73 virtual std::vector<std::string> ListVolatile() = 0;
74 virtual uint64_t GetMaxFileSize() = 0;
75 virtual uint64_t GetCapacity() = 0;
76 virtual uint64_t GetSize() = 0;
77 virtual uint64_t GetSizePinned() = 0;
78 virtual bool SetLimit(uint64_t limit) = 0;
79 virtual uint64_t GetCleanupRate(uint64_t period_s) = 0;
80
81 virtual void Spawn() = 0;
82 virtual pid_t GetPid() = 0;
83 virtual uint32_t GetProtocolRevision() = 0;
84
85 virtual void RegisterBackChannel(int back_channel[2],
86 const std::string &channel_id) = 0;
87 virtual void UnregisterBackChannel(int back_channel[2],
88 const std::string &channel_id) = 0;
89 void BroadcastBackchannels(const std::string &message);
90
91 protected:
92 /**
93 * Hashes over the channel identifier mapped to writing ends of pipes.
94 */
95 std::map<shash::Md5, int> back_channels_;
96 pthread_mutex_t *lock_back_channels_;
97 262 void LockBackChannels() {
98 262 const int retval = pthread_mutex_lock(lock_back_channels_);
99
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 262 times.
262 assert(retval == 0);
100 262 }
101 262 void UnlockBackChannels() {
102 262 const int retval = pthread_mutex_unlock(lock_back_channels_);
103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 262 times.
262 assert(retval == 0);
104 262 }
105
106 /**
107 * Protocol of the running cache manager instance. Needs to be figured out
108 * and set during initialization of concrete instances.
109 */
110 uint32_t protocol_revision_;
111 };
112
113
114 /**
115 * No quota management.
116 */
117 class NoopQuotaManager : public QuotaManager {
118 public:
119 31168 virtual ~NoopQuotaManager() { }
120 virtual bool HasCapability(Capabilities capability) { return false; }
121
122 4126 virtual void Insert(const shash::Any &hash, const uint64_t size,
123 4126 const std::string &description) { }
124 virtual void InsertVolatile(const shash::Any &hash, const uint64_t size,
125 const std::string &description) { }
126 488 virtual bool Pin(const shash::Any &hash, const uint64_t size,
127 const std::string &description, const bool is_catalog) {
128 488 return true;
129 }
130 404 virtual void Unpin(const shash::Any &hash) { }
131 917 virtual void Touch(const shash::Any &hash) { }
132 virtual void Remove(const shash::Any &file) { }
133 virtual bool Cleanup(const uint64_t leave_size) { return false; }
134
135 virtual void RegisterBackChannel(int back_channel[2],
136 const std::string &channel_id) { }
137 virtual void UnregisterBackChannel(int back_channel[2],
138 const std::string &channel_id) { }
139
140 virtual std::vector<std::string> List() { return std::vector<std::string>(); }
141 virtual std::vector<std::string> ListPinned() {
142 return std::vector<std::string>();
143 }
144 virtual std::vector<std::string> ListCatalogs() {
145 return std::vector<std::string>();
146 }
147 virtual std::vector<std::string> ListVolatile() {
148 return std::vector<std::string>();
149 }
150 4805 virtual uint64_t GetMaxFileSize() { return uint64_t(-1); }
151 virtual uint64_t GetCapacity() { return uint64_t(-1); }
152 virtual uint64_t GetSize() { return 0; }
153 virtual uint64_t GetSizePinned() { return 0; }
154 virtual bool SetLimit(uint64_t) { return false; }
155 virtual uint64_t GetCleanupRate(uint64_t period_s) { return 0; }
156
157 virtual void Spawn() { }
158 virtual pid_t GetPid() { return getpid(); }
159 virtual uint32_t GetProtocolRevision() { return 0; }
160 };
161
162 #endif // CVMFS_QUOTA_H_
163