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