diff options
author | Valery Piashchynski <[email protected]> | 2020-10-13 13:55:20 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2020-10-13 13:55:20 +0300 |
commit | 0dc44d54cfcc9dd3fa09a41136f35a9a8d26b994 (patch) | |
tree | ffcb65010bebe9f5b5436192979e64b2402a6ec0 /process_state.go | |
parent | 08d6b6b7f773f83b286cd48c1a0fbec9a62fb42b (diff) |
Initial commit of RR 2.0v2.0.0-alpha1
Diffstat (limited to 'process_state.go')
-rw-r--r-- | process_state.go | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/process_state.go b/process_state.go new file mode 100644 index 00000000..747fa8a8 --- /dev/null +++ b/process_state.go @@ -0,0 +1,58 @@ +package roadrunner + +import ( + "context" + + "github.com/shirou/gopsutil/process" +) + +// ProcessState provides information about specific worker. +type ProcessState struct { + // Pid contains process id. + Pid int `json:"pid"` + + // Status of the worker. + Status string `json:"status"` + + // Number of worker executions. + NumJobs int64 `json:"numExecs"` + + // Created is unix nano timestamp of worker creation time. + Created int64 `json:"created"` + + // MemoryUsage holds the information about worker memory usage in bytes. + // Values might vary for different operating systems and based on RSS. + MemoryUsage uint64 `json:"memoryUsage"` +} + +// WorkerProcessState creates new worker state definition. +func WorkerProcessState(w WorkerBase) (ProcessState, error) { + p, _ := process.NewProcess(int32(w.Pid())) + i, err := p.MemoryInfo() + if err != nil { + return ProcessState{}, err + } + + return ProcessState{ + Pid: int(w.Pid()), + Status: w.State().String(), + NumJobs: w.State().NumExecs(), + Created: w.Created().UnixNano(), + MemoryUsage: i.RSS, + }, nil +} + +// ServerState returns list of all worker states of a given rr server. +func PoolState(pool Pool) ([]ProcessState, error) { + result := make([]ProcessState, 0) + for _, w := range pool.Workers(context.TODO()) { + state, err := WorkerProcessState(w) + if err != nil { + return nil, err + } + + result = append(result, state) + } + + return result, nil +} |