| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // The cache is stored on disk as a collection of block-files, plus an index | 5 // The cache is stored on disk as a collection of block-files, plus an index |
| 6 // file plus a collection of external files. | 6 // file plus a collection of external files. |
| 7 // | 7 // |
| 8 // Any data blob bigger than kMaxBlockSize (net/addr.h) will be stored on a | 8 // Any data blob bigger than kMaxBlockSize (net/addr.h) will be stored on a |
| 9 // separate file named f_xxx where x is a hexadecimal number. Shorter data will | 9 // separate file named f_xxx where x is a hexadecimal number. Shorter data will |
| 10 // be stored as a series of blocks on a block-file. In any case, CacheAddr | 10 // be stored as a series of blocks on a block-file. In any case, CacheAddr |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 // previous run, so it is discarded. | 53 // previous run, so it is discarded. |
| 54 | 54 |
| 55 #ifndef NET_DISK_CACHE_DISK_FORMAT_H_ | 55 #ifndef NET_DISK_CACHE_DISK_FORMAT_H_ |
| 56 #define NET_DISK_CACHE_DISK_FORMAT_H_ | 56 #define NET_DISK_CACHE_DISK_FORMAT_H_ |
| 57 | 57 |
| 58 #include "base/basictypes.h" | 58 #include "base/basictypes.h" |
| 59 #include "net/base/net_export.h" | 59 #include "net/base/net_export.h" |
| 60 | 60 |
| 61 namespace disk_cache { | 61 namespace disk_cache { |
| 62 | 62 |
| 63 typedef uint32 CacheAddr; | |
| 64 | |
| 65 const int kIndexTablesize = 0x10000; | |
| 66 const uint32 kIndexMagic = 0xC103CAC3; | |
| 67 const uint32 kCurrentVersion = 0x20000; // Version 2.0. | 63 const uint32 kCurrentVersion = 0x20000; // Version 2.0. |
| 68 | 64 |
| 69 struct LruData { | |
| 70 int32 pad1[2]; | |
| 71 int32 filled; // Flag to tell when we filled the cache. | |
| 72 int32 sizes[5]; | |
| 73 CacheAddr heads[5]; | |
| 74 CacheAddr tails[5]; | |
| 75 CacheAddr transaction; // In-flight operation target. | |
| 76 int32 operation; // Actual in-flight operation. | |
| 77 int32 operation_list; // In-flight operation list. | |
| 78 int32 pad2[7]; | |
| 79 }; | |
| 80 | |
| 81 // Header for the master index file. | 65 // Header for the master index file. |
| 82 struct NET_EXPORT_PRIVATE IndexHeader { | 66 struct NET_EXPORT_PRIVATE IndexHeader { |
| 83 IndexHeader(); | 67 IndexHeader(); |
| 84 | 68 |
| 85 uint32 magic; | 69 uint32 magic; |
| 86 uint32 version; | 70 uint32 version; |
| 87 int32 num_entries; // Number of entries currently stored. | 71 int32 num_entries; // Number of entries currently stored. |
| 88 int32 num_bytes; // Total size of the stored data. | 72 int32 num_bytes; // Total size of the stored data. |
| 89 int32 last_file; // Last external file created. | 73 int32 last_file; // Last external file created. |
| 90 int32 this_id; // Id for all entries being changed (dirty flag). | 74 int32 this_id; // Id for all entries being changed (dirty flag). |
| 91 CacheAddr stats; // Storage for usage data. | 75 CacheAddr stats; // Storage for usage data. |
| 92 int32 table_len; // Actual size of the table (0 == kIndexTablesize). | 76 int32 table_len; // Actual size of the table (0 == kIndexTablesize). |
| 93 int32 crash; // Signals a previous crash. | 77 int32 crash; // Signals a previous crash. |
| 94 int32 experiment; // Id of an ongoing test. | 78 int32 experiment; // Id of an ongoing test. |
| 95 uint64 create_time; // Creation time for this set of files. | 79 uint64 create_time; // Creation time for this set of files. |
| 96 int32 pad[52]; | 80 int32 pad[52]; |
| 97 LruData lru; // Eviction control data. | 81 LruData lru; // Eviction control data. |
| 98 }; | 82 }; |
| 99 | 83 |
| 100 // The structure of the whole index file. | 84 // The structure of the whole index file. |
| 101 struct Index { | 85 struct Index { |
| 102 IndexHeader header; | 86 IndexHeader header; |
| 103 CacheAddr table[kIndexTablesize]; // Default size. Actual size controlled | 87 CacheAddr table[kIndexTablesize]; // Default size. Actual size controlled |
| 104 // by header.table_len. | 88 // by header.table_len. |
| 105 }; | 89 }; |
| 106 | 90 |
| 91 // Possible states for a given entry. |
| 92 enum EntryState { |
| 93 ENTRY_NORMAL = 0, |
| 94 ENTRY_EVICTED, // The entry was recently evicted from the cache. |
| 95 ENTRY_DOOMED // The entry was doomed. |
| 96 }; |
| 97 |
| 98 // Flags that can be applied to an entry. |
| 99 enum EntryFlags { |
| 100 PARENT_ENTRY = 1, // This entry has children (sparse) entries. |
| 101 CHILD_ENTRY = 1 << 1 // Child entry that stores sparse data. |
| 102 }; |
| 103 |
| 107 // Main structure for an entry on the backing storage. If the key is longer than | 104 // Main structure for an entry on the backing storage. If the key is longer than |
| 108 // what can be stored on this structure, it will be extended on consecutive | 105 // what can be stored on this structure, it will be extended on consecutive |
| 109 // blocks (adding 256 bytes each time), up to 4 blocks (1024 - 32 - 1 chars). | 106 // blocks (adding 256 bytes each time), up to 4 blocks (1024 - 32 - 1 chars). |
| 110 // After that point, the whole key will be stored as a data block or external | 107 // After that point, the whole key will be stored as a data block or external |
| 111 // file. | 108 // file. |
| 112 struct EntryStore { | 109 struct EntryStore { |
| 113 uint32 hash; // Full hash of the key. | 110 uint32 hash; // Full hash of the key. |
| 114 CacheAddr next; // Next entry with the same hash or bucket. | 111 CacheAddr next; // Next entry with the same hash or bucket. |
| 115 CacheAddr rankings_node; // Rankings node for this entry. | 112 CacheAddr rankings_node; // Rankings node for this entry. |
| 116 int32 reuse_count; // How often is this entry used. | 113 int32 reuse_count; // How often is this entry used. |
| 117 int32 refetch_count; // How often is this fetched from the net. | 114 int32 refetch_count; // How often is this fetched from the net. |
| 118 int32 state; // Current state. | 115 int32 state; // Current state. |
| 119 uint64 creation_time; | 116 uint64 creation_time; |
| 120 int32 key_len; | 117 int32 key_len; |
| 121 CacheAddr long_key; // Optional address of a long key. | 118 CacheAddr long_key; // Optional address of a long key. |
| 122 int32 data_size[4]; // We can store up to 4 data streams for each | 119 int32 data_size[4]; // We can store up to 4 data streams for each |
| 123 CacheAddr data_addr[4]; // entry. | 120 CacheAddr data_addr[4]; // entry. |
| 124 uint32 flags; // Any combination of EntryFlags. | 121 uint32 flags; // Any combination of EntryFlags. |
| 125 int32 pad[4]; | 122 int32 pad[4]; |
| 126 uint32 self_hash; // The hash of EntryStore up to this point. | 123 uint32 self_hash; // The hash of EntryStore up to this point. |
| 127 char key[256 - 24 * 4]; // null terminated | 124 char key[256 - 24 * 4]; // null terminated |
| 128 }; | 125 }; |
| 129 | 126 |
| 130 COMPILE_ASSERT(sizeof(EntryStore) == 256, bad_EntyStore); | 127 COMPILE_ASSERT(sizeof(EntryStore) == 256, bad_EntyStore); |
| 131 const int kMaxInternalKeyLength = 4 * sizeof(EntryStore) - | 128 const int kMaxInternalKeyLength = 4 * sizeof(EntryStore) - |
| 132 offsetof(EntryStore, key) - 1; | 129 offsetof(EntryStore, key) - 1; |
| 133 | 130 |
| 134 // Possible states for a given entry. | |
| 135 enum EntryState { | |
| 136 ENTRY_NORMAL = 0, | |
| 137 ENTRY_EVICTED, // The entry was recently evicted from the cache. | |
| 138 ENTRY_DOOMED // The entry was doomed. | |
| 139 }; | |
| 140 | |
| 141 // Flags that can be applied to an entry. | |
| 142 enum EntryFlags { | |
| 143 PARENT_ENTRY = 1, // This entry has children (sparse) entries. | |
| 144 CHILD_ENTRY = 1 << 1 // Child entry that stores sparse data. | |
| 145 }; | |
| 146 | |
| 147 #pragma pack(push, 4) | |
| 148 // Rankings information for a given entry. | |
| 149 struct RankingsNode { | |
| 150 uint64 last_used; // LRU info. | |
| 151 uint64 last_modified; // LRU info. | |
| 152 CacheAddr next; // LRU list. | |
| 153 CacheAddr prev; // LRU list. | |
| 154 CacheAddr contents; // Address of the EntryStore. | |
| 155 int32 dirty; // The entry is being modifyied. | |
| 156 uint32 self_hash; // RankingsNode's hash. | |
| 157 }; | |
| 158 #pragma pack(pop) | |
| 159 | |
| 160 COMPILE_ASSERT(sizeof(RankingsNode) == 36, bad_RankingsNode); | |
| 161 | |
| 162 const uint32 kBlockMagic = 0xC104CAC3; | |
| 163 const int kBlockHeaderSize = 8192; // Two pages: almost 64k entries | |
| 164 const int kMaxBlocks = (kBlockHeaderSize - 80) * 8; | |
| 165 | |
| 166 // Bitmap to track used blocks on a block-file. | |
| 167 typedef uint32 AllocBitmap[kMaxBlocks / 32]; | |
| 168 | |
| 169 // A block-file is the file used to store information in blocks (could be | |
| 170 // EntryStore blocks, RankingsNode blocks or user-data blocks). | |
| 171 // We store entries that can expand for up to 4 consecutive blocks, and keep | |
| 172 // counters of the number of blocks available for each type of entry. For | |
| 173 // instance, an entry of 3 blocks is an entry of type 3. We also keep track of | |
| 174 // where did we find the last entry of that type (to avoid searching the bitmap | |
| 175 // from the beginning every time). | |
| 176 // This Structure is the header of a block-file: | |
| 177 struct NET_EXPORT_PRIVATE BlockFileHeader { | |
| 178 BlockFileHeader(); | |
| 179 | |
| 180 uint32 magic; | |
| 181 uint32 version; | |
| 182 int16 this_file; // Index of this file. | |
| 183 int16 next_file; // Next file when this one is full. | |
| 184 int32 entry_size; // Size of the blocks of this file. | |
| 185 int32 num_entries; // Number of stored entries. | |
| 186 int32 max_entries; // Current maximum number of entries. | |
| 187 int32 empty[4]; // Counters of empty entries for each type. | |
| 188 int32 hints[4]; // Last used position for each entry type. | |
| 189 volatile int32 updating; // Keep track of updates to the header. | |
| 190 int32 user[5]; | |
| 191 AllocBitmap allocation_map; | |
| 192 }; | |
| 193 | |
| 194 COMPILE_ASSERT(sizeof(BlockFileHeader) == kBlockHeaderSize, bad_header); | |
| 195 | |
| 196 // Sparse data support: | |
| 197 // We keep a two level hierarchy to enable sparse data for an entry: the first | |
| 198 // level consists of using separate "child" entries to store ranges of 1 MB, | |
| 199 // and the second level stores blocks of 1 KB inside each child entry. | |
| 200 // | |
| 201 // Whenever we need to access a particular sparse offset, we first locate the | |
| 202 // child entry that stores that offset, so we discard the 20 least significant | |
| 203 // bits of the offset, and end up with the child id. For instance, the child id | |
| 204 // to store the first megabyte is 0, and the child that should store offset | |
| 205 // 0x410000 has an id of 4. | |
| 206 // | |
| 207 // The child entry is stored the same way as any other entry, so it also has a | |
| 208 // name (key). The key includes a signature to be able to identify children | |
| 209 // created for different generations of the same resource. In other words, given | |
| 210 // that a given sparse entry can have a large number of child entries, and the | |
| 211 // resource can be invalidated and replaced with a new version at any time, it | |
| 212 // is important to be sure that a given child actually belongs to certain entry. | |
| 213 // | |
| 214 // The full name of a child entry is composed with a prefix ("Range_"), and two | |
| 215 // hexadecimal 64-bit numbers at the end, separated by semicolons. The first | |
| 216 // number is the signature of the parent key, and the second number is the child | |
| 217 // id as described previously. The signature itself is also stored internally by | |
| 218 // the child and the parent entries. For example, a sparse entry with a key of | |
| 219 // "sparse entry name", and a signature of 0x052AF76, may have a child entry | |
| 220 // named "Range_sparse entry name:052af76:4", which stores data in the range | |
| 221 // 0x400000 to 0x4FFFFF. | |
| 222 // | |
| 223 // Each child entry keeps track of all the 1 KB blocks that have been written | |
| 224 // to the entry, but being a regular entry, it will happily return zeros for any | |
| 225 // read that spans data not written before. The actual sparse data is stored in | |
| 226 // one of the data streams of the child entry (at index 1), while the control | |
| 227 // information is stored in another stream (at index 2), both by parents and | |
| 228 // the children. | |
| 229 | |
| 230 // This structure contains the control information for parent and child entries. | |
| 231 // It is stored at offset 0 of the data stream with index 2. | |
| 232 // It is possible to write to a child entry in a way that causes the last block | |
| 233 // to be only partialy filled. In that case, last_block and last_block_len will | |
| 234 // keep track of that block. | |
| 235 struct SparseHeader { | |
| 236 int64 signature; // The parent and children signature. | |
| 237 uint32 magic; // Structure identifier (equal to kIndexMagic). | |
| 238 int32 parent_key_len; // Key length for the parent entry. | |
| 239 int32 last_block; // Index of the last written block. | |
| 240 int32 last_block_len; // Lenght of the last written block. | |
| 241 int32 dummy[10]; | |
| 242 }; | |
| 243 | |
| 244 // The SparseHeader will be followed by a bitmap, as described by this | |
| 245 // structure. | |
| 246 struct SparseData { | |
| 247 SparseHeader header; | |
| 248 uint32 bitmap[32]; // Bitmap representation of known children (if this | |
| 249 // is a parent entry), or used blocks (for child | |
| 250 // entries. The size is fixed for child entries but | |
| 251 // not for parents; it can be as small as 4 bytes | |
| 252 // and as large as 8 KB. | |
| 253 }; | |
| 254 | |
| 255 // The number of blocks stored by a child entry. | |
| 256 const int kNumSparseBits = 1024; | |
| 257 COMPILE_ASSERT(sizeof(SparseData) == sizeof(SparseHeader) + kNumSparseBits / 8, | |
| 258 Invalid_SparseData_bitmap); | |
| 259 | |
| 260 } // namespace disk_cache | 131 } // namespace disk_cache |
| 261 | 132 |
| 262 #endif // NET_DISK_CACHE_DISK_FORMAT_H_ | 133 #endif // NET_DISK_CACHE_DISK_FORMAT_H_ |
| OLD | NEW |