本文整理汇总了Golang中syscall.Getrusage函数的典型用法代码示例。如果您正苦于以下问题:Golang Getrusage函数的具体用法?Golang Getrusage怎么用?Golang Getrusage使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Getrusage函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: loadFromAll
func loadFromAll(generators []generator, datacentre string,
logger *log.Logger) (*mdb.Mdb, error) {
machineMap := make(map[string]mdb.Machine)
startTime := time.Now()
var rusageStart, rusageStop syscall.Rusage
syscall.Getrusage(syscall.RUSAGE_SELF, &rusageStart)
for _, gen := range generators {
mdb, err := gen.Generate(datacentre, logger)
if err != nil {
return nil, err
}
for _, machine := range mdb.Machines {
if oldMachine, ok := machineMap[machine.Hostname]; ok {
oldMachine.UpdateFrom(machine)
machineMap[machine.Hostname] = oldMachine
} else {
machineMap[machine.Hostname] = machine
}
}
}
var newMdb mdb.Mdb
for _, machine := range machineMap {
newMdb.Machines = append(newMdb.Machines, machine)
}
syscall.Getrusage(syscall.RUSAGE_SELF, &rusageStop)
loadTimeDistribution.Add(time.Since(startTime))
loadCpuTimeDistribution.Add(time.Duration(
rusageStop.Utime.Sec)*time.Second +
time.Duration(rusageStop.Utime.Usec)*time.Microsecond -
time.Duration(rusageStart.Utime.Sec)*time.Second -
time.Duration(rusageStart.Utime.Usec)*time.Microsecond)
return &newMdb, nil
}
示例2: main
func main() {
lim := &syscall.Rlimit{}
syscall.Getrlimit(syscall.RLIMIT_NOFILE, lim)
lim.Cur = 15
syscall.Setrlimit(syscall.RLIMIT_NOFILE, lim)
fmt.Println(lim)
var s []*os.File
for i := 0; i < int(lim.Max+10); i++ {
f, err := os.Open("/tmp/whj.txt")
fmt.Println(f, err)
s = append(s, f)
err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, lim)
fmt.Println(lim, err)
}
err := syscall.Getrlimit(syscall.RLIMIT_CORE, lim)
fmt.Println(lim, err)
rusage := &syscall.Rusage{}
err = syscall.Getrusage(0, rusage)
fmt.Println(rusage, err)
err = syscall.Getrusage(1, rusage)
fmt.Println(rusage, err)
err = syscall.Getrusage(2, rusage)
fmt.Println(rusage, err)
err = syscall.Getrusage(3, rusage)
fmt.Println(rusage, err)
err = syscall.Getrusage(4, rusage)
fmt.Println(rusage, err)
}
示例3: WriteHtml
func (imageObjectServers *imageObjectServersType) WriteHtml(writer io.Writer) {
// TODO(rgooch): These statistics should be cached and the cache invalidated
// when images and objects are added/deleted.
var rusageStart, rusageStop syscall.Rusage
syscall.Getrusage(syscall.RUSAGE_SELF, &rusageStart)
objectsMap := imageObjectServers.objSrv.ListObjectSizes()
var totalBytes uint64
for _, bytes := range objectsMap {
totalBytes += bytes
}
numObjects := len(objectsMap)
fmt.Fprintf(writer, "Number of objects: %d, consumimg %s<br>\n",
numObjects, format.FormatBytes(totalBytes))
for _, imageName := range imageObjectServers.imdb.ListImages() {
image := imageObjectServers.imdb.GetImage(imageName)
if image == nil {
continue
}
for _, inode := range image.FileSystem.InodeTable {
if inode, ok := inode.(*filesystem.RegularInode); ok {
delete(objectsMap, inode.Hash)
}
}
}
var unreferencedBytes uint64
for _, bytes := range objectsMap {
unreferencedBytes += bytes
}
unreferencedObjectsPercent := 0.0
if numObjects > 0 {
unreferencedObjectsPercent =
100.0 * float64(len(objectsMap)) / float64(numObjects)
}
unreferencedBytesPercent := 0.0
if totalBytes > 0 {
unreferencedBytesPercent =
100.0 * float64(unreferencedBytes) / float64(totalBytes)
}
syscall.Getrusage(syscall.RUSAGE_SELF, &rusageStop)
statisticsComputeCpuTimeDistribution.Add(time.Duration(
rusageStop.Utime.Sec)*time.Second +
time.Duration(rusageStop.Utime.Usec)*time.Microsecond -
time.Duration(rusageStart.Utime.Sec)*time.Second -
time.Duration(rusageStart.Utime.Usec)*time.Microsecond)
fmt.Fprintf(writer,
"Number of unreferenced objects: %d (%.1f%%), "+
"consuming %s (%.1f%%)<br>\n",
len(objectsMap), unreferencedObjectsPercent,
format.FormatBytes(unreferencedBytes), unreferencedBytesPercent)
}
示例4: buildUpdateRequest
// Returns true if no update needs to be performed.
func (sub *Sub) buildUpdateRequest(request *subproto.UpdateRequest) bool {
var state state
state.subFS = &sub.fileSystem.FileSystem
requiredImage := sub.herd.getImage(sub.requiredImage)
state.requiredFS = requiredImage.FileSystem
filter := requiredImage.Filter
request.Triggers = requiredImage.Triggers
state.requiredInodeToSubInode = make(map[uint64]uint64)
state.inodesChanged = make(map[uint64]bool)
state.inodesCreated = make(map[uint64]string)
state.subObjectCacheUsage = make(map[hash.Hash]uint64,
len(sub.fileSystem.ObjectCache))
var rusageStart, rusageStop syscall.Rusage
syscall.Getrusage(syscall.RUSAGE_SELF, &rusageStart)
// Populate subObjectCacheUsage.
for _, hash := range sub.fileSystem.ObjectCache {
state.subObjectCacheUsage[hash] = 0
}
compareDirectories(request, &state,
&state.subFS.DirectoryInode, &state.requiredFS.DirectoryInode,
"/", filter)
// Look for multiply used objects and tell the sub.
for obj, useCount := range state.subObjectCacheUsage {
if useCount > 1 {
if request.MultiplyUsedObjects == nil {
request.MultiplyUsedObjects = make(map[hash.Hash]uint64)
}
request.MultiplyUsedObjects[obj] = useCount
}
}
syscall.Getrusage(syscall.RUSAGE_SELF, &rusageStop)
sub.lastComputeUpdateCpuDuration = time.Duration(
rusageStop.Utime.Sec)*time.Second +
time.Duration(rusageStop.Utime.Usec)*time.Microsecond -
time.Duration(rusageStart.Utime.Sec)*time.Second -
time.Duration(rusageStart.Utime.Usec)*time.Microsecond
computeCpuTimeDistribution.Add(sub.lastComputeUpdateCpuDuration)
if len(request.FilesToCopyToCache) > 0 ||
len(request.InodesToMake) > 0 ||
len(request.HardlinksToMake) > 0 ||
len(request.PathsToDelete) > 0 ||
len(request.DirectoriesToMake) > 0 ||
len(request.InodesToChange) > 0 {
sub.herd.logger.Printf(
"buildUpdateRequest(%s) took: %s user CPU time\n",
sub.hostname, sub.lastComputeUpdateCpuDuration)
return false
}
return true
}
示例5: RunSysStats
func RunSysStats(startedAt time.Time, interval time.Duration) {
const nsInMs uint64 = 1000 * 1000
ticker := time.NewTicker(interval)
defer func() {
ticker.Stop()
}()
var (
ms = new(runtime.MemStats)
rusage = &syscall.Rusage{}
lastUserTime int64
lastSysTime int64
userTime int64
sysTime int64
userCpuUtil float64
sysCpuUtil float64
)
for _ = range ticker.C {
runtime.ReadMemStats(ms)
syscall.Getrusage(syscall.RUSAGE_SELF, rusage)
syscall.Getrusage(syscall.RUSAGE_SELF, rusage)
userTime = rusage.Utime.Sec*1000000000 + int64(rusage.Utime.Usec)
sysTime = rusage.Stime.Sec*1000000000 + int64(rusage.Stime.Usec)
userCpuUtil = float64(userTime-lastUserTime) * 100 / float64(interval)
sysCpuUtil = float64(sysTime-lastSysTime) * 100 / float64(interval)
lastUserTime = userTime
lastSysTime = sysTime
log.Info("ver:%s, since:%s, go:%d, gc:%dms/%d=%d, heap:{%s, %s, %s, %s %s} cpu:{%3.2f%%us, %3.2f%%sy}",
BuildId,
time.Since(startedAt),
runtime.NumGoroutine(),
ms.PauseTotalNs/nsInMs,
ms.NumGC,
ms.PauseTotalNs/(nsInMs*uint64(ms.NumGC))+1,
gofmt.ByteSize(ms.HeapSys), // bytes it has asked the operating system for
gofmt.ByteSize(ms.HeapAlloc), // bytes currently allocated in the heap
gofmt.ByteSize(ms.HeapIdle), // bytes in the heap that are unused
gofmt.ByteSize(ms.HeapReleased), // bytes returned to the operating system, 5m for scavenger
gofmt.Comma(int64(ms.HeapObjects)),
userCpuUtil,
sysCpuUtil)
}
}
示例6: getrusage
func getrusage(who int, rusage *Rusage) error {
switch who {
case RUSAGE_CHILDREN:
who = syscall.RUSAGE_CHILDREN
case RUSAGE_SELF:
who = syscall.RUSAGE_SELF
default:
return syscall.ENOTSUP
}
var syscallRusage syscall.Rusage
if err := syscall.Getrusage(who, &syscallRusage); err != nil {
return err
}
rusage.Utime.Sec = int64(syscallRusage.Utime.Sec)
rusage.Utime.Usec = int64(syscallRusage.Utime.Usec)
rusage.Stime.Sec = int64(syscallRusage.Stime.Sec)
rusage.Stime.Usec = int64(syscallRusage.Stime.Usec)
rusage.Maxrss = int64(syscallRusage.Maxrss) >> 10
rusage.Ixrss = int64(syscallRusage.Ixrss) >> 10
rusage.Idrss = int64(syscallRusage.Idrss) >> 10
rusage.Minflt = int64(syscallRusage.Minflt)
rusage.Majflt = int64(syscallRusage.Majflt)
rusage.Nswap = int64(syscallRusage.Nswap)
rusage.Inblock = int64(syscallRusage.Inblock)
rusage.Oublock = int64(syscallRusage.Oublock)
rusage.Msgsnd = int64(syscallRusage.Msgsnd)
rusage.Msgrcv = int64(syscallRusage.Msgrcv)
rusage.Nsignals = int64(syscallRusage.Nsignals)
rusage.Nvcsw = int64(syscallRusage.Nvcsw)
rusage.Nivcsw = int64(syscallRusage.Nivcsw)
return nil
}
示例7: Stats
func (s *Stats) Stats() map[string]int64 {
st := make(map[string]int64)
st["cmd_get"] = s.cmd_get
st["cmd_set"] = s.cmd_set
st["cmd_delete"] = s.cmd_delete
st["get_hits"] = s.get_hits
st["get_misses"] = s.get_misses
st["curr_connections"] = s.curr_connections
st["total_connections"] = s.total_connections
st["bytes_read"] = s.bytes_read
st["bytes_written"] = s.bytes_written
for k, v := range s.stat {
st[k] = v
}
t := time.Now()
st["time"] = int64(t.Second())
st["uptime"] = int64(t.Sub(s.start).Seconds())
st["pid"] = int64(os.Getpid())
st["threads"] = int64(runtime.NumGoroutine())
rusage := syscall.Rusage{}
syscall.Getrusage(0, &rusage)
st["rusage_user"] = int64(rusage.Utime.Sec)
st["rusage_system"] = int64(rusage.Stime.Sec)
var memstat runtime.MemStats
runtime.ReadMemStats(&memstat)
st["rusage_maxrss"] = int64(memstat.HeapSys/1024) + cmem.Alloced()/1024
return st
}
示例8: CommonGmetrics
func CommonGmetrics(gmetric MetricSender) {
gmetric("goroutines", fmt.Sprintf("%d", runtime.NumGoroutine()), Uint, "num", false)
var mem runtime.MemStats
runtime.ReadMemStats(&mem)
gmetric("mem_alloc", fmt.Sprintf("%d", mem.Alloc), Uint, "bytes", false)
gmetric("mem_sys", fmt.Sprintf("%d", mem.Sys), Uint, "bytes", false)
gmetric("mem_gc_pause_last", fmt.Sprintf("%.6f", float64(mem.PauseNs[(mem.NumGC+255)%256])/1e6), Float, "ms", false)
var gcPauseMax uint64
for _, v := range mem.PauseNs {
if v > gcPauseMax {
gcPauseMax = v
}
}
gmetric("mem_gc_pause_max", fmt.Sprintf("%.6f", float64(gcPauseMax)/1e6), Float, "ms", false)
gmetric("mem_gc_pause_total", fmt.Sprintf("%.6f", float64(mem.PauseTotalNs)/1e6), Float, "ms", true)
since := time.Now().Sub(time.Unix(0, int64(mem.LastGC))).Seconds()
gmetric("mem_gc_pause_since", fmt.Sprintf("%.6f", since), Float, "sec", false)
var r syscall.Rusage
if syscall.Getrusage(syscall.RUSAGE_SELF, &r) == nil {
gmetric("rusage_utime", fmt.Sprintf("%.6f", float64(r.Utime.Nano())/1e9), Float, "cpusecs", true)
gmetric("rusage_stime", fmt.Sprintf("%.6f", float64(r.Stime.Nano())/1e9), Float, "cpusecs", true)
gmetric("cpu_pct", fmt.Sprintf("%.4f", 100*float64((r.Utime.Nano()+r.Stime.Nano()))/1e9), Float, "%", true)
}
}
示例9: GetSystemStats
func GetSystemStats() SystemStats {
stats := SystemStats{}
stats.NumGoRoutines = runtime.NumGoroutine()
var r syscall.Rusage
if syscall.Getrusage(syscall.RUSAGE_SELF, &r) == nil {
stats.UserTime = float64(r.Utime.Nano()) / 1e9
stats.SystemTime = float64(r.Stime.Nano()) / 1e9
}
var mem runtime.MemStats
runtime.ReadMemStats(&mem)
stats.BytesAlloc = mem.Alloc
stats.BytesFromSystem = mem.Sys
stats.GCPauseTimeLast = float64(mem.PauseNs[(mem.NumGC+255)%256]) / 1e6
var gcPauseMax uint64
for _, v := range mem.PauseNs {
if v > gcPauseMax {
gcPauseMax = v
}
}
stats.GCPauseTimeMax = float64(gcPauseMax) / 1e6
stats.GCPauseTimeTotal = float64(mem.PauseTotalNs) / 1e6
stats.GCPauseSince = time.Now().Sub(time.Unix(0, int64(mem.LastGC))).Seconds()
return stats
}
示例10: savePerfStats
func savePerfStats(file string) {
fd, err := os.Create(file)
if err != nil {
panic(err)
}
var prevUsage int64
var prevTime int64
var rusage syscall.Rusage
var memstats runtime.MemStats
t0 := time.Now()
for t := range time.NewTicker(250 * time.Millisecond).C {
syscall.Getrusage(syscall.RUSAGE_SELF, &rusage)
curTime := time.Now().UnixNano()
timeDiff := curTime - prevTime
curUsage := rusage.Utime.Nano() + rusage.Stime.Nano()
usageDiff := curUsage - prevUsage
cpuUsagePercent := 100 * float64(usageDiff) / float64(timeDiff)
prevTime = curTime
prevUsage = curUsage
runtime.ReadMemStats(&memstats)
startms := int(t.Sub(t0).Seconds() * 1000)
fmt.Fprintf(fd, "%d\t%f\t%d\t%d\n", startms, cpuUsagePercent, memstats.Alloc, memstats.Sys-memstats.HeapReleased)
}
}
示例11: registerPlatformEnvironment
func registerPlatformEnvironment(group *MonitorGroup) {
group.Chain("rusage", MonitorFunc(
func(cb func(name string, val float64)) {
var rusage syscall.Rusage
err := syscall.Getrusage(syscall.RUSAGE_SELF, &rusage)
if err != nil {
logger.Errorf("failed getting rusage data: %s", err)
return
}
MonitorStruct(&rusage, cb)
}))
group.Chain("proc.stat", MonitorFunc(
func(cb func(name string, val float64)) {
var stat procSelfStat
err := readProcSelfStat(&stat)
if err != nil {
logger.Errorf("failed getting /proc/self/stat data: %s", err)
return
}
MonitorStruct(&stat, cb)
}))
group.Chain("proc.statm", MonitorFunc(
func(cb func(name string, val float64)) {
var stat procSelfStatm
err := readProcSelfStatm(&stat)
if err != nil {
logger.Errorf("failed getting /proc/self/statm data: %s", err)
return
}
MonitorStruct(&stat, cb)
}))
}
示例12: clock
func clock(l *State) int {
var rusage syscall.Rusage
_ = syscall.Getrusage(syscall.RUSAGE_SELF, &rusage) // ignore errors
l.PushNumber(float64(rusage.Utime.Sec+rusage.Stime.Sec) + float64(rusage.Utime.Usec+rusage.Stime.Usec)/1000000.0)
return 1
}
示例13: statistics
func (server *server) statistics() serverStats {
stats := *server.stats
stats.Uptime = time.Since(server.startedAt).Seconds()
stats.CurrentTubes = len(server.tubes)
stats.TotalJobs = len(server.jobs)
for _, tube := range server.tubes {
stats.CurrentJobsBuried += tube.buried.Len()
stats.CurrentJobsDelayed += tube.delayed.Len()
stats.CurrentJobsReady += tube.ready.Len()
stats.CurrentJobsReserved += tube.reserved.Len()
}
var duration time.Duration
usage := new(syscall.Rusage)
err := syscall.Getrusage(syscall.RUSAGE_SELF, usage)
if err == nil {
s, ns := usage.Utime.Unix()
duration, err = time.ParseDuration(fmt.Sprintf("%d.%ds", s, ns))
stats.RusageStime = duration.Seconds()
s, ns = usage.Stime.Unix()
duration, err = time.ParseDuration(fmt.Sprintf("%d.%ds", s, ns))
stats.RusageUtime = duration.Seconds()
} else {
pf("failed to get rusage : %v", err)
}
return stats
}
示例14: writeHeader
func writeHeader(writer io.Writer) {
fmt.Fprintf(writer, "Start time: %s<br>\n", startTime.Format(timeFormat))
uptime := time.Since(startTime) + time.Millisecond*50
uptime = (uptime / time.Millisecond / 100) * time.Millisecond * 100
fmt.Fprintf(writer, "Uptime: %s<br>\n", uptime)
var rusage syscall.Rusage
syscall.Getrusage(syscall.RUSAGE_SELF, &rusage)
userCpuTime := time.Duration(rusage.Utime.Sec)*time.Second +
time.Duration(rusage.Utime.Usec)*time.Microsecond
sysCpuTime := time.Duration(rusage.Stime.Sec)*time.Second +
time.Duration(rusage.Stime.Usec)*time.Microsecond
cpuTime := rusage.Utime.Sec + rusage.Stime.Sec
fmt.Fprintf(writer, "CPU Time: %.1f%% (User: %s Sys: %s)<br>\n",
float64(cpuTime*100)/float64(uptime.Seconds()), userCpuTime, sysCpuTime)
var memStatsBeforeGC, memStatsAfterGC runtime.MemStats
runtime.ReadMemStats(&memStatsBeforeGC)
runtime.GC()
runtime.ReadMemStats(&memStatsAfterGC)
fmt.Fprintf(writer, "Allocated memory: %s (%s after GC)<br>\n",
format.FormatBytes(memStatsBeforeGC.Alloc),
format.FormatBytes(memStatsAfterGC.Alloc))
fmt.Fprintf(writer, "System memory: %s (%s after GC)<br>\n",
format.FormatBytes(memStatsBeforeGC.Sys),
format.FormatBytes(memStatsAfterGC.Sys))
fmt.Fprintln(writer, "Raw <a href=\"metrics\">metrics</a>")
}
示例15: showSystemStat
func showSystemStat(interval time.Duration, count int) {
usage1 := &syscall.Rusage{}
var lastUtime int64
var lastStime int64
counter := 0
for {
//http://man7.org/linux/man-pages/man3/vtimes.3.html
syscall.Getrusage(syscall.RUSAGE_SELF, usage1)
utime := usage1.Utime.Nano()
stime := usage1.Stime.Nano()
userCPUUtil := float64(utime-lastUtime) * 100 / float64(interval)
sysCPUUtil := float64(stime-lastStime) * 100 / float64(interval)
memUtil := usage1.Maxrss * 1024
lastUtime = utime
lastStime = stime
if counter > 0 {
fmt.Printf("cpu: %3.2f%% us %3.2f%% sy, mem:%s \n", userCPUUtil, sysCPUUtil, toH(uint64(memUtil)))
}
counter += 1
if count >= 1 && count < counter {
return
}
time.Sleep(interval)
}
}