1
0
mirror of https://github.com/google/gops.git synced 2024-11-24 08:22:25 +02:00
gops/agent/agent.go

99 lines
2.6 KiB
Go
Raw Normal View History

2016-11-04 07:43:04 +02:00
// Copyright 2016 The Go Authors. All rights reserved.
2016-11-04 05:59:53 +02:00
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package agent provides hooks programs can register to retrieve
// diagnostics data by using gops.
package agent
import (
"fmt"
"log"
"net"
"os"
2016-11-04 09:54:41 +02:00
gosignal "os/signal"
2016-11-04 05:59:53 +02:00
"runtime"
2016-11-04 09:54:41 +02:00
"github.com/google/gops/signal"
2016-11-04 05:59:53 +02:00
)
func init() {
2016-11-06 08:45:55 +02:00
// TODO(jbd): Expose these endpoints on HTTP. Then, we can enable
// the agent on Windows systems.
2016-11-04 05:59:53 +02:00
sock := fmt.Sprintf("/tmp/gops%d.sock", os.Getpid())
l, err := net.Listen("unix", sock)
if err != nil {
log.Fatal(err)
}
2016-11-04 09:54:41 +02:00
c := make(chan os.Signal, 1)
gosignal.Notify(c, os.Interrupt)
go func() {
// cleanup the socket on shutdown.
<-c
os.Remove(sock)
os.Exit(1)
}()
2016-11-04 05:59:53 +02:00
go func() {
buf := make([]byte, 1)
for {
fd, err := l.Accept()
if err != nil {
2016-11-04 08:10:27 +02:00
fmt.Fprintf(os.Stderr, "gops: %v", err)
2016-11-04 05:59:53 +02:00
continue
}
if _, err := fd.Read(buf); err != nil {
2016-11-04 08:10:27 +02:00
fmt.Fprintf(os.Stderr, "gops: %v", err)
2016-11-04 05:59:53 +02:00
continue
}
if err := handle(fd, buf); err != nil {
2016-11-04 08:10:27 +02:00
fmt.Fprintf(os.Stderr, "gops: %v", err)
2016-11-04 05:59:53 +02:00
continue
}
fd.Close()
}
}()
}
func handle(conn net.Conn, msg []byte) error {
switch msg[0] {
2016-11-04 07:32:46 +02:00
case signal.Stack:
2016-11-04 05:59:53 +02:00
buf := make([]byte, 1<<16)
n := runtime.Stack(buf, true)
_, err := conn.Write(buf[:n])
return err
2016-11-04 07:32:46 +02:00
case signal.GC:
2016-11-04 06:08:37 +02:00
runtime.GC()
_, err := conn.Write([]byte("ok"))
return err
2016-11-04 08:28:51 +02:00
case signal.MemStats:
var s runtime.MemStats
runtime.ReadMemStats(&s)
fmt.Fprintf(conn, "alloc: %v\n", s.Alloc)
fmt.Fprintf(conn, "total-alloc: %v\n", s.TotalAlloc)
fmt.Fprintf(conn, "sys: %v\n", s.Sys)
fmt.Fprintf(conn, "lookups: %v\n", s.Lookups)
fmt.Fprintf(conn, "mallocs: %v\n", s.Mallocs)
fmt.Fprintf(conn, "frees: %v\n", s.Frees)
fmt.Fprintf(conn, "heap-alloc: %v\n", s.HeapAlloc)
fmt.Fprintf(conn, "heap-sys: %v\n", s.HeapSys)
fmt.Fprintf(conn, "heap-idle: %v\n", s.HeapIdle)
fmt.Fprintf(conn, "heap-in-use: %v\n", s.HeapInuse)
fmt.Fprintf(conn, "heap-released: %v\n", s.HeapReleased)
fmt.Fprintf(conn, "heap-objects: %v\n", s.HeapObjects)
fmt.Fprintf(conn, "stack-in-use: %v\n", s.StackInuse)
fmt.Fprintf(conn, "stack-sys: %v\n", s.StackSys)
fmt.Fprintf(conn, "next-gc: %v\n", s.NextGC)
fmt.Fprintf(conn, "last-gc: %v ns ago\n", s.LastGC)
fmt.Fprintf(conn, "gc-pause: %v ns\n", s.PauseTotalNs)
fmt.Fprintf(conn, "num-gc: %v\n", s.NumGC)
fmt.Fprintf(conn, "enable-gc: %v\n", s.EnableGC)
fmt.Fprintf(conn, "debug-gc: %v\n", s.DebugGC)
2016-11-04 07:53:07 +02:00
case signal.Version:
fmt.Fprintf(conn, "%v\n", runtime.Version())
2016-11-04 05:59:53 +02:00
}
return nil
}