| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/lru_md.h |
| Date: | 2025-10-19 02:35:28 |
| 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 | 1953 | 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 | 1953 | *(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 | 691 | explicit InodeCache(unsigned int cache_size, perf::Statistics *statistics) | |
| 45 | 691 | : LruCache<fuse_ino_t, catalog::DirectoryEntry>( | |
| 46 | ✗ | cache_size, fuse_ino_t(-1), hasher_inode, | |
| 47 |
3/6✓ Branch 2 taken 691 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 691 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 691 times.
✗ Branch 9 not taken.
|
691 | 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 | 691 | explicit PathCache(unsigned int cache_size, perf::Statistics *statistics) | |
| 76 | 691 | : LruCache<fuse_ino_t, PathString>( | |
| 77 | ✗ | cache_size, fuse_ino_t(-1), hasher_inode, | |
| 78 |
3/6✓ Branch 2 taken 691 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 691 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 691 times.
✗ Branch 9 not taken.
|
691 | 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 | 878 | explicit Md5PathCache(unsigned int cache_size, perf::Statistics *statistics) | |
| 105 | 878 | : LruCache<shash::Md5, catalog::DirectoryEntry>( | |
| 106 |
2/4✓ Branch 2 taken 878 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 878 times.
✗ Branch 7 not taken.
|
1756 | cache_size, shash::Md5(shash::AsciiPtr("!")), hasher_md5, |
| 107 |
4/8✓ Branch 2 taken 878 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 878 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 878 times.
✗ Branch 9 not taken.
✓ Branch 14 taken 878 times.
✗ Branch 15 not taken.
|
2634 | perf::StatisticsTemplate("md5_path_cache", statistics)) { |
| 108 |
2/4✓ Branch 1 taken 878 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 878 times.
✗ Branch 5 not taken.
|
878 | dirent_negative_ = catalog::DirectoryEntry(catalog::kDirentNegative); |
| 109 | 878 | } | |
| 110 | |||
| 111 | 527 | bool Insert(const shash::Md5 &hash, const catalog::DirectoryEntry &dirent) { | |
| 112 |
1/2✓ Branch 3 taken 527 times.
✗ Branch 4 not taken.
|
1054 | LogCvmfs(kLogLru, kLogDebug, "insert md5 --> dirent: %s -> '%s'", |
| 113 |
1/2✓ Branch 2 taken 527 times.
✗ Branch 3 not taken.
|
1581 | hash.ToString().c_str(), dirent.name().c_str()); |
| 114 | 527 | const bool result = LruCache<shash::Md5, catalog::DirectoryEntry>::Insert( | |
| 115 | hash, dirent); | ||
| 116 | 527 | return result; | |
| 117 | } | ||
| 118 | |||
| 119 | 62 | bool InsertNegative(const shash::Md5 &hash) { | |
| 120 | 62 | const bool result = Insert(hash, dirent_negative_); | |
| 121 |
1/2✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
|
62 | if (result) |
| 122 | 62 | perf::Inc(counters_.n_insert_negative); | |
| 123 | 62 | return result; | |
| 124 | } | ||
| 125 | |||
| 126 | 899 | bool Lookup(const shash::Md5 &hash, catalog::DirectoryEntry *dirent, | |
| 127 | bool update_lru = true) { | ||
| 128 | 899 | const bool result = LruCache<shash::Md5, catalog::DirectoryEntry>::Lookup( | |
| 129 | hash, dirent); | ||
| 130 |
3/4✓ Branch 0 taken 372 times.
✓ Branch 1 taken 527 times.
✓ Branch 4 taken 899 times.
✗ Branch 5 not taken.
|
1798 | LogCvmfs(kLogLru, kLogDebug, "lookup md5 --> dirent: %s (%s)", |
| 131 | 1798 | hash.ToString().c_str(), result ? "hit" : "miss"); | |
| 132 | 899 | 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 |