forked from hanwen/go-fuse
/
pathdebug.go
182 lines (157 loc) · 4.13 KB
/
pathdebug.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
package fuse
import (
"fmt"
"log"
"os"
"path/filepath"
"sort"
"strings"
"sync"
"syscall"
)
var _ = fmt.Println
var _ = log.Println
const (
DebugDir = ".debug"
)
type getter func() []byte
// FileSystemDebug exposes a .debug directory, exposing files for
// which a read hooks into a callback. This is useful for exporting
// metrics and debug information from the daemon.
//
// TODO - should use in-process mount instead?
type FileSystemDebug struct {
sync.RWMutex
callbacks map[string]getter
FileSystem
}
func NewFileSystemDebug() *FileSystemDebug {
return &FileSystemDebug{
callbacks: make(map[string]getter),
}
}
func (me *FileSystemDebug) Add(name string, callback getter) {
me.RWMutex.Lock()
defer me.RWMutex.Unlock()
me.callbacks[name] = callback
}
func (me *FileSystemDebug) Open(path string, flags uint32) (fuseFile File, status Status) {
content := me.getContent(path)
if content != nil {
return NewReadOnlyFile(content), OK
}
return me.FileSystem.Open(path, flags)
}
var SeparatorString = string([]byte{filepath.Separator})
func (me *FileSystemDebug) getContent(path string) []byte {
comps := strings.Split(path, SeparatorString, -1)
if comps[0] == DebugDir {
me.RWMutex.RLock()
defer me.RWMutex.RUnlock()
f := me.callbacks[comps[1]]
if f != nil {
return f()
}
}
return nil
}
func (me *FileSystemDebug) GetXAttr(name string, attr string) ([]byte, Status) {
if strings.HasPrefix(name, DebugDir) {
return nil, syscall.ENODATA
}
return me.FileSystem.GetXAttr(name, attr)
}
func (me *FileSystemDebug) GetAttr(path string) (*os.FileInfo, Status) {
if !strings.HasPrefix(path, DebugDir) {
return me.FileSystem.GetAttr(path)
}
if path == DebugDir {
return &os.FileInfo{
Mode: S_IFDIR | 0755,
},OK
}
c := me.getContent(path)
if c != nil {
return &os.FileInfo{
Mode: S_IFREG | 0644,
Size: int64(len(c)),
},OK
}
return nil, ENOENT
}
func FloatMapToBytes(m map[string]float64) []byte {
keys := make([]string, 0, len(m))
for k, _ := range m {
keys = append(keys, k)
}
sort.SortStrings(keys)
var r []string
for _, k := range keys {
r = append(r, fmt.Sprintf("%v %v", k, m[k]))
}
return []byte(strings.Join(r, "\n"))
}
// Ugh - generics.
func IntMapToBytes(m map[string]int) []byte {
keys := make([]string, 0, len(m))
for k, _ := range m {
keys = append(keys, k)
}
sort.SortStrings(keys)
var r []string
for _, k := range keys {
r = append(r, fmt.Sprintf("%v %v", k, m[k]))
}
return []byte(strings.Join(r, "\n"))
}
func (me *FileSystemDebug) OpenDir(name string) (stream chan DirEntry, status Status) {
if name == DebugDir {
me.RWMutex.RLock()
defer me.RWMutex.RUnlock()
stream = make(chan DirEntry, len(me.callbacks))
for k, _ := range me.callbacks {
stream <- DirEntry{
Name: k,
Mode: S_IFREG,
}
}
close(stream)
return stream, OK
}
return me.FileSystem.OpenDir(name)
}
func (me *FileSystemDebug) AddMountState(state *MountState) {
me.Add("mountstate-latencies",
func() []byte { return FloatMapToBytes(state.Latencies()) })
me.Add("mountstate-opcounts",
func() []byte { return IntMapToBytes(state.OperationCounts()) })
me.Add("mountstate-bufferpool",
func() []byte { return []byte(state.BufferPoolStats()) })
}
func (me *FileSystemDebug) AddFileSystemConnector(conn *FileSystemConnector) {
me.Add("filesystemconnector-stats",
func() []byte { return []byte(conn.Statistics()) })
}
func hotPaths(timing *TimingFileSystem) []byte {
hot := timing.HotPaths("GetAttr")
unique := len(hot)
top := 20
start := len(hot) - top
if start < 0 {
start = 0
}
return []byte(fmt.Sprintf("Unique GetAttr paths: %d\nTop %d GetAttr paths: %v",
unique, top, hot[start:]))
}
func (me *FileSystemDebug) AddTimingFileSystem(tfs *TimingFileSystem) {
me.Add("timingfs-latencies",
func() []byte { return FloatMapToBytes(tfs.Latencies()) })
me.Add("timingfs-opcounts",
func() []byte { return IntMapToBytes(tfs.OperationCounts()) })
me.Add("timingfs-hotpaths",
func() []byte { return hotPaths(tfs) })
}
func (me *FileSystemDebug) AddRawTimingFileSystem(tfs *TimingRawFileSystem) {
me.Add("rawtimingfs-latencies",
func() []byte { return FloatMapToBytes(tfs.Latencies()) })
}