Debian Codegraph @

src/runtime/tracetype.go

raw
1 // Copyright 2023 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Trace type table.
6
7 package runtime
8
9 import (
10 "internal/abi"
11 "internal/goarch"
12 "internal/trace/tracev2"
13 "unsafe"
14 )
15
16 // traceTypeTable maps types to unique uint32 ids.
17 // It is lock-free for reading.
18 type traceTypeTable struct {
19 tab traceMap
20 }
21
22 // put returns a unique id for the type typ and caches it in the table,
23 // if it's seeing it for the first time.
24 //
25 // N.B. typ must be kept alive forever for this to work correctly.
26 func (t *traceTypeTable) put(typ *abi.Type) uint64 {
27 if typ == nil {
28 return 0
29 }
30 // Insert the pointer to the type itself.
31 id, _ := t.tab.put(noescape(unsafe.Pointer(&typ)), goarch.PtrSize)
32 return id
33 }
34
35 // dump writes all previously cached types to trace buffers and
36 // releases all memory and resets state. It must only be called once the caller
37 // can guarantee that there are no more writers to the table.
38 func (t *traceTypeTable) dump(gen uintptr) {
39 w := unsafeTraceExpWriter(gen, nil, tracev2.AllocFree)
40 if root := (*traceMapNode)(t.tab.root.Load()); root != nil {
41 w = dumpTypesRec(root, w)
42 }
43 w.flush().end()
44 t.tab.reset()
45 }
46
47 func dumpTypesRec(node *traceMapNode, w traceWriter) traceWriter {
48 typ := (*abi.Type)(*(*unsafe.Pointer)(unsafe.Pointer(&node.data[0])))
49 typName := toRType(typ).string()
50
51 // The maximum number of bytes required to hold the encoded type.
52 maxBytes := 1 + 5*traceBytesPerNumber + len(typName)
53
54 // Estimate the size of this record. This
55 // bound is pretty loose, but avoids counting
56 // lots of varint sizes.
57 //
58 // Add 1 because we might also write a traceAllocFreeTypesBatch byte.
59 var flushed bool
60 w, flushed = w.ensure(1 + maxBytes)
61 if flushed {
62 // Annotate the batch as containing types.
63 w.byte(byte(traceAllocFreeTypesBatch))
64 }
65
66 // Emit type.
67 w.varint(node.id)
68 w.varint(uint64(uintptr(unsafe.Pointer(typ))))
69 w.varint(uint64(typ.Size()))
70 w.varint(uint64(typ.PtrBytes))
71 w.varint(uint64(len(typName)))
72 w.stringData(typName)
73
74 // Recursively walk all child nodes.
75 for i := range node.children {
76 child := node.children[i].Load()
77 if child == nil {
78 continue
79 }
80 w = dumpTypesRec((*traceMapNode)(child), w)
81 }
82 return w
83 }