...

Source file src/github.com/shirou/gopsutil/load/load_bsd.go

Documentation: github.com/shirou/gopsutil/load

     1  // +build freebsd openbsd
     2  
     3  package load
     4  
     5  import (
     6  	"context"
     7  	"os/exec"
     8  	"strings"
     9  	"unsafe"
    10  
    11  	"golang.org/x/sys/unix"
    12  )
    13  
    14  func Avg() (*AvgStat, error) {
    15  	return AvgWithContext(context.Background())
    16  }
    17  
    18  func AvgWithContext(ctx context.Context) (*AvgStat, error) {
    19  	// This SysctlRaw method borrowed from
    20  	// https://github.com/prometheus/node_exporter/blob/master/collector/loadavg_freebsd.go
    21  	type loadavg struct {
    22  		load  [3]uint32
    23  		scale int
    24  	}
    25  	b, err := unix.SysctlRaw("vm.loadavg")
    26  	if err != nil {
    27  		return nil, err
    28  	}
    29  	load := *(*loadavg)(unsafe.Pointer((&b[0])))
    30  	scale := float64(load.scale)
    31  	ret := &AvgStat{
    32  		Load1:  float64(load.load[0]) / scale,
    33  		Load5:  float64(load.load[1]) / scale,
    34  		Load15: float64(load.load[2]) / scale,
    35  	}
    36  
    37  	return ret, nil
    38  }
    39  
    40  type forkstat struct {
    41  	forks    int
    42  	vforks   int
    43  	__tforks int
    44  }
    45  
    46  // Misc returns miscellaneous host-wide statistics.
    47  // darwin use ps command to get process running/blocked count.
    48  // Almost same as Darwin implementation, but state is different.
    49  func Misc() (*MiscStat, error) {
    50  	return MiscWithContext(context.Background())
    51  }
    52  
    53  func MiscWithContext(ctx context.Context) (*MiscStat, error) {
    54  	bin, err := exec.LookPath("ps")
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  	out, err := invoke.CommandWithContext(ctx, bin, "axo", "state")
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  	lines := strings.Split(string(out), "\n")
    63  
    64  	ret := MiscStat{}
    65  	for _, l := range lines {
    66  		if strings.Contains(l, "R") {
    67  			ret.ProcsRunning++
    68  		} else if strings.Contains(l, "D") {
    69  			ret.ProcsBlocked++
    70  		}
    71  	}
    72  
    73  	f, err := getForkStat()
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  	ret.ProcsCreated = f.forks
    78  
    79  	return &ret, nil
    80  }
    81  

View as plain text