...
Source file
src/runtime/trace2string.go
Documentation: runtime
1
2
3
4
5
6
7
8
9 package runtime
10
11
12
13 const maxTraceStringLen = 1024
14
15
16
17 type traceStringTable struct {
18
19 lock mutex
20 buf *traceBuf
21
22
23 tab traceMap
24 }
25
26
27 func (t *traceStringTable) put(gen uintptr, s string) uint64 {
28
29 ss := stringStructOf(&s)
30 id, added := t.tab.put(ss.str, uintptr(ss.len))
31 if added {
32
33 systemstack(func() {
34 t.writeString(gen, id, s)
35 })
36 }
37 return id
38 }
39
40
41 func (t *traceStringTable) emit(gen uintptr, s string) uint64 {
42
43 id := t.tab.stealID()
44 systemstack(func() {
45 t.writeString(gen, id, s)
46 })
47 return id
48 }
49
50
51
52
53
54
55 func (t *traceStringTable) writeString(gen uintptr, id uint64, s string) {
56
57 if len(s) > maxTraceStringLen {
58 s = s[:maxTraceStringLen]
59 }
60
61 lock(&t.lock)
62 w := unsafeTraceWriter(gen, t.buf)
63
64
65 var flushed bool
66 w, flushed = w.ensure(2 + 2*traceBytesPerNumber + len(s) )
67 if flushed {
68
69 w.byte(byte(traceEvStrings))
70 }
71
72
73 w.byte(byte(traceEvString))
74 w.varint(id)
75 w.varint(uint64(len(s)))
76 w.stringData(s)
77
78
79 t.buf = w.traceBuf
80 unlock(&t.lock)
81 }
82
83
84
85
86
87
88
89
90
91
92 func (t *traceStringTable) reset(gen uintptr) {
93 if t.buf != nil {
94 lock(&trace.lock)
95 traceBufFlush(t.buf, gen)
96 unlock(&trace.lock)
97 t.buf = nil
98 }
99
100
101 lock(&t.tab.lock)
102 t.tab.reset()
103 unlock(&t.tab.lock)
104 }
105
View as plain text