Skip to content

Commit

Permalink
Add support for VmSwap. Fixes #33.
Browse files Browse the repository at this point in the history
  • Loading branch information
ncabatoff committed Aug 5, 2018
1 parent 0b76aee commit b620179
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 26 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ Number of bytes of memory used. The extra label `memtype` can have two values:
*virtual*: Field vsize(23) from /proc/[pid]/stat, virtual memory size.

*swapped*: Field VmSwap from /proc/[pid]/status, translated from KB to bytes.

### open_filedesc gauge

Number of file descriptors, based on counting how many entries are in the directory
Expand Down
2 changes: 2 additions & 0 deletions cmd/process-exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,8 @@ func (p *NamedProcessCollector) scrape(ch chan<- prometheus.Metric) {
prometheus.GaugeValue, float64(gcounts.Memory.ResidentBytes), gname, "resident")
ch <- prometheus.MustNewConstMetric(membytesDesc,
prometheus.GaugeValue, float64(gcounts.Memory.VirtualBytes), gname, "virtual")
ch <- prometheus.MustNewConstMetric(membytesDesc,
prometheus.GaugeValue, float64(gcounts.Memory.VmSwapBytes), gname, "swapped")
ch <- prometheus.MustNewConstMetric(startTimeDesc,
prometheus.GaugeValue, float64(gcounts.OldestStartTime.Unix()), gname)
ch <- prometheus.MustNewConstMetric(openFDsDesc,
Expand Down
2 changes: 1 addition & 1 deletion fixtures/14804/status
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ VmExe: 3692 kB
VmLib: 0 kB
VmPTE: 48 kB
VmPMD: 20 kB
VmSwap: 0 kB
VmSwap: 10 kB
HugetlbPages: 0 kB
Threads: 7
SigQ: 0/31421
Expand Down
1 change: 1 addition & 0 deletions proc/grouper.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func groupadd(grp Group, ts Update) Group {
grp.Procs++
grp.Memory.ResidentBytes += ts.Memory.ResidentBytes
grp.Memory.VirtualBytes += ts.Memory.VirtualBytes
grp.Memory.VmSwapBytes += ts.Memory.VmSwapBytes
if ts.Filedesc.Open != -1 {
grp.OpenFDs += uint64(ts.Filedesc.Open)
}
Expand Down
42 changes: 21 additions & 21 deletions proc/grouper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,30 @@ func TestGrouperBasic(t *testing.T) {
}{
{
[]IDInfo{
piinfost(p1, n1, Counts{1, 2, 3, 4, 5, 6}, Memory{7, 8},
piinfost(p1, n1, Counts{1, 2, 3, 4, 5, 6}, Memory{7, 8, 0},
Filedesc{4, 400}, 2, States{Other: 1}),
piinfost(p2, n2, Counts{2, 3, 4, 5, 6, 7}, Memory{8, 9},
piinfost(p2, n2, Counts{2, 3, 4, 5, 6, 7}, Memory{8, 9, 0},
Filedesc{40, 400}, 3, States{Waiting: 1}),
},
GroupByName{
"g1": Group{Counts{}, States{Other: 1}, 1, Memory{7, 8}, starttime,
"g1": Group{Counts{}, States{Other: 1}, 1, Memory{7, 8, 0}, starttime,
4, 0.01, 2, nil},
"g2": Group{Counts{}, States{Waiting: 1}, 1, Memory{8, 9}, starttime,
"g2": Group{Counts{}, States{Waiting: 1}, 1, Memory{8, 9, 0}, starttime,
40, 0.1, 3, nil},
},
},
{
[]IDInfo{
piinfost(p1, n1, Counts{2, 3, 4, 5, 6, 7},
Memory{6, 7}, Filedesc{100, 400}, 4, States{Zombie: 1}),
Memory{6, 7, 0}, Filedesc{100, 400}, 4, States{Zombie: 1}),
piinfost(p2, n2, Counts{4, 5, 6, 7, 8, 9},
Memory{9, 8}, Filedesc{400, 400}, 2, States{Running: 1}),
Memory{9, 8, 0}, Filedesc{400, 400}, 2, States{Running: 1}),
},
GroupByName{
"g1": Group{Counts{1, 1, 1, 1, 1, 1}, States{Zombie: 1}, 1,
Memory{6, 7}, starttime, 100, 0.25, 4, nil},
Memory{6, 7, 0}, starttime, 100, 0.25, 4, nil},
"g2": Group{Counts{2, 2, 2, 2, 2, 2}, States{Running: 1}, 1,
Memory{9, 8}, starttime, 400, 1, 2, nil},
Memory{9, 8, 0}, starttime, 400, 1, 2, nil},
},
},
}
Expand All @@ -95,35 +95,35 @@ func TestGrouperProcJoin(t *testing.T) {
}{
{
[]IDInfo{
piinfo(p1, n1, Counts{1, 2, 3, 4, 5, 6}, Memory{3, 4}, Filedesc{4, 400}, 2),
piinfo(p1, n1, Counts{1, 2, 3, 4, 5, 6}, Memory{3, 4, 0}, Filedesc{4, 400}, 2),
},
GroupByName{
"g1": Group{Counts{}, States{}, 1, Memory{3, 4}, starttime, 4, 0.01, 2, nil},
"g1": Group{Counts{}, States{}, 1, Memory{3, 4, 0}, starttime, 4, 0.01, 2, nil},
},
}, {
// The counts for pid2 won't be factored into the total yet because we only add
// to counts starting with the second time we see a proc. Memory and FDs are
// affected though.
[]IDInfo{
piinfost(p1, n1, Counts{3, 4, 5, 6, 7, 8},
Memory{3, 4}, Filedesc{4, 400}, 2, States{Running: 1}),
Memory{3, 4, 0}, Filedesc{4, 400}, 2, States{Running: 1}),
piinfost(p2, n2, Counts{1, 1, 1, 1, 1, 1},
Memory{1, 2}, Filedesc{40, 400}, 3, States{Sleeping: 1}),
Memory{1, 2, 0}, Filedesc{40, 400}, 3, States{Sleeping: 1}),
},
GroupByName{
"g1": Group{Counts{2, 2, 2, 2, 2, 2}, States{Running: 1, Sleeping: 1}, 2,
Memory{4, 6}, starttime, 44, 0.1, 5, nil},
Memory{4, 6, 0}, starttime, 44, 0.1, 5, nil},
},
}, {
[]IDInfo{
piinfost(p1, n1, Counts{4, 5, 6, 7, 8, 9},
Memory{1, 5}, Filedesc{4, 400}, 2, States{Running: 1}),
Memory{1, 5, 0}, Filedesc{4, 400}, 2, States{Running: 1}),
piinfost(p2, n2, Counts{2, 2, 2, 2, 2, 2},
Memory{2, 4}, Filedesc{40, 400}, 3, States{Running: 1}),
Memory{2, 4, 0}, Filedesc{40, 400}, 3, States{Running: 1}),
},
GroupByName{
"g1": Group{Counts{4, 4, 4, 4, 4, 4}, States{Running: 2}, 2,
Memory{3, 9}, starttime, 44, 0.1, 5, nil},
Memory{3, 9, 0}, starttime, 44, 0.1, 5, nil},
},
},
}
Expand All @@ -150,18 +150,18 @@ func TestGrouperNonDecreasing(t *testing.T) {
}{
{
[]IDInfo{
piinfo(p1, n1, Counts{3, 4, 5, 6, 7, 8}, Memory{3, 4}, Filedesc{4, 400}, 2),
piinfo(p2, n2, Counts{1, 1, 1, 1, 1, 1}, Memory{1, 2}, Filedesc{40, 400}, 3),
piinfo(p1, n1, Counts{3, 4, 5, 6, 7, 8}, Memory{3, 4, 0}, Filedesc{4, 400}, 2),
piinfo(p2, n2, Counts{1, 1, 1, 1, 1, 1}, Memory{1, 2, 0}, Filedesc{40, 400}, 3),
},
GroupByName{
"g1": Group{Counts{}, States{}, 2, Memory{4, 6}, starttime, 44, 0.1, 5, nil},
"g1": Group{Counts{}, States{}, 2, Memory{4, 6, 0}, starttime, 44, 0.1, 5, nil},
},
}, {
[]IDInfo{
piinfo(p1, n1, Counts{4, 5, 6, 7, 8, 9}, Memory{1, 5}, Filedesc{4, 400}, 2),
piinfo(p1, n1, Counts{4, 5, 6, 7, 8, 9}, Memory{1, 5, 0}, Filedesc{4, 400}, 2),
},
GroupByName{
"g1": Group{Counts{1, 1, 1, 1, 1, 1}, States{}, 1, Memory{1, 5}, starttime, 4, 0.01, 2, nil},
"g1": Group{Counts{1, 1, 1, 1, 1, 1}, States{}, 1, Memory{1, 5, 0}, starttime, 4, 0.01, 2, nil},
},
}, {
[]IDInfo{},
Expand Down
7 changes: 7 additions & 0 deletions proc/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type (
Memory struct {
ResidentBytes uint64
VirtualBytes uint64
VmSwapBytes uint64
}

// Filedesc describes a proc's file descriptor usage and soft limit.
Expand Down Expand Up @@ -403,6 +404,11 @@ func (p proc) GetMetrics() (Metrics, int, error) {
// Ditto for states
states, _ := p.GetStates()

status, err := p.getStatus()
if err != nil {
return Metrics{}, 0, err
}

numfds, err := p.Proc.FileDescriptorsLen()
if err != nil {
numfds = -1
Expand All @@ -419,6 +425,7 @@ func (p proc) GetMetrics() (Metrics, int, error) {
Memory: Memory{
ResidentBytes: uint64(stat.ResidentMemory()),
VirtualBytes: uint64(stat.VirtualMemory()),
VmSwapBytes: uint64(status.VmSwapKB * 1024),
},
Filedesc: Filedesc{
Open: int64(numfds),
Expand Down
1 change: 1 addition & 0 deletions proc/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func TestReadFixture(t *testing.T) {
Memory: Memory{
ResidentBytes: 0x7b1000,
VirtualBytes: 0x1061000,
VmSwapBytes: 0x2800,
},
Filedesc: Filedesc{
Open: 5,
Expand Down
8 changes: 4 additions & 4 deletions proc/tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ func TestTrackerMetrics(t *testing.T) {
want Update
}{
{
piinfost(p, n, Counts{1, 2, 3, 4, 5, 6}, Memory{7, 8},
piinfost(p, n, Counts{1, 2, 3, 4, 5, 6}, Memory{7, 8, 0},
Filedesc{1, 10}, 9, States{Sleeping: 1}),
Update{n, Delta{}, Memory{7, 8}, Filedesc{1, 10}, tm,
Update{n, Delta{}, Memory{7, 8, 0}, Filedesc{1, 10}, tm,
9, States{Sleeping: 1}, nil},
},
{
piinfost(p, n, Counts{2, 3, 4, 5, 6, 7}, Memory{1, 2},
piinfost(p, n, Counts{2, 3, 4, 5, 6, 7}, Memory{1, 2, 0},
Filedesc{2, 20}, 1, States{Running: 1}),
Update{n, Delta{1, 1, 1, 1, 1, 1}, Memory{1, 2},
Update{n, Delta{1, 1, 1, 1, 1, 1}, Memory{1, 2, 0},
Filedesc{2, 20}, tm, 1, States{Running: 1}, nil},
},
}
Expand Down

0 comments on commit b620179

Please sign in to comment.