-
Notifications
You must be signed in to change notification settings - Fork 455
/
main.go
83 lines (67 loc) · 2.31 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// SPDX-License-Identifier: AGPL-3.0-only
package main
import (
"flag"
"fmt"
"hash/crc32"
"log"
"os"
"unsafe"
"github.com/grafana/dskit/flagext"
"github.com/prometheus/prometheus/tsdb/fileutil"
"github.com/prometheus/prometheus/tsdb/index"
)
func main() {
// Clean up all flags registered via init() methods of 3rd-party libraries.
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
// Parse CLI arguments.
args, err := flagext.ParseFlagsAndArguments(flag.CommandLine)
if err != nil {
log.Fatalln(err.Error())
}
if len(args) != 1 {
log.Fatalf("the command expect 1 argument but %d was provided\n", len(args))
}
filepath := args[0]
finfo, err := os.Stat(filepath)
if err != nil {
log.Fatalf(err.Error())
}
indexSize := finfo.Size()
f, err := fileutil.OpenMmapFile(filepath)
if err != nil {
log.Fatalf(err.Error())
}
toc, err := index.NewTOCFromByteSlice(realByteSlice(f.Bytes()))
if err != nil {
log.Fatalf(err.Error())
}
// See https://github.com/prometheus/prometheus/blob/main/tsdb/docs/format/index.md on the index format.
fmt.Println("Index size: ", indexSize)
fmt.Println("Symbols table size: ", toc.Series-toc.Symbols)
fmt.Println("Series size: ", toc.LabelIndices-toc.Series)
fmt.Println("Label indices: ", toc.Postings-toc.LabelIndices)
fmt.Println("Postings: ", toc.LabelIndicesTable-toc.Postings)
fmt.Println("Label offset table: ", toc.PostingsTable-toc.LabelIndicesTable)
// Requires the full index to be correct.
if uint64(len(f.Bytes())) > toc.PostingsTable {
// TOC is a simple struct so unsafe.Sizeof() works correctly.
tocLength := uint64(unsafe.Sizeof(index.TOC{})) + crc32.Size
postingsTbl := uint64(len(f.Bytes())) - toc.PostingsTable - tocLength
indexHdr := toc.Series - toc.Symbols + postingsTbl + 34
fmt.Println("Postings offset table: ", postingsTbl)
fmt.Println("Index-header size (est):", indexHdr)
fmt.Printf("Index-header %% (est): %.3f\n", 100.0*float64(indexHdr)/float64(indexSize))
} else {
fmt.Println("Postings offset table: N/A")
fmt.Println("Index-header size (est): N/A")
fmt.Println("Index-header % (est): N/A")
}
}
type realByteSlice []byte
func (b realByteSlice) Len() int {
return len(b)
}
func (b realByteSlice) Range(start, end int) []byte {
return b[start:end]
}