-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use constructor to generate stats (#1)
- Loading branch information
Showing
3 changed files
with
74 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |