func (self *Swap) Get() error { nswap := C.swapctl(C.SWAP_NSWAP, unsafe.Pointer(uintptr(0)), 0) // If there are no swap devices, nothing to do here. if nswap == 0 { return nil } swdev := make([]Swapent, nswap) rnswap := C.swapctl(C.SWAP_STATS, unsafe.Pointer(&swdev[0]), nswap) if rnswap == 0 { return nil } for i := 0; i < int(nswap); i++ { if swdev[i].se_flags&C.SWF_ENABLE == 2 { self.Used = self.Used + uint64(swdev[i].se_inuse/(1024/C.DEV_BSIZE)) self.Total = self.Total + uint64(swdev[i].se_nblks/(1024/C.DEV_BSIZE)) } } self.Free = self.Total - self.Used return nil }
func getSysSwap() (int64, int64, error) { var tSwap int64 var fSwap int64 var diskblksPerPage int64 num, err := C.swapctl(C.SC_GETNSWP, nil) if err != nil { return -1, -1, err } st := C.allocSwaptable(num) _, err = C.swapctl(C.SC_LIST, unsafe.Pointer(st)) if err != nil { C.freeSwaptable(st) return -1, -1, err } diskblksPerPage = int64(C.sysconf(C._SC_PAGESIZE) >> C.DEV_BSHIFT) for i := 0; i < int(num); i++ { swapent := C.getSwapEnt(&st.swt_ent[0], C.int(i)) tSwap += int64(swapent.ste_pages) * diskblksPerPage fSwap += int64(swapent.ste_free) * diskblksPerPage } C.freeSwaptable(st) return tSwap, fSwap, nil }