simple_file_tracker.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef NET_DISK_CACHE_SIMPLE_SIMPLE_FILE_TRACKER_H_
  5. #define NET_DISK_CACHE_SIMPLE_SIMPLE_FILE_TRACKER_H_
  6. #include <stdint.h>
  7. #include <algorithm>
  8. #include <list>
  9. #include <memory>
  10. #include <unordered_map>
  11. #include <vector>
  12. #include "base/files/file.h"
  13. #include "base/memory/raw_ptr.h"
  14. #include "base/memory/ref_counted.h"
  15. #include "base/synchronization/lock.h"
  16. #include "net/base/net_export.h"
  17. #include "net/disk_cache/simple/simple_entry_format.h"
  18. namespace disk_cache {
  19. class BackendFileOperations;
  20. class SimpleSynchronousEntry;
  21. // This keeps track of all the files SimpleCache has open, across all the
  22. // backend instancess, in order to prevent us from running out of file
  23. // descriptors.
  24. // TODO(morlovich): Actually implement closing and re-opening of things if we
  25. // run out.
  26. //
  27. // This class is thread-safe.
  28. class NET_EXPORT_PRIVATE SimpleFileTracker {
  29. public:
  30. enum class SubFile { FILE_0, FILE_1, FILE_SPARSE };
  31. // A RAII helper that guards access to a file grabbed for use from
  32. // SimpleFileTracker::Acquire(). While it's still alive, if IsOK() is true,
  33. // then using the underlying base::File via get() or the -> operator will be
  34. // safe.
  35. //
  36. // This class is movable but not copyable. It should only be used from a
  37. // single logical sequence of execution, and should not outlive the
  38. // corresponding SimpleSynchronousEntry.
  39. class NET_EXPORT_PRIVATE FileHandle {
  40. public:
  41. FileHandle();
  42. FileHandle(FileHandle&& other);
  43. FileHandle(const FileHandle&) = delete;
  44. FileHandle& operator=(const FileHandle&) = delete;
  45. ~FileHandle();
  46. FileHandle& operator=(FileHandle&& other);
  47. base::File* operator->() const;
  48. base::File* get() const;
  49. // Returns true if this handle points to a valid file. This should normally
  50. // be the first thing called on the object, after getting it from
  51. // SimpleFileTracker::Acquire.
  52. bool IsOK() const;
  53. private:
  54. friend class SimpleFileTracker;
  55. FileHandle(SimpleFileTracker* file_tracker,
  56. const SimpleSynchronousEntry* entry,
  57. SimpleFileTracker::SubFile subfile,
  58. base::File* file);
  59. // All the pointer fields are nullptr in the default/moved away from form.
  60. raw_ptr<SimpleFileTracker> file_tracker_ = nullptr;
  61. raw_ptr<const SimpleSynchronousEntry> entry_ = nullptr;
  62. SimpleFileTracker::SubFile subfile_;
  63. raw_ptr<base::File> file_ = nullptr;
  64. };
  65. struct EntryFileKey {
  66. EntryFileKey() = default;
  67. explicit EntryFileKey(uint64_t hash) : entry_hash(hash) {}
  68. uint64_t entry_hash = 0;
  69. // 0 means this a non-doomed, active entry, for its backend that will be
  70. // checked on OpenEntry(key) where hash(key) = entry_hash. Other values of
  71. // |doom_generation| are used to generate distinct file names for entries
  72. // that have been Doom()ed, either by explicit API call by the client or
  73. // internal operation (eviction, collisions, etc.)
  74. uint64_t doom_generation = 0;
  75. };
  76. // The default limit here is half of what's available on our target OS where
  77. // Chrome has the lowest limit.
  78. explicit SimpleFileTracker(int file_limit = 512);
  79. SimpleFileTracker(const SimpleFileTracker&) = delete;
  80. SimpleFileTracker& operator=(const SimpleFileTracker&) = delete;
  81. ~SimpleFileTracker();
  82. // Established |file| as what's backing |subfile| for |owner|. This is
  83. // intended to be called when SimpleSynchronousEntry first sets up the file to
  84. // transfer its ownership to SimpleFileTracker. Any Register() call must be
  85. // eventually followed by a corresponding Close() call before the |owner| is
  86. // destroyed. |file->IsValid()| must be true.
  87. void Register(const SimpleSynchronousEntry* owner,
  88. SubFile subfile,
  89. std::unique_ptr<base::File> file);
  90. // Lends out a file to SimpleSynchronousEntry for use. SimpleFileTracker
  91. // will ensure that it doesn't close the file until the handle is destroyed.
  92. // The caller should check .IsOK() on the returned value before using it, as
  93. // it's possible that the file had to be closed and re-opened due to FD
  94. // pressure, and that open may have failed. This should not be called twice
  95. // with the exact same arguments until the handle returned from the previous
  96. // such call is destroyed.
  97. FileHandle Acquire(BackendFileOperations* file_operations,
  98. const SimpleSynchronousEntry* owner,
  99. SubFile subfile);
  100. // Tells SimpleFileTracker that SimpleSynchronousEntry will not be interested
  101. // in the file further, so it can be closed and forgotten about. It's OK to
  102. // call this while a handle to the file is alive, in which case the effect
  103. // takes place after the handle is destroyed.
  104. // If Close() has been called and the handle to the file is no longer alive,
  105. // a new backing file can be established by calling Register() again.
  106. void Close(const SimpleSynchronousEntry* owner, SubFile file);
  107. // Updates key->doom_generation to one not in use for the hash; it's the
  108. // caller's responsibility to update file names accordingly, and to prevent
  109. // others from stomping on things while this is going on. SimpleBackendImpl's
  110. // entries_pending_doom_ is the mechanism for protecting this action from
  111. // races.
  112. void Doom(const SimpleSynchronousEntry* owner, EntryFileKey* key);
  113. // Returns true if there is no in-memory state around, e.g. everything got
  114. // cleaned up. This is a test-only method since this object is expected to be
  115. // shared between multiple threads, in which case its return value may be
  116. // outdated the moment it's returned.
  117. bool IsEmptyForTesting();
  118. private:
  119. struct TrackedFiles {
  120. // We can potentially run through this state machine multiple times for
  121. // FILE_1, as that's often missing, so SimpleSynchronousEntry can sometimes
  122. // close and remove the file for an empty stream, then re-open it on actual
  123. // data.
  124. enum State {
  125. TF_NO_REGISTRATION = 0,
  126. TF_REGISTERED = 1,
  127. TF_ACQUIRED = 2,
  128. TF_ACQUIRED_PENDING_CLOSE = 3,
  129. };
  130. NET_EXPORT_PRIVATE TrackedFiles();
  131. NET_EXPORT_PRIVATE ~TrackedFiles();
  132. // True if this isn't keeping track of anything any more.
  133. bool Empty() const;
  134. // True if this has open files. Note that this is not the same as !Empty()
  135. // as this may be false when an entry had its files temporarily closed, but
  136. // is still relevant.
  137. bool HasOpenFiles() const;
  138. // We use pointers to SimpleSynchronousEntry two ways:
  139. // 1) As opaque keys. This is handy as it avoids having to compare paths in
  140. // case multiple backends use the same key. Since we access the
  141. // bookkeeping under |lock_|
  142. //
  143. // 2) To get info on the caller of our operation.
  144. // Accessing |owner| from any other TrackedFiles would be unsafe (as it
  145. // may be doing its own thing in a different thread).
  146. raw_ptr<const SimpleSynchronousEntry> owner;
  147. EntryFileKey key;
  148. // Some of these may be nullptr, if they are not open. Non-null pointers
  149. // to files that are not valid will not be stored here.
  150. // Note that these are stored indirect since we hand out pointers to these,
  151. // and we don't want those to become invalid if some other thread appends
  152. // things here.
  153. std::unique_ptr<base::File> files[kSimpleEntryTotalFileCount];
  154. State state[kSimpleEntryTotalFileCount];
  155. std::list<TrackedFiles*>::iterator position_in_lru;
  156. // true if position_in_lru is valid. For entries where we closed everything,
  157. // we try not to keep them in the LRU so that we don't have to constantly
  158. // rescan them.
  159. bool in_lru = false;
  160. };
  161. // Marks the file that was previously returned by Acquire as eligible for
  162. // closing again. Called by ~FileHandle.
  163. void Release(const SimpleSynchronousEntry* owner, SubFile subfile);
  164. // Precondition: entry for given |owner| must already be in tracked_files_
  165. TrackedFiles* Find(const SimpleSynchronousEntry* owner);
  166. // Handles state transition of closing file (when we are not deferring it),
  167. // and moves the file out. Note that this may delete |*owners_files|.
  168. std::unique_ptr<base::File> PrepareClose(TrackedFiles* owners_files,
  169. int file_index);
  170. // If too many files are open, picks some to close, and moves them to
  171. // |*files_to_close|, updating other state as appropriate.
  172. void CloseFilesIfTooManyOpen(
  173. std::vector<std::unique_ptr<base::File>>* files_to_close);
  174. // Tries to reopen given file, updating |*owners_files| if successful.
  175. void ReopenFile(BackendFileOperations* file_operations,
  176. TrackedFiles* owners_files,
  177. SubFile subfile);
  178. // Makes sure the entry is marked as most recently used, adding it to LRU
  179. // if needed.
  180. void EnsureInFrontOfLRU(TrackedFiles* owners_files);
  181. base::Lock lock_;
  182. std::unordered_map<uint64_t, std::vector<std::unique_ptr<TrackedFiles>>>
  183. tracked_files_;
  184. std::list<TrackedFiles*> lru_;
  185. int file_limit_;
  186. // How many actually open files we are using.
  187. // Note that when a thread commits to closing a file, but hasn't actually
  188. // executed the close yet, the file is no longer counted as open here, so this
  189. // might be a little off. This should be OK as long as file_limit_ is set
  190. // conservatively, considering SimpleCache's parallelism is bounded by a low
  191. // number of threads, and getting it exact would require re-acquiring the
  192. // lock after closing the file.
  193. int open_files_ = 0;
  194. };
  195. } // namespace disk_cache
  196. #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_FILE_TRACKER_H_