Skip to content

Commit

Permalink
fix profiler and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
cha87de committed Jun 3, 2019
1 parent 89ef815 commit c653cd7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
17 changes: 8 additions & 9 deletions profiler/pickup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"fmt"
"strconv"

"github.com/cha87de/kvmtop/collectors"
"github.com/cha87de/kvmtop/models"

"github.com/cha87de/kvmtop/collectors/cpucollector"
)

func pickupCPU(domain models.Domain) (int, int, int) {
func pickupCPU(host models.Host, domain models.Domain) (int, int, int) {
cputimeAllCores, err := strconv.Atoi(cpucollector.CpuPrintThreadMetric(&domain, "cpu_threadIDs", "cpu_times"))
if err != nil {
fmt.Printf("err Atiu cpu_times: %v\n", err)
Expand All @@ -25,12 +24,12 @@ func pickupCPU(domain models.Domain) (int, int, int) {
return cpuUtil, min, max
}

func pickupIO(domain models.Domain) (int, int, int) {
readBytes, err := strconv.Atoi(collectors.GetMetricDiffUint64(domain.Measurable, "io_read_bytes", true))
func pickupIO(host models.Host, domain models.Domain) (int, int, int) {
readBytes, err := strconv.Atoi(domain.GetMetricDiffUint64("io_read_bytes", true))
if err != nil {
fmt.Printf("err Atiu io_read_bytes: %v\n", err)
}
writtenbytes, err := strconv.Atoi(collectors.GetMetricDiffUint64(domain.Measurable, "io_write_bytes", true))
writtenbytes, err := strconv.Atoi(domain.GetMetricDiffUint64("io_write_bytes", true))
if err != nil {
fmt.Printf("err Atiu io_write_bytes: %v\n", err)
}
Expand All @@ -42,16 +41,16 @@ func pickupIO(domain models.Domain) (int, int, int) {
return total, min, max
}

func pickupNet(domain models.Domain) (int, int, int) {
rawRx := collectors.GetMetricDiffUint64(domain.Measurable, "net_ReceivedBytes", true)
func pickupNet(host models.Host, domain models.Domain) (int, int, int) {
rawRx := domain.GetMetricDiffUint64("net_ReceivedBytes", true)
if rawRx == "" {
rawRx = "0"
}
receivedBytes, err := strconv.Atoi(rawRx)
if err != nil {
fmt.Printf("err Atiu net_ReceivedBytes: %v\n", err)
}
rawTx := collectors.GetMetricDiffUint64(domain.Measurable, "net_TransmittedBytes", true)
rawTx := domain.GetMetricDiffUint64("net_TransmittedBytes", true)
if rawTx == "" {
rawTx = "0"
}
Expand All @@ -61,7 +60,7 @@ func pickupNet(domain models.Domain) (int, int, int) {
}
total := receivedBytes + transmittedBytes
min := 0
rawNetSpeed := collectors.GetMetricUint64(models.Collection.Host.Measurable, "net_host_speed", 0)
rawNetSpeed, _ := host.GetMetricUint64("net_host_speed", 0)
if rawNetSpeed == "0" {
// set default to 1GBit/s
defaultSpeed := 1 * 1024 * 1024 * 1024
Expand Down
12 changes: 7 additions & 5 deletions profiler/profiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ func pickup() {
return true
})

host := models.Collection.Host

// for each domain ...
models.Collection.Domains.Map.Range(func(key, domainRaw interface{}) bool {
models.Collection.Domains.Range(func(key, domainRaw interface{}) bool {
domain := domainRaw.(models.Domain)
uuid := key.(string)

Expand All @@ -68,15 +70,15 @@ func pickup() {

// pick up collector measurement
metrics := make([]tsprofilerModels.TSInputMetric, 0)
models.Collection.Collectors.Map.Range(func(nameRaw interface{}, collectorRaw interface{}) bool {
models.Collection.Collectors.Range(func(nameRaw interface{}, collector models.Collector) bool {
name := nameRaw.(string)
var util, min, max int
if name == "cpu" {
util, min, max = pickupCPU(domain)
util, min, max = pickupCPU(host, domain)
} else if name == "io" {
util, min, max = pickupIO(domain)
util, min, max = pickupIO(host, domain)
} else if name == "net" {
util, min, max = pickupNet(domain)
util, min, max = pickupNet(host, domain)
}

metrics = append(metrics, tsprofilerModels.TSInputMetric{
Expand Down

0 comments on commit c653cd7

Please sign in to comment.