-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.go
192 lines (156 loc) · 4.25 KB
/
server.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"context"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"os/exec"
"runtime"
"time"
"golang.org/x/sync/errgroup"
"loov.dev/allocview/internal/packet"
"loov.dev/allocview/internal/series"
)
// ConnectDeadline defines how fast clients should connect to the server.
const ConnectDeadline = 10 * time.Second
// Server is a profile listening server.
type Server struct {
profiles chan *Profile
}
// NewServer returns a new server.
func NewServer() *Server {
return &Server{
profiles: make(chan *Profile, 1024),
}
}
// Profile returns channel for profiles.
func (server *Server) Profiles() <-chan *Profile { return server.profiles }
// Exec starts listening to cmd.
func (server *Server) Exec(ctx context.Context, group *errgroup.Group, cmd *exec.Cmd) error {
// create a temporary socket name
tmpfile, err := ioutil.TempFile("", "alloclog")
if err != nil {
return fmt.Errorf("unable to create temporary alloclog file: %w", err)
}
sockname := tmpfile.Name()
tmpfile.Close()
os.Remove(sockname)
// setup listener
addr := &net.UnixAddr{Name: sockname, Net: "unix"}
sock, err := net.ListenUnix("unix", addr)
if err != nil {
return fmt.Errorf("unable to start unix socket on %q: %w", sockname, err)
}
sock.SetUnlinkOnClose(true)
// start the program
cmd.Env = append(
os.Environ(),
"ALLOCLOGSOCK="+sockname,
)
err = cmd.Start() // TODO: use pgroup
if err != nil {
return fmt.Errorf("failed to start %q: %w", cmd.Args, err)
}
// wait for the program to connect
err = sock.SetDeadline(time.Now().Add(ConnectDeadline))
if err != nil {
_ = cmd.Process.Kill()
_ = sock.Close()
return fmt.Errorf("failed to set socket deadline: %w", err)
}
conn, err := sock.AcceptUnix()
if err != nil {
_ = cmd.Process.Kill()
_ = sock.Close()
return fmt.Errorf("no connection established, did you import `loov.dev/allocview/attach`: %w", err)
}
// we'll set deadline for the first packet to handle misconfigurations
err = conn.SetReadDeadline(time.Now().Add(ConnectDeadline))
if err != nil {
_ = cmd.Process.Kill()
_ = sock.Close()
return fmt.Errorf("failed to set read deadline: %w", err)
}
var dec packet.Decoder
err = dec.Read(conn)
if err != nil {
return fmt.Errorf("failed to read first packet: %w", err)
}
err = conn.SetReadDeadline(time.Time{})
if err != nil {
_ = cmd.Process.Kill()
_ = sock.Close()
return fmt.Errorf("failed to set read deadline: %w", err)
}
// TODO: handle magic header better
magic := dec.String()
if magic != "alloclog" {
return fmt.Errorf("invalid header %q expected %q", magic, "alloclog")
}
exename := dec.String()
funcname := dec.String()
funcaddr := dec.Uintptr()
// Reading of profiles.
group.Go(func() error {
err := server.readProfiles(conn, exename, funcname, funcaddr)
log.Printf("readProfiles returned: %v", err)
return err
})
group.Go(func() error {
// waits for program to close
err := cmd.Wait()
log.Printf("program exited: %v", err)
return err
})
return nil
}
func (server *Server) readProfiles(conn *net.UnixConn, exename, funcname string, funcaddr uintptr) error {
lastState := map[[32]uintptr]series.Sample{}
var dec packet.Decoder
for {
err := dec.Read(conn)
if err != nil {
return fmt.Errorf("failed to read packet: %w", err)
}
unixnano := dec.Int64()
count := dec.Uint32()
profile := &Profile{
ExeName: exename,
FuncName: funcname,
FuncAddr: funcaddr,
Time: time.Unix(0, unixnano),
Records: make([]runtime.MemProfileRecord, count),
}
for i, rec := range profile.Records {
var next series.Sample
next.AllocBytes = dec.Int64()
next.FreeBytes = dec.Int64()
next.AllocObjects = dec.Int64()
next.FreeObjects = dec.Int64()
for i := 0; ; i++ {
frame := dec.Uintptr()
if frame == 0 {
break
}
rec.Stack0[i] = frame
}
last, _ := lastState[rec.Stack0]
lastState[rec.Stack0] = next
rec.AllocBytes = next.AllocBytes - last.AllocBytes
rec.FreeBytes = next.FreeBytes - last.FreeBytes
rec.AllocObjects = next.AllocObjects - last.AllocObjects
rec.FreeObjects = next.FreeObjects - last.FreeObjects
profile.Records[i] = rec
}
server.profiles <- profile
}
}
type Profile struct {
ExeName string
FuncName string
FuncAddr uintptr
Time time.Time
Records []runtime.MemProfileRecord
}