Skip to content

Commit 2950a85

Browse files
author
Brian Picciano
committed
add --cpu-profile option, for writing out a cpu profile
1 parent 450c012 commit 2950a85

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

config/config.go

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var (
1717
RedisPoolSize int
1818
Debug bool
1919
BGPushPoolSize int
20+
CPUProfile string
2021
)
2122

2223
func init() {
@@ -61,6 +62,10 @@ func init() {
6162
Description: "Number of goroutines to have processing NOBLOCK Q*PUSH commands",
6263
Default: "128",
6364
})
65+
l.Add(lever.Param{
66+
Name: "--cpu-profile",
67+
Description: "Name of a file to write a cpu profile out to. If set the cpu profile will be written until okq is closed",
68+
})
6469
l.Parse()
6570

6671
ListenAddr, _ = l.ParamStr("--listen-addr")
@@ -71,4 +76,5 @@ func init() {
7176
RedisPoolSize, _ = l.ParamInt("--redis-pool-size")
7277
Debug = l.ParamFlag("--debug")
7378
BGPushPoolSize, _ = l.ParamInt("--bg-push-pool-size")
79+
CPUProfile, _ = l.ParamStr("--cpu-profile")
7480
}

server.go

+23
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import (
44
"errors"
55
"math/rand"
66
"net"
7+
"os"
8+
"os/signal"
9+
"runtime/pprof"
710
"time"
811

912
"github.com/mc0/okq/clients"
@@ -27,6 +30,10 @@ func main() {
2730

2831
incomingConns := make(chan net.Conn)
2932

33+
if config.CPUProfile != "" {
34+
go cpuProfile(config.CPUProfile)
35+
}
36+
3037
go acceptConns(server, incomingConns)
3138

3239
for {
@@ -93,3 +100,19 @@ outer:
93100
commands.Dispatch(client, command, args)
94101
}
95102
}
103+
104+
func cpuProfile(filename string) {
105+
log.L.Printf("starting cpu profile, writing to %q", filename)
106+
f, err := os.Create(filename)
107+
if err != nil {
108+
log.L.Fatalf("could not open cpu profile %q: %s", filename, err)
109+
}
110+
pprof.StartCPUProfile(f)
111+
112+
ch := make(chan os.Signal, 1)
113+
signal.Notify(ch, os.Interrupt)
114+
<-ch
115+
pprof.StopCPUProfile()
116+
signal.Stop(ch)
117+
log.L.Printf("stopping cpu profile, ctrl-c again to exit")
118+
}

0 commit comments

Comments
 (0)