func processes() ([]Process, error) { results := make([]Process, 0, 50) mib := []int32{CTLKern, KernProc, KernProcProc, 0} buf, length, err := common.CallSyscall(mib) if err != nil { return results, err } // get kinfo_proc size k := KinfoProc{} procinfoLen := int(unsafe.Sizeof(k)) count := int(length / uint64(procinfoLen)) // parse buf to procs for i := 0; i < count; i++ { b := buf[i*procinfoLen : i*procinfoLen+procinfoLen] k, err := parseKinfoProc(b) if err != nil { continue } p, err := NewProcess(int32(k.KiPid)) if err != nil { continue } copyParams(&k, p) results = append(results, *p) } return results, nil }
func (p *Process) getKProc() (*KinfoProc, error) { mib := []int32{CTLKern, KernProc, KernProcPID, p.Pid} buf, length, err := common.CallSyscall(mib) if err != nil { return nil, err } procK := KinfoProc{} if length != uint64(unsafe.Sizeof(procK)) { return nil, err } k, err := parseKinfoProc(buf) if err != nil { return nil, err } return &k, nil }
func DiskIOCounters() (map[string]DiskIOCountersStat, error) { // statinfo->devinfo->devstat // /usr/include/devinfo.h // sysctl.sysctl ('kern.devstat.all', 0) ret := make(map[string]DiskIOCountersStat) mib := []int32{CTLKern, KernDevstat, KernDevstatAll} buf, length, err := common.CallSyscall(mib) if err != nil { return nil, err } ds := Devstat{} devstatLen := int(unsafe.Sizeof(ds)) count := int(length / uint64(devstatLen)) buf = buf[8:] // devstat.all has version in the head. // parse buf to Devstat for i := 0; i < count; i++ { b := buf[i*devstatLen : i*devstatLen+devstatLen] d, err := parseDevstat(b) if err != nil { continue } un := strconv.Itoa(int(d.Unit_number)) name := common.IntToString(d.Device_name[:]) + un ds := DiskIOCountersStat{ ReadCount: d.Operations[DEVSTAT_READ], WriteCount: d.Operations[DEVSTAT_WRITE], ReadBytes: d.Bytes[DEVSTAT_READ], WriteBytes: d.Bytes[DEVSTAT_WRITE], ReadTime: d.Duration[DEVSTAT_READ].Compute(), WriteTime: d.Duration[DEVSTAT_WRITE].Compute(), Name: name, } ret[name] = ds } return ret, nil }