OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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 |
| 5 // The cache is stored on disk as a collection of block-files, plus an index |
| 6 // plus a collection of external files. |
| 7 // |
| 8 // Any data blob bigger than kMaxBlockSize (disk_cache/addr.h) will be stored on |
| 9 // a separate file named f_xxx where x is a hexadecimal number. Shorter data |
| 10 // will be stored as a series of blocks on a block-file. In any case, CacheAddr |
| 11 // represents the address of the data inside the cache. |
| 12 // |
| 13 // The index is actually a collection of four files that store a hash table with |
| 14 // allocation bitmaps and backup data. Hash collisions are handled directly by |
| 15 // the table, which from some point of view behaves like a 4-way associative |
| 16 // cache with overflow buckets (so not really open addressing). |
| 17 // |
| 18 // Basically the hash table is a collection of buckets. The first part of the |
| 19 // table has a fixed number of buckets and it is directly addressed by the hash, |
| 20 // while the second part of the table (stored on a second file) has a variable |
| 21 // number of buckets. Each bucket stores up to four cells (each cell represents |
| 22 // a possibl entry). The index bitmap tracks the state of individual cells. |
| 23 // |
| 24 // The last element of the cache is the block-file. A block file is a file |
| 25 // designed to store blocks of data of a given size. For more details see |
| 26 // disk_cache/disk_format_base.h |
| 27 // |
| 28 // A new cache is initialized with a set of block files (named data_0 through |
| 29 // data_6), each one dedicated to store blocks of a given size or function. The |
| 30 // number at the end of the file name is the block file number (in decimal). |
| 31 // |
| 32 // There are three "special" types of blocks: normal entries, evicted entries |
| 33 // and control data for external files. |
| 34 // |
| 35 // The files that store internal information for the cache (blocks and index) |
| 36 // are memory mapped. They have a location that is signaled every time the |
| 37 // internal structures are modified, so it is possible to detect (most of the |
| 38 // time) when the process dies in the middle of an update. There are dedicated |
| 39 // backup files for cache bitmaps, used to detect entries out of date. |
| 40 |
| 41 #ifndef NET_DISK_CACHE_V3_DISK_FORMAT_V3_H_ |
| 42 #define NET_DISK_CACHE_V3_DISK_FORMAT_V3_H_ |
| 43 |
| 44 #include "base/basictypes.h" |
| 45 #include "net/base/net_export.h" |
| 46 #include "net/disk_cache/disk_format_base.h" |
| 47 |
| 48 namespace disk_cache { |
| 49 |
| 50 const int kIndexTablesize = 0x10000; |
| 51 const uint32 kIndexMagic = 0xC103CAC3; |
| 52 const uint32 kCurrentVersion = 0x30000; // Version 3.0. |
| 53 |
| 54 // Flags for a given cache. |
| 55 enum CacheFlags { |
| 56 CACHE_EVICTION_2 = 1, // Keep multiple lists for eviction. |
| 57 CACHE_EVICTED = 1 << 1 // Already evicted at least one entry. |
| 58 }; |
| 59 |
| 60 // Header for the master index file. |
| 61 struct NET_EXPORT_PRIVATE IndexHeaderV3 { |
| 62 IndexHeaderV3(); |
| 63 |
| 64 uint32 magic; |
| 65 uint32 version; |
| 66 int32 num_entries; // Number of entries currently stored. |
| 67 int32 num_bytes; // Total size of the stored data. |
| 68 int32 max_bytes; // Total maximum size of the stored data. |
| 69 int32 last_file; // Last external file created. |
| 70 int32 reserved1; |
| 71 CacheAddr stats; // Storage for usage data. |
| 72 int32 table_len; // Actual size of the table. |
| 73 int32 crash; // Signals a previous crash. |
| 74 int32 experiment; // Id of an ongoing test. |
| 75 uint32 flags; |
| 76 int32 used_cells; |
| 77 int32 max_bucket; |
| 78 uint64 create_time; // Creation time for this set of files. |
| 79 uint64 base_time; // Current base for timestamps. |
| 80 uint64 old_time; // Previous time used for timestamps. |
| 81 int32 max_block_file; |
| 82 int32 num_no_use_entries; |
| 83 int32 num_low_use_entries; |
| 84 int32 num_high_use_entries; |
| 85 int32 reserved; |
| 86 int32 num_evicted_entries; |
| 87 int32 pad[6]; |
| 88 }; |
| 89 |
| 90 const int kBaseBitmapBytes = 3968; |
| 91 // The IndexBitmap is directly saved to a file named index. The file grows in |
| 92 // page increments (4096 bytes), but all bits don't have to be in use at any |
| 93 // given time. The required file size can be computed from header.table_len. |
| 94 struct IndexBitmap { |
| 95 IndexHeaderV3 header; |
| 96 uint32 bitmap[kBaseBitmapBytes / 4]; // First page of the bitmap. |
| 97 }; |
| 98 COMPILE_ASSERT(sizeof(IndexBitmap) == 4096, bad_IndexHeader); |
| 99 |
| 100 // Possible states for a given entry. |
| 101 enum EntryState { |
| 102 ENTRY_FREE = 0, // Available slot. |
| 103 ENTRY_NEW, // The entry is being created. |
| 104 ENTRY_OPEN, // The entry is being accessed. |
| 105 ENTRY_MODIFIED, // The entry is being modified. |
| 106 ENTRY_DELETED, // The entry is being deleted. |
| 107 ENTRY_FIXING, // Inconsistent state. The entry is being verified. |
| 108 ENTRY_USED // The slot is in use (entry is present). |
| 109 }; |
| 110 |
| 111 enum EntryGroup { |
| 112 ENTRY_NO_USE = 0, // The entry has not been reused. |
| 113 ENTRY_LOW_USE, // The entry has low reuse. |
| 114 ENTRY_HIGH_USE, // The entry has high reuse. |
| 115 ENTRY_RESERVED, // Reserved for future use. |
| 116 ENTRY_EVICTED // The entry was deleted. |
| 117 }; |
| 118 |
| 119 #pragma pack(push, 1) |
| 120 struct IndexCell { |
| 121 void Clear() { memset(this, 0, sizeof(*this)); } |
| 122 |
| 123 unsigned int address : 22; // 0..21. |
| 124 unsigned int state : 3; // 22..24. |
| 125 unsigned int group : 3; // 25..27. |
| 126 unsigned int reuse : 4; // 28..31. |
| 127 uint16 hash; |
| 128 uint16 timestamp_and_hash; // 2 bits of hash. |
| 129 uint8 timestamp_and_sum; // 2 bits of sum. |
| 130 }; |
| 131 COMPILE_ASSERT(sizeof(IndexCell) == 9, bad_IndexCell); |
| 132 |
| 133 struct IndexBucket { |
| 134 IndexCell cells[4]; |
| 135 int32 next; |
| 136 unsigned int hash : 24; // 0..23. |
| 137 unsigned int flags : 8; // 24..31. Reserved. |
| 138 }; |
| 139 COMPILE_ASSERT(sizeof(IndexBucket) == 44, bad_IndexBucket); |
| 140 const int kBytesPerCell = 44 / 4; |
| 141 |
| 142 // The main cache index. Backed by a file named index_tb. |
| 143 struct Index { |
| 144 IndexBucket table[kIndexTablesize]; // Default size. Actual size controlled |
| 145 // by header.table_len. |
| 146 }; |
| 147 #pragma pack(pop) |
| 148 |
| 149 // Flags that can be applied to an entry. |
| 150 enum EntryFlags { |
| 151 PARENT_ENTRY = 1, // This entry has children (sparse) entries. |
| 152 CHILD_ENTRY = 1 << 1 // Child entry that stores sparse data. |
| 153 }; |
| 154 |
| 155 struct EntryRecord { |
| 156 EntryRecord(); |
| 157 uint32 hash; // Full hash of the key. |
| 158 uint32 pad1; |
| 159 uint8 reuse_count; // How often is this entry used. |
| 160 uint8 refetch_count; // How often is this fetched from the net. |
| 161 int8 state; // Current state. |
| 162 uint8 flags; // Any combination of EntryFlags. |
| 163 int32 key_len; |
| 164 int32 data_size[4]; // We can store up to 4 data streams for each |
| 165 CacheAddr data_addr[4]; // entry. |
| 166 uint32 data_hash[4]; |
| 167 uint64 creation_time; |
| 168 uint64 last_modified_time; |
| 169 uint64 last_access_time; |
| 170 int32 pad[3]; |
| 171 uint32 self_hash; // The hash of EntryRecord. |
| 172 }; |
| 173 COMPILE_ASSERT(sizeof(EntryRecord) == 104, bad_EntryRecord); |
| 174 |
| 175 struct ShortEntryRecord { |
| 176 ShortEntryRecord(); |
| 177 uint32 hash; // Full hash of the key. |
| 178 uint32 pad1; |
| 179 uint8 reuse_count; // How often is this entry used. |
| 180 uint8 refetch_count; // How often is this fetched from the net. |
| 181 int8 state; // Current state. |
| 182 uint8 flags; |
| 183 int32 key_len; |
| 184 uint64 last_access_time; |
| 185 uint32 long_hash[5]; |
| 186 uint32 self_hash; // The hash of ShortEntryRecord. |
| 187 }; |
| 188 COMPILE_ASSERT(sizeof(ShortEntryRecord) == 48, bad_ShortEntryRecord); |
| 189 |
| 190 } // namespace disk_cache |
| 191 |
| 192 #endif // NET_DISK_CACHE_V3_DISK_FORMAT_V3_H_ |
OLD | NEW |