Skip to content

Commit

Permalink
Use constructor to generate stats (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
nakabonne authored Oct 31, 2020
1 parent 9e95afc commit 83bece4
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 27 deletions.
29 changes: 6 additions & 23 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@ import (
"net"
"os"
"os/signal"
"runtime"
"strconv"
"strings"
"sync"
"syscall"

"github.com/shirou/gopsutil/process"

"github.com/nakabonne/gosivy/pidfile"
"github.com/nakabonne/gosivy/stats"
)
Expand Down Expand Up @@ -158,22 +155,11 @@ func listen() {
}

func handle(conn io.ReadWriter, msg []byte) error {
// TODO: Make it singleton if possible.
process, err := process.NewProcess(int32(os.Getpid()))
if err != nil {
return err
}
switch msg[0] {
case stats.SignalMeta:
meta := stats.Meta{
GoMaxProcs: runtime.GOMAXPROCS(0),
NumCPU: runtime.NumCPU(),
}
if u, err := process.Username(); err == nil {
meta.Username = u
}
if c, err := process.Cmdline(); err == nil {
meta.Cmmand = c
meta, err := stats.NewMeta()
if err != nil {
return err
}
b, err := json.Marshal(meta)
if err != nil {
Expand All @@ -182,12 +168,9 @@ func handle(conn io.ReadWriter, msg []byte) error {
_, err = conn.Write(b)
return err
case stats.SignalStats:
s := stats.Stats{
Goroutines: runtime.NumGoroutine(),
}
runtime.ReadMemStats(&s.MemStats)
if c, err := process.CPUPercent(); err == nil {
s.CPUUsage = c
s, err := stats.NewStats()
if err != nil {
return err
}
b, err := json.Marshal(s)
if err != nil {
Expand Down
33 changes: 31 additions & 2 deletions stats/meta.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@
package stats

// Meta represents process metadata. This will be not changed
import (
"os"
"runtime"

"github.com/shirou/gopsutil/process"
)

// Meta represents process metadata, which will be not changed
// as long as the process continues.
type Meta struct {
Username string
Cmmand string
Command string
GoMaxProcs int
NumCPU int
}

func NewMeta() (*Meta, error) {
// TODO: Make it singleton if possible.
process, err := process.NewProcess(int32(os.Getpid()))
if err != nil {
return nil, err
}

var username, command string
if u, err := process.Username(); err == nil {
username = u
}
if c, err := process.Cmdline(); err == nil {
command = c
}
return &Meta{
Username: username,
Command: command,
GoMaxProcs: runtime.GOMAXPROCS(0),
NumCPU: runtime.NumCPU(),
}, nil
}
39 changes: 37 additions & 2 deletions stats/stats.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,47 @@
package stats

import "runtime"
import (
"os"
"runtime"

"github.com/shirou/gopsutil/process"
)

// Stats represents the statistical data of the process at the time of measurement.
type Stats struct {
// The number of goroutines that currently exist.
Goroutines int
// How many percent of the CPU time this process uses
CPUUsage float64
MemStats runtime.MemStats
MemStats
}

// MemStats records statistics about the memory allocator.
type MemStats struct {
HeapAlloc uint64
HeapIdle uint64
HeapInuse uint64
}

func NewStats() (*Stats, error) {
// TODO: Make it singleton if possible.
process, err := process.NewProcess(int32(os.Getpid()))
if err != nil {
return nil, err
}
var cpuUsage float64
if c, err := process.CPUPercent(); err == nil {
cpuUsage = c
}
var m runtime.MemStats
runtime.ReadMemStats(&m)
return &Stats{
Goroutines: runtime.NumGoroutine(),
CPUUsage: cpuUsage,
MemStats: MemStats{
HeapAlloc: m.HeapAlloc,
HeapIdle: m.HeapIdle,
HeapInuse: m.HeapInuse,
},
}, nil
}

0 comments on commit 83bece4

Please sign in to comment.