Skip to content

Commit

Permalink
refactoring: rm sync.Map, clean up, one file per model, fix race cond…
Browse files Browse the repository at this point in the history
…ition (fixes #29)
  • Loading branch information
cha87de committed Jun 3, 2019
1 parent d59a9a4 commit 89ef815
Show file tree
Hide file tree
Showing 38 changed files with 546 additions and 466 deletions.
6 changes: 0 additions & 6 deletions cmd/kvmprofiler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/cha87de/kvmtop/config"
"github.com/cha87de/kvmtop/connector"
"github.com/cha87de/kvmtop/models"
"github.com/cha87de/kvmtop/profiler"
"github.com/cha87de/kvmtop/runners"
)
Expand All @@ -25,11 +24,6 @@ func main() {
os.Exit(1)
}

// initialize host measureable
models.Collection.Host = &models.Host{
Measurable: &models.Measurable{},
}

// start lookup and collect runners
var wg sync.WaitGroup
wg.Add(1) // terminate when first thread terminates
Expand Down
5 changes: 0 additions & 5 deletions cmd/kvmtop/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ func main() {
os.Exit(1)
}

// initialize host measureable
models.Collection.Host = &models.Host{
Measurable: &models.Measurable{},
}

// start runners
runners.InitializeRunners()

Expand Down
87 changes: 0 additions & 87 deletions collectors/collectors.go

This file was deleted.

12 changes: 6 additions & 6 deletions collectors/cpucollector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type Collector struct {

// Lookup cpu collector data
func (collector *Collector) Lookup() {
models.Collection.Domains.Map.Range(func(key, value interface{}) bool {
models.Collection.Domains.Range(func(key, value interface{}) bool {
uuid := key.(string)
domain := value.(models.Domain)
libvirtDomain, _ := models.Collection.LibvirtDomains.Load(uuid)
Expand All @@ -21,21 +21,21 @@ func (collector *Collector) Lookup() {
})

// lookup details for host
cpuLookupHost(models.Collection.Host)
cpuLookupHost(&models.Collection.Host)
}

// Collect cpu collector data
func (collector *Collector) Collect() {
// lookup for each domain
models.Collection.Domains.Map.Range(func(key, value interface{}) bool {
models.Collection.Domains.Range(func(key, value interface{}) bool {
// uuid := key.(string)
domain := value.(models.Domain)
cpuCollect(&domain)
return true
})

// collect host measurements
cpuCollectHost(models.Collection.Host)
cpuCollectHost(&models.Collection.Host)
}

// Print returns the collectors measurements in a Printable struct
Expand Down Expand Up @@ -68,15 +68,15 @@ func (collector *Collector) Print() models.Printable {

// lookup for each domain
printable.DomainValues = make(map[string][]string)
models.Collection.Domains.Map.Range(func(key, value interface{}) bool {
models.Collection.Domains.Range(func(key, value interface{}) bool {
uuid := key.(string)
domain := value.(models.Domain)
printable.DomainValues[uuid] = cpuPrint(&domain)
return true
})

// lookup for host
printable.HostValues = cpuPrintHost(models.Collection.Host)
printable.HostValues = cpuPrintHost(&models.Collection.Host)

return printable
}
Expand Down
9 changes: 4 additions & 5 deletions collectors/cpucollector/host.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cpucollector

import (
"github.com/cha87de/kvmtop/collectors"
"github.com/cha87de/kvmtop/config"
"github.com/cha87de/kvmtop/models"

Expand Down Expand Up @@ -39,10 +38,10 @@ func cpuCollectHost(host *models.Host) {
}

func cpuPrintHost(host *models.Host) []string {
cpuMinfreq := collectors.GetMetricFloat64(host.Measurable, "cpu_minfreq", 0)
cpuMaxfreq := collectors.GetMetricFloat64(host.Measurable, "cpu_maxfreq", 0)
cpuCurfreq := collectors.GetMetricFloat64(host.Measurable, "cpu_curfreq", 0)
cpuCores := collectors.GetMetricUint64(host.Measurable, "cpu_cores", 0)
cpuMinfreq := host.GetMetricFloat64("cpu_minfreq", 0)
cpuMaxfreq := host.GetMetricFloat64("cpu_maxfreq", 0)
cpuCurfreq := host.GetMetricFloat64("cpu_curfreq", 0)
cpuCores, _ := host.GetMetricUint64("cpu_cores", 0)

// put results together
result := append([]string{cpuCores}, cpuCurfreq)
Expand Down
5 changes: 2 additions & 3 deletions collectors/cpucollector/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"fmt"

"github.com/cha87de/kvmtop/collectors"
"github.com/cha87de/kvmtop/config"
"github.com/cha87de/kvmtop/models"
"github.com/cha87de/kvmtop/util"
Expand Down Expand Up @@ -104,7 +103,7 @@ func cpuCollectMeasurements(domain *models.Domain, metricName string, measuremen
}

func cpuPrint(domain *models.Domain) []string {
cores := collectors.GetMetricUint64(domain.Measurable, "cpu_cores", 0)
cores, _ := domain.GetMetricUint64("cpu_cores", 0)

// cpu util for vcores
cputimeAllCores := CpuPrintThreadMetric(domain, "cpu_threadIDs", "cpu_times")
Expand All @@ -128,7 +127,7 @@ func CpuPrintThreadMetric(domain *models.Domain, lookupMetric string, metric str
var measurementCount int
for _, threadID := range threadIDs {
metricName := fmt.Sprint(metric, "_", threadID)
measurementStr := collectors.GetMetricDiffUint64(domain.Measurable, metricName, true)
measurementStr := domain.GetMetricDiffUint64(metricName, true)
if measurementStr == "" {
continue
}
Expand Down
15 changes: 7 additions & 8 deletions collectors/diskcollector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package diskcollector
import (
"strings"

"github.com/cha87de/kvmtop/collectors"
"github.com/cha87de/kvmtop/config"
"github.com/cha87de/kvmtop/models"
)
Expand All @@ -17,14 +16,14 @@ type Collector struct {
func (collector *Collector) Lookup() {
hostDiskSources := ""

models.Collection.Domains.Map.Range(func(key, value interface{}) bool {
models.Collection.Domains.Range(func(key, value interface{}) bool {
uuid := key.(string)
domain := value.(models.Domain)
libvirtDomain, _ := models.Collection.LibvirtDomains.Load(uuid)

diskLookup(&domain, libvirtDomain)
// merge sourcedir metrics from domains to one metric for host
disksources := strings.Split(collectors.GetMetricString(domain.Measurable, "disk_sources", 0), ",")
disksources := strings.Split(domain.GetMetricString("disk_sources", 0), ",")
for _, disksource := range disksources {
if !strings.Contains(hostDiskSources, disksource) {
if hostDiskSources != "" {
Expand All @@ -39,19 +38,19 @@ func (collector *Collector) Lookup() {

models.Collection.Host.AddMetricMeasurement("disk_sources", models.CreateMeasurement(hostDiskSources))

diskHostLookup(models.Collection.Host)
diskHostLookup(&models.Collection.Host)
}

// Collect disk collector data
func (collector *Collector) Collect() {
// lookup for each domain
models.Collection.Domains.Map.Range(func(key, value interface{}) bool {
models.Collection.Domains.Range(func(key, value interface{}) bool {
// uuid := key.(string)
domain := value.(models.Domain)
diskCollect(&domain)
return true
})
diskHostCollect(models.Collection.Host)
diskHostCollect(&models.Collection.Host)
}

// Print returns the collectors measurements in a Printable struct
Expand Down Expand Up @@ -96,15 +95,15 @@ func (collector *Collector) Print() models.Printable {

// lookup for each domain
printable.DomainValues = make(map[string][]string)
models.Collection.Domains.Map.Range(func(key, value interface{}) bool {
models.Collection.Domains.Range(func(key, value interface{}) bool {
uuid := key.(string)
domain := value.(models.Domain)
printable.DomainValues[uuid] = diskPrint(&domain)
return true
})

// lookup for host
printable.HostValues = diskPrintHost(models.Collection.Host)
printable.HostValues = diskPrintHost(&models.Collection.Host)

return printable
}
Expand Down
25 changes: 12 additions & 13 deletions collectors/diskcollector/host-worker.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package diskcollector

import (
"github.com/cha87de/kvmtop/collectors"
"github.com/cha87de/kvmtop/config"
"github.com/cha87de/kvmtop/util"

Expand All @@ -14,7 +13,7 @@ func diskHostLookup(host *models.Host) {
// find relevant devices
devices := []string{}
mounts := util.GetProcMounts()
diskSources := strings.Split(collectors.GetMetricString(host.Measurable, "disk_sources", 0), ",")
diskSources := strings.Split(host.GetMetricString("disk_sources", 0), ",")
for _, source := range diskSources {
// find best matching mountpoint
var bestMount util.ProcMount
Expand Down Expand Up @@ -99,17 +98,17 @@ func diskHostCollect(host *models.Host) {
}

func diskPrintHost(host *models.Host) []string {
diskDeviceReads := collectors.GetMetricDiffUint64(host.Measurable, "disk_device_reads", true)
diskDeviceReadsmerged := collectors.GetMetricDiffUint64(host.Measurable, "disk_device_readsmerged", true)
diskDeviceSectorsread := collectors.GetMetricDiffUint64(host.Measurable, "disk_device_sectorsread", true)
diskDeviceTimereading := collectors.GetMetricDiffUint64(host.Measurable, "disk_device_timereading", true)
diskDeviceWrites := collectors.GetMetricDiffUint64(host.Measurable, "disk_device_writes", true)
diskDeviceWritesmerged := collectors.GetMetricDiffUint64(host.Measurable, "disk_device_writesmerged", true)
diskDeviceSectorswritten := collectors.GetMetricDiffUint64(host.Measurable, "disk_device_sectorswritten", true)
diskDeviceTimewriting := collectors.GetMetricDiffUint64(host.Measurable, "disk_device_timewriting", true)
diskDeviceCurrentops := collectors.GetMetricDiffUint64(host.Measurable, "disk_device_currentops", true)
diskDeviceTimeforops := collectors.GetMetricDiffUint64(host.Measurable, "disk_device_timeforops", true)
diskDeviceWeightedtimeforops := collectors.GetMetricDiffUint64(host.Measurable, "disk_device_weightedtimeforops", true)
diskDeviceReads := host.GetMetricDiffUint64("disk_device_reads", true)
diskDeviceReadsmerged := host.GetMetricDiffUint64("disk_device_readsmerged", true)
diskDeviceSectorsread := host.GetMetricDiffUint64("disk_device_sectorsread", true)
diskDeviceTimereading := host.GetMetricDiffUint64("disk_device_timereading", true)
diskDeviceWrites := host.GetMetricDiffUint64("disk_device_writes", true)
diskDeviceWritesmerged := host.GetMetricDiffUint64("disk_device_writesmerged", true)
diskDeviceSectorswritten := host.GetMetricDiffUint64("disk_device_sectorswritten", true)
diskDeviceTimewriting := host.GetMetricDiffUint64("disk_device_timewriting", true)
diskDeviceCurrentops := host.GetMetricDiffUint64("disk_device_currentops", true)
diskDeviceTimeforops := host.GetMetricDiffUint64("disk_device_timeforops", true)
diskDeviceWeightedtimeforops := host.GetMetricDiffUint64("disk_device_weightedtimeforops", true)

result := append([]string{diskDeviceReads}, diskDeviceWrites)
if config.Options.Verbose {
Expand Down
31 changes: 15 additions & 16 deletions collectors/diskcollector/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"path/filepath"
"strings"

"github.com/cha87de/kvmtop/collectors"
"github.com/cha87de/kvmtop/config"
"github.com/cha87de/kvmtop/models"
"github.com/cha87de/kvmtop/util"
Expand Down Expand Up @@ -158,21 +157,21 @@ func diskCollect(domain *models.Domain) {
}

func diskPrint(domain *models.Domain) []string {
capacity := collectors.GetMetricUint64(domain.Measurable, "disk_size_capacity", 0)
allocation := collectors.GetMetricUint64(domain.Measurable, "disk_size_allocation", 0)
physical := collectors.GetMetricUint64(domain.Measurable, "disk_size_physical", 0)

// errs := collectors.GetMetricDiffUint64(domain.Measurable, "disk_stats_errs", true)
flushreq := collectors.GetMetricDiffUint64(domain.Measurable, "disk_stats_flushreq", true)
flushtotaltimes := collectors.GetMetricDiffUint64(domain.Measurable, "disk_stats_flushtotaltimes", true)
rdbytes := collectors.GetMetricDiffUint64(domain.Measurable, "disk_stats_rdbytes", true)
rdreq := collectors.GetMetricDiffUint64(domain.Measurable, "disk_stats_rdreq", true)
rdtotaltimes := collectors.GetMetricDiffUint64(domain.Measurable, "disk_stats_rdtotaltimes", true)
wrbytes := collectors.GetMetricDiffUint64(domain.Measurable, "disk_stats_wrbytes", true)
wrreq := collectors.GetMetricDiffUint64(domain.Measurable, "disk_stats_wrreq", true)
wrtotaltimes := collectors.GetMetricDiffUint64(domain.Measurable, "disk_stats_wrtotaltimes", true)

delayblkio := collectors.GetMetricDiffUint64(domain.Measurable, "disk_delayblkio", true)
capacity,_ := domain.GetMetricUint64("disk_size_capacity", 0)
allocation,_ := domain.GetMetricUint64("disk_size_allocation", 0)
physical,_ := domain.GetMetricUint64("disk_size_physical", 0)

// errs := domain.GetMetricDiffUint64("disk_stats_errs", true)
flushreq := domain.GetMetricDiffUint64("disk_stats_flushreq", true)
flushtotaltimes := domain.GetMetricDiffUint64("disk_stats_flushtotaltimes", true)
rdbytes := domain.GetMetricDiffUint64("disk_stats_rdbytes", true)
rdreq := domain.GetMetricDiffUint64("disk_stats_rdreq", true)
rdtotaltimes := domain.GetMetricDiffUint64("disk_stats_rdtotaltimes", true)
wrbytes := domain.GetMetricDiffUint64("disk_stats_wrbytes", true)
wrreq := domain.GetMetricDiffUint64("disk_stats_wrreq", true)
wrtotaltimes := domain.GetMetricDiffUint64("disk_stats_wrtotaltimes", true)

delayblkio := domain.GetMetricDiffUint64("disk_delayblkio", true)

result := append([]string{capacity}, allocation)
if config.Options.Verbose {
Expand Down
Loading

0 comments on commit 89ef815

Please sign in to comment.