Skip to content

Commit

Permalink
go code
Browse files Browse the repository at this point in the history
to link with sigar c code
  • Loading branch information
ilanuriel committed Jan 4, 2016
1 parent 0f0dbe3 commit 9158290
Show file tree
Hide file tree
Showing 19 changed files with 988 additions and 0 deletions.
29 changes: 29 additions & 0 deletions go_bindings/gotoc/avg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package gotoc

import (



)
/*
#include "../../../../../Include/sigar.h"
*/
import "C"

func Avg()C.sigar_loadavg_t{

var sigar *C.sigar_t=GetSigarHandle()

var avg C.sigar_loadavg_t


C.sigar_loadavg_get(sigar, &avg)


return avg

}
52 changes: 52 additions & 0 deletions go_bindings/gotoc/cpu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package gotoc


import (


"unsafe"
)


//#include "../../../../../Include/sigar.h"
import "C"






func Cpu() (C.sigar_cpu_list_t,[]C.sigar_cpu_t){



var sigar *C.sigar_t=GetSigarHandle()

var cpulists C.sigar_cpu_list_t

C.sigar_cpu_list_get(sigar, &cpulists)

var length int=int(cpulists.number)


usp:=GetGoSlice(length, unsafe.Pointer(cpulists.data))

var goCpu []C.sigar_cpu_t
goCpu = *(*[]C.sigar_cpu_t)(unsafe.Pointer(&usp))


C.sigar_cpu_list_destroy(sigar, &cpulists)



return cpulists, goCpu


}







68 changes: 68 additions & 0 deletions go_bindings/gotoc/cpuInfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package gotoc

import (
"fmt"
"unsafe"
)
/*
#include "../../../../../Include/sigar.h"
*/
import "C"

type CpuInfo struct{
Vendor string
Model string
Mhz int
MhzMax int
MhzMin int
CacheSize uint
TotalSockets int
TotalCores int
CorePerSocket int
}

func GetCpuInfo()(result []*CpuInfo, err error){

defer func() {
if r := recover() ; r != nil {
err = fmt.Errorf("Failed to rertieve cpu info due to: %v", err)
}
}()
var sigar *C.sigar_t=GetSigarHandle()

var cpuInfoList C.sigar_cpu_info_list_t

C.sigar_cpu_info_list_get(sigar, &cpuInfoList)

var length int=int(cpuInfoList.number)

cCpuInfo:=GetGoSlice(length, unsafe.Pointer(cpuInfoList.data))

var goCpuInfo []C.sigar_cpu_info_t
goCpuInfo= *(*[]C.sigar_cpu_info_t)(unsafe.Pointer(&cCpuInfo))

C.sigar_cpu_info_list_destroy(sigar,&cpuInfoList)

result = make([]*CpuInfo, len(goCpuInfo))
for i,sigarCpuinfo := range goCpuInfo {

result[i] = &CpuInfo{
Vendor : C.GoString(&sigarCpuinfo.vendor[0]),
Model : C.GoString(&sigarCpuinfo.model[0]),
Mhz : int(sigarCpuinfo.mhz),
MhzMax : int(sigarCpuinfo.mhz_max),
MhzMin : int(sigarCpuinfo.mhz_min),
CacheSize : uint(sigarCpuinfo.cache_size),
TotalSockets : int(sigarCpuinfo.total_sockets),
TotalCores : int(sigarCpuinfo.total_cores),
CorePerSocket : int(sigarCpuinfo.cores_per_socket),
}
}

return result,nil

}
45 changes: 45 additions & 0 deletions go_bindings/gotoc/fileInfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package gotoc


import (


"unsafe"
)
/*
#include "../../../../../Include/sigar.h"
*/
import "C"


func FileInfo() []C.sigar_file_system_t{

var sigar *C.sigar_t=GetSigarHandle()
var fileSystemList C.sigar_file_system_list_t
//C.fileInfo(sigar,&fileSystemList)
C.sigar_file_system_list_get(sigar, &fileSystemList);

var length int=int(fileSystemList.number)


cFs:=GetGoSlice(length, unsafe.Pointer(fileSystemList.data))

var goFs []C.sigar_file_system_t
goFs = *(*[]C.sigar_file_system_t)(unsafe.Pointer(&cFs))

//fmt.Printf("%v\n", C.GoString(&goFs[1].dir_name[0]))
C.sigar_file_system_list_destroy(sigar, &fileSystemList);

return goFs



}





58 changes: 58 additions & 0 deletions go_bindings/gotoc/handle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package gotoc

import (

"fmt"
"reflect"
"unsafe"

)

/*
#include "../../../../../Include/sigar.h"
*/
import "C"

var sigar *C.sigar_t=nil
func GetSigarHandle()*C.sigar_t{

if(sigar!=nil){
return sigar
}
sigar=new (C.sigar_t)
ret:=C.sigar_open(&sigar)
if(ret!=C.SIGAR_OK){
return nil
}
return sigar

}

func CloseSigarHandle(sigar *C.sigar_t) {

if(sigar!=nil){
C.sigar_close(sigar)
return
}
fmt.Println("Trying to close a nil handel, ignoring")
}


func GetGoSlice(number int, pointer unsafe.Pointer ) reflect.SliceHeader{
var length int=int(number)


cObj := reflect.SliceHeader{
Data: uintptr(pointer),
Len: length,
Cap: length,
}




return cObj


}
11 changes: 11 additions & 0 deletions go_bindings/gotoc/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package gotoc

import (

)

func main(){


NetInfo()
}
48 changes: 48 additions & 0 deletions go_bindings/gotoc/mem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package gotoc


import (



)
/*
#include "../../../../../Include/sigar.h"
*/
import "C"

type Info struct {
Free int64
}

type MemInfo struct {
Mem *Info
Swap *Info
}

func GetMemInfo() (*MemInfo, error){

var sigar *C.sigar_t=GetSigarHandle()
var mem C.sigar_mem_t
var swap C.sigar_swap_t

C.sigar_mem_get( sigar, &mem)
C.sigar_swap_get(sigar, &swap);

return &MemInfo{
Mem : &Info{ Free : int64(mem.free)},
Swap : &Info{ Free : int64(swap.free)},
},nil



}





Loading

0 comments on commit 9158290

Please sign in to comment.