Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimise processTopology() #3074

Merged
merged 3 commits into from
Feb 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions probe/process/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,35 +85,34 @@ func (r *Reporter) processTopology() (report.Topology, error) {
pidstr := strconv.Itoa(p.PID)
nodeID := report.MakeProcessNodeID(r.scope, pidstr)
node := report.MakeNode(nodeID)
for _, tuple := range []struct{ key, value string }{
{PID, pidstr},
{Name, p.Name},
{Threads, strconv.Itoa(p.Threads)},
} {
if tuple.value != "" {
node = node.WithLatests(map[string]string{tuple.key: tuple.value})
}
node = node.WithLatest(PID, now, pidstr)
node = node.WithLatest(Threads, now, strconv.Itoa(p.Threads))
if p.Name != "" {
node = node.WithLatest(Name, now, p.Name)
}

if p.Cmdline != "" {
if r.noCommandLineArguments {
node = node.WithLatests(map[string]string{Cmdline: strings.Split(p.Cmdline, " ")[0]})
node = node.WithLatest(Cmdline, now, strings.Split(p.Cmdline, " ")[0])
} else {
node = node.WithLatests(map[string]string{Cmdline: p.Cmdline})
node = node.WithLatest(Cmdline, now, p.Cmdline)
}
}

if p.PPID > 0 {
node = node.WithLatests(map[string]string{PPID: strconv.Itoa(p.PPID)})
node = node.WithLatest(PPID, now, strconv.Itoa(p.PPID))
}

var metrics = report.Metrics{
MemoryUsage: report.MakeSingletonMetric(now, float64(p.RSSBytes)).WithMax(float64(p.RSSBytesLimit)),
OpenFilesCount: report.MakeSingletonMetric(now, float64(p.OpenFilesCount)).WithMax(float64(p.OpenFilesLimit)),
}
if deltaTotal > 0 {
cpuUsage := float64(p.Jiffies-prev.Jiffies) / float64(deltaTotal) * 100.
node = node.WithMetric(CPUUsage, report.MakeSingletonMetric(now, cpuUsage).WithMax(maxCPU))
metrics[CPUUsage] = report.MakeSingletonMetric(now, cpuUsage).WithMax(maxCPU)
}

node = node.WithMetric(MemoryUsage, report.MakeSingletonMetric(now, float64(p.RSSBytes)).WithMax(float64(p.RSSBytesLimit)))
node = node.WithMetric(OpenFilesCount, report.MakeSingletonMetric(now, float64(p.OpenFilesCount)).WithMax(float64(p.OpenFilesLimit)))
node = node.WithMetrics(metrics)

t.AddNode(node)
})
Expand Down
14 changes: 14 additions & 0 deletions probe/process/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,17 @@ func TestCmdlineRemoval(t *testing.T) {
}
testReporter(t, true, test)
}

func BenchmarkReporter(t *testing.B) {
walker := &mockWalker{processes: processes}
getDeltaTotalJiffies := func() (uint64, float64, error) { return 0, 0., nil }
reporter := process.NewReporter(walker, "", getDeltaTotalJiffies, false)
t.ResetTimer()

for i := 0; i < t.N; i++ {
_, err := reporter.Report()
if err != nil {
t.Error(err)
}
}
}
6 changes: 6 additions & 0 deletions report/controls.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ type Control struct {

// Merge merges other with cs, returning a fresh Controls.
func (cs Controls) Merge(other Controls) Controls {
if len(other) > len(cs) {
cs, other = other, cs
}
if len(other) == 0 {
return cs
}
result := cs.Copy()
for k, v := range other {
result[k] = v
Expand Down
6 changes: 3 additions & 3 deletions report/metadata_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ func (e MetadataTemplates) Copy() MetadataTemplates {
// Merge merges two sets of MetadataTemplates so far just ignores based
// on duplicate id key
func (e MetadataTemplates) Merge(other MetadataTemplates) MetadataTemplates {
if e == nil && other == nil {
return nil
}
if len(other) > len(e) {
e, other = other, e
}
if len(other) == 0 {
return e
}
result := e.Copy()
for k, v := range other {
if existing, ok := result[k]; !ok || existing.Priority < v.Priority {
Expand Down
3 changes: 3 additions & 0 deletions report/metric_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ func (e MetricTemplates) Merge(other MetricTemplates) MetricTemplates {
if len(other) > len(e) {
e, other = other, e
}
if len(other) == 0 {
return e
}
result := e.Copy()
for k, v := range other {
if existing, ok := result[k]; !ok || existing.Priority < v.Priority {
Expand Down
3 changes: 3 additions & 0 deletions report/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ func (m Metrics) Merge(other Metrics) Metrics {
if len(other) > len(m) {
m, other = other, m
}
if len(other) == 0 {
return m
}
result := m.Copy()
for k, v := range other {
if rv, ok := result[k]; ok {
Expand Down
3 changes: 3 additions & 0 deletions report/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ func (n Nodes) Merge(other Nodes) Nodes {
if len(other) > len(n) {
n, other = other, n
}
if len(other) == 0 {
return n
}
cp := n.Copy()
for k, v := range other {
if n, ok := cp[k]; ok { // don't overwrite
Expand Down