-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproc.go
107 lines (88 loc) · 2.61 KB
/
proc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package cmd
import (
"context"
"fmt"
proc "github.com/shirou/gopsutil/process"
"github.com/spf13/cobra"
procGraph "github.com/krishpranav/gotop/src/display/process"
"github.com/krishpranav/gotop/src/general"
"github.com/krishpranav/gotop/src/process"
"github.com/krishpranav/gotop/src/utils"
"golang.org/x/sync/errgroup"
)
const (
defaultProcRefreshRate = 3000
defaultProcPid = 0
)
// procCmd represents the proc command
var procCmd = &cobra.Command{
Use: "proc",
Short: "proc command is used to get per-process information",
Long: `proc command is used to get information about each running process in the system.
Syntax:
gotop proc
To get information about a particular process whose PID is known the -p or --pid flag can be used.
Syntax:
gotop proc -p [PID]`,
Aliases: []string{"process", "processess"},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
return fmt.Errorf("the proc command should have no arguments, see gotop proc --help for further info")
}
pid, _ := cmd.Flags().GetInt32("pid")
procRefreshRate, _ := cmd.Flags().GetUint64("refresh")
if procRefreshRate < 1000 {
return fmt.Errorf("invalid refresh rate: minimum refresh rate is 1000(ms)")
}
if pid != defaultProcPid {
dataChannel := make(chan *process.Process, 1)
eg, ctx := errgroup.WithContext(context.Background())
proc, err := process.NewProcess(pid)
if err != nil {
utils.ErrorMsg("pid")
return fmt.Errorf("invalid pid")
}
eg.Go(func() error {
return process.Serve(proc, dataChannel, ctx, int64(4*procRefreshRate/5))
})
eg.Go(func() error {
return procGraph.ProcVisuals(ctx, dataChannel, procRefreshRate)
})
if err := eg.Wait(); err != nil {
if err != general.ErrCanceledByUser {
fmt.Printf("Error: %v\n", err)
}
}
} else {
dataChannel := make(chan []*proc.Process, 1)
eg, ctx := errgroup.WithContext(context.Background())
eg.Go(func() error {
return process.ServeProcs(dataChannel, ctx, int64(4*procRefreshRate/5))
})
eg.Go(func() error {
return procGraph.AllProcVisuals(dataChannel, ctx, procRefreshRate)
})
if err := eg.Wait(); err != nil {
if err != general.ErrCanceledByUser {
fmt.Printf("Error: %v\n", err)
}
}
}
return nil
},
}
func init() {
rootCmd.AddCommand(procCmd)
procCmd.Flags().Uint64P(
"refresh",
"r",
defaultProcRefreshRate,
"Process information UI refreshes rate in milliseconds greater than 1000",
)
procCmd.Flags().Int32P(
"pid",
"p",
defaultProcPid,
"specify PID of process. Passing PID 0 lists all the processes (same as not using the -p flag).",
)
}