Skip to content

Commit

Permalink
terminate profilers for deleted vms
Browse files Browse the repository at this point in the history
  • Loading branch information
cha87de committed Nov 2, 2018
1 parent b669811 commit 74ddaed
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
28 changes: 26 additions & 2 deletions profiler/profiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/cha87de/kvmtop/config"
"github.com/cha87de/kvmtop/models"
"github.com/cha87de/kvmtop/printers"
"github.com/cha87de/kvmtop/util"
)

var domainProfiler sync.Map
Expand All @@ -32,12 +33,20 @@ func InitializeProfiler(wg *sync.WaitGroup) {
}

func pickup() {

// create list of cached profilers
domIDs := make([]string, 0)
domainProfiler.Range(func(key, _ interface{}) bool {
domIDs = append(domIDs, key.(string))
return true
})

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

// write measurements to profiler
// get or create profiler
profilerRaw, found := domainProfiler.Load(uuid)
var profiler spec.TSProfiler
if found {
Expand All @@ -52,6 +61,7 @@ func pickup() {
})
}

// pick up collector measurement
metrics := make([]spec.TSDataMetric, 0)
models.Collection.Collectors.Map.Range(func(nameRaw interface{}, collectorRaw interface{}) bool {
name := nameRaw.(string)
Expand All @@ -70,14 +80,28 @@ func pickup() {
return true
})

// send measurement to profiler
tsdata := spec.TSData{
Metrics: metrics,
}
profiler.Put(tsdata)

// store profiler
domainProfiler.Store(uuid, profiler)

// mark domain as considered by removing from cache
domIDs = util.RemoveFromArray(domIDs, uuid)

return true
})

// TODO clean up: remove old domains
// remove cached profilers for not existent domains
for _, uuid := range domIDs {
profilerRaw, found := domainProfiler.Load(uuid)
if found {
profiler := profilerRaw.(spec.TSProfiler)
profiler.Terminate()
}
domainProfiler.Delete(uuid)
}
}
11 changes: 1 addition & 10 deletions runners/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func Lookup() {
if err != nil {
continue
}
domIDs = removeFromArray(domIDs, domain.UUID)
domIDs = util.RemoveFromArray(domIDs, domain.UUID)
}

// remove cached but not existent domains
Expand Down Expand Up @@ -111,12 +111,3 @@ func handleDomain(dom libvirt.Domain) (models.Domain, error) {

return domain, nil
}

func removeFromArray(s []string, r string) []string {
for i, v := range s {
if v == r {
return append(s[:i], s[i+1:]...)
}
}
return s
}
11 changes: 11 additions & 0 deletions util/array.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package util

// RemoveFromArray removes the element with key `r` from array `s`
func RemoveFromArray(s []string, r string) []string {
for i, v := range s {
if v == r {
return append(s[:i], s[i+1:]...)
}
}
return s
}

0 comments on commit 74ddaed

Please sign in to comment.