Skip to content

Commit 9158290

Browse files
committed
go code
to link with sigar c code
1 parent 0f0dbe3 commit 9158290

19 files changed

+988
-0
lines changed

go_bindings/gotoc/avg.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package gotoc
2+
3+
import (
4+
5+
6+
7+
)
8+
/*
9+
10+
#include "../../../../../Include/sigar.h"
11+
12+
13+
14+
*/
15+
import "C"
16+
17+
func Avg()C.sigar_loadavg_t{
18+
19+
var sigar *C.sigar_t=GetSigarHandle()
20+
21+
var avg C.sigar_loadavg_t
22+
23+
24+
C.sigar_loadavg_get(sigar, &avg)
25+
26+
27+
return avg
28+
29+
}

go_bindings/gotoc/cpu.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package gotoc
2+
3+
4+
import (
5+
6+
7+
"unsafe"
8+
)
9+
10+
11+
//#include "../../../../../Include/sigar.h"
12+
import "C"
13+
14+
15+
16+
17+
18+
19+
func Cpu() (C.sigar_cpu_list_t,[]C.sigar_cpu_t){
20+
21+
22+
23+
var sigar *C.sigar_t=GetSigarHandle()
24+
25+
var cpulists C.sigar_cpu_list_t
26+
27+
C.sigar_cpu_list_get(sigar, &cpulists)
28+
29+
var length int=int(cpulists.number)
30+
31+
32+
usp:=GetGoSlice(length, unsafe.Pointer(cpulists.data))
33+
34+
var goCpu []C.sigar_cpu_t
35+
goCpu = *(*[]C.sigar_cpu_t)(unsafe.Pointer(&usp))
36+
37+
38+
C.sigar_cpu_list_destroy(sigar, &cpulists)
39+
40+
41+
42+
return cpulists, goCpu
43+
44+
45+
}
46+
47+
48+
49+
50+
51+
52+

go_bindings/gotoc/cpuInfo.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package gotoc
2+
3+
import (
4+
"fmt"
5+
"unsafe"
6+
)
7+
/*
8+
9+
#include "../../../../../Include/sigar.h"
10+
11+
12+
13+
*/
14+
import "C"
15+
16+
type CpuInfo struct{
17+
Vendor string
18+
Model string
19+
Mhz int
20+
MhzMax int
21+
MhzMin int
22+
CacheSize uint
23+
TotalSockets int
24+
TotalCores int
25+
CorePerSocket int
26+
}
27+
28+
func GetCpuInfo()(result []*CpuInfo, err error){
29+
30+
defer func() {
31+
if r := recover() ; r != nil {
32+
err = fmt.Errorf("Failed to rertieve cpu info due to: %v", err)
33+
}
34+
}()
35+
var sigar *C.sigar_t=GetSigarHandle()
36+
37+
var cpuInfoList C.sigar_cpu_info_list_t
38+
39+
C.sigar_cpu_info_list_get(sigar, &cpuInfoList)
40+
41+
var length int=int(cpuInfoList.number)
42+
43+
cCpuInfo:=GetGoSlice(length, unsafe.Pointer(cpuInfoList.data))
44+
45+
var goCpuInfo []C.sigar_cpu_info_t
46+
goCpuInfo= *(*[]C.sigar_cpu_info_t)(unsafe.Pointer(&cCpuInfo))
47+
48+
C.sigar_cpu_info_list_destroy(sigar,&cpuInfoList)
49+
50+
result = make([]*CpuInfo, len(goCpuInfo))
51+
for i,sigarCpuinfo := range goCpuInfo {
52+
53+
result[i] = &CpuInfo{
54+
Vendor : C.GoString(&sigarCpuinfo.vendor[0]),
55+
Model : C.GoString(&sigarCpuinfo.model[0]),
56+
Mhz : int(sigarCpuinfo.mhz),
57+
MhzMax : int(sigarCpuinfo.mhz_max),
58+
MhzMin : int(sigarCpuinfo.mhz_min),
59+
CacheSize : uint(sigarCpuinfo.cache_size),
60+
TotalSockets : int(sigarCpuinfo.total_sockets),
61+
TotalCores : int(sigarCpuinfo.total_cores),
62+
CorePerSocket : int(sigarCpuinfo.cores_per_socket),
63+
}
64+
}
65+
66+
return result,nil
67+
68+
}

go_bindings/gotoc/fileInfo.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package gotoc
2+
3+
4+
import (
5+
6+
7+
"unsafe"
8+
)
9+
/*
10+
#include "../../../../../Include/sigar.h"
11+
12+
13+
14+
*/
15+
import "C"
16+
17+
18+
func FileInfo() []C.sigar_file_system_t{
19+
20+
var sigar *C.sigar_t=GetSigarHandle()
21+
var fileSystemList C.sigar_file_system_list_t
22+
//C.fileInfo(sigar,&fileSystemList)
23+
C.sigar_file_system_list_get(sigar, &fileSystemList);
24+
25+
var length int=int(fileSystemList.number)
26+
27+
28+
cFs:=GetGoSlice(length, unsafe.Pointer(fileSystemList.data))
29+
30+
var goFs []C.sigar_file_system_t
31+
goFs = *(*[]C.sigar_file_system_t)(unsafe.Pointer(&cFs))
32+
33+
//fmt.Printf("%v\n", C.GoString(&goFs[1].dir_name[0]))
34+
C.sigar_file_system_list_destroy(sigar, &fileSystemList);
35+
36+
return goFs
37+
38+
39+
40+
}
41+
42+
43+
44+
45+

go_bindings/gotoc/handle.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package gotoc
2+
3+
import (
4+
5+
"fmt"
6+
"reflect"
7+
"unsafe"
8+
9+
)
10+
11+
/*
12+
#include "../../../../../Include/sigar.h"
13+
14+
*/
15+
import "C"
16+
17+
var sigar *C.sigar_t=nil
18+
func GetSigarHandle()*C.sigar_t{
19+
20+
if(sigar!=nil){
21+
return sigar
22+
}
23+
sigar=new (C.sigar_t)
24+
ret:=C.sigar_open(&sigar)
25+
if(ret!=C.SIGAR_OK){
26+
return nil
27+
}
28+
return sigar
29+
30+
}
31+
32+
func CloseSigarHandle(sigar *C.sigar_t) {
33+
34+
if(sigar!=nil){
35+
C.sigar_close(sigar)
36+
return
37+
}
38+
fmt.Println("Trying to close a nil handel, ignoring")
39+
}
40+
41+
42+
func GetGoSlice(number int, pointer unsafe.Pointer ) reflect.SliceHeader{
43+
var length int=int(number)
44+
45+
46+
cObj := reflect.SliceHeader{
47+
Data: uintptr(pointer),
48+
Len: length,
49+
Cap: length,
50+
}
51+
52+
53+
54+
55+
return cObj
56+
57+
58+
}

go_bindings/gotoc/main.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package gotoc
2+
3+
import (
4+
5+
)
6+
7+
func main(){
8+
9+
10+
NetInfo()
11+
}

go_bindings/gotoc/mem.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package gotoc
2+
3+
4+
import (
5+
6+
7+
8+
)
9+
/*
10+
11+
#include "../../../../../Include/sigar.h"
12+
13+
14+
15+
*/
16+
import "C"
17+
18+
type Info struct {
19+
Free int64
20+
}
21+
22+
type MemInfo struct {
23+
Mem *Info
24+
Swap *Info
25+
}
26+
27+
func GetMemInfo() (*MemInfo, error){
28+
29+
var sigar *C.sigar_t=GetSigarHandle()
30+
var mem C.sigar_mem_t
31+
var swap C.sigar_swap_t
32+
33+
C.sigar_mem_get( sigar, &mem)
34+
C.sigar_swap_get(sigar, &swap);
35+
36+
return &MemInfo{
37+
Mem : &Info{ Free : int64(mem.free)},
38+
Swap : &Info{ Free : int64(swap.free)},
39+
},nil
40+
41+
42+
43+
}
44+
45+
46+
47+
48+

0 commit comments

Comments
 (0)