-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
128 lines (110 loc) · 3.21 KB
/
main.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
package main
import (
"flag"
"log"
"os"
"os/signal"
"runtime"
"time"
"github.com/awelzel/zeek-spy/zeekspy"
)
func init() {
// We are using ptrace(2) - must stick to the same thread.
//
// https://github.com/golang/go/issues/7699
runtime.LockOSThread()
}
var (
pid int
hz uint
zeekprofile string
debug bool
statsInterval time.Duration
)
func main() {
fiveSeconds, _ := time.ParseDuration("5s")
flag.IntVar(&pid, "pid", 0, "PID of Zeek process")
flag.UintVar(&hz, "hz", 100, "Sampling frequency")
flag.BoolVar(&debug, "debug", false, "Enable sample debugging")
flag.StringVar(&zeekprofile, "profile", "", "Store pprof `profile` here")
flag.DurationVar(&statsInterval, "stats", fiveSeconds,
"Print stats every `interval` times.")
flag.Parse()
if pid == 0 || zeekprofile == "" {
flag.PrintDefaults()
os.Exit(1)
}
profileFile, err := os.Create(zeekprofile)
if err != nil {
log.Fatal(err)
}
defer profileFile.Close()
// Redirect Ctrl+C to signalChannel
signalChannel := make(chan os.Signal, 1)
signal.Notify(signalChannel, os.Interrupt)
period := time.Duration((1000000 / hz)) * time.Microsecond
log.Printf("Using pid=%d, hz=%v period=%v (%.6f ms) profile=%v\n",
pid, hz, period, period.Seconds()*1000, zeekprofile)
zp := zeekspy.ZeekProcessFromPid(pid)
log.Printf("Profiling %s\n", zp)
if version, err := zp.Version(); err == nil {
log.Printf("Found Zeek version '%s'", version)
} else {
log.Fatalf("Error reading version: %v", err)
}
profileBuilder := zeekspy.NewProfileBuilder(period)
stopped := false
statsSamplingTime := time.Duration(0)
totalSamples := 0
nonEmptySamples := 0
totalSkipped := 0
totalStart := time.Now()
nextSample := totalStart
nextStats := totalStart.Add(statsInterval)
diff := time.Duration(0)
for !stopped {
start := time.Now()
if result, err := zp.Spy(); err != nil {
log.Printf("[WARN] Failed to spy, exiting (%v)\n", err)
stopped = true
break
} else {
diff = time.Since(start)
totalSamples += 1
profileBuilder.AddSample(result.Stack)
if !result.Empty {
nonEmptySamples = nonEmptySamples + 1
if debug {
for i, s := range result.Stack {
log.Printf("Sample[%d][%d] %+v\n",
totalSamples, i, s)
}
}
}
}
statsSamplingTime += diff
skippedSamples := int(diff / period)
totalSkipped += skippedSamples
nextSample = nextSample.Add(time.Duration(1+skippedSamples) * period)
select {
case <-time.After(time.Until(nextSample)):
//
case sig := <-signalChannel:
log.Printf("Exiting after signal: %v\n", sig)
stopped = true
}
if now := time.Now(); now.After(nextStats) {
elapsed := now.Sub(totalStart)
fraction := statsSamplingTime.Seconds() / statsInterval.Seconds()
samplingRate := float64(totalSamples) / time.Since(totalStart).Seconds()
log.Printf("[STATS] elapsed=%.2fs samples=%d (%d total) skipped=%d frequency=%.1fhz overhead=%.2f%% (%v)\n",
elapsed.Seconds(), nonEmptySamples, totalSamples, totalSkipped,
samplingRate, fraction*100, statsSamplingTime)
nextStats = nextStats.Add(statsInterval)
statsSamplingTime = time.Duration(0)
}
}
log.Printf("Writing protobuf...\n")
profileBuilder.WriteProfile(profileFile)
log.Printf("Done.\n")
}