本文整理汇总了Golang中github.com/elastic/gosigar.ProcTime类的典型用法代码示例。如果您正苦于以下问题:Golang ProcTime类的具体用法?Golang ProcTime怎么用?Golang ProcTime使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ProcTime类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
}
开发者ID:Ingensi,项目名称:dockerbeat,代码行数:36,代码来源:sigar.go
示例2: 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, " "))
}
}
开发者ID:ChongFeng,项目名称:beats,代码行数:33,代码来源:ps.go
示例3: 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
}
开发者ID:miketonks,项目名称:beats,代码行数:43,代码来源:sigar.go
示例4: 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
}
开发者ID:jarpy,项目名称:beats,代码行数:38,代码来源:sigar.go
示例5: SampleEnvironment
// SampleEnvironment queries the runtime system for various interesting metrics,
// storing the resulting values in the set of metric gauges maintained by
// RuntimeStatSampler. This makes runtime statistics more convenient for
// consumption by the time series and status systems.
//
// This method should be called periodically by a higher level system in order
// to keep runtime statistics current.
func (rsr *RuntimeStatSampler) SampleEnvironment() {
// Record memory and call stats from the runtime package.
// TODO(mrtracy): memory statistics will not include usage from RocksDB.
// Determine an appropriate way to compute total memory usage.
numCgoCall := runtime.NumCgoCall()
numGoroutine := runtime.NumGoroutine()
// It might be useful to call ReadMemStats() more often, but it stops the
// world while collecting stats so shouldn't be called too often.
ms := runtime.MemStats{}
runtime.ReadMemStats(&ms)
// Retrieve Mem and CPU statistics.
pid := os.Getpid()
mem := gosigar.ProcMem{}
if err := mem.Get(pid); err != nil {
log.Errorf("unable to get mem usage: %v", err)
}
cpu := gosigar.ProcTime{}
if err := cpu.Get(pid); err != nil {
log.Errorf("unable to get cpu usage: %v", err)
}
// Time statistics can be compared to the total elapsed time to create a
// useful percentage of total CPU usage, which would be somewhat less accurate
// if calculated later using downsampled time series data.
now := rsr.clock.PhysicalNow()
dur := float64(now - rsr.lastNow)
// cpu.{User,Sys} are in milliseconds, convert to nanoseconds.
newUtime := int64(cpu.User) * 1e6
newStime := int64(cpu.Sys) * 1e6
uPerc := float64(newUtime-rsr.lastUtime) / dur
sPerc := float64(newStime-rsr.lastStime) / dur
pausePerc := float64(ms.PauseTotalNs-rsr.lastPauseTime) / dur
rsr.lastNow = now
rsr.lastUtime = newUtime
rsr.lastStime = newStime
rsr.lastPauseTime = ms.PauseTotalNs
// Log summary of statistics to console.
cgoRate := float64((numCgoCall-rsr.lastCgoCall)*int64(time.Second)) / dur
log.Infof("runtime stats: %s RSS, %d goroutines, %s active, %.2fcgo/sec, %.2f/%.2f %%(u/s)time, %.2f %%gc (%dx)",
humanize.IBytes(mem.Resident), numGoroutine, humanize.IBytes(ms.Alloc),
cgoRate, uPerc, sPerc, pausePerc, ms.NumGC-rsr.lastNumGC)
if log.V(2) {
log.Infof("memstats: %+v", ms)
}
if logOSStats != nil {
logOSStats()
}
rsr.lastCgoCall = numCgoCall
rsr.lastNumGC = ms.NumGC
rsr.cgoCalls.Update(numCgoCall)
rsr.goroutines.Update(int64(numGoroutine))
rsr.allocBytes.Update(int64(ms.Alloc))
rsr.sysBytes.Update(int64(ms.Sys))
rsr.gcCount.Update(int64(ms.NumGC))
rsr.gcPauseNS.Update(int64(ms.PauseTotalNs))
rsr.gcPausePercent.Update(pausePerc)
rsr.cpuUserNS.Update(newUtime)
rsr.cpuUserPercent.Update(uPerc)
rsr.cpuSysNS.Update(newStime)
rsr.cpuSysPercent.Update(sPerc)
rsr.rss.Update(int64(mem.Resident))
}
开发者ID:GitGoldie,项目名称:cockroach,代码行数:73,代码来源:runtime.go
示例6: SampleEnvironment
// SampleEnvironment queries the runtime system for various interesting metrics,
// storing the resulting values in the set of metric gauges maintained by
// RuntimeStatSampler. This makes runtime statistics more convenient for
// consumption by the time series and status systems.
//
// This method should be called periodically by a higher level system in order
// to keep runtime statistics current.
func (rsr *RuntimeStatSampler) SampleEnvironment() {
// Record memory and call stats from the runtime package.
// TODO(mrtracy): memory statistics will not include usage from RocksDB.
// Determine an appropriate way to compute total memory usage.
numCgoCall := runtime.NumCgoCall()
numGoroutine := runtime.NumGoroutine()
// It might be useful to call ReadMemStats() more often, but it stops the
// world while collecting stats so shouldn't be called too often.
// NOTE: the MemStats fields do not get decremented when memory is released,
// to get accurate numbers, be sure to subtract. eg: ms.Sys - ms.HeapReleased for
// current memory reserved.
ms := runtime.MemStats{}
runtime.ReadMemStats(&ms)
// Retrieve Mem and CPU statistics.
pid := os.Getpid()
mem := gosigar.ProcMem{}
if err := mem.Get(pid); err != nil {
log.Errorf(context.TODO(), "unable to get mem usage: %v", err)
}
cpu := gosigar.ProcTime{}
if err := cpu.Get(pid); err != nil {
log.Errorf(context.TODO(), "unable to get cpu usage: %v", err)
}
// Time statistics can be compared to the total elapsed time to create a
// useful percentage of total CPU usage, which would be somewhat less accurate
// if calculated later using downsampled time series data.
now := rsr.clock.PhysicalNow()
dur := float64(now - rsr.lastNow)
// cpu.{User,Sys} are in milliseconds, convert to nanoseconds.
newUtime := int64(cpu.User) * 1e6
newStime := int64(cpu.Sys) * 1e6
uPerc := float64(newUtime-rsr.lastUtime) / dur
sPerc := float64(newStime-rsr.lastStime) / dur
pausePerc := float64(ms.PauseTotalNs-rsr.lastPauseTime) / dur
rsr.lastNow = now
rsr.lastUtime = newUtime
rsr.lastStime = newStime
rsr.lastPauseTime = ms.PauseTotalNs
var cgoAllocated, cgoTotal uint64
if getCgoMemStats != nil {
var err error
cgoAllocated, cgoTotal, err = getCgoMemStats()
if err != nil {
log.Warningf(context.TODO(), "problem fetching CGO memory stats: %s, CGO stats will be empty.", err)
}
}
goAllocated := ms.Alloc
goTotal := ms.Sys - ms.HeapReleased
// Log summary of statistics to console.
cgoRate := float64((numCgoCall-rsr.lastCgoCall)*int64(time.Second)) / dur
log.Infof(context.TODO(), "runtime stats: %s RSS, %d goroutines, %s/%s/%s GO alloc/idle/total, %s/%s CGO alloc/total, %.2fcgo/sec, %.2f/%.2f %%(u/s)time, %.2f %%gc (%dx)",
humanize.IBytes(mem.Resident), numGoroutine,
humanize.IBytes(goAllocated), humanize.IBytes(ms.HeapIdle-ms.HeapReleased), humanize.IBytes(goTotal),
humanize.IBytes(cgoAllocated), humanize.IBytes(cgoTotal),
cgoRate, uPerc, sPerc, pausePerc, ms.NumGC-rsr.lastNumGC)
if log.V(2) {
log.Infof(context.TODO(), "memstats: %+v", ms)
}
rsr.lastCgoCall = numCgoCall
rsr.lastNumGC = ms.NumGC
rsr.cgoCalls.Update(numCgoCall)
rsr.goroutines.Update(int64(numGoroutine))
rsr.goAllocBytes.Update(int64(goAllocated))
rsr.goTotalBytes.Update(int64(goTotal))
rsr.cgoAllocBytes.Update(int64(cgoAllocated))
rsr.cgoTotalBytes.Update(int64(cgoTotal))
rsr.gcCount.Update(int64(ms.NumGC))
rsr.gcPauseNS.Update(int64(ms.PauseTotalNs))
rsr.gcPausePercent.Update(pausePerc)
rsr.cpuUserNS.Update(newUtime)
rsr.cpuUserPercent.Update(uPerc)
rsr.cpuSysNS.Update(newStime)
rsr.cpuSysPercent.Update(sPerc)
rsr.rss.Update(int64(mem.Resident))
}
开发者ID:yangxuanjia,项目名称:cockroach,代码行数:89,代码来源:runtime.go
注:本文中的github.com/elastic/gosigar.ProcTime类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论