Debian Codegraph @

bench/trace.go

raw
1 //go:build trace
2
3 package bench
4
5 import (
6 "bufio"
7 "encoding/binary"
8 "fmt"
9 "os"
10 "strings"
11
12 "github.com/opencoff/go-mmap"
13 )
14
15 // Request represents a single cache access from a trace.
16 type Request[T any] struct {
17 Key T
18 }
19
20 // Trace holds the full sequence of requests and the count of unique keys.
21 type Trace[T any] struct {
22 Requests []Request[T]
23 Unique int
24 }
25
26 // LoadCSV reads a CSV trace file and returns a Trace.
27 // The parse function receives split fields for each line and returns
28 // the key value and true, or false to skip the line.
29 // Unique counting requires T to be comparable; the caller provides
30 // a key-extraction identity (the parse func itself serves this role
31 // since T is the key).
32 func LoadCSV(path string, parse func(fields []string) (string, bool)) (*Trace[string], error) {
33 f, err := os.Open(path)
34 if err != nil {
35 return nil, fmt.Errorf("LoadCSV: open %s: %w", path, err)
36 }
37 defer f.Close()
38
39 var requests []Request[string]
40 seen := make(map[string]struct{})
41
42 scanner := bufio.NewScanner(f)
43 // Increase buffer for long lines
44 scanner.Buffer(make([]byte, 1024*1024), 1024*1024)
45
46 for scanner.Scan() {
47 line := scanner.Text()
48 fields := strings.Split(line, ",")
49 key, ok := parse(fields)
50 if !ok {
51 continue
52 }
53 requests = append(requests, Request[string]{Key: key})
54 seen[key] = struct{}{}
55 }
56 if err := scanner.Err(); err != nil {
57 return nil, fmt.Errorf("LoadCSV: scan %s: %w", path, err)
58 }
59
60 return &Trace[string]{
61 Requests: requests,
62 Unique: len(seen),
63 }, nil
64 }
65
66 // ParseTwitter extracts the anonymized key (field index 1) from a Twitter trace line.
67 func ParseTwitter(fields []string) (string, bool) {
68 if len(fields) < 2 {
69 return "", false
70 }
71 return fields[1], true
72 }
73
74 // ParseMetaCDN extracts the cacheKey (field index 1) from a Meta CDN trace line.
75 func ParseMetaCDN(fields []string) (string, bool) {
76 if len(fields) < 2 {
77 return "", false
78 }
79 return fields[1], true
80 }
81
82 // LoadOracleGeneral reads an oracleGeneral binary trace file.
83 // Each record is 24 bytes:
84 //
85 // Offset 0: uint32 timestamp
86 // Offset 4: uint64 obj_id (the key)
87 // Offset 12: uint32 obj_size
88 // Offset 16: int64 next_access_vtime
89 //
90 // All little-endian. The file is read entirely into memory.
91 func LoadOracleGeneral(path string) (*Trace[uint64], error) {
92 fd, err := os.Open(path)
93 if err != nil {
94 return nil, fmt.Errorf("LoadOracleGeneral: read %s: %w", path, err)
95 }
96
97 defer fd.Close()
98
99 mm := mmap.New(fd)
100
101 mapping, err := mm.Map(0, 0, mmap.PROT_READ, mmap.F_READAHEAD)
102 if err != nil {
103 return nil, fmt.Errorf("LoadOracleGeneral: mmap %s: %w", path, err)
104 }
105
106 defer mm.Unmap(mapping)
107
108 data := mapping.Bytes()
109
110 const recordSize = 24
111 if len(data)%recordSize != 0 {
112 return nil, fmt.Errorf("LoadOracleGeneral: %s size %d not a multiple of %d", path, len(data), recordSize)
113 }
114
115 nRecords := len(data) / recordSize
116 requests := make([]Request[uint64], 0, nRecords)
117 seen := make(map[uint64]struct{}, nRecords/4)
118
119 for i := 0; i < len(data); i += recordSize {
120 objID := binary.LittleEndian.Uint64(data[i+4 : i+12])
121 requests = append(requests, Request[uint64]{Key: objID})
122 seen[objID] = struct{}{}
123 }
124
125 return &Trace[uint64]{
126 Requests: requests,
127 Unique: len(seen),
128 }, nil
129 }