本文整理匯總了Golang中github.com/elastic/gosigar.ProcArgs類的典型用法代碼示例。如果您正苦於以下問題:Golang ProcArgs類的具體用法?Golang ProcArgs怎麽用?Golang ProcArgs使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了ProcArgs類的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: getDetails
// getDetails fills in CPU, memory, and command line details for the process
func (proc *Process) getDetails(cmdline string) error {
mem := sigar.ProcMem{}
if err := mem.Get(proc.Pid); err != nil {
return fmt.Errorf("error getting process mem for pid=%d: %v", proc.Pid, err)
}
proc.Mem = &ProcMemStat{
Size: mem.Size,
Rss: mem.Resident,
Share: mem.Share,
}
cpu := sigar.ProcTime{}
if err := cpu.Get(proc.Pid); err != nil {
return fmt.Errorf("error getting process cpu time for pid=%d: %v", proc.Pid, err)
}
proc.Cpu = &ProcCpuTime{
Start: cpu.FormatStartTime(),
Total: cpu.Total,
User: cpu.User,
System: cpu.Sys,
}
if cmdline == "" {
args := sigar.ProcArgs{}
if err := args.Get(proc.Pid); err != nil {
return fmt.Errorf("error getting process arguments for pid=%d: %v", proc.Pid, err)
}
proc.CmdLine = strings.Join(args.List, " ")
} else {
proc.CmdLine = cmdline
}
return nil
}
示例2: getDetails
// getDetails fills in CPU, memory, FD usage, and command line details for the process.
func (proc *Process) getDetails(cmdline string) error {
proc.Mem = sigar.ProcMem{}
if err := proc.Mem.Get(proc.Pid); err != nil {
return fmt.Errorf("error getting process mem for pid=%d: %v", proc.Pid, err)
}
proc.Cpu = sigar.ProcTime{}
if err := proc.Cpu.Get(proc.Pid); err != nil {
return fmt.Errorf("error getting process cpu time for pid=%d: %v", proc.Pid, err)
}
if cmdline == "" {
args := sigar.ProcArgs{}
if err := args.Get(proc.Pid); err != nil {
return fmt.Errorf("error getting process arguments for pid=%d: %v", proc.Pid, err)
}
proc.CmdLine = strings.Join(args.List, " ")
} else {
proc.CmdLine = cmdline
}
if fd, err := getProcFDUsage(proc.Pid); err != nil {
return fmt.Errorf("error getting process file descriptor usage for pid=%d: %v", proc.Pid, err)
} else if fd != nil {
proc.FD = *fd
}
return nil
}
示例3: main
func main() {
pids := gosigar.ProcList{}
pids.Get()
// ps -eo pid,ppid,stime,time,rss,user,state,command
fmt.Print(" PID PPID STIME TIME RSS USER S COMMAND\n")
for _, pid := range pids.List {
state := gosigar.ProcState{}
mem := gosigar.ProcMem{}
time := gosigar.ProcTime{}
args := gosigar.ProcArgs{}
if err := state.Get(pid); err != nil {
continue
}
if err := mem.Get(pid); err != nil {
continue
}
if err := time.Get(pid); err != nil {
continue
}
if err := args.Get(pid); err != nil {
continue
}
fmt.Printf("%5d %5d %s %s %6d %-15s %c %s\n",
pid, state.Ppid,
time.FormatStartTime(), time.FormatTotal(),
mem.Resident/1024, state.Username, state.State,
strings.Join(args.List, " "))
}
}
示例4: TestProcArgsUnknown
func TestProcArgsUnknown(t *testing.T) {
args := sigar.ProcArgs{}
err := args.Get(math.MaxInt32)
if err == nil {
t.Fatal("Expected process not found")
}
if !strings.Contains(err.Error(), "Process not found") {
t.Fatal("Expected error containing 'Process not found'")
}
}
示例5: TestProcArgs
func TestProcArgs(t *testing.T) {
args := sigar.ProcArgs{}
err := args.Get(os.Getpid())
if err != nil {
t.Fatal(err)
}
if len(args.List) == 0 {
t.Fatalf("Expected at least one arg")
}
}
示例6: GetProcess
func GetProcess(pid int) (*Process, error) {
state := sigar.ProcState{}
mem := sigar.ProcMem{}
cpu := sigar.ProcTime{}
args := sigar.ProcArgs{}
err := state.Get(pid)
if err != nil {
return nil, fmt.Errorf("Error getting state info: %v", err)
}
err = mem.Get(pid)
if err != nil {
return nil, fmt.Errorf("Error getting mem info: %v", err)
}
err = cpu.Get(pid)
if err != nil {
return nil, fmt.Errorf("Error getting cpu info: %v", err)
}
err = args.Get(pid)
if err != nil {
return nil, fmt.Errorf("Error getting command line: %v", err)
}
cmdLine := strings.Join(args.List, " ")
proc := Process{
Pid: pid,
Ppid: state.Ppid,
Name: state.Name,
State: getProcState(byte(state.State)),
Username: state.Username,
CmdLine: cmdLine,
Mem: mem,
Cpu: cpu,
}
proc.ctime = time.Now()
return &proc, nil
}
示例7: GetProcess
func GetProcess(pid int, cmdline string) (*Process, error) {
state := sigar.ProcState{}
if err := state.Get(pid); err != nil {
return nil, fmt.Errorf("error getting process state for pid=%d: %v", pid, err)
}
mem := sigar.ProcMem{}
if err := mem.Get(pid); err != nil {
return nil, fmt.Errorf("error getting process mem for pid=%d: %v", pid, err)
}
cpu := sigar.ProcTime{}
if err := cpu.Get(pid); err != nil {
return nil, fmt.Errorf("error getting process cpu time for pid=%d: %v", pid, err)
}
if cmdline == "" {
args := sigar.ProcArgs{}
if err := args.Get(pid); err != nil {
return nil, fmt.Errorf("error getting process arguments for pid=%d: %v", pid, err)
}
cmdline = strings.Join(args.List, " ")
}
proc := Process{
Pid: pid,
Ppid: state.Ppid,
Name: state.Name,
State: getProcState(byte(state.State)),
Username: state.Username,
CmdLine: cmdline,
Mem: mem,
Cpu: cpu,
ctime: time.Now(),
}
return &proc, nil
}