Directory: | cvmfs/ |
---|---|
File: | cvmfs/lru_md.h |
Date: | 2025-07-06 02:35:01 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 29 | 62 | 46.8% |
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 | 2646 | 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 | return static_cast<uint32_t>( | ||
32 | 2646 | *(reinterpret_cast<const uint32_t *>(key.digest) + 1)); | |
33 | } | ||
34 | |||
35 | ✗ | static inline uint32_t hasher_inode(const fuse_ino_t &inode) { | |
36 | ✗ | return MurmurHash2(&inode, sizeof(inode), 0x07387a4f); | |
37 | } | ||
38 | // uint32_t hasher_md5(const shash::Md5 &key); | ||
39 | // uint32_t hasher_inode(const fuse_ino_t &inode); | ||
40 | |||
41 | |||
42 | class InodeCache : public LruCache<fuse_ino_t, catalog::DirectoryEntry> { | ||
43 | public: | ||
44 | 480 | explicit InodeCache(unsigned int cache_size, perf::Statistics *statistics) | |
45 | 480 | : LruCache<fuse_ino_t, catalog::DirectoryEntry>( | |
46 | ✗ | cache_size, fuse_ino_t(-1), hasher_inode, | |
47 |
3/6✓ Branch 2 taken 480 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 480 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 480 times.
✗ Branch 9 not taken.
|
480 | perf::StatisticsTemplate("inode_cache", statistics)) { } |
48 | |||
49 | ✗ | bool Insert(const fuse_ino_t &inode, const catalog::DirectoryEntry &dirent) { | |
50 | ✗ | LogCvmfs(kLogLru, kLogDebug, "insert inode --> dirent: %lu -> '%s'", inode, | |
51 | ✗ | dirent.name().c_str()); | |
52 | ✗ | const bool result = LruCache<fuse_ino_t, catalog::DirectoryEntry>::Insert( | |
53 | inode, dirent); | ||
54 | ✗ | return result; | |
55 | } | ||
56 | |||
57 | ✗ | bool Lookup(const fuse_ino_t &inode, catalog::DirectoryEntry *dirent, | |
58 | bool update_lru = true) { | ||
59 | ✗ | const bool result = LruCache<fuse_ino_t, catalog::DirectoryEntry>::Lookup( | |
60 | inode, dirent); | ||
61 | ✗ | LogCvmfs(kLogLru, kLogDebug, "lookup inode --> dirent: %lu (%s)", inode, | |
62 | result ? "hit" : "miss"); | ||
63 | ✗ | return result; | |
64 | } | ||
65 | |||
66 | ✗ | void Drop() { | |
67 | ✗ | LogCvmfs(kLogLru, kLogDebug, "dropping inode cache"); | |
68 | ✗ | LruCache<fuse_ino_t, catalog::DirectoryEntry>::Drop(); | |
69 | } | ||
70 | }; // InodeCache | ||
71 | |||
72 | |||
73 | class PathCache : public LruCache<fuse_ino_t, PathString> { | ||
74 | public: | ||
75 | 480 | explicit PathCache(unsigned int cache_size, perf::Statistics *statistics) | |
76 | 480 | : LruCache<fuse_ino_t, PathString>( | |
77 | ✗ | cache_size, fuse_ino_t(-1), hasher_inode, | |
78 |
3/6✓ Branch 2 taken 480 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 480 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 480 times.
✗ Branch 9 not taken.
|
480 | perf::StatisticsTemplate("path_cache", statistics)) { } |
79 | |||
80 | ✗ | bool Insert(const fuse_ino_t &inode, const PathString &path) { | |
81 | ✗ | LogCvmfs(kLogLru, kLogDebug, "insert inode --> path %lu -> '%s'", inode, | |
82 | path.c_str()); | ||
83 | ✗ | const bool result = LruCache<fuse_ino_t, PathString>::Insert(inode, path); | |
84 | ✗ | return result; | |
85 | } | ||
86 | |||
87 | ✗ | bool Lookup(const fuse_ino_t &inode, PathString *path, | |
88 | bool update_lru = true) { | ||
89 | ✗ | const bool found = LruCache<fuse_ino_t, PathString>::Lookup(inode, path); | |
90 | ✗ | LogCvmfs(kLogLru, kLogDebug, "lookup inode --> path: %lu (%s)", inode, | |
91 | found ? "hit" : "miss"); | ||
92 | ✗ | return found; | |
93 | } | ||
94 | |||
95 | ✗ | void Drop() { | |
96 | ✗ | LogCvmfs(kLogLru, kLogDebug, "dropping path cache"); | |
97 | ✗ | LruCache<fuse_ino_t, PathString>::Drop(); | |
98 | } | ||
99 | }; // PathCache | ||
100 | |||
101 | |||
102 | class Md5PathCache : public LruCache<shash::Md5, catalog::DirectoryEntry> { | ||
103 | public: | ||
104 | 733 | explicit Md5PathCache(unsigned int cache_size, perf::Statistics *statistics) | |
105 | 733 | : LruCache<shash::Md5, catalog::DirectoryEntry>( | |
106 |
2/4✓ Branch 2 taken 733 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 733 times.
✗ Branch 7 not taken.
|
1466 | cache_size, shash::Md5(shash::AsciiPtr("!")), hasher_md5, |
107 |
4/8✓ Branch 2 taken 733 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 733 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 733 times.
✗ Branch 9 not taken.
✓ Branch 14 taken 733 times.
✗ Branch 15 not taken.
|
2199 | perf::StatisticsTemplate("md5_path_cache", statistics)) { |
108 |
2/4✓ Branch 1 taken 733 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 733 times.
✗ Branch 5 not taken.
|
733 | dirent_negative_ = catalog::DirectoryEntry(catalog::kDirentNegative); |
109 | 733 | } | |
110 | |||
111 | 714 | bool Insert(const shash::Md5 &hash, const catalog::DirectoryEntry &dirent) { | |
112 |
1/2✓ Branch 3 taken 714 times.
✗ Branch 4 not taken.
|
1428 | LogCvmfs(kLogLru, kLogDebug, "insert md5 --> dirent: %s -> '%s'", |
113 |
1/2✓ Branch 2 taken 714 times.
✗ Branch 3 not taken.
|
2142 | hash.ToString().c_str(), dirent.name().c_str()); |
114 | 714 | const bool result = LruCache<shash::Md5, catalog::DirectoryEntry>::Insert( | |
115 | hash, dirent); | ||
116 | 714 | return result; | |
117 | } | ||
118 | |||
119 | 84 | bool InsertNegative(const shash::Md5 &hash) { | |
120 | 84 | const bool result = Insert(hash, dirent_negative_); | |
121 |
1/2✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
|
84 | if (result) |
122 | 84 | perf::Inc(counters_.n_insert_negative); | |
123 | 84 | return result; | |
124 | } | ||
125 | |||
126 | 1218 | bool Lookup(const shash::Md5 &hash, catalog::DirectoryEntry *dirent, | |
127 | bool update_lru = true) { | ||
128 | 1218 | const bool result = LruCache<shash::Md5, catalog::DirectoryEntry>::Lookup( | |
129 | hash, dirent); | ||
130 |
3/4✓ Branch 0 taken 504 times.
✓ Branch 1 taken 714 times.
✓ Branch 4 taken 1218 times.
✗ Branch 5 not taken.
|
2436 | LogCvmfs(kLogLru, kLogDebug, "lookup md5 --> dirent: %s (%s)", |
131 | 2436 | hash.ToString().c_str(), result ? "hit" : "miss"); | |
132 | 1218 | return result; | |
133 | } | ||
134 | |||
135 | ✗ | bool Forget(const shash::Md5 &hash) { | |
136 | ✗ | LogCvmfs(kLogLru, kLogDebug, "forget md5: %s", hash.ToString().c_str()); | |
137 | ✗ | return LruCache<shash::Md5, catalog::DirectoryEntry>::Forget(hash); | |
138 | } | ||
139 | |||
140 | ✗ | void Drop() { | |
141 | ✗ | LogCvmfs(kLogLru, kLogDebug, "dropping md5path cache"); | |
142 | ✗ | LruCache<shash::Md5, catalog::DirectoryEntry>::Drop(); | |
143 | } | ||
144 | |||
145 | private: | ||
146 | catalog::DirectoryEntry dirent_negative_; | ||
147 | }; // Md5PathCache | ||
148 | |||
149 | } // namespace lru | ||
150 | |||
151 | #endif // CVMFS_LRU_MD_H_ | ||
152 |