Directory: | cvmfs/ |
---|---|
File: | cvmfs/lru_md.h |
Date: | 2025-02-02 02:34:22 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 28 | 62 | 45.2% |
Branches: | 20 | 46 | 43.5% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /** | ||
2 | * This file is part of the CernVM File System. | ||
3 | * | ||
4 | * Provides the LRU sub classes used for the file system client meta-data cache | ||
5 | */ | ||
6 | |||
7 | #ifndef CVMFS_LRU_MD_H_ | ||
8 | #define CVMFS_LRU_MD_H_ | ||
9 | |||
10 | #ifndef __STDC_FORMAT_MACROS | ||
11 | #define __STDC_FORMAT_MACROS | ||
12 | #endif | ||
13 | |||
14 | #include <stdint.h> | ||
15 | |||
16 | #include "crypto/hash.h" | ||
17 | #include "directory_entry.h" | ||
18 | #include "duplex_fuse.h" | ||
19 | #include "lru.h" | ||
20 | #include "shortstring.h" | ||
21 | #include "util/atomic.h" | ||
22 | #include "util/logging.h" | ||
23 | #include "util/murmur.hxx" | ||
24 | |||
25 | |||
26 | namespace lru { | ||
27 | |||
28 | // Hash functions | ||
29 | 63 | static inline uint32_t hasher_md5(const shash::Md5 &key) { | |
30 | // Don't start with the first bytes, because == is using them as well | ||
31 | 63 | return (uint32_t) *(reinterpret_cast<const uint32_t *>(key.digest) + 1); | |
32 | } | ||
33 | |||
34 | ✗ | static inline uint32_t hasher_inode(const fuse_ino_t &inode) { | |
35 | ✗ | return MurmurHash2(&inode, sizeof(inode), 0x07387a4f); | |
36 | } | ||
37 | // uint32_t hasher_md5(const shash::Md5 &key); | ||
38 | // uint32_t hasher_inode(const fuse_ino_t &inode); | ||
39 | |||
40 | |||
41 | class InodeCache : public LruCache<fuse_ino_t, catalog::DirectoryEntry> | ||
42 | { | ||
43 | public: | ||
44 | 16 | explicit InodeCache(unsigned int cache_size, perf::Statistics *statistics) : | |
45 | LruCache<fuse_ino_t, catalog::DirectoryEntry>( | ||
46 | ✗ | cache_size, fuse_ino_t(-1), hasher_inode, | |
47 |
3/6✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 16 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 16 times.
✗ Branch 9 not taken.
|
16 | perf::StatisticsTemplate("inode_cache", statistics)) |
48 | { | ||
49 | 16 | } | |
50 | |||
51 | ✗ | bool Insert(const fuse_ino_t &inode, const catalog::DirectoryEntry &dirent) { | |
52 | ✗ | LogCvmfs(kLogLru, kLogDebug, "insert inode --> dirent: %lu -> '%s'", | |
53 | ✗ | inode, dirent.name().c_str()); | |
54 | const bool result = | ||
55 | ✗ | LruCache<fuse_ino_t, catalog::DirectoryEntry>::Insert(inode, dirent); | |
56 | ✗ | return result; | |
57 | } | ||
58 | |||
59 | ✗ | bool Lookup(const fuse_ino_t &inode, catalog::DirectoryEntry *dirent, | |
60 | bool update_lru = true) | ||
61 | { | ||
62 | const bool result = | ||
63 | ✗ | LruCache<fuse_ino_t, catalog::DirectoryEntry>::Lookup(inode, dirent); | |
64 | ✗ | LogCvmfs(kLogLru, kLogDebug, "lookup inode --> dirent: %lu (%s)", | |
65 | inode, result ? "hit" : "miss"); | ||
66 | ✗ | return result; | |
67 | } | ||
68 | |||
69 | ✗ | void Drop() { | |
70 | ✗ | LogCvmfs(kLogLru, kLogDebug, "dropping inode cache"); | |
71 | ✗ | LruCache<fuse_ino_t, catalog::DirectoryEntry>::Drop(); | |
72 | } | ||
73 | }; // InodeCache | ||
74 | |||
75 | |||
76 | class PathCache : public LruCache<fuse_ino_t, PathString> { | ||
77 | public: | ||
78 | 16 | explicit PathCache(unsigned int cache_size, perf::Statistics *statistics) : | |
79 | ✗ | LruCache<fuse_ino_t, PathString>(cache_size, fuse_ino_t(-1), hasher_inode, | |
80 |
3/6✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 16 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 16 times.
✗ Branch 9 not taken.
|
16 | perf::StatisticsTemplate("path_cache", statistics)) |
81 | { | ||
82 | 16 | } | |
83 | |||
84 | ✗ | bool Insert(const fuse_ino_t &inode, const PathString &path) { | |
85 | ✗ | LogCvmfs(kLogLru, kLogDebug, "insert inode --> path %lu -> '%s'", | |
86 | inode, path.c_str()); | ||
87 | const bool result = | ||
88 | ✗ | LruCache<fuse_ino_t, PathString>::Insert(inode, path); | |
89 | ✗ | return result; | |
90 | } | ||
91 | |||
92 | ✗ | bool Lookup(const fuse_ino_t &inode, PathString *path, | |
93 | bool update_lru = true) | ||
94 | { | ||
95 | const bool found = | ||
96 | ✗ | LruCache<fuse_ino_t, PathString>::Lookup(inode, path); | |
97 | ✗ | LogCvmfs(kLogLru, kLogDebug, "lookup inode --> path: %lu (%s)", | |
98 | inode, found ? "hit" : "miss"); | ||
99 | ✗ | return found; | |
100 | } | ||
101 | |||
102 | ✗ | void Drop() { | |
103 | ✗ | LogCvmfs(kLogLru, kLogDebug, "dropping path cache"); | |
104 | ✗ | LruCache<fuse_ino_t, PathString>::Drop(); | |
105 | } | ||
106 | }; // PathCache | ||
107 | |||
108 | |||
109 | class Md5PathCache : | ||
110 | public LruCache<shash::Md5, catalog::DirectoryEntry> | ||
111 | { | ||
112 | public: | ||
113 | 23 | explicit Md5PathCache(unsigned int cache_size, perf::Statistics *statistics) : | |
114 | LruCache<shash::Md5, catalog::DirectoryEntry>( | ||
115 |
2/4✓ Branch 2 taken 23 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 23 times.
✗ Branch 7 not taken.
|
46 | cache_size, shash::Md5(shash::AsciiPtr("!")), hasher_md5, |
116 |
4/8✓ Branch 2 taken 23 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 23 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 23 times.
✗ Branch 9 not taken.
✓ Branch 14 taken 23 times.
✗ Branch 15 not taken.
|
69 | perf::StatisticsTemplate("md5_path_cache", statistics)) |
117 | { | ||
118 |
2/4✓ Branch 1 taken 23 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 23 times.
✗ Branch 5 not taken.
|
23 | dirent_negative_ = catalog::DirectoryEntry(catalog::kDirentNegative); |
119 | 23 | } | |
120 | |||
121 | 17 | bool Insert(const shash::Md5 &hash, const catalog::DirectoryEntry &dirent) { | |
122 |
1/2✓ Branch 3 taken 17 times.
✗ Branch 4 not taken.
|
34 | LogCvmfs(kLogLru, kLogDebug, "insert md5 --> dirent: %s -> '%s'", |
123 |
1/2✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
|
51 | hash.ToString().c_str(), dirent.name().c_str()); |
124 | const bool result = | ||
125 | 17 | LruCache<shash::Md5, catalog::DirectoryEntry>::Insert(hash, dirent); | |
126 | 17 | return result; | |
127 | } | ||
128 | |||
129 | 2 | bool InsertNegative(const shash::Md5 &hash) { | |
130 | 2 | const bool result = Insert(hash, dirent_negative_); | |
131 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (result) |
132 | 2 | perf::Inc(counters_.n_insert_negative); | |
133 | 2 | return result; | |
134 | } | ||
135 | |||
136 | 29 | bool Lookup(const shash::Md5 &hash, catalog::DirectoryEntry *dirent, | |
137 | bool update_lru = true) | ||
138 | { | ||
139 | const bool result = | ||
140 | 29 | LruCache<shash::Md5, catalog::DirectoryEntry>::Lookup(hash, dirent); | |
141 |
3/4✓ Branch 0 taken 12 times.
✓ Branch 1 taken 17 times.
✓ Branch 4 taken 29 times.
✗ Branch 5 not taken.
|
58 | LogCvmfs(kLogLru, kLogDebug, "lookup md5 --> dirent: %s (%s)", |
142 | 58 | hash.ToString().c_str(), result ? "hit" : "miss"); | |
143 | 29 | return result; | |
144 | } | ||
145 | |||
146 | ✗ | bool Forget(const shash::Md5 &hash) { | |
147 | ✗ | LogCvmfs(kLogLru, kLogDebug, "forget md5: %s", | |
148 | ✗ | hash.ToString().c_str()); | |
149 | ✗ | return LruCache<shash::Md5, catalog::DirectoryEntry>::Forget(hash); | |
150 | } | ||
151 | |||
152 | ✗ | void Drop() { | |
153 | ✗ | LogCvmfs(kLogLru, kLogDebug, "dropping md5path cache"); | |
154 | ✗ | LruCache<shash::Md5, catalog::DirectoryEntry>::Drop(); | |
155 | } | ||
156 | |||
157 | private: | ||
158 | catalog::DirectoryEntry dirent_negative_; | ||
159 | }; // Md5PathCache | ||
160 | |||
161 | } // namespace lru | ||
162 | |||
163 | #endif // CVMFS_LRU_MD_H_ | ||
164 |