forked from keybase/kbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofilelist.go
178 lines (145 loc) · 4.15 KB
/
profilelist.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
// Copyright 2016 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package libfuse
import (
"bytes"
"io"
"os"
"runtime/pprof"
"runtime/trace"
"strings"
"time"
"bazil.org/fuse"
"bazil.org/fuse/fs"
"github.com/keybase/kbfs/libfs"
"golang.org/x/net/context"
)
type timedProfile interface {
Start(w io.Writer) error
Stop()
}
type cpuProfile struct{}
func (p cpuProfile) Start(w io.Writer) error {
return pprof.StartCPUProfile(w)
}
func (p cpuProfile) Stop() {
pprof.StopCPUProfile()
}
type traceProfile struct{}
func (p traceProfile) Start(w io.Writer) error {
return trace.Start(w)
}
func (p traceProfile) Stop() {
trace.Stop()
}
// timedProfileFile represents a file whose contents are determined by
// taking a profile for some duration.
type timedProfileFile struct {
duration time.Duration
profile timedProfile
}
var _ fs.Node = timedProfileFile{}
func (f timedProfileFile) Attr(ctx context.Context, a *fuse.Attr) error {
// Have a low non-zero value for Valid to avoid being swamped
// with requests.
a.Valid = 1 * time.Second
now := time.Now()
a.Size = 0
a.Mtime = now
a.Ctime = now
a.Mode = 0444
return nil
}
var _ fs.NodeOpener = timedProfileFile{}
func (f timedProfileFile) Open(ctx context.Context,
req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) {
// TODO: Blocking here until the profile is done is
// weird. Blocking on read is better.
//
// TODO: Maybe keep around a special last_profile file to be
// able to start capturing a profile and then interrupt when
// done, which would also be useful in general, since you be
// able to save a profile even if you open it up with a tool.
var buf bytes.Buffer
err := f.profile.Start(&buf)
if err != nil {
return nil, err
}
defer f.profile.Stop()
select {
case <-time.After(f.duration):
case <-ctx.Done():
return nil, ctx.Err()
}
f.profile.Stop()
resp.Flags |= fuse.OpenDirectIO
return fs.DataHandle(buf.Bytes()), nil
}
// ProfileList is a node that can list all of the available profiles.
type ProfileList struct{}
var _ fs.Node = ProfileList{}
// Attr implements the fs.Node interface.
func (ProfileList) Attr(_ context.Context, a *fuse.Attr) error {
a.Mode = os.ModeDir | 0755
return nil
}
var _ fs.NodeRequestLookuper = ProfileList{}
const cpuProfilePrefix = "profile."
const traceProfilePrefix = "trace."
// Lookup implements the fs.NodeRequestLookuper interface.
func (pl ProfileList) Lookup(_ context.Context, req *fuse.LookupRequest, resp *fuse.LookupResponse) (node fs.Node, err error) {
// Handle timed profiles first.
if strings.HasPrefix(req.Name, cpuProfilePrefix) {
durationStr := strings.TrimPrefix(req.Name, cpuProfilePrefix)
duration, err := time.ParseDuration(durationStr)
if err != nil {
return nil, err
}
return timedProfileFile{duration, cpuProfile{}}, nil
} else if strings.HasPrefix(req.Name, traceProfilePrefix) {
durationStr := strings.TrimPrefix(req.Name, traceProfilePrefix)
duration, err := time.ParseDuration(durationStr)
if err != nil {
return nil, err
}
return timedProfileFile{duration, traceProfile{}}, nil
}
f := libfs.ProfileGet(req.Name)
if f == nil {
return nil, fuse.ENOENT
}
resp.EntryValid = 0
return &SpecialReadFile{read: f}, nil
}
var _ fs.Handle = ProfileList{}
var _ fs.HandleReadDirAller = ProfileList{}
// ReadDirAll implements the ReadDirAll interface.
func (pl ProfileList) ReadDirAll(_ context.Context) (res []fuse.Dirent, err error) {
profiles := pprof.Profiles()
res = make([]fuse.Dirent, 0, len(profiles))
for _, p := range profiles {
name := p.Name()
if !libfs.IsSupportedProfileName(name) {
continue
}
res = append(res, fuse.Dirent{
Type: fuse.DT_File,
Name: name,
})
}
res = append(res, fuse.Dirent{
Type: fuse.DT_File,
Name: cpuProfilePrefix + "30s",
})
res = append(res, fuse.Dirent{
Type: fuse.DT_File,
Name: traceProfilePrefix + "1s",
})
return res, nil
}
var _ fs.NodeRemover = (*FolderList)(nil)
// Remove implements the fs.NodeRemover interface for ProfileList.
func (ProfileList) Remove(_ context.Context, req *fuse.RemoveRequest) (err error) {
return fuse.EPERM
}