Skip to content

Commit

Permalink
add options to control kvmprofiler; catch and print errors
Browse files Browse the repository at this point in the history
  • Loading branch information
cha87de committed Dec 17, 2018
1 parent 11ed86e commit 30a8078
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 16 deletions.
3 changes: 1 addition & 2 deletions cmd/kvmprofiler/config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package main

import (
"os"

"fmt"
"os"

"github.com/cha87de/kvmtop/collectors/cpucollector"
"github.com/cha87de/kvmtop/collectors/iocollector"
Expand Down
9 changes: 7 additions & 2 deletions config/options.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package config

// Options holds set of runtime configuration parameters
var Options struct {
// OptionsType defines the runtime configuration parameters
type OptionsType struct {
Version bool `short:"v" long:"version" description:"Show version"`
Frequency int `short:"f" long:"frequency" description:"Frequency (in seconds) for collecting metrics" default:"1"`
Runs int `short:"r" long:"runs" description:"Amount of collection runs" default:"-1"`
Expand All @@ -22,4 +22,9 @@ var Options struct {
OutputTarget string `long:"target" description:"for output 'file' the location, for 'tcp' the url to the tcp server"`

NetworkDevice string `long:"netdev" description:"The network device used for the virtual traffic"`

Profiler ProfilerOptionsType `group:"Profiler Options"`
}

// Options holds the OptionsType configuration parameters
var Options OptionsType
12 changes: 12 additions & 0 deletions config/profiler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package config

import "time"

// ProfilerOptionsType describes profiler options
type ProfilerOptionsType struct {
States int `long:"states" default:"4"`
BufferSize int `long:"buffersize" default:"10"`
History int `long:"history" default:"1"`
FilterStdDevs int `long:"filterstddevs" default:"2"`
OutputFreq time.Duration `long:"outputFreq" default:"60"`
}
31 changes: 25 additions & 6 deletions profiler/pickup.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package profiler

import (
"fmt"
"strconv"

"github.com/cha87de/kvmtop/collectors"
Expand All @@ -10,22 +11,40 @@ import (
)

func pickupCPU(domain models.Domain) int {
cputimeAllCores, _ := strconv.Atoi(cpucollector.CpuPrintThreadMetric(&domain, "cpu_threadIDs", "cpu_times"))
queuetimeAllCores, _ := strconv.Atoi(cpucollector.CpuPrintThreadMetric(&domain, "cpu_threadIDs", "cpu_runqueues"))
cputimeAllCores, err := strconv.Atoi(cpucollector.CpuPrintThreadMetric(&domain, "cpu_threadIDs", "cpu_times"))
if err != nil {
fmt.Printf("err Atiu cpu_times: %v\n", err)
}
queuetimeAllCores, err := strconv.Atoi(cpucollector.CpuPrintThreadMetric(&domain, "cpu_threadIDs", "cpu_runqueues"))
if err != nil {
fmt.Printf("err Atiu cpu_runqueues: %v\n", err)
}
cpuUtil := cputimeAllCores + queuetimeAllCores
return cpuUtil
}

func pickupIO(domain models.Domain) int {
readBytes, _ := strconv.Atoi(collectors.GetMetricDiffUint64(domain.Measurable, "io_read_bytes", true))
writtenbytes, _ := strconv.Atoi(collectors.GetMetricDiffUint64(domain.Measurable, "io_write_bytes", true))
readBytes, err := strconv.Atoi(collectors.GetMetricDiffUint64(domain.Measurable, "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))
if err != nil {
fmt.Printf("err Atiu io_write_bytes: %v\n", err)
}
total := readBytes + writtenbytes
return total
}

func pickupNet(domain models.Domain) int {
receivedBytes, _ := strconv.Atoi(collectors.GetMetricDiffUint64(domain.Measurable, "net_ReceivedBytes", true))
transmittedBytes, _ := strconv.Atoi(collectors.GetMetricDiffUint64(domain.Measurable, "net_TransmittedBytes", true))
receivedBytes, err := strconv.Atoi(collectors.GetMetricDiffUint64(domain.Measurable, "net_ReceivedBytes", true))
if err != nil {
fmt.Printf("err Atiu net_ReceivedBytes: %v\n", err)
}
transmittedBytes, err := strconv.Atoi(collectors.GetMetricDiffUint64(domain.Measurable, "net_TransmittedBytes", true))
if err != nil {
fmt.Printf("err Atiu net_TransmittedBytes: %v\n", err)
}
total := receivedBytes + transmittedBytes
// max, _ := strconv.Atoi(collectors.GetMetricUint64(models.Collection.Host.Measurable, "net_host_speed", 0)) // MBit
// max = max * 1024 * 1024 / 8 // to Byte
Expand Down
10 changes: 7 additions & 3 deletions profiler/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package profiler

import (
"encoding/json"
"fmt"

"github.com/cha87de/kvmtop/printers"
"github.com/cha87de/tsprofiler/spec"
Expand All @@ -16,8 +17,11 @@ func profileOutput(data spec.TSProfile) {
profilerOutput := profilerOutput{
Profile: data,
}
json, _ := json.Marshal(profilerOutput)
json, err := json.Marshal(profilerOutput)
// fmt.Printf("%s\n", json)

printers.Output(string(json) + "\n")
if err != nil {
fmt.Printf("Error while marshaling profiler output (%+v): %+v", profilerOutput, err)
} else {
printers.Output(string(json) + "\n")
}
}
11 changes: 8 additions & 3 deletions profiler/profiler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package profiler

import (
"fmt"
"sync"
"time"

Expand Down Expand Up @@ -54,9 +55,11 @@ func pickup() {
} else {
profiler = impl.NewProfiler(spec.Settings{
Name: uuid,
BufferSize: config.Options.Frequency * 10,
States: 4,
OutputFreq: time.Duration(20) * time.Second,
BufferSize: config.Options.Profiler.BufferSize,
States: config.Options.Profiler.States,
History: config.Options.Profiler.History,
FilterStdDevs: config.Options.Profiler.FilterStdDevs,
OutputFreq: config.Options.Profiler.OutputFreq,
OutputCallback: profileOutput,
})
}
Expand All @@ -68,11 +71,13 @@ func pickup() {
var util int
if name == "cpu" {
util = pickupCPU(domain)
fmt.Printf("cpu util: %d\n", util)
} else if name == "io" {
util = pickupIO(domain)
} else if name == "net" {
util = pickupNet(domain)
}

metrics = append(metrics, spec.TSDataMetric{
Name: name,
Value: float64(util),
Expand Down

0 comments on commit 30a8078

Please sign in to comment.