Skip to content

Commit

Permalink
Go sources update
Browse files Browse the repository at this point in the history
With latest changes from Lean Agent
  • Loading branch information
ilanuriel committed Jan 4, 2016
1 parent 9158290 commit ad47dc3
Show file tree
Hide file tree
Showing 14 changed files with 1,211 additions and 133 deletions.
125 changes: 120 additions & 5 deletions go_bindings/gotoc/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,134 @@ package gotoc


import (


"github.com/vmware/leap/util"
"unsafe"
"fmt"
"time"
"strconv"
)


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




var _prevCpuUsage C.sigar_cpu_t //usged for percentage calculation
var prevSampleTs int64

type CpuUsageInfo struct {
User uint64
Sys uint64
Nice uint64
Idle uint64
Wait uint64
Irq uint64
SoftIrq uint64
Stolen uint64
Total uint64
}

type CpuPctUsageInfo struct {
User float64
Sys float64
Nice float64
Idle float64
Wait float64
Irq float64
SoftIrq float64
Stolen float64
Combined float64
}
func (this *CpuPctUsageInfo) String() string {
return "CPU states: "+
strconv.FormatFloat(this.User,'f', 1, 64)+"% user, "+
strconv.FormatFloat(this.Sys,'f', 1, 64)+"% system, "+
strconv.FormatFloat(this.Nice,'f', 1, 64)+"% nice, "+
strconv.FormatFloat(this.Wait,'f', 1, 64) +"% wait, "+
strconv.FormatFloat(this.Idle,'f', 1, 64)+"% idle"
}

//------------------------------------------------------------------------------------------------------------------------------------------------
// CpuPctUsageInfo (Float64) CpuPctUsageInfo (Float64) CpuPctUsageInfo (Float64) CpuPctUsageInfo (Float64) CpuPctUsageInfo (Float64)
//------------------------------------------------------------------------------------------------------------------------------------------------
func GetCpuPctUsageInfo() (result *CpuPctUsageInfo, err error) {
defer util.Panic2Error(&err)

sigar := GetSigarHandle()
cur_c_cpu_t, err := _getCpu(sigar)
if err != nil {
return nil,err
}

if prevSampleTs == 0 {

_prevCpuUsage = *cur_c_cpu_t
time.Sleep(500*time.Millisecond)

cur_c_cpu_t, err = _getCpu(sigar)
if err != nil {
return nil,err
}
}

var c_pct_cpu_t C.sigar_cpu_perc_t
C.sigar_cpu_perc_calculate(&_prevCpuUsage, cur_c_cpu_t, &c_pct_cpu_t)
_prevCpuUsage = *cur_c_cpu_t
prevSampleTs = util.NowEpoch()

return &CpuPctUsageInfo{
User : float64(c_pct_cpu_t.user),
Sys : float64(c_pct_cpu_t.sys),
Nice : float64(c_pct_cpu_t.nice),
Idle : float64(c_pct_cpu_t.idle),
Wait : float64(c_pct_cpu_t.wait),
Irq : float64(c_pct_cpu_t.irq),
SoftIrq : float64(c_pct_cpu_t.soft_irq),
Stolen : float64(c_pct_cpu_t.stolen),
Combined : float64(c_pct_cpu_t.combined),
},nil

}

//------------------------------------------------------------------------------------------------------------------------------------------------
//CpuUsageInfo (uint64) CpuUsageInfo (uint64) CpuUsageInfo (uint64) CpuUsageInfo (uint64) CpuUsageInfo (uint64) CpuUsageInfo (uint64)
//------------------------------------------------------------------------------------------------------------------------------------------------
func GetCpuUsageInfo() (result *CpuUsageInfo, err error) {
defer util.Panic2Error(&err)

c_cpu_t, err := _getCpu(GetSigarHandle())
if err != nil {
return nil,err
}

return &CpuUsageInfo{
User : uint64(c_cpu_t.user),
Sys : uint64(c_cpu_t.sys),
Nice : uint64(c_cpu_t.nice),
Idle : uint64(c_cpu_t.idle),
Wait : uint64(c_cpu_t.wait),
Irq : uint64(c_cpu_t.irq),
SoftIrq : uint64(c_cpu_t.soft_irq),
Stolen : uint64(c_cpu_t.stolen),
Total : uint64(c_cpu_t.total),
},nil
}

//------------------------------------------------------------------------------------------------------------------------------------------------
//Helpers Helpers Helpers Helpers Helpers Helpers Helpers Helpers Helpers Helpers Helpers Helpers Helpers Helpers Helpers Helpers Helpers Helpers
//------------------------------------------------------------------------------------------------------------------------------------------------
func _getCpu(sigar *C.sigar_t) (*C.sigar_cpu_t, error) {

var c_cpu_t C.sigar_cpu_t
if status := int(C.sigar_cpu_get(sigar, &c_cpu_t)) ; status != SIGAR_OK {
return nil,fmt.Errorf("Failed to retrieve cpu usage info with error: %v", status)
}

return &c_cpu_t,nil
}
//------------------------------------------------------------------------------------------------------------------------------------------------
//List List List List List List List List List List List List List List List List List List List List List List List List List List List List
//------------------------------------------------------------------------------------------------------------------------------------------------

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

Expand Down
71 changes: 59 additions & 12 deletions go_bindings/gotoc/fileInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package gotoc


import (


"fmt"
"unsafe"
"strconv"
)
/*
#include "../../../../../Include/sigar.h"
Expand All @@ -15,30 +15,77 @@ import (
import "C"


func FileInfo() []C.sigar_file_system_t{
const (
FSTYPE_UNKNOWN = iota
FSTYPE_NONE
FSTYPE_LOCAL_DISK
FSTYPE_NETWORK
FSTYPE_RAM_DISK
FSTYPE_CDROM
FSTYPE_SWAP
FSTYPE_MAX
)


type FsInfo struct {
DirName string
DevName string
TypeName string
SysTypeName string
Options string
Type int
Flags uint64
}
func (this *FsInfo) String() string {
return fmt.Sprintf("{Dirname: %v, DevName: %v, TypeName: %v, SysTypeName: %v, Options: %v, Type: %v, Flags:%v",
this.DirName,
this.DevName,
this.TypeName,
this.SysTypeName,
this.Options,
this.Type,
this.Flags,
)
}

func GetFsInfo() (result []*FsInfo, err error){
defer func() {
if r := recover() ; r != nil{
err = fmt.Errorf("Failed to discover file systems due to: " + err.Error())
}
}()
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);
defer C.sigar_file_system_list_destroy(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


result = make([]*FsInfo, length)
for i,fs := range goFs {
fsType,err := strconv.Atoi(fmt.Sprintf("%v", fs._type))
if err != nil {
fsType = FSTYPE_UNKNOWN
}
result[i] = &FsInfo{
DirName : C.GoString(&fs.dir_name[0]),
DevName : C.GoString(&fs.dev_name[0]),
TypeName : C.GoString(&fs.type_name[0]),
SysTypeName : C.GoString(&fs.sys_type_name[0]),
Options : C.GoString(&fs.options[0]),
Type : fsType,
Flags : uint64(fs.flags),
}
}

return result,nil

}




Expand Down
122 changes: 122 additions & 0 deletions go_bindings/gotoc/fsusage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package gotoc


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

type sigar_file_system_usage_t struct {
UsedPct float64
Total int64
Free int64
Used int64
Avail int64
Files int64
FreeFiles int64
}

type FsUsage struct {
UsedPct float64
Total int64
Free int64
Used int64
Avail int64
Files int64
FreeFiles int64
}

func (this *FsUsage) String() string { return fmt.Sprintf("used: %v, total: %v, free: %v, used: %v, avail: %v, files: %v, freeFiles: %v", this.UsedPct, this.Total, this.Free, this.Used, this.Avail, this.Files, this.FreeFiles) }

type DiskUsage struct {
Reads int64
Writes int64
WriteBytes int64
ReadBytes int64
Rtime int64
Wtime int64
Qtime int64
Time int64
SnapTime int64
ServiceTime float64
Queue float64
}

type FsUsageInfo struct {
Fs *FsUsage
Disk *DiskUsage
}

func GetFsUsageInfo(dirName string, diskName string)(fsUsageInfo *FsUsageInfo,err error){

var sigar *C.sigar_t=GetSigarHandle()
//TODO: noticed that sigar_file_system_usage_t contains a sigar_disk_usage_t member, should there be two invocations?
fsUsage, err := _getFsUsage(dirName, sigar)
if err != nil {
return nil, err
}

diskUsage, err := _getDiskUsage(diskName, sigar) ;
if err != nil {
return nil,err
}


return &FsUsageInfo {fsUsage, diskUsage}, err
}


func GetFsUsage(dirName string) (*FsUsage, error) {
return _getFsUsage(dirName, GetSigarHandle())
}

func _getFsUsage(dirName string, sigar *C.sigar_t) (out *FsUsage, err error) {
var fsusage C.sigar_file_system_usage_t

dir:=C.CString(dirName)

C.free(unsafe.Pointer(dir))

C.sigar_file_system_usage_get(sigar , dir, &fsusage)

out = &FsUsage{
float64(fsusage.use_percent),
int64(fsusage.total),
int64(fsusage.free),
int64(fsusage.used),
int64(fsusage.avail),
int64(fsusage.files),
int64(fsusage.free_files),
}

return out,nil
}

func GetDiskUsage(diskName string) (*DiskUsage, error) {
return _getDiskUsage(diskName, GetSigarHandle())
}

func _getDiskUsage(diskName string, sigar *C.sigar_t) (*DiskUsage, error) {
var diskusage C.sigar_disk_usage_t
disk:=C.CString(diskName)
defer C.free(unsafe.Pointer(disk))
C.sigar_disk_usage_get(sigar , disk, &diskusage)
return &DiskUsage {
int64(diskusage.reads),
int64(diskusage.writes),
int64(diskusage.write_bytes),
int64(diskusage.read_bytes),
int64(diskusage.rtime),
int64(diskusage.wtime),
int64(diskusage.qtime),
int64(diskusage.time),
int64(diskusage.snaptime),
float64(diskusage.service_time),
float64(diskusage.queue),
},nil
}
14 changes: 11 additions & 3 deletions go_bindings/gotoc/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,19 @@ func GetGoSlice(number int, pointer unsafe.Pointer ) reflect.SliceHeader{
Len: length,
Cap: length,
}




return cObj
}


func CArr2SlicePtr(length int, cArray interface{}) unsafe.Pointer{

vl := reflect.ValueOf(cArray)
header := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(vl.Pointer())),
Len: length,
Cap: length,
}

return unsafe.Pointer(&header)
}
Loading

0 comments on commit ad47dc3

Please sign in to comment.