Skip to content

Commit

Permalink
Merge pull request #2 from yoheimuta/include-dead-children
Browse files Browse the repository at this point in the history
Option: exclude cumulative cpu time (cutime and cstime)
  • Loading branch information
tkuchiki authored Jun 19, 2016
2 parents 30f414a + 50cba71 commit a1282d5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ Linux processes metrics plugin for mackerel.io agent.
## Synopsis

```shell
mackerel-plugin-linux-proc-stats -pid=<pid>|-pidfile=<pidfile>|-process-pattern=<process pattern> [-metric-key-prefix=<metric-key-prefix>] [-tempfile=<tempfile>] [-follow-child-processes]
mackerel-plugin-linux-proc-stats -pid=<pid>|-pidfile=<pidfile>|-process-pattern=<process pattern> [-metric-key-prefix=<metric-key-prefix>] [-tempfile=<tempfile>] [-follow-child-processes] [-include-dead-children=0]
```

```shell
$ ./mackerel-plugin-linux-proc-stats --help
Usage of ./mackerel-plugin-linux-proc-stats:
-follow-child-processes
Follow child processes
-include-dead-children
Include dead kids in cpu sum (default true)
-metric-key-prefix string
Metric key prefix
-pid string
Expand Down
20 changes: 15 additions & 5 deletions linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ var cpuTick float64
var metricKey string

// CPUUsage retrieve cpu usage
func (p *Process) CPUUsage() float64 {
total := p.UTime + p.STime + p.CUTime + p.CSTime
func (p *Process) CPUUsage(includeDeadChildren bool) float64 {
total := p.UTime + p.STime
if includeDeadChildren {
total += p.CUTime + p.CSTime
}

sec := uptime - (p.StartTime / cpuTick)
return 100 * ((total / cpuTick) / sec)
}
Expand All @@ -64,6 +68,7 @@ type LinuxProcStatsPlugin struct {
Pid string
Pids []string
FollowChildProcesses bool
IncludeDeadChildren bool
}

// FetchMetrics interface for mackerelplugin
Expand Down Expand Up @@ -96,7 +101,7 @@ func (lp LinuxProcStatsPlugin) FetchMetrics() (stats map[string]interface{}, err
}
}

running, numThreads, vSize, rss, cpuUsage := sumStats(ps)
running, numThreads, vSize, rss, cpuUsage := sumStats(ps, lp.IncludeDeadChildren)
stats = make(map[string]interface{})
stats["running"] = running
stats["processes"] = float64(len(ps))
Expand Down Expand Up @@ -272,15 +277,18 @@ func childStats(ps Processes, options []string) (Processes, error) {
return ps, err
}

func sumStats(ps Processes) (running, numThreads, vSize, rss, cpuUsage float64) {
func sumStats(
ps Processes,
includeDeadChildren bool,
) (running, numThreads, vSize, rss, cpuUsage float64) {
for _, p := range ps {
if p.State == `R` {
running++
}
numThreads += p.NumThreads
vSize += p.VSize
rss += p.RSS
cpuUsage += p.CPUUsage()
cpuUsage += p.CPUUsage(includeDeadChildren)
}

return running, numThreads, vSize, rss, cpuUsage
Expand Down Expand Up @@ -381,6 +389,7 @@ func main() {
optFollowChildProcesses := flag.Bool("follow-child-processes", false, "Follow child processes")
optMetricKey := flag.String("metric-key-prefix", "", "Metric key prefix")
optVersion := flag.Bool("version", false, "Version")
optIncludeDeadChildren := flag.Bool("include-dead-children", true, "Include dead kids in cpu sum")
flag.Parse()

if *optVersion {
Expand Down Expand Up @@ -414,6 +423,7 @@ func main() {
procStats.Pid = pid
procStats.Pids = pids
procStats.FollowChildProcesses = *optFollowChildProcesses
procStats.IncludeDeadChildren = *optIncludeDeadChildren

uptime, err = getUptime()
if err != nil {
Expand Down

0 comments on commit a1282d5

Please sign in to comment.