Skip to content

Commit

Permalink
Merge pull request #4 from SonicCloudOrg/fix-pefd
Browse files Browse the repository at this point in the history
Fix pefd
  • Loading branch information
ZhouYixun authored Nov 9, 2022
2 parents e8bbd17 + c1a9dd7 commit ffa9b0e
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 31 deletions.
34 changes: 33 additions & 1 deletion device.go
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,39 @@ func (d *device) PerfStart(opts ...PerfOption) (data <-chan []byte, err error) {
outCh := make(chan []byte, 100)

if perfOptions.SysCPU || perfOptions.SysMem || perfOptions.SysDisk ||
perfOptions.SysNetwork {
perfOptions.SysNetwork || len(perfOptions.ProcessAttributes) > 1 {

sysCpuAndMemAttr := []string{
"vmCompressorPageCount",
"vmExtPageCount",
"vmFreeCount",
"vmIntPageCount",
"vmPurgeableCount",
"vmWireCount",
"vmUsedCount",
"__vmSwapUsage"}

if perfOptions.SysMem || perfOptions.SysCPU {
perfOptions.SystemAttributes = append(perfOptions.SystemAttributes, sysCpuAndMemAttr...)
}

if perfOptions.SysDisk {
diskAttr := []string{ // disk
"diskBytesRead",
"diskBytesWritten",
"diskReadOps",
"diskWriteOps"}
perfOptions.SystemAttributes = append(perfOptions.SystemAttributes, diskAttr...)
}

if perfOptions.SysNetwork {
networkAttr := []string{ // network
"netBytesIn",
"netBytesOut",
"netPacketsIn",
"netPacketsOut"}
perfOptions.SystemAttributes = append(perfOptions.SystemAttributes, networkAttr...)
}
perfd, err := d.newPerfdSysmontap(perfOptions)
if err != nil {
return nil, err
Expand Down
40 changes: 10 additions & 30 deletions perfd.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,36 +32,16 @@ type PerfOptions struct {

func defaulPerfOption() *PerfOptions {
return &PerfOptions{
SysCPU: true, // default on
SysMem: true, // default on
SysDisk: false,
SysNetwork: false,
gpu: false,
FPS: false,
Network: false,
OutputInterval: 1000, // default 1000ms
SystemAttributes: []string{
// disk
"diskBytesRead",
"diskBytesWritten",
"diskReadOps",
"diskWriteOps",
// memory
"vmCompressorPageCount",
"vmExtPageCount",
"vmFreeCount",
"vmIntPageCount",
"vmPurgeableCount",
"vmWireCount",
"vmUsedCount",
"__vmSwapUsage",
// network
"netBytesIn",
"netBytesOut",
"netPacketsIn",
"netPacketsOut",
},
ProcessAttributes: []string{
SysCPU: false, // default on
SysMem: false, // default on
SysDisk: false,
SysNetwork: false,
gpu: false,
FPS: false,
Network: false,
OutputInterval: 1000, // default 1000ms
SystemAttributes: []string{},
ProcessAttributes: []string{ // default cpuUsage
"pid",
"cpuUsage",
},
Expand Down
70 changes: 70 additions & 0 deletions perfd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,76 @@ func TestPerfSystemMonitor(t *testing.T) {
}
}

func TestPerfSystemCpu(t *testing.T) {
setupLockdownSrv(t)

data, err := dev.PerfStart(
WithPerfSystemCPU(true),
WithPerfOutputInterval(1000),
)
if err != nil {
t.Fatal(err)
}

timer := time.NewTimer(time.Duration(time.Second * 10))
for {
select {
case <-timer.C:
dev.PerfStop()
return
case d := <-data:
fmt.Println(string(d))
}
}
}

func TestPerfSystemMem(t *testing.T) {
setupLockdownSrv(t)

data, err := dev.PerfStart(
WithPerfSystemMem(true),
WithPerfOutputInterval(1000),
)
if err != nil {
t.Fatal(err)
}

timer := time.NewTimer(time.Duration(time.Second * 10))
for {
select {
case <-timer.C:
dev.PerfStop()
return
case d := <-data:
fmt.Println(string(d))
}
}
}

func TestPerfNotSystemPerfData(t *testing.T) {
setupLockdownSrv(t)

data, err := dev.PerfStart(
WithPerfSystemMem(false),
WithPerfSystemCPU(false),
WithPerfOutputInterval(1000),
)
if err != nil {
t.Fatal(err)
}

timer := time.NewTimer(time.Duration(time.Second * 10))
for {
select {
case <-timer.C:
dev.PerfStop()
return
case d := <-data:
fmt.Println(string(d))
}
}
}

func TestPerfProcessMonitor(t *testing.T) {
setupLockdownSrv(t)

Expand Down

0 comments on commit ffa9b0e

Please sign in to comment.