@@ -238,6 +238,36 @@ func NewLevelDBDatabaseWithFreezer(file string, cache int, handles int, freezer
238238 return frdb , nil
239239}
240240
241+ type counter uint64
242+
243+ func (c counter ) String () string {
244+ return fmt .Sprintf ("%d" , c )
245+ }
246+
247+ func (c counter ) Percentage (current uint64 ) string {
248+ return fmt .Sprintf ("%d" , current * 100 / uint64 (c ))
249+ }
250+
251+ // stat stores sizes and count for a parameter
252+ type stat struct {
253+ size common.StorageSize
254+ count counter
255+ }
256+
257+ // Add size to the stat and increase the counter by 1
258+ func (s * stat ) Add (size common.StorageSize ) {
259+ s .size += size
260+ s .count ++
261+ }
262+
263+ func (s * stat ) Size () string {
264+ return s .size .String ()
265+ }
266+
267+ func (s * stat ) Count () string {
268+ return s .count .String ()
269+ }
270+
241271// InspectDatabase traverses the entire database and checks the size
242272// of all different categories of data.
243273func InspectDatabase (db ethdb.Database ) error {
@@ -250,36 +280,38 @@ func InspectDatabase(db ethdb.Database) error {
250280 logged = time .Now ()
251281
252282 // Key-value store statistics
253- total common.StorageSize
254- headerSize common.StorageSize
255- bodySize common.StorageSize
256- receiptSize common.StorageSize
257- tdSize common.StorageSize
258- numHashPairing common.StorageSize
259- hashNumPairing common.StorageSize
260- trieSize common.StorageSize
261- codeSize common.StorageSize
262- txlookupSize common.StorageSize
263- accountSnapSize common.StorageSize
264- storageSnapSize common.StorageSize
265- preimageSize common.StorageSize
266- bloomBitsSize common.StorageSize
267- cliqueSnapsSize common.StorageSize
283+ headers stat
284+ bodies stat
285+ receipts stat
286+ tds stat
287+ numHashPairings stat
288+ hashNumPairings stat
289+ tries stat
290+ codes stat
291+ txLookups stat
292+ accountSnaps stat
293+ storageSnaps stat
294+ preimages stat
295+ bloomBits stat
296+ cliqueSnaps stat
268297
269298 // Ancient store statistics
270- ancientHeaders common.StorageSize
271- ancientBodies common.StorageSize
272- ancientReceipts common.StorageSize
273- ancientHashes common.StorageSize
274- ancientTds common.StorageSize
299+ ancientHeadersSize common.StorageSize
300+ ancientBodiesSize common.StorageSize
301+ ancientReceiptsSize common.StorageSize
302+ ancientTdsSize common.StorageSize
303+ ancientHashesSize common.StorageSize
275304
276305 // Les statistic
277- chtTrieNodes common. StorageSize
278- bloomTrieNodes common. StorageSize
306+ chtTrieNodes stat
307+ bloomTrieNodes stat
279308
280309 // Meta- and unaccounted data
281- metadata common.StorageSize
282- unaccounted common.StorageSize
310+ metadata stat
311+ unaccounted stat
312+
313+ // Totals
314+ total common.StorageSize
283315 )
284316 // Inspect key-value database first.
285317 for it .Next () {
@@ -289,98 +321,104 @@ func InspectDatabase(db ethdb.Database) error {
289321 )
290322 total += size
291323 switch {
292- case bytes .HasPrefix (key , headerPrefix ) && bytes .HasSuffix (key , headerTDSuffix ):
293- tdSize += size
294- case bytes .HasPrefix (key , headerPrefix ) && bytes .HasSuffix (key , headerHashSuffix ):
295- numHashPairing += size
296324 case bytes .HasPrefix (key , headerPrefix ) && len (key ) == (len (headerPrefix )+ 8 + common .HashLength ):
297- headerSize += size
298- case bytes .HasPrefix (key , headerNumberPrefix ) && len (key ) == (len (headerNumberPrefix )+ common .HashLength ):
299- hashNumPairing += size
325+ headers .Add (size )
300326 case bytes .HasPrefix (key , blockBodyPrefix ) && len (key ) == (len (blockBodyPrefix )+ 8 + common .HashLength ):
301- bodySize += size
327+ bodies . Add ( size )
302328 case bytes .HasPrefix (key , blockReceiptsPrefix ) && len (key ) == (len (blockReceiptsPrefix )+ 8 + common .HashLength ):
303- receiptSize += size
329+ receipts .Add (size )
330+ case bytes .HasPrefix (key , headerPrefix ) && bytes .HasSuffix (key , headerTDSuffix ):
331+ tds .Add (size )
332+ case bytes .HasPrefix (key , headerPrefix ) && bytes .HasSuffix (key , headerHashSuffix ):
333+ numHashPairings .Add (size )
334+ case bytes .HasPrefix (key , headerNumberPrefix ) && len (key ) == (len (headerNumberPrefix )+ common .HashLength ):
335+ hashNumPairings .Add (size )
336+ case len (key ) == common .HashLength :
337+ tries .Add (size )
338+ case bytes .HasPrefix (key , codePrefix ) && len (key ) == len (codePrefix )+ common .HashLength :
339+ codes .Add (size )
304340 case bytes .HasPrefix (key , txLookupPrefix ) && len (key ) == (len (txLookupPrefix )+ common .HashLength ):
305- txlookupSize += size
341+ txLookups . Add ( size )
306342 case bytes .HasPrefix (key , SnapshotAccountPrefix ) && len (key ) == (len (SnapshotAccountPrefix )+ common .HashLength ):
307- accountSnapSize += size
343+ accountSnaps . Add ( size )
308344 case bytes .HasPrefix (key , SnapshotStoragePrefix ) && len (key ) == (len (SnapshotStoragePrefix )+ 2 * common .HashLength ):
309- storageSnapSize += size
345+ storageSnaps . Add ( size )
310346 case bytes .HasPrefix (key , preimagePrefix ) && len (key ) == (len (preimagePrefix )+ common .HashLength ):
311- preimageSize += size
347+ preimages . Add ( size )
312348 case bytes .HasPrefix (key , bloomBitsPrefix ) && len (key ) == (len (bloomBitsPrefix )+ 10 + common .HashLength ):
313- bloomBitsSize += size
349+ bloomBits . Add ( size )
314350 case bytes .HasPrefix (key , []byte ("clique-" )) && len (key ) == 7 + common .HashLength :
315- cliqueSnapsSize += size
351+ cliqueSnaps . Add ( size )
316352 case bytes .HasPrefix (key , []byte ("cht-" )) && len (key ) == 4 + common .HashLength :
317- chtTrieNodes += size
353+ chtTrieNodes . Add ( size )
318354 case bytes .HasPrefix (key , []byte ("blt-" )) && len (key ) == 4 + common .HashLength :
319- bloomTrieNodes += size
320- case bytes .HasPrefix (key , codePrefix ) && len (key ) == len (codePrefix )+ common .HashLength :
321- codeSize += size
322- case len (key ) == common .HashLength :
323- trieSize += size
355+ bloomTrieNodes .Add (size )
324356 default :
325357 var accounted bool
326358 for _ , meta := range [][]byte {databaseVerisionKey , headHeaderKey , headBlockKey , headFastBlockKey , fastTrieProgressKey } {
327359 if bytes .Equal (key , meta ) {
328- metadata += size
360+ metadata . Add ( size )
329361 accounted = true
330362 break
331363 }
332364 }
333365 if ! accounted {
334- unaccounted += size
366+ unaccounted . Add ( size )
335367 }
336368 }
337- count += 1
369+ count ++
338370 if count % 1000 == 0 && time .Since (logged ) > 8 * time .Second {
339371 log .Info ("Inspecting database" , "count" , count , "elapsed" , common .PrettyDuration (time .Since (start )))
340372 logged = time .Now ()
341373 }
342374 }
343375 // Inspect append-only file store then.
344- ancients := []* common.StorageSize {& ancientHeaders , & ancientBodies , & ancientReceipts , & ancientHashes , & ancientTds }
376+ ancientSizes := []* common.StorageSize {& ancientHeadersSize , & ancientBodiesSize , & ancientReceiptsSize , & ancientHashesSize , & ancientTdsSize }
345377 for i , category := range []string {freezerHeaderTable , freezerBodiesTable , freezerReceiptTable , freezerHashTable , freezerDifficultyTable } {
346378 if size , err := db .AncientSize (category ); err == nil {
347- * ancients [i ] += common .StorageSize (size )
379+ * ancientSizes [i ] += common .StorageSize (size )
348380 total += common .StorageSize (size )
349381 }
350382 }
383+ // Get number of ancient rows inside the freezer
384+ ancients := counter (0 )
385+ if count , err := db .Ancients (); err == nil {
386+ ancients = counter (count )
387+ }
351388 // Display the database statistic.
352389 stats := [][]string {
353- {"Key-Value store" , "Headers" , headerSize . String ()},
354- {"Key-Value store" , "Bodies" , bodySize . String ()},
355- {"Key-Value store" , "Receipts " , receiptSize . String ()},
356- {"Key-Value store" , "Difficulties" , tdSize . String ()},
357- {"Key-Value store" , "Block number->hash" , numHashPairing . String ()},
358- {"Key-Value store" , "Block hash->number" , hashNumPairing . String ()},
359- {"Key-Value store" , "Transaction index" , txlookupSize . String ()},
360- {"Key-Value store" , "Bloombit index" , bloomBitsSize . String ()},
361- {"Key-Value store" , "Contract codes" , codeSize . String ()},
362- {"Key-Value store" , "Trie nodes" , trieSize . String ()},
363- {"Key-Value store" , "Trie preimages" , preimageSize . String ()},
364- {"Key-Value store" , "Account snapshot" , accountSnapSize . String ()},
365- {"Key-Value store" , "Storage snapshot" , storageSnapSize . String ()},
366- {"Key-Value store" , "Clique snapshots" , cliqueSnapsSize . String ()},
367- {"Key-Value store" , "Singleton metadata" , metadata .String ()},
368- {"Ancient store" , "Headers" , ancientHeaders .String ()},
369- {"Ancient store" , "Bodies" , ancientBodies .String ()},
370- {"Ancient store" , "Receipts " , ancientReceipts .String ()},
371- {"Ancient store" , "Difficulties" , ancientTds .String ()},
372- {"Ancient store" , "Block number->hash" , ancientHashes .String ()},
373- {"Light client" , "CHT trie nodes" , chtTrieNodes .String ()},
374- {"Light client" , "Bloom trie nodes" , bloomTrieNodes .String ()},
390+ {"Key-Value store" , "Headers" , headers . Size (), headers . Count ()},
391+ {"Key-Value store" , "Bodies" , bodies . Size (), bodies . Count ()},
392+ {"Key-Value store" , "Receipt lists " , receipts . Size (), receipts . Count ()},
393+ {"Key-Value store" , "Difficulties" , tds . Size (), tds . Count ()},
394+ {"Key-Value store" , "Block number->hash" , numHashPairings . Size (), numHashPairings . Count ()},
395+ {"Key-Value store" , "Block hash->number" , hashNumPairings . Size (), hashNumPairings . Count ()},
396+ {"Key-Value store" , "Transaction index" , txLookups . Size (), txLookups . Count ()},
397+ {"Key-Value store" , "Bloombit index" , bloomBits . Size (), bloomBits . Count ()},
398+ {"Key-Value store" , "Contract codes" , codes . Size (), codes . Count ()},
399+ {"Key-Value store" , "Trie nodes" , tries . Size (), tries . Count ()},
400+ {"Key-Value store" , "Trie preimages" , preimages . Size (), preimages . Count ()},
401+ {"Key-Value store" , "Account snapshot" , accountSnaps . Size (), accountSnaps . Count ()},
402+ {"Key-Value store" , "Storage snapshot" , storageSnaps . Size (), storageSnaps . Count ()},
403+ {"Key-Value store" , "Clique snapshots" , cliqueSnaps . Size (), cliqueSnaps . Count ()},
404+ {"Key-Value store" , "Singleton metadata" , metadata .Size (), metadata . Count ()},
405+ {"Ancient store" , "Headers" , ancientHeadersSize . String (), ancients .String ()},
406+ {"Ancient store" , "Bodies" , ancientBodiesSize . String (), ancients .String ()},
407+ {"Ancient store" , "Receipt lists " , ancientReceiptsSize . String (), ancients .String ()},
408+ {"Ancient store" , "Difficulties" , ancientTdsSize . String (), ancients .String ()},
409+ {"Ancient store" , "Block number->hash" , ancientHashesSize . String (), ancients .String ()},
410+ {"Light client" , "CHT trie nodes" , chtTrieNodes .Size (), chtTrieNodes . Count ()},
411+ {"Light client" , "Bloom trie nodes" , bloomTrieNodes .Size (), bloomTrieNodes . Count ()},
375412 }
376413 table := tablewriter .NewWriter (os .Stdout )
377- table .SetHeader ([]string {"Database" , "Category" , "Size" })
378- table .SetFooter ([]string {"" , "Total" , total .String ()})
414+ table .SetHeader ([]string {"Database" , "Category" , "Size" , "Items" })
415+ table .SetFooter ([]string {"" , "Total" , total .String (), " " })
379416 table .AppendBulk (stats )
380417 table .Render ()
381418
382- if unaccounted > 0 {
383- log .Error ("Database contains unaccounted data" , "size" , unaccounted )
419+ if unaccounted . size > 0 {
420+ log .Error ("Database contains unaccounted data" , "size" , unaccounted . size , "count" , unaccounted . count )
384421 }
422+
385423 return nil
386424}
0 commit comments