Skip to content

Instantly share code, notes, and snippets.

@jasperla
Last active April 8, 2016 12:19
Show Gist options
  • Select an option

  • Save jasperla/85fc6b4b4eb92753de990663411b3184 to your computer and use it in GitHub Desktop.

Select an option

Save jasperla/85fc6b4b4eb92753de990663411b3184 to your computer and use it in GitHub Desktop.
package main
/*
#include <sys/types.h>
#include <sys/swap.h>
#include <unistd.h>
*/
import "C"
import (
"fmt"
"unsafe"
"github.com/davecgh/go-spew/spew"
)
type Swapent struct {
se_dev C.dev_t
se_flags int
se_nblks int
se_inuse int
se_priority int
sw_path []byte
// se_path [C.PATH_MAX]C.char // not quite right but we don't use this field
}
func main() {
swap()
}
func swap() 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)
spew.Dump(swdev)
fmt.Printf("nswap: %v\n", nswap)
rnswap := C.swapctl(C.SWAP_STATS, unsafe.Pointer(&swdev), nswap)
if rnswap == 0 {
return nil
}
fmt.Println(swdev[0].se_nblks)
for i := 0; i < int(nswap); i++ {
spew.Dump(swdev)
// if swdev[i].se_flags&C.SWF_ENABLE == 1 {
//
// }
}
return nil
}
/*
static int
swapmode(int *used, int *total)
{
struct swapent *swdev;
int nswap, rnswap, i;
nswap = swapctl(SWAP_NSWAP, 0, 0);
if (nswap == 0)
return 0;
swdev = calloc(nswap, sizeof(*swdev));
if (swdev == NULL)
return 0;
rnswap = swapctl(SWAP_STATS, swdev, nswap);
if (rnswap == -1) {
free(swdev);
return 0;
}
*total = *used = 0;
for (i = 0; i < nswap; i++) {
if (swdev[i].se_flags & SWF_ENABLE) {
*used += (swdev[i].se_inuse / (1024 / DEV_BSIZE));
*total += (swdev[i].se_nblks / (1024 / DEV_BSIZE));
}
}
free(swdev);
return 1;
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment