123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- package procfs
- import (
- "bufio"
- "errors"
- "os"
- "regexp"
- "strconv"
- )
- var (
- cpuLineRE = regexp.MustCompile(`cpu(\d+) (\d+) (\d+) (\d+) (\d+) (\d+) (\d+) (\d+) (\d+) (\d+)`)
- procLineRE = regexp.MustCompile(`(\d+) (\d+) (\d+)`)
- )
- type Schedstat struct {
- CPUs []*SchedstatCPU
- }
- type SchedstatCPU struct {
- CPUNum string
- RunningNanoseconds uint64
- WaitingNanoseconds uint64
- RunTimeslices uint64
- }
- type ProcSchedstat struct {
- RunningNanoseconds uint64
- WaitingNanoseconds uint64
- RunTimeslices uint64
- }
- func (fs FS) Schedstat() (*Schedstat, error) {
- file, err := os.Open(fs.proc.Path("schedstat"))
- if err != nil {
- return nil, err
- }
- defer file.Close()
- stats := &Schedstat{}
- scanner := bufio.NewScanner(file)
- for scanner.Scan() {
- match := cpuLineRE.FindStringSubmatch(scanner.Text())
- if match != nil {
- cpu := &SchedstatCPU{}
- cpu.CPUNum = match[1]
- cpu.RunningNanoseconds, err = strconv.ParseUint(match[8], 10, 64)
- if err != nil {
- continue
- }
- cpu.WaitingNanoseconds, err = strconv.ParseUint(match[9], 10, 64)
- if err != nil {
- continue
- }
- cpu.RunTimeslices, err = strconv.ParseUint(match[10], 10, 64)
- if err != nil {
- continue
- }
- stats.CPUs = append(stats.CPUs, cpu)
- }
- }
- return stats, nil
- }
- func parseProcSchedstat(contents string) (stats ProcSchedstat, err error) {
- match := procLineRE.FindStringSubmatch(contents)
- if match != nil {
- stats.RunningNanoseconds, err = strconv.ParseUint(match[1], 10, 64)
- if err != nil {
- return
- }
- stats.WaitingNanoseconds, err = strconv.ParseUint(match[2], 10, 64)
- if err != nil {
- return
- }
- stats.RunTimeslices, err = strconv.ParseUint(match[3], 10, 64)
- return
- }
- err = errors.New("could not parse schedstat")
- return
- }
|