|
| 1 | +// Copyright 2016 CodisLabs. All Rights Reserved. |
| 2 | +// Licensed under the MIT (MIT-LICENSE.txt) license. |
| 3 | + |
| 4 | +// +build linux |
| 5 | + |
| 6 | +package utils |
| 7 | + |
| 8 | +import ( |
| 9 | + "bufio" |
| 10 | + "fmt" |
| 11 | + "os" |
| 12 | + "syscall" |
| 13 | + "time" |
| 14 | + |
| 15 | + "github.com/CodisLabs/codis/pkg/utils/errors" |
| 16 | +) |
| 17 | + |
| 18 | +/* |
| 19 | +#include <unistd.h> |
| 20 | +*/ |
| 21 | +import "C" |
| 22 | + |
| 23 | +type Usage struct { |
| 24 | + Utime time.Duration `json:"utime"` |
| 25 | + Stime time.Duration `json:"stime"` |
| 26 | + Cutime time.Duration `json:"cutime"` |
| 27 | + Cstime time.Duration `json:"cstime"` |
| 28 | + |
| 29 | + NumThreads int `json:"num_threads"` |
| 30 | + |
| 31 | + VmSize int64 `json:"vm_size"` |
| 32 | + VmRss int64 `json:"vm_rss"` |
| 33 | +} |
| 34 | + |
| 35 | +func (u *Usage) MemTotal() int64 { |
| 36 | + return u.VmRss |
| 37 | +} |
| 38 | + |
| 39 | +func (u *Usage) CPUTotal() time.Duration { |
| 40 | + return time.Duration(u.Utime + u.Stime + u.Cutime + u.Cstime) |
| 41 | +} |
| 42 | + |
| 43 | +func GetUsage() (*Usage, error) { |
| 44 | + f, err := os.Open("/proc/self/stat") |
| 45 | + if err != nil { |
| 46 | + return nil, errors.Trace(err) |
| 47 | + } |
| 48 | + defer f.Close() |
| 49 | + |
| 50 | + var ignore struct { |
| 51 | + s string |
| 52 | + d int64 |
| 53 | + } |
| 54 | + |
| 55 | + r := bufio.NewReader(f) |
| 56 | + u := &Usage{} |
| 57 | + if _, err := fmt.Fscanf(r, "%d %s %s %d %d %d", |
| 58 | + &ignore.d, &ignore.s, &ignore.s, &ignore.d, &ignore.d, &ignore.d); err != nil { |
| 59 | + return nil, errors.Trace(err) |
| 60 | + } |
| 61 | + if _, err := fmt.Fscanf(r, "%d %d %d", |
| 62 | + &ignore.d, &ignore.d, &ignore.d); err != nil { |
| 63 | + return nil, errors.Trace(err) |
| 64 | + } |
| 65 | + if _, err := fmt.Fscanf(r, "%d %d %d %d", |
| 66 | + &ignore.d, &ignore.d, &ignore.d, &ignore.d); err != nil { |
| 67 | + return nil, errors.Trace(err) |
| 68 | + } |
| 69 | + |
| 70 | + var ticks struct { |
| 71 | + u int64 |
| 72 | + s int64 |
| 73 | + } |
| 74 | + unit := time.Second / time.Duration(C.sysconf(C._SC_CLK_TCK)) |
| 75 | + |
| 76 | + if _, err := fmt.Fscanf(r, "%d %d", |
| 77 | + &ticks.u, &ticks.s); err != nil { |
| 78 | + return nil, errors.Trace(err) |
| 79 | + } |
| 80 | + u.Utime = time.Duration(ticks.u) * unit |
| 81 | + u.Stime = time.Duration(ticks.s) * unit |
| 82 | + |
| 83 | + if _, err := fmt.Fscanf(r, "%d %d", |
| 84 | + &ticks.u, &ticks.s); err != nil { |
| 85 | + return nil, errors.Trace(err) |
| 86 | + } |
| 87 | + u.Cutime = time.Duration(ticks.u) * unit |
| 88 | + u.Cstime = time.Duration(ticks.s) * unit |
| 89 | + |
| 90 | + if _, err := fmt.Fscanf(r, "%d %d %d %d %d", |
| 91 | + &ignore.d, &ignore.d, &u.NumThreads, &ignore.d, &ignore.d); err != nil { |
| 92 | + return nil, errors.Trace(err) |
| 93 | + } |
| 94 | + if _, err := fmt.Fscanf(r, "%d %d", |
| 95 | + &u.VmSize, &u.VmRss); err != nil { |
| 96 | + return nil, errors.Trace(err) |
| 97 | + } |
| 98 | + u.VmRss *= int64(syscall.Getpagesize()) |
| 99 | + return u, nil |
| 100 | +} |
0 commit comments