forked from hyperic/sigar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
to link with sigar c code
- Loading branch information
Showing
19 changed files
with
988 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
||
|
||
} | ||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package gotoc | ||
|
||
import ( | ||
|
||
) | ||
|
||
func main(){ | ||
|
||
|
||
NetInfo() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
||
|
||
} | ||
|
||
|
||
|
||
|
||
|
Oops, something went wrong.