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 // For a general description of the files used by the cache see file_format.h. |
6 // file plus a collection of external files. | |
7 // | 6 // |
8 // Any data blob bigger than kMaxBlockSize (net/addr.h) will be stored on a | 7 // A block file is a file designed to store blocks of data of a given size. It |
9 // separate file named f_xxx where x is a hexadecimal number. Shorter data will | 8 // is able to store data that spans from one to four consecutive "blocks", and |
10 // be stored as a series of blocks on a block-file. In any case, CacheAddr | 9 // it grows as needed to store up to approximately 65000 blocks. It has a fixed |
11 // represents the address of the data inside the cache. | 10 // size header used for book keeping such as tracking free of blocks on the |
12 // | 11 // file. For example, a block-file for 1KB blocks will grow from 8KB when |
13 // The index file is just a simple hash table that maps a particular entry to | 12 // totally empty to about 64MB when completely full. At that point, data blocks |
14 // a CacheAddr value. Linking for a given hash bucket is handled internally | 13 // of 1KB will be stored on a second block file that will store the next set of |
15 // by the cache entry. | 14 // 65000 blocks. The first file contains the number of the second file, and the |
16 // | 15 // second file contains the number of a third file, created when the second file |
17 // The last element of the cache is the block-file. A block file is a file | 16 // reaches its limit. It is important to remember that no matter how long the |
18 // designed to store blocks of data of a given size. It is able to store data | 17 // chain of files is, any given block can be located directly by its address, |
19 // that spans from one to four consecutive "blocks", and it grows as needed to | 18 // which contains the file number and starting block inside the file. |
20 // store up to approximately 65000 blocks. It has a fixed size header used for | |
21 // book keeping such as tracking free of blocks on the file. For example, a | |
22 // block-file for 1KB blocks will grow from 8KB when totally empty to about 64MB | |
23 // when completely full. At that point, data blocks of 1KB will be stored on a | |
24 // second block file that will store the next set of 65000 blocks. The first | |
25 // file contains the number of the second file, and the second file contains the | |
26 // number of a third file, created when the second file reaches its limit. It is | |
27 // important to remember that no matter how long the chain of files is, any | |
28 // given block can be located directly by its address, which contains the file | |
29 // number and starting block inside the file. | |
30 // | |
31 // A new cache is initialized with four block files (named data_0 through | |
32 // data_3), each one dedicated to store blocks of a given size. The number at | |
33 // the end of the file name is the block file number (in decimal). | |
34 // | |
35 // There are two "special" types of blocks: an entry and a rankings node. An | |
36 // entry keeps track of all the information related to the same cache entry, | |
37 // such as the key, hash value, data pointers etc. A rankings node keeps track | |
38 // of the information that is updated frequently for a given entry, such as its | |
39 // location on the LRU lists, last access time etc. | |
40 // | |
41 // The files that store internal information for the cache (blocks and index) | |
42 // are at least partially memory mapped. They have a location that is signaled | |
43 // every time the internal structures are modified, so it is possible to detect | |
44 // (most of the time) when the process dies in the middle of an update. | |
45 // | |
46 // In order to prevent dirty data to be used as valid (after a crash), every | |
47 // cache entry has a dirty identifier. Each running instance of the cache keeps | |
48 // a separate identifier (maintained on the "this_id" header field) that is used | |
49 // to mark every entry that is created or modified. When the entry is closed, | |
50 // and all the data can be trusted, the dirty flag is cleared from the entry. | |
51 // When the cache encounters an entry whose identifier is different than the one | |
52 // being currently used, it means that the entry was not properly closed on a | |
53 // previous run, so it is discarded. | |
54 | 19 |
55 #ifndef NET_DISK_CACHE_DISK_FORMAT_H_ | 20 #ifndef NET_DISK_CACHE_DISK_FORMAT_BASE_H_ |
56 #define NET_DISK_CACHE_DISK_FORMAT_H_ | 21 #define NET_DISK_CACHE_DISK_FORMAT_BASE_H_ |
57 | 22 |
58 #include "base/basictypes.h" | 23 #include "base/basictypes.h" |
59 #include "net/base/net_export.h" | 24 #include "net/base/net_export.h" |
60 | 25 |
61 namespace disk_cache { | 26 namespace disk_cache { |
62 | 27 |
63 typedef uint32 CacheAddr; | 28 typedef uint32 CacheAddr; |
64 | 29 |
65 const int kIndexTablesize = 0x10000; | 30 const uint32 kBlockVersion2 = 0x20000; // Version 2.0. |
66 const uint32 kIndexMagic = 0xC103CAC3; | 31 const uint32 kBlockCurrentVersion = 0x30000; // Version 3.0. |
67 const uint32 kCurrentVersion = 0x20000; // Version 2.0. | |
68 | |
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. | |
82 struct NET_EXPORT_PRIVATE IndexHeader { | |
83 IndexHeader(); | |
84 | |
85 uint32 magic; | |
86 uint32 version; | |
87 int32 num_entries; // Number of entries currently stored. | |
88 int32 num_bytes; // Total size of the stored data. | |
89 int32 last_file; // Last external file created. | |
90 int32 this_id; // Id for all entries being changed (dirty flag). | |
91 CacheAddr stats; // Storage for usage data. | |
92 int32 table_len; // Actual size of the table (0 == kIndexTablesize). | |
93 int32 crash; // Signals a previous crash. | |
94 int32 experiment; // Id of an ongoing test. | |
95 uint64 create_time; // Creation time for this set of files. | |
96 int32 pad[52]; | |
97 LruData lru; // Eviction control data. | |
98 }; | |
99 | |
100 // The structure of the whole index file. | |
101 struct Index { | |
102 IndexHeader header; | |
103 CacheAddr table[kIndexTablesize]; // Default size. Actual size controlled | |
104 // by header.table_len. | |
105 }; | |
106 | |
107 // 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 | |
109 // 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 | |
111 // file. | |
112 struct EntryStore { | |
113 uint32 hash; // Full hash of the key. | |
114 CacheAddr next; // Next entry with the same hash or bucket. | |
115 CacheAddr rankings_node; // Rankings node for this entry. | |
116 int32 reuse_count; // How often is this entry used. | |
117 int32 refetch_count; // How often is this fetched from the net. | |
118 int32 state; // Current state. | |
119 uint64 creation_time; | |
120 int32 key_len; | |
121 CacheAddr long_key; // Optional address of a long key. | |
122 int32 data_size[4]; // We can store up to 4 data streams for each | |
123 CacheAddr data_addr[4]; // entry. | |
124 uint32 flags; // Any combination of EntryFlags. | |
125 int32 pad[4]; | |
126 uint32 self_hash; // The hash of EntryStore up to this point. | |
127 char key[256 - 24 * 4]; // null terminated | |
128 }; | |
129 | |
130 COMPILE_ASSERT(sizeof(EntryStore) == 256, bad_EntyStore); | |
131 const int kMaxInternalKeyLength = 4 * sizeof(EntryStore) - | |
132 offsetof(EntryStore, key) - 1; | |
133 | |
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 | 32 |
162 const uint32 kBlockMagic = 0xC104CAC3; | 33 const uint32 kBlockMagic = 0xC104CAC3; |
163 const int kBlockHeaderSize = 8192; // Two pages: almost 64k entries | 34 const int kBlockHeaderSize = 8192; // Two pages: almost 64k entries |
164 const int kMaxBlocks = (kBlockHeaderSize - 80) * 8; | 35 const int kMaxBlocks = (kBlockHeaderSize - 80) * 8; |
| 36 const int kNumExtraBlocks = 1024; |
165 | 37 |
166 // Bitmap to track used blocks on a block-file. | 38 // Bitmap to track used blocks on a block-file. |
167 typedef uint32 AllocBitmap[kMaxBlocks / 32]; | 39 typedef uint32 AllocBitmap[kMaxBlocks / 32]; |
168 | 40 |
169 // A block-file is the file used to store information in blocks (could be | 41 // A block-file is the file used to store information in blocks (could be |
170 // EntryStore blocks, RankingsNode blocks or user-data blocks). | 42 // EntryStore blocks, RankingsNode blocks or user-data blocks). |
171 // We store entries that can expand for up to 4 consecutive blocks, and keep | 43 // 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 | 44 // 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 | 45 // 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 | 46 // where did we find the last entry of that type (to avoid searching the bitmap |
175 // from the beginning every time). | 47 // from the beginning every time). |
| 48 // |
| 49 // The only difference between versions 2 and 3 is that the latter stores the |
| 50 // header and allocation file in a dedicated file, and the data in another file, |
| 51 // instead of using a single file for both things (as does version 2). |
176 // This Structure is the header of a block-file: | 52 // This Structure is the header of a block-file: |
177 struct NET_EXPORT_PRIVATE BlockFileHeader { | 53 struct NET_EXPORT_PRIVATE BlockFileHeader { |
178 BlockFileHeader(); | |
179 | |
180 uint32 magic; | 54 uint32 magic; |
181 uint32 version; | 55 uint32 version; |
182 int16 this_file; // Index of this file. | 56 int16 this_file; // Index of this file. |
183 int16 next_file; // Next file when this one is full. | 57 int16 next_file; // Next file when this one is full. |
184 int32 entry_size; // Size of the blocks of this file. | 58 int32 entry_size; // Size of the blocks of this file. |
185 int32 num_entries; // Number of stored entries. | 59 int32 num_entries; // Number of stored entries. |
186 int32 max_entries; // Current maximum number of entries. | 60 int32 max_entries; // Current maximum number of entries. |
187 int32 empty[4]; // Counters of empty entries for each type. | 61 int32 empty[4]; // Counters of empty entries for each type. |
188 int32 hints[4]; // Last used position for each entry type. | 62 int32 hints[4]; // Last used position for each entry type. |
189 volatile int32 updating; // Keep track of updates to the header. | 63 volatile int32 updating; // Keep track of updates to the header. |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 // and as large as 8 KB. | 126 // and as large as 8 KB. |
253 }; | 127 }; |
254 | 128 |
255 // The number of blocks stored by a child entry. | 129 // The number of blocks stored by a child entry. |
256 const int kNumSparseBits = 1024; | 130 const int kNumSparseBits = 1024; |
257 COMPILE_ASSERT(sizeof(SparseData) == sizeof(SparseHeader) + kNumSparseBits / 8, | 131 COMPILE_ASSERT(sizeof(SparseData) == sizeof(SparseHeader) + kNumSparseBits / 8, |
258 Invalid_SparseData_bitmap); | 132 Invalid_SparseData_bitmap); |
259 | 133 |
260 } // namespace disk_cache | 134 } // namespace disk_cache |
261 | 135 |
262 #endif // NET_DISK_CACHE_DISK_FORMAT_H_ | 136 #endif // NET_DISK_CACHE_DISK_FORMAT_BASE_H_ |
OLD | NEW |