summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorValery Piashchynski <[email protected]>2021-04-20 10:06:45 +0300
committerGitHub <[email protected]>2021-04-20 10:06:45 +0300
commit3362f211f6358d60bea47ac2de4cc29a47373973 (patch)
treed36dc3ce9a36fff1b15b8795e8fa08d397317d14 /tools
parent35d6a50aa3640c870b99c120b26c9b9012b424be (diff)
parent779cc3f5aebb749ab4bc6190e03cc86ff3f151a0 (diff)
#634 feat(plugin): new plugin `service`v2.1.0-beta.2
feat(plugin): new plugin `service`
Diffstat (limited to 'tools')
-rw-r--r--tools/process.go44
-rw-r--r--tools/worker_table.go49
2 files changed, 40 insertions, 53 deletions
diff --git a/tools/process.go b/tools/process.go
deleted file mode 100644
index a6eb1139..00000000
--- a/tools/process.go
+++ /dev/null
@@ -1,44 +0,0 @@
-package tools
-
-import (
- "github.com/shirou/gopsutil/process"
- "github.com/spiral/errors"
- "github.com/spiral/roadrunner/v2/pkg/worker"
-)
-
-// 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 uint64 `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 worker.BaseProcess) (ProcessState, error) {
- const op = errors.Op("worker_process_state")
- p, _ := process.NewProcess(int32(w.Pid()))
- i, err := p.MemoryInfo()
- if err != nil {
- return ProcessState{}, errors.E(op, err)
- }
-
- return ProcessState{
- Pid: int(w.Pid()),
- Status: w.State().String(),
- NumJobs: w.State().NumExecs(),
- Created: w.Created().UnixNano(),
- MemoryUsage: i.RSS,
- }, nil
-}
diff --git a/tools/worker_table.go b/tools/worker_table.go
index 157aa972..4dd70d8e 100644
--- a/tools/worker_table.go
+++ b/tools/worker_table.go
@@ -8,31 +8,62 @@ import (
"github.com/dustin/go-humanize"
"github.com/fatih/color"
"github.com/olekukonko/tablewriter"
+ "github.com/spiral/roadrunner/v2/pkg/process"
)
// WorkerTable renders table with information about rr server workers.
-func WorkerTable(writer io.Writer, workers []ProcessState) *tablewriter.Table {
+func WorkerTable(writer io.Writer, workers []process.State) *tablewriter.Table {
tw := tablewriter.NewWriter(writer)
- tw.SetHeader([]string{"PID", "Status", "Execs", "Memory", "Created"})
+ tw.SetHeader([]string{"PID", "Status", "Execs", "Memory", "CPU%", "Created"})
tw.SetColMinWidth(0, 7)
tw.SetColMinWidth(1, 9)
tw.SetColMinWidth(2, 7)
tw.SetColMinWidth(3, 7)
- tw.SetColMinWidth(4, 18)
+ tw.SetColMinWidth(4, 7)
+ tw.SetColMinWidth(5, 18)
- for key := range workers {
+ for i := 0; i < len(workers); i++ {
tw.Append([]string{
- strconv.Itoa(workers[key].Pid),
- renderStatus(workers[key].Status),
- renderJobs(workers[key].NumJobs),
- humanize.Bytes(workers[key].MemoryUsage),
- renderAlive(time.Unix(0, workers[key].Created)),
+ strconv.Itoa(workers[i].Pid),
+ renderStatus(workers[i].Status),
+ renderJobs(workers[i].NumJobs),
+ humanize.Bytes(workers[i].MemoryUsage),
+ renderCPU(workers[i].CPUPercent),
+ renderAlive(time.Unix(0, workers[i].Created)),
})
}
return tw
}
+// ServiceWorkerTable renders table with information about rr server workers.
+func ServiceWorkerTable(writer io.Writer, workers []process.State) *tablewriter.Table {
+ tw := tablewriter.NewWriter(writer)
+ tw.SetAutoWrapText(false)
+ tw.SetHeader([]string{"PID", "Memory", "CPU%", "Command"})
+ tw.SetColMinWidth(0, 7)
+ tw.SetColMinWidth(1, 7)
+ tw.SetColMinWidth(2, 7)
+ tw.SetColMinWidth(3, 18)
+ tw.SetAlignment(tablewriter.ALIGN_LEFT)
+
+ for i := 0; i < len(workers); i++ {
+ tw.Append([]string{
+ strconv.Itoa(workers[i].Pid),
+ humanize.Bytes(workers[i].MemoryUsage),
+ renderCPU(workers[i].CPUPercent),
+ workers[i].Command,
+ })
+ }
+
+ return tw
+}
+
+//go:inline
+func renderCPU(cpu float64) string {
+ return strconv.FormatFloat(cpu, 'f', 2, 64)
+}
+
func renderStatus(status string) string {
switch status {
case "inactive":